fix the prompt

This commit is contained in:
johnnyfish
2024-08-25 00:56:38 +03:00
parent 0e4dcfdd49
commit 010ba2e58d

View File

@@ -195,7 +195,7 @@ function alignForeignKeyDataTypes(diagram: Diagram) {
});
}
const generateSQLPrompt = (databaseType: string, sqlScript: string) => {
const generateSQLPrompt = (databaseType: DatabaseType, sqlScript: string) => {
const basePrompt = `
You are generating SQL scripts for creating database tables and sequences, handling primary keys, indices, and other table attributes.
The following instructions will guide you in optimizing the scripts for the ${databaseType} dialect:
@@ -203,15 +203,16 @@ const generateSQLPrompt = (databaseType: string, sqlScript: string) => {
- **Column Name Conflicts**: When a column name conflicts with a data type or reserved keyword (e.g., fulltext), escape the column name by enclosing it.
`;
const dialectInstructions =
const dialectInstructionMap: Record<DatabaseType, string> =
{
POSTGRESQL: `
generic:'',
postgresql: `
- **Sequence Creation**: Use \`CREATE SEQUENCE IF NOT EXISTS\` for sequence creation.
- **Table and Index Creation**: Use \`CREATE TABLE IF NOT EXISTS\` and \`CREATE INDEX IF NOT EXISTS\` to avoid errors if the object already exists.
- **Serial and Identity Columns**: For auto-increment columns, use \`SERIAL\` or \`GENERATED BY DEFAULT AS IDENTITY\`.
- **Conditional Statements**: Utilize PostgreSQLs support for \`IF NOT EXISTS\` in relevant \`CREATE\` statements.
`,
MYSQL: `
 mysql: `
- **Table Creation**: Use \`CREATE TABLE IF NOT EXISTS\` for creating tables. While creating the table structure, ensure that all foreign key columns use the correct data types as determined in the foreign key review.
- **Auto-Increment**: Use \`AUTO_INCREMENT\` for auto-incrementing primary key columns.
- **Index Creation**: Place all \`CREATE INDEX\` statements separately after the \`CREATE TABLE\` statement. Avoid using \`IF NOT EXISTS\` in \`CREATE INDEX\` statements.
@@ -226,13 +227,13 @@ const generateSQLPrompt = (databaseType: string, sqlScript: string) => {
**Reminder**: Ensure all column names that conflict with reserved keywords or data types (like \`fulltext\`) are escaped using backticks (\`).
`,
SQL_SERVER: `
sql_server: `
- **Sequence Creation**: Use \`CREATE SEQUENCE\` without \`IF NOT EXISTS\`, and employ conditional logic (\`IF NOT EXISTS\`) to check for sequence existence before creation.
- **Identity Columns**: Always prefer using the \`IDENTITY\` keyword (e.g., \`INT IDENTITY(1,1)\`) for auto-incrementing primary key columns when possible.
- **Conditional Logic**: Use a conditional block like \`IF NOT EXISTS (SELECT * FROM sys.objects WHERE ...)\` since SQL Server doesnt support \`IF NOT EXISTS\` directly in \`CREATE\` statements.
- **Avoid Unsupported Syntax**: Ensure the script does not include unsupported statements like \`CREATE TABLE IF NOT EXISTS\`.
`,
MARIADB: `
mariadb: `
- **Table Creation**: Use \`CREATE TABLE IF NOT EXISTS\` for creating tables. While creating the table structure, ensure that all foreign key columns use the correct data types as determined in the foreign key review.
- **Auto-Increment**: Use \`AUTO_INCREMENT\` for auto-incrementing primary key columns.
- **Index Creation**: Place all \`CREATE INDEX\` statements separately after the \`CREATE TABLE\` statement. Avoid using \`IF NOT EXISTS\` in \`CREATE INDEX\` statements.
@@ -247,7 +248,7 @@ const generateSQLPrompt = (databaseType: string, sqlScript: string) => {
**Reminder**: Ensure all column names that conflict with reserved keywords or data types (like \`fulltext\`) are escaped using backticks (\`).
`,
SQLITE: `
sqlite: `
- **Table Creation**: Use \`CREATE TABLE IF NOT EXISTS\`.
- **Auto-Increment**: Use \`AUTOINCREMENT\` with \`INTEGER PRIMARY KEY\` for auto-increment functionality.
- **No Sequence Support**: SQLite does not support sequences; rely solely on \`AUTOINCREMENT\` for similar functionality.
@@ -256,7 +257,9 @@ const generateSQLPrompt = (databaseType: string, sqlScript: string) => {
- **General SQLite Constraints**: Remember, \`ALTER TABLE\` in SQLite is limited and cannot add constraints after the table is created.
- **Conditional Logic**: Ensure the script uses SQLite-compatible syntax and does not include unsupported features.
`,
}[databaseType] || '';
};
const dialectInstruction:string = dialectInstructionMap[databaseType] ?? '';
const additionalInstructions = `
**Provide just the SQL commands without markdown tags.**
@@ -268,7 +271,7 @@ const generateSQLPrompt = (databaseType: string, sqlScript: string) => {
Feel free to suggest corrections for suspected typos.
`;
return `${basePrompt}\n${dialectInstructions}\n
return `${basePrompt}\n${dialectInstruction}\n
- **Validation**: After generating the script, validate it against the respective SQL dialect by attempting to execute it in a corresponding database environment.
- **Syntax Checking**: Use SQL linting tools specific to each dialect to ensure the script is free from syntax errors.
- **Manual Review**: Include a step where a knowledgeable developer reviews the generated script to ensure it meets the required specifications and adheres to best practices.