Skip to content

Commit 7a19c0e

Browse files
committed
1) 1.11.0.
2) The SQLite flavor is added. 3) `Column.default()` is added.
1 parent 0226af5 commit 7a19c0e

File tree

9 files changed

+162
-56
lines changed

9 files changed

+162
-56
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,20 @@ This is the "entry point" of the builder. It contains only `static` methods and
207207

208208
The following methods return `this` to allow method chaining.
209209

210-
- primary()
211-
212-
Adds `PRIMARY KEY` to this column definition.
210+
- <a name="columnDefault"></a>default()
211+
212+
Adds `DEFAULT value` to this column definition. The `value` is quoted if it's a string.
213213

214214
- foreign()
215215

216216
Adds `REFERENCES tableName(columnName)` to this column definition.
217217

218218
tb.integer("type").foreign("tableName", "columnName");
219219

220+
- notNull()
221+
222+
Adds `NOT NULL` to this column definition.
223+
220224
- onDelete()
221225

222226
Adds `ON DELETE action` to this column definition.
@@ -225,9 +229,9 @@ The following methods return `this` to allow method chaining.
225229
.foreign("tableName", "column name")
226230
.onDelete("action");
227231

228-
- notNull()
232+
- primary()
229233

230-
Adds `NOT NULL` to this column definition.
234+
Adds `PRIMARY KEY` to this column definition.
231235

232236
#### <a name="uniqueBuilder"></a>[UniqueBuilder<i class="icon-up"></i>](#cuniqueBuilder)
233237

@@ -483,7 +487,8 @@ The following methods return `this` to allow method chaining.
483487

