happy-sql 1.6.0 → 1.7.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 (32) hide show
  1. package/ChangeLog +11 -0
  2. package/lib/parser/fixture/with-named-insert-fix.js +11 -0
  3. package/lib/parser/fixture/with-named-insert.sql +13 -0
  4. package/lib/parser/visitors/clause/column-definition/column-definition.js +39 -15
  5. package/lib/parser/visitors/clause/create-sequence/fixture/create-sequence-fix.js +0 -1
  6. package/lib/parser/visitors/clause/create-table/fixture/create-table-default-fix.js +5 -0
  7. package/lib/parser/visitors/clause/create-table/fixture/create-table-default.sql +1 -0
  8. package/lib/parser/visitors/clause/create-table/fixture/create-table-nextval-fix.js +6 -0
  9. package/lib/parser/visitors/clause/create-table/fixture/create-table-nextval.sql +1 -0
  10. package/lib/parser/visitors/clause/create-table/fixture/create-table-not-null-fix.js +5 -0
  11. package/lib/parser/visitors/clause/create-table/fixture/create-table-not-null.sql +1 -0
  12. package/lib/printer/visitors/call-expression/clauses/clauses.js +2 -0
  13. package/lib/printer/visitors/call-expression/clauses/insert/fixture/insert-fix.sql +1 -1
  14. package/lib/printer/visitors/call-expression/clauses/into/fixture/single-column-fix.sql +1 -1
  15. package/lib/printer/visitors/call-expression/clauses/nextval/fixture/nextval-fix.sql +1 -0
  16. package/lib/printer/visitors/call-expression/clauses/nextval/fixture/nextval.js +6 -0
  17. package/lib/printer/visitors/call-expression/clauses/nextval/nextval.js +4 -0
  18. package/lib/printer/visitors/call-expression/clauses/on-conflict/fixture/on-conflict-fix.sql +1 -1
  19. package/lib/printer/visitors/call-expression/clauses/on-conflict/fixture/on-conflict-multi-fix.sql +1 -1
  20. package/lib/printer/visitors/call-expression/clauses/returning/fixture/returning-conflict-fix.sql +1 -1
  21. package/lib/printer/visitors/call-expression/clauses/returning/fixture/returning-fix.sql +1 -1
  22. package/lib/printer/visitors/call-expression/clauses/returning/fixture/returning-id-fix.sql +1 -1
  23. package/lib/printer/visitors/call-expression/clauses/returning/fixture/returning-multi-fix.sql +1 -1
  24. package/lib/printer/visitors/call-expression/clauses/values/fixture/values-fix.sql +1 -1
  25. package/lib/printer/visitors/call-expression/clauses/values/fixture/values-quoted-fix.sql +1 -0
  26. package/lib/printer/visitors/call-expression/clauses/values/fixture/values-quoted.js +1 -0
  27. package/lib/printer/visitors/call-expression/clauses/values/values.js +10 -1
  28. package/lib/printer/visitors/call-expression/clauses/with-named/fixture/with-named-insert-fix.sql +10 -0
  29. package/lib/printer/visitors/call-expression/clauses/with-named/fixture/with-named-insert.js +11 -0
  30. package/lib/printer/visitors/call-expression/clauses/with-named/fixture/withNamed-fix.sql +1 -1
  31. package/package.json +1 -1
  32. package/lib/printer/visitors/call-expression/clauses/count/fixture/count-fix.js +0 -2
package/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2026.08.19, v1.7.1
2
+
3
+ fix:
4
+ - 269dea4 happy-sql: values printer: preserve string param quotes
5
+
6
+ 2026.08.18, v1.7.0
7
+
8
+ feature:
9
+ - 9446e52 happy-sql: nextval
10
+ - 60be73e nextval
11
+
1
12
  2026.08.15, v1.6.0
2
13
 
3
14
  fix:
