Skip to content

Commit bc00c09

Browse files
authored
Merge pull request #292 from cyyeh/fix-comments-in-sql
fix #236, can't write comments in sql files
2 parents c60ab59 + f0d06c9 commit bc00c09

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

packages/core/src/lib/template-engine/built-in-extensions/cache/cacheTagRunner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export class CacheTagRunner extends TagRunner {
4444
query = query
4545
.split(/\r?\n/)
4646
.filter((line) => line.trim().length > 0)
47-
.join('\n');
47+
.join('\n')
48+
.replace(/--.*(?:\n|$)|\/\*[\s\S]*?\*\//g, ''); // remove single-line comments and multi-line comments
49+
4850
// Set the default vulcan created cache table schema, so we could query the cache table directly, not need user to type schema in the SQL.
4951
query = `set schema=${vulcanCacheSchemaName};`.concat('\n').concat(query);
5052
// Create the builder which access "vulcan.cache" data source for cache layer query

packages/core/src/lib/template-engine/built-in-extensions/query-builder/reqTagRunner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ export class ReqTagRunner extends TagRunner {
4040
for (let index = 0; index < contentArgs.length; index++) {
4141
query += await contentArgs[index]();
4242
}
43+
4344
query = query
4445
.split(/\r?\n/)
4546
.filter((line) => line.trim().length > 0)
46-
.join('\n');
47+
.join('\n')
48+
.replace(/--.*(?:\n|$)|\/\*[\s\S]*?\*\//g, ''); // remove single-line comments and multi-line comments
4749

4850
let builder: IDataQueryBuilder | undefined;
4951
// Replace to put the directly query cache builder to original query main builder of "__wrapper__builder",

packages/core/test/template-engine/built-in-extensions/cache/cache.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,48 @@ select count(*) as count from vulcan.user where user.id = {{ context.params.user
174174
expect(binding[0].get('$1')).toBe(`user-id`);
175175
expect(resultData).toEqual([{ count: 1 }]);
176176
});
177+
178+
it('Extension should remove comments in sql statements', async () => {
179+
// Arrange
180+
const {
181+
compiler,
182+
loader,
183+
builder,
184+
executeTemplate,
185+
getCreatedQueries,
186+
getCreatedBinding,
187+
} = await createTestCompiler();
188+
const { compiledData } = await compiler.compile(`
189+
-- this is comment1
190+
/*
191+
this is multiline comments
192+
*/
193+
{% cache %} -- this is comment2
194+
select count(*) as count from vulcan.user where user.id = {{ context.params.userId }}; -- this is comment3
195+
{% endcache %} -- this is comment4
196+
-- this is comment5
197+
198+
/*
199+
this is multiline comments
200+
*/
201+
`);
202+
builder.value.onFirstCall().resolves({
203+
getColumns: () => [],
204+
getData: () => arrayToStream([{ count: 1 }]),
205+
});
206+
// Action
207+
loader.setSource('test', compiledData);
208+
const result = await executeTemplate('test', {
209+
userId: 'user-id',
210+
});
211+
const resultData = await streamToArray(result.getData());
212+
const queries = await getCreatedQueries();
213+
const binding = await getCreatedBinding();
214+
215+
// Assert, cache tag runner add the "set schema=vulcan;" in the prefix sql statement to make query could get correct cache table of schema.
216+
expect(queries[0].trim()).toBe(
217+
`set schema=vulcan;\n select count(*) as count from vulcan.user where user.id = $1;`
218+
);
219+
expect(binding[0].get('$1')).toBe(`user-id`);
220+
expect(resultData).toEqual([{ count: 1 }]);
221+
});

packages/core/test/template-engine/built-in-extensions/query-builder/builder.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,53 @@ it('Extension should throw error when no profile defined', async () => {
167167
`No profile name found`
168168
);
169169
});
170+
171+
it('Extension should remove comments in sql statements', async () => {
172+
// Arrange
173+
const {
174+
compiler,
175+
loader,
176+
builder,
177+
executeTemplate,
178+
getCreatedQueries,
179+
getCreatedBinding,
180+
} = await createTestCompiler();
181+
const { compiledData } = await compiler.compile(`
182+
-- this is comment1
183+
/*
184+
this is multiline comments
185+
*/
186+
{% req userCount main %} -- this is comment2
187+
/*
188+
this is multiline comments
189+
*/
190+
-- this is comment3
191+
select count(*) as count from user where user.id = {{ context.params.userId }}; -- this is comment4
192+
-- this is comment5
193+
{% endreq %}
194+
195+
/*
196+
this is multiline comments
197+
*/
198+
199+
-- this is comment6
200+
`);
201+
builder.value.onFirstCall().resolves({
202+
getColumns: () => [],
203+
getData: () => arrayToStream([{ count: 1 }]),
204+
});
205+
// Action
206+
loader.setSource('test', compiledData);
207+
const result = await executeTemplate('test', {
208+
userId: 'user-id',
209+
});
210+
const resultData = await streamToArray(result.getData());
211+
const queries = await getCreatedQueries();
212+
const binding = await getCreatedBinding();
213+
// Assert
214+
expect(queries[0].trim()).toBe(
215+
`select count(*) as count from user where user.id = $1;`
216+
);
217+
expect(binding[0].get('$1')).toBe(`user-id`);
218+
expect(resultData).toEqual([{ count: 1 }]);
219+
});

0 commit comments

Comments
 (0)