@asla/yoursql 0.3.1 → 0.4.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/dist/mod.js CHANGED
@@ -54,7 +54,91 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
54
54
  }
55
55
  return Array.from(keys);
56
56
  }
57
+ /** @public */
58
+ function genWhere(where, type = "AND") {
59
+ return genWhereHaving(where, "WHERE", type);
60
+ }
61
+ /** @public */
62
+ function genHaving(having, type = "AND") {
63
+ return genWhereHaving(having, "HAVING", type);
64
+ }
65
+ /**
66
+ * @public
67
+ * ```ts
68
+ * genSelect({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
69
+ * genSelect(["c1", "count(*) AS c2", "column as c3"]) // "c1,count(*) AS c2,column as c3"
70
+ * ```
71
+ */
72
+ function genSelect(columns) {
73
+ let sql = "";
74
+ if (columns instanceof Array) {
75
+ if (columns.length)
76
+ sql += columns[0];
77
+ else
78
+ throw new Error("没有选择任何列");
79
+ for (let i = 1; i < columns.length; i++)
80
+ sql += "," + columns[i];
81
+ }
82
+ else {
83
+ const keys = Object.keys(columns);
84
+ if (keys.length === 0)
85
+ throw new Error("没有选择任何列");
86
+ let k = keys[0];
87
+ let v = columns[k];
88
+ if (typeof v === "string")
89
+ sql += v + " AS " + k;
90
+ else
91
+ sql += k;
92
+ for (let i = 1; i < keys.length; i++) {
93
+ k = keys[i];
94
+ v = columns[k];
95
+ sql += ",";
96
+ if (typeof v === "string")
97
+ sql += v + " AS " + k;
98
+ else
99
+ sql += k;
100
+ }
101
+ }
102
+ return sql;
103
+ }
104
+ /** @public */
105
+ function genOderBy(orderBy) {
106
+ if (typeof orderBy === "function")
107
+ orderBy = orderBy();
108
+ let sql = "";
109
+ if (typeof orderBy === "string")
110
+ sql += "\nORDER BY " + orderBy;
111
+ else if (orderBy instanceof Array) {
112
+ if (orderBy.length)
113
+ sql += "\nORDER BY " + orderBy.join(",");
114
+ }
115
+ else {
116
+ let keys = Object.keys(orderBy);
117
+ if (keys.length)
118
+ sql += "\nORDER BY " + keys[0] + " " + orderBy[keys[0]];
119
+ for (let i = 1; i < keys.length; i++) {
120
+ sql += "," + orderBy[keys[i]] + " " + orderBy[keys[i]];
121
+ }
122
+ }
123
+ return sql;
124
+ }
125
+ function genWhereHaving(where, statement, type = "AND") {
126
+ if (typeof where === "function")
127
+ where = where();
128
+ type = " " + type + " ";
129
+ let sql = "";
130
+ if (typeof where === "string")
131
+ sql += "\n" + statement + " " + where;
132
+ else {
133
+ if (where.length)
134
+ sql += "\n" + statement + " " + +where[0];
135
+ for (let i = 1; i < where.length; i++)
136
+ sql += type + where[i];
137
+ }
138
+ return sql;
139
+ }
57
140
 
141
+ var _SqlQueryStatement_sql;
58
142
  /**
59
143
  * 可选择项。可以是 table、查询结果等,它能被 select 语句选择
60
144
  * @example
@@ -116,16 +200,22 @@ class DbTable extends SqlSelectable {
116
200
  */
117
201
  class SqlQueryStatement extends SqlSelectable {
118
202
  constructor(sql, columns) {
203
+ if (sql instanceof SqlQueryStatement) {
204
+ columns = sql.columns;
205
+ sql = __classPrivateFieldGet(sql, _SqlQueryStatement_sql, "f");
206
+ }
119
207
  super(columns);
120
- this.sql = sql;
208
+ _SqlQueryStatement_sql.set(this, void 0);
209
+ __classPrivateFieldSet(this, _SqlQueryStatement_sql, sql, "f");
121
210
  }
122
211
  toString() {
123
- return this.sql;
212
+ return __classPrivateFieldGet(this, _SqlQueryStatement_sql, "f");
124
213
  }
125
214
  toSelect() {
126
215
  return "(" + this.toString() + ")";
127
216
  }
128
217
  }
