From ab7d878af9db13a791b49737a4c59621d93839dd Mon Sep 17 00:00:00 2001 From: Brian Noah Date: Wed, 16 Sep 2015 15:29:10 -0700 Subject: [PATCH 1/3] update documention --- Readme.md | 635 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 632 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 7bb37a2..f6f4303 100644 --- a/Readme.md +++ b/Readme.md @@ -1,11 +1,11 @@ -## NodeJS SQL query builder +# NodeJS SQL query builder [![Build Status](https://secure.travis-ci.org/dresende/node-sql-query.png?branch=master)](http://travis-ci.org/dresende/node-sql-query) ## Install ```sh -npm install sql-query +npm install sql-query --save ``` ## Dialects @@ -17,4 +17,633 @@ npm install sql-query ## About -This module is used by [ORM](http://dresende.github.com/node-orm2) to build SQL queries in the different supported dialects. Sorry there is no API documentation but there are a couple of tests you can read and find out how to use it if you want. +This module is used by [ORM](http://dresende.github.com/node-orm2) to build SQL queries in the different supported dialects. +Sorry the API documentation is not complete. There are tests in ./test/integration that you can read. + + +#Usage + + var sqlQuery = require('sql-query'); + +##Create + + sqlQuery + .Create() + .table('table1') + .build() + "CREATE TABLE 'table1'()" + + sqlQuery + .Create() + .table('table1') + .field('id','id') + .build() + "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT)" + + sqlQuery + .Create() + .table('table1') + .fields({id: 'id', a_text: 'text'}) + .build() + "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_text' TEXT)" + + sqlQuery + .Create() + .table('table1') + .fields({id: 'id', a_num: 'int'}) + .build() + "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_num' INTEGER)" + + sqlQuery + .Create() + .table('table1') + .fields({id: 'id', a_num: 'float'}) + .build() + "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_num' FLOAT(12,2))" + + sqlQuery + .Create() + .table('table1') + .fields({id: 'id', a_bool: 'bool'}) + .build() + "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_bool' TINYINT(1))" + +##Select + + sqlQuery + .Select() + .from('table1') + .build(); + "SELECT * FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select('id', 'name') + .build(); + "SELECT `id`, `name` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select('id', 'name') + .as('label') + .build(); + "SELECT `id`, `name` AS `label` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select('id', 'name') + .select('title') + .as('label') + .build(); + "SELECT `id`, `name`, `title` AS `label` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select('id', 'name') + .as('label') + .select('title') + .build(); + "SELECT `id`, `name` AS `label`, `title` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select([ 'id', 'name' ]) + .build(); + "SELECT `id`, `name` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select() + .build(); + "SELECT * FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .select(['abc','def', { a: 'ghi', sql: 'SOMEFUNC(ghi)' }]) + .build(); + "SELECT `abc`, `def`, (SOMEFUNC(ghi)) AS `ghi` FROM `table1`" + + sqlQuery + .Select() + .calculateFoundRows() + .from('table1') + .build(); + "SELECT SQL_CALC_FOUND_ROWS * FROM `table1`" + + sqlQuery + .Select() + .calculateFoundRows() + .from('table1') + .select('id') + .build(); + "SELECT SQL_CALC_FOUND_ROWS `id` FROM `table1`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .from('table1') + .from('table2', 'id2', 'table1', 'id1') + .count() + .build(); + "SELECT COUNT(*) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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`" + + sqlQuery + .Select() + .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 + + sqlQuery + .Select() + .from('table1') + .where() + .build(); + "SELECT * FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .where(null) + .build(); + "SELECT * FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .where({ col: 1 }) + .build(); + "SELECT * FROM `table1` WHERE `col` = 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: 0 }) + .build(); + "SELECT * FROM `table1` WHERE `col` = 0" + + sqlQuery + .Select() + .from('table1') + .where({ col: null }) + .build(); + "SELECT * FROM `table1` WHERE `col` IS NULL" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.eq(null) }) + .build(); + "SELECT * FROM `table1` WHERE `col` IS NULL" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.ne(null) }) + .build(); + "SELECT * FROM `table1` WHERE `col` IS NOT NULL" + + sqlQuery + .Select() + .from('table1') + .where({ col: undefined }) + .build(); + "SELECT * FROM `table1` WHERE `col` IS NULL" + + sqlQuery + .Select() + .from('table1') + .where({ col: false }) + .build(); + "SELECT * FROM `table1` WHERE `col` = false" + + sqlQuery + .Select() + .from('table1') + .where({ col: "" }) + .build(); + "SELECT * FROM `table1` WHERE `col` = ''" + + sqlQuery + .Select() + .from('table1') + .where({ col: true }) + .build(); + "SELECT * FROM `table1` WHERE `col` = true" + + sqlQuery + .Select() + .from('table1') + .where({ col: 'a' }) + .build(); + "SELECT * FROM `table1` WHERE `col` = 'a'" + + sqlQuery + .Select() + .from('table1') + .where({ col: 'a\'' }) + .build(); + "SELECT * FROM `table1` WHERE `col` = 'a\\''" + + sqlQuery + .Select() + .from('table1') + .where({ col: [ 1, 2, 3 ] }) + .build(); + "SELECT * FROM `table1` WHERE `col` IN (1, 2, 3)" + + sqlQuery + .Select() + .from('table1') + .where({ col: [] }) + .build(); + "SELECT * FROM `table1` WHERE FALSE" + + sqlQuery + .Select() + .from('table1') + .where({ col1: 1, col2: 2 }) + .build(); + "SELECT * FROM `table1` WHERE `col1` = 1 AND `col2` = 2" + + sqlQuery + .Select() + .from('table1') + .where({ col1: 1 }, { col2: 2 }) + .build(); + "SELECT * FROM `table1` WHERE (`col1` = 1) AND (`col2` = 2)" + + sqlQuery + .Select() + .from('table1') + .where({ col: 1 }).where({ col: 2 }) + .build(); + "SELECT * FROM `table1` WHERE (`col` = 1) AND (`col` = 2)" + + sqlQuery + .Select() + .from('table1') + .where({ col1: 1, col2: 2 }).where({ col3: 3 }) + .build(); + "SELECT * FROM `table1` WHERE (`col1` = 1 AND `col2` = 2) AND (`col3` = 3)" + + sqlQuery + .Select() + .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)" + + sqlQuery + .Select() + .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)" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.gt(1) }) + .build(); + "SELECT * FROM `table1` WHERE `col` > 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.gte(1) }) + .build(); + "SELECT * FROM `table1` WHERE `col` >= 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.lt(1) }) + .build(); + "SELECT * FROM `table1` WHERE `col` < 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.lte(1) }) + .build(); + "SELECT * FROM `table1` WHERE `col` <= 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.eq(1) }) + .build(); + "SELECT * FROM `table1` WHERE `col` = 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.ne(1) }) + .build(); + "SELECT * FROM `table1` WHERE `col` <> 1" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.between('a', 'b') }) + .build(); + "SELECT * FROM `table1` WHERE `col` BETWEEN 'a' AND 'b'" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.not_between('a', 'b') }) + .build(); + "SELECT * FROM `table1` WHERE `col` NOT BETWEEN 'a' AND 'b'" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.like('abc') }) + .build(); + "SELECT * FROM `table1` WHERE `col` LIKE 'abc'" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.not_like('abc') }) + .build(); + "SELECT * FROM `table1` WHERE `col` NOT LIKE 'abc'" + + sqlQuery + .Select() + .from('table1') + .where({ col: sqlQuery.Query.not_in([ 1, 2, 3 ]) }) + .build(); + "SELECT * FROM `table1` WHERE `col` NOT IN (1, 2, 3)" + + sqlQuery + .Select() + .from('table1') + .where({ __sql: [["LOWER(`stuff`) LIKE 'peaches'"]] }) + .build(); + "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches'" + + sqlQuery + .Select() + .from('table1') + .where({ __sql: [["LOWER(`stuff`) LIKE ?", ['peaches']]] }) + .build(); + "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches'" + + sqlQuery + .Select() + .from('table1') + .where({ __sql: [["LOWER(`stuff`) LIKE ? AND `number` > ?", ['peaches', 12]]] }) + .build(); + "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches' AND `number` > 12" + + sqlQuery + .Select() + .from('table1') + .where({ __sql: [["LOWER(`stuff`) LIKE ? AND `number` == ?", ['peaches']]] }) + .build(); + "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches' AND `number` == NULL" + +##Order + + sqlQuery + .Select() + .from('table1') + .order('col') + .build(); + "SELECT * FROM `table1` ORDER BY `col` ASC" + + sqlQuery + .Select() + .from('table1') + .order('col', 'A') + .build(); + "SELECT * FROM `table1` ORDER BY `col` ASC" + + sqlQuery + .Select() + .from('table1') + .order('col', 'Z') + .build(); + "SELECT * FROM `table1` ORDER BY `col` DESC" + + sqlQuery + .Select() + .from('table1') + .order('col').order('col2', 'Z') + .build(); + "SELECT * FROM `table1` ORDER BY `col` ASC, `col2` DESC" + + sqlQuery + .Select() + .from('table1') + .order('col', []) + .build(); + "SELECT * FROM `table1` ORDER BY col" + + sqlQuery + .Select() + .from('table1') + .order('?? DESC', ['col']) + .build(); + "SELECT * FROM `table1` ORDER BY `col` DESC" + + sqlQuery + .Select() + .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 + + sqlQuery + .Select() + .from('table1') + .limit(123) + .build(); + "SELECT * FROM `table1` LIMIT 123" + + sqlQuery + .Select() + .from('table1') + .limit('123456789') + .build(); + "SELECT * FROM `table1` LIMIT 123456789" + +##Select Function + + sqlQuery + .Select() + .from('table1') + .fun('myfun', 'col1') + .build(); + "SELECT MYFUN(`col1`) FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .fun('myfun', [ 'col1', 'col2']) + .build(); + "SELECT MYFUN(`col1`, `col2`) FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .fun('dbo.fnBalance', [ 80, null, null], 'balance') + .build(); + "SELECT DBO.FNBALANCE(80, NULL, NULL) AS `balance` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .fun('myfun', [ 'col1', 'col2'], 'alias') + .build(); + "SELECT MYFUN(`col1`, `col2`) AS `alias` FROM `table1`" + + sqlQuery + .Select() + .from('table1') + .fun('myfun', [ 'col1', sqlQuery.Text('col2') ], 'alias') + .build(); + "SELECT MYFUN(`col1`, 'col2') AS `alias` FROM `table1`" + +##Insert + + sqlQuery + .Insert() + .into('table1') + .build(); + "INSERT INTO `table1`" + + sqlQuery + .Insert() + .into('table1') + .set({}) + .build(); + "INSERT INTO `table1` VALUES()" + + sqlQuery + .Insert() + .into('table1') + .set({ col: 1 }) + .build(); + "INSERT INTO `table1` (`col`) VALUES (1)" + + sqlQuery + .Insert() + .into('table1') + .set({ col1: 1, col2: 'a' }) + .build(); + "INSERT INTO `table1` (`col1`, `col2`) VALUES (1, 'a')" + +##Update + + sqlQuery + .Update() + .into('table1') + .build(); + "UPDATE `table1`" + + sqlQuery + .Update() + .into('table1') + .set({ col: 1 }) + .build(); + "UPDATE `table1` SET `col` = 1" + + sqlQuery + .Update() + .into('table1') + .set({ col1: 1, col2: 2 }) + .build(); + "UPDATE `table1` SET `col1` = 1, `col2` = 2" + + sqlQuery + .Update() + .into('table1') + .set({ col1: 1, col2: 2 }).where({ id: 3 }) + .build(); + "UPDATE `table1` SET `col1` = 1, `col2` = 2 WHERE `id` = 3" From 3f85a8de732aaeb996da002ab163da6ed90fcc26 Mon Sep 17 00:00:00 2001 From: Brian Noah Date: Thu, 17 Sep 2015 13:54:11 -0700 Subject: [PATCH 2/3] update docs to match actual use vs test usage --- Readme.md | 270 ++++++++++++++++++++---------------------------------- 1 file changed, 101 insertions(+), 169 deletions(-) diff --git a/Readme.md b/Readme.md index f6f4303..a0e7b07 100644 --- a/Readme.md +++ b/Readme.md @@ -23,46 +23,42 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati #Usage - var sqlQuery = require('sql-query'); + var sqlQuery = require('sql-query').Query(); ##Create - sqlQuery - .Create() + var sqlCreate = sqlQuery.create(); + + sqlCreate .table('table1') .build() "CREATE TABLE 'table1'()" - sqlQuery - .Create() + sqlCreate .table('table1') .field('id','id') .build() "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT)" - sqlQuery - .Create() + sqlCreate .table('table1') .fields({id: 'id', a_text: 'text'}) .build() "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_text' TEXT)" - sqlQuery - .Create() + sqlCreate .table('table1') .fields({id: 'id', a_num: 'int'}) .build() "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_num' INTEGER)" - sqlQuery - .Create() + sqlCreate .table('table1') .fields({id: 'id', a_num: 'float'}) .build() "CREATE TABLE 'table1'('id' INTEGER PRIMARY KEY AUTO_INCREMENT,'a_num' FLOAT(12,2))" - sqlQuery - .Create() + sqlCreate .table('table1') .fields({id: 'id', a_bool: 'bool'}) .build() @@ -70,29 +66,27 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Select - sqlQuery - .Select() + var sqlSelect = sqlQuery.select(); + + sqlSelect .from('table1') .build(); "SELECT * FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id', 'name') .build(); "SELECT `id`, `name` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id', 'name') .as('label') .build(); "SELECT `id`, `name` AS `label` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id', 'name') .select('title') @@ -100,8 +94,7 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT `id`, `name`, `title` AS `label` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id', 'name') .as('label') @@ -109,44 +102,38 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT `id`, `name` AS `label`, `title` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select([ 'id', 'name' ]) .build(); "SELECT `id`, `name` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select() .build(); "SELECT * FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select(['abc','def', { a: 'ghi', sql: 'SOMEFUNC(ghi)' }]) .build(); "SELECT `abc`, `def`, (SOMEFUNC(ghi)) AS `ghi` FROM `table1`" - sqlQuery - .Select() + sqlSelect .calculateFoundRows() .from('table1') .build(); "SELECT SQL_CALC_FOUND_ROWS * FROM `table1`" - sqlQuery - .Select() + sqlSelect .calculateFoundRows() .from('table1') .select('id') .build(); "SELECT SQL_CALC_FOUND_ROWS `id` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id1', 'name') .from('table2', 'id2', 'id1') @@ -154,8 +141,7 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT `t1`.`id1`, `t1`.`name`, `t2`.`id2` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id1') .from('table2', 'id2', 'id1', { joinType: 'left inner' }) @@ -163,8 +149,7 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT `t1`.`id1`, `t2`.`id2` FROM `table1` `t1` LEFT INNER JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" - sqlQuery - .Select() + sqlSelect .from('table1') .select('id1', 'name') .from('table2', 'id2', 'table1', 'id1') @@ -172,32 +157,28 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT `t1`.`id1`, `t1`.`name`, `t2`.`id2` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" - sqlQuery - .Select() + sqlSelect .from('table1') .from('table2', 'id2', 'table1', 'id1') .count() .build(); "SELECT COUNT(*) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" - sqlQuery - .Select() + 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`" - sqlQuery - .Select() + 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`" - sqlQuery - .Select() + sqlSelect .from('table1') .count('id') .from('table2', 'id2', 'table1', 'id1') @@ -205,8 +186,7 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT COUNT(`t1`.`id`), COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" - sqlQuery - .Select() + sqlSelect .from('table1') .from('table2', 'id2', 'table1', 'id1') .count('id') @@ -214,16 +194,14 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati .build(); "SELECT COUNT(`t2`.`id`), COUNT(`t2`.`col`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" - sqlQuery - .Select() + 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`" - sqlQuery - .Select() + sqlSelect .from('table1') .from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']) .count('id') @@ -232,255 +210,221 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Where - sqlQuery - .Select() + var sqlSelect = sqlQuery.select(); + + sqlSelect .from('table1') .where() .build(); "SELECT * FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .where(null) .build(); "SELECT * FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: 1 }) .build(); "SELECT * FROM `table1` WHERE `col` = 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: 0 }) .build(); "SELECT * FROM `table1` WHERE `col` = 0" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: null }) .build(); "SELECT * FROM `table1` WHERE `col` IS NULL" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.eq(null) }) .build(); "SELECT * FROM `table1` WHERE `col` IS NULL" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.ne(null) }) .build(); "SELECT * FROM `table1` WHERE `col` IS NOT NULL" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: undefined }) .build(); "SELECT * FROM `table1` WHERE `col` IS NULL" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: false }) .build(); "SELECT * FROM `table1` WHERE `col` = false" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: "" }) .build(); "SELECT * FROM `table1` WHERE `col` = ''" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: true }) .build(); "SELECT * FROM `table1` WHERE `col` = true" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: 'a' }) .build(); "SELECT * FROM `table1` WHERE `col` = 'a'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: 'a\'' }) .build(); "SELECT * FROM `table1` WHERE `col` = 'a\\''" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: [ 1, 2, 3 ] }) .build(); "SELECT * FROM `table1` WHERE `col` IN (1, 2, 3)" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: [] }) .build(); "SELECT * FROM `table1` WHERE FALSE" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col1: 1, col2: 2 }) .build(); "SELECT * FROM `table1` WHERE `col1` = 1 AND `col2` = 2" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col1: 1 }, { col2: 2 }) .build(); "SELECT * FROM `table1` WHERE (`col1` = 1) AND (`col2` = 2)" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: 1 }).where({ col: 2 }) .build(); "SELECT * FROM `table1` WHERE (`col` = 1) AND (`col` = 2)" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col1: 1, col2: 2 }).where({ col3: 3 }) .build(); "SELECT * FROM `table1` WHERE (`col1` = 1 AND `col2` = 2) AND (`col3` = 3)" - sqlQuery - .Select() + 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)" - sqlQuery - .Select() + 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)" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.gt(1) }) .build(); "SELECT * FROM `table1` WHERE `col` > 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.gte(1) }) .build(); "SELECT * FROM `table1` WHERE `col` >= 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.lt(1) }) .build(); "SELECT * FROM `table1` WHERE `col` < 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.lte(1) }) .build(); "SELECT * FROM `table1` WHERE `col` <= 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.eq(1) }) .build(); "SELECT * FROM `table1` WHERE `col` = 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.ne(1) }) .build(); "SELECT * FROM `table1` WHERE `col` <> 1" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.between('a', 'b') }) .build(); "SELECT * FROM `table1` WHERE `col` BETWEEN 'a' AND 'b'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.not_between('a', 'b') }) .build(); "SELECT * FROM `table1` WHERE `col` NOT BETWEEN 'a' AND 'b'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.like('abc') }) .build(); "SELECT * FROM `table1` WHERE `col` LIKE 'abc'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.not_like('abc') }) .build(); "SELECT * FROM `table1` WHERE `col` NOT LIKE 'abc'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ col: sqlQuery.Query.not_in([ 1, 2, 3 ]) }) .build(); "SELECT * FROM `table1` WHERE `col` NOT IN (1, 2, 3)" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ __sql: [["LOWER(`stuff`) LIKE 'peaches'"]] }) .build(); "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ __sql: [["LOWER(`stuff`) LIKE ?", ['peaches']]] }) .build(); "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches'" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ __sql: [["LOWER(`stuff`) LIKE ? AND `number` > ?", ['peaches', 12]]] }) .build(); "SELECT * FROM `table1` WHERE LOWER(`stuff`) LIKE 'peaches' AND `number` > 12" - sqlQuery - .Select() + sqlSelect .from('table1') .where({ __sql: [["LOWER(`stuff`) LIKE ? AND `number` == ?", ['peaches']]] }) .build(); @@ -488,50 +432,45 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Order - sqlQuery - .Select() + var sqlSelect = sqlQuery.select(); + + sqlSelect .from('table1') .order('col') .build(); "SELECT * FROM `table1` ORDER BY `col` ASC" - sqlQuery - .Select() + sqlSelect .from('table1') .order('col', 'A') .build(); "SELECT * FROM `table1` ORDER BY `col` ASC" - sqlQuery - .Select() + sqlSelect .from('table1') .order('col', 'Z') .build(); "SELECT * FROM `table1` ORDER BY `col` DESC" - sqlQuery - .Select() + sqlSelect .from('table1') .order('col').order('col2', 'Z') .build(); "SELECT * FROM `table1` ORDER BY `col` ASC, `col2` DESC" - sqlQuery - .Select() + sqlSelect .from('table1') .order('col', []) .build(); "SELECT * FROM `table1` ORDER BY col" - sqlQuery - .Select() + sqlSelect .from('table1') .order('?? DESC', ['col']) .build(); "SELECT * FROM `table1` ORDER BY `col` DESC" - sqlQuery - .Select() + sqlSelect .from('table1') .order('ST_Distance(??, ST_GeomFromText(?,4326))', ['geopoint', 'POINT(-68.3394 27.5578)']) .build(); @@ -539,15 +478,15 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Limit - sqlQuery - .Select() + var sqlSelect = sqlQuery.select(); + + sqlSelect .from('table1') .limit(123) .build(); "SELECT * FROM `table1` LIMIT 123" - sqlQuery - .Select() + sqlSelect .from('table1') .limit('123456789') .build(); @@ -555,36 +494,33 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Select Function - sqlQuery - .Select() + var sqlSelect = sqlQuery.select(); + + sqlSelect .from('table1') .fun('myfun', 'col1') .build(); "SELECT MYFUN(`col1`) FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .fun('myfun', [ 'col1', 'col2']) .build(); "SELECT MYFUN(`col1`, `col2`) FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .fun('dbo.fnBalance', [ 80, null, null], 'balance') .build(); "SELECT DBO.FNBALANCE(80, NULL, NULL) AS `balance` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .fun('myfun', [ 'col1', 'col2'], 'alias') .build(); "SELECT MYFUN(`col1`, `col2`) AS `alias` FROM `table1`" - sqlQuery - .Select() + sqlSelect .from('table1') .fun('myfun', [ 'col1', sqlQuery.Text('col2') ], 'alias') .build(); @@ -592,28 +528,26 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Insert - sqlQuery - .Insert() + var sqlInsert = sqlQuery.insert(); + + sqlInsert .into('table1') .build(); "INSERT INTO `table1`" - sqlQuery - .Insert() + sqlInsert .into('table1') .set({}) .build(); "INSERT INTO `table1` VALUES()" - sqlQuery - .Insert() + sqlInsert .into('table1') .set({ col: 1 }) .build(); "INSERT INTO `table1` (`col`) VALUES (1)" - sqlQuery - .Insert() + sqlInsert .into('table1') .set({ col1: 1, col2: 'a' }) .build(); @@ -621,28 +555,26 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati ##Update - sqlQuery - .Update() + var sqlUpdate = sqlQuery.update() + + sqlUpdate .into('table1') .build(); "UPDATE `table1`" - sqlQuery - .Update() + sqlUpdate .into('table1') .set({ col: 1 }) .build(); "UPDATE `table1` SET `col` = 1" - sqlQuery - .Update() + sqlUpdate .into('table1') .set({ col1: 1, col2: 2 }) .build(); "UPDATE `table1` SET `col1` = 1, `col2` = 2" - sqlQuery - .Update() + sqlUpdate .into('table1') .set({ col1: 1, col2: 2 }).where({ id: 3 }) .build(); From 8322fc137092d71e26f945657f3c03f34c472061 Mon Sep 17 00:00:00 2001 From: Brian Noah Date: Mon, 21 Sep 2015 10:47:34 -0700 Subject: [PATCH 3/3] update comparators --- Readme.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Readme.md b/Readme.md index a0e7b07..c6b590c 100644 --- a/Readme.md +++ b/Readme.md @@ -22,8 +22,8 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati #Usage - - var sqlQuery = require('sql-query').Query(); + var sql = require('sql-query'), + sqlQuery = sql.Query(); ##Create @@ -244,13 +244,13 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati sqlSelect .from('table1') - .where({ col: sqlQuery.Query.eq(null) }) + .where({ col: sql.eq(null) }) .build(); "SELECT * FROM `table1` WHERE `col` IS NULL" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.ne(null) }) + .where({ col: sql.ne(null) }) .build(); "SELECT * FROM `table1` WHERE `col` IS NOT NULL" @@ -342,67 +342,67 @@ Sorry the API documentation is not complete. There are tests in ./test/integrati sqlSelect .from('table1') - .where({ col: sqlQuery.Query.gt(1) }) + .where({ col: sql.gt(1) }) .build(); "SELECT * FROM `table1` WHERE `col` > 1" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.gte(1) }) + .where({ col: sql.gte(1) }) .build(); "SELECT * FROM `table1` WHERE `col` >= 1" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.lt(1) }) + .where({ col: sql.lt(1) }) .build(); "SELECT * FROM `table1` WHERE `col` < 1" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.lte(1) }) + .where({ col: sql.lte(1) }) .build(); "SELECT * FROM `table1` WHERE `col` <= 1" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.eq(1) }) + .where({ col: sql.eq(1) }) .build(); "SELECT * FROM `table1` WHERE `col` = 1" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.ne(1) }) + .where({ col: sql.ne(1) }) .build(); "SELECT * FROM `table1` WHERE `col` <> 1" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.between('a', 'b') }) + .where({ col: sql.between('a', 'b') }) .build(); "SELECT * FROM `table1` WHERE `col` BETWEEN 'a' AND 'b'" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.not_between('a', 'b') }) + .where({ col: sql.not_between('a', 'b') }) .build(); "SELECT * FROM `table1` WHERE `col` NOT BETWEEN 'a' AND 'b'" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.like('abc') }) + .where({ col: sql.like('abc') }) .build(); "SELECT * FROM `table1` WHERE `col` LIKE 'abc'" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.not_like('abc') }) + .where({ col: sql.not_like('abc') }) .build(); "SELECT * FROM `table1` WHERE `col` NOT LIKE 'abc'" sqlSelect .from('table1') - .where({ col: sqlQuery.Query.not_in([ 1, 2, 3 ]) }) + .where({ col: sql.not_in([ 1, 2, 3 ]) }) .build(); "SELECT * FROM `table1` WHERE `col` NOT IN (1, 2, 3)"