Skip to content

Commit 1f73638

Browse files
authored
Merge pull request #233 from jetstreamapp/bug/startsWith
Fixed composeQuery whereClause bug
2 parents ca32ba7 + 569cab5 commit 1f73638

File tree

5 files changed

+122
-39
lines changed

5 files changed

+122
-39
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.9.1
4+
5+
May 29, 2023
6+
7+
Fixed bug with composeQuery when some of the WHERE clause values were not strings.
8+
39
## 4.9.0
410

511
May 23, 2023
@@ -470,7 +476,6 @@ export type FieldType =
470476
-}
471477

472478
+export type HavingClause = HavingClauseWithoutOperator | HavingClauseWithRightCondition;
473-
474479
```
475480

476481
## 2.5.5
@@ -851,7 +856,6 @@ export interface FunctionExp {
851856
isAggregateFn?: boolean;
852857
- fn?: FunctionExp;
853858
}
854-
855859
```
856860

857861
## 1.2.1

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ export function getWhereValue(value: any | any[], literalType?: LiteralType | Li
243243
switch (literalType) {
244244
case 'STRING': {
245245
if (Array.isArray(value)) {
246-
return value.filter(Boolean).map(val => ((val as string).startsWith("'") ? val : `'${val ?? ''}'`));
246+
return value.filter(Boolean).map(val => (isString(val) && val.startsWith("'") ? val : `'${val ?? ''}'`));
247247
} else {
248248
value = String(value ?? '');
249-
return value.startsWith("'") ? value : `'${value}'`;
249+
return isString(value) && value.startsWith("'") ? value : `'${value ?? ''}'`;
250250
}
251251
}
252252
case 'APEX_BIND_VARIABLE': {
@@ -262,8 +262,7 @@ export function getWhereValue(value: any | any[], literalType?: LiteralType | Li
262262
function whereValueHelper(value: any, literalType?: LiteralType) {
263263
switch (literalType) {
264264
case 'STRING': {
265-
value = String(value ?? '');
266-
return value.startsWith("'") ? value : `'${value ?? ''}'`;
265+
return isString(value) && value.startsWith("'") ? value : `'${value ?? ''}'`;
267266
}
268267
default: {
269268
return value;

test/test-cases-compose.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,81 @@ export const testCases: TestCase[] = [
8181
},
8282
},
8383
},
84+
{
85+
testCase: 6,
86+
soql: "SELECT Id, Fax FROM Account WHERE Fax IN ('55', 'foo')",
87+
input: {
88+
sObject: 'Account',
89+
fields: [
90+
{
91+
type: 'Field',
92+
field: 'Id',
93+
},
94+
{
95+
type: 'Field',
96+
field: 'Fax',
97+
},
98+
],
99+
where: {
100+
left: {
101+
field: 'Fax',
102+
operator: 'IN',
103+
value: [55, null, undefined, 'foo'],
104+
literalType: 'STRING',
105+
},
106+
},
107+
},
108+
},
109+
{
110+
testCase: 7,
111+
soql: "SELECT Id, Fax FROM Account WHERE Fax IN ('55')",
112+
input: {
113+
sObject: 'Account',
114+
fields: [
115+
{
116+
type: 'Field',
117+
field: 'Id',
118+
},
119+
{
120+
type: 'Field',
121+
field: 'Fax',
122+
},
123+
],
124+
where: {
125+
left: {
126+
field: 'Fax',
127+
operator: 'IN',
128+
value: 55 as any,
129+
literalType: 'STRING',
130+
},
131+
},
132+
},
133+
},
134+
{
135+
testCase: 7,
136+
soql: "SELECT Id, Fax FROM Account WHERE Fax = '55'",
137+
input: {
138+
sObject: 'Account',
139+
fields: [
140+
{
141+
type: 'Field',
142+
field: 'Id',
143+
},
144+
{
145+
type: 'Field',
146+
field: 'Fax',
147+
},
148+
],
149+
where: {
150+
left: {
151+
field: 'Fax',
152+
operator: '=',
153+
value: 55 as any,
154+
literalType: 'STRING',
155+
},
156+
},
157+
},
158+
},
84159
];
85160

86161
export default testCases;

test/test-cases.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ export const testCases: TestCase[] = [
284284
},
285285
{
286286
testCase: 21,
287-
soql:
288-
'SELECT TYPEOF What WHEN Account THEN Phone, NumberOfEmployees WHEN Opportunity THEN Amount, CloseDate ELSE Name, Email END FROM Event',
287+
soql: 'SELECT TYPEOF What WHEN Account THEN Phone, NumberOfEmployees WHEN Opportunity THEN Amount, CloseDate ELSE Name, Email END FROM Event',
289288
output: {
290289
fields: [
291290
{
@@ -320,8 +319,7 @@ export const testCases: TestCase[] = [
320319
},
321320
{
322321
testCase: 23,
323-
soql:
324-
'SELECT Amount, Id, Name, (SELECT Quantity, ListPrice, PricebookEntry.UnitPrice, PricebookEntry.Name FROM OpportunityLineItems) FROM Opportunity',
322+
soql: 'SELECT Amount, Id, Name, (SELECT Quantity, ListPrice, PricebookEntry.UnitPrice, PricebookEntry.Name FROM OpportunityLineItems) FROM Opportunity',
325323
output: {
326324
fields: [
327325
{ type: 'Field', field: 'Amount' },
@@ -361,8 +359,7 @@ export const testCases: TestCase[] = [
361359
},
362360
{
363361
testCase: 25,
364-
soql:
365-
'SELECT UserId, COUNT(Id) FROM LoginHistory WHERE LoginTime > 2010-09-20T22:16:30.000Z AND LoginTime < 2010-09-21 GROUP BY UserId',
362+
soql: 'SELECT UserId, COUNT(Id) FROM LoginHistory WHERE LoginTime > 2010-09-20T22:16:30.000Z AND LoginTime < 2010-09-21 GROUP BY UserId',
366363
output: {
367364
fields: [
368365
{ type: 'Field', field: 'UserId' },
@@ -464,8 +461,7 @@ export const testCases: TestCase[] = [
464461
},
465462
{
466463
testCase: 30,
467-
soql:
468-
'SELECT Type, BillingCountry, GROUPING(Type)grpType, GROUPING(BillingCountry) grpCty, COUNT(id) accts FROM Account GROUP BY CUBE(Type,BillingCountry) ORDER BY GROUPING(Type), GROUPING(Id,BillingCountry), Name DESC NULLS FIRST, Id ASC NULLS LAST',
464+
soql: 'SELECT Type, BillingCountry, GROUPING(Type)grpType, GROUPING(BillingCountry) grpCty, COUNT(id) accts FROM Account GROUP BY CUBE(Type,BillingCountry) ORDER BY GROUPING(Type), GROUPING(Id,BillingCountry), Name DESC NULLS FIRST, Id ASC NULLS LAST',
469465
soqlComposed:
470466
'SELECT Type, BillingCountry, GROUPING(Type) grpType, GROUPING(BillingCountry) grpCty, COUNT(id) accts FROM Account GROUP BY CUBE(Type, BillingCountry) ORDER BY GROUPING(Type), GROUPING(Id, BillingCountry), Name DESC NULLS FIRST, Id ASC NULLS LAST',
471467
output: {
@@ -519,8 +515,7 @@ export const testCases: TestCase[] = [
519515
},
520516
{
521517
testCase: 32,
522-
soql:
523-
"SELECT Id FROM Account WHERE (Id IN ('1', '2', '3') OR (NOT Id = '2') OR (Name LIKE '%FOO%' OR (Name LIKE '%ARM%' AND FOO = 'bar')))",
518+
soql: "SELECT Id FROM Account WHERE (Id IN ('1', '2', '3') OR (NOT Id = '2') OR (Name LIKE '%FOO%' OR (Name LIKE '%ARM%' AND FOO = 'bar')))",
524519
output: {
525520
fields: [{ type: 'Field', field: 'Id' }],
526521
sObject: 'Account',
@@ -637,8 +632,7 @@ export const testCases: TestCase[] = [
637632
},
638633
{
639634
testCase: 37,
640-
soql:
641-
"SELECT UrlName FROM KnowledgeArticleVersion WHERE PublishStatus = 'draft' WITH DATA CATEGORY Geography__c AT usa__c AND Product__c ABOVE_OR_BELOW mobile_phones__c",
635+
soql: "SELECT UrlName FROM KnowledgeArticleVersion WHERE PublishStatus = 'draft' WITH DATA CATEGORY Geography__c AT usa__c AND Product__c ABOVE_OR_BELOW mobile_phones__c",
642636
output: {
643637
fields: [{ type: 'Field', field: 'UrlName' }],
644638
sObject: 'KnowledgeArticleVersion',
@@ -683,8 +677,7 @@ export const testCases: TestCase[] = [
683677
},
684678
{
685679
testCase: 44,
686-
soql:
687-
"SELECT amount, FORMAT(amount) Amt, convertCurrency(amount) editDate, FORMAT(convertCurrency(amount)) convertedCurrency FROM Opportunity WHERE id = '12345'",
680+
soql: "SELECT amount, FORMAT(amount) Amt, convertCurrency(amount) editDate, FORMAT(convertCurrency(amount)) convertedCurrency FROM Opportunity WHERE id = '12345'",
688681
output: {
689682
fields: [
690683
{ type: 'Field', field: 'amount' },
@@ -809,8 +802,7 @@ export const testCases: TestCase[] = [
809802
},
810803
{
811804
testCase: 49,
812-
soql:
813-
"SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName LIKE 'apple%') AND Id IN (SELECT AccountId FROM Opportunity WHERE isClosed = FALSE)",
805+
soql: "SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName LIKE 'apple%') AND Id IN (SELECT AccountId FROM Opportunity WHERE isClosed = FALSE)",
814806
output: {
815807
fields: [
816808
{ type: 'Field', field: 'Id' },
@@ -939,8 +931,7 @@ export const testCases: TestCase[] = [
939931
},
940932
{
941933
testCase: 55,
942-
soql:
943-
'SELECT Id, CreatedById, CreatedDate, DefType, IsDeleted, Format, LastModifiedById, LastModifiedDate, AuraDefinitionBundleId, ManageableState, Source, SystemModstamp FROM AuraDefinition',
934+
soql: 'SELECT Id, CreatedById, CreatedDate, DefType, IsDeleted, Format, LastModifiedById, LastModifiedDate, AuraDefinitionBundleId, ManageableState, Source, SystemModstamp FROM AuraDefinition',
944935
output: {
945936
fields: [
946937
{ type: 'Field', field: 'Id' },
@@ -974,8 +965,7 @@ export const testCases: TestCase[] = [
974965
},
975966
{
976967
testCase: 57,
977-
soql:
978-
"SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus = 'online' WITH DATA CATEGORY Geography__c ABOVE usa__c WITH SECURITY_ENFORCED",
968+
soql: "SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus = 'online' WITH DATA CATEGORY Geography__c ABOVE usa__c WITH SECURITY_ENFORCED",
979969
output: {
980970
fields: [{ type: 'Field', field: 'Title' }],
981971
sObject: 'KnowledgeArticleVersion',
@@ -986,8 +976,7 @@ export const testCases: TestCase[] = [
986976
},
987977
{
988978
testCase: 58,
989-
soql:
990-
"SELECT Id FROM Account WHERE (((Name = '1' OR Name = '2') AND Name = '3')) AND (((Description = '123') OR (Id = '1' AND Id = '2'))) AND Id = '1'",
979+
soql: "SELECT Id FROM Account WHERE (((Name = '1' OR Name = '2') AND Name = '3')) AND (((Description = '123') OR (Id = '1' AND Id = '2'))) AND Id = '1'",
991980
output: {
992981
fields: [{ type: 'Field', field: 'Id' }],
993982
sObject: 'Account',
@@ -1190,8 +1179,7 @@ export const testCases: TestCase[] = [
11901179
},
11911180
{
11921181
testCase: 71,
1193-
soql:
1194-
'SELECT CALENDAR_YEAR(convertTimezone(CreatedDate)) calYear, SUM(Amount) mySum FROM Opportunity GROUP BY CALENDAR_YEAR(convertTimezone(CreatedDate))',
1182+
soql: 'SELECT CALENDAR_YEAR(convertTimezone(CreatedDate)) calYear, SUM(Amount) mySum FROM Opportunity GROUP BY CALENDAR_YEAR(convertTimezone(CreatedDate))',
11951183
output: {
11961184
fields: [
11971185
{
@@ -1341,8 +1329,7 @@ export const testCases: TestCase[] = [
13411329
},
13421330
{
13431331
testCase: 79,
1344-
soql:
1345-
'SELECT LeadSource, Rating, GROUPING(LeadSource) grpLS, GROUPING(Rating) grpRating, COUNT(Name) cnt FROM Lead GROUP BY ROLLUP(LeadSource, Rating)',
1332+
soql: 'SELECT LeadSource, Rating, GROUPING(LeadSource) grpLS, GROUPING(Rating) grpRating, COUNT(Name) cnt FROM Lead GROUP BY ROLLUP(LeadSource, Rating)',
13461333
output: {
13471334
fields: [
13481335
{ type: 'Field', field: 'LeadSource' },
@@ -1376,8 +1363,7 @@ export const testCases: TestCase[] = [
13761363
},
13771364
{
13781365
testCase: 80,
1379-
soql:
1380-
'SELECT Type, BillingCountry, GROUPING(Type) grpType, GROUPING(BillingCountry) grpCty, COUNT(id) accts FROM Account GROUP BY CUBE(Type, BillingCountry) ORDER BY GROUPING(Type), GROUPING(BillingCountry)',
1366+
soql: 'SELECT Type, BillingCountry, GROUPING(Type) grpType, GROUPING(BillingCountry) grpCty, COUNT(id) accts FROM Account GROUP BY CUBE(Type, BillingCountry) ORDER BY GROUPING(Type), GROUPING(BillingCountry)',
13811367
output: {
13821368
fields: [
13831369
{ type: 'Field', field: 'Type' },
@@ -1415,8 +1401,7 @@ export const testCases: TestCase[] = [
14151401
},
14161402
{
14171403
testCase: 81,
1418-
soql:
1419-
'SELECT HOUR_IN_DAY(convertTimezone(CreatedDate)), SUM(Amount) FROM Opportunity GROUP BY HOUR_IN_DAY(convertTimezone(CreatedDate))',
1404+
soql: 'SELECT HOUR_IN_DAY(convertTimezone(CreatedDate)), SUM(Amount) FROM Opportunity GROUP BY HOUR_IN_DAY(convertTimezone(CreatedDate))',
14201405
output: {
14211406
fields: [
14221407
{
@@ -1907,8 +1892,7 @@ export const testCases: TestCase[] = [
19071892
},
19081893
{
19091894
testCase: 101,
1910-
soql:
1911-
'SELECT Id FROM LoginHistory WHERE LoginTime > 2020-04-23T09:00:00.00000000000000000000000000000000+00:00 AND LoginTime < 2020-04-15T02:40:03.000+0000',
1895+
soql: 'SELECT Id FROM LoginHistory WHERE LoginTime > 2020-04-23T09:00:00.00000000000000000000000000000000+00:00 AND LoginTime < 2020-04-15T02:40:03.000+0000',
19121896
output: {
19131897
fields: [{ type: 'Field', field: 'Id' }],
19141898
sObject: 'LoginHistory',
@@ -2405,6 +2389,28 @@ export const testCases: TestCase[] = [
24052389
{
24062390
testCase: 118,
24072391
options: { allowApexBindVariables: true },
2392+
soql: `SELECT Name FROM Account WHERE Name IN ('GenePoint\\'s \\n Ok!?!@#$^%$&*()_+')`,
2393+
output: {
2394+
fields: [
2395+
{
2396+
type: 'Field',
2397+
field: 'Name',
2398+
},
2399+
],
2400+
sObject: 'Account',
2401+
where: {
2402+
left: {
2403+
field: 'Name',
2404+
literalType: 'STRING',
2405+
operator: 'IN',
2406+
value: [`'GenePoint\\'s \\n Ok!?!@#$^%$&*()_+'`],
2407+
},
2408+
},
2409+
},
2410+
},
2411+
{
2412+
testCase: 119,
2413+
options: { allowApexBindVariables: true },
24082414
soql: `SELECT State_Abbr_c FROM Contact WHERE State_Abbr_c = 'MI' OR State_Abbr_c = 'km'`,
24092415
output: {
24102416
fields: [

0 commit comments

Comments
 (0)