@ignisia/sql 0.4.0 → 0.4.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.
Files changed (93) hide show
  1. package/dist/cjs/database/alter.d.cts +2 -2
  2. package/dist/cjs/database/column.d.cts +2 -2
  3. package/dist/cjs/database/contract.d.cts +2 -2
  4. package/dist/cjs/database/index.d.cts +2 -2
  5. package/dist/cjs/database/table.d.cts +2 -2
  6. package/dist/cjs/database/types.d.cts +1 -1
  7. package/dist/cjs/database/wrapper.d.cts +1 -1
  8. package/dist/cjs/{index-CZhrzE5r.d.cts → index-BjgeY76W.d.cts} +1 -1
  9. package/dist/cjs/{index-CHxuUiO4.d.cts → index-K07efKgt.d.cts} +102 -74
  10. package/dist/cjs/index.d.cts +2 -2
  11. package/dist/cjs/migration/index.d.cts +2 -2
  12. package/dist/cjs/migration/type.d.cts +2 -2
  13. package/dist/cjs/query/ast.d.cts +8 -0
  14. package/dist/cjs/query/ast.js +2 -0
  15. package/dist/cjs/query/builder.d.cts +3 -3
  16. package/dist/cjs/query/builder.js +7 -9
  17. package/dist/cjs/query/compiler.d.cts +13 -0
  18. package/dist/cjs/query/compiler.js +114 -0
  19. package/dist/cjs/query/condition/common.d.cts +16 -11
  20. package/dist/cjs/query/condition/common.js +0 -12
  21. package/dist/cjs/query/condition/core.d.cts +1 -1
  22. package/dist/cjs/query/condition/core.js +67 -33
  23. package/dist/cjs/query/condition/index.d.cts +2 -2
  24. package/dist/cjs/query/condition/index.js +0 -7
  25. package/dist/cjs/query/condition/not.d.cts +15 -3
  26. package/dist/cjs/query/constants.d.cts +8 -9
  27. package/dist/cjs/query/constants.js +9 -10
  28. package/dist/cjs/query/contract.d.cts +1 -1
  29. package/dist/cjs/query/explain.d.cts +1 -1
  30. package/dist/cjs/query/explain.js +1 -1
  31. package/dist/cjs/query/helper.d.cts +1 -1
  32. package/dist/cjs/query/helper.js +0 -4
  33. package/dist/cjs/query/index.d.cts +3 -3
  34. package/dist/cjs/query/index.js +1 -11
  35. package/dist/cjs/query/join.d.cts +5 -5
  36. package/dist/cjs/query/join.js +19 -11
  37. package/dist/cjs/query/sql.d.cts +2 -2
  38. package/dist/cjs/query/sql.js +24 -16
  39. package/dist/cjs/query/types.d.cts +1 -1
  40. package/dist/cjs/query/utilities.d.cts +4 -6
  41. package/dist/cjs/query/utilities.js +0 -116
  42. package/dist/cjs/table/index.d.cts +1 -1
  43. package/dist/cjs/table/types.d.cts +1 -1
  44. package/dist/cjs/table/utilities.d.cts +1 -1
  45. package/dist/esm/database/alter.d.ts +2 -2
  46. package/dist/esm/database/column.d.ts +2 -2
  47. package/dist/esm/database/contract.d.ts +2 -2
  48. package/dist/esm/database/index.d.ts +2 -2
  49. package/dist/esm/database/table.d.ts +2 -2
  50. package/dist/esm/database/types.d.ts +1 -1
  51. package/dist/esm/database/wrapper.d.ts +1 -1
  52. package/dist/esm/{index-DgOs61lH.d.ts → index-B4y4GiNB.d.ts} +1 -1
  53. package/dist/esm/{index-CjurLJdK.d.ts → index-CARBkj9h.d.ts} +102 -74
  54. package/dist/esm/index.d.ts +2 -2
  55. package/dist/esm/migration/index.d.ts +2 -2
  56. package/dist/esm/migration/type.d.ts +2 -2
  57. package/dist/esm/query/ast.d.ts +8 -0
  58. package/dist/esm/query/ast.js +1 -0
  59. package/dist/esm/query/builder.d.ts +3 -3
  60. package/dist/esm/query/builder.js +7 -9
  61. package/dist/esm/query/compiler.d.ts +13 -0
  62. package/dist/esm/query/compiler.js +112 -0
  63. package/dist/esm/query/condition/common.d.ts +16 -11
  64. package/dist/esm/query/condition/common.js +1 -12
  65. package/dist/esm/query/condition/core.d.ts +1 -1
  66. package/dist/esm/query/condition/core.js +68 -34
  67. package/dist/esm/query/condition/index.d.ts +2 -2
  68. package/dist/esm/query/condition/index.js +0 -1
  69. package/dist/esm/query/condition/not.d.ts +15 -3
  70. package/dist/esm/query/constants.d.ts +8 -9
  71. package/dist/esm/query/constants.js +9 -11
  72. package/dist/esm/query/contract.d.ts +1 -1
  73. package/dist/esm/query/explain.d.ts +1 -1
  74. package/dist/esm/query/explain.js +1 -1
  75. package/dist/esm/query/helper.d.ts +1 -1
  76. package/dist/esm/query/helper.js +1 -4
  77. package/dist/esm/query/index.d.ts +3 -3
  78. package/dist/esm/query/index.js +2 -12
  79. package/dist/esm/query/join.d.ts +5 -5
  80. package/dist/esm/query/join.js +19 -11
  81. package/dist/esm/query/sql.d.ts +2 -2
  82. package/dist/esm/query/sql.js +25 -17
  83. package/dist/esm/query/types.d.ts +1 -1
  84. package/dist/esm/query/utilities.d.ts +4 -6
  85. package/dist/esm/query/utilities.js +2 -116
  86. package/dist/esm/table/index.d.ts +1 -1
  87. package/dist/esm/table/types.d.ts +1 -1
  88. package/dist/esm/table/utilities.d.ts +1 -1
  89. package/package.json +4 -4
  90. package/dist/cjs/query/condition/raw.d.cts +0 -8
  91. package/dist/cjs/query/condition/raw.js +0 -54
  92. package/dist/esm/query/condition/raw.d.ts +0 -8
  93. package/dist/esm/query/condition/raw.js +0 -50
@@ -0,0 +1,114 @@
1
+ 'use strict';
2
+
3
+ var constants$1 = require('../table/constants');
4
+ var constants = require('./constants');
5
+
6
+ function compileComparison(dialect, node, params) {
7
+ const column = node.column;
8
+ if (node.operator === constants.AcceptedOperator.BETWEEN || node.operator === constants.AcceptedOperator.IN) {
9
+ params.push(...node.values);
10
+ } else if (node.operator !== constants.AcceptedOperator.IS_NULL && "value" in node) {
11
+ params.push(node.value);
12
+ }
13
+ switch (node.operator) {
14
+ case constants.AcceptedOperator.EQ:
15
+ return `${column} = ?`;
16
+ case constants.AcceptedOperator.NE:
17
+ return `${column} != ?`;
18
+ case constants.AcceptedOperator.GT:
19
+ return `${column} > ?`;
20
+ case constants.AcceptedOperator.LT:
21
+ return `${column} < ?`;
22
+ case constants.AcceptedOperator.GTE:
23
+ return `${column} >= ?`;
24
+ case constants.AcceptedOperator.LTE:
25
+ return `${column} <= ?`;
26
+ case constants.AcceptedOperator.IN:
27
+ return `${column} IN (${node.values.map(() => "?").join(", ")})`;
28
+ case constants.AcceptedOperator.LIKE:
29
+ return `${column} LIKE ?`;
30
+ case constants.AcceptedOperator.ILIKE:
31
+ if (dialect === constants$1.Dialect.POSTGRES) {
32
+ return `${column} ILIKE ?`;
33
+ }
34
+ return `LOWER(${column}) LIKE LOWER(?)`;
35
+ case constants.AcceptedOperator.IS_NULL:
36
+ return `${column} IS NULL`;
37
+ case constants.AcceptedOperator.BETWEEN:
38
+ return `${column} BETWEEN ? AND ?`;
39
+ case constants.AcceptedOperator.STARTS_WITH:
40
+ return `${column} LIKE ?`;
41
+ case constants.AcceptedOperator.ENDS_WITH:
42
+ return `${column} LIKE ?`;
43
+ case constants.AcceptedOperator.REG_EXP: {
44
+ switch (dialect) {
45
+ case constants$1.Dialect.POSTGRES:
46
+ return `${column} ~ ?`;
47
+ case constants$1.Dialect.MYSQL:
48
+ return `${column} REGEXP ?`;
49
+ case constants$1.Dialect.SQLITE:
50
+ return `${column} GLOB ?`;
51
+ default:
52
+ throw new Error("Operator not supported");
53
+ }
54
+ }
55
+ case constants.AcceptedOperator.RLIKE: {
56
+ switch (dialect) {
57
+ case constants$1.Dialect.POSTGRES:
58
+ return `${column} ~* ?`;
59
+ case constants$1.Dialect.MYSQL:
60
+ return `${column} RLIKE ?`;
61
+ case constants$1.Dialect.SQLITE:
62
+ return `${column} GLOB ?`;
63
+ default:
64
+ throw new Error("Operator not supported");
65
+ }
66
+ }
67
+ default:
68
+ throw new Error("Invalid operator");
69
+ }
70
+ }
71
+ function compileNot(dialect, node, params) {
72
+ const inner = compileAst(dialect, node.child, params);
73
+ return `NOT (${inner})`;
74
+ }
75
+ function compileGroup(dialect, node, params) {
76
+ if (!node.children.length) {
77
+ return "";
78
+ }
79
+ const compiled = node.children.map(
80
+ (child) => compileAst(dialect, child, params)
81
+ );
82
+ return `(${compiled.join(` ${node.operator} `)})`;
83
+ }
84
+ function compileAst(dialect, node, params) {
85
+ switch (node.type) {
86
+ case constants.AstType.COMPARISON:
87
+ return compileComparison(dialect, node, params);
88
+ case constants.AstType.GROUP:
89
+ return compileGroup(dialect, node, params);
90
+ case constants.AstType.NOT:
91
+ return compileNot(dialect, node, params);
92
+ default:
93
+ throw new Error(`Unknown AST node type`);
94
+ }
95
+ }
96
+ function compileJoin(dialect, join, params) {
97
+ const table = join.table.name;
98
+ const alias = join.alias;
99
+ if (join.join === constants.AcceptedJoin.CROSS || join.join === constants.AcceptedJoin.NATURAL) {
100
+ return `${join.join} JOIN ${table} AS ${alias}`;
101
+ }
102
+ if (!join.on) {
103
+ throw new Error(`${join.join} join requires ON condition`);
104
+ }
105
+ const onSql = compileAst(
106
+ dialect,
107
+ join.on,
108
+ params
109
+ );
110
+ return `${join.join} JOIN ${table} AS ${alias} ON ${onSql}`;
111
+ }
112
+
113
+ exports.compileAst = compileAst;
114
+ exports.compileJoin = compileJoin;
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, W as WhereValue, l as QueryBuilder, n as QueryTransformerContract, o as QueryConditionContract } from '../../index-CHxuUiO4.cjs';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, W as WhereValue, c as QueryBuilder, e as QueryTransformerContract, f as QueryConditionContract, G as GroupNode } from '../../index-K07efKgt.cjs';
2
2
  import { Column } from '../../column/index.cjs';
