Skip to content

Commit ddfb4a0

Browse files
committed
1.0.1
1 parent 00d7eb6 commit ddfb4a0

File tree

4 files changed

+194
-11
lines changed

4 files changed

+194
-11
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017
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.

README.md

+170-8
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,30 @@ A simple SQL query builder. It executes built queries if an executing function i
66
### <a name="usage"></a>[Usage](#cusage)
77

88
1. <a name="csqlBuilder"></a>[SqlBuilder](#sqlBuilder)
9-
1. <a name="ctableBuilder"></a>[TableBuilder](#tableBuilder)
10-
1. <a name="ccolumn"></a>[Column](#column)
11-
1. <a name="cuniqueBuilder"></a>[UniqueBuilder](#uniqueBuilder)
9+
2. <a name="ctableBuilder"></a>[TableBuilder](#tableBuilder)
10+
3. <a name="ccolumn"></a>[Column](#column)
11+
4. <a name="cuniqueBuilder"></a>[UniqueBuilder](#uniqueBuilder)
12+
5. <a name="cinsertUpdateBuilder"></a>[InsertUpdateBuilder](#insertUpdateBuilder)
13+
6. <a name="cwhereBuilder"></a>[WhereBuilder](#whereBuilder)
14+
7. <a name="ccondition"></a>[Condition](#condition)
15+
8. <a name="cselectBuilder"></a>[SelectBuilder](#selectBuilder)
16+
9. <a name="cfromBuilder"></a>[FromBuilder](#fromBuilder)
1217

1318
#### <a name="sqlBuilder"></a>[SqlBuilder<i class="icon-up"></i>](#csqlBuilder)
1419

1520
This is the "entry point" of the builder. It contains only `static` methods and fields.
1621

17-
import SqlBuilder from "react-native-sql-query-builder";
22+
import SqlBuilder from "simple-sql-query-builder";
1823

1924
1. <a name="csqlBuilderSetDebug"></a> [setDebug()](#sqlBuilderSetDebug)
20-
1. <a name="csqlBuilderSetSqlExecutor"></a>[setSqlExecutor()](#sqlBuilderSetSqlExecutor)
21-
1. <a name="csqlBuilderExecuteSql"></a>[executeSql()](#sqlBuilderExecuteSql)
22-
1. <a name="csqlBuilderCreateTable"></a>[createTable()](#sqlBuilderCreateTable)
25+
2. <a name="csqlBuilderSetSqlExecutor"></a>[setSqlExecutor()](#sqlBuilderSetSqlExecutor)
26+
3. <a name="csqlBuilderExecuteSql"></a>[executeSql()](#sqlBuilderExecuteSql)
27+
4. <a name="csqlBuilderCreateTable"></a>[createTable()](#sqlBuilderCreateTable)
28+
5. <a name="csqlBuilderInsert"></a>[insert()](#sqlBuilderInsert)
29+
6. <a name="csqlBuilderSelect"></a>[select()](#sqlBuilderSelect)
30+
7. <a name="csqlBuilderUpdate"></a>[update()](#sqlBuilderUpdate)
31+
8. <a name="csqlBuilderDelete"></a>[delete()](#sqlBuilderDelete)
32+
9. <a name="csqlBuilderStaticFields"></a>[static fields](#sqlBuilerStaticFields)
2333

2434
<!-- -->
2535

@@ -55,7 +65,7 @@ This is the "entry point" of the builder. It contains only `static` methods and
5565

5666
const name = "weights";
5767
58-
const callback = tableBuilder => {
68+
const callback = tb => {
5969
tb.integer("rowid").primary();
6070
tb.integer("millis").notNull();
6171
tb.integer("gross").notNull();
@@ -67,6 +77,62 @@ This is the "entry point" of the builder. It contains only `static` methods and
6777
6878
SqlBuilder.createTable(name, callback, ifNotExists);
6979

80+
- <a name="sqlBuilderInsert"></a>[insert()<i class="icon-up"></i>](#csqlBuilderInsert)
81+
82+
Inserts a row using [InsertUpdateBuilder](#insertUpdateBuilder).
83+
84+
const table = "weights";
85+
86+
const callback = ib => ib
87+
.columnValue("millis", new Date().getTime())
88+
.columnValue("gross", gross)
89+
.columnValue("net", net)
90+
.columnValue("comment", comment);
91+
92+
SqlBuilder.insert(table, callback);
93+
94+
- <a name="sqlBuilderSelect"></a>[select()<i class="icon-up"></i>](#csqlBuilderSelect)
95+
96+
Selects rows using [SelectBuilder](#selectBuilder).
97+
98+
SqlBuilder.select(sb => sb
99+
.column("rowid")
100+
.column("*")
101+
.from("weights"));
102+
103+
- <a name="sqlBuilderUpdate"></a>[update()<i class="icon-up"></i>](#csqlBuilderUpdate)
104+
105+
Updates rows using [InsertUpdateBuilder](#insertUpdateBuilder).
106+
107+
const table = "expenseImages";
108+
109+
const callback = ub => ub
110+
.columnValue("path", path)
111+
.where(wb => wb.column("rowid").e(image.rowid));
112+
113+
SqlBuilder.update(table, callback);
114+
115+
- <a name="sqlBuilderDelete"></a>[delete()<i class="icon-up"></i>](#csqlBuilderDelete)
116+
117+
Deletes rows using [WhereBuilder](#whereBuilder).
118+
119+
const table = "journeys";
120+
const callback = wb => wb.column("rowid").e(rowid);
121+
122+
SqlBuilder.delete(table, callback);
123+
124+
- <a name="sqlBuilderStaticFields"></a>[static fields<i class="icon-up"></i>](#csqlBuilderStaticFields)
125+
126+
There are several `static` fields in the class to facilitate creating instances of commonly used auxiliary classes: `SelectBuilder`, `WhereBuilder` and `Condition`. So instead of
127+
128+
import SelectBuilder from "simple-sql-query-builder/js/SelectBuilder";
129+
130+
const sb = new SelectBuilder();
131+
132+
you can just write
133+
134+
const sb = new SqlBuilder.SelectBuilder();
135+
70136
#### <a name="tableBuilder"></a>[TableBuilder<i class="icon-up"></i>](#ctableBuilder)
71137

72138
- column()
@@ -103,6 +169,8 @@ This is the "entry point" of the builder. It contains only `static` methods and
103169

104170
#### <a name="column"></a>[Column](#ccolumn)
105171

172+
All these methods return `this` to allow method chaining.
173+
106174
- primary()
107175

108176
Adds `PRIMARY KEY` to this column definition.
@@ -136,10 +204,104 @@ This is the "entry point" of the builder. It contains only `static` methods and
136204
.collate("NOCASE")
137205
.order("ASC");
138206

207+
#### <a name="insertUpdateBuilder"></a>[InsertUpdateBuilder<i class="icon-up"></i>](#cinsertUpdateBuilder)
208+
209+
All these methods return `this` to allow method chaining.
210+
211+
- columnValue()
212+
213+
Specifies a column value.
214+
215+
insertUpdateBuilder.columnValue(
216+
column,
217+
value,
218+
add, // Boolean. If true this column will be added to the generated SQL code. Default: true.
219+
quoteIfString // Boolean. If true and value is a string, quotes are added to the generated SQL code for this value. Default: true.
220+
);
221+
222+
Examples:
223+
224+
- `"INSERT INTO tableName (columnName1) VALUES (10);"`
225+
226+
SqlBuilder.insert(
227+
"tableName",
228+
ib => ib.columnValue("columnName1", 10));
229+
230+
or
231+
232+
SqlBuilder.insert(
233+
"tableName",
234+
ib => ib
235+
.columnValue("columnName1", 10)
236+
.columnValue("columnValue2", 20, false));
237+
238+
- `"INSERT INTO tableName (columnName1, columnName2) VALUES (10, "String value");"`
239+
240+
SqlBuilder.insert(
241+
"tableName",
242+
ib => ib
243+
.columnValue("columnName1", 10)
244+
.columnValue("columnName2", "String value"));
245+
246+
- where()
247+
248+
Specifies a `WHERE` clause. It's a no-op if this instance is used for insertion.
249+
250+
insertUpdateBuilder.where(
251+
callbackOrConditionString, // See below.
252+
add // Boolean. If true the WHERE-clause will be added to the generated SQL code. Default: true.
253+
);
254+
255+
`callbackOrConditionString` can be one of:
256+
257+
- A callback receiving a [WhereBuilder](#whereBuilder) instance;
258+
- a string without `WHERE` itself;
259+
- A [Condition](#condition) instance;
260+
261+
Examples:
262+
263+
- `UPDATE tableName SET columnName1 = 10, columnName2 = "String value" WHERE columnName3 = 314;`
264+
265+
SqlBuilder.update(
266+
"tableName",
267+
ib => ib
268+
.columnValue("columnName1", 10)
269+
.columnValue("columnName2", "String value")
270+
.where(wb => wb.column("columnName3").e(314)));
271+
272+
or
273+
274+
SqlBuilder.update(
275+
"tableName", ib => ib
276+
.columnValue("columnName1", 10)
277+
.columnValue("columnName2", "String value")
278+
.where("columnName3 = 314"));
279+
280+
or
281+
282+
const condition = new SqlBuilder.Condition("columnName3");
283+
condition.e(314);
284+
285+
SqlBuilder.update(
286+
"tableName",
287+
ib => ib
288+
.columnValue("columnName1", 10)
289+
.columnValue("columnName2", "String value")
290+
.where(condition));
291+
292+
#### <a name="whereBuilder"></a>[WhereBuilder<i class="icon-up"></i>](#cwhereBuilder)
293+
294+
#### <a name="condition"></a>[Condition<i class="icon-up"></i>](#ccondition)
295+
296+
#### <a name="selectBuilder"></a>[SelectBuilder<i class="icon-up"></i>](#cselectBuilder)
297+
298+
#### <a name="fromBuilder"></a>[FromBuilder<i class="icon-up"></i>](#cfromBuilder)
299+
139300
### <a name="versionHistory"></a>[Version history](#cversionHistory)
140301

141302
Version number|Changes
142303
-|-
304+
v1.0.1|1.&nbsp;Readme updated.<br>2.&nbsp;`UPDATE` queries weren't terminated with `;`. Fixed.
143305
v1.0.0|Initial release.
144306

145307
<br><br>

js/InsertUpdateBuilder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class InsertUpdateBuilder extends BuilderWithWhere {
4141
str = new ArrayStringifier(this.pairs)
4242
.setPrefix(`UPDATE ${this.table} SET `)
4343
.setElementProcessor(pair => `${pair[0]} = ${pair[1]}`)
44-
.setPostfix(`${this.whereBuilder}`)
44+
.setPostfix(`${this.whereBuilder};`)
4545
.process();
4646
}
4747

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-sql-query-builder",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A simple SQL query builder.",
55
"main": "index.js",
66
"scripts": {
@@ -17,7 +17,7 @@
1717
"sql query builder"
1818
],
1919
"author": "Ruben Shalimov <[email protected]>",
20-
"license": "ISC",
20+
"license": "MIT",
2121
"bugs": {
2222
"url": "https://github.com/RobinBobin/simple-sql-query-builder/issues"
2323
},

0 commit comments

Comments
 (0)