imodel-pg 0.19.1 → 0.19.2
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/index.d.mts +1 -1
- package/index.mjs +42 -6
- package/package.json +1 -1
package/index.d.mts
CHANGED
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.19.
|
|
2
|
+
* imodel v0.19.2
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -80,6 +80,7 @@ function getBaseType(s, {
|
|
|
80
80
|
case 'json':
|
|
81
81
|
return 'json';
|
|
82
82
|
}
|
|
83
|
+
return s;
|
|
83
84
|
}
|
|
84
85
|
/**
|
|
85
86
|
*
|
|
@@ -522,14 +523,17 @@ function buildSelect(env, selectList) {
|
|
|
522
523
|
offset,
|
|
523
524
|
limit,
|
|
524
525
|
group,
|
|
525
|
-
having
|
|
526
|
+
having,
|
|
527
|
+
lock
|
|
526
528
|
} of selectList) {
|
|
527
529
|
const Table = Sql.Table(table);
|
|
528
530
|
const selectSql = getSelect(Table, select);
|
|
529
531
|
const orderSql = getOrder(sort);
|
|
530
532
|
const limitSql = limit && limit > 0 ? Sql('LIMIT', limit) : undefined;
|
|
531
533
|
const offsetSql = offset && offset > 0 ? Sql('OFFSET', offset) : undefined;
|
|
532
|
-
list.push(Sql(Sql`SELECT`, distinct ? Sql`DISTINCT` : undefined, Sql`${selectSql}`, Sql`FROM ${Table}`, getWhere(env, where), orderSql, limitSql, offsetSql, group?.length ? Sql`GROUP BY ${Sql`,`.glue(group.map(v => Sql.Field(v)))}` : undefined, getWhere(env, having, 'HAVING')
|
|
534
|
+
list.push(Sql(Sql`SELECT`, distinct ? Sql`DISTINCT` : undefined, Sql`${selectSql}`, Sql`FROM ${Table}`, getWhere(env, where), orderSql, limitSql, offsetSql, group?.length ? Sql`GROUP BY ${Sql`,`.glue(group.map(v => Sql.Field(v)))}` : undefined, getWhere(env, having, 'HAVING'),
|
|
535
|
+
// eslint-disable-next-line no-nested-ternary
|
|
536
|
+
lock === 'X' ? Sql`FOR UPDATE` : lock === 'S' ? Sql`FOR SHARE` : undefined));
|
|
533
537
|
}
|
|
534
538
|
if (!list.length) {
|
|
535
539
|
return null;
|
|
@@ -1316,6 +1320,35 @@ async function loadTables(env, query, tables, schema) {
|
|
|
1316
1320
|
return dbTables;
|
|
1317
1321
|
}
|
|
1318
1322
|
|
|
1323
|
+
/**
|
|
1324
|
+
* 生成 PostgreSQL ALTER TABLE 语句中强制类型转换的 USING 子句
|
|
1325
|
+
* @param {Sql} COLUMN - 列名
|
|
1326
|
+
* @param {boolean?} [array]
|
|
1327
|
+
* @param {boolean?} [nullable]
|
|
1328
|
+
* @param {boolean?} [oldArray]
|
|
1329
|
+
* @param {Sql?} [def]
|
|
1330
|
+
* @returns {Sql}
|
|
1331
|
+
*/
|
|
1332
|
+
function generateUsingClause(COLUMN, array, nullable, oldArray, def) {
|
|
1333
|
+
if (array && !oldArray) {
|
|
1334
|
+
if (nullable) {
|
|
1335
|
+
return Sql`(CASE
|
|
1336
|
+
WHEN ${COLUMN} IS NULL THEN NULL
|
|
1337
|
+
ELSE ARRAY[${COLUMN}]
|
|
1338
|
+
END)`;
|
|
1339
|
+
}
|
|
1340
|
+
return Sql`(CASE
|
|
1341
|
+
WHEN ${COLUMN} IS NULL THEN ARRAY[]
|
|
1342
|
+
ELSE ARRAY[${COLUMN}]
|
|
1343
|
+
END)`;
|
|
1344
|
+
}
|
|
1345
|
+
const val = oldArray && !array ? Sql`${COLUMN}[1]` : Sql`${COLUMN}`;
|
|
1346
|
+
if (nullable || !def) {
|
|
1347
|
+
return val;
|
|
1348
|
+
}
|
|
1349
|
+
return Sql`COALESCE(${val}, ${def})`;
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1319
1352
|
/** @import { Environment, IConnection } from 'imodel' */
|
|
1320
1353
|
/** @import { PgEnvTrans } from '../index.mjs' */
|
|
1321
1354
|
/** @import { DBIndex, DBTable, DBColumn } from 'imodel' */
|
|
@@ -1639,15 +1672,18 @@ WHERE table_name = ${table} AND constraint_type = 'PRIMARY KEY'
|
|
|
1639
1672
|
size
|
|
1640
1673
|
}, array);
|
|
1641
1674
|
const oldType = getType(old.type, old, old.array);
|
|
1642
|
-
|
|
1643
|
-
|
|
1675
|
+
const def = getDefault(env, defaultValue, type, array);
|
|
1676
|
+
if (newType !== oldType || !nullable && old.nullable) {
|
|
1677
|
+
console.log(newType, oldType);
|
|
1678
|
+
const usingClause = generateUsingClause(Sql.Field(fieldName), array, nullable, old.array, def);
|
|
1679
|
+
const type = Sql(newType);
|
|
1680
|
+
COLUMNs.push(Sql`${COLUMN} TYPE ${type} USING ${usingClause}::${type}`);
|
|
1644
1681
|
// TODO: USING "description"::int2
|
|
1645
1682
|
}
|
|
1646
1683
|
if (Boolean(nullable) !== Boolean(old.nullable)) {
|
|
1647
1684
|
COLUMNs.push(nullable ? Sql`${COLUMN} DROP NOT NULL` : Sql`${COLUMN} SET NOT NULL`);
|
|
1648
1685
|
}
|
|
1649
1686
|
if (!defaultIsQe(defaultValue, old.default)) {
|
|
1650
|
-
const def = getDefault(env, defaultValue, type, array);
|
|
1651
1687
|
COLUMNs.push(def ? Sql`${COLUMN} SET DEFAULT ${def}` : Sql`${COLUMN} DROP DEFAULT`);
|
|
1652
1688
|
}
|
|
1653
1689
|
}
|