-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tanmay Vaij <[email protected]>
- Loading branch information
1 parent
ae31c68
commit 939a152
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Request, Response } from "express"; | ||
import { db } from "../config/db"; | ||
|
||
export const getTableColumnsController = (req: Request, res: Response) => { | ||
const { tableName } = req.query; | ||
|
||
// Validate input | ||
if (!tableName || typeof tableName !== "string") { | ||
res.status(400).json({ | ||
isSuccess: false, | ||
message: "Table name is required and should be a string.", | ||
}); | ||
return | ||
} | ||
|
||
// SQL query to retrieve columns | ||
const query = ` | ||
SELECT COLUMN_NAME | ||
FROM INFORMATION_SCHEMA.COLUMNS | ||
WHERE TABLE_NAME = ? | ||
`; | ||
|
||
// Execute the query | ||
db.query(query, [tableName], (err, results: any[]) => { | ||
if (err) { | ||
console.error("Error retrieving columns:", err); | ||
return res.status(500).json({ | ||
isSuccess: false, | ||
error: err.message, | ||
}); | ||
} | ||
|
||
// Check if the table exists | ||
if (results.length === 0) { | ||
return res.status(404).json({ | ||
isSuccess: false, | ||
message: `Table '${tableName}' not found.`, | ||
}); | ||
} | ||
|
||
// Return the columns | ||
const columns = results.map((row: any) => row.COLUMN_NAME); | ||
|
||
return res.status(200).json({ | ||
isSuccess: true, | ||
columns: columns, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters