@dbml/connector 5.2.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/connector.test.ts +1 -1
- package/__tests__/connectors/mssql/out-files/schema.json +97 -6
- package/__tests__/connectors/mysql/expect-out-files/schema.json +1 -1
- package/__tests__/connectors/mysql/out-files/schema.json +92 -2
- package/__tests__/connectors/oracle/connection.json +4 -0
- package/__tests__/connectors/oracle/expect-out-files/schema.json +3866 -0
- package/__tests__/connectors/oracle/out-files/schema.json +3866 -0
- package/__tests__/connectors/postgres/out-files/schema.json +119 -8
- package/dist/connectors/{bigqueryConnector.d.ts → bigquery/index.d.ts} +1 -1
- package/dist/connectors/{bigqueryConnector.js → bigquery/index.js} +2 -2
- package/dist/connectors/connector.js +13 -10
- package/dist/connectors/{snowflakeConnector.d.ts → mssql/index.d.ts} +1 -1
- package/dist/connectors/{mssqlConnector.js → mssql/index.js} +1 -1
- package/dist/connectors/{mysqlConnector.d.ts → mysql/index.d.ts} +1 -1
- package/dist/connectors/oracle/constraints.d.ts +7 -0
- package/dist/connectors/oracle/constraints.js +112 -0
- package/dist/connectors/oracle/index.d.ts +3 -0
- package/dist/connectors/oracle/index.js +68 -0
- package/dist/connectors/oracle/indexes.d.ts +3 -0
- package/dist/connectors/oracle/indexes.js +65 -0
- package/dist/connectors/oracle/refs.d.ts +3 -0
- package/dist/connectors/oracle/refs.js +67 -0
- package/dist/connectors/oracle/tables.d.ts +7 -0
- package/dist/connectors/oracle/tables.js +280 -0
- package/dist/connectors/oracle/utils.d.ts +9 -0
- package/dist/connectors/oracle/utils.js +27 -0
- package/dist/connectors/{mssqlConnector.d.ts → postgres/index.d.ts} +1 -1
- package/dist/connectors/{postgresConnector.js → postgres/index.js} +1 -1
- package/dist/connectors/{postgresConnector.d.ts → snowflake/index.d.ts} +1 -1
- package/dist/connectors/{snowflakeConnector.js → snowflake/index.js} +1 -1
- package/dist/connectors/types.d.ts +1 -1
- package/package.json +4 -2
- package/src/connectors/{bigqueryConnector.ts → bigquery/index.ts} +3 -3
- package/src/connectors/connector.ts +8 -5
- package/src/connectors/{mssqlConnector.ts → mssql/index.ts} +2 -2
- package/src/connectors/{mysqlConnector.ts → mysql/index.ts} +1 -1
- package/src/connectors/oracle/constraints.ts +113 -0
- package/src/connectors/oracle/index.ts +76 -0
- package/src/connectors/oracle/indexes.ts +71 -0
- package/src/connectors/oracle/refs.ts +73 -0
- package/src/connectors/oracle/tables.ts +305 -0
- package/src/connectors/oracle/utils.ts +23 -0
- package/src/connectors/{postgresConnector.ts → postgres/index.ts} +2 -2
- package/src/connectors/{snowflakeConnector.ts → snowflake/index.ts} +2 -2
- package/src/connectors/types.ts +1 -0
- package/test-output.log +0 -18
- /package/dist/connectors/{mysqlConnector.js → mysql/index.js} +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Connection } from 'oracledb';
|
|
2
|
+
import { Index, IndexesDictionary } from '../types';
|
|
3
|
+
import { EXECUTE_OPTIONS } from './utils';
|
|
4
|
+
|
|
5
|
+
export async function generateIndexes (client: Connection): Promise<IndexesDictionary> {
|
|
6
|
+
const query = `
|
|
7
|
+
SELECT
|
|
8
|
+
i.INDEX_NAME AS "index_name",
|
|
9
|
+
i.TABLE_NAME AS "table_name",
|
|
10
|
+
CASE
|
|
11
|
+
WHEN i.INDEX_TYPE LIKE '%NORMAL' THEN 'btree'
|
|
12
|
+
ELSE LOWER(REGEXP_REPLACE(i.INDEX_TYPE, '^FUNCTION-BASED ', ''))
|
|
13
|
+
END AS "index_type",
|
|
14
|
+
CASE WHEN i.UNIQUENESS = 'UNIQUE' THEN 1 ELSE 0 END AS "is_unique",
|
|
15
|
+
ic.COLUMN_NAME AS "column_name",
|
|
16
|
+
ic.COLUMN_POSITION AS "column_position",
|
|
17
|
+
ie.COLUMN_EXPRESSION AS "column_expression"
|
|
18
|
+
FROM USER_INDEXES i
|
|
19
|
+
LEFT JOIN USER_IND_COLUMNS ic
|
|
20
|
+
ON i.INDEX_NAME = ic.INDEX_NAME
|
|
21
|
+
LEFT JOIN USER_IND_EXPRESSIONS ie
|
|
22
|
+
ON i.INDEX_NAME = ie.INDEX_NAME
|
|
23
|
+
AND ic.COLUMN_POSITION = ie.COLUMN_POSITION
|
|
24
|
+
LEFT JOIN USER_OBJECTS obj
|
|
25
|
+
ON obj.OBJECT_NAME = i.TABLE_NAME
|
|
26
|
+
AND obj.OBJECT_TYPE = 'TABLE'
|
|
27
|
+
WHERE NOT EXISTS (
|
|
28
|
+
SELECT 1
|
|
29
|
+
FROM USER_CONSTRAINTS c
|
|
30
|
+
WHERE c.INDEX_NAME = i.INDEX_NAME
|
|
31
|
+
)
|
|
32
|
+
ORDER BY obj.CREATED, obj.OBJECT_ID, i.INDEX_NAME, ic.COLUMN_POSITION
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
const res = await client.execute(query, [], EXECUTE_OPTIONS);
|
|
36
|
+
|
|
37
|
+
const indexes: IndexesDictionary = {};
|
|
38
|
+
const indexMap: Record<string, { table: string; type: string; unique: boolean; columns: Array<{ type: 'column' | 'expression'; value: string }> }> = {};
|
|
39
|
+
|
|
40
|
+
res.rows?.forEach((row) => {
|
|
41
|
+
const { index_name, table_name, index_type, is_unique, column_name, column_expression } = row as Record<string, unknown>;
|
|
42
|
+
|
|
43
|
+
const name = index_name as string;
|
|
44
|
+
const table = table_name as string;
|
|
45
|
+
const type = index_type as string;
|
|
46
|
+
const unique = !!(is_unique as number);
|
|
47
|
+
|
|
48
|
+
if (column_expression) {
|
|
49
|
+
if (!indexMap[name]) {
|
|
50
|
+
indexMap[name] = { table, type, unique, columns: [] };
|
|
51
|
+
}
|
|
52
|
+
indexMap[name].columns.push({ type: 'expression', value: column_expression as string });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (column_name) {
|
|
56
|
+
if (!indexMap[name]) {
|
|
57
|
+
indexMap[name] = { table, type, unique, columns: [] };
|
|
58
|
+
}
|
|
59
|
+
indexMap[name].columns.push({ type: 'column', value: column_name as string });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
Object.entries(indexMap).forEach(([name, { table, type, unique, columns }]) => {
|
|
65
|
+
const index: Index = { name, type, unique, columns };
|
|
66
|
+
if (!indexes[table]) indexes[table] = [];
|
|
67
|
+
indexes[table].push(index);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return indexes;
|
|
71
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Connection } from 'oracledb';
|
|
2
|
+
import { Ref, RefEndpoint } from '../types';
|
|
3
|
+
import { EXECUTE_OPTIONS, LIST_SEPARATOR } from './utils';
|
|
4
|
+
|
|
5
|
+
export async function generateRawRefs (client: Connection): Promise<Ref[]> {
|
|
6
|
+
const refs: Ref[] = [];
|
|
7
|
+
|
|
8
|
+
const refsListSql = `
|
|
9
|
+
SELECT
|
|
10
|
+
c.CONSTRAINT_NAME AS "fk_name",
|
|
11
|
+
c.TABLE_NAME AS "child_table",
|
|
12
|
+
LISTAGG(cc.COLUMN_NAME, '${LIST_SEPARATOR}') WITHIN GROUP (ORDER BY cc.POSITION) AS "child_columns",
|
|
13
|
+
p.TABLE_NAME AS "parent_table",
|
|
14
|
+
LISTAGG(pc.COLUMN_NAME, '${LIST_SEPARATOR}') WITHIN GROUP (ORDER BY pc.POSITION) AS "parent_columns",
|
|
15
|
+
c.DELETE_RULE AS "delete_rule"
|
|
16
|
+
FROM USER_CONSTRAINTS c
|
|
17
|
+
JOIN USER_CONSTRAINTS p
|
|
18
|
+
ON c.R_CONSTRAINT_NAME = p.CONSTRAINT_NAME
|
|
19
|
+
JOIN USER_CONS_COLUMNS cc
|
|
20
|
+
ON c.CONSTRAINT_NAME = cc.CONSTRAINT_NAME
|
|
21
|
+
JOIN USER_CONS_COLUMNS pc
|
|
22
|
+
ON p.CONSTRAINT_NAME = pc.CONSTRAINT_NAME
|
|
23
|
+
AND cc.POSITION = pc.POSITION
|
|
24
|
+
LEFT JOIN USER_OBJECTS obj_child
|
|
25
|
+
ON obj_child.OBJECT_NAME = c.TABLE_NAME
|
|
26
|
+
AND obj_child.OBJECT_TYPE = 'TABLE'
|
|
27
|
+
LEFT JOIN USER_OBJECTS obj_parent
|
|
28
|
+
ON obj_parent.OBJECT_NAME = p.TABLE_NAME
|
|
29
|
+
AND obj_parent.OBJECT_TYPE = 'TABLE'
|
|
30
|
+
WHERE c.CONSTRAINT_TYPE = 'R'
|
|
31
|
+
GROUP BY
|
|
32
|
+
c.CONSTRAINT_NAME,
|
|
33
|
+
c.TABLE_NAME,
|
|
34
|
+
p.TABLE_NAME,
|
|
35
|
+
c.DELETE_RULE,
|
|
36
|
+
obj_child.CREATED,
|
|
37
|
+
obj_child.OBJECT_ID,
|
|
38
|
+
obj_parent.CREATED,
|
|
39
|
+
obj_parent.OBJECT_ID
|
|
40
|
+
ORDER BY
|
|
41
|
+
obj_child.CREATED,
|
|
42
|
+
obj_child.OBJECT_ID,
|
|
43
|
+
c.CONSTRAINT_NAME
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
const refsQueryResult = await client.execute(refsListSql, [], EXECUTE_OPTIONS);
|
|
47
|
+
refsQueryResult.rows?.forEach((refRow) => {
|
|
48
|
+
const { fk_name, child_table, child_columns, parent_table, parent_columns, delete_rule } = refRow as Record<string, string>;
|
|
49
|
+
|
|
50
|
+
const ep1: RefEndpoint = {
|
|
51
|
+
tableName: child_table,
|
|
52
|
+
schemaName: '',
|
|
53
|
+
fieldNames: child_columns.split(LIST_SEPARATOR),
|
|
54
|
+
relation: '*',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const ep2: RefEndpoint = {
|
|
58
|
+
tableName: parent_table,
|
|
59
|
+
schemaName: '',
|
|
60
|
+
fieldNames: parent_columns.split(LIST_SEPARATOR),
|
|
61
|
+
relation: '1',
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
refs.push({
|
|
65
|
+
name: fk_name,
|
|
66
|
+
endpoints: [ep1, ep2],
|
|
67
|
+
onDelete: delete_rule === 'NO ACTION' ? null : delete_rule,
|
|
68
|
+
onUpdate: null,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return refs;
|
|
73
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { Connection } from 'oracledb';
|
|
2
|
+
import { DefaultInfo, DefaultType, Field, FieldsDictionary, Table } from '../types';
|
|
3
|
+
import { EXECUTE_OPTIONS } from './utils';
|
|
4
|
+
|
|
5
|
+
export async function generateTablesAndFields (client: Connection): Promise<{
|
|
6
|
+
tables: Table[];
|
|
7
|
+
fields: FieldsDictionary;
|
|
8
|
+
}> {
|
|
9
|
+
const fields: FieldsDictionary = {};
|
|
10
|
+
// Note: DATA_DEFAULT is LONG type which doesn't support SQL string operations
|
|
11
|
+
// The oracledb driver fetches LONG as string, so we detect default type in JavaScript
|
|
12
|
+
const tablesAndFieldsSql = `
|
|
13
|
+
SELECT
|
|
14
|
+
cols.TABLE_NAME AS "table_name",
|
|
15
|
+
cols.COLUMN_NAME AS "column_name",
|
|
16
|
+
cols.COLUMN_ID AS "column_id",
|
|
17
|
+
cols.DATA_TYPE AS "data_type",
|
|
18
|
+
cols.DATA_LENGTH AS "data_length",
|
|
19
|
+
cols.DATA_PRECISION AS "data_precision",
|
|
20
|
+
cols.DATA_SCALE AS "data_scale",
|
|
21
|
+
CASE WHEN cols.IDENTITY_COLUMN = 'YES' THEN 1 ELSE 0 END AS "identity_increment",
|
|
22
|
+
CASE WHEN cols.NULLABLE = 'Y' THEN 1 ELSE 0 END AS "is_nullable",
|
|
23
|
+
cols.DATA_DEFAULT AS "data_default",
|
|
24
|
+
tcoms.COMMENTS AS "table_comment",
|
|
25
|
+
ccoms.COMMENTS AS "column_comment",
|
|
26
|
+
obj.CREATED AS "table_created"
|
|
27
|
+
FROM USER_TAB_COLUMNS cols
|
|
28
|
+
LEFT JOIN USER_COL_COMMENTS ccoms
|
|
29
|
+
ON ccoms.COLUMN_NAME = cols.COLUMN_NAME
|
|
30
|
+
AND ccoms.TABLE_NAME = cols.TABLE_NAME
|
|
31
|
+
LEFT JOIN USER_TAB_COMMENTS tcoms
|
|
32
|
+
ON tcoms.TABLE_NAME = cols.TABLE_NAME
|
|
33
|
+
LEFT JOIN USER_OBJECTS obj
|
|
34
|
+
ON obj.OBJECT_NAME = cols.TABLE_NAME
|
|
35
|
+
AND obj.OBJECT_TYPE = 'TABLE'
|
|
36
|
+
ORDER BY obj.CREATED, obj.OBJECT_ID, cols.COLUMN_ID
|
|
37
|
+
`;
|
|
38
|
+
|
|
39
|
+
const tablesAndFieldsResult = await client.execute(tablesAndFieldsSql, [], EXECUTE_OPTIONS);
|
|
40
|
+
const tableMap = tablesAndFieldsResult.rows?.reduce((acc: Record<string, Table>, row) => {
|
|
41
|
+
const { table_name, table_comment } = row as any;
|
|
42
|
+
if (!acc[table_name]) {
|
|
43
|
+
acc[table_name] = {
|
|
44
|
+
name: table_name,
|
|
45
|
+
schemaName: '',
|
|
46
|
+
note: table_comment ? { value: table_comment } : { value: '' },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!fields[table_name]) fields[table_name] = [];
|
|
51
|
+
const field = generateField(row as any);
|
|
52
|
+
fields[table_name].push(field);
|
|
53
|
+
|
|
54
|
+
return acc;
|
|
55
|
+
}, {} as Record<string, Table>) || {};
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
tables: Object.values(tableMap),
|
|
59
|
+
fields,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Field utils
|
|
64
|
+
|
|
65
|
+
function getDefaultType (columnDefault: string | null | undefined): string {
|
|
66
|
+
if (columnDefault === null || columnDefault === undefined) {
|
|
67
|
+
return 'expression';
|
|
68
|
+
}
|
|
69
|
+
const trimmed = columnDefault.trim();
|
|
70
|
+
// String defaults are wrapped in single quotes: 'value'
|
|
71
|
+
// Example: `'string'`, `'string with quotes'''`
|
|
72
|
+
if (/^'.*'$/.test(trimmed)) {
|
|
73
|
+
return 'string';
|
|
74
|
+
}
|
|
75
|
+
// Number defaults are integers or non-scientific floats
|
|
76
|
+
// Example: `123`, `0.12`, `.12`, `12.`
|
|
77
|
+
if (/^[0-9]+$/.test(trimmed) || /^[0-9]*\.[0-9]*$/.test(trimmed)) {
|
|
78
|
+
return 'number';
|
|
79
|
+
}
|
|
80
|
+
// Example: Strings that are not enclosed in `'` or do not contain only numbers and `.`
|
|
81
|
+
// * CURRENT_TIMESTAMP
|
|
82
|
+
// * SYSDATE
|
|
83
|
+
// * "number-column" + 2
|
|
84
|
+
return 'expression';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function generateField (row: Record<string, any>): Field {
|
|
88
|
+
const {
|
|
89
|
+
column_name,
|
|
90
|
+
data_type,
|
|
91
|
+
data_length,
|
|
92
|
+
data_precision,
|
|
93
|
+
data_scale,
|
|
94
|
+
identity_increment,
|
|
95
|
+
is_nullable,
|
|
96
|
+
data_default,
|
|
97
|
+
column_comment,
|
|
98
|
+
} = row;
|
|
99
|
+
|
|
100
|
+
const defaultType = getDefaultType(data_default);
|
|
101
|
+
const dbdefault = data_default && !identity_increment ? getDbdefault(data_default, defaultType) : null;
|
|
102
|
+
|
|
103
|
+
const fieldType = {
|
|
104
|
+
type_name: getFieldType(data_type, data_length, data_precision, data_scale),
|
|
105
|
+
schemaName: null,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
name: column_name,
|
|
110
|
+
type: fieldType,
|
|
111
|
+
dbdefault,
|
|
112
|
+
not_null: !is_nullable,
|
|
113
|
+
increment: !!identity_increment,
|
|
114
|
+
note: column_comment ? { value: column_comment } : { value: '' },
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Field default utils
|
|
119
|
+
|
|
120
|
+
function getDbdefault (columnDefault: string | null, defaultType: string): DefaultInfo | null {
|
|
121
|
+
if (columnDefault === null) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
if (defaultType === 'string') {
|
|
125
|
+
const trimmed = columnDefault.trim();
|
|
126
|
+
return {
|
|
127
|
+
value: trimmed.slice(1, -1) // slice off single quotes at the start and end of the value
|
|
128
|
+
.replaceAll('\'\'', '\''), // escape sequences are preserved: in oracle, double single quotes = escape a single quote
|
|
129
|
+
type: defaultType,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { value: columnDefault, type: defaultType as DefaultType };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Field type utils
|
|
137
|
+
|
|
138
|
+
// These datatypes are never qualified: data size, data scale and data precision are all ignore.
|
|
139
|
+
// Ref: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html
|
|
140
|
+
const UNQUALIFIED_DATA_TYPES = new Set([
|
|
141
|
+
// datetime
|
|
142
|
+
'date',
|
|
143
|
+
// number
|
|
144
|
+
'binary_float',
|
|
145
|
+
'binary_double',
|
|
146
|
+
// long and raw
|
|
147
|
+
'long',
|
|
148
|
+
'long raw',
|
|
149
|
+
// large object
|
|
150
|
+
'blob',
|
|
151
|
+
'clob',
|
|
152
|
+
'nclob',
|
|
153
|
+
'bfile',
|
|
154
|
+
// rowid
|
|
155
|
+
'rowid',
|
|
156
|
+
// ansi supported
|
|
157
|
+
'integer',
|
|
158
|
+
'int',
|
|
159
|
+
'smallint',
|
|
160
|
+
'double precision',
|
|
161
|
+
'real',
|
|
162
|
+
// any
|
|
163
|
+
'AnyData',
|
|
164
|
+
'AnyType',
|
|
165
|
+
'AnyDataSet',
|
|
166
|
+
// xml
|
|
167
|
+
'XMLType',
|
|
168
|
+
'URIType',
|
|
169
|
+
// spatial
|
|
170
|
+
'SDO_Geometry',
|
|
171
|
+
'SDO_Topo_Geometry',
|
|
172
|
+
'SDO_GeoRaster',
|
|
173
|
+
]);
|
|
174
|
+
|
|
175
|
+
// These datatypes can be and only be qualified with data size
|
|
176
|
+
// For simplicity the data size in bytes is assumed
|
|
177
|
+
// Ref: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html
|
|
178
|
+
const SIZED_DATA_TYPES = new Set([
|
|
179
|
+
// character
|
|
180
|
+
'char',
|
|
181
|
+
'varchar2',
|
|
182
|
+
'nchar',
|
|
183
|
+
'nvarchar2',
|
|
184
|
+
// long and raw
|
|
185
|
+
'raw',
|
|
186
|
+
// rowid
|
|
187
|
+
'urowid',
|
|
188
|
+
// ansi supported
|
|
189
|
+
'character varying',
|
|
190
|
+
'character',
|
|
191
|
+
'char varying',
|
|
192
|
+
'nchar varying',
|
|
193
|
+
'varchar',
|
|
194
|
+
'national character varying',
|
|
195
|
+
'national character',
|
|
196
|
+
'national char varying',
|
|
197
|
+
'national char',
|
|
198
|
+
]);
|
|
199
|
+
|
|
200
|
+
// These datatypes can be and only be qualified with precision
|
|
201
|
+
// For datetime, these are the types that only have 1 precision
|
|
202
|
+
// Ref: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html
|
|
203
|
+
const PRECISION_DATA_TYPES = new Set([
|
|
204
|
+
// number
|
|
205
|
+
'float',
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
// This datatype can be qualified with a fractional second precision
|
|
209
|
+
const TIMESTAMP_WITH_LOCAL_TIME_ZONE_DATA_TYPE = 'timestamp with local time zone';
|
|
210
|
+
// This datatype can be qualified with a second precision
|
|
211
|
+
const TIMESTAMP_WITH_TIME_ZONE_DATA_TYPE = 'timestamp with time zone';
|
|
212
|
+
// This datatype can be qualified with a second precision
|
|
213
|
+
const TIMESTAMP_DATA_TYPE = 'timestamp';
|
|
214
|
+
// This datatype can be qualified with a year precision
|
|
215
|
+
const INTERVAL_YEAR_DATA_TYPE = 'interval year to month';
|
|
216
|
+
// This datatype can be qualified with a day precision and a fraction second precision
|
|
217
|
+
const INTERVAL_DAY_DATA_TYPE = 'interval day to second';
|
|
218
|
+
|
|
219
|
+
// These datatypes can be and only be qualified with precision and scale
|
|
220
|
+
// For datetime, these are the types that can have 2 precisions
|
|
221
|
+
// Ref: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html
|
|
222
|
+
const PRECISION_SCALED_DATA_TYPES = new Set([
|
|
223
|
+
// number
|
|
224
|
+
'number',
|
|
225
|
+
// ansi supported
|
|
226
|
+
'numeric',
|
|
227
|
+
'decimal',
|
|
228
|
+
'dec',
|
|
229
|
+
]);
|
|
230
|
+
|
|
231
|
+
// Format the field type to a standard string for aesthetic when exporting to DBML (`ANYDATA`, `anydata` are all normalized to `AnyData`)
|
|
232
|
+
// Some types contain multiple words with arbitrary spaces, for example, `TIMESTAMP (6) WITH LOCAL TIME ZONE`, we should normalize this to allow processing to be uniform, for example when checking for equality
|
|
233
|
+
function normalizeFieldType (dataType: string): string {
|
|
234
|
+
const normalizedDataType = dataType.split(' ').filter(Boolean).join(' ').toLowerCase();
|
|
235
|
+
switch (normalizedDataType) {
|
|
236
|
+
case 'anydata':
|
|
237
|
+
return 'AnyData';
|
|
238
|
+
case 'anytype':
|
|
239
|
+
return 'AnyType';
|
|
240
|
+
case 'anydataset':
|
|
241
|
+
return 'AnyDataSet';
|
|
242
|
+
case 'xmltype':
|
|
243
|
+
return 'XMLType';
|
|
244
|
+
case 'uritype':
|
|
245
|
+
return 'URIType';
|
|
246
|
+
case 'sdo_geometry':
|
|
247
|
+
return 'SDO_Geometry';
|
|
248
|
+
case 'sdo_topo_geometry':
|
|
249
|
+
return 'SDO_Topo_Geometry';
|
|
250
|
+
case 'sdo_georaster':
|
|
251
|
+
return 'SDO_GeoRaster';
|
|
252
|
+
default:
|
|
253
|
+
return normalizedDataType;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function getFieldType (dataType: string, characterMaximumLength: number, numericPrecision: number | null, numericScale: number | null): string {
|
|
258
|
+
const normalizedDataType = normalizeFieldType(dataType);
|
|
259
|
+
if (normalizedDataType === TIMESTAMP_WITH_LOCAL_TIME_ZONE_DATA_TYPE) {
|
|
260
|
+
if (numericScale !== null) return `timestamp (${numericScale}) with local time zone`;
|
|
261
|
+
return normalizedDataType;
|
|
262
|
+
}
|
|
263
|
+
if (normalizedDataType === TIMESTAMP_WITH_TIME_ZONE_DATA_TYPE) {
|
|
264
|
+
if (numericScale !== null) return `timestamp (${numericScale}) with time zone`;
|
|
265
|
+
return normalizedDataType;
|
|
266
|
+
}
|
|
267
|
+
if (normalizedDataType === TIMESTAMP_DATA_TYPE) {
|
|
268
|
+
if (numericScale !== null) return `timestamp (${numericScale})`;
|
|
269
|
+
return normalizedDataType;
|
|
270
|
+
}
|
|
271
|
+
if (normalizedDataType === INTERVAL_YEAR_DATA_TYPE) {
|
|
272
|
+
if (numericPrecision !== null) return `interval year (${numericPrecision}) to month`;
|
|
273
|
+
return normalizedDataType;
|
|
274
|
+
}
|
|
275
|
+
if (normalizedDataType === INTERVAL_DAY_DATA_TYPE) {
|
|
276
|
+
if (numericPrecision !== null && numericScale !== null) return `interval day (${numericPrecision}) to second (${numericScale})`;
|
|
277
|
+
if (numericPrecision !== null) return `interval day (${numericPrecision}) to second`;
|
|
278
|
+
if (numericScale !== null) return `interval day to second (${numericScale})`;
|
|
279
|
+
return normalizedDataType;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (SIZED_DATA_TYPES.has(normalizedDataType)) {
|
|
283
|
+
if (characterMaximumLength) return `${normalizedDataType}(${characterMaximumLength})`;
|
|
284
|
+
return normalizedDataType;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (PRECISION_DATA_TYPES.has(normalizedDataType)) {
|
|
288
|
+
if (numericPrecision !== null) return `${normalizedDataType}(${numericPrecision})`;
|
|
289
|
+
return normalizedDataType;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (PRECISION_SCALED_DATA_TYPES.has(normalizedDataType)) {
|
|
293
|
+
if (numericPrecision !== null && numericScale !== null) return `${normalizedDataType}(${numericPrecision}, ${numericScale})`;
|
|
294
|
+
if (numericPrecision !== null) return `${normalizedDataType}(${numericPrecision})`;
|
|
295
|
+
// There's no situation where numericScale is not null but numericPrecision is null
|
|
296
|
+
return normalizedDataType;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// This is not needed since we return `normalizedDataType` either if the condition holds true or not
|
|
300
|
+
// if (UNQUALIFIED_DATA_TYPES.has(normalizedDataType)) {
|
|
301
|
+
// return normalizedDataType;
|
|
302
|
+
// }
|
|
303
|
+
|
|
304
|
+
return normalizedDataType;
|
|
305
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import oracledb from 'oracledb';
|
|
2
|
+
|
|
3
|
+
export const LIST_SEPARATOR = '@@@@@@@@'; // Used to join multiple fields into a string field to simulate array column types
|
|
4
|
+
|
|
5
|
+
export const EXECUTE_OPTIONS = { outFormat: oracledb.OUT_FORMAT_OBJECT };
|
|
6
|
+
|
|
7
|
+
// Expect an Easy Connect string format: username/password@[//]host[:port][/database]
|
|
8
|
+
// Explanation of the format: https://www.orafaq.com/wiki/EZCONNECT
|
|
9
|
+
export function processEasyConnectString (connection: string): { username: string; password: string; dbidentifier: string } {
|
|
10
|
+
// Capturing group `username`: a string without the characters `/` `@` `:`
|
|
11
|
+
// Capturing group `password`: a string without the characters `/` `@` `:`
|
|
12
|
+
// Capturing group `dbidentifier`: a string containing the `host[:port][/database]` part
|
|
13
|
+
const matches = connection.match(/^(?<username>[^/@:]+)\/(?<password>[^/@:]+)@(\/\/)?(?<dbidentifier>.+)$/);
|
|
14
|
+
const { username, password, dbidentifier } = matches?.groups || {};
|
|
15
|
+
if (!username || !password || !dbidentifier) {
|
|
16
|
+
throw new Error('Invalid Easy Connect string. Expect a string of format \'username/password@[//]host[:port][/database]\'');
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
username,
|
|
20
|
+
password,
|
|
21
|
+
dbidentifier,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Client } from 'pg';
|
|
2
|
-
import { buildSchemaQuery, parseConnectionString } from '
|
|
2
|
+
import { buildSchemaQuery, parseConnectionString } from '../../utils/parseSchema';
|
|
3
3
|
import {
|
|
4
4
|
CheckConstraintDictionary,
|
|
5
5
|
DatabaseSchema,
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
RefEndpoint,
|
|
14
14
|
Table,
|
|
15
15
|
TableConstraintsDictionary,
|
|
16
|
-
} from '
|
|
16
|
+
} from '../types';
|
|
17
17
|
|
|
18
18
|
const getValidatedClient = async (connection: string): Promise<Client> => {
|
|
19
19
|
const client = new Client(connection);
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import snowflake from 'snowflake-sdk';
|
|
10
10
|
import type { Connection, LogLevel, ConnectionOptions } from 'snowflake-sdk';
|
|
11
|
-
import { parseSchema } from '
|
|
11
|
+
import { parseSchema } from '../../utils/parseSchema';
|
|
12
12
|
import {
|
|
13
13
|
DatabaseSchema,
|
|
14
14
|
DefaultInfo,
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
IndexesDictionary,
|
|
20
20
|
Table,
|
|
21
21
|
TableConstraintsDictionary,
|
|
22
|
-
} from '
|
|
22
|
+
} from '../types';
|
|
23
23
|
|
|
24
24
|
type ConstraintRow = {
|
|
25
25
|
schemaName: string;
|
package/src/connectors/types.ts
CHANGED
package/test-output.log
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @dbml/connector@5.1.0 test
|
|
3
|
-
> jest
|
|
4
|
-
|
|
5
|
-
PASS __tests__/connector.test.ts
|
|
6
|
-
@dbml/connector
|
|
7
|
-
✓ connectors/mssql (264 ms)
|
|
8
|
-
✓ connectors/mysql (31 ms)
|
|
9
|
-
✓ connectors/postgres (55 ms)
|
|
10
|
-
|
|
11
|
-
Test Suites: 1 passed, 1 total
|
|
12
|
-
Tests: 3 passed, 3 total
|
|
13
|
-
Snapshots: 0 total
|
|
14
|
-
Time: 1.208 s, estimated 2 s
|
|
15
|
-
Ran all test suites.
|
|
16
|
-
Jest did not exit one second after the test run has completed.
|
|
17
|
-
|
|
18
|
-
'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
|
|
File without changes
|