@sqg/sqg 0.19.1 → 0.21.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.
@@ -69,7 +69,7 @@ export class {{className}} {
69
69
  {{#unless skipGenerateFunction}}
70
70
  {{functionName}}({{#each variables}}{{name}}: {{type}}{{#unless @last}}, {{/unless}}{{/each}}): {{> returnType }} {
71
71
  const stmt = this.prepare<[{{> paramTypes}}], {{> resultType}}>('{{id}}',
72
- {{{quote sqlQuery}}}{{#if isPluck}}, true{{/if}});
72
+ {{{sqlExpr this}}}{{#if isPluck}}, true{{/if}});
73
73
  {{> execute}}
74
74
  }
75
75
  {{/unless}}
@@ -75,6 +75,7 @@ public class {{className}} {
75
75
 
76
76
  {{#*inline "columnTypesRecord"}}
77
77
  {{#if isQuery}}
78
+ {{#if (shouldDeclareType this)}}
78
79
  {{#if isOne}}
79
80
  public record {{rowType}}({{#each columns}}{{type}} {{name}}{{#unless @last}}, {{/unless}}{{/each}}) {}
80
81
  {{else}}
@@ -83,7 +84,7 @@ public record {{rowType}}(PreparedStatement statement, RootAllocator allocator,
83
84
  public boolean loadNextBatch() throws IOException {
84
85
  return reader.loadNextBatch();
85
86
  }
86
-
87
+
87
88
  public int getRowCount() throws IOException {
88
89
  return reader.getVectorSchemaRoot().getRowCount();
89
90
  }
@@ -96,6 +97,7 @@ public record {{rowType}}(PreparedStatement statement, RootAllocator allocator,
96
97
  }
97
98
  {{/if}}
98
99
  {{/if}}
100
+ {{/if}}
99
101
  {{/inline}}
100
102
 
101
103
  {{#*inline "returnType"}}
@@ -223,6 +223,15 @@ public class {{className}} {
223
223
 
224
224
  {{{declareEnums queries}}}
225
225
 
226
+ {{#each attachers}}
227
+ /** Attach the "{{alias}}" postgres source under the same alias used during generation. */
228
+ public void {{functionName}}(String connectionString) throws SQLException {
229
+ try (var stmt = connection.createStatement()) {
230
+ stmt.execute("ATTACH '" + connectionString + "' AS {{alias}} (TYPE postgres)");
231
+ }
232
+ }
233
+
234
+ {{/each}}
226
235
  {{#each queries}}
227
236
  {{#unless skipGenerateFunction}}
228
237
  {{>columnTypesRecord}}
@@ -135,6 +135,12 @@ class {{className}}:
135
135
  {{/if}}
136
136
  {{/if}}
137
137
 
138
+ {{#each attachers}}
139
+ def {{functionName}}(self, connection_string: str) -> None:
140
+ """Attach the "{{alias}}" postgres source under the same alias used during generation."""
141
+ self._conn.execute(f"ATTACH '{connection_string}' AS {{alias}} (TYPE postgres)")
142
+
143
+ {{/each}}
138
144
  {{#each queries}}
139
145
  {{#unless skipGenerateFunction}}
140
146
  {{#if isQuery}}
@@ -142,7 +148,7 @@ class {{className}}:
142
148
  {{#if isOne}}
143
149
  def {{functionName}}(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> {{{pyTypeOrNone (lookup columns 0)}}}:
144
150
  row = self._conn.execute(
145
- {{{quote sqlQuery}}}, {{> params}}
151
+ {{{sqlExpr this}}}, {{> params}}
146
152
  ).fetchone()
147
153
  if row is None:
148
154
  return None
@@ -150,19 +156,19 @@ class {{className}}:
150
156
 
151
157
  def {{functionName}}_raw(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> tuple[Any, ...] | None:
152
158
  return self._conn.execute(
153
- {{{quote sqlQuery}}}, {{> params}}
159
+ {{{sqlExpr this}}}, {{> params}}
154
160
  ).fetchone()
155
161
 
156
162
  {{else}}
157
163
  def {{functionName}}(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> list[{{{pyType (lookup columns 0)}}}]:
158
164
  rows = self._conn.execute(
159
- {{{quote sqlQuery}}}, {{> params}}
165
+ {{{sqlExpr this}}}, {{> params}}
160
166
  ).fetchall()
161
167
  return [r[0] for r in rows]
162
168
 
163
169
  def {{functionName}}_raw(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> list[tuple[Any, ...]]:
164
170
  return self._conn.execute(
165
- {{{quote sqlQuery}}}, {{> params}}
171
+ {{{sqlExpr this}}}, {{> params}}
166
172
  ).fetchall()
167
173
 
168
174
  {{/if}}
@@ -170,7 +176,7 @@ class {{className}}:
170
176
  {{#if isOne}}
171
177
  def {{functionName}}(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> {{{rowType}}} | None:
172
178
  row = self._conn.execute(
173
- {{{quote sqlQuery}}}, {{> params}}
179
+ {{{sqlExpr this}}}, {{> params}}
174
180
  ).fetchone()
175
181
  if row is None:
176
182
  return None
@@ -178,19 +184,19 @@ class {{className}}:
178
184
 
179
185
  def {{functionName}}_raw(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> tuple[Any, ...] | None:
180
186
  return self._conn.execute(
181
- {{{quote sqlQuery}}}, {{> params}}
187
+ {{{sqlExpr this}}}, {{> params}}
182
188
  ).fetchone()
183
189
 
184
190
  {{else}}
185
191
  def {{functionName}}(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> list[{{{rowType}}}]:
186
192
  rows = self._conn.execute(
187
- {{{quote sqlQuery}}}, {{> params}}
193
+ {{{sqlExpr this}}}, {{> params}}
188
194
  ).fetchall()
189
195
  return [{{{constructRow this}}} for row in rows]
190
196
 
191
197
  def {{functionName}}_raw(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> list[tuple[Any, ...]]:
192
198
  return self._conn.execute(
193
- {{{quote sqlQuery}}}, {{> params}}
199
+ {{{sqlExpr this}}}, {{> params}}
194
200
  ).fetchall()
195
201
 
196
202
  {{/if}}
@@ -198,7 +204,7 @@ class {{className}}:
198
204
  {{else}}
199
205
  def {{functionName}}(self{{#each variables}}, {{name}}: {{{type}}}{{/each}}) -> None:
200
206
  self._conn.execute(
201
- {{{quote sqlQuery}}}, {{> params}}
207
+ {{{sqlExpr this}}}, {{> params}}
202
208
  )
203
209
 
204
210
  {{/if}}
@@ -58,10 +58,17 @@ export class {{className}} {
58
58
  );
59
59
  }
60
60
 
61
+ {{#each attachers}}
62
+ /** Attach the "{{alias}}" postgres source under the same alias used during generation. */
63
+ async {{functionName}}(connectionString: string): Promise<void> {
64
+ await this.conn.run(`ATTACH '${connectionString}' AS {{alias}} (TYPE postgres)`);
65
+ }
66
+
67
+ {{/each}}
61
68
  {{#each queries}}
62
69
  {{#unless skipGenerateFunction}}
63
70
  async {{functionName}}({{#each variables}}{{name}}: {{type}}{{#unless @last}}, {{/unless}}{{/each}}): Promise<{{> returnType }}> {
64
- const sql = {{{quote sqlQuery}}};
71
+ const sql = {{{sqlExpr this}}};
65
72
  {{#if (hasListParams this)}}
66
73
  const stmt = await this.conn.prepare(sql);
67
74
  {{{bindStatements this}}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqg/sqg",
3
- "version": "0.19.1",
3
+ "version": "0.21.0",
4
4
  "description": "SQG - SQL Query Generator - Type-safe code generation from SQL (https://sqg.dev)",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",