Azpect3120 b5159e8976 FEAT: Support for mysql and mariadb!!
However, the enum tree is not supported since they do not support them.
But other than that, mysql and maria DB seem to both be supported.
2024-08-19 17:26:19 -07:00

20 lines
3.5 KiB
Go

package query
// PostgreSQL queries
// Drivers: postgres
const GET_TABLE_LIST_PSQL string = `SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';`
const GET_TABLE_PK_PSQL string = `SELECT kcu.column_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema WHERE tc.constraint_type = 'PRIMARY KEY' AND tc.table_name = '%s';`
const GET_TABLE_FKS_PSQL string = `SELECT tc.table_schema, tc.table_name, kcu.column_name, ccu.table_schema AS foreign_table_schema, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s';`
const GET_TABLE_RESTRAINS_PSQL string = `SELECT c.column_name, c.is_nullable, c.data_type, c.character_maximum_length, t.typname AS enum_type FROM information_schema.columns c JOIN pg_type t ON c.udt_name = t.typname WHERE c.table_name = '%s';`
const GET_TABLE_UNIQUE_COLS_PSQL string = `SELECT kcu.column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema WHERE tc.constraint_type = 'UNIQUE' AND kcu.table_name = '%s';`
const GET_ENUM_LIST_PSQL string = `SELECT t.typname AS enum_name, e.enumlabel AS enum_value FROM pg_type t JOIN pg_enum e ON t.oid = e.enumtypid JOIN pg_namespace n ON n.oid = t.typnamespace WHERE t.typcategory = 'E' AND n.nspname NOT IN ('pg_catalog', 'information_schema') ORDER BY t.typname, e.enumsortorder;`
// MySQL queries
// Drivers: mysql, mariadb
const GET_TABLE_LIST_MYSQL string = `SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE();`
const GET_TABLE_PK_MYSQL string = `SELECT column_name FROM information_schema.key_column_usage WHERE table_schema = DATABASE() AND table_name = '%s' AND constraint_name = 'PRIMARY';`
const GET_TABLE_FKS_MYSQL string = `SELECT tc.constraint_schema AS table_schema, tc.table_name, kcu.column_name, kcu.referenced_table_schema AS foreign_table_schema, kcu.referenced_table_name AS foreign_table_name, kcu.referenced_column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.constraint_schema = kcu.constraint_schema WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s';`
const GET_TABLE_RESTRAINS_MYSQL string = `SELECT c.column_name, c.is_nullable, c.data_type, CAST(c.character_maximum_length AS UNSIGNED) AS max_length, CASE WHEN c.data_type = 'enum' THEN SUBSTRING(c.column_type, 6, LENGTH(c.column_type) - 6 - 1) ELSE NULL END AS enum_type FROM information_schema.columns c WHERE c.table_name = '%s' AND c.table_schema = DATABASE();`
const GET_TABLE_UNIQUE_COLS_MYSQL string = `SELECT kcu.column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.constraint_schema = kcu.table_schema WHERE tc.constraint_type = 'UNIQUE' AND kcu.table_name = '%s' AND kcu.table_schema = DATABASE();`