218
+ _SqlQueryStatement_sql = new WeakMap();
129
219
 
130
220
  var _SqlRaw_value, _YourValuesAs_asName, _YourValuesAs_valuesStr, _YourValuesAs_sql;
131
221
  /**
@@ -394,35 +484,98 @@ const pgSqlTransformer = new Map([
394
484
  [Date, (value) => SqlValuesCreator.string(value.toISOString())],
395
485
  ]);
396
486
 
397
- function genOderBy(orderBy, orderNullRule) {
398
- let select = [];
399
- let v;
400
- for (const key of Object.keys(orderBy)) {
401
- v = orderBy[key];
402
- switch (v) {
403
- case "ASC":
404
- break;
405
- case "DESC":
406
- break;
407
- default:
408
- throw new Error("orderBy 只能是 ASE 或 DESC. 当前值:" + String(v));
409
- }
410
- select.push(key + " " + v);
487
+ var _SelectImpl_instances, _a, _SelectImpl_join;
488
+ class FinalSelectImpl extends SqlQueryStatement {
489
+ constructor(statement) {
490
+ super(statement);
411
491
  }
412
- let sql = "\nORDER BY " + select.join(", ");
413
- if (orderNullRule) {
414
- switch (orderNullRule) {
415
- case "FIRST":
416
- break;
417
- case "LAST":
418
- break;
419
- default:
420
- throw new Error("orderNullRule 只能是 FIRST 或 LAST. 当前值:" + String(orderNullRule));
421
- }
422
- sql += "NULLS " + orderNullRule;
492
+ filter(option) {
493
+ let { orderBy } = option;
494
+ let sql = this.toString();
495
+ if (orderBy)
496
+ sql += genOderBy(orderBy);
497
+ if (option.limit)
498
+ sql += "\nLIMIT " + option.limit;
499
+ if (option.offset)
500
+ sql += "\nOFFSET " + option.offset;
501
+ return new SqlQueryStatement(sql, Array.from(this.columns));
423
502
  }
503
+ }
504
+ /** @public */
505
+ function createSelect(selectable, as) {
506
+ return new SelectImpl("FROM " + fromAs(selectable, as));
507
+ }
508
+ function fromAs(selectable, as) {
509
+ let sql = typeof selectable === "string" ? selectable : selectable.toSelect();
510
+ if (as)
511
+ sql += " AS " + as;
424
512
  return sql;
425
513
  }