484488
Version number|Changes
485489
-|-
486-
v1.10.0|1. `SqlBuilder.beginTransaction()` added.<br>2. Several `SqlBuilder` documented.
490+
v1.11.0|1. The SQLite flavor is added.<br>2. [`Column.default()`](#columnDefault) is added.
491+
v1.10.0|1. `SqlBuilder.beginTransaction()` added.<br>2. Several `SqlBuilder` methods documented.
487492
v1.9.2|`Condition.like()` parameter default values were invalid.
488493
v1.9.1|`Condition.like()`: parameters `startsWith` / `endsWith` [added](#conditionLike).
489494
v1.9.0|Parameter `debug` is added to each sql-executing method.

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import SqlBldr from "./js/SqlBuilder";
1+
import Condition from "./js/Condition";
22
import SelectBuilder from "./js/SelectBuilder";
3+
import SqlBldr from "./js/SqlBuilder";
34
import WhereBuilder from "./js/WhereBuilder";
4-
import Condition from "./js/Condition";
5+
import PG from "./js/flavors/PG";
6+
import SQLite from "./js/flavors/SQLite/SQLite";
57

68
export default class SqlBuilder extends SqlBldr {
7-
static SelectBuilder = SelectBuilder;
8-
static WhereBuilder = WhereBuilder;
9-
static Condition = Condition;
9+
static Condition = Condition;
10+
static PG = PG;
11+
static SelectBuilder = SelectBuilder;
12+
static SQLite = SQLite;
13+
static WhereBuilder = WhereBuilder;
1014
};

js/Column.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
import { ArrayStringifier } from "simple-common-utils";
1+
import {
2+
ArrayStringifier,
3+
StaticUtils
4+
} from "simple-common-utils";
25
import Entry from "./Entry";
6+
import SqlBuilderOptions from "./SqlBuilderOptions";
37

48
export default class Column extends Entry {
5-
constructor(name, type) {
6-
super(name);
7-
8-
this.type = type;
9-
}
10-
11-
primary() {
12-
return this.attr("PRIMARY KEY");
13-
}
14-
15-
foreign(tableName, columnName) {
16-
return this.attr(`REFERENCES ${tableName}(${columnName})`);
17-
}
18-
19-
onDelete(action) {
20-
return this.attr(`ON DELETE ${action}`);
21-
}
22-
23-
notNull() {
24-
return this.attr("NOT NULL");
25-
}
26-
27-
toString() {
28-
return `${this.name} ${this.type}` + new ArrayStringifier(this.attrs)
29-
.setPrefix(" ")
30-
.setSeparator(" ")
31-
.process();
32-
}
9+
constructor(name, type) {
10+
super(name);
11+
12+
this.type = type;
13+
}
14+
15+
default(value) {
16+
return this.attr(`DEFAULT ${StaticUtils.quoteIfString(value, SqlBuilderOptions.getQuotingSymbol())}`);
17+
}
18+
19+
foreign(tableName, columnName) {
20+
return this.attr(`REFERENCES ${tableName}(${columnName})`);
21+
}
22+
23+
notNull() {
24+
return this.attr("NOT NULL");
25+
}
26+
27+
onDelete(action) {
28+
return this.attr(`ON DELETE ${action}`);
29+
}
30+
31+
primary() {
32+
return this.attr("PRIMARY KEY");
33+
}
34+
35+
toString() {
36+
return `${this.name} ${this.type}` + new ArrayStringifier(this.attrs)
37+
.setPrefix(" ")
38+
.setSeparator(" ")
39+
.process();
40+
}
3341
}

js/InsertUpdateBuilder.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ export default class InsertUpdateBuilder extends BuilderWithWhere {
4242
str = this._finalizeToStringProcessing(str);
4343
}
4444
} else {
45+
let prefix = "UPDATE";
46+
47+
if (this.table) {
48+
prefix += ` ${this.table}`;
49+
}
50+
51+
prefix += " SET ";
52+
4553
str = new ArrayStringifier(this.pairs)
46-
.setPrefix(`UPDATE ${this.table} SET `)
54+
.setPrefix(prefix)
4755
.setElementProcessor(pair => `${pair[0]} = ${pair[1]}`)
4856
.setPostfix(`${this.whereBuilder}`)
4957
.process();

js/SqlBuilder.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,8 @@ import SelectBuilder from "./SelectBuilder";
33
import SqlBuilderOptions from "./SqlBuilderOptions";
44
import TableBuilder from "./TableBuilder";
55
import WhereBuilder from "./WhereBuilder";
6-
import PG from "./flavors/PG";
7-
8-
function executeSql(callback, obj, debug = false) {
9-
callback(obj);
10-
11-
return SqlBuilder.executeSql(obj.toString(), debug);
12-
}
136

147
export default class SqlBuilder {
15-
static PG = PG;
16-
178
static beginTransaction(debug = false) {
189
return SqlBuilder.executeSql("BEGIN TRANSACTION", debug);
1910
}
@@ -23,7 +14,7 @@ export default class SqlBuilder {
2314
}
2415

2516
static createTable(name, callback, ifNotExists = true, debug = false) {
26-
return executeSql(callback, new TableBuilder(name, ifNotExists), debug);
17+
return SqlBuilder.prepareAndExecute(callback, new TableBuilder(name, ifNotExists), debug);
2718
}
2819

2920
static delete(table, callbackOrWhere, debug = false) {
@@ -59,7 +50,13 @@ export default class SqlBuilder {
5950
}
6051

6152
static insert(table, callback, debug = false) {
62-
return executeSql(callback, new InsertUpdateBuilder(true, table), debug);
53+
return SqlBuilder.prepareAndExecute(callback, new InsertUpdateBuilder(true, table), debug);
54+
}
55+
56+
static prepareAndExecute(callback, obj, debug = false) {
57+
callback(obj);
58+
59+
return SqlBuilder.executeSql(obj.toString(), debug);
6360
}
6461

6562
static rollback(debug = false) {
@@ -72,7 +69,7 @@ export default class SqlBuilder {
7269
let result;
7370

7471
if (!asSubquery) {
75-
result = executeSql(callback, sb, debug);
72+
result = SqlBuilder.prepareAndExecute(callback, sb, debug);
7673
} else {
7774
callback(sb);
7875

@@ -117,6 +114,6 @@ export default class SqlBuilder {
117114
}
118115

119116
static update(table, callback, debug = false) {
120-
return executeSql(callback, new InsertUpdateBuilder(false, table), debug);
117+
return SqlBuilder.prepareAndExecute(callback, new InsertUpdateBuilder(false, table), debug);
121118
}
122119
}

js/flavors/Flavor.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
export default class SqlFlavor {
2-
}
2+
static extend() {
3+
throw new Error("SqlFlavor.extend() must be overriden");
4+
}
5+
6+
static reset() {
7+
throw new Error("SqlFlavor.reset() must be overriden");
8+
}
9+
};

js/flavors/SQLite/SQLite.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import UpsertBuilder from "./UpsertBuilder";
2+
import Flavor from "../Flavor";
3+
import SqlBuilder from "../../SqlBuilder";
4+
5+
export default class SQLite extends Flavor {
6+
static extend() {
7+
SqlBuilder.upsert = SQLite.__upsert;
8+
}
9+
10+
static reset() {
11+
delete SqlBuilder.upsert;
12+
}
13+
14+
static __upsert(table, callback, debug = false) {
15+
return SqlBuilder.prepareAndExecute(callback, new UpsertBuilder(table), debug);
16+
}
17+
};

js/flavors/SQLite/UpsertBuilder.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ArrayStringifier } from "simple-common-utils";
2+
import BuilderWithWhere from "../../BuilderWithWhere";
3+
import InsertUpdateBuilder from "../../InsertUpdateBuilder";
4+
5+
export default class UpsertBuilder extends InsertUpdateBuilder {
6+
constructor(table) {
7+
super(true, table);
8+
}
9+
10+
doNothing() {
11+
this.__doUpdate = null;
12+
}
13+
14+
doUpdate() {
15+
this.__doUpdate = new InsertUpdateBuilder(false, "");
16+
17+
return this.__doUpdate;
18+
}
19+
20+
onConflict(...indexedColumns) {
21+
this.__indexedColumns = indexedColumns.length ? indexedColumns : null;
22+
23+
return this;
24+
}
25+
26+
toString() {
27+
let str = super.toString();
28+
29+
str += " ON CONFLICT";
30+
31+
if (this.__indexedColumns) {
32+
str += new ArrayStringifier(this.__indexedColumns)
33+
.setPrefix(" (")
34+
.setPostfix(")")
35+
.process();
36+
37+
if (this.__onConflictWhere) {
38+
str += this.__onConflictWhere.whereBuilder.toString();
39+
}
40+
}
41+
42+
str += " DO ";
43+
44+
if (!this.__doUpdate) {
45+
str += "NOTHING";
46+
} else {
47+
str += this.__doUpdate.toString();
48+
}
49+
50+
return str;
51+
}
52+
53+
where(callbackOrConditionString, add = true) {
54+
this.__onConflictWhere = new BuilderWithWhere();
55+
56+
this.__onConflictWhere.where(callbackOrConditionString, add);
57+
58+
return this;
59+
}
60+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-sql-query-builder",
3-
"version": "1.10.0",
3+
"version": "1.11.0",
44
"description": "A simple SQL query builder.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)