├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── lib ├── builder.js ├── docs.js ├── helper.js ├── languages │ ├── Ansi.js │ ├── MariaDB.js │ ├── MySQL.js │ ├── Oracle.js │ ├── PostgreSQL.js │ ├── SQLServer.js │ └── SQLite.js ├── operator.js ├── predefined.js ├── reserved-words.js ├── syntax.js ├── test.js └── tools.js ├── package-lock.json ├── package.json ├── quick-runs ├── OracleWhere.js └── ssFetch.js ├── sql ├── helpers │ ├── aggregation │ │ ├── README.md │ │ ├── aggregationHelper.js │ │ ├── avg │ │ │ ├── README.md │ │ │ └── avg.js │ │ ├── count │ │ │ ├── README.md │ │ │ └── count.js │ │ ├── max │ │ │ ├── README.md │ │ │ └── max.js │ │ ├── min │ │ │ ├── README.md │ │ │ └── min.js │ │ └── sum │ │ │ ├── README.md │ │ │ └── sum.js │ ├── arithmetic │ │ ├── .arithmeticHelper.js │ │ ├── add │ │ │ ├── README.md │ │ │ └── add.js │ │ ├── div │ │ │ ├── README.md │ │ │ └── div.js │ │ └── mul │ │ │ ├── README.md │ │ │ └── mul.js │ ├── comparision │ │ ├── between │ │ │ ├── README.md │ │ │ └── between.js │ │ ├── contains │ │ │ ├── README.md │ │ │ └── contains.js │ │ ├── endsWith │ │ │ ├── README.md │ │ │ └── endsWith.js │ │ ├── eq │ │ │ ├── README.md │ │ │ └── eq.js │ │ ├── gt │ │ │ ├── README.md │ │ │ └── gt.js │ │ ├── gte │ │ │ ├── README.md │ │ │ └── gte.js │ │ ├── in │ │ │ ├── README.md │ │ │ └── in.js │ │ ├── like │ │ │ ├── README.md │ │ │ └── like.js │ │ ├── lt │ │ │ ├── README.md │ │ │ └── lt.js │ │ ├── lte │ │ │ ├── README.md │ │ │ └── lte.js │ │ ├── ne │ │ │ ├── README.md │ │ │ └── ne.js │ │ ├── nin │ │ │ ├── README.md │ │ │ └── nin.js │ │ └── startsWith │ │ │ ├── README.md │ │ │ └── startsWith.js │ ├── ddl │ │ ├── column │ │ │ ├── README.md │ │ │ ├── column.js │ │ │ └── private │ │ │ │ ├── default │ │ │ │ ├── README.md │ │ │ │ └── default.js │ │ │ │ ├── size │ │ │ │ ├── README.md │ │ │ │ └── size.js │ │ │ │ └── type │ │ │ │ ├── README.md │ │ │ │ └── type.js │ │ ├── columns │ │ │ ├── README.md │ │ │ └── columns.js │ │ └── constraint │ │ │ ├── README.md │ │ │ ├── check │ │ │ ├── README.md │ │ │ └── check.js │ │ │ ├── constraint.js │ │ │ ├── foreignKey │ │ │ ├── README.md │ │ │ └── foreignKey.js │ │ │ ├── primary │ │ │ ├── README.md │ │ │ └── primary.js │ │ │ ├── references │ │ │ ├── README.md │ │ │ ├── private │ │ │ │ ├── match │ │ │ │ │ ├── README.md │ │ │ │ │ └── match.js │ │ │ │ ├── onDelete │ │ │ │ │ ├── README.md │ │ │ │ │ └── onDelete.js │ │ │ │ └── onUpdate │ │ │ │ │ ├── README.md │ │ │ │ │ └── onUpdate.js │ │ │ └── references.js │ │ │ └── unique │ │ │ ├── README.md │ │ │ └── unique.js │ ├── functions │ │ ├── cast │ │ │ ├── README.md │ │ │ └── cast.js │ │ ├── comparision │ │ │ ├── cmp │ │ │ │ ├── README.md │ │ │ │ ├── cmp.js │ │ │ │ └── private │ │ │ │ │ └── comparator │ │ │ │ │ ├── README.md │ │ │ │ │ └── comparator.js │ │ │ └── coalesce │ │ │ │ ├── README.md │ │ │ │ └── coalesce.js │ │ ├── datetime │ │ │ ├── date │ │ │ │ ├── README.md │ │ │ │ └── date.js │ │ │ ├── now │ │ │ │ ├── README.md │ │ │ │ └── now.js │ │ │ └── to_timestamp │ │ │ │ ├── README.md │ │ │ │ └── to_timestamp.js │ │ ├── json │ │ │ ├── PostgreSQL │ │ │ │ ├── jsonAgg │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonAgg.js │ │ │ │ ├── jsonBuildObject │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonBuildObject.js │ │ │ │ ├── jsonEach │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonEach.js │ │ │ │ ├── jsonObjectAgg │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonObjectAgg.js │ │ │ │ ├── jsonbAgg │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonbAgg.js │ │ │ │ ├── jsonbBuildObject │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonbBuildObject.js │ │ │ │ ├── jsonbEach │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonbEach.js │ │ │ │ ├── jsonbObjectAgg │ │ │ │ │ ├── README.md │ │ │ │ │ └── jsonbObjectAgg.js │ │ │ │ ├── jsonbSet │ │ │ │ │ ├── README.md │ │ │ │ │ ├── jsonbSet.js │ │ │ │ │ └── private │ │ │ │ │ │ ├── target │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ └── target.js │ │ │ │ │ │ └── value │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ └── value.js │ │ │ │ └── rowToJson │ │ │ │ │ ├── README.md │ │ │ │ │ └── rowToJson.js │ │ │ ├── README.md │ │ │ └── json.js │ │ ├── misc │ │ │ └── isnull │ │ │ │ ├── README.md │ │ │ │ └── isnull.js │ │ ├── string │ │ │ ├── concat │ │ │ │ ├── README.md │ │ │ │ └── concat.js │ │ │ └── left │ │ │ │ ├── README.md │ │ │ │ └── left.js │ │ └── systeminformations │ │ │ ├── current_database │ │ │ ├── README.md │ │ │ └── current_database.js │ │ │ ├── current_schema │ │ │ ├── README.md │ │ │ └── current_schema.js │ │ │ └── current_user │ │ │ ├── README.md │ │ │ └── current_user.js │ ├── logical │ │ ├── and │ │ │ ├── README.md │ │ │ └── and.js │ │ ├── not │ │ │ ├── README.md │ │ │ └── not.js │ │ └── or │ │ │ ├── README.md │ │ │ └── or.js │ ├── misc │ │ ├── $ │ │ │ ├── $.js │ │ │ └── README.md │ │ ├── __ │ │ │ ├── README.md │ │ │ └── __.js │ │ ├── anyExpr │ │ │ ├── README.md │ │ │ └── anyExpr.js │ │ └── i │ │ │ ├── README.md │ │ │ └── i.js │ └── queries │ │ ├── excluded │ │ ├── README.md │ │ └── excluded.js │ │ ├── from │ │ ├── README.md │ │ └── from.js │ │ ├── join │ │ ├── .joinhelper.js │ │ ├── README.md │ │ ├── crossJoin │ │ │ ├── README.md │ │ │ └── crossJoin.js │ │ ├── fullOuterJoin │ │ │ ├── README.md │ │ │ └── fullOuterJoin.js │ │ ├── innerJoin │ │ │ ├── README.md │ │ │ └── innerJoin.js │ │ ├── join.js │ │ ├── leftJoin │ │ │ ├── README.md │ │ │ └── leftJoin.js │ │ ├── private │ │ │ ├── .joinHelper.js │ │ │ ├── cross │ │ │ │ ├── README.md │ │ │ │ └── cross.js │ │ │ ├── full │ │ │ │ ├── README.md │ │ │ │ └── full.js │ │ │ ├── inner │ │ │ │ ├── README.md │ │ │ │ └── inner.js │ │ │ ├── lateral │ │ │ │ ├── README.md │ │ │ │ └── lateral.js │ │ │ ├── left │ │ │ │ ├── README.md │ │ │ │ └── left.js │ │ │ ├── on │ │ │ │ ├── README.md │ │ │ │ └── on.js │ │ │ └── right │ │ │ │ ├── README.md │ │ │ │ └── right.js │ │ └── rightJoin │ │ │ ├── README.md │ │ │ └── rightJoin.js │ │ ├── limit │ │ ├── README.md │ │ └── limit.js │ │ ├── offset │ │ ├── README.md │ │ └── offset.js │ │ ├── orFetch │ │ ├── README.md │ │ └── orFetch.js │ │ ├── orOffset │ │ ├── README.md │ │ └── orOffset.js │ │ ├── orderBy │ │ ├── README.md │ │ ├── orderBy.js │ │ └── private │ │ │ ├── asc │ │ │ ├── README.md │ │ │ └── asc.js │ │ │ ├── desc │ │ │ ├── README.md │ │ │ └── desc.js │ │ │ ├── nullsFirst │ │ │ ├── README.md │ │ │ └── nullsFirst.js │ │ │ └── nullsLast │ │ │ ├── README.md │ │ │ └── nullsLast.js │ │ ├── returning │ │ ├── README.md │ │ ├── private │ │ │ ├── columns │ │ │ │ ├── README.md │ │ │ │ └── columns.js │ │ │ └── into │ │ │ │ ├── README.md │ │ │ │ └── into.js │ │ └── returning.js │ │ ├── ssFetch │ │ ├── README.md │ │ └── ssFetch.js │ │ ├── tableFunction │ │ ├── README.md │ │ └── tableFunction.js │ │ ├── top │ │ ├── README.md │ │ └── top.js │ │ ├── using │ │ ├── README.md │ │ └── using.js │ │ └── where │ │ ├── README.md │ │ └── where.js ├── keywords │ └── README.md └── operators │ ├── createIndex │ ├── README.md │ ├── createIndex.js │ └── private │ │ ├── columns │ │ ├── README.md │ │ ├── columns.js │ │ └── private │ │ │ ├── asc │ │ │ ├── README.md │ │ │ └── asc.js │ │ │ ├── desc │ │ │ ├── README.md │ │ │ └── desc.js │ │ │ ├── nullsFirst │ │ │ ├── README.md │ │ │ └── nullsFirst.js │ │ │ └── nullsLast │ │ │ ├── README.md │ │ │ └── nullsLast.js │ │ ├── storageParameters │ │ ├── README.md │ │ └── storageParameters.js │ │ └── using │ │ ├── README.md │ │ └── using.js │ ├── createTable │ ├── README.md │ ├── createTable.js │ └── private │ │ ├── define │ │ ├── README.md │ │ └── define.js │ │ ├── options │ │ ├── README.md │ │ └── options.js │ │ └── tableOptions │ │ ├── README.md │ │ └── tableOptions.js │ ├── createView │ ├── README.md │ ├── createView.js │ └── private │ │ └── checkOption │ │ ├── README.md │ │ └── checkOption.js │ ├── delete │ ├── README.md │ └── delete.js │ ├── except │ ├── README.md │ ├── except.js │ └── private │ │ ├── all │ │ ├── README.md │ │ └── all.js │ │ └── distinct │ │ ├── README.md │ │ └── distinct.js │ ├── insert │ ├── README.md │ ├── insert.js │ └── private │ │ ├── columns │ │ ├── README.md │ │ └── columns.js │ │ ├── documents │ │ ├── README.md │ │ └── documents.js │ │ ├── onConflict │ │ ├── README.md │ │ ├── onConflict.js │ │ └── private │ │ │ └── columns │ │ │ ├── README.md │ │ │ └── columns.js │ │ ├── onDuplicateKeyUpdate │ │ ├── README.md │ │ ├── onDuplicateKeyUpdate.js │ │ └── private │ │ │ └── values │ │ │ ├── README.md │ │ │ └── values.js │ │ ├── records │ │ ├── README.md │ │ └── records.js │ │ └── values │ │ ├── README.md │ │ └── values.js │ ├── intersect │ ├── README.md │ ├── intersect.js │ └── private │ │ ├── all │ │ ├── README.md │ │ └── all.js │ │ └── distinct │ │ ├── README.md │ │ └── distinct.js │ ├── select │ ├── README.md │ ├── private │ │ ├── columns │ │ │ ├── README.md │ │ │ └── columns.js │ │ ├── groupBy │ │ │ ├── README.md │ │ │ ├── groupBy.js │ │ │ └── private │ │ │ │ ├── .rollupHelper.js │ │ │ │ ├── cube │ │ │ │ ├── README.md │ │ │ │ └── cube.js │ │ │ │ ├── groupingSets │ │ │ │ ├── README.md │ │ │ │ └── groupingSets.js │ │ │ │ └── rollup │ │ │ │ ├── README.md │ │ │ │ └── rollup.js │ │ ├── having │ │ │ ├── README.md │ │ │ └── having.js │ │ ├── into │ │ │ ├── README.md │ │ │ ├── into.js │ │ │ └── private │ │ │ │ ├── dumpfile │ │ │ │ ├── README.md │ │ │ │ └── dumpfile.js │ │ │ │ └── outfile │ │ │ │ ├── README.md │ │ │ │ └── outfile.js │ │ └── join │ │ │ ├── README.md │ │ │ ├── join.js │ │ │ └── private │ │ │ ├── .joinHelper.js │ │ │ ├── cross │ │ │ ├── README.md │ │ │ └── cross.js │ │ │ ├── full │ │ │ ├── README.md │ │ │ └── full.js │ │ │ ├── inner │ │ │ ├── README.md │ │ │ └── inner.js │ │ │ ├── lateral │ │ │ ├── README.md │ │ │ └── lateral.js │ │ │ ├── left │ │ │ ├── README.md │ │ │ └── left.js │ │ │ ├── on │ │ │ ├── README.md │ │ │ └── on.js │ │ │ ├── right │ │ │ ├── README.md │ │ │ └── right.js │ │ │ └── using │ │ │ ├── README.md │ │ │ └── using.js │ └── select.js │ ├── union │ ├── README.md │ ├── private │ │ ├── all │ │ │ ├── README.md │ │ │ └── all.js │ │ └── distinct │ │ │ ├── README.md │ │ │ └── distinct.js │ └── union.js │ ├── update │ ├── README.md │ ├── private │ │ └── set │ │ │ ├── README.md │ │ │ └── set.js │ └── update.js │ └── with │ ├── README.md │ ├── private │ ├── cte │ │ ├── README.md │ │ ├── cte.js │ │ └── private │ │ │ └── columns │ │ │ ├── README.md │ │ │ └── columns.js │ └── query │ │ ├── README.md │ │ └── query.js │ └── with.js ├── tests └── internal-tests.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2018 planetarydev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const SQLBuilder = require('./lib/builder'); 4 | global.SQLBuilder = SQLBuilder; 5 | 6 | module.exports = SQLBuilder; 7 | -------------------------------------------------------------------------------- /lib/helper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const SQLOperator = require('./operator'); 4 | 5 | class SQLHelper extends SQLOperator { 6 | constructor(sqlBuilderInstance){ 7 | super(sqlBuilderInstance); 8 | 9 | this.__is__ = 'helper'; 10 | } 11 | } 12 | 13 | module.exports = SQLHelper; 14 | -------------------------------------------------------------------------------- /lib/languages/Ansi.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(sql) { 4 | /* 5 | // Add ANSI operators 6 | sql.registerOperator('select'); 7 | 8 | // Add ANSI logical helpers 9 | sql.registerHelper('/logical/and'); 10 | 11 | // Add ANSI comparision helpers 12 | sql.registerHelper('/comparision/eq'); 13 | 14 | // Add ANSI aggregation helpers 15 | sql.registerHelper('/aggregation/count'); 16 | 17 | // Add ANSI functions 18 | sql.registerHelper('/functions/comparision/cmp'); 19 | 20 | // Add ANSI misc helpers 21 | sql.registerHelper('/functions/misc/isnull'); 22 | */ 23 | } 24 | -------------------------------------------------------------------------------- /lib/languages/MariaDB.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(sql) { 4 | sql.setLanguage('MariaDB'); 5 | 6 | sql.setQuoteChar('`'); 7 | 8 | sql.placeholder = function() { 9 | return '?'; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /lib/languages/MySQL.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(sql) { 4 | sql.setLanguage('MySQL'); 5 | 6 | sql.setQuoteChar('`'); 7 | 8 | sql.placeholder = function() { 9 | return '?'; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /lib/languages/Oracle.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | 4 | 5 | module.exports = function(sql) { 6 | sql.setLanguage('Oracle'); 7 | 8 | sql.setQuoteChar('"'); 9 | 10 | sql.placeholder = function() { 11 | return ':param' + sql._values.length; 12 | } 13 | 14 | sql.transformValueResult = function(valuesAsArray) { 15 | // let resultAsObj = {} 16 | // sql.forEach(valuesAsArray, (value, index) => { 17 | // resultAsObj['param' + index] = { val: value } 18 | // }); 19 | // return resultAsObj; 20 | // Oracle Nodejs client needs array of values, so no need to convert into object, so commented above code. 21 | return valuesAsArray; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /lib/languages/PostgreSQL.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(sql) { 4 | sql.setLanguage('PostgreSQL'); 5 | 6 | sql.setQuoteChar('"'); 7 | 8 | sql.placeholder = function() { 9 | return '$' + sql._values.length; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /lib/languages/SQLServer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(sql) { 4 | sql.setLanguage('SQLServer'); 5 | 6 | sql.setQuoteChar('[', ']'); 7 | 8 | sql.placeholder = function() { 9 | return '@param' + sql._values.length; 10 | } 11 | 12 | sql.transformValueResult = function(valuesAsArray) { 13 | let resultAsObj = {} 14 | sql.forEach(valuesAsArray, (value, index) => { 15 | resultAsObj['param' + (index + 1)] = value 16 | }); 17 | return resultAsObj; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lib/languages/SQLite.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(sql) { 4 | sql.setLanguage('SQLite'); 5 | 6 | sql.setQuoteChar('"'); 7 | 8 | sql.placeholder = function() { 9 | return '?'; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /lib/tools.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | 6 | /** 7 | * walk 8 | * 9 | * Returns all files from the given directory in 10 | * a recursive way as array. 11 | * 12 | * @param dir {String} Specifies the directory 13 | * @return files {Array} All files found recursivly 14 | */ 15 | let walk = function(dir) { 16 | var results = []; 17 | var list = fs.readdirSync(dir); 18 | list.forEach(function(file) { 19 | file = path.join(dir, file); 20 | var stat = fs.statSync(file); 21 | if (stat && stat.isDirectory()){ 22 | results = results.concat(walk(file)); 23 | } else { 24 | results.push(file) 25 | } 26 | }); 27 | return results; 28 | } 29 | module.exports.walk = walk; 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-sql-builder2", 3 | "version": "1.0.23", 4 | "description": "Query Builder to translate JSON dataformat a SQL.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node lib/test.js" 8 | }, 9 | "keywords": [ 10 | "database", 11 | "sql", 12 | "mysql", 13 | "pg", 14 | "postgres", 15 | "PostgreSQL", 16 | "mariadb", 17 | "maria", 18 | "plsql", 19 | "oracle", 20 | "mssql", 21 | "SQLServer", 22 | "T-SQL", 23 | "Transact-SQL", 24 | "mongo", 25 | "SQLite", 26 | "json", 27 | "build", 28 | "translate", 29 | "query" 30 | ], 31 | "author": "planetarydev", 32 | "license": "MIT", 33 | "dependencies": { 34 | "lodash": "^4.17.14" 35 | }, 36 | "devDependencies": { 37 | "chai": "^4.2.0", 38 | "mocha": "^6.1.4", 39 | "colors": "^1.3.3", 40 | "eslint": "^4.19.1", 41 | "js-beautify": "^1.10.0", 42 | "sql-formatter": "^2.3.2" 43 | }, 44 | "repository": { 45 | "type": "git", 46 | "url": "git+https://github.com/planetarydev/json-sql-builder2.git" 47 | }, 48 | "bugs": { 49 | "url": "https://github.com/planetarydev/json-sql-builder2/issues" 50 | }, 51 | "homepage": "https://github.com/planetarydev/json-sql-builder2#readme" 52 | } 53 | -------------------------------------------------------------------------------- /quick-runs/OracleWhere.js: -------------------------------------------------------------------------------- 1 | const SQLBuilder = require('../index'); 2 | 3 | let obj = { 4 | $from: 'MAYUR.Persons', 5 | // $where: {id: 'Mayur'} 6 | $where: {id: 1}, 7 | $orFetch: 10, 8 | $orOffset: 10, 9 | }; 10 | 11 | const sql = new SQLBuilder('Oracle', {quoteIdentifiers: true}); 12 | let output = sql['$select'](obj); 13 | 14 | console.log(output.sql); 15 | console.log(output.values); 16 | -------------------------------------------------------------------------------- /quick-runs/ssFetch.js: -------------------------------------------------------------------------------- 1 | const SQLBuilder = require('../index'); 2 | 3 | let obj = { 4 | $from: 'dbo.Products', 5 | $ssFetch: { 6 | $skip: 30, 7 | $fetch: 10, 8 | }, 9 | $orderBy: 'ProductName' 10 | }; 11 | 12 | const sql = new SQLBuilder('SQLServer'); 13 | let output = sql['$select'](obj); 14 | 15 | console.log(output.sql); 16 | console.log(output.values); 17 | -------------------------------------------------------------------------------- /sql/helpers/aggregation/avg/avg.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const aggregationHelper = require('../aggregationHelper') 4 | 5 | class avg extends aggregationHelper.definition { 6 | constructor(sql){ 7 | super(sql, 'AVG'); 8 | } 9 | } 10 | 11 | module.exports = { 12 | definition: avg, 13 | description: 'Specifies the aggregation function `AVG` as Helper.', 14 | supportedBy: { 15 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_avg', 16 | MariaDB: 'https://mariadb.com/kb/en/library/avg/', 17 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-aggregate.html', 18 | SQLite: 'https://sqlite.org/lang_aggfunc.html#avg', 19 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions011.htm', 20 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/functions/avg-transact-sql' 21 | }, 22 | examples: { 23 | String: { 24 | "Basic Usage": function(sql){ 25 | return { 26 | test: function(){ 27 | return sql.$select({ 28 | average_age: { $avg: 'age' }, 29 | $from: 'people' 30 | }); 31 | }, 32 | expectedResults: { 33 | sql: 'SELECT AVG(age) AS average_age FROM people', 34 | values:{} 35 | } 36 | } 37 | } 38 | }, 39 | Object: { 40 | "Basic Usage": function(sql){ 41 | return { 42 | test: function(){ 43 | return sql.$select({ 44 | average_age: { $avg: { $expr: '~~age', $distinct: true } }, 45 | $from: 'people' 46 | }); 47 | 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT AVG(DISTINCT age) AS average_age FROM people', 51 | values:{} 52 | } 53 | } 54 | } 55 | }, 56 | Function: { 57 | "Basic Usage": function(sql){ 58 | return { 59 | supportedBy: { 60 | SQLServer: true 61 | }, 62 | test: function(){ 63 | return sql.$select({ 64 | average_age: { $avg: sql.isNull('~~age', 40) }, 65 | $from: 'people' 66 | }); 67 | 68 | }, 69 | expectedResults: { 70 | sql: 'SELECT AVG(ISNULL(age, $1)) AS average_age FROM people', 71 | values:{ 72 | $1: 40 73 | } 74 | } 75 | } 76 | }, 77 | "Using avg callee with DISTINCT parameter": function(sql){ 78 | return { 79 | test: function(){ 80 | return sql.$select({ 81 | average_age: sql.avg(sql.DISTINCT, 'age'), 82 | $from: 'people' 83 | }); 84 | 85 | }, 86 | expectedResults: { 87 | sql: 'SELECT AVG(DISTINCT age) AS average_age FROM people', 88 | values:{} 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /sql/helpers/aggregation/count/count.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const aggregationHelper = require('../aggregationHelper') 4 | 5 | class count extends aggregationHelper.definition { 6 | constructor(sql){ 7 | super(sql, 'COUNT'); 8 | } 9 | } 10 | 11 | module.exports = { 12 | definition: count, 13 | description: 'Specifies the aggregation function `COUNT` as Helper.', 14 | supportedBy: { 15 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_count', 16 | MariaDB: 'https://mariadb.com/kb/en/library/count/', 17 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-aggregate.html', 18 | SQLite: 'https://sqlite.org/lang_aggfunc.html#count', 19 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm', 20 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/functions/count-transact-sql' 21 | }, 22 | examples: { 23 | String: { 24 | "Basic Usage": function(sql){ 25 | return { 26 | test: function(){ 27 | return sql.$select({ 28 | total: { $count: '*' }, 29 | $from: 'people', 30 | $where: { 31 | age: 18 32 | } 33 | }); 34 | }, 35 | expectedResults: { 36 | sql: 'SELECT COUNT(*) AS total FROM people WHERE age = $1', 37 | values:{ 38 | $1: 18 39 | } 40 | } 41 | } 42 | } 43 | }, 44 | Object: { 45 | "Basic Usage": function(sql){ 46 | return { 47 | test: function(){ 48 | return sql.$select({ 49 | total_age: { $count: { $expr: '~~age', $distinct: true } }, 50 | $from: 'people' 51 | }); 52 | 53 | }, 54 | expectedResults: { 55 | sql: 'SELECT COUNT(DISTINCT age) AS total_age FROM people', 56 | values:{} 57 | } 58 | } 59 | } 60 | }, 61 | Function: { 62 | "Basic Usage": function(sql){ 63 | return { 64 | supportedBy: { 65 | SQLServer: true 66 | }, 67 | test: function(){ 68 | return sql.$select({ 69 | total_age: { $count: sql.isNull('~~age', 40) }, 70 | $from: 'people' 71 | }); 72 | 73 | }, 74 | expectedResults: { 75 | sql: 'SELECT COUNT(ISNULL(age, $1)) AS total_age FROM people', 76 | values:{ 77 | $1: 40 78 | } 79 | } 80 | } 81 | }, 82 | "Using count callee with DISTINCT parameter": function(sql){ 83 | return { 84 | test: function(){ 85 | return sql.$select({ 86 | total_age: sql.count(sql.DISTINCT, 'age'), 87 | $from: 'people' 88 | }); 89 | 90 | }, 91 | expectedResults: { 92 | sql: 'SELECT COUNT(DISTINCT age) AS total_age FROM people', 93 | values:{} 94 | } 95 | } 96 | } 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /sql/helpers/aggregation/max/max.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const aggregationHelper = require('../aggregationHelper') 4 | 5 | class max extends aggregationHelper.definition { 6 | constructor(sql){ 7 | super(sql, 'MAX'); 8 | } 9 | } 10 | 11 | module.exports = { 12 | definition: max, 13 | description: 'Specifies the aggregation function `MAX` as Helper.', 14 | supportedBy: { 15 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_max', 16 | MariaDB: 'https://mariadb.com/kb/en/library/max/', 17 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-aggregate.html', 18 | SQLite: 'https://sqlite.org/lang_aggfunc.html#max', 19 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions087.htm', 20 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/functions/max-transact-sql' 21 | }, 22 | examples: { 23 | String: { 24 | "Basic Usage": function(sql){ 25 | return { 26 | test: function(){ 27 | return sql.$select({ 28 | max_age: { $max: 'age' }, 29 | $from: 'people' 30 | }); 31 | }, 32 | expectedResults: { 33 | sql: 'SELECT MAX(age) AS max_age FROM people', 34 | values:{} 35 | } 36 | } 37 | } 38 | }, 39 | Object: { 40 | "Basic Usage": function(sql){ 41 | return { 42 | test: function(){ 43 | return sql.$select({ 44 | max_age: { $max: { $expr: '~~age', $distinct: true } }, 45 | $from: 'people' 46 | }); 47 | 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT MAX(DISTINCT age) AS max_age FROM people', 51 | values:{} 52 | } 53 | } 54 | } 55 | }, 56 | Function: { 57 | "Basic Usage": function(sql){ 58 | return { 59 | supportedBy: { 60 | SQLServer: true 61 | }, 62 | test: function(){ 63 | return sql.$select({ 64 | max_age: { $max: sql.isNull('~~age', 40) }, 65 | $from: 'people' 66 | }); 67 | 68 | }, 69 | expectedResults: { 70 | sql: 'SELECT MAX(ISNULL(age, $1)) AS max_age FROM people', 71 | values:{ 72 | $1: 40 73 | } 74 | } 75 | } 76 | }, 77 | "Using max callee with DISTINCT parameter": function(sql){ 78 | return { 79 | test: function(){ 80 | return sql.$select({ 81 | max_age: sql.max(sql.DISTINCT, 'age'), 82 | $from: 'people' 83 | }); 84 | 85 | }, 86 | expectedResults: { 87 | sql: 'SELECT MAX(DISTINCT age) AS max_age FROM people', 88 | values:{} 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /sql/helpers/aggregation/min/min.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const aggregationHelper = require('../aggregationHelper') 4 | 5 | class min extends aggregationHelper.definition { 6 | constructor(sql){ 7 | super(sql, 'MIN'); 8 | } 9 | } 10 | 11 | module.exports = { 12 | definition: min, 13 | description: 'Specifies the aggregation function `MIN` as Helper.', 14 | supportedBy: { 15 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_min', 16 | MariaDB: 'https://mariadb.com/kb/en/library/min/', 17 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-aggregate.html', 18 | SQLite: 'https://sqlite.org/lang_aggfunc.html#min', 19 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions087.htm', 20 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/functions/min-transact-sql' 21 | }, 22 | examples: { 23 | String: { 24 | "Basic Usage": function(sql){ 25 | return { 26 | test: function(){ 27 | return sql.$select({ 28 | min_age: { $min: 'age' }, 29 | $from: 'people' 30 | }); 31 | }, 32 | expectedResults: { 33 | sql: 'SELECT MIN(age) AS min_age FROM people', 34 | values:{} 35 | } 36 | } 37 | } 38 | }, 39 | Object: { 40 | "Basic Usage": function(sql){ 41 | return { 42 | test: function(){ 43 | return sql.$select({ 44 | min_age: { $min: { $expr: '~~age', $distinct: true } }, 45 | $from: 'people' 46 | }); 47 | 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT MIN(DISTINCT age) AS min_age FROM people', 51 | values:{} 52 | } 53 | } 54 | } 55 | }, 56 | Function: { 57 | "Basic Usage": function(sql){ 58 | return { 59 | supportedBy: { 60 | SQLServer: true 61 | }, 62 | test: function(){ 63 | return sql.$select({ 64 | min_age: { $min: sql.isNull('~~age', 40) }, 65 | $from: 'people' 66 | }); 67 | 68 | }, 69 | expectedResults: { 70 | sql: 'SELECT MIN(ISNULL(age, $1)) AS min_age FROM people', 71 | values:{ 72 | $1: 40 73 | } 74 | } 75 | } 76 | }, 77 | "Using min callee with DISTINCT parameter": function(sql){ 78 | return { 79 | test: function(){ 80 | return sql.$select({ 81 | min_age: sql.min(sql.DISTINCT, 'age'), 82 | $from: 'people' 83 | }); 84 | 85 | }, 86 | expectedResults: { 87 | sql: 'SELECT MIN(DISTINCT age) AS min_age FROM people', 88 | values:{} 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /sql/helpers/aggregation/sum/sum.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const aggregationHelper = require('../aggregationHelper') 4 | 5 | class sum extends aggregationHelper.definition { 6 | constructor(sql){ 7 | super(sql, 'SUM'); 8 | } 9 | } 10 | 11 | module.exports = { 12 | definition: sum, 13 | description: 'Specifies the aggregation function `SUM` as Helper.', 14 | supportedBy: { 15 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_sum', 16 | MariaDB: 'https://mariadb.com/kb/en/library/sum/', 17 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-aggregate.html', 18 | SQLite: 'https://sqlite.org/lang_aggfunc.html#sum', 19 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions087.htm', 20 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/functions/sum-transact-sql' 21 | }, 22 | examples: { 23 | String: { 24 | "Basic Usage": function(sql){ 25 | return { 26 | test: function(){ 27 | return sql.$select({ 28 | sum_age: { $sum: 'age' }, 29 | $from: 'people' 30 | }); 31 | }, 32 | expectedResults: { 33 | sql: 'SELECT SUM(age) AS sum_age FROM people', 34 | values:{} 35 | } 36 | } 37 | } 38 | }, 39 | Object: { 40 | "Basic Usage": function(sql){ 41 | return { 42 | test: function(){ 43 | return sql.$select({ 44 | sum_age: { $sum: { $expr: '~~age', $distinct: true } }, 45 | $from: 'people' 46 | }); 47 | 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT SUM(DISTINCT age) AS sum_age FROM people', 51 | values:{} 52 | } 53 | } 54 | } 55 | }, 56 | Function: { 57 | "Basic Usage": function(sql){ 58 | return { 59 | supportedBy: { 60 | SQLServer: true 61 | }, 62 | test: function(){ 63 | return sql.$select({ 64 | sum_age: { $sum: sql.isNull('~~age', 40) }, 65 | $from: 'people' 66 | }); 67 | 68 | }, 69 | expectedResults: { 70 | sql: 'SELECT SUM(ISNULL(age, $1)) AS sum_age FROM people', 71 | values:{ 72 | $1: 40 73 | } 74 | } 75 | } 76 | }, 77 | "Using sum callee with DISTINCT parameter": function(sql){ 78 | return { 79 | test: function(){ 80 | return sql.$select({ 81 | sum_age: sql.sum(sql.DISTINCT, 'age'), 82 | $from: 'people' 83 | }); 84 | 85 | }, 86 | expectedResults: { 87 | sql: 'SELECT SUM(DISTINCT age) AS sum_age FROM people', 88 | values:{} 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /sql/helpers/arithmetic/.arithmeticHelper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class ArithmeticHelper extends SQLBuilder.SQLHelper { 4 | constructor(sql, helperName, arithmeticOperator){ 5 | super(sql); 6 | 7 | this.__name__ = helperName; 8 | 9 | this.Types({ 10 | Number: { syntax: this.Syntax(`${arithmeticOperator} `) }, 11 | String: { syntax: this.Syntax(`${arithmeticOperator} `) }, 12 | Object: { syntax: this.Syntax(`${arithmeticOperator} `) }, 13 | Function: { syntax: this.Syntax(`${arithmeticOperator} `) }, 14 | Array: { 15 | eachItemOf: { 16 | Number: { syntax: this.Syntax(`[ ${arithmeticOperator} ... ]`) }, 17 | String: { syntax: this.Syntax(`[ ${arithmeticOperator} ... ]`) }, 18 | Object: { syntax: this.Syntax(`[ ${arithmeticOperator} ... ]`) }, 19 | Function: { syntax: this.Syntax(`[ ${arithmeticOperator} ... ]`) } 20 | } 21 | } 22 | }); 23 | 24 | this.callee = function(...args) { 25 | let identifier = args.pop(); 26 | return this._callHelper('$' + helperName, args, identifier); 27 | } 28 | } 29 | 30 | preBuild(query, identifier){ 31 | // add a shortcut for UPDATE ... SET mycol = mycol + 1 32 | // --> { 33 | // $update: { 34 | // $set: { 35 | // mycol: { $add: 1 } 36 | // } 37 | // } 38 | // } 39 | // 40 | // we turn this into an array notation immediatly before building the result: 41 | // $update: { 42 | // $set: { 43 | // mycol: { $add: ['mycol', 1] } 44 | // } 45 | // } 46 | if ((this.isPreviousHelper('$set') || this.isPreviousHelper('$columns')) && ( 47 | this.isNumber(query) || 48 | this.isString(query) || 49 | this.isPlainObject(query) || 50 | this.isFunction(query) 51 | ) 52 | ) { 53 | query = [ identifier, query ]; 54 | } 55 | return query; 56 | } 57 | } 58 | 59 | module.exports = ArithmeticHelper; 60 | -------------------------------------------------------------------------------- /sql/helpers/comparision/between/README.md: -------------------------------------------------------------------------------- 1 | # between Helper 2 | Specifies the comparision `BETWEEN` Operator as Helper. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) 6 | - [MariaDB](https://mariadb.com/kb/en/library/between-and/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-comparison.html) 8 | - [SQLite](https://sqlite.org/lang_expr.html#between) 9 | - [Oracle](https://docs.oracle.com/cd/B28359_01/server.111/b28286/conditions011.htm#SQLRF52147) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/language-elements/between-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `between` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $between: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | BETWEEN <$min> AND <$max> 27 | ``` 28 | 29 | **Registered Helpers** 30 | 31 | Name|Required|Public|SQL-Definition|Supported by 32 | :---|:------:|:----:|:-------------|:----------- 33 | [min](../../../helpers/aggregation/min/)|:heavy_check_mark:|:heavy_check_mark:|| 34 | [max](../../../helpers/aggregation/max/)|:heavy_check_mark:|:heavy_check_mark:|| 35 | 36 | :bulb: **Example:** 37 | ```javascript 38 | function() { 39 | return sql.build({ 40 | $select: { 41 | $from: 'people', 42 | $where: { 43 | age: { $between: { $min: 18, $max: 45 } } 44 | } 45 | } 46 | }); 47 | } 48 | 49 | // SQL output 50 | SELECT 51 | * 52 | FROM 53 | people 54 | WHERE 55 | age BETWEEN $1 56 | AND $2 57 | 58 | // Values 59 | { 60 | "$1": 18, 61 | "$2": 45 62 | } 63 | ``` 64 | 65 | ## Further Examples 66 | 67 | :bulb: **Usage as SQL-Function** 68 | ```javascript 69 | function() { 70 | return sql.build({ 71 | $select: { 72 | $from: 'people', 73 | $where: { 74 | age: sql.between(18, 45) 75 | } 76 | } 77 | }); 78 | } 79 | 80 | // SQL output 81 | SELECT 82 | * 83 | FROM 84 | people 85 | WHERE 86 | age BETWEEN $1 87 | AND $2 88 | 89 | // Values 90 | { 91 | "$1": 18, 92 | "$2": 45 93 | } 94 | ``` 95 | 96 | -------------------------------------------------------------------------------- /sql/helpers/comparision/between/between.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class between extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { syntax: this.Syntax('BETWEEN <$min> AND <$max>', SQLBuilder.CALLEE) } 9 | }); 10 | 11 | // add the min, max expressions as private helpers 12 | // so they can't run in conflict with the public 13 | // aggregation $min and $max 14 | this.$min = new SQLBuilder.SQLPredefined.Expression(sql); 15 | this.$max = new SQLBuilder.SQLPredefined.Expression(sql); 16 | } 17 | } 18 | 19 | module.exports = { 20 | definition: between, 21 | description: 'Specifies the comparision `BETWEEN` Operator as Helper.', 22 | supportedBy: { 23 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between', 24 | MariaDB: 'https://mariadb.com/kb/en/library/between-and/', 25 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-comparison.html', 26 | SQLite: 'https://sqlite.org/lang_expr.html#between', 27 | Oracle: 'https://docs.oracle.com/cd/B28359_01/server.111/b28286/conditions011.htm#SQLRF52147', 28 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/between-transact-sql' 29 | }, 30 | examples: { 31 | Object: { 32 | "Basic Usage": function(sql){ 33 | return { 34 | test: function(){ 35 | return sql.build({ 36 | $select: { 37 | $from: 'people', 38 | $where: { 39 | age: { $between: { $min: 18, $max: 45 } } 40 | } 41 | } 42 | }); 43 | }, 44 | expectedResults: { 45 | sql: 'SELECT * FROM people WHERE age BETWEEN $1 AND $2', 46 | values: { 47 | $1: 18, 48 | $2: 45 49 | } 50 | } 51 | } 52 | }, 53 | "Usage as SQL-Function": function(sql){ 54 | return { 55 | test: function(){ 56 | return sql.build({ 57 | $select: { 58 | $from: 'people', 59 | $where: { 60 | age: sql.between(18, 45) 61 | } 62 | } 63 | }); 64 | }, 65 | expectedResults: { 66 | sql: 'SELECT * FROM people WHERE age BETWEEN $1 AND $2', 67 | values: { 68 | $1: 18, 69 | $2: 45 70 | } 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /sql/helpers/comparision/contains/README.md: -------------------------------------------------------------------------------- 1 | # contains Helper 2 | Specifies the comparision `LIKE` Operator as Helper. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like) 6 | - [MariaDB](https://mariadb.com/kb/en/library/like/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-matching.html) 8 | - [SQLite](https://sqlite.org/lang_expr.html#like) 9 | - [Oracle](https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `contains` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $contains: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | LIKE 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | $from: 'people', 35 | $where: { 36 | first_name: { $contains: 'oe' } 37 | } 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | SELECT 44 | * 45 | FROM 46 | people 47 | WHERE 48 | first_name LIKE $1 49 | 50 | // Values 51 | { 52 | "$1": "%oe%" 53 | } 54 | ``` 55 | 56 | ## Further Examples 57 | 58 | :bulb: **Usage as SQL-Function** 59 | ```javascript 60 | function() { 61 | let averageAge = 45; 62 | 63 | return sql.build({ 64 | $select: { 65 | $from: 'people', 66 | $where: { 67 | first_name: sql.contains('oe') 68 | } 69 | } 70 | }); 71 | } 72 | 73 | // SQL output 74 | SELECT 75 | * 76 | FROM 77 | people 78 | WHERE 79 | first_name LIKE $1 80 | 81 | // Values 82 | { 83 | "$1": "%oe%" 84 | } 85 | ``` 86 | 87 | -------------------------------------------------------------------------------- /sql/helpers/comparision/contains/contains.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class contains extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('LIKE ') } 9 | }); 10 | } 11 | 12 | preBuild(query, identifier){ 13 | if (this.isString(query)) { 14 | query = '%' + query + '%'; 15 | } 16 | return query 17 | } 18 | 19 | callee(value){ 20 | if (this.isString(value)) { 21 | value = '%' + value + '%'; 22 | return 'LIKE ' + this.addValue(value); 23 | } 24 | 25 | throw new Error('Parameter value on using function contains must always be a String.'); 26 | } 27 | } 28 | 29 | module.exports = { 30 | definition: contains, 31 | description: 'Specifies the comparision `LIKE` Operator as Helper.', 32 | supportedBy: { 33 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like', 34 | MariaDB: 'https://mariadb.com/kb/en/library/like/', 35 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-matching.html', 36 | SQLite: 'https://sqlite.org/lang_expr.html#like', 37 | Oracle: 'https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm', 38 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql' 39 | }, 40 | examples: { 41 | String: { 42 | "Basic Usage": function(sql){ 43 | return { 44 | test: function(){ 45 | return sql.build({ 46 | $select: { 47 | $from: 'people', 48 | $where: { 49 | first_name: { $contains: 'oe' } 50 | } 51 | } 52 | }); 53 | }, 54 | expectedResults: { 55 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 56 | values: { 57 | $1: '%oe%' 58 | } 59 | } 60 | } 61 | }, 62 | "Usage as SQL-Function": function(sql){ 63 | return { 64 | test: function(){ 65 | let averageAge = 45; 66 | 67 | return sql.build({ 68 | $select: { 69 | $from: 'people', 70 | $where: { 71 | first_name: sql.contains('oe') 72 | } 73 | } 74 | }); 75 | }, 76 | expectedResults: { 77 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 78 | values: { 79 | $1: '%oe%' 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sql/helpers/comparision/endsWith/README.md: -------------------------------------------------------------------------------- 1 | # endsWith Helper 2 | Specifies the comparision `LIKE` Operator as Helper. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like) 6 | - [MariaDB](https://mariadb.com/kb/en/library/like/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-matching.html) 8 | - [SQLite](https://sqlite.org/lang_expr.html#like) 9 | - [Oracle](https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `endsWith` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $endsWith: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | LIKE 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | $from: 'people', 35 | $where: { 36 | first_name: { $endsWith: 'oe' } 37 | } 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | SELECT 44 | * 45 | FROM 46 | people 47 | WHERE 48 | first_name LIKE $1 49 | 50 | // Values 51 | { 52 | "$1": "%oe" 53 | } 54 | ``` 55 | 56 | ## Further Examples 57 | 58 | :bulb: **Usage as SQL-Function** 59 | ```javascript 60 | function() { 61 | let averageAge = 45; 62 | 63 | return sql.build({ 64 | $select: { 65 | $from: 'people', 66 | $where: { 67 | first_name: sql.endsWith('oe') 68 | } 69 | } 70 | }); 71 | } 72 | 73 | // SQL output 74 | SELECT 75 | * 76 | FROM 77 | people 78 | WHERE 79 | first_name LIKE $1 80 | 81 | // Values 82 | { 83 | "$1": "%oe" 84 | } 85 | ``` 86 | 87 | -------------------------------------------------------------------------------- /sql/helpers/comparision/endsWith/endsWith.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class endsWith extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('LIKE ') } 9 | }); 10 | } 11 | 12 | preBuild(query, identifier){ 13 | if (this.isString(query)) { 14 | query = '%' + query; 15 | } 16 | return query 17 | } 18 | 19 | callee(value){ 20 | if (this.isString(value)) { 21 | value = '%' + value; 22 | return 'LIKE ' + this.addValue(value); 23 | } 24 | 25 | throw new Error('Parameter value on using function endsWith must always be a String.'); 26 | } 27 | } 28 | 29 | module.exports = { 30 | definition: endsWith, 31 | description: 'Specifies the comparision `LIKE` Operator as Helper.', 32 | supportedBy: { 33 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like', 34 | MariaDB: 'https://mariadb.com/kb/en/library/like/', 35 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-matching.html', 36 | SQLite: 'https://sqlite.org/lang_expr.html#like', 37 | Oracle: 'https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm', 38 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql' 39 | }, 40 | examples: { 41 | String: { 42 | "Basic Usage": function(sql){ 43 | return { 44 | test: function(){ 45 | return sql.build({ 46 | $select: { 47 | $from: 'people', 48 | $where: { 49 | first_name: { $endsWith: 'oe' } 50 | } 51 | } 52 | }); 53 | }, 54 | expectedResults: { 55 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 56 | values: { 57 | $1: '%oe' 58 | } 59 | } 60 | } 61 | }, 62 | "Usage as SQL-Function": function(sql){ 63 | return { 64 | test: function(){ 65 | let averageAge = 45; 66 | 67 | return sql.build({ 68 | $select: { 69 | $from: 'people', 70 | $where: { 71 | first_name: sql.endsWith('oe') 72 | } 73 | } 74 | }); 75 | }, 76 | expectedResults: { 77 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 78 | values: { 79 | $1: '%oe' 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sql/helpers/comparision/like/README.md: -------------------------------------------------------------------------------- 1 | # like Helper 2 | Specifies the comparision `LIKE` Operator as Helper. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like) 6 | - [MariaDB](https://mariadb.com/kb/en/library/like/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-matching.html) 8 | - [SQLite](https://sqlite.org/lang_expr.html#like) 9 | - [Oracle](https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `like` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $like: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | LIKE 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | $from: 'people', 35 | $where: { 36 | first_name: { $like: 'J___n%' } 37 | } 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | SELECT 44 | * 45 | FROM 46 | people 47 | WHERE 48 | first_name LIKE $1 49 | 50 | // Values 51 | { 52 | "$1": "J___n%" 53 | } 54 | ``` 55 | 56 | ## Further Examples 57 | 58 | :bulb: **Usage as SQL-Function** 59 | ```javascript 60 | function() { 61 | let averageAge = 45; 62 | 63 | return sql.build({ 64 | $select: { 65 | $from: 'people', 66 | $where: { 67 | first_name: sql.like('J___n%') 68 | } 69 | } 70 | }); 71 | } 72 | 73 | // SQL output 74 | SELECT 75 | * 76 | FROM 77 | people 78 | WHERE 79 | first_name LIKE $1 80 | 81 | // Values 82 | { 83 | "$1": "J___n%" 84 | } 85 | ``` 86 | 87 | -------------------------------------------------------------------------------- /sql/helpers/comparision/like/like.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class like extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('LIKE ', SQLBuilder.CALLEE) } 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: like, 15 | description: 'Specifies the comparision `LIKE` Operator as Helper.', 16 | supportedBy: { 17 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like', 18 | MariaDB: 'https://mariadb.com/kb/en/library/like/', 19 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-matching.html', 20 | SQLite: 'https://sqlite.org/lang_expr.html#like', 21 | Oracle: 'https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm', 22 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql' 23 | }, 24 | examples: { 25 | String: { 26 | "Basic Usage": function(sql){ 27 | return { 28 | test: function(){ 29 | return sql.build({ 30 | $select: { 31 | $from: 'people', 32 | $where: { 33 | first_name: { $like: 'J___n%' } 34 | } 35 | } 36 | }); 37 | }, 38 | expectedResults: { 39 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 40 | values: { 41 | $1: 'J___n%' 42 | } 43 | } 44 | } 45 | }, 46 | "Usage as SQL-Function": function(sql){ 47 | return { 48 | test: function(){ 49 | let averageAge = 45; 50 | 51 | return sql.build({ 52 | $select: { 53 | $from: 'people', 54 | $where: { 55 | first_name: sql.like('J___n%') 56 | } 57 | } 58 | }); 59 | }, 60 | expectedResults: { 61 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 62 | values: { 63 | $1: 'J___n%' 64 | } 65 | } 66 | } 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /sql/helpers/comparision/startsWith/README.md: -------------------------------------------------------------------------------- 1 | # startsWith Helper 2 | Specifies the comparision `LIKE` Operator as Helper. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like) 6 | - [MariaDB](https://mariadb.com/kb/en/library/like/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-matching.html) 8 | - [SQLite](https://sqlite.org/lang_expr.html#like) 9 | - [Oracle](https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `startsWith` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $startsWith: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | LIKE 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | $from: 'people', 35 | $where: { 36 | first_name: { $startsWith: 'J' } 37 | } 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | SELECT 44 | * 45 | FROM 46 | people 47 | WHERE 48 | first_name LIKE $1 49 | 50 | // Values 51 | { 52 | "$1": "J%" 53 | } 54 | ``` 55 | 56 | ## Further Examples 57 | 58 | :bulb: **Usage as SQL-Function** 59 | ```javascript 60 | function() { 61 | let averageAge = 45; 62 | 63 | return sql.build({ 64 | $select: { 65 | $from: 'people', 66 | $where: { 67 | first_name: sql.startsWith('J') 68 | } 69 | } 70 | }); 71 | } 72 | 73 | // SQL output 74 | SELECT 75 | * 76 | FROM 77 | people 78 | WHERE 79 | first_name LIKE $1 80 | 81 | // Values 82 | { 83 | "$1": "J%" 84 | } 85 | ``` 86 | 87 | -------------------------------------------------------------------------------- /sql/helpers/comparision/startsWith/startsWith.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class startsWith extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('LIKE ') } 9 | }); 10 | } 11 | 12 | preBuild(query, identifier){ 13 | if (this.isString(query)) { 14 | query += '%'; 15 | } 16 | return query 17 | } 18 | 19 | callee(value){ 20 | if (this.isString(value)) { 21 | value += '%'; 22 | return 'LIKE ' + this.addValue(value); 23 | } 24 | 25 | throw new Error('Parameter value on using function startsWith must always be a String.'); 26 | } 27 | } 28 | 29 | module.exports = { 30 | definition: startsWith, 31 | description: 'Specifies the comparision `LIKE` Operator as Helper.', 32 | supportedBy: { 33 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like', 34 | MariaDB: 'https://mariadb.com/kb/en/library/like/', 35 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-matching.html', 36 | SQLite: 'https://sqlite.org/lang_expr.html#like', 37 | Oracle: 'https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm', 38 | SQLServer: 'https://docs.microsoft.com/en-US/sql/t-sql/language-elements/like-transact-sql' 39 | }, 40 | examples: { 41 | String: { 42 | "Basic Usage": function(sql){ 43 | return { 44 | test: function(){ 45 | return sql.build({ 46 | $select: { 47 | $from: 'people', 48 | $where: { 49 | first_name: { $startsWith: 'J' } 50 | } 51 | } 52 | }); 53 | }, 54 | expectedResults: { 55 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 56 | values: { 57 | $1: 'J%' 58 | } 59 | } 60 | } 61 | }, 62 | "Usage as SQL-Function": function(sql){ 63 | return { 64 | test: function(){ 65 | let averageAge = 45; 66 | 67 | return sql.build({ 68 | $select: { 69 | $from: 'people', 70 | $where: { 71 | first_name: sql.startsWith('J') 72 | } 73 | } 74 | }); 75 | }, 76 | expectedResults: { 77 | sql: 'SELECT * FROM people WHERE first_name LIKE $1', 78 | values: { 79 | $1: 'J%' 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sql/helpers/ddl/column/private/default/README.md: -------------------------------------------------------------------------------- 1 | # default Helper 2 | Specifies the Default Value of a single Column to use on `$define` with `$createTable` Operator. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/create-table/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-createtable.html) 8 | - [SQLite](https://sqlite.org/lang_createtable.html) 9 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7002.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `default` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $default: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.$createTable({ 33 | $table: 'my_people_table', 34 | $define: { 35 | people_id: { $column: { $type: 'INT' } }, 36 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true, $default: 'Unknown' } }, 37 | last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 38 | bio: { $column: { $type: 'TEXT' } } 39 | } 40 | }); 41 | } 42 | 43 | // SQL output 44 | CREATE TABLE my_people_table ( 45 | people_id INT, 46 | first_name VARCHAR(50) NOT NULL DEFAULT $1, 47 | last_name VARCHAR(50) NOT NULL, 48 | bio TEXT 49 | ) 50 | 51 | // Values 52 | { 53 | "$1": "Unknown" 54 | } 55 | ``` 56 | 57 | ## as Number: 58 | 59 | Usage of `default` as **Number** with the following Syntax: 60 | 61 | **Syntax:** 62 | 63 | ```javascript 64 | $default: < Number > 65 | ``` 66 | 67 | **SQL-Definition:** 68 | ```javascript 69 | 70 | ``` 71 | 72 | :bulb: **Example:** 73 | ```javascript 74 | function() { 75 | return sql.$createTable({ 76 | $table: 'my_people_table', 77 | $define: { 78 | people_id: { $column: { $type: 'INT', $default: 0 } }, 79 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 80 | last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 81 | bio: { $column: { $type: 'TEXT' } }, 82 | } 83 | }); 84 | } 85 | 86 | // SQL output 87 | CREATE TABLE my_people_table ( 88 | people_id INT DEFAULT $1, 89 | first_name VARCHAR(50) NOT NULL, 90 | last_name VARCHAR(50) NOT NULL, 91 | bio TEXT 92 | ) 93 | 94 | // Values 95 | { 96 | "$1": 0 97 | } 98 | ``` 99 | 100 | -------------------------------------------------------------------------------- /sql/helpers/ddl/column/private/default/default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class $default extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('') }, 9 | Number: { syntax: this.Syntax('') } 10 | }); 11 | } 12 | } 13 | 14 | module.exports = { 15 | definition: $default, 16 | description: 'Specifies the Default Value of a single Column to use on `$define` with `$createTable` Operator.', 17 | supportedBy: { 18 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/create-table.html', 19 | MariaDB: 'https://mariadb.com/kb/en/library/create-table/', 20 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-createtable.html', 21 | SQLite: 'https://sqlite.org/lang_createtable.html', 22 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7002.htm', 23 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql' 24 | }, 25 | examples: { 26 | String: { 27 | "Basic Usage": function(sql) { 28 | return { 29 | test: function(){ 30 | return sql.$createTable({ 31 | $table: 'my_people_table', 32 | $define: { 33 | people_id: { $column: { $type: 'INT' } }, 34 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true, $default: 'Unknown' } }, 35 | last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 36 | bio: { $column: { $type: 'TEXT' } } 37 | } 38 | }); 39 | }, 40 | expectedResults: { 41 | sql: 'CREATE TABLE my_people_table (people_id INT, first_name VARCHAR(50) NOT NULL DEFAULT $1, last_name VARCHAR(50) NOT NULL, bio TEXT)', 42 | values: { 43 | $1: 'Unknown' 44 | }, 45 | PostgreSQL: { 46 | sql: 'CREATE TABLE my_people_table (people_id INT, first_name VARCHAR(50) NOT NULL DEFAULT \'Unknown\', last_name VARCHAR(50) NOT NULL, bio TEXT)', 47 | values: {}, 48 | } 49 | } 50 | } 51 | } 52 | }, 53 | Number: { 54 | "Basic Usage": function(sql) { 55 | return { 56 | test: function(){ 57 | return sql.$createTable({ 58 | $table: 'my_people_table', 59 | $define: { 60 | people_id: { $column: { $type: 'INT', $default: 0 } }, 61 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 62 | last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 63 | bio: { $column: { $type: 'TEXT' } }, 64 | } 65 | }); 66 | }, 67 | expectedResults: { 68 | sql: 'CREATE TABLE my_people_table (people_id INT DEFAULT $1, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, bio TEXT)', 69 | values: { 70 | $1: 0 71 | }, 72 | PostgreSQL: { 73 | sql: 'CREATE TABLE my_people_table (people_id INT DEFAULT 0, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, bio TEXT)', 74 | values: {}, 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /sql/helpers/ddl/column/private/type/README.md: -------------------------------------------------------------------------------- 1 | # type Helper 2 | Specifies the Data Type of a single Column to use on `$define` with `$createTable` Operator. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/data-types.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/data-types/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/datatype.html) 8 | - [SQLite](https://www.sqlite.org/datatype3.html) 9 | - [Oracle](https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT012) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `type` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $type: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.$createTable({ 33 | $table: 'my_people_table', 34 | $define: { 35 | people_id: { $column: { $type: 'INT', $default: 0 } }, 36 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 37 | last_name: { $column: { $type: sql.VARCHAR, $size: 50, $notNull: true } }, 38 | bio: { $column: { $type: sql.TEXT } }, 39 | weight: { $column: { $type: sql.NUMERIC, $size: { $precision: 3, $scale: 2 } } }, 40 | 41 | pk_people: { $constraint: { $primary: true, $columns: 'people_id' } } 42 | } 43 | }); 44 | } 45 | 46 | // SQL output 47 | CREATE TABLE my_people_table ( 48 | people_id INT DEFAULT $1, 49 | first_name VARCHAR(50) NOT NULL, 50 | last_name VARCHAR(50) NOT NULL, 51 | bio TEXT, 52 | weight NUMERIC(3, 2), 53 | CONSTRAINT pk_people PRIMARY KEY (people_id) 54 | ) 55 | 56 | // Values 57 | { 58 | "$1": 0 59 | } 60 | ``` 61 | 62 | -------------------------------------------------------------------------------- /sql/helpers/functions/cast/README.md: -------------------------------------------------------------------------------- 1 | # cast Helper 2 | Specifies the `CAST` function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/10/functions-formatting.html) 6 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as Object: 11 | 12 | Usage of `cast` as **Object** with the following Syntax: 13 | 14 | **Syntax:** 15 | 16 | ```javascript 17 | $cast: { ... } 18 | ``` 19 | 20 | **SQL-Definition:** 21 | ```javascript 22 | CAST(<$expr> AS <$format>) 23 | ``` 24 | 25 | **Registered Helpers** 26 | 27 | Name|Required|Public|SQL-Definition|Supported by 28 | :---|:------:|:----:|:-------------|:----------- 29 | [expr](./private/expr/)|:heavy_check_mark:|*private*|| 30 | [format](./private/format/)|:heavy_check_mark:|*private*|| 31 | 32 | :bulb: **Example:** 33 | ```javascript 34 | function() { 35 | return sql.$select({ 36 | cast_value: { $cast: { $expr: '2017-02-31', $format: 'varchar' } } 37 | }); 38 | } 39 | 40 | // SQL output 41 | SELECT 42 | CAST($1 AS $2) AS cast_value 43 | 44 | // Values 45 | { 46 | "$1": "2017-02-31", 47 | "$2": "varchar" 48 | } 49 | ``` 50 | 51 | -------------------------------------------------------------------------------- /sql/helpers/functions/cast/cast.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class cast extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { syntax: this.Syntax('CAST(<$expr> AS <$format>)') }, 9 | }); 10 | 11 | this.$expr = new SQLBuilder.SQLPredefined.StringValueParam(sql); 12 | this.$format = new SQLBuilder.SQLPredefined.StringValueParam(sql); 13 | } 14 | } 15 | 16 | module.exports = { 17 | definition: cast, 18 | description: `Specifies the \`CAST\` function.`, 19 | supportedBy: { 20 | PostgreSQL: 'https://www.postgresql.org/docs/10/functions-formatting.html', 21 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm', 22 | }, 23 | examples: { 24 | Object: { 25 | "Basic Usage": function(sql) { 26 | return { 27 | test: function(){ 28 | return sql.$select({ 29 | cast_value: { $cast: { $expr: '2017-02-31', $format: 'varchar' } } 30 | }); 31 | }, 32 | expectedResults: { 33 | sql: 'SELECT CAST($1 AS $2) AS cast_value', 34 | values: { 35 | $1: '2017-02-31', 36 | $2: 'varchar' 37 | } 38 | } 39 | } 40 | } 41 | }, 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /sql/helpers/functions/comparision/cmp/README.md: -------------------------------------------------------------------------------- 1 | # cmp Helper 2 | Specifies a global comparision function `cmp` as Helper. 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `cmp` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $cmp: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | {<$value> }<$comparator>{ <$other>} 27 | ``` 28 | 29 | **Registered Helpers** 30 | 31 | Name|Required|Public|SQL-Definition|Supported by 32 | :---|:------:|:----:|:-------------|:----------- 33 | [value](./private/value/)|:heavy_check_mark:|*private*| <$value> | 34 | [comparator](./private/comparator/)|:heavy_check_mark:|*private*|| 35 | [other](./private/other/)|:heavy_check_mark:|*private*| <$other>| 36 | 37 | :bulb: **Example:** 38 | ```javascript 39 | function() { 40 | return sql.build({ 41 | $select: { 42 | $from: 'people', 43 | $where: { 44 | $and: [ 45 | sql.cmp('~~first_name', '=', 'Jane'), 46 | sql.cmp('~~last_name', '=', 'Doe'), 47 | ] 48 | } 49 | } 50 | }); 51 | } 52 | 53 | // SQL output 54 | SELECT 55 | * 56 | FROM 57 | people 58 | WHERE 59 | first_name = $1 60 | AND last_name = $2 61 | 62 | // Values 63 | { 64 | "$1": "Jane", 65 | "$2": "Doe" 66 | } 67 | ``` 68 | 69 | -------------------------------------------------------------------------------- /sql/helpers/functions/comparision/cmp/cmp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class valueHelper extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('') }, 9 | Number: { syntax: this.Syntax('') }, 10 | Object: { syntax: this.Syntax('') }, 11 | Function: { syntax: this.Syntax('') }, 12 | }); 13 | } 14 | } 15 | 16 | class cmp extends SQLBuilder.SQLHelper { 17 | constructor(sql){ 18 | super(sql); 19 | 20 | this.Types({ 21 | Object: { syntax: this.Syntax('{<$value> }<$comparator>{ <$other>}', SQLBuilder.CALLEE) } 22 | }); 23 | 24 | this.registerPrivateHelper('comparator'); 25 | 26 | //this.$value = new SQLBuilder.SQLPredefined.PrimitiveValueParam(sql); 27 | //this.$other = new SQLBuilder.SQLPredefined.PrimitiveValueParam(sql); 28 | this.$value = new valueHelper(sql); 29 | this.$other = new valueHelper(sql); 30 | } 31 | } 32 | 33 | module.exports = { 34 | definition: cmp, 35 | description: 'Specifies a global comparision function `cmp` as Helper.', 36 | supportedBy: { 37 | MySQL: '', 38 | MariaDB: '', 39 | PostgreSQL: '', 40 | SQLite: '', 41 | Oracle: '', 42 | SQLServer: '' 43 | }, 44 | examples: { 45 | Object: { 46 | "Basic Usage": function(sql) { 47 | return { 48 | test: function(){ 49 | return sql.build({ 50 | $select: { 51 | $from: 'people', 52 | $where: { 53 | $and: [ 54 | sql.cmp('~~first_name', '=', 'Jane'), 55 | sql.cmp('~~last_name', '=', 'Doe'), 56 | ] 57 | } 58 | } 59 | }); 60 | }, 61 | expectedResults: { 62 | sql: 'SELECT * FROM people WHERE first_name = $1 AND last_name = $2', 63 | values: { 64 | $1: 'Jane', 65 | $2: 'Doe' 66 | } 67 | } 68 | } 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /sql/helpers/functions/datetime/date/README.md: -------------------------------------------------------------------------------- 1 | # date Helper 2 | Specifies the `DATE` function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/10/functions-formatting.html) 6 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as String: 11 | 12 | Usage of `date` as **String** with the following Syntax: 13 | 14 | **Syntax:** 15 | 16 | ```javascript 17 | $date: < String > 18 | ``` 19 | 20 | **SQL-Definition:** 21 | ```javascript 22 | DATE() 23 | ``` 24 | 25 | :bulb: **Example:** 26 | ```javascript 27 | function() { 28 | return sql.$select({ 29 | my_timestamp: { $date: '~~foo.column' }, 30 | $from: 'foo' 31 | }); 32 | } 33 | 34 | // SQL output 35 | SELECT 36 | DATE(foo.column) AS my_timestamp 37 | FROM 38 | foo 39 | 40 | // Values 41 | {} 42 | ``` 43 | 44 | -------------------------------------------------------------------------------- /sql/helpers/functions/datetime/date/date.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class date extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('DATE()') }, 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: date, 15 | description: `Specifies the \`DATE\` function.`, 16 | supportedBy: { 17 | PostgreSQL: 'https://www.postgresql.org/docs/10/functions-formatting.html', 18 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm', 19 | }, 20 | examples: { 21 | String: { 22 | "Basic Usage": function(sql) { 23 | return { 24 | test: function(){ 25 | return sql.$select({ 26 | my_timestamp: { $date: '~~foo.column' }, 27 | $from: 'foo' 28 | }); 29 | }, 30 | expectedResults: { 31 | sql: 'SELECT DATE(foo.column) AS my_timestamp FROM foo', 32 | values: {} 33 | } 34 | } 35 | } 36 | }, 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /sql/helpers/functions/datetime/now/README.md: -------------------------------------------------------------------------------- 1 | # now Helper 2 | Specifies the `NOW` function. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now) 6 | - [MariaDB](https://mariadb.com/kb/en/library/now/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/11/functions-datetime.html) 8 | 9 | # Allowed Types and Usage 10 | 11 | ## as Boolean: 12 | 13 | The usage of `now` as **Boolean** is restricted to the following values: 14 | - true 15 | 16 | #### as Boolean with value **true**: 17 | **Syntax:** 18 | 19 | ```javascript 20 | $now: true 21 | ``` 22 | 23 | **SQL-Definition:** 24 | ```javascript 25 | NOW() 26 | ``` 27 | 28 | :bulb: **Example:** 29 | ```javascript 30 | function() { 31 | return sql.$select({ 32 | foo: { $now: true } 33 | }); 34 | } 35 | 36 | // SQL output 37 | SELECT 38 | NOW() AS foo 39 | 40 | // Values 41 | {} 42 | ``` 43 | ## as Number: 44 | 45 | The usage of `now` as **Number** is restricted to the following values: 46 | - 1 47 | 48 | #### as Number with value **1**: 49 | **Syntax:** 50 | 51 | ```javascript 52 | $now: 1 53 | ``` 54 | 55 | **SQL-Definition:** 56 | ```javascript 57 | NOW() 58 | ``` 59 | 60 | :bulb: **Example:** 61 | ```javascript 62 | function() { 63 | return sql.$select({ 64 | foo: { $now: 1 } 65 | }); 66 | } 67 | 68 | // SQL output 69 | SELECT 70 | NOW() AS foo 71 | 72 | // Values 73 | {} 74 | ``` 75 | ## Further Examples 76 | 77 | :bulb: **UPDATE using NOW() function** 78 | ```javascript 79 | function() { 80 | return sql.$update({ 81 | $table: 'my_table', 82 | $set: { 83 | foo: { $now: true } 84 | }, 85 | $where: { 86 | id: 55 87 | } 88 | }); 89 | } 90 | 91 | // SQL output 92 | UPDATE 93 | my_table 94 | SET 95 | foo = NOW() 96 | WHERE 97 | id = $1 98 | 99 | // Values 100 | { 101 | "$1": 55 102 | } 103 | ``` 104 | 105 | -------------------------------------------------------------------------------- /sql/helpers/functions/datetime/now/now.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class now extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('NOW()') 11 | } 12 | }, 13 | Number: { 14 | syntax: { 15 | 1: this.Syntax('NOW()') 16 | } 17 | }, 18 | }); 19 | } 20 | } 21 | 22 | module.exports = { 23 | definition: now, 24 | description: `Specifies the \`NOW\` function.`, 25 | supportedBy: { 26 | MySQL: 'https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now', 27 | // TODO: MariaDb implement optional precision param 28 | MariaDB: 'https://mariadb.com/kb/en/library/now/', 29 | PostgreSQL: 'https://www.postgresql.org/docs/11/functions-datetime.html', 30 | }, 31 | examples: { 32 | Number: { 33 | 1: { 34 | "Basic Usage": function(sql) { 35 | return { 36 | test: function(){ 37 | return sql.$select({ 38 | foo: { $now: 1 } 39 | }); 40 | }, 41 | expectedResults: { 42 | sql: 'SELECT NOW() AS foo', 43 | values: {} 44 | } 45 | } 46 | } 47 | } 48 | }, 49 | Boolean: { 50 | true: { 51 | "Basic Usage": function(sql) { 52 | return { 53 | test: function(){ 54 | return sql.$select({ 55 | foo: { $now: true } 56 | }); 57 | }, 58 | expectedResults: { 59 | sql: 'SELECT NOW() AS foo', 60 | values: {} 61 | } 62 | } 63 | }, 64 | "UPDATE using NOW() function": function(sql) { 65 | return { 66 | test: function(){ 67 | return sql.$update({ 68 | $table: 'my_table', 69 | $set: { 70 | foo: { $now: true } 71 | }, 72 | $where: { 73 | id: 55 74 | } 75 | }); 76 | }, 77 | expectedResults: { 78 | sql: 'UPDATE my_table SET foo = NOW() WHERE id = $1', 79 | values: { 80 | $1: 55 81 | } 82 | } 83 | } 84 | } 85 | } 86 | }, 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /sql/helpers/functions/datetime/to_timestamp/README.md: -------------------------------------------------------------------------------- 1 | # toTimestamp Helper 2 | Specifies the `TO_TIMESTAMP` function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/10/functions-formatting.html) 6 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as String: 11 | 12 | Usage of `toTimestamp` as **String** with the following Syntax: 13 | 14 | **Syntax:** 15 | 16 | ```javascript 17 | $toTimestamp: < String > 18 | ``` 19 | 20 | **SQL-Definition:** 21 | ```javascript 22 | TO_TIMESTAMP() 23 | ``` 24 | 25 | :bulb: **Example:** 26 | ```javascript 27 | function() { 28 | return sql.$select({ 29 | my_timestamp: { $toTimestamp: '~~foo.column' }, 30 | $from: 'foo' 31 | }); 32 | } 33 | 34 | // SQL output 35 | SELECT 36 | TO_TIMESTAMP(foo.column) AS my_timestamp 37 | FROM 38 | foo 39 | 40 | // Values 41 | {} 42 | ``` 43 | 44 | ## as Object: 45 | 46 | Usage of `toTimestamp` as **Object** with the following Syntax: 47 | 48 | **Syntax:** 49 | 50 | ```javascript 51 | $toTimestamp: { ... } 52 | ``` 53 | 54 | **SQL-Definition:** 55 | ```javascript 56 | TO_TIMESTAMP(<$expr>, <$format>) 57 | ``` 58 | 59 | **Registered Helpers** 60 | 61 | Name|Required|Public|SQL-Definition|Supported by 62 | :---|:------:|:----:|:-------------|:----------- 63 | [expr](./private/expr/)|:heavy_check_mark:|*private*|| 64 | [format](./private/format/)|:heavy_check_mark:|*private*|| 65 | 66 | :bulb: **Example:** 67 | ```javascript 68 | function() { 69 | return sql.$select({ 70 | my_timestamp: { $toTimestamp: { $expr: '2017-02-31 30:8:00', $format: 'YYYY-MM-DD HH24:MI:SS' } } 71 | }); 72 | } 73 | 74 | // SQL output 75 | SELECT 76 | TO_TIMESTAMP($1, $2) AS my_timestamp 77 | 78 | // Values 79 | { 80 | "$1": "2017-02-31 30:8:00", 81 | "$2": "YYYY-MM-DD HH24:MI:SS" 82 | } 83 | ``` 84 | 85 | -------------------------------------------------------------------------------- /sql/helpers/functions/datetime/to_timestamp/to_timestamp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class toTimestamp extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('TO_TIMESTAMP()') }, 9 | Object: { syntax: this.Syntax('TO_TIMESTAMP(<$expr>, <$format>)', SQLBuilder.CALLEE) }, 10 | }); 11 | 12 | this.$expr = new SQLBuilder.SQLPredefined.StringValueParam(sql); 13 | this.$format = new SQLBuilder.SQLPredefined.StringValueParam(sql); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: toTimestamp, 19 | description: `Specifies the \`TO_TIMESTAMP\` function.`, 20 | supportedBy: { 21 | PostgreSQL: 'https://www.postgresql.org/docs/10/functions-formatting.html', 22 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm', 23 | }, 24 | examples: { 25 | String: { 26 | "Basic Usage": function(sql) { 27 | return { 28 | test: function(){ 29 | return sql.$select({ 30 | my_timestamp: { $toTimestamp: '~~foo.column' }, 31 | $from: 'foo' 32 | }); 33 | }, 34 | expectedResults: { 35 | sql: 'SELECT TO_TIMESTAMP(foo.column) AS my_timestamp FROM foo', 36 | values: {} 37 | } 38 | } 39 | } 40 | }, 41 | Object: { 42 | "Basic Usage": function(sql) { 43 | return { 44 | test: function(){ 45 | return sql.$select({ 46 | my_timestamp: { $toTimestamp: { $expr: '2017-02-31 30:8:00', $format: 'YYYY-MM-DD HH24:MI:SS' } } 47 | }); 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT TO_TIMESTAMP($1, $2) AS my_timestamp', 51 | values: { 52 | $1: '2017-02-31 30:8:00', 53 | $2: 'YYYY-MM-DD HH24:MI:SS' 54 | } 55 | } 56 | } 57 | } 58 | }, 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonAgg/README.md: -------------------------------------------------------------------------------- 1 | # jsonAgg Operator 2 | Specifies the PostgreSQL `json_agg` aggregation Function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/10/static/functions-aggregate.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `jsonAgg` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $jsonAgg: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | json_agg() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$select({ 28 | people_id: 1, 29 | emails: { $jsonAgg: 'address' }, 30 | $from: 'people_emails', 31 | $groupBy: 'people_id' 32 | }); 33 | } 34 | 35 | // SQL output 36 | SELECT 37 | people_id, 38 | json_agg(address) AS emails 39 | FROM 40 | people_emails 41 | GROUP BY 42 | people_id 43 | 44 | // Values 45 | {} 46 | ``` 47 | 48 | ## as Object: 49 | 50 | Usage of `jsonAgg` as **Object** with the following Syntax: 51 | 52 | **Syntax:** 53 | 54 | ```javascript 55 | $jsonAgg: { ... } 56 | ``` 57 | 58 | **SQL-Definition:** 59 | ```javascript 60 | json_agg() 61 | ``` 62 | 63 | :bulb: **Example:** 64 | ```javascript 65 | function() { 66 | return sql.$select({ 67 | emails: { 68 | $select: { 69 | emails: { 70 | $jsonAgg: { 71 | $jsonBuildObject: { 72 | address: '~~people_emails.address' 73 | } 74 | } 75 | }, 76 | $from: 'people_emails', 77 | $where: { 78 | 'people.id': '~~people_emails.people_id' 79 | } 80 | } 81 | }, 82 | $from: 'people' 83 | }); 84 | } 85 | 86 | // SQL output 87 | SELECT 88 | ( 89 | SELECT 90 | json_agg(json_build_object($1, people_emails.address)) AS emails 91 | FROM 92 | people_emails 93 | WHERE 94 | people.id = people_emails.people_id 95 | ) AS emails 96 | FROM 97 | people 98 | 99 | // Values 100 | { 101 | "$1": "address" 102 | } 103 | ``` 104 | 105 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonAgg/jsonAgg.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonAgg extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('json_agg()', SQLBuilder.CALLEE) }, 9 | Object: { syntax: this.Syntax('json_agg()') }, 10 | }); 11 | } 12 | } 13 | 14 | 15 | module.exports = { 16 | definition: jsonAgg, 17 | description: 'Specifies the PostgreSQL `json_agg` aggregation Function.', 18 | supportedBy: { 19 | PostgreSQL: 'https://www.postgresql.org/docs/10/static/functions-aggregate.html', 20 | }, 21 | examples: { 22 | String: { 23 | "Basic Usage": function(sql) { 24 | return { 25 | test: function() { 26 | return sql.$select({ 27 | people_id: 1, 28 | emails: { $jsonAgg: 'address' }, 29 | $from: 'people_emails', 30 | $groupBy: 'people_id' 31 | }); 32 | }, 33 | expectedResults: { 34 | sql: 'SELECT people_id, json_agg(address) AS emails FROM people_emails GROUP BY people_id', 35 | values: {} 36 | } 37 | } 38 | } 39 | }, 40 | Object: { 41 | "Basic Usage": function(sql) { 42 | return { 43 | test: function() { 44 | return sql.$select({ 45 | emails: { 46 | $select: { 47 | emails: { 48 | $jsonAgg: { 49 | $jsonBuildObject: { 50 | address: '~~people_emails.address' 51 | } 52 | } 53 | }, 54 | $from: 'people_emails', 55 | $where: { 56 | 'people.id': '~~people_emails.people_id' 57 | } 58 | } 59 | }, 60 | $from: 'people' 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT (SELECT json_agg(json_build_object($1, people_emails.address)) AS emails FROM people_emails WHERE people.id = people_emails.people_id) AS emails FROM people', 65 | values: { 66 | $1: 'address' 67 | } 68 | } 69 | } 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonEach/README.md: -------------------------------------------------------------------------------- 1 | # jsonEach Operator 2 | Specifies the PostgreSQL `json_each` Function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-json.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `jsonEach` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $jsonEach: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | json_each() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$select({ 28 | $from: { 29 | jsondata: { 30 | $jsonEach: JSON.stringify({ 31 | a: 'foo', 32 | b: 'bar' 33 | }) 34 | } 35 | }, 36 | }); 37 | } 38 | 39 | // SQL output 40 | SELECT 41 | * 42 | FROM 43 | json_each($1) AS jsondata 44 | 45 | // Values 46 | { 47 | "$1": "{\"a\":\"foo\",\"b\":\"bar\"}" 48 | } 49 | ``` 50 | 51 | ## as Object: 52 | 53 | Usage of `jsonEach` as **Object** with the following Syntax: 54 | 55 | **Syntax:** 56 | 57 | ```javascript 58 | $jsonEach: { ... } 59 | ``` 60 | 61 | **SQL-Definition:** 62 | ```javascript 63 | json_each() 64 | ``` 65 | 66 | :bulb: **Example:** 67 | ```javascript 68 | function() { 69 | return sql.$select({ 70 | $from: { 71 | jsondata: { 72 | $jsonEach: { 73 | $jsonBuildObject: { 74 | a: 'foo', 75 | b: 123 76 | } 77 | } 78 | } 79 | }, 80 | }); 81 | } 82 | 83 | // SQL output 84 | SELECT 85 | * 86 | FROM 87 | json_each(json_build_object($1, $2, $3, $4)) AS jsondata 88 | 89 | // Values 90 | { 91 | "$1": "a", 92 | "$2": "foo", 93 | "$3": "b", 94 | "$4": 123 95 | } 96 | ``` 97 | 98 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonEach/jsonEach.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonEach extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('json_each()', SQLBuilder.CALLEE) }, 9 | Object: { syntax: this.Syntax('json_each()') }, 10 | }); 11 | } 12 | } 13 | 14 | 15 | module.exports = { 16 | definition: jsonEach, 17 | description: 'Specifies the PostgreSQL `json_each` Function.', 18 | supportedBy: { 19 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-json.html', 20 | }, 21 | examples: { 22 | String: { 23 | "Basic Usage": function(sql) { 24 | return { 25 | test: function() { 26 | return sql.$select({ 27 | $from: { 28 | jsondata: { 29 | $jsonEach: JSON.stringify({ 30 | a: 'foo', 31 | b: 'bar' 32 | }) 33 | } 34 | }, 35 | }); 36 | }, 37 | expectedResults: { 38 | sql: 'SELECT * FROM json_each($1) AS jsondata', 39 | values: { 40 | $1: '{"a":"foo","b":"bar"}' 41 | } 42 | } 43 | } 44 | } 45 | }, 46 | Object: { 47 | "Basic Usage": function(sql) { 48 | return { 49 | test: function() { 50 | return sql.$select({ 51 | $from: { 52 | jsondata: { 53 | $jsonEach: { 54 | $jsonBuildObject: { 55 | a: 'foo', 56 | b: 123 57 | } 58 | } 59 | } 60 | }, 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT * FROM json_each(json_build_object($1, $2, $3, $4)) AS jsondata', 65 | values: { 66 | $1: 'a', 67 | $2: 'foo', 68 | $3: 'b', 69 | $4: 123 70 | } 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonObjectAgg/jsonObjectAgg.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonObjectAgg extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | eachItemOf: { 10 | Primitive: { syntax: this.Syntax(', [ , ... ]') }, 11 | Object: { syntax: this.Syntax(', [ , ... ]') }, 12 | } 13 | } 14 | }); 15 | } 16 | 17 | postBuild(result, type, itemType) { 18 | return 'json_object_agg(' + result + ')'; 19 | } 20 | } 21 | 22 | 23 | module.exports = { 24 | definition: jsonObjectAgg, 25 | description: 'Specifies the PostgreSQL `json_object_agg` aggregation Function.', 26 | supportedBy: { 27 | PostgreSQL: 'https://www.postgresql.org/docs/10/static/functions-aggregate.html', 28 | }, 29 | examples: { 30 | Object: { 31 | eachItemOf: { 32 | Primitive: { 33 | "Basic Usage": function(sql) { 34 | return { 35 | test: function() { 36 | return sql.$select({ 37 | services: { $jsonObjectAgg: { '~~servicedata.key': '~~servicedata.value' } }, 38 | $from: { 39 | data: { 40 | $select: { 41 | services: { $jsonBuildObject: { '~~users_loginservices.service_id': '~~users_loginservices.data' } }, 42 | $from: 'users_loginservices' 43 | } 44 | }, 45 | servicedata: { 46 | $jsonEach: '~~data.services' 47 | } 48 | } 49 | }); 50 | }, 51 | expectedResults: { 52 | sql: 'SELECT json_object_agg(servicedata.key, servicedata.value) AS services FROM (SELECT json_build_object(users_loginservices.service_id, users_loginservices.data) AS services FROM users_loginservices) AS data, json_each(data.services) AS servicedata', 53 | values: {} 54 | } 55 | } 56 | } 57 | }, 58 | Object: { 59 | "Basic Usage": function(sql) { 60 | return { 61 | test: function() { 62 | return sql.$select({ 63 | jobj: { $jsonObjectAgg: { '~~jsondata.key': '~~jsondata.value' } }, 64 | $from: { 65 | jsondata: { 66 | $jsonEach: { 67 | $jsonBuildObject: { 68 | a: 'foo', 69 | b: 123 70 | } 71 | } 72 | } 73 | } 74 | }); 75 | }, 76 | expectedResults: { 77 | sql: 'SELECT json_object_agg(jsondata.key, jsondata.value) AS jobj FROM json_each(json_build_object($1, $2, $3, $4)) AS jsondata', 78 | values: { 79 | $1: 'a', 80 | $2: 'foo', 81 | $3: 'b', 82 | $4: 123 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbAgg/README.md: -------------------------------------------------------------------------------- 1 | # jsonbAgg Operator 2 | Specifies the PostgreSQL `jsonb_agg` aggregation Function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/10/static/functions-aggregate.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `jsonbAgg` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $jsonbAgg: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | jsonb_agg() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$select({ 28 | people_id: 1, 29 | emails: { $jsonbAgg: 'address' }, 30 | $from: 'people_emails', 31 | $groupBy: 'people_id' 32 | }); 33 | } 34 | 35 | // SQL output 36 | SELECT 37 | people_id, 38 | jsonb_agg(address) AS emails 39 | FROM 40 | people_emails 41 | GROUP BY 42 | people_id 43 | 44 | // Values 45 | {} 46 | ``` 47 | 48 | ## as Object: 49 | 50 | Usage of `jsonbAgg` as **Object** with the following Syntax: 51 | 52 | **Syntax:** 53 | 54 | ```javascript 55 | $jsonbAgg: { ... } 56 | ``` 57 | 58 | **SQL-Definition:** 59 | ```javascript 60 | jsonb_agg() 61 | ``` 62 | 63 | :bulb: **Example:** 64 | ```javascript 65 | function() { 66 | return sql.$select({ 67 | emails: { 68 | $select: { 69 | emails: { 70 | $jsonbAgg: { 71 | $jsonbBuildObject: { 72 | address: '~~people_emails.address' 73 | } 74 | } 75 | }, 76 | $from: 'people_emails', 77 | $where: { 78 | 'people.id': '~~people_emails.people_id' 79 | } 80 | } 81 | }, 82 | $from: 'people' 83 | }); 84 | } 85 | 86 | // SQL output 87 | SELECT 88 | ( 89 | SELECT 90 | jsonb_agg(jsonb_build_object($1, people_emails.address)) AS emails 91 | FROM 92 | people_emails 93 | WHERE 94 | people.id = people_emails.people_id 95 | ) AS emails 96 | FROM 97 | people 98 | 99 | // Values 100 | { 101 | "$1": "address" 102 | } 103 | ``` 104 | 105 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbAgg/jsonbAgg.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonbAgg extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('jsonb_agg()', SQLBuilder.CALLEE) }, 9 | Object: { syntax: this.Syntax('jsonb_agg()') }, 10 | }); 11 | } 12 | } 13 | 14 | 15 | module.exports = { 16 | definition: jsonbAgg, 17 | description: 'Specifies the PostgreSQL `jsonb_agg` aggregation Function.', 18 | supportedBy: { 19 | PostgreSQL: 'https://www.postgresql.org/docs/10/static/functions-aggregate.html', 20 | }, 21 | examples: { 22 | String: { 23 | "Basic Usage": function(sql) { 24 | return { 25 | test: function() { 26 | return sql.$select({ 27 | people_id: 1, 28 | emails: { $jsonbAgg: 'address' }, 29 | $from: 'people_emails', 30 | $groupBy: 'people_id' 31 | }); 32 | }, 33 | expectedResults: { 34 | sql: 'SELECT people_id, jsonb_agg(address) AS emails FROM people_emails GROUP BY people_id', 35 | values: {} 36 | } 37 | } 38 | } 39 | }, 40 | Object: { 41 | "Basic Usage": function(sql) { 42 | return { 43 | test: function() { 44 | return sql.$select({ 45 | emails: { 46 | $select: { 47 | emails: { 48 | $jsonbAgg: { 49 | $jsonbBuildObject: { 50 | address: '~~people_emails.address' 51 | } 52 | } 53 | }, 54 | $from: 'people_emails', 55 | $where: { 56 | 'people.id': '~~people_emails.people_id' 57 | } 58 | } 59 | }, 60 | $from: 'people' 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT (SELECT jsonb_agg(jsonb_build_object($1, people_emails.address)) AS emails FROM people_emails WHERE people.id = people_emails.people_id) AS emails FROM people', 65 | values: { 66 | $1: 'address' 67 | } 68 | } 69 | } 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbEach/README.md: -------------------------------------------------------------------------------- 1 | # jsonbEach Operator 2 | Specifies the PostgreSQL `jsonb_each` Function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-json.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `jsonbEach` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $jsonbEach: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | jsonb_each() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$select({ 28 | $from: { 29 | jsondata: { 30 | $jsonbEach: JSON.stringify({ 31 | a: 'foo', 32 | b: 'bar' 33 | }) 34 | } 35 | }, 36 | }); 37 | } 38 | 39 | // SQL output 40 | SELECT 41 | * 42 | FROM 43 | jsonb_each($1) AS jsondata 44 | 45 | // Values 46 | { 47 | "$1": "{\"a\":\"foo\",\"b\":\"bar\"}" 48 | } 49 | ``` 50 | 51 | ## as Object: 52 | 53 | Usage of `jsonbEach` as **Object** with the following Syntax: 54 | 55 | **Syntax:** 56 | 57 | ```javascript 58 | $jsonbEach: { ... } 59 | ``` 60 | 61 | **SQL-Definition:** 62 | ```javascript 63 | jsonb_each() 64 | ``` 65 | 66 | :bulb: **Example:** 67 | ```javascript 68 | function() { 69 | return sql.$select({ 70 | $from: { 71 | jsondata: { 72 | $jsonbEach: { 73 | $jsonbBuildObject: { 74 | a: 'foo', 75 | b: 123 76 | } 77 | } 78 | } 79 | }, 80 | }); 81 | } 82 | 83 | // SQL output 84 | SELECT 85 | * 86 | FROM 87 | jsonb_each(jsonb_build_object($1, $2, $3, $4)) AS jsondata 88 | 89 | // Values 90 | { 91 | "$1": "a", 92 | "$2": "foo", 93 | "$3": "b", 94 | "$4": 123 95 | } 96 | ``` 97 | 98 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbEach/jsonbEach.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonbEach extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('jsonb_each()', SQLBuilder.CALLEE) }, 9 | Object: { syntax: this.Syntax('jsonb_each()') }, 10 | }); 11 | } 12 | } 13 | 14 | 15 | module.exports = { 16 | definition: jsonbEach, 17 | description: 'Specifies the PostgreSQL `jsonb_each` Function.', 18 | supportedBy: { 19 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-json.html', 20 | }, 21 | examples: { 22 | String: { 23 | "Basic Usage": function(sql) { 24 | return { 25 | test: function() { 26 | return sql.$select({ 27 | $from: { 28 | jsondata: { 29 | $jsonbEach: JSON.stringify({ 30 | a: 'foo', 31 | b: 'bar' 32 | }) 33 | } 34 | }, 35 | }); 36 | }, 37 | expectedResults: { 38 | sql: 'SELECT * FROM jsonb_each($1) AS jsondata', 39 | values: { 40 | $1: '{"a":"foo","b":"bar"}' 41 | } 42 | } 43 | } 44 | } 45 | }, 46 | Object: { 47 | "Basic Usage": function(sql) { 48 | return { 49 | test: function() { 50 | return sql.$select({ 51 | $from: { 52 | jsondata: { 53 | $jsonbEach: { 54 | $jsonbBuildObject: { 55 | a: 'foo', 56 | b: 123 57 | } 58 | } 59 | } 60 | }, 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT * FROM jsonb_each(jsonb_build_object($1, $2, $3, $4)) AS jsondata', 65 | values: { 66 | $1: 'a', 67 | $2: 'foo', 68 | $3: 'b', 69 | $4: 123 70 | } 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbObjectAgg/jsonbObjectAgg.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonbObjectAgg extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | eachItemOf: { 10 | Primitive: { syntax: this.Syntax(', [ , ... ]') }, 11 | Object: { syntax: this.Syntax(', [ , ... ]') }, 12 | } 13 | } 14 | }); 15 | } 16 | 17 | postBuild(result, type, itemType) { 18 | return 'jsonb_object_agg(' + result + ')'; 19 | } 20 | } 21 | 22 | 23 | module.exports = { 24 | definition: jsonbObjectAgg, 25 | description: 'Specifies the PostgreSQL `jsonb_object_agg` aggregation Function.', 26 | supportedBy: { 27 | PostgreSQL: 'https://www.postgresql.org/docs/10/static/functions-aggregate.html', 28 | }, 29 | examples: { 30 | Object: { 31 | eachItemOf: { 32 | Primitive: { 33 | "Basic Usage": function(sql) { 34 | return { 35 | test: function() { 36 | return sql.$select({ 37 | services: { $jsonbObjectAgg: { '~~servicedata.key': '~~servicedata.value' } }, 38 | $from: { 39 | data: { 40 | $select: { 41 | services: { $jsonbBuildObject: { '~~users_loginservices.service_id': '~~users_loginservices.data' } }, 42 | $from: 'users_loginservices' 43 | } 44 | }, 45 | servicedata: { 46 | $jsonbEach: '~~data.services' 47 | } 48 | } 49 | }); 50 | }, 51 | expectedResults: { 52 | sql: 'SELECT jsonb_object_agg(servicedata.key, servicedata.value) AS services FROM (SELECT jsonb_build_object(users_loginservices.service_id, users_loginservices.data) AS services FROM users_loginservices) AS data, jsonb_each(data.services) AS servicedata', 53 | values: {} 54 | } 55 | } 56 | } 57 | }, 58 | Object: { 59 | "Basic Usage": function(sql) { 60 | return { 61 | test: function() { 62 | return sql.$select({ 63 | jobj: { $jsonbObjectAgg: { '~~jsondata.key': '~~jsondata.value' } }, 64 | $from: { 65 | jsondata: { 66 | $jsonbEach: { 67 | $jsonbBuildObject: { 68 | a: 'foo', 69 | b: 123 70 | } 71 | } 72 | } 73 | } 74 | }); 75 | }, 76 | expectedResults: { 77 | sql: 'SELECT jsonb_object_agg(jsondata.key, jsondata.value) AS jobj FROM jsonb_each(jsonb_build_object($1, $2, $3, $4)) AS jsondata', 78 | values: { 79 | $1: 'a', 80 | $2: 'foo', 81 | $3: 'b', 82 | $4: 123 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbSet/README.md: -------------------------------------------------------------------------------- 1 | # jsonbSet Operator 2 | Specifies the `jsonb_set` function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Object: 10 | 11 | Usage of `jsonbSet` as **Object** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $jsonbSet: { ... } 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | jsonb_set(<$target>, <$path>, <$value>{, [$createMissing]}) 22 | ``` 23 | 24 | **Registered Helpers** 25 | 26 | Name|Required|Public|SQL-Definition|Supported by 27 | :---|:------:|:----:|:-------------|:----------- 28 | [target](./private/target/)|:heavy_check_mark:|*private*|| 29 | [path](./private/path/)|:heavy_check_mark:|*private*|| 30 | [value](./private/value/)|:heavy_check_mark:|*private*|| 31 | [createMissing](./private/createMissing/)|*optional*|*private*|, [$createMissing]| 32 | 33 | :bulb: **Example:** 34 | ```javascript 35 | function() { 36 | return sql.$update({ 37 | $table: 'people', 38 | $set: { 39 | data: { 40 | $jsonbSet: { 41 | $target: '~~data', 42 | $path: '{profile,firstName}', 43 | $value: 'John' 44 | } 45 | } 46 | }, 47 | $where: { 48 | people_id: 456 49 | } 50 | }); 51 | } 52 | 53 | // SQL output 54 | UPDATE 55 | people 56 | SET 57 | data = jsonb_set(data, $1, $2) 58 | WHERE 59 | people_id = $3 60 | 61 | // Values 62 | { 63 | "$1": "{profile,firstName}", 64 | "$2": "\"John\"", 65 | "$3": 456 66 | } 67 | ``` 68 | 69 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/jsonbSet/jsonbSet.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class jsonbSet extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | syntax: this.Syntax('jsonb_set(<$target>, <$path>, <$value>{, [$createMissing]})', SQLBuilder.CALLEE) 10 | } 11 | }); 12 | 13 | //this.$target = new SQLBuilder.SQLPredefined.Expression(sql); 14 | this.$path = new SQLBuilder.SQLPredefined.StringValueParam(sql); 15 | 16 | this.registerPrivateHelper('target'); 17 | this.registerPrivateHelper('value'); 18 | } 19 | } 20 | 21 | 22 | module.exports = { 23 | definition: jsonbSet, 24 | description: 'Specifies the `jsonb_set` function.', 25 | supportedBy: { 26 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE', 27 | }, 28 | examples: { 29 | Object: { 30 | 'Basic Usage': function(sql) { 31 | return { 32 | test: function() { 33 | return sql.$update({ 34 | $table: 'people', 35 | $set: { 36 | data: { 37 | $jsonbSet: { 38 | $target: '~~data', 39 | $path: '{profile,firstName}', 40 | $value: 'John' 41 | } 42 | } 43 | }, 44 | $where: { 45 | people_id: 456 46 | } 47 | }); 48 | }, 49 | expectedResults: { 50 | sql: 'UPDATE people SET data = jsonb_set(data, $1, $2) WHERE people_id = $3', 51 | values: { 52 | $1: '{profile,firstName}', 53 | $2: '"John"', 54 | $3: 456 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/rowToJson/README.md: -------------------------------------------------------------------------------- 1 | # rowToJson Operator 2 | Specifies the PostgreSQL `row_to_json` Function. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/functions-json.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `rowToJson` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $rowToJson: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | row_to_json() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$select({ 28 | profile: { $rowToJson: 'people' }, 29 | $from: 'people' 30 | }); 31 | } 32 | 33 | // SQL output 34 | SELECT 35 | row_to_json(people) AS profile 36 | FROM 37 | people 38 | 39 | // Values 40 | {} 41 | ``` 42 | 43 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/PostgreSQL/rowToJson/rowToJson.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class rowToJson extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('row_to_json()', SQLBuilder.CALLEE) }, 9 | }); 10 | } 11 | } 12 | 13 | 14 | module.exports = { 15 | definition: rowToJson, 16 | description: 'Specifies the PostgreSQL `row_to_json` Function.', 17 | supportedBy: { 18 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/functions-json.html', 19 | }, 20 | examples: { 21 | String: { 22 | "Basic Usage": function(sql) { 23 | return { 24 | test: function() { 25 | return sql.$select({ 26 | profile: { $rowToJson: 'people' }, 27 | $from: 'people' 28 | }); 29 | }, 30 | expectedResults: { 31 | sql: 'SELECT row_to_json(people) AS profile FROM people', 32 | values: {} 33 | } 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /sql/helpers/functions/json/json.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class json extends SQLBuilder.SQLOperator { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Primitive: { syntax: this.Syntax('') }, 9 | Array: { syntax: this.Syntax('') }, 10 | Object: { syntax: this.Syntax('') } 11 | }); 12 | } 13 | 14 | preBuild(query, identifier) { 15 | return JSON.stringify(query); 16 | } 17 | } 18 | 19 | 20 | module.exports = { 21 | definition: json, 22 | description: 'Specifies a generic JSON Helper that will stringify the current value assigned.', 23 | supportedBy: { 24 | MySQL: '', 25 | MariaDB: '', 26 | PostgreSQL: '', 27 | SQLite: '', 28 | Oracle: '', 29 | SQLServer: '' 30 | }, 31 | examples: { 32 | Primitive: { 33 | "Basic Usage": function(sql) { 34 | return { 35 | test: function() { 36 | return sql.$insert({ 37 | $table: 'people', 38 | $documents: { 39 | first_name: 'John', 40 | last_name: 'Doe', 41 | json_str: { $json: 'Stringvalue' }, 42 | json_bool: { $json: true }, 43 | json_num: { $json: 88} 44 | } 45 | }); 46 | }, 47 | expectedResults: { 48 | sql: 'INSERT INTO people (first_name, last_name, json_str, json_bool, json_num) VALUES ($1, $2, $3, $4, $5)', 49 | values: { 50 | $1: 'John', 51 | $2: 'Doe', 52 | $3: '"Stringvalue"', 53 | $4: 'true', 54 | $5: '88' 55 | } 56 | } 57 | } 58 | } 59 | }, 60 | Object: { 61 | "Basic Usage": function(sql) { 62 | return { 63 | test: function() { 64 | return sql.$insert({ 65 | $table: 'people', 66 | $documents: { 67 | first_name: 'John', 68 | last_name: 'Doe', 69 | hobbies: { 70 | $json: { 71 | football: 'beginner', 72 | basketball: 'good', 73 | swimming: false 74 | } 75 | } 76 | } 77 | }); 78 | }, 79 | expectedResults: { 80 | sql: 'INSERT INTO people (first_name, last_name, hobbies) VALUES ($1, $2, $3)', 81 | values: { 82 | $1: 'John', 83 | $2: 'Doe', 84 | $3: '{"football":"beginner","basketball":"good","swimming":false}' 85 | } 86 | } 87 | } 88 | } 89 | }, 90 | Array: { 91 | "Basic Usage": function(sql) { 92 | return { 93 | test: function() { 94 | return sql.$insert({ 95 | $table: 'people', 96 | $documents: { 97 | first_name: 'John', 98 | last_name: 'Doe', 99 | hobbies: { $json: ['football', 'basketball'] } 100 | } 101 | }); 102 | }, 103 | expectedResults: { 104 | sql: 'INSERT INTO people (first_name, last_name, hobbies) VALUES ($1, $2, $3)', 105 | values: { 106 | $1: 'John', 107 | $2: 'Doe', 108 | $3: '["football","basketball"]' 109 | } 110 | } 111 | } 112 | } 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /sql/helpers/functions/string/concat/README.md: -------------------------------------------------------------------------------- 1 | # concat Helper 2 | Specifies the `CONCAT` function. 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Array: 15 | 16 | Usage of `concat` as **Array** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $concat: [ ... ] 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | CONCAT([ , ... ]) 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.$select({ 33 | people_name: { $concat: ['~~first_name', ' ', '~~last_name'] }, 34 | $from: 'people' 35 | }); 36 | } 37 | 38 | // SQL output 39 | SELECT 40 | CONCAT(first_name, $1, last_name) AS people_name 41 | FROM 42 | people 43 | 44 | // Values 45 | { 46 | "$1": " " 47 | } 48 | ``` 49 | 50 | ## Further Examples 51 | 52 | :bulb: **Usage of CONCAT as Function** 53 | ```javascript 54 | function() { 55 | return sql.$select({ 56 | people_name: sql.concat('~~first_name', ' ', '~~last_name'), 57 | $from: 'people' 58 | }); 59 | } 60 | 61 | // SQL output 62 | SELECT 63 | CONCAT(first_name, $1, last_name) AS people_name 64 | FROM 65 | people 66 | 67 | // Values 68 | { 69 | "$1": " " 70 | } 71 | ``` 72 | 73 | :bulb: **Usage with INLINE-SQL** 74 | ```javascript 75 | function() { 76 | return sql.$select({ 77 | people_name: { $concat: ["__:COALESCE(first_name, '')", ' ', '~~last_name'] }, 78 | $from: 'people' 79 | }); 80 | } 81 | 82 | // SQL output 83 | SELECT 84 | CONCAT(COALESCE(first_name, ''), $1, last_name) AS people_name 85 | FROM 86 | people 87 | 88 | // Values 89 | { 90 | "$1": " " 91 | } 92 | ``` 93 | 94 | :bulb: **Support for SQLLite** 95 | ```javascript 96 | function() { 97 | return sql.$select({ 98 | greeting: { $concat: ['Hello', ' ', '~~first_name'] }, 99 | $from: 'people' 100 | }); 101 | } 102 | 103 | // SQL output 104 | SELECT 105 | $1 || $2 || first_name AS greeting 106 | FROM 107 | people 108 | 109 | // Values 110 | { 111 | "$1": "Hello", 112 | "$2": " " 113 | } 114 | ``` 115 | 116 | -------------------------------------------------------------------------------- /sql/helpers/functions/systeminformations/current_database/README.md: -------------------------------------------------------------------------------- 1 | # currentDatabase Helper 2 | Specifies the `current_database()` function to use with PostgreSQL. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/11/functions-info.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Boolean: 10 | 11 | Usage of `currentDatabase` as **Boolean** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $currentDatabase: true | false 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | current_database() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.build({ 28 | $select: { 29 | db: { $currentDatabase: true } 30 | } 31 | }); 32 | } 33 | 34 | // SQL output 35 | SELECT 36 | current_database() AS db 37 | 38 | // Values 39 | {} 40 | ``` 41 | 42 | ## as Number: 43 | 44 | Usage of `currentDatabase` as **Number** with the following Syntax: 45 | 46 | **Syntax:** 47 | 48 | ```javascript 49 | $currentDatabase: < Number > 50 | ``` 51 | 52 | **SQL-Definition:** 53 | ```javascript 54 | current_database() 55 | ``` 56 | 57 | :bulb: **Example:** 58 | ```javascript 59 | function() { 60 | return sql.build({ 61 | $select: { 62 | db: { $currentDatabase: 1 } 63 | } 64 | }); 65 | } 66 | 67 | // SQL output 68 | SELECT 69 | current_database() AS db 70 | 71 | // Values 72 | {} 73 | ``` 74 | 75 | ## Further Examples 76 | 77 | :bulb: **Usage as Function** 78 | ```javascript 79 | function() { 80 | return sql.build({ 81 | $select: { 82 | db: sql.currentDatabase() 83 | } 84 | }); 85 | } 86 | 87 | // SQL output 88 | SELECT 89 | current_database() AS db 90 | 91 | // Values 92 | {} 93 | ``` 94 | 95 | -------------------------------------------------------------------------------- /sql/helpers/functions/systeminformations/current_database/current_database.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class currentDatabase extends SQLBuilder.SQLHelper { 4 | constructor(sql) { 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { syntax: this.Syntax('current_database()', SQLBuilder.CALLEE) }, 9 | Number: { syntax: this.Syntax('current_database()') } 10 | }); 11 | } 12 | } 13 | 14 | module.exports = { 15 | definition: currentDatabase, 16 | description: 'Specifies the `current_database()` function to use with PostgreSQL.', 17 | supportedBy: { 18 | PostgreSQL: 'https://www.postgresql.org/docs/11/functions-info.html', 19 | }, 20 | examples: { 21 | Boolean: { 22 | "Basic Usage": function(sql) { 23 | return { 24 | test: function() { 25 | return sql.build({ 26 | $select: { 27 | db: { $currentDatabase: true } 28 | } 29 | }); 30 | }, 31 | expectedResults: { 32 | sql: 'SELECT current_database() AS db', 33 | values: {} 34 | } 35 | } 36 | }, 37 | "Usage as Function": function(sql) { 38 | return { 39 | test: function() { 40 | return sql.build({ 41 | $select: { 42 | db: sql.currentDatabase() 43 | } 44 | }); 45 | }, 46 | expectedResults: { 47 | sql: 'SELECT current_database() AS db', 48 | values: {} 49 | } 50 | } 51 | } 52 | }, 53 | Number: { 54 | "Basic Usage": function(sql) { 55 | return { 56 | test: function() { 57 | return sql.build({ 58 | $select: { 59 | db: { $currentDatabase: 1 } 60 | } 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT current_database() AS db', 65 | values: {} 66 | } 67 | } 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /sql/helpers/functions/systeminformations/current_schema/README.md: -------------------------------------------------------------------------------- 1 | # currentSchema Helper 2 | Specifies the `current_schema` function to use with PostgreSQL. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/11/functions-info.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Boolean: 10 | 11 | Usage of `currentSchema` as **Boolean** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $currentSchema: true | false 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | current_schema 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.build({ 28 | $select: { 29 | schema: { $currentSchema: true } 30 | } 31 | }); 32 | } 33 | 34 | // SQL output 35 | SELECT 36 | current_schema AS schema 37 | 38 | // Values 39 | {} 40 | ``` 41 | 42 | ## as Number: 43 | 44 | Usage of `currentSchema` as **Number** with the following Syntax: 45 | 46 | **Syntax:** 47 | 48 | ```javascript 49 | $currentSchema: < Number > 50 | ``` 51 | 52 | **SQL-Definition:** 53 | ```javascript 54 | current_schema 55 | ``` 56 | 57 | :bulb: **Example:** 58 | ```javascript 59 | function() { 60 | return sql.build({ 61 | $select: { 62 | schema: { $currentSchema: 1 } 63 | } 64 | }); 65 | } 66 | 67 | // SQL output 68 | SELECT 69 | current_schema AS schema 70 | 71 | // Values 72 | {} 73 | ``` 74 | 75 | ## Further Examples 76 | 77 | :bulb: **Usage as Function** 78 | ```javascript 79 | function() { 80 | return sql.build({ 81 | $select: { 82 | schema: sql.currentSchema() 83 | } 84 | }); 85 | } 86 | 87 | // SQL output 88 | SELECT 89 | current_schema AS schema 90 | 91 | // Values 92 | {} 93 | ``` 94 | 95 | -------------------------------------------------------------------------------- /sql/helpers/functions/systeminformations/current_schema/current_schema.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class currentSchema extends SQLBuilder.SQLHelper { 4 | constructor(sql) { 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { syntax: this.Syntax('current_schema', SQLBuilder.CALLEE) }, 9 | Number: { syntax: this.Syntax('current_schema') } 10 | }); 11 | } 12 | } 13 | 14 | module.exports = { 15 | definition: currentSchema, 16 | description: 'Specifies the `current_schema` function to use with PostgreSQL.', 17 | supportedBy: { 18 | PostgreSQL: 'https://www.postgresql.org/docs/11/functions-info.html', 19 | }, 20 | examples: { 21 | Boolean: { 22 | "Basic Usage": function(sql) { 23 | return { 24 | test: function() { 25 | return sql.build({ 26 | $select: { 27 | schema: { $currentSchema: true } 28 | } 29 | }); 30 | }, 31 | expectedResults: { 32 | sql: 'SELECT current_schema AS schema', 33 | values: {} 34 | } 35 | } 36 | }, 37 | "Usage as Function": function(sql) { 38 | return { 39 | test: function() { 40 | return sql.build({ 41 | $select: { 42 | schema: sql.currentSchema() 43 | } 44 | }); 45 | }, 46 | expectedResults: { 47 | sql: 'SELECT current_schema AS schema', 48 | values: {} 49 | } 50 | } 51 | } 52 | }, 53 | Number: { 54 | "Basic Usage": function(sql) { 55 | return { 56 | test: function() { 57 | return sql.build({ 58 | $select: { 59 | schema: { $currentSchema: 1 } 60 | } 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT current_schema AS schema', 65 | values: {} 66 | } 67 | } 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /sql/helpers/functions/systeminformations/current_user/README.md: -------------------------------------------------------------------------------- 1 | # currentUser Helper 2 | Specifies the `current_user` function to use with PostgreSQL. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/11/functions-info.html) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Boolean: 10 | 11 | Usage of `currentUser` as **Boolean** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $currentUser: true | false 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | current_user 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.build({ 28 | $select: { 29 | username: { $currentUser: true } 30 | } 31 | }); 32 | } 33 | 34 | // SQL output 35 | SELECT 36 | current_user AS username 37 | 38 | // Values 39 | {} 40 | ``` 41 | 42 | ## as Number: 43 | 44 | Usage of `currentUser` as **Number** with the following Syntax: 45 | 46 | **Syntax:** 47 | 48 | ```javascript 49 | $currentUser: < Number > 50 | ``` 51 | 52 | **SQL-Definition:** 53 | ```javascript 54 | current_user 55 | ``` 56 | 57 | :bulb: **Example:** 58 | ```javascript 59 | function() { 60 | return sql.build({ 61 | $select: { 62 | username: { $currentUser: 1 } 63 | } 64 | }); 65 | } 66 | 67 | // SQL output 68 | SELECT 69 | current_user AS username 70 | 71 | // Values 72 | {} 73 | ``` 74 | 75 | ## Further Examples 76 | 77 | :bulb: **Usage as Function** 78 | ```javascript 79 | function() { 80 | return sql.build({ 81 | $select: { 82 | username: sql.currentUser() 83 | } 84 | }); 85 | } 86 | 87 | // SQL output 88 | SELECT 89 | current_user AS username 90 | 91 | // Values 92 | {} 93 | ``` 94 | 95 | -------------------------------------------------------------------------------- /sql/helpers/functions/systeminformations/current_user/current_user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class currentUser extends SQLBuilder.SQLHelper { 4 | constructor(sql) { 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { syntax: this.Syntax('current_user', SQLBuilder.CALLEE) }, 9 | Number: { syntax: this.Syntax('current_user') } 10 | }); 11 | } 12 | } 13 | 14 | module.exports = { 15 | definition: currentUser, 16 | description: 'Specifies the `current_user` function to use with PostgreSQL.', 17 | supportedBy: { 18 | PostgreSQL: 'https://www.postgresql.org/docs/11/functions-info.html', 19 | }, 20 | examples: { 21 | Boolean: { 22 | "Basic Usage": function(sql) { 23 | return { 24 | test: function() { 25 | return sql.build({ 26 | $select: { 27 | username: { $currentUser: true } 28 | } 29 | }); 30 | }, 31 | expectedResults: { 32 | sql: 'SELECT current_user AS username', 33 | values: {} 34 | } 35 | } 36 | }, 37 | "Usage as Function": function(sql) { 38 | return { 39 | test: function() { 40 | return sql.build({ 41 | $select: { 42 | username: sql.currentUser() 43 | } 44 | }); 45 | }, 46 | expectedResults: { 47 | sql: 'SELECT current_user AS username', 48 | values: {} 49 | } 50 | } 51 | } 52 | }, 53 | Number: { 54 | "Basic Usage": function(sql) { 55 | return { 56 | test: function() { 57 | return sql.build({ 58 | $select: { 59 | username: { $currentUser: 1 } 60 | } 61 | }); 62 | }, 63 | expectedResults: { 64 | sql: 'SELECT current_user AS username', 65 | values: {} 66 | } 67 | } 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /sql/helpers/logical/not/README.md: -------------------------------------------------------------------------------- 1 | # not Helper 2 | Specifies the logical `NOT` Operator as Helper. 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `not` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $not: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | NOT 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | $from: 'people', 35 | $where: { 36 | first_name: { $not: { $startsWith: 'D' } } 37 | } 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | SELECT 44 | * 45 | FROM 46 | people 47 | WHERE 48 | first_name NOT LIKE $1 49 | 50 | // Values 51 | { 52 | "$1": "D%" 53 | } 54 | ``` 55 | 56 | -------------------------------------------------------------------------------- /sql/helpers/logical/not/not.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class not extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { syntax: this.Syntax('NOT ', SQLBuilder.CALLEE) } 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: not, 15 | description: 'Specifies the logical `NOT` Operator as Helper.', 16 | supportedBy: { 17 | MySQL: '', 18 | MariaDB: '', 19 | PostgreSQL: '', 20 | SQLite: '', 21 | Oracle: '', 22 | SQLServer: '' 23 | }, 24 | examples: { 25 | Object: { 26 | "Basic Usage": function(sql) { 27 | return { 28 | test: function(){ 29 | return sql.build({ 30 | $select: { 31 | $from: 'people', 32 | $where: { 33 | first_name: { $not: { $startsWith: 'D' } } 34 | } 35 | } 36 | }); 37 | }, 38 | expectedResults: { 39 | sql: 'SELECT * FROM people WHERE first_name NOT LIKE $1', 40 | values: { 41 | $1: 'D%' 42 | } 43 | } 44 | } 45 | } 46 | }, 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /sql/helpers/misc/$/$.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class $ extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | syntax: this.Syntax('[ ... ]') 10 | }, 11 | Function: { 12 | syntax: this.Syntax('[ ... ]') 13 | } 14 | }); 15 | } 16 | } 17 | 18 | module.exports = { 19 | definition: $, 20 | description: 'Specifies a Helper that builds `any` given object-expression', 21 | supportedBy: { 22 | MySQL: '', 23 | MariaDB: '', 24 | PostgreSQL: '', 25 | SQLite: '', 26 | Oracle: '', 27 | SQLServer: '' 28 | }, 29 | examples: { 30 | Object: { 31 | 'Basic Usage': function(sql) { 32 | return { 33 | test: function(){ 34 | return sql.build({ 35 | $select: { 36 | city: 1, 37 | total_salary_by_city: sql.sum('salary'), 38 | $from: 'people', 39 | $groupBy: 'city', 40 | $having: { 41 | $and: [ 42 | { $: { $sum: 'salary', $gt: 450000 } }, 43 | { $: { $sum: 'salary', $lt: 990000 } } 44 | ] 45 | } 46 | } 47 | }); 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT city, SUM(salary) AS total_salary_by_city FROM people GROUP BY city HAVING SUM(salary) > $1 AND SUM(salary) < $2', 51 | values:{ 52 | $1: 450000, 53 | $2: 990000 54 | } 55 | } 56 | } 57 | } 58 | }, 59 | Function: { 60 | 'Basic Usage': function(sql) { 61 | return { 62 | test: function(){ 63 | return sql.build({ 64 | $select: { 65 | city: 1, 66 | total_salary_by_city: sql.sum('salary'), 67 | $from: 'people', 68 | $groupBy: 'city', 69 | $having: { 70 | $and: [ 71 | { $: { $sum: 'salary', $gt: 450000 } }, 72 | { $: { $sum: 'salary', $lt: 990000 } } 73 | ] 74 | } 75 | } 76 | }); 77 | }, 78 | expectedResults: { 79 | sql: 'SELECT city, SUM(salary) AS total_salary_by_city FROM people GROUP BY city HAVING SUM(salary) > $1 AND SUM(salary) < $2', 80 | values:{ 81 | $1: 450000, 82 | $2: 990000 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /sql/helpers/misc/$/README.md: -------------------------------------------------------------------------------- 1 | # Helper 2 | Specifies a Helper that builds `any` given object-expression 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | [ ... ] 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | city: 1, 35 | total_salary_by_city: sql.sum('salary'), 36 | $from: 'people', 37 | $groupBy: 'city', 38 | $having: { 39 | $and: [ 40 | { $: { $sum: 'salary', $gt: 450000 } }, 41 | { $: { $sum: 'salary', $lt: 990000 } } 42 | ] 43 | } 44 | } 45 | }); 46 | } 47 | 48 | // SQL output 49 | SELECT 50 | city, 51 | SUM(salary) AS total_salary_by_city 52 | FROM 53 | people 54 | GROUP BY 55 | city 56 | HAVING 57 | SUM(salary) > $1 58 | AND SUM(salary) < $2 59 | 60 | // Values 61 | { 62 | "$1": 450000, 63 | "$2": 990000 64 | } 65 | ``` 66 | 67 | ## as Function: 68 | 69 | Usage of `` as **Function** with the following Syntax: 70 | 71 | **Syntax:** 72 | 73 | ```javascript 74 | $: sql.([params]) 75 | ``` 76 | 77 | **SQL-Definition:** 78 | ```javascript 79 | [ ... ] 80 | ``` 81 | 82 | :bulb: **Example:** 83 | ```javascript 84 | function() { 85 | return sql.build({ 86 | $select: { 87 | city: 1, 88 | total_salary_by_city: sql.sum('salary'), 89 | $from: 'people', 90 | $groupBy: 'city', 91 | $having: { 92 | $and: [ 93 | { $: { $sum: 'salary', $gt: 450000 } }, 94 | { $: { $sum: 'salary', $lt: 990000 } } 95 | ] 96 | } 97 | } 98 | }); 99 | } 100 | 101 | // SQL output 102 | SELECT 103 | city, 104 | SUM(salary) AS total_salary_by_city 105 | FROM 106 | people 107 | GROUP BY 108 | city 109 | HAVING 110 | SUM(salary) > $1 111 | AND SUM(salary) < $2 112 | 113 | // Values 114 | { 115 | "$1": 450000, 116 | "$2": 990000 117 | } 118 | ``` 119 | 120 | -------------------------------------------------------------------------------- /sql/helpers/misc/__/README.md: -------------------------------------------------------------------------------- 1 | # __ Helper 2 | Specifies an Operator for writing INLINE-SQL. 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `__` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | __: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.$select({ 33 | max_age: { __: 'MAX(age) + 1' }, 34 | $from: 'people' 35 | }); 36 | } 37 | 38 | // SQL output 39 | SELECT 40 | MAX(age) + 1 AS max_age 41 | FROM 42 | people 43 | 44 | // Values 45 | {} 46 | ``` 47 | 48 | ## Further Examples 49 | 50 | :bulb: **Only INLINE-SQL test** 51 | ```javascript 52 | function() { 53 | return sql.build({ 54 | __: 'SELECT MAX(age) FROM people' 55 | }); 56 | } 57 | 58 | // SQL output 59 | SELECT 60 | MAX(age) 61 | FROM 62 | people 63 | 64 | // Values 65 | {} 66 | ``` 67 | 68 | -------------------------------------------------------------------------------- /sql/helpers/misc/__/__.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class __ extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('') } 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: __, 15 | description: `Specifies an Operator for writing INLINE-SQL.`, 16 | supportedBy: { 17 | MySQL: '', 18 | MariaDB: '', 19 | PostgreSQL: '', 20 | SQLite: '', 21 | Oracle: '', 22 | SQLServer: '' 23 | }, 24 | examples: { 25 | String: { 26 | "Basic Usage": function(sql) { 27 | return { 28 | test: function(){ 29 | return sql.$select({ 30 | max_age: { __: 'MAX(age) + 1' }, 31 | $from: 'people' 32 | }); 33 | }, 34 | expectedResults: { 35 | sql: 'SELECT MAX(age) + 1 AS max_age FROM people', 36 | values: {} 37 | } 38 | } 39 | }, 40 | "Only INLINE-SQL test": function(sql) { 41 | return { 42 | test: function(){ 43 | return sql.build({ 44 | __: 'SELECT MAX(age) FROM people' 45 | }); 46 | }, 47 | expectedResults: { 48 | sql: 'SELECT MAX(age) FROM people', 49 | values: {} 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /sql/helpers/misc/anyExpr/README.md: -------------------------------------------------------------------------------- 1 | # anyExpr Helper 2 | Specifies a Helper that builds `any` given object-expression 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `anyExpr` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $anyExpr: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | [ ... ] 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | city: 1, 35 | total_salary_by_city: sql.sum('salary'), 36 | $from: 'people', 37 | $groupBy: 'city', 38 | $having: { 39 | $and: [ 40 | { $anyExpr: { $sum: 'salary', $gt: 450000 } }, 41 | { $anyExpr: { $sum: 'salary', $lt: 990000 } } 42 | ] 43 | } 44 | } 45 | }); 46 | } 47 | 48 | // SQL output 49 | SELECT 50 | city, 51 | SUM(salary) AS total_salary_by_city 52 | FROM 53 | people 54 | GROUP BY 55 | city 56 | HAVING 57 | SUM(salary) > $1 58 | AND SUM(salary) < $2 59 | 60 | // Values 61 | { 62 | "$1": 450000, 63 | "$2": 990000 64 | } 65 | ``` 66 | 67 | -------------------------------------------------------------------------------- /sql/helpers/misc/anyExpr/anyExpr.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class anyExpr extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | syntax: this.Syntax('[ ... ]') 10 | } 11 | }); 12 | } 13 | } 14 | 15 | module.exports = { 16 | definition: anyExpr, 17 | description: 'Specifies a Helper that builds `any` given object-expression', 18 | supportedBy: { 19 | MySQL: '', 20 | MariaDB: '', 21 | PostgreSQL: '', 22 | SQLite: '', 23 | Oracle: '', 24 | SQLServer: '' 25 | }, 26 | examples: { 27 | Object: { 28 | 'Basic Usage': function(sql) { 29 | return { 30 | test: function(){ 31 | return sql.build({ 32 | $select: { 33 | city: 1, 34 | total_salary_by_city: sql.sum('salary'), 35 | $from: 'people', 36 | $groupBy: 'city', 37 | $having: { 38 | $and: [ 39 | { $anyExpr: { $sum: 'salary', $gt: 450000 } }, 40 | { $anyExpr: { $sum: 'salary', $lt: 990000 } } 41 | ] 42 | } 43 | } 44 | }); 45 | }, 46 | expectedResults: { 47 | sql: 'SELECT city, SUM(salary) AS total_salary_by_city FROM people GROUP BY city HAVING SUM(salary) > $1 AND SUM(salary) < $2', 48 | values:{ 49 | $1: 450000, 50 | $2: 990000 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /sql/helpers/misc/i/README.md: -------------------------------------------------------------------------------- 1 | # i Helper 2 | Specifies an Identifier for a Table, Column, Index, etc. as Helper where normally a String value was expected. 3 | 4 | #### Supported by 5 | - MySQL 6 | - MariaDB 7 | - PostgreSQL 8 | - SQLite 9 | - Oracle 10 | - SQLServer 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as String: 15 | 16 | Usage of `i` as **String** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $i: < String > 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | $from: 'people', 35 | $where: { 36 | first_name: { $eq: { $i: 'last_name' } } 37 | } 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | SELECT 44 | * 45 | FROM 46 | people 47 | WHERE 48 | first_name = last_name 49 | 50 | // Values 51 | {} 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /sql/helpers/misc/i/i.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class i extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('', SQLBuilder.CALLEE) }, 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: i, 15 | description: 'Specifies an Identifier for a Table, Column, Index, etc. as Helper where normally a String value was expected.', 16 | supportedBy: { 17 | MySQL: '', 18 | MariaDB: '', 19 | PostgreSQL: '', 20 | SQLite: '', 21 | Oracle: '', 22 | SQLServer: '' 23 | }, 24 | examples: { 25 | String: { 26 | "Basic Usage": function(sql){ 27 | return { 28 | test: function(){ 29 | return sql.build({ 30 | $select: { 31 | $from: 'people', 32 | $where: { 33 | first_name: { $eq: { $i: 'last_name' } } 34 | } 35 | } 36 | }); 37 | }, 38 | expectedResults: { 39 | sql: 'SELECT * FROM people WHERE first_name = last_name', 40 | values: {} 41 | } 42 | } 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /sql/helpers/queries/excluded/README.md: -------------------------------------------------------------------------------- 1 | # excluded Helper 2 | Specifies the special `EXCLUDED` table for the `ON CONFLICT ... DO UPDATE` clause. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `excluded` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $excluded: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | EXCLUDED. 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$insert({ 28 | $table: 'people', 29 | $documents: { 30 | staff_no: 1, 31 | first_name: 'John', 32 | last_name: 'Doe', 33 | age: 40 34 | }, 35 | $onConflict: { 36 | $onConstraint: 'pk_people', 37 | $doUpdateSet: { 38 | staff_no: sql.select({ new_stuff_no: { $: { $max: 'staff_no', $add: 1 } } }, { 39 | $from: 'people' 40 | }), 41 | first_name: { $excluded: 'first_name' }, 42 | last_name: sql.excluded('last_name') 43 | } 44 | } 45 | }); 46 | } 47 | 48 | // SQL output 49 | INSERT INTO 50 | people (staff_no, first_name, last_name, age) 51 | VALUES 52 | ($1, $2, $3, $4) ON CONFLICT ON CONSTRAINT pk_people DO 53 | UPDATE 54 | SET 55 | staff_no = ( 56 | SELECT 57 | MAX(staff_no) + $5 AS new_stuff_no 58 | FROM 59 | people 60 | ), 61 | first_name = EXCLUDED.first_name, 62 | last_name = EXCLUDED.last_name 63 | 64 | // Values 65 | { 66 | "$1": 1, 67 | "$2": "John", 68 | "$3": "Doe", 69 | "$4": 40, 70 | "$5": 1 71 | } 72 | ``` 73 | 74 | -------------------------------------------------------------------------------- /sql/helpers/queries/excluded/excluded.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class excluded extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('EXCLUDED.', SQLBuilder.CALLEE) }, 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: excluded, 15 | description: 'Specifies the special `EXCLUDED` table for the `ON CONFLICT ... DO UPDATE` clause.', 16 | supportedBy: { 17 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT' 18 | }, 19 | examples: { 20 | String: { 21 | "Basic Usage": function(sql) { 22 | return { 23 | test: function(){ 24 | return sql.$insert({ $table: 'people', 25 | $documents: { 26 | staff_no: 1, 27 | first_name: 'John', 28 | last_name: 'Doe', 29 | age: 40 30 | }, 31 | $onConflict: { 32 | $onConstraint: 'pk_people', 33 | $doUpdateSet: { 34 | staff_no: sql.select({ new_stuff_no: { $: { $max: 'staff_no', $add: 1 } } }, { 35 | $from: 'people' 36 | }), 37 | first_name: { $excluded: 'first_name' }, 38 | last_name: sql.excluded('last_name') 39 | } 40 | } 41 | }); 42 | }, 43 | expectedResults: { 44 | sql: 'INSERT INTO people (staff_no, first_name, last_name, age) VALUES ($1, $2, $3, $4) ON CONFLICT ON CONSTRAINT pk_people DO UPDATE SET staff_no = (SELECT MAX(staff_no) + $5 AS new_stuff_no FROM people), first_name = EXCLUDED.first_name, last_name = EXCLUDED.last_name', 45 | values:{ 46 | $1: 1, 47 | $2: 'John', 48 | $3: 'Doe', 49 | $4: 40, 50 | $5: 1 51 | } 52 | } 53 | } 54 | } 55 | }, 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /sql/helpers/queries/join/private/.joinHelper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sqlJoin = { 4 | left: 'LEFT JOIN' 5 | } 6 | class joinHelper extends SQLBuilder.SQLHelper { 7 | constructor(sql, name){ 8 | super(sql); 9 | let aliasKeyword = sql.isOracle() ? ' ' : ' AS '; 10 | this.Types({ 11 | String: { syntax: this.Syntax('' + aliasKeyword + '') }, 12 | Object: { syntax: this.Syntax('' + aliasKeyword + '') }, 13 | Function: { syntax: this.Syntax('' + aliasKeyword + '') }, 14 | }); 15 | 16 | // set the name, otherwise all classes that inherit will named "joinHelper" 17 | let helperName = '$' + name; 18 | this.__name__ = helperName; 19 | } 20 | } 21 | 22 | module.exports = joinHelper; 23 | -------------------------------------------------------------------------------- /sql/helpers/queries/limit/README.md: -------------------------------------------------------------------------------- 1 | # limit Helper 2 | Specifies the `LIMIT` clause for the `SELECT` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/limit/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html#SQL-LIMIT) 8 | - [SQLite](https://sqlite.org/lang_select.html#limitoffset) 9 | 10 | # Allowed Types and Usage 11 | 12 | ## as Number: 13 | 14 | Usage of `limit` as **Number** with the following Syntax: 15 | 16 | **Syntax:** 17 | 18 | ```javascript 19 | $limit: < Number > 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```javascript 24 | 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $limit: 10 34 | } 35 | }); 36 | 37 | return query; 38 | } 39 | 40 | // SQL output 41 | SELECT 42 | * 43 | FROM 44 | people 45 | LIMIT 46 | $1 47 | 48 | // Values 49 | { 50 | "$1": 10 51 | } 52 | ``` 53 | 54 | ## as String: 55 | 56 | The usage of `limit` as **String** is restricted to the following values: 57 | - ALL 58 | 59 | #### as String with value **ALL**: 60 | **Syntax:** 61 | 62 | ```javascript 63 | $limit: 'ALL' 64 | ``` 65 | 66 | **SQL-Definition:** 67 | ```javascript 68 | 69 | ``` 70 | 71 | :bulb: **Example:** 72 | ```javascript 73 | function() { 74 | let query = sql.build({ 75 | $select: { 76 | $from: 'people', 77 | $limit: 'ALL' 78 | } 79 | }); 80 | return query; 81 | } 82 | 83 | // SQL output 84 | SELECT 85 | * 86 | FROM 87 | people 88 | LIMIT 89 | ALL 90 | 91 | // Values 92 | {} 93 | ``` 94 | ## Further Examples 95 | 96 | :bulb: **MySQL turns $limit: 'ALL' to LIMIT 9007199254740991** 97 | ```javascript 98 | function() { 99 | let query = sql.build({ 100 | $select: { 101 | $from: 'people', 102 | $limit: 'ALL' 103 | } 104 | }); 105 | return query; 106 | } 107 | 108 | // SQL output 109 | SELECT 110 | * 111 | FROM 112 | people 113 | LIMIT 114 | 9007199254740991 115 | 116 | // Values 117 | {} 118 | ``` 119 | 120 | :bulb: **SQLite turns $limit: 'ALL' to LIMIT -1** 121 | ```javascript 122 | function() { 123 | let query = sql.build({ 124 | $select: { 125 | $from: 'people', 126 | $limit: 'ALL' 127 | } 128 | }); 129 | return query; 130 | } 131 | 132 | // SQL output 133 | SELECT 134 | * 135 | FROM 136 | people 137 | LIMIT 138 | -1 139 | 140 | // Values 141 | {} 142 | ``` 143 | 144 | -------------------------------------------------------------------------------- /sql/helpers/queries/offset/README.md: -------------------------------------------------------------------------------- 1 | # offset Helper 2 | Specifies the `OFFSET` clause for the `SELECT` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/limit/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html#SQL-LIMIT) 8 | - [SQLite](https://sqlite.org/lang_select.html#limitoffset) 9 | 10 | # Allowed Types and Usage 11 | 12 | ## as Number: 13 | 14 | Usage of `offset` as **Number** with the following Syntax: 15 | 16 | **Syntax:** 17 | 18 | ```javascript 19 | $offset: < Number > 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```javascript 24 | 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $limit: 10, 34 | $offset: 100 35 | } 36 | }); 37 | 38 | return query; 39 | } 40 | 41 | // SQL output 42 | SELECT 43 | * 44 | FROM 45 | people 46 | LIMIT 47 | $1 OFFSET $2 48 | 49 | // Values 50 | { 51 | "$1": 10, 52 | "$2": 100 53 | } 54 | ``` 55 | 56 | -------------------------------------------------------------------------------- /sql/helpers/queries/offset/offset.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class offset extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Number: { syntax: this.Syntax('') }, 9 | }); 10 | } 11 | 12 | link(query){ 13 | // check for LIMIT and OFFSET 14 | // can't use OFFSET WITHOUT LIMIT 15 | if (this.isMySQL() || this.isMariaDB()) { 16 | if (query.$offset && !query.$limit) { 17 | throw new Error(`Error. Can't use '$offset' without '$limit'.`); 18 | } 19 | } 20 | } 21 | } 22 | 23 | module.exports = { 24 | definition: offset, 25 | description: 'Specifies the `OFFSET` clause for the `SELECT` Statement.', 26 | supportedBy: { 27 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 28 | MariaDB: 'https://mariadb.com/kb/en/library/limit/', 29 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html#SQL-LIMIT', 30 | SQLite: 'https://sqlite.org/lang_select.html#limitoffset' 31 | }, 32 | examples: { 33 | Number: { 34 | 'Basic Usage': function(sql) { 35 | return { 36 | test: function() { 37 | let query = sql.build({ 38 | $select: { 39 | $from: 'people', 40 | $limit: 10, 41 | $offset: 100 42 | } 43 | }); 44 | 45 | return query; 46 | }, 47 | expectedResults: { 48 | sql: 'SELECT * FROM people LIMIT $1 OFFSET $2', 49 | values: { 50 | $1: 10, 51 | $2: 100 52 | } 53 | } 54 | } 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /sql/helpers/queries/orFetch/README.md: -------------------------------------------------------------------------------- 1 | # top Helper 2 | Specifies the `FETCH` clause for the `SELECT` Statement for Oracle db only. 3 | 4 | #### Supported by 5 | - [Oracle](https://www.oracletutorial.com/oracle-basics/oracle-fetch/) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Number: 10 | 11 | Usage of `ssFetch` as **Number** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $orFetch: 10 // it will fetch 10 rows only 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```sql 21 | FETCH NEXT 15 ROWS ONLY 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | let query = sql.build({ 28 | $select: { 29 | $orFetch: 10, 30 | $from: 'people' 31 | } 32 | }); 33 | 34 | return query; 35 | } 36 | 37 | // SQL output 38 | SELECT 39 | * 40 | FROM 41 | people 42 | FETCH NEXT 10 ROWS ONLY 43 | 44 | // Values 45 | {} 46 | ``` 47 | -------------------------------------------------------------------------------- /sql/helpers/queries/orFetch/orFetch.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class orFetch extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Number: { 9 | syntax: this.Syntax('', this.syntaxHook) 10 | } 11 | }); 12 | } 13 | 14 | syntaxHook(query, idententifier) { 15 | return `FETCH NEXT ${query} ROWS ONLY`; 16 | } 17 | } 18 | 19 | module.exports = { 20 | definition: orFetch, 21 | description: 'Specifies the `FETCH NEXT ${query} ROWS ONLY` clause for the `SELECT` Statement.', 22 | supportedBy: { 23 | Oracle: 'https://www.oracletutorial.com/oracle-basics/oracle-fetch/' 24 | }, 25 | examples: { 26 | Number: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $orFetch: 10, 33 | $from: 'Products', 34 | $orderBy: 'ProductName' 35 | } 36 | }); 37 | 38 | return query; 39 | }, 40 | expectedResults: { 41 | sql: 'SELECT * FROM Products ORDER BY ProductName ASC OFFSET 30 ROWS FETCH NEXT 10 ROWS ONLY', 42 | values: {} 43 | } 44 | } 45 | }, 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /sql/helpers/queries/orOffset/README.md: -------------------------------------------------------------------------------- 1 | # top Helper 2 | Specifies the `OFFSET` clause for the `SELECT` Statement for Oracle db only. 3 | 4 | #### Supported by 5 | - [Oracle](https://www.oracletutorial.com/oracle-basics/oracle-fetch/) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Number: 10 | 11 | Usage of `orOffset` as **Number** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $orOffset: 10 // it will fetch 10 rows only 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```sql 21 | OFFSET 10 ROWS 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | let query = sql.build({ 28 | $select: { 29 | $orOffset: 10, 30 | $from: 'people' 31 | } 32 | }); 33 | 34 | return query; 35 | } 36 | 37 | // SQL output 38 | SELECT 39 | * 40 | FROM 41 | people 42 | OFFSET 10 ROWS 43 | 44 | // Values 45 | {} 46 | ``` 47 | -------------------------------------------------------------------------------- /sql/helpers/queries/orOffset/orOffset.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class orOffset extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Number: { 9 | syntax: this.Syntax('', this.syntaxHook) 10 | } 11 | }); 12 | } 13 | 14 | syntaxHook(query, idententifier) { 15 | return `OFFSET ${query} ROWS`; 16 | } 17 | } 18 | 19 | module.exports = { 20 | definition: orOffset, 21 | description: 'Specifies the `OFFSET ${query} ROWS` clause for the `SELECT` Statement.', 22 | supportedBy: { 23 | Oracle: 'https://www.oracletutorial.com/oracle-basics/oracle-fetch/' 24 | }, 25 | examples: { 26 | Number: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $orOffset: 10, 33 | $from: 'Products', 34 | $orderBy: 'ProductName' 35 | } 36 | }); 37 | 38 | return query; 39 | }, 40 | expectedResults: { 41 | sql: 'SELECT * FROM Products ORDER BY ProductName ASC OFFSET 10 ROWS', 42 | values: {} 43 | } 44 | } 45 | }, 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/asc/README.md: -------------------------------------------------------------------------------- 1 | # asc Helper 2 | Specifies the `ASC` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [MariaDB](https://mariadb.com/kb/en/library/order-by/) 6 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 7 | - [SQLite](https://sqlite.org/lang_select.html) 8 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 9 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Boolean: 15 | 16 | The usage of `asc` as **Boolean** is restricted to the following values: 17 | - true 18 | 19 | #### as Boolean with value **true**: 20 | **Syntax:** 21 | 22 | ```javascript 23 | $asc: true 24 | ``` 25 | 26 | **SQL-Definition:** 27 | ```javascript 28 | -->Accepted->Return: 29 | ``` 30 | 31 | :bulb: **Example:** 32 | ```javascript 33 | function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | } 47 | 48 | // SQL output 49 | SELECT 50 | * 51 | FROM 52 | people 53 | ORDER BY 54 | last_name ASC, 55 | first_name ASC, 56 | age DESC 57 | 58 | // Values 59 | {} 60 | ``` 61 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/asc/asc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class asc extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('-->Accepted->Return:') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: asc, 19 | description: 'Specifies the `ASC` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | MariaDB: 'https://mariadb.com/kb/en/library/order-by/', 22 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 23 | SQLite: 'https://sqlite.org/lang_select.html', 24 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 25 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 26 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql' 27 | }, 28 | examples: { 29 | Boolean: { 30 | true: { 31 | 'Basic Usage': function(sql) { 32 | return { 33 | test: function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | }, 47 | expectedResults: { 48 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC, age DESC', 49 | values: {} 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/desc/README.md: -------------------------------------------------------------------------------- 1 | # desc Helper 2 | Specifies the `DESC` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [MariaDB](https://mariadb.com/kb/en/library/order-by/) 6 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 7 | - [SQLite](https://sqlite.org/lang_select.html) 8 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 9 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Boolean: 15 | 16 | The usage of `desc` as **Boolean** is restricted to the following values: 17 | - true 18 | 19 | #### as Boolean with value **true**: 20 | **Syntax:** 21 | 22 | ```javascript 23 | $desc: true 24 | ``` 25 | 26 | **SQL-Definition:** 27 | ```javascript 28 | -->Accepted->Return: 29 | ``` 30 | 31 | :bulb: **Example:** 32 | ```javascript 33 | function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | } 47 | 48 | // SQL output 49 | SELECT 50 | * 51 | FROM 52 | people 53 | ORDER BY 54 | last_name ASC, 55 | first_name ASC, 56 | age DESC 57 | 58 | // Values 59 | {} 60 | ``` 61 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/desc/desc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class desc extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('-->Accepted->Return:') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: desc, 19 | description: 'Specifies the `DESC` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | MariaDB: 'https://mariadb.com/kb/en/library/order-by/', 22 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 23 | SQLite: 'https://sqlite.org/lang_select.html', 24 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 25 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 26 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql' 27 | }, 28 | examples: { 29 | Boolean: { 30 | true: { 31 | 'Basic Usage': function(sql) { 32 | return { 33 | test: function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | }, 47 | expectedResults: { 48 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC, age DESC', 49 | values: {} 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/nullsFirst/README.md: -------------------------------------------------------------------------------- 1 | # nullsFirst Helper 2 | Specifies the `NULLS FIRST` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 6 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as Boolean: 11 | 12 | The usage of `nullsFirst` as **Boolean** is restricted to the following values: 13 | - true 14 | 15 | #### as Boolean with value **true**: 16 | **Syntax:** 17 | 18 | ```javascript 19 | $nullsFirst: true 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```javascript 24 | FIRST 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | } 43 | 44 | // SQL output 45 | SELECT 46 | * 47 | FROM 48 | people 49 | ORDER BY 50 | last_name ASC, 51 | first_name ASC NULLS FIRST, 52 | age DESC NULLS LAST 53 | 54 | // Values 55 | {} 56 | ``` 57 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/nullsFirst/nullsFirst.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class nullsFirst extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('FIRST') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: nullsFirst, 19 | description: 'Specifies the `NULLS FIRST` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 22 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 23 | }, 24 | examples: { 25 | Boolean: { 26 | true: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | }, 43 | expectedResults: { 44 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC NULLS FIRST, age DESC NULLS LAST', 45 | values: {} 46 | } 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/nullsLast/README.md: -------------------------------------------------------------------------------- 1 | # nullsLast Helper 2 | Specifies the `NULLS LAST` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 6 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as Boolean: 11 | 12 | The usage of `nullsLast` as **Boolean** is restricted to the following values: 13 | - true 14 | 15 | #### as Boolean with value **true**: 16 | **Syntax:** 17 | 18 | ```javascript 19 | $nullsLast: true 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```javascript 24 | LAST 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | } 43 | 44 | // SQL output 45 | SELECT 46 | * 47 | FROM 48 | people 49 | ORDER BY 50 | last_name ASC, 51 | first_name ASC NULLS FIRST, 52 | age DESC NULLS LAST 53 | 54 | // Values 55 | {} 56 | ``` 57 | -------------------------------------------------------------------------------- /sql/helpers/queries/orderBy/private/nullsLast/nullsLast.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class nullsLast extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('LAST') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: nullsLast, 19 | description: 'Specifies the `NULLS LAST` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 22 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 23 | }, 24 | examples: { 25 | Boolean: { 26 | true: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | }, 43 | expectedResults: { 44 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC NULLS FIRST, age DESC NULLS LAST', 45 | values: {} 46 | } 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /sql/helpers/queries/ssFetch/README.md: -------------------------------------------------------------------------------- 1 | # top Helper 2 | Specifies the `FETCH` clause for the `SELECT` Statement. 3 | 4 | #### Supported by 5 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Number: 10 | 11 | Usage of `ssFetch` as **Number** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $ssFetch: { 17 | $skip: 30, // it will skip 30 rows 18 | $fetch: 10 // it will fetch next 10 rows 19 | } 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```sql 24 | OFFSET 30 ROWS FETCH FIRST 10 ROWS ONLY 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $ssFetch: { 33 | $skip: 30, 34 | $fetch: 10 35 | }, 36 | $from: 'people' 37 | } 38 | }); 39 | 40 | return query; 41 | } 42 | 43 | // SQL output 44 | SELECT 45 | * 46 | FROM 47 | people 48 | OFFSET 30 ROWS FETCH FIRST 10 ROWS ONLY 49 | 50 | // Values 51 | {} 52 | ``` 53 | -------------------------------------------------------------------------------- /sql/helpers/queries/ssFetch/ssFetch.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class ssFetch extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | syntax: this.Syntax('', this.syntaxHook) 10 | } 11 | }); 12 | } 13 | 14 | syntaxHook(query, idententifier) { 15 | return `OFFSET ${query.$skip} ROWS FETCH FIRST ${query.$fetch} ROWS ONLY`; 16 | } 17 | } 18 | 19 | module.exports = { 20 | definition: ssFetch, 21 | description: 'Specifies the `OFFSET ${query.$skip} ROWS FETCH FIRST ${query.$fetch} ROWS ONLY` clause for the `SELECT` Statement.', 22 | supportedBy: { 23 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql' 24 | }, 25 | examples: { 26 | Object: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $ssFetch: { $skip: 30, $fetch: 10}, 33 | $from: 'Products', 34 | $orderBy: 'ProductName' 35 | } 36 | }); 37 | 38 | return query; 39 | }, 40 | expectedResults: { 41 | sql: 'SELECT * FROM dbo.[Products] ORDER BY [ProductName] ASC OFFSET 30 ROWS FETCH FIRST 10 ROWS ONLY', 42 | values: {} 43 | } 44 | } 45 | }, 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /sql/helpers/queries/top/README.md: -------------------------------------------------------------------------------- 1 | # top Helper 2 | Specifies the `TOP` clause for the `SELECT` Statement. 3 | 4 | #### Supported by 5 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as Number: 10 | 11 | Usage of `top` as **Number** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $top: < Number > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | TOP() 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | let query = sql.build({ 28 | $select: { 29 | $top: 10, 30 | $from: 'people' 31 | } 32 | }); 33 | 34 | return query; 35 | } 36 | 37 | // SQL output 38 | SELECT 39 | TOP($1) * 40 | FROM 41 | people 42 | 43 | // Values 44 | { 45 | "$1": 10 46 | } 47 | ``` 48 | 49 | ## as Object: 50 | 51 | Usage of `top` as **Object** with the following Syntax: 52 | 53 | **Syntax:** 54 | 55 | ```javascript 56 | $top: { ... } 57 | ``` 58 | 59 | **SQL-Definition:** 60 | ```javascript 61 | TOP(<$value>){ PERCENT[$percent]} 62 | ``` 63 | 64 | **Registered Helpers** 65 | 66 | Name|Required|Public|SQL-Definition|Supported by 67 | :---|:------:|:----:|:-------------|:----------- 68 | [value](./private/value/)|:heavy_check_mark:|*private*|| 69 | [percent](./private/percent/)|*optional*|*private*| PERCENT [$percent]| 70 | 71 | :bulb: **Example:** 72 | ```javascript 73 | function() { 74 | let query = sql.build({ 75 | $select: { 76 | $top: { $value: 5, $percent: true }, 77 | $from: 'people' 78 | } 79 | }); 80 | 81 | return query; 82 | } 83 | 84 | // SQL output 85 | SELECT 86 | TOP($1) PERCENT * 87 | FROM 88 | people 89 | 90 | // Values 91 | { 92 | "$1": 5 93 | } 94 | ``` 95 | 96 | ## Further Examples 97 | 98 | :bulb: **Usage with variable and $percent: false** 99 | ```javascript 100 | function() { 101 | let query = sql.build({ 102 | $select: { 103 | $top: { $value: '~~@mytop10', $percent: false }, 104 | $from: 'people' 105 | } 106 | }); 107 | 108 | return query; 109 | } 110 | 111 | // SQL output 112 | SELECT 113 | TOP(@mytop10) * 114 | FROM 115 | people 116 | 117 | // Values 118 | {} 119 | ``` 120 | 121 | -------------------------------------------------------------------------------- /sql/helpers/queries/top/top.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class top extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Number: { syntax: this.Syntax('TOP()') }, 9 | Object: { syntax: this.Syntax('TOP(<$value>){ PERCENT[$percent]}') } 10 | }); 11 | 12 | this.$value = new SQLBuilder.SQLPredefined.Expression(sql); 13 | this.$percent = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: top, 19 | description: 'Specifies the `TOP` clause for the `SELECT` Statement.', 20 | supportedBy: { 21 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql' 22 | }, 23 | examples: { 24 | Number: { 25 | 'Basic Usage': function(sql) { 26 | return { 27 | test: function() { 28 | let query = sql.build({ 29 | $select: { 30 | $top: 10, 31 | $from: 'people' 32 | } 33 | }); 34 | 35 | return query; 36 | }, 37 | expectedResults: { 38 | sql: 'SELECT TOP($1) * FROM people', 39 | values: { 40 | $1: 10 41 | } 42 | } 43 | } 44 | } 45 | }, 46 | Object: { 47 | 'Basic Usage': function(sql) { 48 | return { 49 | test: function() { 50 | let query = sql.build({ 51 | $select: { 52 | $top: { $value: 5, $percent: true}, 53 | $from: 'people' 54 | } 55 | }); 56 | 57 | return query; 58 | }, 59 | expectedResults: { 60 | sql: 'SELECT TOP($1) PERCENT * FROM people', 61 | values: { 62 | $1: 5 63 | } 64 | } 65 | } 66 | }, 67 | 'Usage with variable and $percent: false': function(sql) { 68 | return { 69 | test: function() { 70 | let query = sql.build({ 71 | $select: { 72 | $top: { $value: '~~@mytop10', $percent: false}, 73 | $from: 'people' 74 | } 75 | }); 76 | 77 | return query; 78 | }, 79 | expectedResults: { 80 | sql: 'SELECT TOP(@mytop10) * FROM people', 81 | values: {} 82 | } 83 | } 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /sql/helpers/queries/where/README.md: -------------------------------------------------------------------------------- 1 | # where Helper 2 | Specifies the `WHERE` clause for the `SELECT` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/select/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 8 | - [SQLite](https://sqlite.org/lang_select.html) 9 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `where` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $where: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | {* AND [$and] *} {* OR [$or] *} 27 | ``` 28 | 29 | **Registered Helpers** 30 | 31 | Name|Required|Public|SQL-Definition|Supported by 32 | :---|:------:|:----:|:-------------|:----------- 33 | [and](../../../helpers/logical/and/)|*optional*|:heavy_check_mark:|| 34 | [or](../../../helpers/logical/or/)|*optional*|:heavy_check_mark:|| 35 | 36 | :bulb: **Example:** 37 | ```javascript 38 | function() { 39 | return sql.build({ 40 | $select: { 41 | $from: 'people', 42 | $where: { 43 | first_name: 'John' 44 | } 45 | } 46 | }); 47 | } 48 | 49 | // SQL output 50 | SELECT 51 | * 52 | FROM 53 | people 54 | WHERE 55 | first_name = $1 56 | 57 | // Values 58 | { 59 | "$1": "John" 60 | } 61 | ``` 62 | 63 | -------------------------------------------------------------------------------- /sql/helpers/queries/where/where.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class where extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | syntax: this.Syntax('{* AND [$and] *} {* OR [$or] *}') 10 | } 11 | }); 12 | } 13 | 14 | preBuild(query) { 15 | // by defaut move all conditions to $and 16 | // if there is no $and / $or defined 17 | if (!query.$and && !query.$or) { 18 | let andItems = []; 19 | this.forEach(query, (value, key)=>{ 20 | if (this.isIdentifier(key)) { 21 | let o = {}; o[key]=value; 22 | andItems.push(o); 23 | } 24 | }); 25 | if (andItems.length > 0) { 26 | query.$and = andItems; //_.cloneDeep(andItems); 27 | this.forEach(query, (value, key)=>{ 28 | if (this.isIdentifier(key)) { 29 | delete query[key]; 30 | } 31 | }); 32 | } 33 | } 34 | 35 | return query; 36 | } 37 | } 38 | 39 | module.exports = { 40 | definition: where, 41 | description: 'Specifies the `WHERE` clause for the `SELECT` Statement.', 42 | supportedBy: { 43 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 44 | MariaDB: 'https://mariadb.com/kb/en/library/select/', 45 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 46 | SQLite: 'https://sqlite.org/lang_select.html', 47 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm', 48 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql' 49 | }, 50 | examples: { 51 | Object: { 52 | 'Basic Usage': function(sql) { 53 | return { 54 | test: function() { 55 | return sql.build({ 56 | $select: { 57 | $from: 'people', 58 | $where: { 59 | first_name: 'John' 60 | } 61 | } 62 | }); 63 | }, 64 | expectedResults: { 65 | sql: 'SELECT * FROM people WHERE first_name = $1', 66 | values: { 67 | $1: 'John' 68 | } 69 | } 70 | } 71 | } 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /sql/operators/createIndex/README.md: -------------------------------------------------------------------------------- 1 | # createIndex Operator 2 | Specifies the `CREATE INDEX` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/create-index.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/create-index/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-createindex.html) 8 | - [SQLite](https://sqlite.org/lang_createview.html) 9 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5010.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `createIndex` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $createIndex: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | CREATE{ UNIQUE[$unique]} INDEX 27 | { CONCURRENTLY[$concurrently]}-->(PostgreSQL) 28 | { IF NOT EXISTS [$ine] | [$ifNotExists]} 29 | { [$name]} ON <$table> 30 | { USING [$using]}-->(PostgreSQL,MariaDB,MySQL) 31 | (<$columns>) 32 | { WITH ([$storageParameters])} 33 | { TABLESPACE [$tablespace]}-->(PostgreSQL) 34 | { WHERE [$where]} 35 | 36 | ``` 37 | 38 | **Registered Helpers** 39 | 40 | Name|Required|Public|SQL-Definition|Supported by 41 | :---|:------:|:----:|:-------------|:----------- 42 | [unique](../../helpers/ddl/constraint/unique/)|*optional*|:heavy_check_mark:| UNIQUE [$unique]| 43 | [concurrently](./private/concurrently/)|*optional*|*private*| CONCURRENTLY [$concurrently]|`PostgreSQL` 44 | [ine](./private/ine/)|*optional*|*private*| IF NOT EXISTS [$ine]| 45 | [ifNotExists](./private/ifNotExists/)|*optional*|*private*| IF NOT EXISTS [$ifNotExists]| 46 | [name](./private/name/)|*optional*|*private*| [$name]| 47 | [table](./private/table/)|:heavy_check_mark:|*private*|| 48 | [using](../../helpers/queries/using/)|*optional*|:heavy_check_mark:| USING [$using]|`PostgreSQL` `MariaDB` `MySQL` 49 | [columns](../../helpers/ddl/columns/)|:heavy_check_mark:|:heavy_check_mark:|| 50 | [storageParameters](./private/storageParameters/)|*optional*|*private*| WITH ( [$storageParameters])| 51 | [tablespace](./private/tablespace/)|*optional*|*private*| TABLESPACE [$tablespace]|`PostgreSQL` 52 | [where](../../helpers/queries/where/)|*optional*|:heavy_check_mark:| WHERE [$where]| 53 | 54 | :bulb: **Example:** 55 | ```javascript 56 | function() { 57 | return sql.$createIndex({ 58 | $unique: true, 59 | $table: 'people', 60 | $columns: { 61 | first_name: true, 62 | last_name: true 63 | } 64 | }); 65 | } 66 | 67 | // SQL output 68 | CREATE UNIQUE INDEX ON people (first_name ASC, last_name ASC) 69 | 70 | // Values 71 | {} 72 | ``` 73 | 74 | -------------------------------------------------------------------------------- /sql/operators/createIndex/createIndex.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const SYNTAX = 4 | `CREATE{ UNIQUE[$unique]} INDEX 5 | { CONCURRENTLY[$concurrently]}-->(PostgreSQL) 6 | { IF NOT EXISTS [$ine] | [$ifNotExists]} 7 | { [$name]} ON <$table> 8 | { USING [$using]}-->(PostgreSQL,MariaDB,MySQL) 9 | (<$columns>) 10 | { WITH ([$storageParameters])} 11 | { TABLESPACE [$tablespace]}-->(PostgreSQL) 12 | { WHERE [$where]} 13 | `; 14 | 15 | class createIndex extends SQLBuilder.SQLOperator { 16 | constructor(sql){ 17 | super(sql); 18 | 19 | this.Types({ 20 | Object: { 21 | syntax: this.Syntax(SYNTAX, SQLBuilder.CALLEE) 22 | } 23 | }); 24 | 25 | this.$unique = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 26 | this.$concurrently = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 27 | this.$ine = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 28 | // add a synonym for ine = ifNotExists 29 | this.$ifNotExists = this.$ine; 30 | this.$name = new SQLBuilder.SQLPredefined.StringIdentifier(sql); 31 | this.$table = new SQLBuilder.SQLPredefined.StringIdentifier(sql); 32 | this.$tablespace = new SQLBuilder.SQLPredefined.StringIdentifier(sql); 33 | 34 | this.registerPrivateHelper('using'); 35 | this.registerPrivateHelper('columns'); 36 | this.registerPrivateHelper('storageParameters'); 37 | } 38 | } 39 | 40 | module.exports = { 41 | definition: createIndex, 42 | description: 'Specifies the `CREATE INDEX` Statement.', 43 | supportedBy: { 44 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/create-index.html', 45 | MariaDB: 'https://mariadb.com/kb/en/library/create-index/', 46 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-createindex.html', 47 | SQLite: 'https://sqlite.org/lang_createview.html', 48 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5010.htm', 49 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql' 50 | }, 51 | examples: { 52 | Object: { 53 | "Basic Usage": function(sql) { 54 | return { 55 | test: function(){ 56 | return sql.$createIndex({ 57 | $unique: true, 58 | $table: 'people', 59 | $columns: { 60 | first_name: true, 61 | last_name: true 62 | } 63 | }); 64 | }, 65 | expectedResults: { 66 | sql: 'CREATE UNIQUE INDEX ON people (first_name ASC, last_name ASC)', 67 | values: {}, 68 | } 69 | } 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/asc/README.md: -------------------------------------------------------------------------------- 1 | # asc Helper 2 | Specifies the `ASC` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [MariaDB](https://mariadb.com/kb/en/library/order-by/) 6 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 7 | - [SQLite](https://sqlite.org/lang_select.html) 8 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 9 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Boolean: 15 | 16 | The usage of `asc` as **Boolean** is restricted to the following values: 17 | - true 18 | 19 | #### as Boolean with value **true**: 20 | **Syntax:** 21 | 22 | ```javascript 23 | $asc: true 24 | ``` 25 | 26 | **SQL-Definition:** 27 | ```javascript 28 | -->Accepted->Return: 29 | ``` 30 | 31 | :bulb: **Example:** 32 | ```javascript 33 | function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | } 47 | 48 | // SQL output 49 | SELECT 50 | * 51 | FROM 52 | people 53 | ORDER BY 54 | last_name ASC, 55 | first_name ASC, 56 | age DESC 57 | 58 | // Values 59 | {} 60 | ``` 61 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/asc/asc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class asc extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('-->Accepted->Return:') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: asc, 19 | description: 'Specifies the `ASC` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | MariaDB: 'https://mariadb.com/kb/en/library/order-by/', 22 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 23 | SQLite: 'https://sqlite.org/lang_select.html', 24 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 25 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 26 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql' 27 | }, 28 | examples: { 29 | Boolean: { 30 | true: { 31 | 'Basic Usage': function(sql) { 32 | return { 33 | test: function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | }, 47 | expectedResults: { 48 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC, age DESC', 49 | values: {} 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/desc/README.md: -------------------------------------------------------------------------------- 1 | # desc Helper 2 | Specifies the `DESC` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [MariaDB](https://mariadb.com/kb/en/library/order-by/) 6 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 7 | - [SQLite](https://sqlite.org/lang_select.html) 8 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 9 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Boolean: 15 | 16 | The usage of `desc` as **Boolean** is restricted to the following values: 17 | - true 18 | 19 | #### as Boolean with value **true**: 20 | **Syntax:** 21 | 22 | ```javascript 23 | $desc: true 24 | ``` 25 | 26 | **SQL-Definition:** 27 | ```javascript 28 | -->Accepted->Return: 29 | ``` 30 | 31 | :bulb: **Example:** 32 | ```javascript 33 | function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | } 47 | 48 | // SQL output 49 | SELECT 50 | * 51 | FROM 52 | people 53 | ORDER BY 54 | last_name ASC, 55 | first_name ASC, 56 | age DESC 57 | 58 | // Values 59 | {} 60 | ``` 61 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/desc/desc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class desc extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('-->Accepted->Return:') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: desc, 19 | description: 'Specifies the `DESC` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | MariaDB: 'https://mariadb.com/kb/en/library/order-by/', 22 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 23 | SQLite: 'https://sqlite.org/lang_select.html', 24 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 25 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 26 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql' 27 | }, 28 | examples: { 29 | Boolean: { 30 | true: { 31 | 'Basic Usage': function(sql) { 32 | return { 33 | test: function() { 34 | let query = sql.build({ 35 | $select: { 36 | $from: 'people', 37 | $orderBy: { 38 | last_name: true, 39 | first_name: { $asc: true }, 40 | age: { $desc: true } 41 | } 42 | } 43 | }); 44 | 45 | return query; 46 | }, 47 | expectedResults: { 48 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC, age DESC', 49 | values: {} 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/nullsFirst/README.md: -------------------------------------------------------------------------------- 1 | # nullsFirst Helper 2 | Specifies the `NULLS FIRST` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 6 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as Boolean: 11 | 12 | The usage of `nullsFirst` as **Boolean** is restricted to the following values: 13 | - true 14 | 15 | #### as Boolean with value **true**: 16 | **Syntax:** 17 | 18 | ```javascript 19 | $nullsFirst: true 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```javascript 24 | FIRST 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | } 43 | 44 | // SQL output 45 | SELECT 46 | * 47 | FROM 48 | people 49 | ORDER BY 50 | last_name ASC, 51 | first_name ASC NULLS FIRST, 52 | age DESC NULLS LAST 53 | 54 | // Values 55 | {} 56 | ``` 57 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/nullsFirst/nullsFirst.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class nullsFirst extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('FIRST') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: nullsFirst, 19 | description: 'Specifies the `NULLS FIRST` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 22 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 23 | }, 24 | examples: { 25 | Boolean: { 26 | true: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | }, 43 | expectedResults: { 44 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC NULLS FIRST, age DESC NULLS LAST', 45 | values: {} 46 | } 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/nullsLast/README.md: -------------------------------------------------------------------------------- 1 | # nullsLast Helper 2 | Specifies the `NULLS LAST` option for the `ORDER BY` clause on each column. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 6 | - [Oracle](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as Boolean: 11 | 12 | The usage of `nullsLast` as **Boolean** is restricted to the following values: 13 | - true 14 | 15 | #### as Boolean with value **true**: 16 | **Syntax:** 17 | 18 | ```javascript 19 | $nullsLast: true 20 | ``` 21 | 22 | **SQL-Definition:** 23 | ```javascript 24 | LAST 25 | ``` 26 | 27 | :bulb: **Example:** 28 | ```javascript 29 | function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | } 43 | 44 | // SQL output 45 | SELECT 46 | * 47 | FROM 48 | people 49 | ORDER BY 50 | last_name ASC, 51 | first_name ASC NULLS FIRST, 52 | age DESC NULLS LAST 53 | 54 | // Values 55 | {} 56 | ``` 57 | -------------------------------------------------------------------------------- /sql/operators/createIndex/private/columns/private/nullsLast/nullsLast.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class nullsLast extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Boolean: { 9 | syntax: { 10 | true: this.Syntax('LAST') 11 | } 12 | } 13 | }); 14 | } 15 | } 16 | 17 | module.exports = { 18 | definition: nullsLast, 19 | description: 'Specifies the `NULLS LAST` option for the `ORDER BY` clause on each column.', 20 | supportedBy: { 21 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 22 | Oracle: 'https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html', 23 | }, 24 | examples: { 25 | Boolean: { 26 | true: { 27 | 'Basic Usage': function(sql) { 28 | return { 29 | test: function() { 30 | let query = sql.build({ 31 | $select: { 32 | $from: 'people', 33 | $orderBy: { 34 | last_name: true, 35 | first_name: { $asc: true, $nullsFirst: true }, 36 | age: { $desc: true, $nullsLast: true } 37 | } 38 | } 39 | }); 40 | 41 | return query; 42 | }, 43 | expectedResults: { 44 | sql: 'SELECT * FROM people ORDER BY last_name ASC, first_name ASC NULLS FIRST, age DESC NULLS LAST', 45 | values: {} 46 | } 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /sql/operators/createTable/README.md: -------------------------------------------------------------------------------- 1 | # createTable Operator 2 | Specifies the `CREATE TABLE` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/create-table/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-createtable.html) 8 | - [SQLite](https://sqlite.org/lang_createtable.html) 9 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7002.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `createTable` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $createTable: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | CREATE 27 | { OR REPLACE[$orReplace]}-->(MariaDB) 28 | { TEMPORARY[$temp]} 29 | { UNLOGGED[$unlogged]}-->(PostgreSQL) 30 | TABLE {IF NOT EXISTS [$ine] | [$ifNotExists] }<$table> (<$define>) 31 | { WITH ([$tableOptions])}-->(PostgreSQL,SQLServer) 32 | { [$options]}-->(MariaDB,MySQL) 33 | { TABLESPACE [$tablespace]}-->(PostgreSQL) 34 | 35 | ``` 36 | 37 | **Registered Helpers** 38 | 39 | Name|Required|Public|SQL-Definition|Supported by 40 | :---|:------:|:----:|:-------------|:----------- 41 | [orReplace](./private/orReplace/)|*optional*|*private*| OR REPLACE [$orReplace]|`MariaDB` 42 | [temp](./private/temp/)|*optional*|*private*| TEMPORARY [$temp]| 43 | [unlogged](./private/unlogged/)|*optional*|*private*| UNLOGGED [$unlogged]|`PostgreSQL` 44 | [ine](./private/ine/)|*optional*|*private*|IF NOT EXISTS [$ine] | 45 | [ifNotExists](./private/ifNotExists/)|*optional*|*private*|IF NOT EXISTS [$ifNotExists] | 46 | [table](./private/table/)|:heavy_check_mark:|*private*|| 47 | [define](./private/define/)|:heavy_check_mark:|*private*|| 48 | [tableOptions](./private/tableOptions/)|*optional*|*private*| WITH ( [$tableOptions])|`PostgreSQL` `SQLServer` 49 | [options](./private/options/)|*optional*|*private*| [$options]|`MariaDB` `MySQL` 50 | [tablespace](./private/tablespace/)|*optional*|*private*| TABLESPACE [$tablespace]|`PostgreSQL` 51 | 52 | :bulb: **Example:** 53 | ```javascript 54 | function() { 55 | return sql.$createTable({ 56 | $temp: true, 57 | $table: 'my_temp_people_table', 58 | $define: { 59 | people_id: { $column: { $type: 'INT', $default: 0 } }, 60 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 61 | last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 62 | bio: { $column: { $type: 'TEXT' } } 63 | } 64 | }); 65 | } 66 | 67 | // SQL output 68 | CREATE TEMPORARY TABLE my_temp_people_table ( 69 | people_id INT DEFAULT $1, 70 | first_name VARCHAR(50) NOT NULL, 71 | last_name VARCHAR(50) NOT NULL, 72 | bio TEXT 73 | ) 74 | 75 | // Values 76 | { 77 | "$1": 0 78 | } 79 | ``` 80 | 81 | -------------------------------------------------------------------------------- /sql/operators/createTable/createTable.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const SYNTAX = 4 | `CREATE 5 | { OR REPLACE[$orReplace]}-->(MariaDB) 6 | { TEMPORARY[$temp]} 7 | { UNLOGGED[$unlogged]}-->(PostgreSQL) 8 | TABLE {IF NOT EXISTS [$ine] | [$ifNotExists] }<$table> (<$define>) 9 | { WITH ([$tableOptions])}-->(PostgreSQL,SQLServer) 10 | { [$options]}-->(MariaDB,MySQL) 11 | { TABLESPACE [$tablespace]}-->(PostgreSQL) 12 | `; 13 | 14 | class createTable extends SQLBuilder.SQLOperator { 15 | constructor(sql){ 16 | super(sql); 17 | 18 | this.Types({ 19 | Object: { 20 | syntax: this.Syntax(SYNTAX, SQLBuilder.CALLEE) 21 | } 22 | }); 23 | 24 | this.$orReplace = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 25 | this.$temp = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 26 | this.$unlogged = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 27 | this.$ine = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql); 28 | // add a synonym for ine = ifNotExists 29 | this.$ifNotExists = this.$ine; 30 | 31 | this.$table = new SQLBuilder.SQLPredefined.StringIdentifier(sql); 32 | this.$tablespace = new SQLBuilder.SQLPredefined.StringIdentifier(sql); 33 | 34 | this.registerPrivateHelper('define'); 35 | 36 | if (sql.isPostgreSQL() || sql.isSQLServer()) { 37 | this.registerPrivateHelper('tableOptions'); 38 | } 39 | 40 | if (sql.isMySQL() || sql.isMariaDB()) { 41 | this.registerPrivateHelper('options'); 42 | } 43 | } 44 | } 45 | 46 | module.exports = { 47 | definition: createTable, 48 | description: 'Specifies the `CREATE TABLE` Statement.', 49 | supportedBy: { 50 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/create-table.html', 51 | MariaDB: 'https://mariadb.com/kb/en/library/create-table/', 52 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-createtable.html', 53 | SQLite: 'https://sqlite.org/lang_createtable.html', 54 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7002.htm', 55 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql' 56 | }, 57 | examples: { 58 | Object: { 59 | "Basic Usage": function(sql) { 60 | return { 61 | test: function(){ 62 | return sql.$createTable({ 63 | $temp: true, 64 | $table: 'my_temp_people_table', 65 | $define: { 66 | people_id: { $column: { $type: 'INT', $default: 0 } }, 67 | first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 68 | last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } }, 69 | bio: { $column: { $type: 'TEXT' } } 70 | } 71 | }); 72 | }, 73 | expectedResults: { 74 | sql: 'CREATE TEMPORARY TABLE my_temp_people_table (people_id INT DEFAULT $1, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, bio TEXT)', 75 | values: { 76 | $1: 0 77 | }, 78 | PostgreSQL: { 79 | sql: 'CREATE TEMPORARY TABLE my_temp_people_table (people_id INT DEFAULT 0, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, bio TEXT)', 80 | values: {} 81 | } 82 | } 83 | } 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /sql/operators/except/private/all/all.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class all extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | const ARRAY_SYNTAX = ` 8 | [$select] 9 | {[$union]} 10 | {[$intersect]} 11 | {[$except]} 12 | [ EXCEPT ALL ... ]`; 13 | 14 | this.Types({ 15 | Array: { 16 | eachItemOf: { 17 | Object: { syntax: this.Syntax(ARRAY_SYNTAX) } 18 | } 19 | } 20 | }); 21 | } 22 | 23 | postBuild(result) { 24 | return '(' + result + ')'; 25 | } 26 | } 27 | 28 | module.exports = { 29 | definition: all, 30 | description: 'Specifies the `EXCEPT ALL` Operator.', 31 | supportedBy: { 32 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/union.html', 33 | MariaDB: 'https://mariadb.com/kb/en/library/union', 34 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/queries-union.html', 35 | SQLite: 'https://sqlite.org/syntax/compound-select-stmt.html', 36 | //Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm', 37 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql' 38 | }, 39 | examples: { 40 | Array: { 41 | eachItemOf: { 42 | Object: { 43 | 'Basic Usage': function(sql) { 44 | return { 45 | test: function() { 46 | return sql.$except({ 47 | $all: [ 48 | { $select: { first_name: true, $from: 'people' } }, 49 | { $select: { last_name: true, $from: 'people' } } 50 | ] 51 | }); 52 | }, 53 | expectedResults: { 54 | sql: '((SELECT first_name FROM people) EXCEPT ALL (SELECT last_name FROM people))', 55 | values: {} 56 | } 57 | } 58 | }, 59 | 'Nested unions': function(sql) { 60 | return { 61 | test: function() { 62 | return sql.$union([ 63 | { $select: { first_name: true, $from: 'people' } }, 64 | { $except: { 65 | $all: [ 66 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('K') } } }, 67 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('A') } } } 68 | ] 69 | }} 70 | ]); 71 | }, 72 | expectedResults: { 73 | sql: '(SELECT first_name FROM people) UNION ((SELECT last_name FROM people WHERE last_name LIKE $1) EXCEPT ALL (SELECT last_name FROM people WHERE last_name LIKE $2))', 74 | values: { 75 | $1: 'K%', 76 | $2: 'A%' 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /sql/operators/except/private/distinct/distinct.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class distinct extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | const ARRAY_SYNTAX = ` 8 | [$select] 9 | {[$union]} 10 | {[$intersect]} 11 | {[$except]} 12 | [ EXCEPT ... ]`; 13 | 14 | this.Types({ 15 | Array: { 16 | eachItemOf: { 17 | Object: { syntax: this.Syntax(ARRAY_SYNTAX) } 18 | } 19 | } 20 | }); 21 | } 22 | 23 | postBuild(result) { 24 | return '(' + result + ')'; 25 | } 26 | } 27 | 28 | module.exports = { 29 | definition: distinct, 30 | description: 'Specifies the `EXCEPT [DISTINCT]` Operator.', 31 | supportedBy: { 32 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/union.html', 33 | MariaDB: 'https://mariadb.com/kb/en/library/union', 34 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/queries-union.html', 35 | SQLite: 'https://sqlite.org/syntax/compound-select-stmt.html', 36 | //Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm', 37 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql' 38 | }, 39 | examples: { 40 | Array: { 41 | eachItemOf: { 42 | Object: { 43 | 'Basic Usage': function(sql) { 44 | return { 45 | test: function() { 46 | return sql.$except({ 47 | $distinct: [ 48 | { $select: { first_name: true, $from: 'people' } }, 49 | { $select: { last_name: true, $from: 'people' } } 50 | ] 51 | }); 52 | }, 53 | expectedResults: { 54 | sql: '((SELECT first_name FROM people) EXCEPT (SELECT last_name FROM people))', 55 | values: {} 56 | } 57 | } 58 | }, 59 | 'Nested unions': function(sql) { 60 | return { 61 | test: function() { 62 | return sql.$union([ 63 | { $select: { first_name: true, $from: 'people' } }, 64 | { $except: { 65 | $distinct: [ 66 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('K') } } }, 67 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('A') } } } 68 | ] 69 | }} 70 | ]); 71 | }, 72 | expectedResults: { 73 | sql: '(SELECT first_name FROM people) UNION ((SELECT last_name FROM people WHERE last_name LIKE $1) EXCEPT (SELECT last_name FROM people WHERE last_name LIKE $2))', 74 | values: { 75 | $1: 'K%', 76 | $2: 'A%' 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /sql/operators/insert/private/onConflict/private/columns/README.md: -------------------------------------------------------------------------------- 1 | # columns Helper 2 | Specifies the columns Helper for the `ON CONFLICT` clause. 3 | 4 | #### Supported by 5 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT) 6 | 7 | # Allowed Types and Usage 8 | 9 | ## as String: 10 | 11 | Usage of `columns` as **String** with the following Syntax: 12 | 13 | **Syntax:** 14 | 15 | ```javascript 16 | $columns: < String > 17 | ``` 18 | 19 | **SQL-Definition:** 20 | ```javascript 21 | 22 | ``` 23 | 24 | :bulb: **Example:** 25 | ```javascript 26 | function() { 27 | return sql.$insert({ 28 | $table: 'people', 29 | $documents: { 30 | staff_no: 1, 31 | first_name: 'John', 32 | last_name: 'Doe', 33 | age: 40 34 | }, 35 | $onConflict: { 36 | $columns: 'staff_no', 37 | $doNothing: true 38 | } 39 | }); 40 | } 41 | 42 | // SQL output 43 | INSERT INTO 44 | people (staff_no, first_name, last_name, age) 45 | VALUES 46 | ($1, $2, $3, $4) ON CONFLICT (staff_no) DO NOTHING 47 | 48 | // Values 49 | { 50 | "$1": 1, 51 | "$2": "John", 52 | "$3": "Doe", 53 | "$4": 40 54 | } 55 | ``` 56 | 57 | ## as Array: 58 | 59 | The Usage of `columns` as **Array** is restricted to childs have the following Type: 60 | 61 | - String 62 | 63 | ## as Array :arrow_right: String: 64 | 65 | Usage of `columns` as **Array** with a child of Type **String** : 66 | 67 | **Syntax:** 68 | 69 | ```javascript 70 | $columns: [ 71 | [, ... ] 72 | ] 73 | ``` 74 | 75 | **SQL-Definition:** 76 | ```javascript 77 | [ , ... ] 78 | ``` 79 | 80 | :bulb: **Example:** 81 | ```javascript 82 | function() { 83 | return sql.$insert({ 84 | $table: 'people', 85 | $documents: { 86 | staff_no: 1, 87 | first_name: 'John', 88 | last_name: 'Doe', 89 | age: 40 90 | }, 91 | $onConflict: { 92 | $columns: ['staff_no', 'last_name'], 93 | $doNothing: true 94 | } 95 | }); 96 | } 97 | 98 | // SQL output 99 | INSERT INTO 100 | people (staff_no, first_name, last_name, age) 101 | VALUES 102 | ($1, $2, $3, $4) ON CONFLICT (staff_no, last_name) DO NOTHING 103 | 104 | // Values 105 | { 106 | "$1": 1, 107 | "$2": "John", 108 | "$3": "Doe", 109 | "$4": 40 110 | } 111 | ``` 112 | -------------------------------------------------------------------------------- /sql/operators/insert/private/onConflict/private/columns/columns.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class columns extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('') }, 9 | Array: { 10 | eachItemOf: { 11 | String: { syntax: this.Syntax('[ , ... ]') } 12 | } 13 | } 14 | }); 15 | } 16 | } 17 | 18 | module.exports = { 19 | definition: columns, 20 | description: `Specifies the columns Helper for the \`ON CONFLICT\` clause.`, 21 | supportedBy: { 22 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT', 23 | }, 24 | examples: { 25 | String: { 26 | "Basic Usage": function(sql) { 27 | return { 28 | test: function(){ 29 | return sql.$insert({ $table: 'people', 30 | $documents: { 31 | staff_no: 1, 32 | first_name: 'John', 33 | last_name: 'Doe', 34 | age: 40 35 | }, 36 | $onConflict: { 37 | $columns: 'staff_no', 38 | $doNothing: true 39 | } 40 | }); 41 | }, 42 | expectedResults: { 43 | sql: 'INSERT INTO people (staff_no, first_name, last_name, age) VALUES ($1, $2, $3, $4) ON CONFLICT (staff_no) DO NOTHING', 44 | values:{ 45 | $1: 1, 46 | $2: 'John', 47 | $3: 'Doe', 48 | $4: 40 49 | } 50 | } 51 | } 52 | } 53 | }, 54 | Array: { 55 | eachItemOf: { 56 | String: { 57 | "Basic Usage": function(sql) { 58 | return { 59 | test: function(){ 60 | return sql.$insert({ $table: 'people', 61 | $documents: { 62 | staff_no: 1, 63 | first_name: 'John', 64 | last_name: 'Doe', 65 | age: 40 66 | }, 67 | $onConflict: { 68 | $columns: ['staff_no', 'last_name'], 69 | $doNothing: true 70 | } 71 | }); 72 | }, 73 | expectedResults: { 74 | sql: 'INSERT INTO people (staff_no, first_name, last_name, age) VALUES ($1, $2, $3, $4) ON CONFLICT (staff_no, last_name) DO NOTHING', 75 | values:{ 76 | $1: 1, 77 | $2: 'John', 78 | $3: 'Doe', 79 | $4: 40 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /sql/operators/intersect/private/all/all.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class all extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | const ARRAY_SYNTAX = ` 8 | [$select] 9 | {[$union]} 10 | {[$intersect]} 11 | {[$except]} 12 | [ INTERSECT ALL ... ]`; 13 | 14 | this.Types({ 15 | Array: { 16 | eachItemOf: { 17 | Object: { syntax: this.Syntax(ARRAY_SYNTAX) } 18 | } 19 | } 20 | }); 21 | } 22 | 23 | postBuild(result) { 24 | return '(' + result + ')'; 25 | } 26 | } 27 | 28 | module.exports = { 29 | definition: all, 30 | description: 'Specifies the `UNION ALL` Operator.', 31 | supportedBy: { 32 | MariaDB: 'https://mariadb.com/kb/en/library/intersect/', 33 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/queries-union.html', 34 | SQLite: 'https://sqlite.org/syntax/compound-select-stmt.html', 35 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm', 36 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql' 37 | }, 38 | examples: { 39 | Array: { 40 | eachItemOf: { 41 | Object: { 42 | 'Basic Usage': function(sql) { 43 | return { 44 | test: function() { 45 | return sql.$union({ 46 | $all: [ 47 | { $select: { first_name: true, $from: 'people' } }, 48 | { $select: { last_name: true, $from: 'people' } } 49 | ] 50 | }); 51 | }, 52 | expectedResults: { 53 | sql: '((SELECT first_name FROM people) UNION ALL (SELECT last_name FROM people))', 54 | values: {} 55 | } 56 | } 57 | }, 58 | 'Nested unions': function(sql) { 59 | return { 60 | test: function() { 61 | return sql.$union([ 62 | { $select: { first_name: true, $from: 'people' } }, 63 | { $union: { $all: [ 64 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('K') } } }, 65 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('A') } } } 66 | ]}} 67 | ]); 68 | }, 69 | expectedResults: { 70 | sql: '(SELECT first_name FROM people) UNION ((SELECT last_name FROM people WHERE last_name LIKE $1) UNION ALL (SELECT last_name FROM people WHERE last_name LIKE $2))', 71 | values: { 72 | $1: 'K%', 73 | $2: 'A%' 74 | } 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /sql/operators/intersect/private/distinct/distinct.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class distinct extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | const ARRAY_SYNTAX = ` 8 | [$select] 9 | {[$union]} 10 | {[$intersect]} 11 | {[$except]} 12 | [ INTERSECT ... ]`; 13 | 14 | this.Types({ 15 | Array: { 16 | eachItemOf: { 17 | Object: { syntax: this.Syntax(ARRAY_SYNTAX) } 18 | } 19 | } 20 | }); 21 | } 22 | 23 | postBuild(result) { 24 | return '(' + result + ')'; 25 | } 26 | } 27 | 28 | module.exports = { 29 | definition: distinct, 30 | description: 'Specifies the `UNION [DISTINCT]` Operator.', 31 | supportedBy: { 32 | MariaDB: 'https://mariadb.com/kb/en/library/intersect/', 33 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/queries-union.html', 34 | SQLite: 'https://sqlite.org/syntax/compound-select-stmt.html', 35 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm', 36 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql' 37 | }, 38 | examples: { 39 | Array: { 40 | eachItemOf: { 41 | Object: { 42 | 'Basic Usage': function(sql) { 43 | return { 44 | test: function() { 45 | return sql.$intersect({ 46 | $distinct: [ 47 | { $select: { first_name: true, $from: 'people' } }, 48 | { $select: { last_name: true, $from: 'people' } } 49 | ] 50 | }); 51 | }, 52 | expectedResults: { 53 | sql: '((SELECT first_name FROM people) INTERSECT (SELECT last_name FROM people))', 54 | values: {} 55 | } 56 | } 57 | }, 58 | 'Nested unions': function(sql) { 59 | return { 60 | test: function() { 61 | return sql.$union([ 62 | { $select: { first_name: true, $from: 'people' } }, 63 | { $intersect: { 64 | $distinct: [ 65 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('K') } } }, 66 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('A') } } } 67 | ] 68 | }} 69 | ]); 70 | }, 71 | expectedResults: { 72 | sql: '(SELECT first_name FROM people) UNION ((SELECT last_name FROM people WHERE last_name LIKE $1) INTERSECT (SELECT last_name FROM people WHERE last_name LIKE $2))', 73 | values: { 74 | $1: 'K%', 75 | $2: 'A%' 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /sql/operators/select/private/groupBy/private/.rollupHelper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class rollupHelper extends SQLBuilder.SQLHelper { 4 | constructor(sql, name){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | eachItemOf: { 10 | Number: { 11 | syntax: { 12 | 1: this.Syntax('[ , ... ]') 13 | } 14 | }, 15 | Array: { syntax: this.Syntax('([ , ... ])') }, 16 | Object: { syntax: this.Syntax('([ , ... ])') } 17 | } 18 | } 19 | }); 20 | 21 | // set the name, otherwise all classes that inherit will named "rollupHelper" 22 | let helperName = '$' + name; 23 | this.__name__ = helperName; 24 | 25 | this.link = function(query) { 26 | /* turn objected rollup definition 27 | * into an array-definition because we can't use 28 | * the round brackets on an object concat each key:value pair 29 | { 30 | "$rollup": { 31 | "state": 1, 32 | "grp1": { 33 | "state": 1, 34 | "city": 1 35 | }, 36 | "grp2": { 37 | "state": 1, 38 | "city": 1, 39 | "sales_manager": 1 40 | } 41 | } 42 | } 43 | 44 | so we turn the object for grp1, grp2 above into an array like: 45 | { 46 | "$rollup": { 47 | "state": 1, 48 | "grp1": ['state', 'city'] 49 | "grp2": ['state', 'city', 'sales_manager'] 50 | } 51 | }*/ 52 | this.forEach(query[helperName], (value, key) => { 53 | // check for identifier and PlainObject 54 | if (this.isIdentifier(key) && this.isPlainObject(value)) { 55 | query[helperName][key] = []; 56 | this.forEach(value, (active, column) => { 57 | if (active && (this.isNumber(active) || this.isBoolean(active))) { 58 | query[helperName][key].push(column) 59 | } 60 | }); 61 | } 62 | }); 63 | } 64 | } 65 | } 66 | 67 | module.exports = rollupHelper; 68 | -------------------------------------------------------------------------------- /sql/operators/select/private/having/README.md: -------------------------------------------------------------------------------- 1 | # having Helper 2 | Specifies the `HAVING` clause for the `SELECT` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/select/) 7 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/sql-select.html) 8 | - [SQLite](https://sqlite.org/lang_select.html) 9 | - [Oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm) 10 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/select-having-transact-sql) 11 | 12 | # Allowed Types and Usage 13 | 14 | ## as Object: 15 | 16 | Usage of `having` as **Object** with the following Syntax: 17 | 18 | **Syntax:** 19 | 20 | ```javascript 21 | $having: { ... } 22 | ``` 23 | 24 | **SQL-Definition:** 25 | ```javascript 26 | [ ... ] 27 | ``` 28 | 29 | :bulb: **Example:** 30 | ```javascript 31 | function() { 32 | return sql.build({ 33 | $select: { 34 | city: 1, 35 | total_salary_by_city: sql.sum('salary'), 36 | $from: 'people', 37 | $groupBy: 'city', 38 | $having: { 39 | $sum: 'salary', 40 | $gt: 450000 41 | } 42 | } 43 | }); 44 | } 45 | 46 | // SQL output 47 | SELECT 48 | city, 49 | SUM(salary) AS total_salary_by_city 50 | FROM 51 | people 52 | GROUP BY 53 | city 54 | HAVING 55 | SUM(salary) > $1 56 | 57 | // Values 58 | { 59 | "$1": 450000 60 | } 61 | ``` 62 | 63 | ## Further Examples 64 | 65 | :bulb: **Usage with $and** 66 | ```javascript 67 | function() { 68 | return sql.build({ 69 | $select: { 70 | city: 1, 71 | total_salary_by_city: sql.sum('salary'), 72 | $from: 'people', 73 | $groupBy: 'city', 74 | $having: { 75 | $and: [ 76 | { $anyExpr: { $sum: 'salary', $gt: 450000 } }, 77 | { $anyExpr: { $sum: 'salary', $lt: 990000 } } 78 | ] 79 | } 80 | } 81 | }); 82 | } 83 | 84 | // SQL output 85 | SELECT 86 | city, 87 | SUM(salary) AS total_salary_by_city 88 | FROM 89 | people 90 | GROUP BY 91 | city 92 | HAVING 93 | SUM(salary) > $1 94 | AND SUM(salary) < $2 95 | 96 | // Values 97 | { 98 | "$1": 450000, 99 | "$2": 990000 100 | } 101 | ``` 102 | 103 | -------------------------------------------------------------------------------- /sql/operators/select/private/having/having.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class having extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | Object: { 9 | syntax: this.Syntax('[ ... ]') 10 | } 11 | }); 12 | } 13 | 14 | preBuild(query) { 15 | // by defaut move all conditions to $and 16 | // if there is no $and / $or defined 17 | if (!query.$and && !query.$or) { 18 | let andItems = []; 19 | this.forEach(query, (value, key)=>{ 20 | if (this.isIdentifier(key)) { 21 | let o = {}; o[key]=value; 22 | andItems.push(o); 23 | } 24 | }); 25 | if (andItems.length > 0) { 26 | query.$and = andItems; //_.cloneDeep(andItems); 27 | this.forEach(query, (value, key)=>{ 28 | if (this.isIdentifier(key)) { 29 | delete query[key]; 30 | } 31 | }); 32 | } 33 | } 34 | 35 | return query; 36 | } 37 | } 38 | 39 | module.exports = { 40 | definition: having, 41 | description: 'Specifies the `HAVING` clause for the `SELECT` Statement.', 42 | supportedBy: { 43 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 44 | MariaDB: 'https://mariadb.com/kb/en/library/select/', 45 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html', 46 | SQLite: 'https://sqlite.org/lang_select.html', 47 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm', 48 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/select-having-transact-sql' 49 | }, 50 | examples: { 51 | Object: { 52 | 'Basic Usage': function(sql) { 53 | return { 54 | test: function(){ 55 | return sql.build({ 56 | $select: { 57 | city: 1, 58 | total_salary_by_city: sql.sum('salary'), 59 | $from: 'people', 60 | $groupBy: 'city', 61 | $having: { 62 | $sum: 'salary', $gt: 450000 63 | } 64 | } 65 | }); 66 | }, 67 | expectedResults: { 68 | sql: 'SELECT city, SUM(salary) AS total_salary_by_city FROM people GROUP BY city HAVING SUM(salary) > $1', 69 | values:{ 70 | $1: 450000 71 | } 72 | } 73 | } 74 | }, 75 | 'Usage with $and': function(sql) { 76 | return { 77 | test: function(){ 78 | return sql.build({ 79 | $select: { 80 | city: 1, 81 | total_salary_by_city: sql.sum('salary'), 82 | $from: 'people', 83 | $groupBy: 'city', 84 | $having: { 85 | $and: [ 86 | { $anyExpr: { $sum: 'salary', $gt: 450000 } }, 87 | { $anyExpr: { $sum: 'salary', $lt: 990000 } } 88 | ] 89 | } 90 | } 91 | }); 92 | }, 93 | expectedResults: { 94 | sql: 'SELECT city, SUM(salary) AS total_salary_by_city FROM people GROUP BY city HAVING SUM(salary) > $1 AND SUM(salary) < $2', 95 | values:{ 96 | $1: 450000, 97 | $2: 990000 98 | } 99 | } 100 | } 101 | } 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /sql/operators/select/private/into/private/dumpfile/README.md: -------------------------------------------------------------------------------- 1 | # dumpfile Helper 2 | Specifies the `DUMPFILE` option for the `SELECT ... INTO` Statement. 3 | 4 | #### Supported by 5 | - [MySQL](https://dev.mysql.com/doc/refman/5.7/en/select.html) 6 | - [MariaDB](https://mariadb.com/kb/en/library/select-into-dumpfile/) 7 | 8 | # Allowed Types and Usage 9 | 10 | ## as String: 11 | 12 | Usage of `dumpfile` as **String** with the following Syntax: 13 | 14 | **Syntax:** 15 | 16 | ```javascript 17 | $dumpfile: < String > 18 | ``` 19 | 20 | **SQL-Definition:** 21 | ```javascript 22 | 23 | ``` 24 | 25 | :bulb: **Example:** 26 | ```javascript 27 | function() { 28 | let query = sql.build({ 29 | $select: { 30 | photo: 1, 31 | $into: { $dumpfile: '/tmp/peoplephoto.jpg' }, 32 | $from: 'people', 33 | $limit: 1 34 | } 35 | }); 36 | 37 | return query; 38 | } 39 | 40 | // SQL output 41 | SELECT 42 | photo INTO DUMPFILE $1 43 | FROM 44 | people 45 | LIMIT 46 | $2 47 | 48 | // Values 49 | { 50 | "$1": "/tmp/peoplephoto.jpg", 51 | "$2": 1 52 | } 53 | ``` 54 | 55 | -------------------------------------------------------------------------------- /sql/operators/select/private/into/private/dumpfile/dumpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class dumpfile extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | this.Types({ 8 | String: { syntax: this.Syntax('') }, 9 | }); 10 | } 11 | } 12 | 13 | module.exports = { 14 | definition: dumpfile, 15 | description: 'Specifies the `DUMPFILE` option for the `SELECT ... INTO` Statement.', 16 | supportedBy: { 17 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html', 18 | MariaDB: 'https://mariadb.com/kb/en/library/select-into-dumpfile/', 19 | }, 20 | examples: { 21 | String: { 22 | 'Basic Usage': function(sql) { 23 | return { 24 | test: function() { 25 | let query = sql.build({ 26 | $select: { 27 | photo: 1, $into: { $dumpfile: '/tmp/peoplephoto.jpg'}, 28 | $from: 'people', 29 | $limit: 1 30 | } 31 | }); 32 | 33 | return query; 34 | }, 35 | expectedResults: { 36 | sql: 'SELECT photo INTO DUMPFILE $1 FROM people LIMIT $2', 37 | values: { 38 | $1: '/tmp/peoplephoto.jpg', 39 | $2: 1 40 | } 41 | } 42 | } 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /sql/operators/select/private/into/private/outfile/outfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const SYNTAX = 4 | `<$file> 5 | { FIELDS TERMINATED BY [$fieldTerminator]} 6 | { ENCLOSED BY [$enclosedBy]} 7 | { ESCAPED BY [$escapedBy]} 8 | { LINES TERMINATED BY [$lineTerminator]}`; 9 | 10 | class outfile extends SQLBuilder.SQLHelper { 11 | constructor(sql){ 12 | super(sql); 13 | 14 | this.Types({ 15 | String: { syntax: this.Syntax('') }, 16 | Object: { syntax: this.Syntax(SYNTAX) } 17 | }); 18 | 19 | this.$file = new SQLBuilder.SQLPredefined.StringValueParam(sql); 20 | this.$fieldTerminator = new SQLBuilder.SQLPredefined.StringValueParam(sql); 21 | this.$enclosedBy = new SQLBuilder.SQLPredefined.StringValueParam(sql); 22 | this.$escapedBy = new SQLBuilder.SQLPredefined.StringValueParam(sql); 23 | this.$lineTerminator = new SQLBuilder.SQLPredefined.StringValueParam(sql) 24 | } 25 | } 26 | 27 | module.exports = { 28 | definition: outfile, 29 | description: 'Specifies the `OUTFILE` option for the `SELECT ... INTO` Statement.', 30 | supportedBy: { 31 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select-into.html', 32 | MariaDB: 'https://mariadb.com/kb/en/library/selectinto/', 33 | }, 34 | examples: { 35 | String: { 36 | 'Basic Usage': function(sql) { 37 | return { 38 | test: function() { 39 | let query = sql.build({ 40 | $select: { 41 | $into: { $outfile: '/tmp/peopledata.txt'}, 42 | $from: 'people', 43 | $limit: 1 44 | } 45 | }); 46 | 47 | return query; 48 | }, 49 | expectedResults: { 50 | sql: 'SELECT * INTO OUTFILE $1 FROM people LIMIT $2', 51 | values: { 52 | $1: '/tmp/peopledata.txt', 53 | $2: 1 54 | } 55 | } 56 | } 57 | } 58 | }, 59 | Object: { 60 | 'Basic Usage': function(sql) { 61 | return { 62 | test: function() { 63 | let query = sql.build({ 64 | $select: { 65 | people_id: 1, 66 | first_name: 1, 67 | last_name: 1, 68 | $into: { 69 | $outfile: { 70 | $file: '/tmp/peopledata.csv', 71 | $fieldTerminator: ',', 72 | $enclosedBy: '"', 73 | $lineTerminator: '\n' 74 | } 75 | }, 76 | $from: 'people' 77 | } 78 | }); 79 | 80 | return query; 81 | }, 82 | expectedResults: { 83 | sql: 'SELECT people_id, first_name, last_name INTO OUTFILE $1 FIELDS TERMINATED BY $2 ENCLOSED BY $3 LINES TERMINATED BY $4 FROM people', 84 | values: { 85 | $1: '/tmp/peopledata.csv', 86 | $2: ',', 87 | $3: '"', 88 | $4: '\n' 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /sql/operators/select/private/join/private/.joinHelper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sqlJoin = { 4 | left: 'LEFT JOIN' 5 | } 6 | class joinHelper extends SQLBuilder.SQLHelper { 7 | constructor(sql, name){ 8 | super(sql); 9 | let aliasKeyword = sql.isOracle() ? ' ' : ' AS '; 10 | this.Types({ 11 | String: { syntax: this.Syntax('' + aliasKeyword + '') }, 12 | Object: { syntax: this.Syntax('' + aliasKeyword + '') }, 13 | Function: { syntax: this.Syntax('' + aliasKeyword + '') }, 14 | }); 15 | 16 | // set the name, otherwise all classes that inherit will named "joinHelper" 17 | let helperName = '$' + name; 18 | this.__name__ = helperName; 19 | } 20 | } 21 | 22 | module.exports = joinHelper; 23 | -------------------------------------------------------------------------------- /sql/operators/union/private/all/all.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class all extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | const ARRAY_SYNTAX = ` 8 | [$select] 9 | {[$union]} 10 | {[$intersect]} 11 | {[$except]} 12 | [ UNION ALL ... ]`; 13 | 14 | this.Types({ 15 | Array: { 16 | eachItemOf: { 17 | Object: { syntax: this.Syntax(ARRAY_SYNTAX) } 18 | } 19 | } 20 | }); 21 | } 22 | 23 | postBuild(result) { 24 | return '(' + result + ')'; 25 | } 26 | } 27 | 28 | module.exports = { 29 | definition: all, 30 | description: 'Specifies the `UNION ALL` Operator.', 31 | supportedBy: { 32 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/union.html', 33 | MariaDB: 'https://mariadb.com/kb/en/library/union', 34 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/queries-union.html', 35 | SQLite: 'https://sqlite.org/syntax/compound-select-stmt.html', 36 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm', 37 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql' 38 | }, 39 | examples: { 40 | Array: { 41 | eachItemOf: { 42 | Object: { 43 | 'Basic Usage': function(sql) { 44 | return { 45 | test: function() { 46 | return sql.$union({ 47 | $all: [ 48 | { $select: { first_name: true, $from: 'people' } }, 49 | { $select: { last_name: true, $from: 'people' } } 50 | ] 51 | }); 52 | }, 53 | expectedResults: { 54 | sql: '((SELECT first_name FROM people) UNION ALL (SELECT last_name FROM people))', 55 | values: {} 56 | } 57 | } 58 | }, 59 | 'Nested unions': function(sql) { 60 | return { 61 | test: function() { 62 | return sql.$union([ 63 | { $select: { first_name: true, $from: 'people' } }, 64 | { $union: { $all: [ 65 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('K') } } }, 66 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('A') } } } 67 | ]}} 68 | ]); 69 | }, 70 | expectedResults: { 71 | sql: '(SELECT first_name FROM people) UNION ((SELECT last_name FROM people WHERE last_name LIKE $1) UNION ALL (SELECT last_name FROM people WHERE last_name LIKE $2))', 72 | values: { 73 | $1: 'K%', 74 | $2: 'A%' 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /sql/operators/union/private/distinct/distinct.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class distinct extends SQLBuilder.SQLHelper { 4 | constructor(sql){ 5 | super(sql); 6 | 7 | const ARRAY_SYNTAX = ` 8 | [$select] 9 | {[$union]} 10 | {[$intersect]} 11 | {[$except]} 12 | [ UNION ... ]`; 13 | 14 | this.Types({ 15 | Array: { 16 | eachItemOf: { 17 | Object: { syntax: this.Syntax(ARRAY_SYNTAX) } 18 | } 19 | } 20 | }); 21 | } 22 | 23 | postBuild(result) { 24 | return '(' + result + ')'; 25 | } 26 | } 27 | 28 | module.exports = { 29 | definition: distinct, 30 | description: 'Specifies the `UNION [DISTINCT]` Operator.', 31 | supportedBy: { 32 | MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/union.html', 33 | MariaDB: 'https://mariadb.com/kb/en/library/union', 34 | PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/queries-union.html', 35 | SQLite: 'https://sqlite.org/syntax/compound-select-stmt.html', 36 | Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm', 37 | SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql' 38 | }, 39 | examples: { 40 | Array: { 41 | eachItemOf: { 42 | Object: { 43 | 'Basic Usage': function(sql) { 44 | return { 45 | test: function() { 46 | return sql.$union({ 47 | $all: [ 48 | { $select: { first_name: true, $from: 'people' } }, 49 | { $select: { last_name: true, $from: 'people' } } 50 | ] 51 | }); 52 | }, 53 | expectedResults: { 54 | sql: '((SELECT first_name FROM people) UNION ALL (SELECT last_name FROM people))', 55 | values: {} 56 | } 57 | } 58 | }, 59 | 'Nested unions': function(sql) { 60 | return { 61 | test: function() { 62 | return sql.$union([ 63 | { $select: { first_name: true, $from: 'people' } }, 64 | { $union: { $all: [ 65 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('K') } } }, 66 | { $select: { last_name: true, $from: 'people', $where: { last_name: sql.startsWith('A') } } } 67 | ]}} 68 | ]); 69 | }, 70 | expectedResults: { 71 | sql: '(SELECT first_name FROM people) UNION ((SELECT last_name FROM people WHERE last_name LIKE $1) UNION ALL (SELECT last_name FROM people WHERE last_name LIKE $2))', 72 | values: { 73 | $1: 'K%', 74 | $2: 'A%' 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /sql/operators/with/private/query/README.md: -------------------------------------------------------------------------------- 1 | # query Operator 2 | Specifies the `$query` part of the Common Table Expression (CTE). 3 | 4 | #### Supported by 5 | - [MariaDB](https://mariadb.com/kb/en/library/with/) 6 | - [PostgreSQL](https://www.postgresql.org/docs/9.5/static/queries-with.html) 7 | - [SQLite](https://sqlite.org/syntax/with-clause.html) 8 | - [Oracle](https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#SQLRF01702) 9 | - [SQLServer](https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql) 10 | 11 | # Allowed Types and Usage 12 | 13 | ## as Object: 14 | 15 | Usage of `query` as **Object** with the following Syntax: 16 | 17 | **Syntax:** 18 | 19 | ```javascript 20 | $query: { ... } 21 | ``` 22 | 23 | **SQL-Definition:** 24 | ```javascript 25 | 26 | ``` 27 | 28 | :bulb: **Example:** 29 | ```javascript 30 | function() { 31 | return sql.$with({ 32 | $recursive: true, 33 | $cte: { 34 | t: { 35 | $columns: { 36 | n: true 37 | }, 38 | $union: { 39 | $all: [{ 40 | $select: { 41 | 1: 'n' 42 | } 43 | }, { 44 | $select: { 45 | n: { $add: ['n', 1] }, 46 | $from: 't' 47 | } 48 | }] 49 | } 50 | } 51 | }, 52 | $query: { 53 | $select: { 54 | n: true, 55 | $from: 't', 56 | $limit: 10 57 | } 58 | } 59 | }); 60 | } 61 | 62 | // SQL output 63 | WITH RECURSIVE t(n) AS ( 64 | ( 65 | ( 66 | SELECT 67 | 1 AS n 68 | ) 69 | UNION ALL 70 | ( 71 | SELECT 72 | n + $1 AS n 73 | FROM 74 | t 75 | ) 76 | ) 77 | ) 78 | SELECT 79 | n 80 | FROM 81 | t 82 | LIMIT 83 | $2 84 | 85 | // Values 86 | { 87 | "$1": 1, 88 | "$2": 10 89 | } 90 | ``` 91 | 92 | -------------------------------------------------------------------------------- /tests/internal-tests.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const expect = require('chai').expect; 4 | const SQLBuilder = require('../index'); 5 | 6 | describe('SQLBuilder internal Tests', function() { 7 | 8 | describe('Quoted identifiers', function() { 9 | describe('Option quoteIdentifiers', function() { 10 | it('should always quote each identifier', function() { 11 | let sql = new SQLBuilder('PostgreSQL', { 12 | quoteIdentifiers: true 13 | }); 14 | let myQuery = sql.$select({ 15 | first_name: { $left: 1 }, 16 | $from: 'people' 17 | }); 18 | 19 | expect(myQuery.sql).to.equal('SELECT LEFT("first_name", $1) AS "first_name" FROM "people"'); 20 | }); 21 | }); 22 | 23 | describe('Reserved words', function() { 24 | it('should quote identifiers that are reserved Words', function() { 25 | let sql = new SQLBuilder('PostgreSQL'); 26 | let myQuery = sql.$select({ 27 | when: { $left: { $str: '~~first_name', $len: 1 } }, 28 | $from: 'people' 29 | }); 30 | 31 | expect(myQuery.sql).to.equal('SELECT LEFT(first_name, $1) AS "when" FROM people'); 32 | }); 33 | }); 34 | 35 | }) 36 | }) 37 | --------------------------------------------------------------------------------