514
+ class SelectImpl {
515
+ constructor(str) {
516
+ _SelectImpl_instances.add(this);
517
+ this.str = str;
518
+ }
519
+ toString() {
520
+ return this.str;
521
+ }
522
+ fullJoin(selectable, as, on) {
523
+ return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "FULL JOIN", selectable, as, on);
524
+ }
525
+ innerJoin(selectable, as, on) {
526
+ return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "INNER JOIN", selectable, as, on);
527
+ }
528
+ leftJoin(selectable, as, on) {
529
+ return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "LEFT JOIN", selectable, as, on);
530
+ }
531
+ rightJoin(selectable, as, on) {
532
+ return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "RIGHT JOIN", selectable, as, on);
533
+ }
534
+ naturalJoin(selectable, as) {
535
+ return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "NATURAL JOIN", selectable, as);
536
+ }
537
+ crossJoin(selectable, as) {
538
+ return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "CROSS JOIN", selectable, as);
539
+ }
540
+ from(selectable, as) {
541
+ return new _a(this.str + "," + fromAs(selectable, as));
542
+ }
543
+ select(columnsIn) {
544
+ let sql = "SELECT ";
545
+ let columns = [];
546
+ if (typeof columnsIn === "string")
547
+ sql += columnsIn;
548
+ else {
549
+ sql += genSelect(columnsIn);
550
+ if (columnsIn instanceof Array) ;
551
+ else
552
+ columns = Object.keys(columnsIn);
553
+ }
554
+ sql += "\n" + this.str;
555
+ return new FinalSelectImpl(new SqlQueryStatement(sql, columns));
556
+ }
557
+ groupBy(columns) {
558
+ let sql = this.str;
559
+ if (typeof columns === "string")
560
+ sql += " GROUP BY " + columns;
561
+ else
562
+ sql += " GROUP BY " + columns.join(",");
563
+ return new _a(sql);
564
+ }
565
+ having(param) {
566
+ return new _a(this.str + genWhere(param));
567
+ }
568
+ where(param) {
569
+ return new _a(this.str + genWhere(param));
570
+ }
571
+ }
572
+ _a = SelectImpl, _SelectImpl_instances = new WeakSet(), _SelectImpl_join = function _SelectImpl_join(type, selectable, as, on) {
573
+ let sql = this.str + "\n" + type + " " + fromAs(selectable, as);
574
+ if (on)
575
+ sql += " ON " + on;
576
+ return new _a(sql);
577
+ };
578
+
426
579
  function selectColumnsOrTable(columns) {
427
580
  let sqlSelect;
428
581
  let select;
@@ -450,226 +603,6 @@ function selectColumnsOrTable(columns) {
450
603
  throw new Error("选择列为空");
451
604
  return { columns: select, sqlColumns: sqlSelect.join(", ") };
452
605
  }
453
- /**
454
- * @param select 选择的行
455
- * @param tableColumns 全部行
456
- * @param push 要讲选择的行加入到集合中。
457
- */
458
- function genNewColumns(select, tableColumns,
459
- /** newName -> oldName */
460
- push = new Map()) {
461
- if (select === "*") {
462
- for (const key of tableColumns) {
463
- if (push.has(key))
464
- throw new ColumnRepeatError(key);
465
- push.set(key, null);
466
- }
467
- }
468
- else {
469
- genSelectAsColumns(select, push);
470
- }
471
- return push;
472
- }
473
- function genSelectAsColumns(select,
474
- /** newName -> oldName */
475
- push = new Map()) {
476
- if (select instanceof Array) {
477
- for (const key of select) {
478
- if (push.has(key))
479
- throw new ColumnRepeatError(key);
480
- push.set(key, null);
481
- }
482
- }
483
- else {
484
- let finalName;
485
- for (const oldName of Object.keys(select)) {
486
- if (typeof select[oldName] === "string")
487
- finalName = select[oldName];
488
- else if (select[oldName])
489
- finalName = oldName;
490
- else
491
- continue;
492
- if (push.has(finalName))
493
- throw new ColumnRepeatError(finalName);
494
- push.set(finalName, oldName);
495
- }
496
- }
497
- return push;
498
- }
499
- class ColumnRepeatError extends Error {
500
- constructor(columnName) {
501
- super("Column name '" + columnName + "' repeated");
502
- }
503
- }
504
-
505
- var _SelectImpl_instances, _a, _SelectImpl_joinOn, _SelectImpl_join;
506
- /** @public */
507
- const createSelect = function createSelect(tb, columns, option) {
508
- let select = createSelectMap(tb, columns, option).selects;
509
- return new SelectImpl(select);
510
- };
511
- function* mergeColumns(a, b) {
512
- for (const { columns } of a)
513
- yield* columns.keys();
514
- yield* b;
515
- }
516
- class SelectImpl extends SqlSelectable {
517
- constructor(tableList, addedColumns) {
518
- super(addedColumns ? mergeColumns(tableList.values(), addedColumns.keys()) : tableList.keys());
519
- _SelectImpl_instances.add(this);
520
- this.tableList = tableList;
521
- this.addedColumns = addedColumns;
522
- }
523
- toSelect() {
524
- return "(" + this.toString() + ")";
525
- }
526
- toString() {
527
- let tables = [];
528
- let join = "";
529
- let selectColumns = [];
530
- const size = this.tableList.size;
531
- for (const [tableAs, { columns, from, on, type }] of this.tableList) {
532
- let tableStr = tableAs === from ? from : from + " AS " + tableAs;
533
- if (type) {
534
- join += "\n" + type + " " + tableStr;
535
- if (on)
536
- join += " ON " + on;
537
- }
538
- else {
539
- tables.push(tableStr);
540
- }
541
- if (size === 1 && tableAs === from) {
542
- for (const [column, oldName] of columns) {
543
- if (!oldName || oldName == column)
544
- selectColumns.push(column);
545
- else
546
- selectColumns.push(oldName + " AS " + column);
547
- }
548
- }
549
- else {
550
- for (const [column, oldName] of columns) {
551
- if (!oldName || oldName === column)
552
- selectColumns.push(tableAs + "." + column);
553
- else
554
- selectColumns.push(tableAs + "." + oldName + " AS " + column);
555
- }
556
- }
557
- }
558
- if (this.addedColumns) {
559
- for (const [asName, columnStr] of this.addedColumns) {
560
- if (asName === columnStr)
561
- selectColumns.push(asName);
562
- else
563
- selectColumns.push(columnStr + " AS " + asName);
564
- }
565
- }
566
- if (selectColumns.length === 0)
567
- throw new Error("Columns 为空");
568
- let sql = `SELECT ${selectColumns.join(", ")}\nFROM ${tables.join(", ")}` + join;
569
- return sql;
570
- }
571
- fullJoin(tb, columns, option) {
572
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "FULL JOIN", tb, columns, option);
573
- }
574
- innerJoin(tb, columns, option) {
575
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "INNER JOIN", tb, columns, option);
576
- }
577
- leftJoin(tb, columns, option) {
578
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "LEFT JOIN", tb, columns, option);
579
- }
580
- rightJoin(tb, columns, option) {
581
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "RIGHT JOIN", tb, columns, option);
582
- }
583
- naturalJoin(tb, columns, option) {
584
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "NATURAL JOIN", tb, columns, option);
585
- }
586
- crossJoin(tb, columns, option) {
587
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "CROSS JOIN", tb, columns, option);
588
- }
589
- select(tb, columns, option) {
590
- let select = createSelectMap(tb, columns, option, this.tableList).selects;
591
- const obj = new _a(select, new Map(this.addedColumns));
592
- return obj;
593
- }
594
- addColumns(add) {
595
- let addedColumns = this.addedColumns ? new Map(this.addedColumns) : new Map();
596
- for (const [asNewName, columnStr] of Object.entries(add)) {
597
- if (addedColumns.has(asNewName))
598
- throw new Error(`The column ${asNewName} already exists`);
599
- addedColumns.set(asNewName, columnStr);
600
- }
601
- return new _a(this.tableList, addedColumns);
602
- }
603
- toQuery(option = {}) {
604
- const { where, orderNullRule } = option;
605
- let sql = this.toString();
606
- if (where)
607
- sql += "\nWHERE " + where;
608
- const orderBy = option.orderBy;
609
- if (orderBy)
610
- sql += genOderBy(orderBy, orderNullRule);
611
- if (option.limit)
612
- sql += "\nLIMIT " + option.limit;
613
- if (option.offset)
614
- sql += "\nOFFSET " + option.offset;
615
- return new SqlQueryStatement(sql, Array.from(this.columns));
616
- }
617
- }
618
- _a = SelectImpl, _SelectImpl_instances = new WeakSet(), _SelectImpl_joinOn = function _SelectImpl_joinOn(type, tb, columns, option) {
619
- let { selects, tbInfo } = createSelectMap(tb, columns, option, this.tableList);
620
- tbInfo.type = type;
621
- tbInfo.on = option.on;
622
- const next = new _a(selects, new Map(this.addedColumns));
623
- Object.defineProperty(next, "select", { value: undefined, configurable: false, enumerable: false });
624
- return next;
625
- }, _SelectImpl_join = function _SelectImpl_join(type, tb, columns, option) {
626
- let { selects, tbInfo } = createSelectMap(tb, columns, option, this.tableList);
627
- tbInfo.type = type;
628
- const next = new _a(selects, new Map(this.addedColumns));
629
- Object.defineProperty(next, "select", { value: undefined, configurable: false, enumerable: false });
630
- return next;
631
- };
632
- function createSelectMap(tb, columns, option = {}, selects) {
633
- let select = new Map(selects);
634
- let tableAs;
635
- let tbInfo;
636
- if (typeof tb === "string") {
637
- throw new Error("tb 应是 SqlSelectable 类型");
638
- // if (typeof columns !== "object") throw new Error("缺少 option.columns");
639
- // const newColumns = genSelectAsColumns(columns);
640
- // tableAs = option.tableAs ?? tb;
641
- // tbInfo = { table: tb, columns: newColumns };
642
- }
643
- else {
644
- const from = tb.toSelect();
645
- if (tb instanceof DbTable) {
646
- tableAs = option.tableAs ?? from;
647
- }
648
- else {
649
- tableAs = option.tableAs ?? "t" + select.size;
650
- }
651
- if (columns) {
652
- const newColumns = genNewColumns(columns, tb.columns);
653
- tbInfo = { from, columns: newColumns };
654
- }
655
- else
656
- tbInfo = { from, columns: new Map() };
657
- }
658
- if (select.has(tableAs))
659
- throw new TableRepeatError(tableAs);
660
- select.set(tableAs, tbInfo);
661
- return { selects: select, tbInfo: tbInfo };
662
- }
663
- class TableRepeatError extends Error {
664
- constructor(tableName) {
665
- super("Table name '" + tableName + "' repeated");
666
- }
667
- }
668
- // interface SqlFrom<T extends object> {
669
- // from<A extends object>(table: SqlSelectable<A>): SqlFrom<T & A>;
670
- // }
671
- // declare function from<T extends SqlSelectable<any>[]>(...table: T): Select<MergeSelectable<T>>;
672
- // type MergeSelectable<T extends any[]> = T extends [SqlSelectable<infer P>, ...infer Q] ? P & MergeSelectable<Q> : {};
673
606
 
