Skip to content

Commit 64082dc

Browse files
committed
fix #236, can't write comments in sql files
1 parent c6ba0de commit 64082dc

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import { Parameterizer } from '@vulcan-sql/core/data-query';
1111
import { InternalError } from '../../../utils/errors';
1212
import { CACHE_MAIN_BUILDER_VAR_NAME } from '../cache/constants';
1313

14+
import {
15+
getLogger,
16+
} from '@vulcan-sql/core';
17+
18+
const logger = getLogger({
19+
scopeName: 'CORE',
20+
});
21+
1422
@VulcanInternalExtension()
1523
export class ReqTagRunner extends TagRunner {
1624
public tags = ['req'];
@@ -40,10 +48,12 @@ export class ReqTagRunner extends TagRunner {
4048
for (let index = 0; index < contentArgs.length; index++) {
4149
query += await contentArgs[index]();
4250
}
51+
4352
query = query
4453
.split(/\r?\n/)
4554
.filter((line) => line.trim().length > 0)
46-
.join('\n');
55+
.join('\n')
56+
.replace(/--.*(?:\n|$)|\/\*[\s\S]*?\*\//g, '') // remove single-line comments and multi-line comments
4757

4858
let builder: IDataQueryBuilder | undefined;
4959
// 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,41 @@ 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+
{% cache %} -- this is comment2
191+
select count(*) as count from vulcan.user where user.id = {{ context.params.userId }}; -- this is comment3
192+
{% endcache %} -- this is comment4
193+
-- this is comment5
194+
`);
195+
builder.value.onFirstCall().resolves({
196+
getColumns: () => [],
197+
getData: () => arrayToStream([{ count: 1 }]),
198+
});
199+
// Action
200+
loader.setSource('test', compiledData);
201+
const result = await executeTemplate('test', {
202+
userId: 'user-id',
203+
});
204+
const resultData = await streamToArray(result.getData());
205+
const queries = await getCreatedQueries();
206+
const binding = await getCreatedBinding();
207+
208+
// Assert, cache tag runner add the "set schema=vulcan;" in the prefix sql statement to make query could get correct cache table of schema.
209+
expect(queries[0].trim()).toBe(
210+
`set schema=vulcan;\n select count(*) as count from vulcan.user where user.id = $1;`
211+
);
212+
expect(binding[0].get('$1')).toBe(`user-id`);
213+
expect(resultData).toEqual([{ count: 1 }]);
214+
});

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,43 @@ it('Extension should throw error when no profile defined', async () => {
167167
`No profile name found`
168168
);
169169
});
170+
171+
it('xtension 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+
{% req userCount main %} -- this is comment2
184+
-- this is comment3
185+
select count(*) as count from user where user.id = {{ context.params.userId }}; -- this is comment4
186+
-- this is comment5
187+
{% endreq %}
188+
189+
-- this is comment6
190+
`);
191+
builder.value.onFirstCall().resolves({
192+
getColumns: () => [],
193+
getData: () => arrayToStream([{ count: 1 }]),
194+
});
195+
// Action
196+
loader.setSource('test', compiledData);
197+
const result = await executeTemplate('test', {
198+
userId: 'user-id',
199+
});
200+
const resultData = await streamToArray(result.getData());
201+
const queries = await getCreatedQueries();
202+
const binding = await getCreatedBinding();
203+
// Assert
204+
expect(queries[0].trim()).toBe(
205+
`select count(*) as count from user where user.id = $1;`
206+
);
207+
expect(binding[0].get('$1')).toBe(`user-id`);
208+
expect(resultData).toEqual([{ count: 1 }]);
209+
});

0 commit comments

Comments
 (0)