@@ -0,0 +1,11 @@
1
+ [
2
+ withNamed(call = insert(into(
3
+ CallExpression,
4
+ [file, parent_id],
5
+ values(':file', ':parent_id'),
6
+ ), returning(id)), member = insert(into(
7
+ MemberExpression,
8
+ [file, parent_id],
9
+ select(':file', id, from(call)),
10
+ ), returning(id)), insert(into(Identifier, [file, name, parent_id], select(':file', 'isNaN', id, from(member))))),
11
+ ];
@@ -0,0 +1,13 @@
1
+ WITH
2
+ call AS (
3
+ INSERT INTO CallExpression (file, parent_id)
4
+ VALUES (':file', ':parent_id')
5
+ RETURNING id
6
+ ),
7
+ member AS (
8
+ INSERT INTO MemberExpression (file, parent_id)
9
+ SELECT ':file', id FROM call
10
+ RETURNING id
11
+ )
12
+ INSERT INTO Identifier (file, name, parent_id)
13
+ SELECT ':file', 'isNaN', id FROM member
@@ -11,21 +11,41 @@ const getDataType = (dataType) => {
11
11
 
12
12
  const isSerial = (dataType) => getDataType(dataType).name === 'SERIAL';
13
13
 
14
+ const convertGenerated = (constraint) => {
15
+ const kws = [];
16
+
17
+ for (const kw of constraint.generatedKw)
18
+ kws.push(kw.name);
19
+
20
+ if (kws.includes('DEFAULT'))
21
+ return callExpression(identifier('identityByDefault'), []);
22
+
23
+ return callExpression(identifier('identity'), []);
24
+ };
25
+
26
+ const convertDefault = ({expr}) => {
27
+ if (expr.type !== 'func_call' || expr.name.name !== 'nextval')
28
+ return null;
29
+
30
+ const seqName = expr.args.expr.args.items[0].value;
31
+
32
+ return callExpression(identifier('nextval'), [
33
+ identifier(seqName),
34
+ ]);
35
+ };
36
+
37
+ const CONSTRAINT_VISITORS = {
38
+ constraint_primary_key: () => callExpression(identifier('primaryKey'), []),
39
+ constraint_generated: convertGenerated,
40
+ constraint_not_null: () => null,
41
+ constraint_default: convertDefault,
42
+ };
43
+
14
44
  const convertConstraint = (constraint) => {
15
- if (constraint.type === 'constraint_primary_key')
16
- return callExpression(identifier('primaryKey'), []);
45
+ const visitor = CONSTRAINT_VISITORS[constraint.type];
17
46
 
18
- if (constraint.type === 'constraint_generated') {
19
- const kws = [];
20
-
21
- for (const kw of constraint.generatedKw)
22
- kws.push(kw.name);
23
-
24
- if (kws.includes('DEFAULT'))
25
- return callExpression(identifier('identityByDefault'), []);
26
-
27
- return callExpression(identifier('identity'), []);
28
- }
47
+ if (visitor)
48
+ return visitor(constraint);
29
49
 
30
50
  return callExpression(identifier('autoIncrement'), []);
31
51
  };
@@ -46,8 +66,12 @@ export const convertColumnDefinition = (column) => {
46
66
  else
47
67
  args.push(identifier(getDataType(dataType).text));
48
68
 
49
- for (const constraint of constraints)
50
- args.push(convertConstraint(constraint));
69
+ for (const constraint of constraints) {
70
+ const node = convertConstraint(constraint);
71
+
72
+ if (node)
73
+ args.push(node);
74
+ }
51
75
 
52
76
  return callExpression(identifier('column'), args);
53
77
  };
@@ -1,4 +1,3 @@
1
1
  [
2
2
  createSequence(users_id_seq),
3
3
  ];
4
-
@@ -0,0 +1,5 @@
1
+ [
2
+ createTable(t, [
3
+ column(id, INTEGER, primaryKey()),
4
+ ]),
5
+ ];
@@ -0,0 +1 @@
1
+ CREATE TABLE t (id INTEGER DEFAULT 42 PRIMARY KEY)
@@ -0,0 +1,6 @@
1
+ [
2
+ createTable(users, [
3
+ column(id, INTEGER, nextval(users_id_seq), primaryKey()),
4
+ column(name, TEXT),
5
+ ]),
6
+ ];
@@ -0,0 +1 @@
1
+ CREATE TABLE users (id INTEGER DEFAULT nextval('users_id_seq') PRIMARY KEY, name TEXT)
@@ -0,0 +1,5 @@
1
+ [
2
+ createTable(t, [
3
+ column(id, INTEGER, primaryKey()),
4
+ ]),
5
+ ];
@@ -0,0 +1 @@
1
+ CREATE TABLE t (id INTEGER NOT NULL PRIMARY KEY)
@@ -23,6 +23,7 @@ import {serial} from './serial/serial.js';
23
23
  import {primaryKey} from './primary-key/primary-key.js';
24
24
  import {lastInsertRowid} from './last-insert-rowid/last-insert-rowid.js';
25
25
  import {lastval} from './lastval/lastval.js';
26
+ import {nextval} from './nextval/nextval.js';
26
27
  import {like} from './like/like.js';
27
28
  import {isNull, isNotNull} from './is-null/is-null.js';
28
29
  import {inList} from './in-list/in-list.js';
@@ -57,6 +58,7 @@ export const clauses = {
57
58
  primaryKey,
58
59
  lastInsertRowid,
59
60
  lastval,
61
+ nextval,
60
62
  like,
61
63
  isNull,
62
64
  isNotNull,
@@ -1 +1 @@
1
- INSERT INTO t (x) VALUES (:x)
1
+ INSERT INTO t (x) VALUES (':x')
@@ -1 +1 @@
1
- INSERT INTO t (x) VALUES (:x)
1
+ INSERT INTO t (x) VALUES (':x')
@@ -0,0 +1 @@
1
+ CREATE TABLE users (id INTEGER DEFAULT nextval('users_id_seq') PRIMARY KEY, name TEXT)
@@ -0,0 +1,6 @@
1
+ [
2
+ createTable(users, [
3
+ column(id, INTEGER, nextval(users_id_seq), primaryKey()),
4
+ column(name, TEXT),
5
+ ]),
6
+ ];
@@ -0,0 +1,4 @@
1
+ export const nextval = (path, {write}) => {
2
+ const [seq] = path.node.arguments;
3
+ write(`DEFAULT nextval('${seq.name}')`);
4
+ };
@@ -1 +1 @@
1
- INSERT INTO t (x) VALUES (:x) ON CONFLICT (x) DO UPDATE SET x = :x
1
+ INSERT INTO t (x) VALUES (':x') ON CONFLICT (x) DO UPDATE SET x = :x
@@ -1 +1 @@
1
- INSERT INTO t (x) VALUES (:x) ON CONFLICT (x, y) DO UPDATE SET x = :x
1
+ INSERT INTO t (x) VALUES (':x') ON CONFLICT (x, y) DO UPDATE SET x = :x
@@ -1 +1 @@
1
- INSERT INTO t (x) VALUES (:x) ON CONFLICT (x) DO UPDATE SET y = :y RETURNING id
1
+ INSERT INTO t (x) VALUES (':x') ON CONFLICT (x) DO UPDATE SET y = :y RETURNING id
@@ -1 +1 @@
1
- INSERT INTO CallExpression (parent_id) VALUES (:parent_id) RETURNING id AS call_id
1
+ INSERT INTO CallExpression (parent_id) VALUES (':parent_id') RETURNING id AS call_id
@@ -1 +1 @@
1
- INSERT INTO CallExpression (parent_id) VALUES (:parent_id) RETURNING id
1
+ INSERT INTO CallExpression (parent_id) VALUES (':parent_id') RETURNING id
@@ -1 +1 @@
1
- INSERT INTO t (x) VALUES (:x) RETURNING id, created_at
1
+ INSERT INTO t (x) VALUES (':x') RETURNING id, created_at
@@ -1 +1 @@
1
- VALUES (:x)
1
+ VALUES (':x')
@@ -0,0 +1 @@
1
+ [values(':file', ':parent_id')]
@@ -1,3 +1,7 @@
1
+ import {types} from '@putout/babel';
2
+
3
+ const {isStringLiteral} = types;
4
+
1
5
  export const values = (path, {write, traverse}) => {
2
6
  const args = path.get('arguments');
3
7
  write('VALUES (');
@@ -5,7 +9,12 @@ export const values = (path, {write, traverse}) => {
5
9
  if (i > 0)
6
10
  write(', ');
7
11
 
8
- traverse(args[i]);
12
+ const arg = args[i];
13
+
14
+ if (isStringLiteral(arg.node))
15
+ write(`'${arg.node.value}'`);
16
+ else
17
+ traverse(arg);
9
18
  }
10
19
 
11
20
  write(')');
@@ -0,0 +1,10 @@
1
+ WITH
2
+ call AS (
3
+ INSERT INTO CallExpression (file, parent_id) VALUES (':file', ':parent_id') RETURNING id
4
+ ),
5
+ member AS (
6
+ INSERT INTO MemberExpression (file, parent_id) SELECT :file, id
7
+ FROM call RETURNING id
8
+ )
9
+ INSERT INTO Identifier (file, name, parent_id) SELECT :file, 'isNaN', id
10
+ FROM member
@@ -0,0 +1,11 @@
1
+ [
2
+ withNamed(call = insert(into(
3
+ CallExpression,
4
+ [file, parent_id],
5
+ values(':file', ':parent_id'),
6
+ ), returning(id)), member = insert(into(
7
+ MemberExpression,
8
+ [file, parent_id],
9
+ select(':file', id, from(call)),
10
+ ), returning(id)), insert(into(Identifier, [file, name, parent_id], select(':file', 'isNaN', id, from(member))))),
11
+ ];
@@ -1,6 +1,6 @@
1
1
  WITH
2
2
  call AS (
3
- INSERT INTO CallExpression (parent_id) VALUES (:parent_id) RETURNING id
3
+ INSERT INTO CallExpression (parent_id) VALUES (':parent_id') RETURNING id
4
4
  ),
5
5
  member AS (
6
6
  INSERT INTO MemberExpression (parent_id) SELECT id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "happy-sql",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "type": "module",
5
5
  "description": "sql to js ast parser and printer",
6
6
  "exports": {
@@ -1,2 +0,0 @@
1
- SELECT COUNT(*)
2
- FROM users