674
607
  /** @public */
675
608
  class DbTableQuery extends DbTable {
@@ -677,8 +610,8 @@ class DbTableQuery extends DbTable {
677
610
  super(name, columns);
678
611
  this.statement = statement;
679
612
  }
680
- select(columns, option) {
681
- return createSelect(this, columns, option);
613
+ fromAs(as) {
614
+ return createSelect(this, as);
682
615
  }
683
616
  insert(values, option) {
684
617
  let insertCol;
@@ -765,9 +698,6 @@ function genRetuningSql(sql, returns, tableColumns) {
765
698
  sql += "\nRETURNING " + columnsStr;
766
699
  return new SqlQueryStatement(sql, columns);
767
700
  }
768
- function genWhere(where) {
769
- return "\nWHERE " + where;
770
- }
771
701
 
772
702
  /**
773
703
  * 表格列的信息
@@ -940,4 +870,4 @@ class YourTable extends DbTableQuery {
940
870
  }
941
871
  }
942
872
 
943
- export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, createSelect, getObjectListKeys, pgSqlTransformer };
873
+ export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, createSelect, genHaving, genOderBy, genSelect, genWhere, getObjectListKeys, pgSqlTransformer };
@@ -1,17 +1,13 @@
1
1
  import { SqlValuesCreator, SqlRaw } from "../sql_value/sql_value.ts";
2
- import { ColumnsSelected, ColumnsSelectAs, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
3
- import { Select, SelectTableOption } from "./select.ts";
2
+ import { ColumnsSelected, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
3
+ import { JoinSelect } from "./select.ts";
4
4
  import { DbTable, SqlQueryStatement } from "./selectable.ts";
5
+ import { WhereParam } from "../util.ts";
5
6
  /** @public */
6
7
  export declare class DbTableQuery<T extends TableType = Record<string, any>, C extends TableType = Partial<T>> extends DbTable<T> {
7
8
  private statement;
8
9
  constructor(name: string, columns: readonly string[], statement: SqlValuesCreator);
9
- /** 选择全部列 */
10
- select(columns?: undefined, option?: SelectTableOption): Select<{}>;
11
- /** 选择全部列 */
12
- select(columns: "*", option?: SelectTableOption): Select<T>;
13
- /** 选择表中的列并重命名 */
14
- select<R extends ColumnsSelectAs<T>>(columns: R, option?: SelectTableOption): Select<SelectColumns<T, R>>;
10
+ fromAs(as?: string): JoinSelect;
15
11
  insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
16
12
  insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
17
13
  update(values: UpdateRowValue<T>, option?: UpdateOption): string;
@@ -25,14 +21,14 @@ export interface InsertOption<T extends object> {
25
21
  updateValues?: {
26
22
  [key in keyof T]?: undefined | SqlRaw | T[key];
27
23
  };
28
- where?: string;
24
+ where?: WhereParam;
29
25
  }
30
26
  /** @public */
31
27
  export interface UpdateOption {
32
- where?: string;
28
+ where?: WhereParam;
33
29
  }
34
30
  /** @public */
35
31
  export interface DeleteOption {
36
- where?: string;
32
+ where?: WhereParam;
37
33
  }
38
34
  //# sourceMappingURL=TableQuery.d.ts.map
@@ -1,15 +1,6 @@
1
- import { ColumnsSelectAs, ColumnsSelected, RowsOrder } from "./type.ts";
2
- export declare function genOderBy(orderBy: RowsOrder<any>, orderNullRule?: "FIRST" | "LAST"): string;
1
+ import { ColumnsSelectAs } from "./type.ts";
3
2
  export declare function selectColumnsOrTable(columns: ColumnsSelectAs<any> | string[]): {
4
3
  columns: string[];
5
4
  sqlColumns: string;
6
5
  };
7
- /**
8
- * @param select 选择的行
9
- * @param tableColumns 全部行
10
- * @param push 要讲选择的行加入到集合中。
11
- */
12
- export declare function genNewColumns(select: ColumnsSelected<any>, tableColumns: Iterable<string | number | symbol>,
13
- /** newName -> oldName */
14
- push?: Map<string, string | null>): Map<string, string | null>;
15
6
  //# sourceMappingURL=_statement.d.ts.map
@@ -1,57 +1,44 @@
1
- import { SqlSelectable, InferQueryResult, SqlQueryStatement } from "./selectable.ts";
2
- import type { ColumnsSelected, OrderValue, RowsOrder, SelectColumns, TableType } from "./type.ts";
3
- interface AddTableFn<T extends TableType> {
4
- <Q extends SqlSelectable<any>, A extends InferQueryResult<Q>>(tb: Q, columns?: undefined, option?: SelectTableOption): Select<T>;
5
- <Q extends SqlSelectable<any>, A extends InferQueryResult<Q>, C extends ColumnsSelected<A>>(tb: Q, columns: C, option?: SelectTableOption): Select<T & SelectColumns<A, C>>;
6
- }
7
- interface JoinTableFn<T extends TableType, ExtraOption extends object = {}> {
8
- <Q extends SqlSelectable<any>, A extends InferQueryResult<Q>>(tb: Q, columns: undefined, option: ExtraOption & SelectTableOption): JoinSelect<T>;
9
- <Q extends SqlSelectable<any>, A extends InferQueryResult<Q>, C extends ColumnsSelected<A>>(tb: Q, columns: C, option: ExtraOption & SelectTableOption): JoinSelect<T & SelectColumns<A, C>>;
10
- }
1
+ import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
2
+ import { OrderByParam, WhereParam } from "../util.ts";
3
+ import type { TableType } from "./type.ts";
11
4
  /** @public */
12
5
  export interface SelectFilterOption<T extends object> {
13
- orderBy?: RowsOrder<T>;
14
- orderNullRule?: "FIRST" | "LAST";
15
- where?: string;
6
+ orderBy?: string | OrderByParam<T> | (() => string | OrderByParam<T>);
16
7
  offset?: number;
17
8
  limit?: number;
18
9
  }
19
10
  /** @public */
20
11
  export interface FinalSelect<T extends TableType> extends SqlSelectable<T> {
21
- toQuery(option?: SelectFilterOption<T & {
22
- [key: string]: OrderValue;
23
- }>): SqlQueryStatement<T>;
12
+ filter(option: SelectFilterOption<T>): SqlQueryStatement<T>;
24
13
  }
25
14
  /** @public */
26
- export interface JoinSelect<T extends TableType> extends FinalSelect<T> {
27
- addColumns<A extends TableType>(add: {
28
- [key in keyof A]: string;
29
- }): FinalSelect<T & A>;
30
- crossJoin: JoinTableFn<T>;
31
- naturalJoin: JoinTableFn<T>;
32
- innerJoin: JoinTableFn<T, {
33
- on: string;
34
- }>;
35
- leftJoin: JoinTableFn<T, {
36
- on: string;
37
- }>;
38
- rightJoin: JoinTableFn<T, {
39
- on: string;
40
- }>;
41
- fullJoin: JoinTableFn<T, {
42
- on: string;
43
- }>;
44
- }
15
+ export type LastSelect = {
16
+ select<T extends TableType = TableType>(columns: "*" | string[] | {
17
+ [key in keyof T]: string | boolean;
18
+ }): FinalSelect<T>;
19
+ };
45
20
  /** @public */
46
- export interface Select<T extends TableType> extends JoinSelect<T> {
47
- /** 选择表 */
48
- select: AddTableFn<T>;
49
- }
21
+ export type AfterGroup = LastSelect & {
22
+ having(param: WhereParam | (() => WhereParam)): LastSelect;
23
+ };
24
+ /** @public */
25
+ export type AfterWhere = AfterGroup & {
26
+ groupBy(columns: string | string[]): AfterGroup;
27
+ };
50
28
  /** @public */
51
- export declare const createSelect: AddTableFn<{}>;
29
+ export type AfterJoin = AfterWhere & {
30
+ where(param: WhereParam | (() => WhereParam)): AfterWhere;
31
+ };
52
32
  /** @public */
53
- export interface SelectTableOption {
54
- tableAs?: string;
33
+ export interface JoinSelect extends AfterJoin {
34
+ from(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
35
+ crossJoin(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
36
+ naturalJoin(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
37
+ innerJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
38
+ leftJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
39
+ rightJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
40
+ fullJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
55
41
  }
56
- export {};
42
+ /** @public */
43
+ export declare function createSelect(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
57
44
  //# sourceMappingURL=select.d.ts.map
@@ -23,7 +23,7 @@ export declare abstract class SqlSelectable<T extends TableType> {
23
23
  /** 获取 SQL 语句 */
24
24
  abstract toString(): string;
25
25
  /** 保留以推断类型 */
26
- private [SQL_SELECTABLE];
26
+ protected [SQL_SELECTABLE]: T;
27
27
  }
28
28
  /**
29
29
  * 数据库表
@@ -40,8 +40,9 @@ export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
40
40
  * @public
41
41
  */
42
42
  export declare class SqlQueryStatement<T extends TableType = TableType> extends SqlSelectable<T> {
43
- private sql;
43
+ #private;
44
44
  constructor(sql: string, columns: readonly string[]);
45
+ constructor(sql: SqlQueryStatement);
45
46
  toString(): string;
46
47
  toSelect(): string;
47
48
  }
@@ -14,13 +14,6 @@ export type PickColumn<T extends {
14
14
  } & {
15
15
  [key in Pa]?: T[key];
16
16
  };
17
- /**
18
- * 定义行的排序
19
- * @public
20
- */
21
- export type RowsOrder<T extends object> = {
22
- [key in keyof T]?: OrderValue;
23
- };
24
17
  /** @public */
25
18
  export type UpdateRowValue<T extends object> = {
26
19
  [key in keyof T]?: T[key] | SqlRaw;
@@ -9,7 +9,7 @@ export declare class SqlRaw<T = any> {
9
9
  constructor(value: string);
10
10
  toString(): string;
11
11
  /** 保留以推断类型 */
12
- private [SQL_RAW];
12
+ protected [SQL_RAW]: T;
13
13
  }
14
14
  /** @public */
15
15
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
package/dist/util.d.ts CHANGED
@@ -1,6 +1,27 @@
1
+ import { OrderValue } from "./select/type.ts";
1
2
  /**
2
3
  * @public
3
4
  * @param keepUndefinedKey - 是否保留值为 undefined 的 key
4
5
  */
5
6
  export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): string[];
7
+ /** @public */
8
+ export type WhereParam = string | string[];
9
+ /** @public */
10
+ export declare function genWhere(where: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
11
+ /** @public */
12
+ export declare function genHaving(having: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
13
+ /**
14
+ * @public
15
+ * ```ts
16
+ * genSelect({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
17
+ * genSelect(["c1", "count(*) AS c2", "column as c3"]) // "c1,count(*) AS c2,column as c3"
18
+ * ```
19
+ */
20
+ export declare function genSelect(columns: string[] | Record<string, string | boolean>): string;
21
+ /** @public */
22
+ export type OrderByParam<T extends {} = {}> = string | string[] | ({
23
+ [key in keyof T]: OrderValue;
24
+ } & Record<string, OrderValue>);
25
+ /** @public */
26
+ export declare function genOderBy<T extends {} = {}>(orderBy: OrderByParam<T> | (() => OrderByParam<T>)): string;
6
27
  //# sourceMappingURL=util.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",