Mysql Datetime data types modifiers implemented

This commit is contained in:
KarimTamani
2025-06-16 19:25:02 +01:00
parent 84dfd0e9d5
commit 3ec28c3232
13 changed files with 619 additions and 133 deletions
+27 -8
View File
@@ -1,7 +1,7 @@
import { DatabaseDialect } from "@/lib/database";
import { Modifiers } from "@/lib/field";
import { DataTypes, Modifiers, TimeDefaultValues } from "@/lib/field";
import { DataType } from "@/lib/schemas/data-type-schema";
import { DatabaseType } from "@/lib/schemas/database-schema";
import { FieldType } from "@/lib/schemas/field-schema";
@@ -26,8 +26,6 @@ export const DatabaseToAst = (database: DatabaseType, data_types: DataType[]) =>
const sortedTables: TableType[] = sortedTablesIds.map((id: string) =>
renderableTables.find((table: TableType) => table.id == id) as TableType
);
for (const table of sortedTables) {
dbAst.push(TableToAst(table, data_types));
@@ -90,7 +88,7 @@ export const FieldToAst = (field: FieldType, ignorePrimaryKey: boolean = false)
let length: number | null = null;
let scale: number | null = null;
let scale: number | string | null = null;
let character_set: any | null = null;
let collate: any | null = null;
@@ -100,8 +98,11 @@ export const FieldToAst = (field: FieldType, ignorePrimaryKey: boolean = false)
let default_val: any | null = null;
if (modifiers.includes(Modifiers.PRECISION) && field.precision)
if (modifiers.includes(Modifiers.PRECISION) && field.precision) {
length = field.precision;
if (modifiers.includes(Modifiers.SCALE) && !field.scale)
scale = "0";
}
if (modifiers.includes(Modifiers.SCALE) && field.scale)
scale = field.scale;
@@ -153,13 +154,31 @@ export const FieldToAst = (field: FieldType, ignorePrimaryKey: boolean = false)
value: field.defaultValue
}
}
if (field.defaultValue == "true" || field.defaultValue == "false") {
if (field.type.type == DataTypes.TIME && field.defaultValue == TimeDefaultValues.NOW)
default_val = {
type: "default",
value: {
type: "function",
name: {
name: [
{
type: "origin",
value: "CURRENT_TIMESTAMP"
}
]
},
over: null
}
}
else if (field.defaultValue == "true" || field.defaultValue == "false") {
default_val.value.type = "bool"
default_val.value.value = Boolean(field.defaultValue);
default_val.value.value = field.defaultValue == "true" ? true : false;
}
// Check if it's a number (but not empty string or just whitespace)
if (!isNaN(Number(field.defaultValue))) {
else if (!isNaN(Number(field.defaultValue))) {
default_val.value.type = "number"
default_val.value.value = Number(field.defaultValue);
}
+11 -13
View File
@@ -8,10 +8,10 @@ export function fixCharsetPlacement(sql: string): string {
const lines = sql.split('\n');
let currentColumn = '';
const result: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
// Handle table start/end and other non-column lines
if (isTableStructureLine(trimmed)) {
if (currentColumn) {
@@ -21,7 +21,7 @@ export function fixCharsetPlacement(sql: string): string {
result.push(line);
continue;
}
// Handle column definitions
if (trimmed.endsWith(',')) {
currentColumn += ' ' + trimmed.slice(0, -1);
@@ -31,12 +31,10 @@ export function fixCharsetPlacement(sql: string): string {
currentColumn += ' ' + trimmed;
}
}
// Process any remaining column
if (currentColumn) {
result.push(processColumn(currentColumn));
}
return result.join('\n');
}
@@ -44,11 +42,11 @@ export function fixCharsetPlacement(sql: string): string {
* Checks if a line is part of table structure (not a column definition)
*/
function isTableStructureLine(line: string): boolean {
return line.startsWith('CREATE TABLE') ||
line === '(' ||
line === ')' ||
line.endsWith('(') ||
line.endsWith(')');
return line.startsWith('CREATE TABLE') ||
line === '(' ||
line === ')' ||
line.endsWith('(') ||
line.endsWith(')');
}
/**
@@ -62,18 +60,18 @@ function processColumn(columnDef: string): string {
const [_, colName, dataType] = typeMatch;
const rest = columnDef.slice(typeMatch[0].length);
// Extract charset and collate
const charsetMatch = rest.match(/CHARACTER\s+SET\s+\S+/i);
const collateMatch = rest.match(/COLLATE\s+\S+/i);
// Clean the remaining attributes
const cleanRest = rest
.replace(/CHARACTER\s+SET\s+\S+/gi, '')
.replace(/COLLATE\s+\S+/gi, '')
.replace(/\s+/g, ' ')
.trim();
// Reconstruct in correct order
let reconstructed = `${colName} ${dataType}`;
if (charsetMatch) reconstructed += ` ${charsetMatch[0]}`;
+1 -2
View File
@@ -64,8 +64,7 @@ function groupBy(array: any[], key: string) {
return acc;
}, {});
}
export {
areArraysEqual,