Skip to content

Commit

Permalink
validate the usage of constant values (#99)
Browse files Browse the repository at this point in the history
* validate the usage of constant values

* 2.5.23
  • Loading branch information
kbarbounakis authored Sep 3, 2024
1 parent c2914b9 commit b4f123e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 23 deletions.
6 changes: 5 additions & 1 deletion formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ SqlFormatter.prototype.isLogical = function(obj) {
*/
SqlFormatter.prototype.escape = function(value,unquoted)
{
if (_.isNil(value))
if (value == null)
return SqlUtils.escape(null);

if (typeof value === 'object')
Expand All @@ -155,6 +155,10 @@ SqlFormatter.prototype.escape = function(value,unquoted)
return SqlUtils.escape(value);
if (Object.prototype.hasOwnProperty.call(value, '$name')) {
return this.escapeName(value.$name);
} else if (Object.prototype.hasOwnProperty.call(value, '$value')) {
return this.escape(value.$value);
} else if (Object.prototype.hasOwnProperty.call(value, '$literal')) {
return this.escape(value.$literal);
} else {
//check if value is a known expression e.g. { $length:"name" }
var keys = _.keys(value),
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/query",
"version": "2.5.22",
"version": "2.5.23",
"description": "MOST Web Framework Codename Blueshift - Query Module",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 11 additions & 5 deletions query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1723,8 +1723,14 @@ QueryField.fieldNameExpression = /^[A-Za-z_0-9]+$/;
QueryField.prototype.from = function(entity)
{
var name;
if (typeof entity !== 'string')
var fromEntity;
if (entity instanceof QueryEntity) {
fromEntity = entity.$as || entity.name;
} else if (typeof entity === 'string') {
fromEntity = entity;
} else {
throw new Error('Invalid argument. Expected string');
}
//get property
if (!_.isNil(this.$name))
{
Expand All @@ -1733,10 +1739,10 @@ QueryField.prototype.from = function(entity)
name = this.$name;
if (QueryField.fieldNameExpression.test(name))
//if not append entity name
this.$name = entity.concat('.', name);
this.$name = fromEntity.concat('.', name);
else
//split field name and add entity
this.$name = entity.concat('.', name.split('.')[1]);
this.$name = fromEntity.concat('.', name.split('.')[1]);
}
else
throw new Error("Invalid field definition.");
Expand All @@ -1755,10 +1761,10 @@ QueryField.prototype.from = function(entity)
name = expr[aggregate];
if (QueryField.fieldNameExpression.test(name))
//if not append entity name
expr[aggregate] = entity.concat('.', name);
expr[aggregate] = fromEntity.concat('.', name);
else
//split field name and add entity
expr[aggregate] = entity.concat('.', name.split('.')[1]);
expr[aggregate] = fromEntity.concat('.', name.split('.')[1]);
}
return this;
};
Expand Down
83 changes: 83 additions & 0 deletions spec/SqlFormatter.select.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// eslint-disable-next-line no-unused-vars
import {QueryEntity, QueryExpression, QueryField} from '../index';
import { QueryValueRef } from '../query';
import {MemoryAdapter} from './test/TestMemoryAdapter';
import Ajv from 'ajv';

// eslint-disable-next-line no-unused-vars
describe('SqlFormatter', () => {

/**
* @type {MemoryAdapter}
*/
let db;
beforeAll(() => {
db = new MemoryAdapter({
name: 'local',
database: './spec/db/local.db'
});
});
afterAll(async () => {
if (db) {
await db.closeAsync();
}
})
it('should format field expression', async () => {
const Products = new QueryEntity('ProductData');
const q = new QueryExpression().select(
new QueryField('id').from(Products),
new QueryField('name').from(Products)
).from(Products).take(25);
const items = await db.executeAsync(q);
expect(items.length).toBeTruthy();

const schema = {
type: 'object',
properties: {
id: {type: 'number'},
name: {type: 'string'}
},
required: ['id', 'name'],
additionalProperties: false
};

const validate = new Ajv().compile(schema);
items.forEach((item) => expect(validate(item)).toBeTruthy());
});

it('should format field with method', async () => {
const Products = new QueryEntity('ProductData');
const q = new QueryExpression().select(
new QueryField('id').from(Products),
new QueryField({
description: {
$concat: [
new QueryField('model').from(Products),
' ',
new QueryField('name').from(Products),
]
}
})
).from(Products).take(25);
const items = await db.executeAsync(q);
expect(items.length).toBeTruthy();
items.forEach((item) => expect(item.description).toBeTruthy());
});

it('should format field with constant', async () => {
const Products = new QueryEntity('ProductData');
const q = new QueryExpression().select(
new QueryField('id').from(Products),
new QueryField({
status: {
$value: 'active'
}
})
).from(Products).take(25);
const items = await db.executeAsync(q);
expect(items.length).toBeTruthy();
items.forEach((item) => expect(item.status).toBeTruthy());
});


});

0 comments on commit b4f123e

Please sign in to comment.