uql-orm 0.104.1 → 0.105.1
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/README.md +24 -10
- package/dist/browser/querier/httpQuerier.d.ts +3 -3
- package/dist/browser/type/clientQuerier.d.ts +1 -1
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +9 -9
- package/dist/cockroachdb/cockroachDialect.d.ts +2 -2
- package/dist/dialect/abstractDialect.d.ts +3 -3
- package/dist/dialect/abstractSqlDialect.d.ts +39 -37
- package/dist/dialect/abstractSqlDialect.js +35 -32
- package/dist/dialect/aliases.d.ts +2 -0
- package/dist/dialect/aliases.js +2 -0
- package/dist/dialect/jsonSql.js +2 -2
- package/dist/dialect/mergeSqlDialect.d.ts +2 -2
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +7 -7
- package/dist/dialect/mysqlLikeSqlDialect.js +1 -1
- package/dist/dialect/operators.js +2 -4
- package/dist/dialect/queryJoins.d.ts +4 -2
- package/dist/dialect/vectorSqlDialect.d.ts +1 -1
- package/dist/dialect/vectorSqlDialect.js +1 -1
- package/dist/entity/column.d.ts +3 -0
- package/dist/entity/column.js +3 -0
- package/dist/entity/columnOptionsBuilder.d.ts +54 -0
- package/dist/entity/columnOptionsBuilder.js +108 -0
- package/dist/entity/decorator/bag.d.ts +5 -5
- package/dist/entity/decorator/entity.d.ts +3 -20
- package/dist/entity/decorator/entity.js +3 -37
- package/dist/entity/decorator/members.d.ts +46 -76
- package/dist/entity/decorator/members.js +18 -7
- package/dist/entity/index.d.ts +2 -1
- package/dist/entity/index.js +2 -1
- package/dist/entity/metadata/definition.d.ts +15 -26
- package/dist/entity/metadata/definition.js +344 -148
- package/dist/entity/metadata/schemaFixture/animal.d.ts +54 -0
- package/dist/entity/metadata/schemaFixture/animal.js +7 -0
- package/dist/entity/metadata/schemaFixture/author.d.ts +140 -0
- package/dist/entity/metadata/schemaFixture/author.js +25 -0
- package/dist/entity/metadata/schemaFixture/book.d.ts +192 -0
- package/dist/entity/metadata/schemaFixture/book.js +23 -0
- package/dist/http/contract.d.ts +2 -2
- package/dist/http/handler.d.ts +2 -2
- package/dist/http/query.js +12 -13
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -1
- package/dist/mariadb/mariaDialect.d.ts +2 -2
- package/dist/mariadb/mariaDialect.js +1 -1
- package/dist/mariadb/mariadbQuerier.js +1 -3
- package/dist/migrate/builder/columnBuilder.d.ts +20 -68
- package/dist/migrate/builder/columnBuilder.js +104 -110
- package/dist/migrate/builder/migrationBuilder.d.ts +3 -3
- package/dist/migrate/builder/migrationBuilder.js +7 -6
- package/dist/migrate/builder/tableBuilder.d.ts +3 -55
- package/dist/migrate/builder/tableBuilder.js +88 -204
- package/dist/migrate/builder/types.d.ts +52 -151
- package/dist/migrate/builder/types.js +0 -6
- package/dist/migrate/codegen/entityCodeGenerator.d.ts +2 -2
- package/dist/migrate/codegen/entityCodeGenerator.js +24 -24
- package/dist/migrate/codegen/entityTypes.d.ts +2 -2
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +3 -1
- package/dist/migrate/codegen/fieldOptionsSource.js +35 -34
- package/dist/migrate/codegen/indexEntrySource.d.ts +16 -0
- package/dist/migrate/codegen/{indexDecoratorSource.js → indexEntrySource.js} +54 -27
- package/dist/migrate/ddl/defaultSql.js +7 -6
- package/dist/migrate/ddl/indexCapabilities.d.ts +8 -7
- package/dist/migrate/ddl/indexCapabilities.js +26 -13
- package/dist/migrate/ddl/indexDdl.d.ts +13 -13
- package/dist/migrate/ddl/indexDdl.js +33 -30
- package/dist/migrate/ddl/mysqlIndexDdl.d.ts +6 -11
- package/dist/migrate/ddl/mysqlIndexDdl.js +17 -28
- package/dist/migrate/ddl/pgIndexDdl.d.ts +2 -2
- package/dist/migrate/ddl/sqliteIndexDdl.d.ts +2 -2
- package/dist/migrate/drift/driftDetector.js +2 -2
- package/dist/migrate/generator/definitionToNode.d.ts +8 -2
- package/dist/migrate/generator/definitionToNode.js +4 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +11 -2
- package/dist/migrate/introspection/jsonIndexEntry.d.ts +6 -0
- package/dist/migrate/introspection/jsonIndexEntry.js +120 -0
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +4 -4
- package/dist/migrate/introspection/mysqlIntrospector.js +25 -21
- package/dist/migrate/introspection/postgresIntrospector.d.ts +1 -1
- package/dist/migrate/introspection/postgresIntrospector.js +3 -4
- package/dist/migrate/introspection/sqliteIntrospector.js +24 -9
- package/dist/migrate/introspection/tableSchemaAST.js +5 -1
- package/dist/migrate/migrator.d.ts +2 -2
- package/dist/migrate/schemaChange.d.ts +3 -3
- package/dist/migrate/schemaChange.js +3 -2
- package/dist/migrate/schemaGenerator.d.ts +5 -5
- package/dist/migrate/schemaGenerator.js +1 -1
- package/dist/migrate/storage/databaseStorage.js +4 -4
- package/dist/mongodb/mongoDialect.d.ts +41 -44
- package/dist/mongodb/mongoDialect.js +118 -32
- package/dist/mongodb/mongoIntrospector.d.ts +3 -1
- package/dist/mongodb/mongoIntrospector.js +11 -5
- package/dist/mongodb/mongoSchemaGenerator.d.ts +4 -4
- package/dist/mongodb/mongoSchemaGenerator.js +35 -27
- package/dist/mongodb/mongodbQuerier.d.ts +1 -11
- package/dist/mongodb/mongodbQuerier.js +9 -43
- package/dist/mssql/mssqlDialect.d.ts +4 -4
- package/dist/postgres/postgresDialect.d.ts +2 -2
- package/dist/querier/abstractQuerier.d.ts +0 -1
- package/dist/querier/abstractQuerier.js +1 -20
- package/dist/querier/abstractSqlQuerier.d.ts +1 -1
- package/dist/querier/keyset.js +3 -3
- package/dist/schema/declaredColumn.d.ts +11 -0
- package/dist/schema/declaredColumn.js +21 -0
- package/dist/schema/indexColumns.d.ts +0 -3
- package/dist/schema/indexColumns.js +11 -7
- package/dist/schema/indexDifferences.d.ts +10 -5
- package/dist/schema/indexDifferences.js +42 -28
- package/dist/schema/schemaASTBuilder.d.ts +4 -4
- package/dist/schema/schemaASTBuilder.js +28 -36
- package/dist/schema/schemaASTDiffer.js +8 -4
- package/dist/schema/sqlExpression.d.ts +21 -17
- package/dist/schema/sqlExpression.js +11 -23
- package/dist/type/column.d.ts +328 -0
- package/dist/type/column.js +2 -0
- package/dist/type/config.d.ts +3 -3
- package/dist/type/dialect.d.ts +15 -14
- package/dist/type/dialect.js +1 -0
- package/dist/type/entity.d.ts +292 -281
- package/dist/type/entity.js +6 -1
- package/dist/type/index.d.ts +2 -0
- package/dist/type/index.js +2 -0
- package/dist/type/migration.d.ts +11 -11
- package/dist/type/querier.d.ts +2 -2
- package/dist/type/query.d.ts +44 -40
- package/dist/type/query.js +2 -3
- package/dist/type/queryAggregate.d.ts +22 -12
- package/dist/type/querySql.d.ts +22 -6
- package/dist/type/querySql.js +27 -9
- package/dist/type/schemaEntity.d.ts +194 -0
- package/dist/type/schemaEntity.js +1 -0
- package/dist/type/utility.d.ts +4 -7
- package/dist/type/vector.d.ts +1 -1
- package/dist/util/ddlExpression.util.d.ts +12 -13
- package/dist/util/ddlExpression.util.js +12 -21
- package/dist/util/dialect.util.d.ts +11 -5
- package/dist/util/dialect.util.js +31 -37
- package/dist/util/field.util.d.ts +10 -9
- package/dist/util/field.util.js +14 -10
- package/dist/util/fieldOption.util.d.ts +11 -95
- package/dist/util/fieldOption.util.js +49 -16
- package/dist/util/hook.util.d.ts +3 -15
- package/dist/util/hook.util.js +3 -16
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +1 -0
- package/dist/util/indexEntry.util.d.ts +45 -0
- package/dist/util/indexEntry.util.js +138 -0
- package/dist/util/relationQuery.util.d.ts +0 -1
- package/dist/util/relationQuery.util.js +0 -5
- package/dist/util/sql.d.ts +13 -10
- package/dist/util/sql.js +34 -16
- package/dist/util/sql.util.d.ts +4 -5
- package/dist/util/sql.util.js +7 -16
- package/dist/util/string.util.d.ts +2 -0
- package/dist/util/string.util.js +4 -0
- package/dist/util/triggerWrite.d.ts +5 -5
- package/dist/util/uqlError.d.ts +1 -1
- package/dist/util/uqlError.js +1 -1
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +76 -25
- package/dist/migrate/codegen/indexDecoratorSource.d.ts +0 -16
|
@@ -2,6 +2,7 @@ import { jsonTypeMode } from '../../dialect/jsonSql.js';
|
|
|
2
2
|
import { INDEX_FEATURE_LABELS, } from '../../type/index.js';
|
|
3
3
|
import { indexDistance, unsupportedVectorMetric } from '../../type/vector.js';
|
|
4
4
|
import { fulltextConfig, getKeys } from '../../util/index.js';
|
|
5
|
+
import { jsonIndexRead, storedColumns } from '../../util/indexEntry.util.js';
|
|
5
6
|
import { UqlUsageError } from '../../util/uqlError.js';
|
|
6
7
|
import { INDEX_CAPABILITIES } from './indexCapabilities.js';
|
|
7
8
|
/**
|
|
@@ -10,26 +11,32 @@ import { INDEX_CAPABILITIES } from './indexCapabilities.js';
|
|
|
10
11
|
* whether an index wants it - which is how a JSON path once slipped past the entry comparison.
|
|
11
12
|
*/
|
|
12
13
|
const INDEX_FEATURE_PROBES = {
|
|
13
|
-
expression: (index) => index.entries.some((entry) => entry.expression),
|
|
14
|
-
jsonPath: (index) => index.entries.some((entry) => entry
|
|
15
|
-
jsonArray: (index) => index.entries.some((entry) => entry
|
|
14
|
+
expression: (index) => index.entries.some((entry) => entry.expression !== undefined),
|
|
15
|
+
jsonPath: (index) => index.entries.some((entry) => jsonIndexRead(entry)?.elements === false),
|
|
16
|
+
jsonArray: (index) => index.entries.some((entry) => jsonIndexRead(entry)?.elements === true),
|
|
16
17
|
partial: (index) => index.where !== undefined,
|
|
17
|
-
prefixLength: (index) => index.entries.some((entry) => entry.
|
|
18
|
+
prefixLength: (index) => index.entries.some((entry) => entry.prefixLength !== undefined),
|
|
19
|
+
fulltextPath: (index) => index.type === 'fulltext' && index.entries.some((entry) => jsonIndexRead(entry) !== undefined),
|
|
18
20
|
nullsOrder: (index) => index.entries.some((entry) => entry.nulls !== undefined),
|
|
19
21
|
opsClass: (index) => index.entries.some((entry) => entry.opsClass !== undefined),
|
|
20
22
|
include: (index) => Boolean(index.include?.length),
|
|
21
23
|
};
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Refuses an index `capabilities` cannot express: a type it lacks, its hint naming what to declare instead, or a
|
|
26
|
+
* feature that type cannot take, naming the type where another of the engine's can.
|
|
27
|
+
*/
|
|
28
|
+
export function assertExpressible(index, capabilities, engine) {
|
|
29
|
+
const { types, hints } = capabilities;
|
|
30
|
+
const type = index.type ?? 'btree';
|
|
31
|
+
const features = types.get(type);
|
|
32
|
+
if (!features) {
|
|
33
|
+
throw new UqlUsageError(`${engine} has no ${type} index (index "${index.name}")` + (hints.get(type) ?? ''));
|
|
26
34
|
}
|
|
27
|
-
}
|
|
28
|
-
/** Refuses an index asking for a feature `features` lacks. */
|
|
29
|
-
export function assertIndexFeatures(index, features, engine) {
|
|
30
35
|
for (const feature of getKeys(INDEX_FEATURE_PROBES)) {
|
|
31
36
|
if (INDEX_FEATURE_PROBES[feature](index) && !features.has(feature)) {
|
|
32
|
-
|
|
37
|
+
const elsewhere = [...types.values()].some((expressed) => expressed.has(feature));
|
|
38
|
+
throw new UqlUsageError(`${engine} does not support ${INDEX_FEATURE_LABELS[feature]}${elsewhere ? ` on a ${type} index` : ''} ` +
|
|
39
|
+
`(index "${index.name}")`);
|
|
33
40
|
}
|
|
34
41
|
}
|
|
35
42
|
}
|
|
@@ -46,7 +53,7 @@ export class IndexDdl {
|
|
|
46
53
|
this.capabilities = INDEX_CAPABILITIES[dialect.dialectName];
|
|
47
54
|
}
|
|
48
55
|
getCreateIndexStatement(tableName, index, opts = {}) {
|
|
49
|
-
|
|
56
|
+
assertExpressible(index, this.capabilities, this.dialect.engineName);
|
|
50
57
|
const unique = index.unique ? 'UNIQUE ' : '';
|
|
51
58
|
const ifNotExists = (opts.ifNotExists ?? this.dialect.features.indexIfNotExists) ? 'IF NOT EXISTS ' : '';
|
|
52
59
|
return (`CREATE ${unique}${this.indexKeyword(index)} ${ifNotExists}${this.dialect.escapeId(index.name)} ` +
|
|
@@ -60,14 +67,9 @@ export class IndexDdl {
|
|
|
60
67
|
if (!this.capabilities.inline || index.type || index.unique) {
|
|
61
68
|
return undefined;
|
|
62
69
|
}
|
|
63
|
-
|
|
70
|
+
assertExpressible(index, this.capabilities, this.dialect.engineName);
|
|
64
71
|
return `INDEX ${this.dialect.escapeId(index.name)} ${this.indexBody(index)}`;
|
|
65
72
|
}
|
|
66
|
-
assertExpressible(index) {
|
|
67
|
-
const { types, features, hints } = this.capabilities;
|
|
68
|
-
assertIndexType(index, types, this.dialect.engineName, hints);
|
|
69
|
-
assertIndexFeatures(index, features, this.dialect.engineName);
|
|
70
|
-
}
|
|
71
73
|
/** What follows the name and access method, in either form: the columns, then what qualifies them. */
|
|
72
74
|
indexBody(index) {
|
|
73
75
|
return `(${this.indexTarget(index)})${this.indexInclude(index)}${this.indexTuning(index)}${this.indexPredicate(index)}`;
|
|
@@ -86,9 +88,9 @@ export class IndexDdl {
|
|
|
86
88
|
}
|
|
87
89
|
/** What the index is over, between the parentheses: a fulltext one's columns as a search matches them, else its entries. */
|
|
88
90
|
indexTarget(index) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return this.dialect.textSearchTarget(columns, fulltextConfig(index));
|
|
91
|
+
const columns = index.type === 'fulltext' ? storedColumns(index.entries) : undefined;
|
|
92
|
+
if (columns) {
|
|
93
|
+
return this.dialect.textSearchTarget(columns.map((column) => this.dialect.escapeId(column)), fulltextConfig(index));
|
|
92
94
|
}
|
|
93
95
|
return index.entries.map((entry) => this.indexColumn(entry, index)).join(', ');
|
|
94
96
|
}
|
|
@@ -103,17 +105,18 @@ export class IndexDdl {
|
|
|
103
105
|
* expression compiled from the path rather than written out by the caller.
|
|
104
106
|
*/
|
|
105
107
|
indexColumnTarget(entry) {
|
|
106
|
-
if (entry.expression) {
|
|
107
|
-
return `(${entry.
|
|
108
|
+
if (entry.expression !== undefined) {
|
|
109
|
+
return `(${entry.expression})`;
|
|
108
110
|
}
|
|
109
|
-
const
|
|
110
|
-
if (
|
|
111
|
-
return `(${this.
|
|
111
|
+
const json = jsonIndexRead(entry);
|
|
112
|
+
if (json) {
|
|
113
|
+
return `(${this.jsonIndexExpr(this.dialect.escapeId(json.column), json)})`;
|
|
112
114
|
}
|
|
113
|
-
|
|
115
|
+
const column = this.dialect.escapeId(entry.column);
|
|
116
|
+
return entry.prefixLength === undefined ? column : `${column}(${entry.prefixLength})`;
|
|
114
117
|
}
|
|
115
118
|
/** The path read the way a query comparing it reads it, which is how the planner matches the two. */
|
|
116
|
-
|
|
119
|
+
jsonIndexExpr(escapedColumn, json) {
|
|
117
120
|
return this.dialect.jsonPathExpr(escapedColumn, json.path, jsonTypeMode(json.type));
|
|
118
121
|
}
|
|
119
122
|
/** Postgres-wire dialects put a vector or user-declared operator class here. */
|
|
@@ -147,7 +150,7 @@ export class IndexDdl {
|
|
|
147
150
|
return '';
|
|
148
151
|
}
|
|
149
152
|
/**
|
|
150
|
-
* The partial-index predicate. Engines without one reject the index in {@link
|
|
153
|
+
* The partial-index predicate. Engines without one reject the index in {@link assertExpressible}
|
|
151
154
|
* rather than reaching here: silently widening a partial unique index changes which rows the
|
|
152
155
|
* database accepts.
|
|
153
156
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IndexSchema } from '../../type/index.js';
|
|
2
|
+
import type { JsonIndexRead } from '../../util/indexEntry.util.js';
|
|
2
3
|
import { IndexDdl } from './indexDdl.js';
|
|
3
4
|
/** `CREATE INDEX ... (cols) USING btree`, plus the types this family spells as a keyword instead. */
|
|
4
5
|
export declare class MysqlLikeIndexDdl extends IndexDdl {
|
|
@@ -12,17 +13,11 @@ export declare class MysqlLikeIndexDdl extends IndexDdl {
|
|
|
12
13
|
}
|
|
13
14
|
export declare class MySqlIndexDdl extends MysqlLikeIndexDdl {
|
|
14
15
|
/**
|
|
15
|
-
* A
|
|
16
|
-
*
|
|
17
|
-
*
|
|
16
|
+
* A string as `CHAR(n)` in the collation `->>` returns, which the planner strips back to the `->>` a query
|
|
17
|
+
* compares. A boolean compares as JSON, which no key part holds. An array's elements are a multi-valued index,
|
|
18
|
+
* `CAST(col AS CHAR(64) ARRAY)`, over the column itself where the array is the document, as `$all` reads it.
|
|
18
19
|
*/
|
|
19
|
-
protected
|
|
20
|
-
/**
|
|
21
|
-
* A JSON array entry is MySQL's multi-valued index, one key per element: `CAST(col AS CHAR(64) ARRAY)`,
|
|
22
|
-
* over the column itself where the array is the whole document, which is what `$all`'s
|
|
23
|
-
* `JSON_CONTAINS(col, ?)` is matched against. A `path` indexes the array at that path instead.
|
|
24
|
-
*/
|
|
25
|
-
protected indexColumnTarget(entry: IndexColumnSchema): string;
|
|
20
|
+
protected jsonIndexExpr(escapedColumn: string, json: JsonIndexRead): string;
|
|
26
21
|
}
|
|
27
22
|
export declare class MariaIndexDdl extends MysqlLikeIndexDdl {
|
|
28
23
|
/**
|
|
@@ -18,37 +18,27 @@ export class MysqlLikeIndexDdl extends IndexDdl {
|
|
|
18
18
|
}
|
|
19
19
|
export class MySqlIndexDdl extends MysqlLikeIndexDdl {
|
|
20
20
|
/**
|
|
21
|
-
* A
|
|
22
|
-
*
|
|
23
|
-
*
|
|
21
|
+
* A string as `CHAR(n)` in the collation `->>` returns, which the planner strips back to the `->>` a query
|
|
22
|
+
* compares. A boolean compares as JSON, which no key part holds. An array's elements are a multi-valued index,
|
|
23
|
+
* `CAST(col AS CHAR(64) ARRAY)`, over the column itself where the array is the document, as `$all` reads it.
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
jsonIndexExpr(escapedColumn, json) {
|
|
26
|
+
if (json.elements) {
|
|
27
|
+
const source = json.path ? this.dialect.jsonPathExpr(escapedColumn, json.path, 'json') : escapedColumn;
|
|
28
|
+
return `CAST(${source} AS ${arrayCastType(json.type, json.charLength)} ARRAY)`;
|
|
29
|
+
}
|
|
26
30
|
const mode = jsonTypeMode(json.type);
|
|
27
31
|
if (mode === 'json') {
|
|
28
32
|
throw new UqlUsageError(`MySQL cannot index the boolean JSON path '${json.path}', which compares as JSON`);
|
|
29
33
|
}
|
|
30
|
-
const expr = super.
|
|
34
|
+
const expr = super.jsonIndexExpr(escapedColumn, json);
|
|
31
35
|
if (mode === 'numeric') {
|
|
32
36
|
return expr;
|
|
33
37
|
}
|
|
34
|
-
if (!json.
|
|
35
|
-
throw new UqlUsageError(`a MySQL index over the string JSON path '${json.path}' needs a
|
|
36
|
-
}
|
|
37
|
-
return `CAST(${expr} AS CHAR(${json.length}) CHARACTER SET utf8mb4) COLLATE utf8mb4_bin`;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* A JSON array entry is MySQL's multi-valued index, one key per element: `CAST(col AS CHAR(64) ARRAY)`,
|
|
41
|
-
* over the column itself where the array is the whole document, which is what `$all`'s
|
|
42
|
-
* `JSON_CONTAINS(col, ?)` is matched against. A `path` indexes the array at that path instead.
|
|
43
|
-
*/
|
|
44
|
-
indexColumnTarget(entry) {
|
|
45
|
-
const json = entry.jsonArray;
|
|
46
|
-
if (!json) {
|
|
47
|
-
return super.indexColumnTarget(entry);
|
|
38
|
+
if (!json.charLength) {
|
|
39
|
+
throw new UqlUsageError(`a MySQL index over the string JSON path '${json.path}' needs a charLength`);
|
|
48
40
|
}
|
|
49
|
-
|
|
50
|
-
const source = json.path ? this.dialect.jsonPathExpr(column, json.path, 'json') : column;
|
|
51
|
-
return `(CAST(${source} AS ${arrayCastType(json)} ARRAY))`;
|
|
41
|
+
return `CAST(${expr} AS CHAR(${json.charLength}) CHARACTER SET utf8mb4) COLLATE utf8mb4_bin`;
|
|
52
42
|
}
|
|
53
43
|
}
|
|
54
44
|
export class MariaIndexDdl extends MysqlLikeIndexDdl {
|
|
@@ -89,9 +79,8 @@ const ARRAY_CASTS = new Map([
|
|
|
89
79
|
['blob', 'BINARY'],
|
|
90
80
|
['bytea', 'BINARY'],
|
|
91
81
|
]);
|
|
92
|
-
/** The cast an element type compiles to; the sized ones need their
|
|
93
|
-
function arrayCastType(
|
|
94
|
-
const type = json.type;
|
|
82
|
+
/** The cast an element type compiles to; the sized ones need their charLength, since it sizes the key. */
|
|
83
|
+
function arrayCastType(type, charLength) {
|
|
95
84
|
const cast = ARRAY_CASTS.get(typeof type === 'string' ? type.toLowerCase() : type);
|
|
96
85
|
if (!cast) {
|
|
97
86
|
throw new UqlUsageError(`MySQL has no array cast for ${typeof type === 'string' ? type : type.name} elements`);
|
|
@@ -99,8 +88,8 @@ function arrayCastType(json) {
|
|
|
99
88
|
if (cast !== 'CHAR' && cast !== 'BINARY') {
|
|
100
89
|
return cast;
|
|
101
90
|
}
|
|
102
|
-
if (!
|
|
103
|
-
throw new UqlUsageError(`a multi-valued index over ${cast === 'CHAR' ? 'string' : 'binary'} elements needs a
|
|
91
|
+
if (!charLength) {
|
|
92
|
+
throw new UqlUsageError(`a multi-valued index over ${cast === 'CHAR' ? 'string' : 'binary'} elements needs a charLength`);
|
|
104
93
|
}
|
|
105
|
-
return `${cast}(${
|
|
94
|
+
return `${cast}(${charLength})`;
|
|
106
95
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IndexEntry, IndexSchema } from '../../type/index.js';
|
|
2
2
|
import { IndexDdl } from './indexDdl.js';
|
|
3
3
|
/** `CREATE INDEX ... USING hnsw ("embedding" vector_cosine_ops) WITH (m = ...)`, pgvector's form. */
|
|
4
4
|
export declare class PgIndexDdl extends IndexDdl {
|
|
@@ -10,7 +10,7 @@ export declare class PgIndexDdl extends IndexDdl {
|
|
|
10
10
|
* lacks rather than build with the default; any other entry takes the class it declares. Stated even
|
|
11
11
|
* for the default distance: pgvector's own default class is L2, which a cosine search never uses.
|
|
12
12
|
*/
|
|
13
|
-
protected indexColumnOpsClass(entry:
|
|
13
|
+
protected indexColumnOpsClass(entry: IndexEntry<string>, index: IndexSchema): string;
|
|
14
14
|
protected indexInclude(index: IndexSchema): string;
|
|
15
15
|
protected indexTuning(index: IndexSchema): string;
|
|
16
16
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IndexEntry, IndexSchema } from '../../type/index.js';
|
|
2
2
|
import { IndexDdl } from './indexDdl.js';
|
|
3
3
|
/**
|
|
4
4
|
* SQLite's `CREATE INDEX`, which names no index type. A vector index is libSQL's DiskANN where the dialect
|
|
5
5
|
* can index its metric, and a plain index on an engine with none, so an entity written for Postgres migrates.
|
|
6
6
|
*/
|
|
7
7
|
export declare class SqliteIndexDdl extends IndexDdl {
|
|
8
|
-
protected indexColumn(entry:
|
|
8
|
+
protected indexColumn(entry: IndexEntry<string>, index: IndexSchema): string;
|
|
9
9
|
/** The metric libSQL's index names, or `undefined` where the index is no vector index or the engine has none. */
|
|
10
10
|
private vectorIndexMetric;
|
|
11
11
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { canonicalToSql } from '../../schema/canonicalType.js';
|
|
2
2
|
import { diffSchemas, referentialActions } from '../../schema/schemaASTDiffer.js';
|
|
3
|
-
import { SqlExpression, writtenDefault } from '../../schema/sqlExpression.js';
|
|
3
|
+
import { SqlExpression, sqlExpressionText, writtenDefault } from '../../schema/sqlExpression.js';
|
|
4
4
|
/**
|
|
5
5
|
* Compare an expected schema (from entities) with an actual one (from the database) and report every
|
|
6
6
|
* way they have drifted apart.
|
|
@@ -219,7 +219,7 @@ function relationshipDrift(change) {
|
|
|
219
219
|
}
|
|
220
220
|
/** A default as a report shows it: SQL as its SQL, a JSON document as JSON rather than `[object Object]`. */
|
|
221
221
|
function shownDefault(value) {
|
|
222
|
-
return value == null ? 'NULL' : SqlExpression.isExpression(value) ?
|
|
222
|
+
return value == null ? 'NULL' : SqlExpression.isExpression(value) ? sqlExpressionText(value) : writtenDefault(value);
|
|
223
223
|
}
|
|
224
224
|
/** Both actions spelled out, so a side that left one unstated reads the same as one that stated the default. */
|
|
225
225
|
function formatActions(rel) {
|
|
@@ -14,10 +14,16 @@ export declare function bareColumn({ index: _index, foreignKey: _foreignKey, enu
|
|
|
14
14
|
/**
|
|
15
15
|
* The index a column-level `index` or `unique` declares, or nothing: a unique column is a unique index.
|
|
16
16
|
*
|
|
17
|
-
* Shared with `
|
|
17
|
+
* Shared with `tableBuilder`'s `build`, which lifts these into the table it is creating: written twice,
|
|
18
18
|
* `addColumn` had no lift at all and silently emitted a column with no index.
|
|
19
19
|
*/
|
|
20
|
-
export declare function columnIndex(tableName: string, col: FullColumnDefinition):
|
|
20
|
+
export declare function columnIndex(tableName: string, col: FullColumnDefinition): {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly entries: readonly [{
|
|
23
|
+
readonly column: string;
|
|
24
|
+
}];
|
|
25
|
+
readonly unique: boolean;
|
|
26
|
+
} | undefined;
|
|
21
27
|
/**
|
|
22
28
|
* The index that `table.index`, `table.unique` and `createIndex` record. Its entries are normalized, since
|
|
23
29
|
* an entry left as written reaches the generator as a column named `[object Object]`. An unnamed index is
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createTableNode, keyOfColumns } from '../../schema/schemaAST.js';
|
|
2
|
-
import {
|
|
2
|
+
import { namedIndex, enumCheck } from '../../util/ddlExpression.util.js';
|
|
3
|
+
import { normalizeIndexColumn, renderIndexColumn } from '../../util/indexEntry.util.js';
|
|
3
4
|
import { derivedIndexName, splitQualifiedName } from '../../util/sql.util.js';
|
|
4
5
|
/**
|
|
5
6
|
* A migration builder's table definition as the AST nodes the generators render from, so a hand-written
|
|
@@ -35,7 +36,7 @@ export function bareColumn({ index: _index, foreignKey: _foreignKey, enum: _enum
|
|
|
35
36
|
/**
|
|
36
37
|
* The index a column-level `index` or `unique` declares, or nothing: a unique column is a unique index.
|
|
37
38
|
*
|
|
38
|
-
* Shared with `
|
|
39
|
+
* Shared with `tableBuilder`'s `build`, which lifts these into the table it is creating: written twice,
|
|
39
40
|
* `addColumn` had no lift at all and silently emitted a column with no index.
|
|
40
41
|
*/
|
|
41
42
|
export function columnIndex(tableName, col) {
|
|
@@ -61,11 +62,9 @@ export function indexDefinition(columns, { name, unique = false, ...options } =
|
|
|
61
62
|
* from that text the way an entity's is.
|
|
62
63
|
*/
|
|
63
64
|
export function renderIndexDefinition(table, { uniqueName, ...index }, render) {
|
|
64
|
-
const where = index.where && render(index.where);
|
|
65
65
|
return {
|
|
66
66
|
...index,
|
|
67
|
-
|
|
68
|
-
where,
|
|
67
|
+
...namedIndex(index.name, table, index.entries, () => index.where && render(index.where), uniqueName),
|
|
69
68
|
entries: index.entries.map((entry) => renderIndexColumn(entry, render)),
|
|
70
69
|
};
|
|
71
70
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { SqlExpression } from '../../schema/sqlExpression.js';
|
|
2
2
|
import { FOREIGN_KEY_ACTIONS } from '../../schema/types.js';
|
|
3
|
+
import { entryModifiers } from '../../util/indexEntry.util.js';
|
|
3
4
|
import { cachedIn } from '../../util/object.util.js';
|
|
4
5
|
import { sql } from '../../util/sql.js';
|
|
5
6
|
import { isOwnedName } from '../../util/sql.util.js';
|
|
6
7
|
import { withSqlQuerierForMigrations } from '../acquireQuerierForMigrations.js';
|
|
7
8
|
import { knownDefault } from '../ddl/defaultSql.js';
|
|
9
|
+
import { readIndexEntry } from './jsonIndexEntry.js';
|
|
8
10
|
import { renamedTable, tableSchemasToAST } from './tableSchemaAST.js';
|
|
9
11
|
/** A SQL introspector: an engine reads each part of a table from its catalogue (`get*`), on a {@link TableRowReader}. */
|
|
10
12
|
export class AbstractSqlSchemaIntrospector {
|
|
@@ -76,7 +78,7 @@ export class AbstractSqlSchemaIntrospector {
|
|
|
76
78
|
isUnique: unique.has(column.name),
|
|
77
79
|
})),
|
|
78
80
|
primaryKey,
|
|
79
|
-
indexes,
|
|
81
|
+
indexes: indexes.map((index) => ({ ...index, entries: index.entries.map(readEntry) })),
|
|
80
82
|
foreignKeys,
|
|
81
83
|
checks,
|
|
82
84
|
triggers,
|
|
@@ -153,9 +155,16 @@ export class AbstractSqlSchemaIntrospector {
|
|
|
153
155
|
return knownDefault(SqlExpression.parenthesized(sql), this.dialect);
|
|
154
156
|
}
|
|
155
157
|
}
|
|
158
|
+
/** An entry the engine reports as an expression, read back as the JSON path uql compiled it from where it is one. */
|
|
159
|
+
function readEntry(entry) {
|
|
160
|
+
return entry.expression === undefined ? entry : { ...entryModifiers(entry), ...readIndexEntry(entry.expression) };
|
|
161
|
+
}
|
|
156
162
|
/** The columns a unique index makes unique alone: over that one plain column, unfiltered. The key's is not among them. */
|
|
157
163
|
function uniqueColumns(indexes) {
|
|
158
|
-
return new Set(indexes.flatMap(({ unique, where, entries }) =>
|
|
164
|
+
return new Set(indexes.flatMap(({ unique, where, entries }) => {
|
|
165
|
+
const column = entries.length === 1 ? entries[0].column : undefined;
|
|
166
|
+
return unique && !where && column !== undefined ? [column] : [];
|
|
167
|
+
}));
|
|
159
168
|
}
|
|
160
169
|
/** A {@link TableRowReader} over one querier: the same statement is only ever sent once. */
|
|
161
170
|
function createTableRowReader(querier) {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { IndexEntry } from '../../type/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* An index entry an engine reports as an expression, read back as the JSON path entry uql compiled it from where it is
|
|
4
|
+
* one (`{ json, path, type, charLength }`), as MongoDB reports a path; the expression where it is anything else.
|
|
5
|
+
*/
|
|
6
|
+
export declare function readIndexEntry(expression: string): IndexEntry<string>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { JSON_KEY_PATTERN } from '../../util/field.util.js';
|
|
2
|
+
/** One token of an index expression as an engine reprints it; anything else leaves the expression unread. */
|
|
3
|
+
const TOKEN = /\s+|'(?:[^']|'')*'|"(?:[^"]|"")+"|`(?:[^`]|``)+`|->>|->|::|[(),=]|\d+|[A-Za-z_][\w$]*/y;
|
|
4
|
+
/** The words uql's JSON path reads reprint as on every engine; any other word is the column, or something uql never wrote. */
|
|
5
|
+
const READ_WORDS = new Set([
|
|
6
|
+
'array',
|
|
7
|
+
'as',
|
|
8
|
+
'case',
|
|
9
|
+
'cast',
|
|
10
|
+
'char',
|
|
11
|
+
'charset',
|
|
12
|
+
'collate',
|
|
13
|
+
'datetime',
|
|
14
|
+
'double',
|
|
15
|
+
'else',
|
|
16
|
+
'end',
|
|
17
|
+
'json_extract',
|
|
18
|
+
'json_unquote',
|
|
19
|
+
'jsonb_typeof',
|
|
20
|
+
'null',
|
|
21
|
+
'numeric',
|
|
22
|
+
'real',
|
|
23
|
+
'signed',
|
|
24
|
+
'text',
|
|
25
|
+
'then',
|
|
26
|
+
'utf8mb4',
|
|
27
|
+
'utf8mb4_bin',
|
|
28
|
+
'when',
|
|
29
|
+
]);
|
|
30
|
+
/** The element types MySQL's `CAST(... AS <type> ARRAY)` reprints, as uql declares them. */
|
|
31
|
+
const ARRAY_TYPES = new Map([
|
|
32
|
+
['char', String],
|
|
33
|
+
['signed', Number],
|
|
34
|
+
['datetime', Date],
|
|
35
|
+
]);
|
|
36
|
+
const PATH_LITERAL = new RegExp(`^'\\$((?:\\.${JSON_KEY_PATTERN})*)'$`, 'u');
|
|
37
|
+
const KEY_LITERAL = new RegExp(`^'(${JSON_KEY_PATTERN})'$`, 'u');
|
|
38
|
+
/**
|
|
39
|
+
* An index entry an engine reports as an expression, read back as the JSON path entry uql compiled it from where it is
|
|
40
|
+
* one (`{ json, path, type, charLength }`), as MongoDB reports a path; the expression where it is anything else.
|
|
41
|
+
*/
|
|
42
|
+
export function readIndexEntry(expression) {
|
|
43
|
+
return jsonIndexEntry(expression) ?? { expression };
|
|
44
|
+
}
|
|
45
|
+
function jsonIndexEntry(expression) {
|
|
46
|
+
const tokens = tokenize(expression);
|
|
47
|
+
if (!tokens) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const then = tokens.findIndex((token) => token.word === 'then');
|
|
51
|
+
const read = then < 0 ? tokens : tokens.slice(then + 1);
|
|
52
|
+
const columns = new Set(tokens.flatMap(identifier));
|
|
53
|
+
const path = pathOf(tokens, read);
|
|
54
|
+
const [json] = columns;
|
|
55
|
+
if (columns.size !== 1 || json === undefined || path === undefined) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const typed = typeOf(read);
|
|
59
|
+
if (!typed) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return { json, ...(path && { path }), ...typed };
|
|
63
|
+
}
|
|
64
|
+
/** The tokens of `expression`, `undefined` where one is no token uql's reads reprint as. */
|
|
65
|
+
function tokenize(expression) {
|
|
66
|
+
const tokens = [];
|
|
67
|
+
TOKEN.lastIndex = 0;
|
|
68
|
+
while (TOKEN.lastIndex < expression.length) {
|
|
69
|
+
const match = TOKEN.exec(expression);
|
|
70
|
+
if (!match) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
if (match[0].trim()) {
|
|
74
|
+
tokens.push({ text: match[0], word: /^[A-Za-z_]/.test(match[0]) ? match[0].toLowerCase() : '' });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return tokens;
|
|
78
|
+
}
|
|
79
|
+
/** The column a token names: a quoted identifier, or a word no read reprints as. */
|
|
80
|
+
function identifier({ text, word }) {
|
|
81
|
+
const quoted = /^(["`])(.*)\1$/s.exec(text);
|
|
82
|
+
if (quoted) {
|
|
83
|
+
return [quoted[2].replaceAll(quoted[1] + quoted[1], quoted[1])];
|
|
84
|
+
}
|
|
85
|
+
return word && !READ_WORDS.has(word) ? [text] : [];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The dotted path the read reaches: a `'$.a.b'` literal (MySQL, SQLite), or Postgres's chain of `-> 'a'` keys, read
|
|
89
|
+
* past a `CASE`'s type test. `undefined` where a literal is anything else, which no read of uql's holds.
|
|
90
|
+
*/
|
|
91
|
+
function pathOf(tokens, read) {
|
|
92
|
+
const typeTest = tokens.some((token) => token.word === 'case');
|
|
93
|
+
const keyOf = (all, at) => at > 0 && all[at - 1].text.startsWith('->') ? KEY_LITERAL.exec(all[at].text)?.[1] : undefined;
|
|
94
|
+
const known = tokens.every(({ text }, at) => !text.startsWith("'") ||
|
|
95
|
+
PATH_LITERAL.test(text) ||
|
|
96
|
+
keyOf(tokens, at) !== undefined ||
|
|
97
|
+
(typeTest && text === "'number'"));
|
|
98
|
+
const paths = tokens.flatMap((token) => PATH_LITERAL.exec(token.text)?.[1] ?? []);
|
|
99
|
+
if (!known || paths.length > 1) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
return paths.length ? paths[0].slice(1) : read.flatMap((_, at) => keyOf(read, at) ?? []).join('.');
|
|
103
|
+
}
|
|
104
|
+
/** The type and key length a read casts its value to: none for text, a number, a JSON value (a boolean) or each element. */
|
|
105
|
+
function typeOf(read) {
|
|
106
|
+
const words = read.map((token) => token.word);
|
|
107
|
+
const charLength = words.includes('char') ? Number(read[words.indexOf('char') + 2]?.text) : undefined;
|
|
108
|
+
const sized = charLength === undefined ? {} : { charLength };
|
|
109
|
+
const elements = words.indexOf('array');
|
|
110
|
+
if (elements >= 0) {
|
|
111
|
+
const cast = words.slice(0, elements).findLast((word) => ARRAY_TYPES.has(word));
|
|
112
|
+
const type = cast === undefined ? undefined : ARRAY_TYPES.get(cast);
|
|
113
|
+
return type && { type: [type], ...sized };
|
|
114
|
+
}
|
|
115
|
+
if (words.some((word) => word === 'double' || word === 'real' || word === 'numeric')) {
|
|
116
|
+
return { type: Number };
|
|
117
|
+
}
|
|
118
|
+
const asJson = !words.some((word) => word === 'json_extract') && !read.some((token) => token.text === '->>');
|
|
119
|
+
return asJson ? { type: Boolean } : sized;
|
|
120
|
+
}
|
|
@@ -18,6 +18,8 @@ export declare class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospect
|
|
|
18
18
|
protected tableExistsQuery(tableName: string): QuerySql;
|
|
19
19
|
protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
|
|
20
20
|
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
21
|
+
/** A functional key part's text, which MariaDB, having none, does not report. */
|
|
22
|
+
protected readonly indexExpressionSql: QuerySql;
|
|
21
23
|
protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;
|
|
22
24
|
/** Unnamed: MySQL calls every key `PRIMARY`, and drops one by no name. */
|
|
23
25
|
protected getPrimaryKey(read: TableRowReader, tableName: string): Promise<PrimaryKeySchema | undefined>;
|
|
@@ -27,10 +29,7 @@ export declare class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospect
|
|
|
27
29
|
* `_utf8mb4\'x\'`.
|
|
28
30
|
*/
|
|
29
31
|
protected parseDefaultValue(defaultValue: string | null, extra?: string): unknown;
|
|
30
|
-
/**
|
|
31
|
-
* A default's SQL, or `undefined` for a literal. MySQL flags a SQL default `DEFAULT_GENERATED` and prints
|
|
32
|
-
* it escaped like a literal, with charset introducers on its strings: `lower(_utf8mb4\'A\')`.
|
|
33
|
-
*/
|
|
32
|
+
/** A default's SQL, or `undefined` for a literal: MySQL flags a SQL default `DEFAULT_GENERATED`. */
|
|
34
33
|
protected sqlText(defaultValue: string, extra: string): string | undefined;
|
|
35
34
|
}
|
|
36
35
|
/**
|
|
@@ -51,6 +50,7 @@ export declare class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
51
50
|
protected sqlText(defaultValue: string): string | undefined;
|
|
52
51
|
/** Whether an index is MariaDB's vector index, and the distance it was built for. */
|
|
53
52
|
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
53
|
+
protected readonly indexExpressionSql: QuerySql;
|
|
54
54
|
/**
|
|
55
55
|
* A vector index's distance is kept only in the table's own definition, ``VECTOR KEY `ix` (`vec`)
|
|
56
56
|
* `DISTANCE`='cosine'``, and left out there for MariaDB's default, euclidean.
|
|
@@ -86,26 +86,30 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
86
86
|
const rows = await read(sql `
|
|
87
87
|
SELECT
|
|
88
88
|
INDEX_NAME as index_name,
|
|
89
|
-
|
|
89
|
+
COLUMN_NAME as column_name,
|
|
90
|
+
${this.indexExpressionSql} as expression,
|
|
90
91
|
NOT NON_UNIQUE as is_unique,
|
|
91
|
-
|
|
92
|
+
INDEX_TYPE as method
|
|
92
93
|
FROM information_schema.STATISTICS
|
|
93
94
|
WHERE TABLE_SCHEMA = ${this.schemaExpr}
|
|
94
95
|
AND TABLE_NAME = ${tableName}
|
|
95
96
|
AND INDEX_NAME != 'PRIMARY'
|
|
96
|
-
|
|
97
|
-
ORDER BY INDEX_NAME
|
|
97
|
+
ORDER BY INDEX_NAME, SEQ_IN_INDEX
|
|
98
98
|
`);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
//
|
|
104
|
-
// from comparing an entry list the server cannot state against the entity's own.
|
|
105
|
-
entries:
|
|
106
|
-
|
|
99
|
+
// One row per key part, ordered by position, so the rows of an index are its entries in order.
|
|
100
|
+
return [...Map.groupBy(rows, (row) => row.index_name)].map(([name, parts]) => ({
|
|
101
|
+
name,
|
|
102
|
+
...(parts[0].method === 'VECTOR' && { type: 'vector' }),
|
|
103
|
+
// A functional or multi-valued key part has no `COLUMN_NAME`: it is reported as the expression it is, which is
|
|
104
|
+
// what stops diffing from comparing an entry list the server cannot state against the entity's own.
|
|
105
|
+
entries: parts.map(({ column_name, expression }) => column_name === null
|
|
106
|
+
? { expression: expression === null ? '' : mysqlSqlText(expression) }
|
|
107
|
+
: { column: column_name }),
|
|
108
|
+
unique: Boolean(parts[0].is_unique),
|
|
107
109
|
}));
|
|
108
110
|
}
|
|
111
|
+
/** A functional key part's text, which MariaDB, having none, does not report. */
|
|
112
|
+
indexExpressionSql = sql `EXPRESSION`;
|
|
109
113
|
async getForeignKeys(read, tableName) {
|
|
110
114
|
const rows = await read(sql `
|
|
111
115
|
SELECT
|
|
@@ -158,21 +162,16 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
158
162
|
if (sql !== undefined) {
|
|
159
163
|
// Any precision here repeats the column's own, which the column type already states.
|
|
160
164
|
return /^CURRENT_TIMESTAMP(?:\(\d?\))?$/i.test(sql)
|
|
161
|
-
?
|
|
165
|
+
? SqlExpression.named('currentTimestamp')
|
|
162
166
|
: this.sqlDefault(sql);
|
|
163
167
|
}
|
|
164
168
|
const literal = introduced ? unescapeMysqlString(introduced[1]) : defaultValue;
|
|
165
169
|
const quoted = /^'(.*)'$/s.exec(literal);
|
|
166
170
|
return quoted ? unescapeMysqlString(quoted[1]) : literal;
|
|
167
171
|
}
|
|
168
|
-
/**
|
|
169
|
-
* A default's SQL, or `undefined` for a literal. MySQL flags a SQL default `DEFAULT_GENERATED` and prints
|
|
170
|
-
* it escaped like a literal, with charset introducers on its strings: `lower(_utf8mb4\'A\')`.
|
|
171
|
-
*/
|
|
172
|
+
/** A default's SQL, or `undefined` for a literal: MySQL flags a SQL default `DEFAULT_GENERATED`. */
|
|
172
173
|
sqlText(defaultValue, extra) {
|
|
173
|
-
return extra.includes('DEFAULT_GENERATED')
|
|
174
|
-
? unescapeMysqlString(defaultValue).replace(/(?<![\w'])_[a-z0-9]+(?=')/gi, '')
|
|
175
|
-
: undefined;
|
|
174
|
+
return extra.includes('DEFAULT_GENERATED') ? mysqlSqlText(defaultValue) : undefined;
|
|
176
175
|
}
|
|
177
176
|
}
|
|
178
177
|
const TIMESTAMP_TYPES = new Set(['datetime', 'timestamp']);
|
|
@@ -203,6 +202,7 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
203
202
|
}
|
|
204
203
|
/** Whether an index is MariaDB's vector index, and the distance it was built for. */
|
|
205
204
|
indexFacets = new Set(['vector', 'distance']);
|
|
205
|
+
indexExpressionSql = sql `NULL`;
|
|
206
206
|
/**
|
|
207
207
|
* A vector index's distance is kept only in the table's own definition, ``VECTOR KEY `ix` (`vec`)
|
|
208
208
|
* `DISTANCE`='cosine'``, and left out there for MariaDB's default, euclidean.
|
|
@@ -223,7 +223,7 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
223
223
|
}
|
|
224
224
|
const line = lines.find((it) => it.includes(`VECTOR KEY \`${index.name}\``));
|
|
225
225
|
const metric = line?.match(/`DISTANCE`='(\w+)'/)?.[1] ?? 'euclidean';
|
|
226
|
-
return { ...index, distance: this.dialect.
|
|
226
|
+
return { ...index, distance: this.dialect.distanceOfMetric(metric) };
|
|
227
227
|
});
|
|
228
228
|
}
|
|
229
229
|
async getColumns(read, tableName) {
|
|
@@ -240,3 +240,7 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
240
240
|
return columns.map((column) => jsonColumns.has(column.name) ? { ...column, type: 'JSON', length: undefined } : column);
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
|
+
/** SQL as MySQL's catalogue prints it, escaped like a literal, with charset introducers on its strings: `lower(_utf8mb4\'A\')`. */
|
|
244
|
+
function mysqlSqlText(text) {
|
|
245
|
+
return unescapeMysqlString(text).replace(/(?<![\w'])_[a-z0-9]+(?=')/gi, '');
|
|
246
|
+
}
|
|
@@ -40,7 +40,7 @@ export declare class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrosp
|
|
|
40
40
|
*
|
|
41
41
|
* The key's index and an `EXCLUDE`'s are left out. A `UNIQUE` constraint's index stays, as SQL Server
|
|
42
42
|
* and the MySQL family report theirs: the diff reads one over a single column as that column's
|
|
43
|
-
* uniqueness, and one over several as the unique
|
|
43
|
+
* uniqueness, and one over several as the unique index it is.
|
|
44
44
|
*/
|
|
45
45
|
protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
|
|
46
46
|
/** Whether an entry sorts nulls first, which Postgres states on every entry. */
|