3
3
  import { AcceptedOperator } from '../constants.cjs';
4
4
  import 'bun';
@@ -21,13 +21,6 @@ declare function or<Alias extends string, TableRef extends Table<string, Record<
21
21
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
22
22
  where: string[];
23
23
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
24
- declare function on<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, ColName extends StrictAllowedColumn, Operator extends typeof AcceptedOperator.EQ | typeof AcceptedOperator.NE>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, columnA: ColName, operator: Operator, columnB: ColName): QueryBuilder<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
25
- where: string[];
26
- }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>, QueryTransformerContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
27
- where: string[];
28
- }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
29
- where: string[];
30
- }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
31
24
  declare function having<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, ColName extends StrictAllowedColumn, Col extends ColName extends `${infer TableAlias}.${infer TableColumn}` ? TableAlias extends Alias ? TableRef['columns'][TableColumn] : JoinedTables[TableAlias]['columns'][TableColumn] : never, Operator extends AcceptedOperator, Value extends WhereValue<Col>[Operator]>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, column: ColName, operator: Operator, value: Value): QueryBuilder<Alias, TableRef, JoinedTables, Omit<Definition, "having"> & {
32
25
  having: string[];
33
26
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>, QueryTransformerContract<Alias, TableRef, JoinedTables, Omit<Definition, "having"> & {
@@ -35,7 +28,19 @@ declare function having<Alias extends string, TableRef extends Table<string, Rec
35
28
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "having"> & {
36
29
  having: string[];
37
30
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
38
- declare function whereGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn, QueryTransformerContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, QueryConditionContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>;
39
- declare function orGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn, QueryTransformerContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, QueryConditionContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>;
31
+ declare function whereGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
32
+ where: GroupNode;
33
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>, QueryTransformerContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
34
+ where: GroupNode;
35
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
36
+ where: GroupNode;
37
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
38
+ declare function orGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
39
+ where: GroupNode;
40
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>, QueryTransformerContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
41
+ where: GroupNode;
42
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
43
+ where: GroupNode;
44
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
40
45
 
41
- export { having, on, or, orGroup, where, whereGroup };
46
+ export { having, or, orGroup, where, whereGroup };
@@ -25,17 +25,6 @@ function or(column, operator, value) {
25
25
  false
26
26
  );
27
27
  }
28
- function on(columnA, operator, columnB) {
29
- return core.addCondition(
30
- this,
31
- constants.ConditionClause.WHERE,
32
- columnA,
33
- operator,
34
- columnB,
35
- constants.LogicalOperator.ON,
36
- false
37
- );
38
- }
39
28
  function having(column, operator, value) {
40
29
  return core.addCondition(
41
30
  this,
@@ -55,7 +44,6 @@ function orGroup(callback) {
55
44
  }
56
45
 
57
46
  exports.having = having;
58
- exports.on = on;
59
47
  exports.or = or;
60
48
  exports.orGroup = orGroup;
61
49
  exports.where = where;
@@ -1,4 +1,4 @@
1
- export { a as addCondition, b as addGroupCondition } from '../../index-CHxuUiO4.cjs';
1
+ export { a as addCondition, b as addGroupCondition } from '../../index-K07efKgt.cjs';
2
2
  import '../../column/index.cjs';
3
3
  import '../constants.cjs';
4
4
  import 'bun';
@@ -2,53 +2,87 @@
2
2
 
3
3
  var __ = require('..');
4
4
  var constants = require('../constants');
5
- var utilities = require('../utilities');
6
5
 
7
6
  function addCondition(query, clause, column, operator, value, logical, negate) {
8
7
  if (!query.table.dialect) {
9
8
  throw new Error("No DB Dialect defined");
10
9
  }
11
- const isOn = logical === constants.LogicalOperator.ON;
12
10
  const validClause = clause.toLowerCase();
13
- let condition = utilities.getCondition(query.table.dialect, column, operator, value);
14
- if (isOn) {
15
- condition = condition.replace("?", value);
16
- }
17
- if (!query.definition[validClause]) query.definition[validClause] = [];
18
- const logicalPrefix = query.definition[validClause].length > 0 ? isOn ? constants.LogicalOperator.AND : logical : "";
19
- const not = negate ? "NOT " : "";
20
- query.definition[validClause].push(
21
- `${logicalPrefix} ${not}${condition}`.trim()
22
- );
23
- if (operator === constants.AcceptedOperator.IS_NULL || operator === constants.AcceptedOperator.IS_NOT_NULL) {
24
- return query;
11
+ if (!query.definition[validClause]) {
12
+ query.definition[validClause] = {
13
+ type: constants.AstType.GROUP,
14
+ operator: constants.LogicalOperator.AND,
15
+ children: []
16
+ };
25
17
  }
26
- if (!query.definition.params) query.definition.params = [];
27
- if (!isOn) {
28
- if (operator === constants.AcceptedOperator.STARTS_WITH) {
29
- query.definition.params.push(`${value}%`);
30
- } else if (operator === constants.AcceptedOperator.ENDS_WITH) {
31
- query.definition.params.push(`%${value}`);
32
- } else if (Array.isArray(value)) {
33
- query.definition.params.push(...value);
34
- } else {
35
- query.definition.params.push(value);
18
+ const root = query.definition[validClause];
19
+ let node = {
20
+ type: constants.AstType.COMPARISON,
21
+ column,
22
+ operator,
23
+ value,
24
+ values: value
25
+ };
26
+ if (node.type === constants.AstType.COMPARISON) {
27
+ if (node.operator === constants.AcceptedOperator.IS_NULL && "value" in node) {
28
+ delete node.value;
29
+ }
30
+ if (node.operator === constants.AcceptedOperator.STARTS_WITH) {
31
+ node.value = "%" + node.value;
32
+ }
33
+ if (node.operator === constants.AcceptedOperator.ENDS_WITH) {
34
+ node.value = node.value + "%";
36
35
  }
37
36
  }
37
+ if (negate) {
38
+ node = {
39
+ type: constants.AstType.NOT,
40
+ child: node
41
+ };
42
+ }
43
+ if (operator === constants.AcceptedOperator.IS_NULL) {
44
+ return query;
45
+ }
46
+ if (root.operator === logical) {
47
+ root.children.push(node);
48
+ } else {
49
+ query.definition.where = {
50
+ type: constants.AstType.GROUP,
51
+ operator: logical,
52
+ children: [root, node]
53
+ };
54
+ }
38
55
  return query;
39
56
  }
40
57
  function addGroupCondition(query, logical, callback, negate) {
41
58
  const sub = callback(new __.QueryBuilder(query.table));
42
59
  const subDef = sub.definition;
43
- if (!subDef.where?.length) return query;
44
- const not = negate ? "NOT " : "";
45
- const grouped = `${not}(${subDef.where.join(" ")})`;
46
- if (!query.definition.where) query.definition.where = [];
47
- const logicalPrefix = query.definition.where.length > 0 ? logical : "";
48
- query.definition.where.push(`${logicalPrefix} ${grouped}`.trim());
49
- if (subDef.params?.length) {
50
- if (!query.definition.params) query.definition.params = [];
51
- query.definition.params.push(...subDef.params);
60
+ if (!query.definition.where) {
61
+ query.definition.where = {
62
+ type: constants.AstType.GROUP,
63
+ operator: constants.LogicalOperator.AND,
64
+ children: []
65
+ };
66
+ }
67
+ if (!subDef.where || subDef.where.children.length === 0) {
68
+ return query;
69
+ }
70
+ const root = query.definition.where;
71
+ let groupNode = subDef.where;
72
+ if (negate) {
73
+ groupNode = {
74
+ type: constants.AstType.NOT,
75
+ child: groupNode
76
+ };
77
+ }
78
+ if (root.operator === logical) {
79
+ root.children.push(groupNode);
80
+ } else {
81
+ query.definition.where = {
82
+ type: constants.AstType.GROUP,
83
+ operator: logical,
84
+ children: [root, groupNode]
85
+ };
52
86
  }
53
87
  return query;
54
88
  }
@@ -1,5 +1,5 @@
1
- export { having, on, or, orGroup, where, whereGroup } from './common.cjs';
2
- export { a as addCondition, b as addGroupCondition, c as addRawCondition, e as rawHaving, d as rawOr, r as rawWhere } from '../../index-CHxuUiO4.cjs';
1
+ export { having, or, orGroup, where, whereGroup } from './common.cjs';
2
+ export { a as addCondition, b as addGroupCondition } from '../../index-K07efKgt.cjs';
3
3
  export { havingNot, orNot, orNotGroup, whereNot, whereNotGroup } from './not.cjs';
4
4
  import '../../column/index.cjs';
5
5
  import '../../table/constants.cjs';
@@ -3,7 +3,6 @@
3
3
  var common = require('./common');
4
4
  var core = require('./core');
5
5
  var not = require('./not');
6
- var raw = require('./raw');
7
6
 
8
7
 
9
8
 
@@ -25,9 +24,3 @@ Object.keys(not).forEach(function (k) {
25
24
  get: function () { return not[k]; }
26
25
  });
27
26
  });
28
- Object.keys(raw).forEach(function (k) {
29
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
30
- enumerable: true,
31
- get: function () { return raw[k]; }
32
- });
33
- });
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, W as WhereValue, l as QueryBuilder, n as QueryTransformerContract, o as QueryConditionContract } from '../../index-CHxuUiO4.cjs';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, W as WhereValue, c as QueryBuilder, e as QueryTransformerContract, f as QueryConditionContract, G as GroupNode } from '../../index-K07efKgt.cjs';
2
2
  import { Column } from '../../column/index.cjs';
3
3
  import { AcceptedOperator } from '../constants.cjs';
4
4
  import 'bun';
@@ -28,7 +28,19 @@ declare function orNot<Alias extends string, TableRef extends Table<string, Reco
28
28
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
29
29
  where: string[];
30
30
  }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
31
- declare function whereNotGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn, QueryTransformerContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, QueryConditionContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>;
32
- declare function orNotGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn, QueryTransformerContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, QueryConditionContract<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>;
31
+ declare function whereNotGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
32
+ where: GroupNode;
33
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>, QueryTransformerContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
34
+ where: GroupNode;
35
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
36
+ where: GroupNode;
37
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
38
+ declare function orNotGroup<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>>(this: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, callback: (q: QueryBuilder<Alias, TableRef, JoinedTables>) => QueryBuilder<Alias, TableRef, JoinedTables>): QueryBuilder<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
39
+ where: GroupNode;
40
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>, QueryTransformerContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
41
+ where: GroupNode;
42
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>, QueryConditionContract<Alias, TableRef, JoinedTables, Omit<Definition, "where"> & {
43
+ where: GroupNode;
44
+ }, ColumnSelector<Alias, TableRef, JoinedTables>, StrictColumnSelector<Alias, TableRef, JoinedTables>>>;
33
45
 
34
46
  export { havingNot, orNot, orNotGroup, whereNot, whereNotGroup };
@@ -1,9 +1,15 @@
1
1
  declare const LogicalOperator: {
2
2
  readonly AND: "AND";
3
3
  readonly OR: "OR";
4
- readonly ON: "ON";
5
4
  };
6
5
  type LogicalOperator = (typeof LogicalOperator)[keyof typeof LogicalOperator];
6
+ declare const AstType: {
7
+ readonly GROUP: "GROUP";
8
+ readonly COMPARISON: "COMPARISON";
9
+ readonly NOT: "NOT";
10
+ readonly JOIN: "JOIN";
11
+ };
12
+ type AstType = (typeof AstType)[keyof typeof AstType];
7
13
  declare const ConditionClause: {
8
14
  readonly WHERE: "WHERE";
9
15
  readonly HAVING: "HAVING";
@@ -17,21 +23,14 @@ declare const AcceptedOperator: {
17
23
  readonly GTE: "gte";
18
24
  readonly LTE: "lte";
19
25
  readonly IN: "in";
20
- readonly NOT_IN: "notIn";
21
26
  readonly LIKE: "like";
22
- readonly NOT_LIKE: "notLike";
23
27
  readonly ILIKE: "ilike";
24
- readonly NOT_ILIKE: "notILike";
25
28
  readonly IS_NULL: "isNull";
26
- readonly IS_NOT_NULL: "isNotNull";
27
29
  readonly BETWEEN: "between";
28
- readonly NOT_BETWEEN: "notBetween";
29
30
  readonly STARTS_WITH: "startsWith";
30
31
  readonly ENDS_WITH: "endsWith";
31
32
  readonly REG_EXP: "regExp";
32
- readonly NOT_REG_EXP: "notRegExp";
33
33
  readonly RLIKE: "rlike";
34
- readonly NOT_RLIKE: "notRlike";
35
34
  };
36
35
  type AcceptedOperator = (typeof AcceptedOperator)[keyof typeof AcceptedOperator];
37
36
  declare const QueryType: {
@@ -86,4 +85,4 @@ declare const ExplainClause: {
86
85
  };
87
86
  type ExplainClause = (typeof ExplainClause)[keyof typeof ExplainClause];
88
87
 
89
- export { AcceptedJoin, AcceptedOperator, AggregationFunction, ConditionClause, ExplainClause, ExplainFormat, LogicalOperator, OrderBy, QueryHooksType, QueryType };
88
+ export { AcceptedJoin, AcceptedOperator, AggregationFunction, AstType, ConditionClause, ExplainClause, ExplainFormat, LogicalOperator, OrderBy, QueryHooksType, QueryType };
@@ -2,8 +2,13 @@
2
2
 
3
3
  const LogicalOperator = {
4
4
  AND: "AND",
5
- OR: "OR",
6
- ON: "ON"
5
+ OR: "OR"
6
+ };
7
+ const AstType = {
8
+ GROUP: "GROUP",
9
+ COMPARISON: "COMPARISON",
10
+ NOT: "NOT",
11
+ JOIN: "JOIN"
7
12
  };
8
13
  const ConditionClause = {
9
14
  WHERE: "WHERE",
@@ -17,21 +22,14 @@ const AcceptedOperator = {
17
22
  GTE: "gte",
18
23
  LTE: "lte",
19
24
  IN: "in",
20
- NOT_IN: "notIn",
21
25
  LIKE: "like",
22
- NOT_LIKE: "notLike",
23
26
  ILIKE: "ilike",
24
- NOT_ILIKE: "notILike",
25
27
  IS_NULL: "isNull",
26
- IS_NOT_NULL: "isNotNull",
27
28
  BETWEEN: "between",
28
- NOT_BETWEEN: "notBetween",
29
29
  STARTS_WITH: "startsWith",
30
30
  ENDS_WITH: "endsWith",
31
31
  REG_EXP: "regExp",
32
- NOT_REG_EXP: "notRegExp",
33
- RLIKE: "rlike",
34
- NOT_RLIKE: "notRlike"
32
+ RLIKE: "rlike"
35
33
  };
36
34
  const QueryType = {
37
35
  SELECT: "SELECT",
@@ -81,6 +79,7 @@ const ExplainClause = {
81
79
  exports.AcceptedJoin = AcceptedJoin;
82
80
  exports.AcceptedOperator = AcceptedOperator;
83
81
  exports.AggregationFunction = AggregationFunction;
82
+ exports.AstType = AstType;
84
83
  exports.ConditionClause = ConditionClause;
85
84
  exports.ExplainClause = ExplainClause;
86
85
  exports.ExplainFormat = ExplainFormat;
@@ -1,5 +1,5 @@
1
1
  import 'bun';
2
- export { o as QueryConditionContract, n as QueryTransformerContract } from '../index-CHxuUiO4.cjs';
2
+ export { f as QueryConditionContract, e as QueryTransformerContract } from '../index-K07efKgt.cjs';
3
3
  import '../column/index.cjs';
4
4
  import '../table/constants.cjs';
5
5
  import './constants.cjs';
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, l as QueryBuilder, E as ExplainOptions } from '../index-CHxuUiO4.cjs';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, c as QueryBuilder, E as ExplainOptions } from '../index-K07efKgt.cjs';
2
2
  import { Column } from '../column/index.cjs';
3
3
  import 'bun';
4
4
  import '../types.cjs';
@@ -41,7 +41,7 @@ function buildMySqlExplainQuery(options) {
41
41
  clauses.push(`${constants$1.ExplainClause.FORMAT}=${options.format}`);
42
42
  }
43
43
  if (clauses.length > 1) {
44
- throw new Error("Only one explain clause is allowed");
44
+ throw new Error("MySQL EXPLAIN only support one clauses at a time");
45
45
  }
46
46
  return `EXPLAIN ${clauses.join(" ")}`;
47
47
  }
@@ -1,4 +1,4 @@
1
- export { I as aggregateCol, B as alias, F as clone, H as col, G as rawCol } from '../index-CHxuUiO4.cjs';
1
+ export { a1 as aggregateCol, _ as alias, $ as clone, a0 as col } from '../index-K07efKgt.cjs';
2
2
  import '../column/index.cjs';
3
3
  import './constants.cjs';
4
4
  import 'bun';
@@ -12,9 +12,6 @@ function clone() {
12
12
  Object.assign(query.definition, utilities.cloneDefinition(this.definition));
13
13
  return query;
14
14
  }
15
- function rawCol(column) {
16
- return column;
17
- }
18
15
  function col(column, alias2) {
19
16
  return {
20
17
  column,
@@ -33,4 +30,3 @@ exports.aggregateCol = aggregateCol;
33
30
  exports.alias = alias;
34
31
  exports.clone = clone;
35
32
  exports.col = col;
36
- exports.rawCol = rawCol;
@@ -1,8 +1,8 @@
1
+ export { c as QueryBuilder } from '../index-K07efKgt.cjs';
1
2
  import '../column/index.cjs';
2
- export { l as QueryBuilder } from '../index-CHxuUiO4.cjs';
3
3
  import '../types.cjs';
4
4
  import './constants.cjs';
5
- import '../table/constants.cjs';
5
+ import 'bun';
6
6
  import '../column/constants.cjs';
7
+ import '../table/constants.cjs';
7
8
  import '../column/types.cjs';
8
- import 'bun';
@@ -20,14 +20,9 @@ class QueryBuilder {
20
20
  toDebugString;
21
21
  explain;
22
22
  exec;
23
- rawWhere;
24
- rawAnd;
25
- rawOr;
26
- rawHaving;
27
23
  where;
28
24
  and;
29
25
  or;
30
- on;
31
26
  having;
32
27
  whereGroup;
33
28
  orGroup;
@@ -45,15 +40,10 @@ class QueryBuilder {
45
40
  this.toDebugString = sql.toDebugString.bind(this);
46
41
  this.explain = sql.explain.bind(this);
47
42
  this.exec = sql.exec.bind(this);
48
- this.rawWhere = condition.rawWhere.bind(this);
49
- this.rawHaving = condition.rawHaving.bind(this);
50
- this.rawAnd = this.rawWhere;
51
- this.rawOr = condition.rawOr.bind(this);
52
43
  this.where = condition.where.bind(this);
53
44
  this.having = condition.having.bind(this);
54
45
  this.and = this.where;
55
46
  this.or = condition.or.bind(this);
56
- this.on = condition.on.bind(this);
57
47
  this.whereGroup = condition.whereGroup.bind(this);
58
48
  this.orGroup = condition.orGroup.bind(this);
59
49
  this.not = {
@@ -82,7 +72,7 @@ class QueryBuilder {
82
72
  return join.addNoOnJoin(this, constants.AcceptedJoin.CROSS, joinTable, alias2);
83
73
  }
84
74
  naturalJoin(joinTable, alias2) {
85
- return join.prepareJoin(this, constants.AcceptedJoin.NATURAL, joinTable, alias2);
75
+ return join.addNoOnJoin(this, constants.AcceptedJoin.NATURAL, joinTable, alias2);
86
76
  }
87
77
  distinct() {
88
78
  this.definition.distinct = true;
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, l as QueryBuilder, n as QueryTransformerContract, o as QueryConditionContract } from '../index-CHxuUiO4.cjs';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, c as QueryBuilder, J as JoinNode, e as QueryTransformerContract, f as QueryConditionContract } from '../index-K07efKgt.cjs';
2
2
  import { Column } from '../column/index.cjs';
3
3
  import { AcceptedJoin } from './constants.cjs';
4
4
  import 'bun';
@@ -10,18 +10,18 @@ import '../column/types.cjs';
10
10
  declare function addNoOnJoin<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, JoinType extends AcceptedJoin, JoinTable extends Table<string, Record<string, Column>>, JoinAlias extends string, FinalJoinedTables extends JoinedTables & {
11
11
  [K in JoinAlias]: JoinTable;
12
12
  }>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType, joinTable: JoinTable, alias: JoinAlias): QueryBuilder<Alias, TableRef, FinalJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
13
- joins: string[];
13
+ joins: JoinNode[];
14
14
  joinedTables: FinalJoinedTables;
15
15
  }>;
16
16
  declare function prepareJoin<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, JoinType extends AcceptedJoin, JoinTable extends Table<string, Record<string, Column>>, JoinAlias extends string>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType, joinTable: JoinTable, alias: JoinAlias): {
17
17
  on<FinalJoinedTables extends JoinedTables & { [K in JoinAlias]: JoinTable; }, ReturnedJoinedTables extends FinalJoinedTables = FinalJoinedTables>(callback: (q: QueryBuilder<Alias, TableRef, FinalJoinedTables>) => QueryBuilder<Alias, TableRef, ReturnedJoinedTables>): QueryBuilder<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
18
- joins: string[];
18
+ joins: JoinNode[];
19
19
  joinedTables: ReturnedJoinedTables;
20
20
  }, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>, QueryTransformerContract<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
21
- joins: string[];
21
+ joins: JoinNode[];
22
22
  joinedTables: ReturnedJoinedTables;
23
23
  }, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>>, QueryConditionContract<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
24
- joins: string[];
24
+ joins: JoinNode[];
25
25
  joinedTables: ReturnedJoinedTables;
26
26
  }, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>>>;
27
27
  };