Update JSON data type matcher
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
import { querySchemaRows } from "./run-schema-query";
|
||||
import schemaCondition from "./schema-condition";
|
||||
export default async function getTableColumnsGemini({ tableName, config, }) {
|
||||
const schemaCond = schemaCondition(config);
|
||||
const rows = await querySchemaRows({
|
||||
query: `
|
||||
SELECT
|
||||
c.COLUMN_NAME,
|
||||
c.COLUMN_TYPE,
|
||||
c.COLUMN_COMMENT,
|
||||
c.IS_NULLABLE,
|
||||
c.COLUMN_DEFAULT,
|
||||
c.EXTRA,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.CHECK_CONSTRAINTS cc
|
||||
WHERE cc.CONSTRAINT_SCHEMA = c.TABLE_SCHEMA
|
||||
AND cc.TABLE_NAME = c.TABLE_NAME
|
||||
AND cc.CHECK_CLAUSE LIKE CONCAT('%json_valid(\`', c.COLUMN_NAME, '\`)%')
|
||||
) AS IS_JSON
|
||||
FROM information_schema.COLUMNS c
|
||||
WHERE ${schemaCond.where.replace(/\bTABLE_SCHEMA\b/g, "c.TABLE_SCHEMA")}
|
||||
AND c.TABLE_NAME = ?
|
||||
ORDER BY c.ORDINAL_POSITION
|
||||
`,
|
||||
values: [...schemaCond.values, tableName],
|
||||
config,
|
||||
});
|
||||
return rows.map((row) => {
|
||||
const isJson = Number(row.IS_JSON) === 1;
|
||||
// Normalize longtext with a json_valid constraint to "json"
|
||||
let resolvedType = row.COLUMN_TYPE;
|
||||
if (isJson && row.COLUMN_TYPE.toLowerCase() === "longtext") {
|
||||
resolvedType = "json";
|
||||
}
|
||||
return {
|
||||
name: row.COLUMN_NAME,
|
||||
type: resolvedType,
|
||||
comment: row.COLUMN_COMMENT,
|
||||
isNullable: String(row.IS_NULLABLE).toUpperCase() === "YES",
|
||||
columnDefault: row.COLUMN_DEFAULT,
|
||||
extra: row.EXTRA || "",
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user