Skip to content

Commit 9bd42e6

Browse files
committed
fix: fixed backwards compatibility issues
1 parent 2165d6d commit 9bd42e6

File tree

6 files changed

+143
-1353
lines changed

6 files changed

+143
-1353
lines changed

lib/dialects/base/blocks.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,4 +855,31 @@ module.exports = function (dialect) {
855855

856856
throw new TypeError('Unknown `type` property value "' + type + '"');
857857
});
858+
859+
dialect.blocks.add('insert:values', (parameters) => {
860+
let { values } = parameters;
861+
862+
if (!isArray(values)) {
863+
values = [values];
864+
}
865+
866+
const fields =
867+
parameters.fields ||
868+
map(values, (row) => keys(row))
869+
.flat()
870+
.filter((value, index, array) => array.indexOf(value) === index);
871+
872+
return dialect.buildTemplate('insertValues', {
873+
fields,
874+
values: map(values, (row) =>
875+
map(fields, (field) =>
876+
dialect.buildBlock('value', { value: row[field] })
877+
)
878+
),
879+
});
880+
});
881+
882+
dialect.blocks.add('insertValues:values', (parameters) =>
883+
map(parameters.values, (row) => '(' + row.join(', ') + ')').join(', ')
884+
);
858885
};

lib/dialects/base/index.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const blocksInit = require('./blocks.js');
55
const operatorsInit = require('./operators/index.js');
66
const modifiersInit = require('./modifiers.js');
77

8-
const blockRegExp = /{([a-z\d]+)}/gi;
8+
const blockRegExp = /{([a-z\d:]+)}/gi;
99

1010
function Dialect(builder) {
1111
this.builder = builder;
@@ -62,18 +62,46 @@ Dialect.prototype.buildQuery = function (query) {
6262
return this.buildTemplate(template.pattern, query);
6363
};
6464

65-
Dialect.prototype.buildTemplate = function (template, query) {
66-
const result = template.replaceAll(blockRegExp, (match, blockName) => {
67-
const block = this.blocks.get(blockName);
65+
Dialect.prototype.buildTemplate = function (templateName, query) {
66+
// If templateName is already a pattern string, use it directly
67+
if (typeof templateName === 'string' && templateName.includes('{')) {
68+
const template = templateName;
69+
const result = template.replaceAll(blockRegExp, (match, blockName) => {
70+
const block = this.blocks.get(blockName);
6871

69-
if (!block) {
70-
// Return empty string for unknown blocks instead of throwing error
71-
return '';
72-
}
72+
if (!block) {
73+
// Return empty string for unknown blocks instead of throwing error
74+
return '';
75+
}
76+
77+
const blockResult = block.call(this, query);
78+
return blockResult || '';
79+
});
80+
81+
// Clean up extra spaces
82+
return result.replaceAll(/\s+/g, ' ').trim();
83+
}
84+
85+
// Otherwise, get the template by name
86+
const template = this.templates.get(templateName);
87+
if (!template) {
88+
throw new Error('Unknown template "' + templateName + '"');
89+
}
7390

74-
const blockResult = block.call(this, query);
75-
return blockResult || '';
76-
});
91+
const result = template.pattern.replaceAll(
92+
blockRegExp,
93+
(match, blockName) => {
94+
const block = this.blocks.get(blockName);
95+
96+
if (!block) {
97+
// Return empty string for unknown blocks instead of throwing error
98+
return '';
99+
}
100+
101+
const blockResult = block.call(this, query);
102+
return blockResult || '';
103+
}
104+
);
77105

78106
// Clean up extra spaces
79107
return result.replaceAll(/\s+/g, ' ').trim();

lib/dialects/base/templates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = function (dialect) {
6868
});
6969

7070
dialect.templates.add('insertValues', {
71-
pattern: '({fields}) values {values}',
71+
pattern: '({fields}) values {insertValues:values}',
7272
validate(type, parameters) {
7373
templateChecks.requiredProp('values', parameters, 'fields');
7474
templateChecks.propType('values', parameters, 'fields', 'array');
@@ -250,7 +250,7 @@ module.exports = function (dialect) {
250250

251251
dialect.templates.add('insert', {
252252
pattern:
253-
'{with} {withRecursive} insert {or} into {table} {values} {condition} ' +
253+
'{with} {withRecursive} insert {or} into {table} {insert:values} {condition} ' +
254254
'{returning}',
255255
validate(type, parameters) {
256256
templateChecks.onlyOneOfProps(type, parameters, [

0 commit comments

Comments
 (0)