foxhound 1.0.33 → 1.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foxhound",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "A Database Query generation library.",
5
5
  "main": "source/FoxHound.js",
6
6
  "scripts": {
@@ -67,14 +67,33 @@ var FoxHoundDialectALASQL = function()
67
67
  *
68
68
  * @method: generateFieldList
69
69
  * @param: {Object} pParameters SQL Query Parameters
70
- * @return: {String} Returns the field list clause
70
+ * @param {Boolean} pIsForCountClause (optional) If true, generate fields for use within a count clause.
71
+ * @return: {String} Returns the field list clause, or empty string if explicit fields are requested but cannot be fulfilled
72
+ * due to missing schema.
71
73
  */
72
- var generateFieldList = function(pParameters)
74
+ var generateFieldList = function(pParameters, pIsForCountClause)
73
75
  {
74
76
  var tmpDataElements = pParameters.dataElements;
75
77
  if (!Array.isArray(tmpDataElements) || tmpDataElements.length < 1)
76
78
  {
77
- return ' *';
79
+ if (!pIsForCountClause)
80
+ {
81
+ return ' *';
82
+ }
83
+ // we need to list all of the table fields explicitly; get them from the schema
84
+ const tmpSchema = Array.isArray(pParameters.query.schema) ? pParameters.query.schema : [];
85
+ if (tmpSchema.length < 1)
86
+ {
87
+ // this means we have no schema; returning an empty string here signals the calling code to handle this case
88
+ return '';
89
+ }
90
+ const idColumn = tmpSchema.find((entry) => entry.Type === 'AutoIdentity');
91
+ if (!idColumn)
92
+ {
93
+ // this means there is no autoincrementing unique ID column; treat as above
94
+ return '';
95
+ }
96
+ return ` ${idColumn.Column}`;
78
97
  }
79
98
 
80
99
  var tmpFieldList = ' ';
@@ -740,9 +759,14 @@ var FoxHoundDialectALASQL = function()
740
759
  {
741
760
  var tmpTableName = generateTableName(pParameters);
742
761
  var tmpWhere = generateWhere(pParameters);
743
- const tmpOptDistinct = pParameters.distinct ? 'DISTINCT' : '';
744
- const tmpCountClause = pParameters.distinct ? `${tmpOptDistinct}${generateFieldList(pParameters)}` : '*';
762
+ const tmpFieldList = pParameters.distinct ? generateFieldList(pParameters, true) : '*';
745
763
 
764
+ // here, we ignore the distinct keyword if no fields have been specified and
765
+ if (pParameters.distinct && tmpFieldList.length < 1)
766
+ {
767
+ console.warn('Distinct requested but no field list or schema are available, so not honoring distinct for count query.');
768
+ }
769
+ const tmpOptDistinct = pParameters.distinct && tmpFieldList.length > 0 ? 'DISTINCT' : '';
746
770
  if (pParameters.queryOverride)
747
771
  {
748
772
  try
@@ -758,7 +782,7 @@ var FoxHoundDialectALASQL = function()
758
782
  }
759
783
  }
760
784
 
761
- return `SELECT COUNT(${tmpCountClause}) AS RowCount FROM${tmpTableName}${tmpWhere};`;
785
+ return `SELECT COUNT(${tmpOptDistinct}${tmpFieldList || '*'}) AS RowCount FROM${tmpTableName}${tmpWhere};`;
762
786
  };
763
787
 
764
788
  var tmpDialect = ({
@@ -44,28 +44,35 @@ var FoxHoundDialectMySQL = function()
44
44
  *
45
45
  * @method: generateFieldList
46
46
  * @param: {Object} pParameters SQL Query Parameters
47
- * @param {Boolean} pExplicitFields (optional) If true, generate explicit fields rather than "TableName.*" (for count distinct).
47
+ * @param {Boolean} pIsForCountClause (optional) If true, generate fields for use within a count clause.
48
48
  * @return: {String} Returns the field list clause, or empty string if explicit fields are requested but cannot be fulfilled
49
49
  * due to missing schema.
50
50
  */
51
- var generateFieldList = function(pParameters, pExplicitFields)
51
+ var generateFieldList = function(pParameters, pIsForCountClause)
52
52
  {
53
53
  var tmpDataElements = pParameters.dataElements;
54
54
  if (!Array.isArray(tmpDataElements) || tmpDataElements.length < 1)
55
55
  {
56
56
  const tmpTableName = generateTableName(pParameters);
57
- if (!pExplicitFields)
57
+ if (!pIsForCountClause)
58
58
  {
59
59
  return tmpTableName + '.*';
60
60
  }
61
61
  // we need to list all of the table fields explicitly; get them from the schema
62
- var tmpSchema = Array.isArray(pParameters.query.schema) ? pParameters.query.schema : [];
63
- tmpDataElements = tmpSchema.map((entry) => `${tmpTableName}.${entry.Column}`.trim());
64
- if (tmpDataElements.length < 1)
62
+ const tmpSchema = Array.isArray(pParameters.query.schema) ? pParameters.query.schema : [];
63
+ if (tmpSchema.length < 1)
65
64
  {
66
65
  // this means we have no schema; returning an empty string here signals the calling code to handle this case
67
66
  return '';
68
67
  }
68
+ const idColumn = tmpSchema.find((entry) => entry.Type === 'AutoIdentity');
69
+ if (!idColumn)
70
+ {
71
+ // this means there is no autoincrementing unique ID column; treat as above
72
+ return '';
73
+ }
74
+ const qualifiedIDColumn = `${tmpTableName}.${idColumn.Column}`;
75
+ return ` ${generateSafeFieldName(qualifiedIDColumn)}`;
69
76
  }
70
77
 
71
78
  var tmpFieldList = ' ';
@@ -801,7 +808,7 @@ var FoxHoundDialectMySQL = function()
801
808
  var tmpTableName = generateTableName(pParameters);
802
809
  var tmpJoin = generateJoins(pParameters);
803
810
  var tmpWhere = generateWhere(pParameters);
804
- // here, we ignore the distinct keyword if no fields have bene specified and
811
+ // here, we ignore the distinct keyword if no fields have been specified and
805
812
  if (pParameters.distinct && tmpFieldList.length < 1)
806
813
  {
807
814
  console.warn('Distinct requested but no field list or schema are available, so not honoring distinct for count query.');
@@ -347,7 +347,7 @@ suite
347
347
  // This is the query generated by the ALASQL dialect
348
348
  libFable.log.trace('Custom Count Query', tmpQuery.query);
349
349
  Expect(tmpQuery.query.body)
350
- .to.equal('SELECT COUNT(DISTINCT *) AS RowCount FROM Animal WHERE `Age` = :Age_w0;');
350
+ .to.equal('SELECT COUNT(*) AS RowCount FROM Animal WHERE `Age` = :Age_w0;');
351
351
  }
352
352
  );
353
353
  test
@@ -420,6 +420,43 @@ suite
420
420
  .to.equal('SELECT COUNT(*) AS RowCount FROM Animal;');
421
421
  }
422
422
  );
423
+ test
424
+ (
425
+ 'Count Query with Distinct',
426
+ function()
427
+ {
428
+ var tmpQuery = libFoxHound.new(libFable)
429
+ .setDialect('ALASQL')
430
+ .setScope('Animal')
431
+ .setDistinct(true);
432
+
433
+ // Build the query
434
+ tmpQuery.buildCountQuery();
435
+ // This is the query generated by the ALASQL dialect
436
+ libFable.log.trace('Count Query', tmpQuery.query);
437
+ Expect(tmpQuery.query.body)
438
+ .to.equal('SELECT COUNT(*) AS RowCount FROM Animal;');
439
+ }
440
+ );
441
+ test
442
+ (
443
+ 'Count Query with Schema and Distinct',
444
+ function()
445
+ {
446
+ var tmpQuery = libFoxHound.new(libFable)
447
+ .setDialect('ALASQL')
448
+ .setScope('Animal')
449
+ .setDistinct(true);
450
+ tmpQuery.query.schema = _AnimalSchema;
451
+
452
+ // Build the query
453
+ tmpQuery.buildCountQuery();
454
+ // This is the query generated by the ALASQL dialect
455
+ libFable.log.trace('Count Query', tmpQuery.query);
456
+ Expect(tmpQuery.query.body)
457
+ .to.equal('SELECT COUNT(DISTINCT IDAnimal) AS RowCount FROM Animal WHERE `Deleted` = :Deleted_w0;');
458
+ }
459
+ );
423
460
  }
424
461
  );
425
462
 
@@ -352,7 +352,7 @@ suite
352
352
  // This is the query generated by the MySQL dialect
353
353
  libFable.log.trace('Count Distinct Query', tmpQuery.query);
354
354
  Expect(tmpQuery.query.body)
355
- .to.equal('SELECT COUNT(DISTINCT `Animal`.`IDAnimal`, `Animal`.`GUIDAnimal`, `Animal`.`CreateDate`, `Animal`.`CreatingIDUser`, `Animal`.`UpdateDate`, `Animal`.`UpdatingIDUser`, `Animal`.`Deleted`, `Animal`.`DeletingIDUser`, `Animal`.`DeleteDate`) AS RowCount FROM `Animal` WHERE `Animal`.Deleted = :Deleted_w0;');
355
+ .to.equal('SELECT COUNT(DISTINCT `Animal`.`IDAnimal`) AS RowCount FROM `Animal` WHERE `Animal`.Deleted = :Deleted_w0;');
356
356
  }
357
357
  );
358
358
  test