({
diagram,
targetDatabaseType,
isDBMLFlow = false,
onlyRelationships = false,
skipFKGeneration = false,
}: {
diagram: Diagram;
targetDatabaseType: DatabaseType;
isDBMLFlow?: boolean;
onlyRelationships?: boolean;
skipFKGeneration?: boolean;
})
| 152 | }; |
| 153 | |
| 154 | export const exportBaseSQL = ({ |
| 155 | diagram, |
| 156 | targetDatabaseType, |
| 157 | isDBMLFlow = false, |
| 158 | onlyRelationships = false, |
| 159 | skipFKGeneration = false, |
| 160 | }: { |
| 161 | diagram: Diagram; |
| 162 | targetDatabaseType: DatabaseType; |
| 163 | isDBMLFlow?: boolean; |
| 164 | onlyRelationships?: boolean; |
| 165 | skipFKGeneration?: boolean; |
| 166 | }): string => { |
| 167 | const { tables, relationships } = diagram; |
| 168 | |
| 169 | if (!tables || tables.length === 0) { |
| 170 | return ''; |
| 171 | } |
| 172 | |
| 173 | if (!isDBMLFlow && diagram.databaseType === targetDatabaseType) { |
| 174 | switch (diagram.databaseType) { |
| 175 | case DatabaseType.SQL_SERVER: |
| 176 | return exportMSSQL({ diagram, onlyRelationships }); |
| 177 | case DatabaseType.POSTGRESQL: |
| 178 | return exportPostgreSQL({ diagram, onlyRelationships }); |
| 179 | case DatabaseType.SQLITE: |
| 180 | return exportSQLite({ diagram, onlyRelationships }); |
| 181 | case DatabaseType.MYSQL: |
| 182 | case DatabaseType.MARIADB: |
| 183 | return exportMySQL({ diagram, onlyRelationships }); |
| 184 | default: |
| 185 | return exportPostgreSQL({ diagram, onlyRelationships }); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Deterministic cross-dialect exports (PostgreSQL to MySQL/SQL Server) |
| 190 | // These do not use LLM and provide consistent, predictable output |
| 191 | if (!isDBMLFlow && diagram.databaseType === DatabaseType.POSTGRESQL) { |
| 192 | if ( |
| 193 | targetDatabaseType === DatabaseType.MYSQL || |
| 194 | targetDatabaseType === DatabaseType.MARIADB |
| 195 | ) { |
| 196 | return exportPostgreSQLToMySQL({ diagram, onlyRelationships }); |
| 197 | } |
| 198 | if (targetDatabaseType === DatabaseType.SQL_SERVER) { |
| 199 | return exportPostgreSQLToMSSQL({ diagram, onlyRelationships }); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Filter out the tables that are views |
| 204 | const nonViewTables = tables.filter((table) => !table.isView); |
| 205 | |
| 206 | // Align the data types based on foreign key relationships |
| 207 | alignForeignKeyDataTypes(diagram); |
| 208 | |
| 209 | // Initialize the SQL script string |
| 210 | let sqlScript = ''; |
| 211 |
no test coverage detected