@fxjs/sql-query
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -1,3 +0,3 @@ | ||
/// <reference path="../@types/index.d.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CreateQuery = void 0; | ||
/** | ||
@@ -4,0 +4,0 @@ * Instantiate a new CREATE-type query builder |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mssql = exports.sqlite = exports.mysql = void 0; | ||
const mysql = require("./mysql"); | ||
exports.mysql = mysql; | ||
// import * as postgresql from './postgresql' | ||
const postgresql = require('./postgresql'); | ||
exports.postgresql = postgresql; | ||
// const postgresql = require('./postgresql') | ||
// export { postgresql } | ||
const sqlite = require("./sqlite"); | ||
@@ -8,0 +8,0 @@ exports.sqlite = sqlite; |
@@ -1,2 +0,1 @@ | ||
/// <reference path="../../@types/index.d.ts" /> | ||
const util = require("util"); | ||
@@ -3,0 +2,0 @@ const helpers = require("../Helpers"); |
@@ -1,7 +0,8 @@ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Transforms: | ||
// "name LIKE ? AND age > ?", ["John", 23] | ||
// into: | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cutOffOrderDirectionFromColumnFirstStr = exports.escapeValForKnex = exports.bufferToString = exports.ensureNumber = exports.ucfirst = exports.DialectTypes = exports.defaultTableAliasNameRule = exports.autoIncreatementTableIndex = exports.pickColumnAsFromSelectFieldsDescriptor = exports.pickAliasFromFromDescriptor = exports.parseTableInputStr = exports.get_table_alias = exports.zeroPad = exports.dateToString = exports.escapeQuery = void 0; | ||
// "name LIKE 'John' AND age > 23" | ||
exports.escapeQuery = function (Dialect, query, args) { | ||
function escapeQuery(Dialect, query, args) { | ||
let pos = 0; | ||
@@ -16,4 +17,5 @@ return query.replace(/\?{1,2}/g, function (match) { | ||
}); | ||
}; | ||
exports.dateToString = function (date, timeZone, opts) { | ||
} | ||
exports.escapeQuery = escapeQuery; | ||
function dateToString(date, timeZone, opts) { | ||
const dt = new Date(date); | ||
@@ -28,8 +30,8 @@ if (timeZone != 'local') { | ||
const year = dt.getFullYear(); | ||
const month = exports.zeroPad(dt.getMonth() + 1); | ||
const day = exports.zeroPad(dt.getDate()); | ||
const hour = exports.zeroPad(dt.getHours()); | ||
const minute = exports.zeroPad(dt.getMinutes()); | ||
const second = exports.zeroPad(dt.getSeconds()); | ||
const milli = exports.zeroPad(dt.getMilliseconds(), 3); | ||
const month = zeroPad(dt.getMonth() + 1); | ||
const day = zeroPad(dt.getDate()); | ||
const hour = zeroPad(dt.getHours()); | ||
const minute = zeroPad(dt.getMinutes()); | ||
const second = zeroPad(dt.getSeconds()); | ||
const milli = zeroPad(dt.getMilliseconds(), 3); | ||
if (opts.dialect == 'mysql' || timeZone == 'local') { | ||
@@ -41,4 +43,5 @@ return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milli; | ||
} | ||
}; | ||
exports.zeroPad = function (number, n = 2) { | ||
} | ||
exports.dateToString = dateToString; | ||
function zeroPad(number, n = 2) { | ||
number = "" + number; | ||
@@ -49,3 +52,4 @@ while (number.length < n) { | ||
return number; | ||
}; | ||
} | ||
exports.zeroPad = zeroPad; | ||
function convertTimezone(tz) { | ||
@@ -78,3 +82,3 @@ if (tz == "Z") | ||
// } | ||
exports.parseTableInputStr = function (table_name) { | ||
function parseTableInputStr(table_name) { | ||
if (!table_name) | ||
@@ -96,3 +100,4 @@ throw `invalid input table_name!`; | ||
return ta_tuple; | ||
}; | ||
} | ||
exports.parseTableInputStr = parseTableInputStr; | ||
function pickAliasFromFromDescriptor(fd) { | ||
@@ -138,4 +143,4 @@ return fd.alias || fd.a; | ||
return "X'" + buffer.toString('hex') + "'"; | ||
case 'postgresql': | ||
return "'\\x" + buffer.toString('hex') + "'"; | ||
// case 'postgresql': | ||
// return "'\\x" + buffer.toString('hex') + "'"; | ||
} | ||
@@ -142,0 +147,0 @@ } |
@@ -1,3 +0,3 @@ | ||
/// <reference path="../@types/index.d.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InsertQuery = void 0; | ||
const Helpers_1 = require("./Helpers"); | ||
@@ -4,0 +4,0 @@ class InsertQuery { |
/// <reference lib="es2017" /> | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Query = exports.Text = exports.comparators = void 0; | ||
const util = require("util"); | ||
@@ -96,1 +107,2 @@ const Helpers_1 = require("./Helpers"); | ||
} | ||
__exportStar(require("./Typo/index"), exports); |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RemoveQuery = void 0; | ||
const util = require("util"); | ||
@@ -3,0 +4,0 @@ const Where = require("./Where"); |
@@ -1,3 +0,3 @@ | ||
/// <reference path="../@types/index.d.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SelectQuery = void 0; | ||
const Helpers_1 = require("./Helpers"); | ||
@@ -4,0 +4,0 @@ const Helpers = require("./Helpers"); |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.build = void 0; | ||
const Helpers_1 = require("./Helpers"); | ||
@@ -3,0 +4,0 @@ function build(knexQueryBuilder, Dialect, set, opts) { |
@@ -1,3 +0,3 @@ | ||
/// <reference path="../@types/index.d.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UpdateQuery = void 0; | ||
const UpdateSet = require("./Set"); | ||
@@ -4,0 +4,0 @@ const Where = require("./Where"); |
@@ -1,3 +0,3 @@ | ||
/// <reference path="../@types/index.d.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.build = void 0; | ||
const Helpers = require("./Helpers"); | ||
@@ -4,0 +4,0 @@ const ComparatorsHash = require("./Comparators"); |
{ | ||
"version": "0.6.1", | ||
"name": "@fxjs/sql-query", | ||
"types": "@types/index.d.ts", | ||
"author": "Richard <richardo2016@gmail.com>", | ||
"description": "Fibjs SQL query builder", | ||
"keywords": [ | ||
"sql", | ||
"query" | ||
], | ||
"license": "MIT", | ||
"version": "0.7.0", | ||
"private": false, | ||
"description": "", | ||
"main": "lib/Query.js", | ||
"types": "typings/Query.d.ts", | ||
"repository": { | ||
"url": "http://github.com/fxjs-modules/sql-query" | ||
"type": "https", | ||
"url": "https://github.com/fxjs-modules/orm/tree/master/packages/sql-query" | ||
}, | ||
"homepage": "https://github.com/fxjs-modules/orm/tree/master/packages/sql-query", | ||
"scripts": { | ||
"build": "fib-typify src -o lib", | ||
"build": "ftsc ./src/* --outDir ./lib", | ||
"test": "fibjs test/run", | ||
"ci": "npm run build && npm run test", | ||
"prepublishOnly": "npm run build", | ||
"build:rollup": "fibjs ./rollup.build.js", | ||
"test": "fibjs test/run", | ||
"test:rollup": "cross-env ENTRY_SUFFIX=.cjs.js fibjs test/vbox", | ||
"ci": "npm run build && npm run test", | ||
"ci:rollup": "npm run build && npm run build:rollup && npm run test:rollup", | ||
"prepublishOnly": "npm run build && npm run ci:rollup" | ||
"ci:rollup": "npm run build && npm run build:rollup && npm run test:rollup" | ||
}, | ||
"main": "./lib/Query", | ||
"unpkg": "./lib/index.cjs.js", | ||
"jsdelivr": "./lib/index.cjs.js", | ||
"engines": { | ||
"fibjs": "*" | ||
}, | ||
"analyse": false, | ||
"author": "richardo2016 <richardo2016@gmail.com>", | ||
"license": "ISC", | ||
"files": [ | ||
"lib", | ||
"typings" | ||
], | ||
"devDependencies": { | ||
"@fibjs/ci": "^2.2.0", | ||
"@fibjs/types": "^0.27.1", | ||
"@fxjs/knex": "^0.2.0", | ||
"@types/node": "^11.13.0", | ||
"@fibjs/types": "^0.31.0", | ||
"@fxjs/knex": "^0.3.0", | ||
"cross-env": "^5.2.0", | ||
"fib-pool": "^1.6.0", | ||
"fib-rollup": "^0.4.0", | ||
"fib-typify": "^0.6.0", | ||
"knex": "^0.19.5", | ||
"fib-typify": "^0.8.3", | ||
"rollup-plugin-commonjs": "^9.2.0" | ||
@@ -45,16 +41,15 @@ }, | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"ci": { | ||
"type": "travis, appveyor", | ||
"version": [ | ||
"0.21.0", | ||
"0.22.0", | ||
"0.23.0", | ||
"0.24.0", | ||
"0.25.0", | ||
"0.26.0", | ||
"0.26.1", | ||
"0.27.0", | ||
"0.28.0" | ||
"0.28.0", | ||
"0.29.0", | ||
"0.30.0", | ||
"0.31.0" | ||
] | ||
} | ||
}, | ||
"gitHead": "1c80cd6d5609c80e4ec66f17f56adf27acc4cc60" | ||
} |
792
README.md
@@ -1,793 +0,5 @@ | ||
# Fibjs SQL query builder | ||
## @fxjs/sql-query | ||
[![NPM version](https://img.shields.io/npm/v/@fxjs/sql-query.svg)](https://www.npmjs.org/package/@fxjs/sql-query) | ||
[![Build Status](https://travis-ci.org/fxjs-modules/sql-query.svg)](https://travis-ci.org/fxjs-modules/sql-query) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/lg7ephk4a1mjdi0l/branch/master?svg=true)](https://ci.appveyor.com/project/richardo2016/sql-query/branch/master) | ||
[![Build Status](https://travis-ci.org/fxjs-modules/orm.svg)](https://travis-ci.org/fxjs-modules/orm) | ||
**NOTICE**: This is [node-sql-query]'s fibjs version, thx a lot to [node-sql-query]'s author : ) | ||
## Install | ||
```sh | ||
npm install sql-query --save | ||
``` | ||
## Dialects | ||
- MySQL | ||
- SQLite | ||
- MSSQL | ||
## About | ||
This module is used by [@fxjs/orm](https://github.com/fxjs-modules/orm) to build SQL queries in the different supported dialects. | ||
Sorry the API documentation is not complete. There are tests in `./test/integration` that you could read. | ||
# Usage | ||
```js | ||
var sql = require('@fxjs/sql-query') | ||
var sqlQuery = sql.Query(); // for dialect: sql.Query('mysql') | ||
``` | ||
## Create | ||
```js | ||
var sqlCreate = sqlQuery.create(); | ||
sqlCreate | ||
.table('table1') | ||
.build() | ||
"CREATE TABLE 'table1'()" | ||
sqlCreate | ||
.table('table1') | ||
.field('id','id') | ||
.build() | ||
"CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT)" | ||
sqlCreate | ||
.table('table1') | ||
.fields({id: 'id', a_text: 'text'}) | ||
.build() | ||
"CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_text' TEXT)" | ||
sqlCreate | ||
.table('table1') | ||
.fields({id: 'id', a_num: 'int'}) | ||
.build() | ||
"CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_num' INTEGER)" | ||
sqlCreate | ||
.table('table1') | ||
.fields({id: 'id', a_num: 'float'}) | ||
.build() | ||
"CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_num' FLOAT(12,2))" | ||
sqlCreate | ||
.table('table1') | ||
.fields({id: 'id', a_bool: 'bool'}) | ||
.build() | ||
"CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_bool' TINYINT(1))" | ||
``` | ||
## Select | ||
```js | ||
var sqlSelect = sqlQuery.select(); | ||
sqlSelect | ||
.from('table1') | ||
.build(); | ||
"SELECT * FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id', 'name') | ||
.build(); | ||
"SELECT `id`, `name` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id', 'name') | ||
.as('label') | ||
.build(); | ||
"SELECT `id`, `name` AS `label` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id', 'name') | ||
.select('title') | ||
.as('label') | ||
.build(); | ||
"SELECT `id`, `name`, `title` AS `label` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id', 'name') | ||
.as('label') | ||
.select('title') | ||
.build(); | ||
"SELECT `id`, `name` AS `label`, `title` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select([ 'id', 'name' ]) | ||
.build(); | ||
"SELECT `id`, `name` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select() | ||
.build(); | ||
"SELECT * FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select(['abc','def', { as: 'ghi', sql: 'SOMEFUNC(ghi)' }]) | ||
.build(); | ||
sqlSelect | ||
.from('table1') | ||
.select(['abc','def', { a: 'ghi', sql: 'SOMEFUNC(ghi)' }]) | ||
.build(); | ||
"SELECT `abc`, `def`, (SOMEFUNC(ghi)) AS `ghi` FROM `table1`" | ||
sqlSelect | ||
.calculateFoundRows() | ||
.from('table1') | ||
.build(); | ||
"SELECT SQL_CALC_FOUND_ROWS * FROM `table1`" | ||
sqlSelect | ||
.calculateFoundRows() | ||
.from('table1') | ||
.select('id') | ||
.build(); | ||
"SELECT SQL_CALC_FOUND_ROWS `id` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id1', 'name') | ||
.from('table2', 'id2', 'id1') | ||
.select('id2') | ||
.build(); | ||
"SELECT `t1`.`id1`, `t1`.`name`, `t2`.`id2` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id1') | ||
.from('table2', 'id2', 'id1', { joinType: 'left inner' }) | ||
.select('id2') | ||
.build(); | ||
"SELECT `t1`.`id1`, `t2`.`id2` FROM `table1` `t1` LEFT INNER JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.select('id1', 'name') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.select('id2') | ||
.build(); | ||
"SELECT `t1`.`id1`, `t1`.`name`, `t2`.`id2` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.count() | ||
.build(); | ||
"SELECT COUNT(*) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.count(null, 'c') | ||
.build(); | ||
"SELECT COUNT(*) AS `c` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.count('id') | ||
.build(); | ||
"SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.count('id') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.count('id') | ||
.build(); | ||
"SELECT COUNT(`t1`.`id`), COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.count('id') | ||
.count('col') | ||
.build(); | ||
"SELECT COUNT(`t2`.`id`), COUNT(`t2`.`col`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id2', 'table1', 'id1') | ||
.fun('AVG', 'col') | ||
.build(); | ||
"SELECT AVG(`t2`.`col`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']) | ||
.count('id') | ||
.build(); | ||
"SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`" | ||
``` | ||
## Where | ||
```js | ||
var sqlSelect = sqlQuery.select(); | ||
sqlSelect | ||
.from('table1') | ||
.where() | ||
.build(); | ||
"SELECT * FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.where(null) | ||
.build(); | ||
"SELECT * FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: 1 }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: 0 }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = 0" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: null }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` IS NULL" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.eq(null) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` IS NULL" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.ne(null) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` IS NOT NULL" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: undefined }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` IS NULL" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: false }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = false" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: "" }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = ''" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: true }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = true" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: 'a' }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = 'a'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: 'a\'' }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = 'a\\''" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: [ 1, 2, 3 ] }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` IN (1, 2, 3)" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: [] }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE FALSE" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col1: 1, col2: 2 }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col1` = 1 AND `col2` = 2" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col1: 1 }, { col2: 2 }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE (`col1` = 1) AND (`col2` = 2)" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: 1 }).where({ col: 2 }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE (`col` = 1) AND (`col` = 2)" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col1: 1, col2: 2 }).where({ col3: 3 }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE (`col1` = 1 AND `col2` = 2) AND (`col3` = 3)" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id', 'id') | ||
.where('table1', { col: 1 }, 'table2', { col: 2 }) | ||
.build(); | ||
"SELECT * FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id` = `t1`.`id` WHERE (`t1`.`col` = 1) AND (`t2`.`col` = 2)" | ||
sqlSelect | ||
.from('table1') | ||
.from('table2', 'id', 'id') | ||
.where('table1', { col: 1 }, { col: 2 }) | ||
.build(); | ||
"SELECT * FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id` = `t1`.`id` WHERE (`t1`.`col` = 1) AND (`col` = 2)" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.gt(1) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` > 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.gte(1) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` >= 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.lt(1) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` < 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.lte(1) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` <= 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.eq(1) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` = 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.ne(1) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` <> 1" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.between('a', 'b') }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` BETWEEN 'a' AND 'b'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.not_between('a', 'b') }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` NOT BETWEEN 'a' AND 'b'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.like('abc') }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` LIKE 'abc'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: | ||
sql.not_like('abc') }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` NOT LIKE 'abc'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: sql.not_in([ 1, 2, 3 ]) }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` NOT IN (1, 2, 3)" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: { | ||
not_in: ([ 1, 2, 3 ]) | ||
}) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` NOT IN (1, 2, 3)" | ||
sqlSelect | ||
.from('table1') | ||
.where({ col: { | ||
between: [ | ||
"2019-03-06T00:00:16.000Z", | ||
"2019-03-06T00:09:16.000Z", | ||
] | ||
}) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE `col` BETWEEN ('2019-03-06T00:00:16.000Z', '2019-03-06T00:09:16.000Z')" | ||
or | ||
// timezone = 'locale' or Dialect.type === `mysql` | ||
"SELECT * FROM `table1` WHERE `col` BETWEEN ('2019-03-06 T00:00:16.000', '2019-03-06 00:09:16.000')" | ||
sqlSelect | ||
.from('table1') | ||
.where({ __sql: [["LOWER(`stuff`) LIKE 'peaches'"]] }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ __sql: [["LOWER(`stuff`) LIKE ?", ['peaches']]] }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches'" | ||
sqlSelect | ||
.from('table1') | ||
.where({ __sql: [["LOWER(`stuff`) LIKE ? AND `number` > ?", ['peaches', 12]]] }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches' AND `number` > 12" | ||
sqlSelect | ||
.from('table1') | ||
.where({ __sql: [["LOWER(`stuff`) LIKE ? AND `number` == ?", ['peaches']]] }) | ||
.build(); | ||
"SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches' AND `number` == NULL" | ||
``` | ||
## Order | ||
```js | ||
var sqlSelect = sqlQuery.select(); | ||
sqlSelect | ||
.from('table1') | ||
.order('col') | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY `col` ASC" | ||
sqlSelect | ||
.from('table1') | ||
.order('col', 'A') | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY `col` ASC" | ||
sqlSelect | ||
.from('table1') | ||
.order('col', 'Z') | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY `col` DESC" | ||
sqlSelect | ||
.from('table1') | ||
.order('col').order('col2', 'Z') | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY `col` ASC, `col2` DESC" | ||
sqlSelect | ||
.from('table1') | ||
.order('col', []) | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY col" | ||
sqlSelect | ||
.from('table1') | ||
.order('?? DESC', ['col']) | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY `col` DESC" | ||
sqlSelect | ||
.from('table1') | ||
.order('ST_Distance(??, ST_GeomFromText(?,4326))', ['geopoint', 'POINT(-68.3394 27.5578)']) | ||
.build(); | ||
"SELECT * FROM `table1` ORDER BY ST_Distance(`geopoint`, ST_GeomFromText('POINT(-68.3394 27.5578)',4326))" | ||
``` | ||
## Limit | ||
```js | ||
var sqlSelect = sqlQuery.select(); | ||
sqlSelect | ||
.from('table1') | ||
.limit(123) | ||
.build(); | ||
"SELECT * FROM `table1` LIMIT 123" | ||
sqlSelect | ||
.from('table1') | ||
.limit('123456789') | ||
.build(); | ||
"SELECT * FROM `table1` LIMIT 123456789" | ||
``` | ||
## Select function | ||
```js | ||
var sqlSelect = sqlQuery.select(); | ||
sqlSelect | ||
.from('table1') | ||
.fun('myfun', 'col1') | ||
.build(); | ||
"SELECT MYFUN(`col1`) FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.fun('myfun', [ 'col1', 'col2']) | ||
.build(); | ||
"SELECT MYFUN(`col1`, `col2`) FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.fun('dbo.fnBalance', [ 80, null, null], 'balance') | ||
.build(); | ||
"SELECT DBO.FNBALANCE(80, NULL, NULL) AS `balance` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.fun('myfun', [ 'col1', 'col2'], 'alias') | ||
.build(); | ||
"SELECT MYFUN(`col1`, `col2`) AS `alias` FROM `table1`" | ||
sqlSelect | ||
.from('table1') | ||
.fun('myfun', [ 'col1', sqlQuery.Text('col2') ], 'alias') | ||
.build(); | ||
"SELECT MYFUN(`col1`, 'col2') AS `alias` FROM `table1`" | ||
``` | ||
## Insert | ||
```js | ||
var sqlInsert = sqlQuery.insert(); | ||
sqlInsert | ||
.into('table1') | ||
.build(); | ||
"INSERT INTO `table1`" | ||
sqlInsert | ||
.into('table1') | ||
.set({}) | ||
.build(); | ||
"INSERT INTO `table1` VALUES()" | ||
sqlInsert | ||
.into('table1') | ||
.set({ col: 1 }) | ||
.build(); | ||
"INSERT INTO `table1` (`col`) VALUES (1)" | ||
sqlInsert | ||
.into('table1') | ||
.set({ col1: 1, col2: 'a' }) | ||
.build(); | ||
"INSERT INTO `table1` (`col1`, `col2`) VALUES (1, 'a')" | ||
``` | ||
## Update | ||
```js | ||
var sqlUpdate = sqlQuery.update() | ||
sqlUpdate | ||
.into('table1') | ||
.build(); | ||
"UPDATE `table1`" | ||
sqlUpdate | ||
.into('table1') | ||
.set({ col: 1 }) | ||
.build(); | ||
"UPDATE `table1` SET `col` = 1" | ||
sqlUpdate | ||
.into('table1') | ||
.set({ col1: 1, col2: 2 }) | ||
.build(); | ||
"UPDATE `table1` SET `col1` = 1, `col2` = 2" | ||
sqlUpdate | ||
.into('table1') | ||
.set({ col1: 1, col2: 2 }).where({ id: 3 }) | ||
.build(); | ||
"UPDATE `table1` SET `col1` = 1, `col2` = 2 WHERE `id` = 3" | ||
``` | ||
[node-sql-query]:./Readme_orig.md |
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
8
51
1
0
92632
1
2397
6