Skip to content

Commit 0138981

Browse files
Fixed all tests
1 parent 402027b commit 0138981

File tree

6 files changed

+722
-687
lines changed

6 files changed

+722
-687
lines changed

dist/quick-sql.js

Lines changed: 570 additions & 557 deletions
Large diffs are not rendered by default.

dist/quick-sql.umd.cjs

Lines changed: 106 additions & 105 deletions
Large diffs are not rendered by default.

src/sample.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Chance from 'chance';
2+
//import lexer from './lexer.js';
23

34

45
export function generateSample( lTable, lColumn, lType, values ) {
@@ -12,20 +13,16 @@ export function generateSample( lTable, lColumn, lType, values ) {
1213
let min = 0;
1314
let max = values.length;
1415
let value = values[Math.floor(seededRandom() * (max - min)) + min];
15-
var optQuote = '\'';
16-
if( type.startsWith('INTEGER') || type.startsWith('NUMBER') || type.startsWith('DATE') )
17-
optQuote = '';
18-
else {
19-
if( value.toLowerCase && value.toLowerCase() == 'null' )
20-
optQuote = '';
21-
if( value.charAt && value.charAt(0) == '\'' )
22-
optQuote = '';
23-
if( value.charAt && value.charAt(0) == 'q' && value.charAt(1) == '\'' )
24-
optQuote = '';
16+
if( !type.startsWith('INTEGER') && !type.startsWith('NUMBER') && !type.startsWith('DATE')
17+
&& (!value.toLowerCase || value.toLowerCase() != 'null')
18+
&& ( !value.charAt || (value.charAt(0) != 'q' && value.charAt(1) != '\'') )
19+
) {
20+
if( value.charAt && value.charAt(0) == '\'' )
21+
value = value.substring(1,value.length-1);
22+
value = value.replaceAll('\'','\'\'');
23+
value = "'"+value+"'";
2524
}
26-
if( value.replaceAll && 0 < value.indexOf('\'') && value.indexOf('\'') < value.length-1 )
27-
value = value.replaceAll('\'','\'\'');
28-
return optQuote+value+optQuote;
25+
return value;
2926
}
3027

3128
if( column == 'NAME' && 0 <= table.indexOf('DEPARTMENT') ) {

src/tree.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,23 @@ let tree = (function(){
447447
parentPref = parent.parseName()+'_';
448448
const parent_child = concatNames(parentPref,this.parseName());
449449

450-
let values = this.getValues('check');
451-
values = '(' + values + ')';
452450
let offset = tab;
453451
if( parent != null )
454452
offset = ' '.repeat(parent.maxChildNameLen());
455-
if( this.children != null && 0 < this.children.length ) { // table level
456-
ret += tab + 'constraint '+concatNames(ddl.objPrefix(),parent_child,'_ck');
457-
ret += ' check '+values+',\n';
458-
} else {
459-
ret +=' constraint '+concatNames(ddl.objPrefix(),parent_child,'_ck')+'\n';
460-
ret += tab + tab+offset +'check ('+this.parseName()+' in '+values+')';
453+
let constr = this.getGeneralConstraint();
454+
if( constr != null ) {
455+
if( this.children != null && 0 < this.children.length ) { // (general) table level constraint
456+
ret += tab + 'constraint '+concatNames(ddl.objPrefix(),parent_child,'_ck');
457+
ret += ' check '+ constr +',\n';
458+
} else { // general column level constraint
459+
ret +=' constraint '+concatNames(ddl.objPrefix(),parent_child,'_ck')+'\n';
460+
ret += tab + tab+offset +'check '+ constr +'';
461+
}
462+
return ret;
461463
}
462-
464+
const values = this.getValues('check');
465+
ret +=' constraint '+concatNames(ddl.objPrefix(),parent_child,'_ck')+'\n';
466+
ret += tab + tab+offset +'check ('+this.parseName()+' in ('+values+'))';
463467
}
464468
return ret;
465469
}
@@ -564,9 +568,27 @@ let tree = (function(){
564568
return null;
565569
};
566570

571+
this.getGeneralConstraint = function() { // parenthesized constraint to return verbatim, e.g. c1 /check (c1 in ('A','B','C'))
572+
let from = this.indexOf('check');
573+
if( 0 < from && this.src[from-1].value == '/' &&
574+
( this.src[from+1].value == '(' || this.src[from+1].value.toLowerCase() == 'not' )
575+
) {
576+
let i = from+2
577+
for( ; i < this.src.length && this.src[i].value != '/' && this.src[i].value != '[' ; )
578+
i++;
579+
let ret = this.content.substring(this.src[from+1].begin, this.src[i-1].end);
580+
if( ret.charAt(0) != '(' )
581+
ret = '('+ret+')';
582+
return ret;
583+
}
584+
585+
return null;
586+
}
587+
567588
this.listValues = function( check_or_values ) {
568589
let ret = [];
569590
let from = this.indexOf(check_or_values);
591+
570592
let separator = ' '; // e.g. status /check open completed closed /values open, open, open, open, closed, completed
571593
for( let i = from+1; i < this.src.length && this.src[i].value != '/' && this.src[i].value != '[' ; i++ )
572594
if( this.src[i].value == ',' ) {

test/single_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from "fs";
55
try {
66
let file = '//bugs/Bug35637614.quicksql';
77
//file = '//experimental/food_product.json';
8-
//file = '//JSON/car_racing/2.qsql';
8+
file = '//constraints.qsql';
99
//file = '//apex/timecard.quicksql';
1010

1111
let args = process.argv.slice(2);

test/small_tests.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,16 +660,18 @@ students
660660
assert( "0 < output.indexOf(\"check (financial_year in ('23/24','24/25')),\")" );
661661
assert( "0 < output.indexOf(\"check (surname in ('O''Hara',q'{O'Tool}')),\")" );
662662
assert( "0 < output.indexOf(\"check (start_date in (to_date('01-APR-2025','DD-MON-YYYY')))\")" );
663+
assert( "output.indexOf(\"''24/25''\") < 0" );
664+
assert( "output.indexOf(\"q''{O''Tool}''\") < 0" );
665+
assert( "output.indexOf(\"to_date(''01-APR-2025'',''DD-MON-YYYY'')\") < 0" );
663666

664667
}
665668

666-
667669
small_tests();
668670

669671
console.log(assertionCnt);
670672

671673
// metatest that watches tests
672-
const minimalTestCnt = 125;
674+
const minimalTestCnt = 130;
673675
if( assertionCnt < minimalTestCnt ) {
674676
console.error("assertionCnt < "+minimalTestCnt);
675677
throw new Error('Test failed');

0 commit comments

Comments
 (0)