Skip to content

Commit

Permalink
Merge pull request #12 from yourtion/master
Browse files Browse the repository at this point in the history
新增ignore与replace,使用GithubActions
  • Loading branch information
leizongmin committed Dec 17, 2020
2 parents fe7f933 + ce9b618 commit dca48ec
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 33 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run test-cov
- run: npm run coveralls
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ table("test1")
// INSERT INTO `test1` (`a`, `b`) VALUES (123, 456),
// (789, 110)");

// 插入数据,忽略已经存在记录
table("test1")
.insert({ a: 123, b: 456 })
.ignore()
.build();
// INSERT IGNORE INTO `test1` (`a`, `b`) VALUES (123, 456)

// 插入数据,如果记录已经存在则改为更新
table("test1")
.insert({ a: 123, b: 456 })
.onDuplicateKeyUpdate()
.set({ a: "xxx" })
.build();
// INSERT INTO `test1` (`a`, `b`) VALUES (123, 456) ON DUPLICATE KEY UPDATE `a`='xxx'

// 替换数据
table("test1")
.replace({
a: 123,
b: 456,
})
.build();
// REPLACE INTO `test1` (`a`, `b`) VALUES (123, 456);

// 更新数据
table("test1")
.update({
Expand All @@ -99,14 +123,6 @@ table("test1")
.build();
// UPDATE `test1` SET `a`=123, `b`=456 WHERE `b`=777 LIMIT 12

// 插入数据,如果记录已经存在则改为更新
table("test1")
.insert({ a: 123, b: 456 })
.onDuplicateKeyUpdate()
.set({ a: "xxx" })
.build();
// INSERT INTO `test1` (`a`, `b`) VALUES (123, 456) ON DUPLICATE KEY UPDATE `a`='xxx'

// 删除数据
table("test1")
.delete()
Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@
"@types/node": "*"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/debug": "^4.1.2",
"@types/mocha": "^5.2.5",
"@types/chai": "^4.2.14",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.19",
"@types/mysql": "^2.15.6",
"@types/node": "^11.11.1",
"@types/sqlstring": "^2.2.1",
"@types/node": "14.14.14",
"@types/sqlstring": "^2.3.0",
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"debug": "^4.1.1",
"jest": "^24.4.0",
"prettier": "^1.16.4",
"ts-jest": "^24.0.0",
"ts-node": "^8.0.3",
"typescript": "^3.3.3"
"coveralls": "^3.1.0",
"debug": "^4.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
"dependencies": {
"sqlstring": "^2.3.1"
"sqlstring": "^2.3.2"
}
}
39 changes: 38 additions & 1 deletion src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class QueryBuilder<Q = DataRow, R = any> {
update: string[];
insert: string;
insertRows: number;
insertIgnore: boolean;
delete: string;
sql: string;
sqlTpl: string;
Expand Down Expand Up @@ -108,6 +109,7 @@ export class QueryBuilder<Q = DataRow, R = any> {
update: [],
insert: "",
insertRows: 0,
insertIgnore: false,
delete: "",
sql: "",
sqlTpl: "",
Expand Down Expand Up @@ -517,6 +519,10 @@ export class QueryBuilder<Q = DataRow, R = any> {
public insert(data: Array<Partial<Q>>): this;

public insert(data: Partial<Q> | Array<Partial<Q>>): this {
return this._insert(data);
}

private _insert(data: Partial<Q> | Array<Partial<Q>>): this {
assert.ok(this._data.type === "", `cannot change query type after it was set to "${this._data.type}"`);
this._data.type = "INSERT";
assert.ok(data, `missing data`);
Expand Down Expand Up @@ -545,6 +551,32 @@ export class QueryBuilder<Q = DataRow, R = any> {
return this;
}

/**
* 插入忽略已存在
*/
public ignore(): this {
assert.ok(this._data.type === "INSERT", `ignore() must be called after insert()`);
this._data.insertIgnore = true;
return this;
}

/**
* 替换,删除原来的记录,添加新的记录
* @param data 键值对数据
*/
public replace(data: Partial<Q>): this;
/**
* 替换,删除原来的记录,添加新的记录
* @param data 键值对数据数组
*/
public replace(data: Array<Partial<Q>>): this;

public replace(data: Partial<Q> | Array<Partial<Q>>): this {
this._insert(data);
this._data.type = "REPLACE";
return this;
}

/**
* 删除
*/
Expand Down Expand Up @@ -729,6 +761,7 @@ export class QueryBuilder<Q = DataRow, R = any> {
data.conditions = data.conditions.map(v => v.trim()).filter(v => v);
const where = data.conditions.length > 0 ? `WHERE ${data.conditions.join(" AND ")}` : "";
let sql: string;
const ignore = data.insertIgnore ? " IGNORE" : "";

assert.ok(currentTableName && currentTableEscapedName, "missing table name");

Expand Down Expand Up @@ -776,7 +809,11 @@ export class QueryBuilder<Q = DataRow, R = any> {
break;
}
case "INSERT": {
sql = `INSERT INTO ${currentTableEscapedName} ${data.insert}`;
sql = `INSERT${ignore} INTO ${currentTableEscapedName} ${data.insert}`;
break;
}
case "REPLACE": {
sql = `REPLACE INTO ${currentTableEscapedName} ${data.insert}`;
break;
}
case "UPDATE": {
Expand Down
39 changes: 39 additions & 0 deletions src/test/query1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,45 @@ test("insert", function() {
utils.debug(sql);
expect(sql).to.equal("INSERT INTO `test1` (`a`, `b`) VALUES (123, 456),\n(789, 110)");
}
{
const sql = Q.table("test1")
.insert({
a: 123,
b: 456,
})
.ignore()
.build();
utils.debug(sql);
expect(sql).to.equal("INSERT IGNORE INTO `test1` (`a`, `b`) VALUES (123, 456)");
}
});
test("replace", function() {
{
const sql = Q.table("test1")
.replace({
a: 123,
b: 456,
})
.build();
utils.debug(sql);
expect(sql).to.equal("REPLACE INTO `test1` (`a`, `b`) VALUES (123, 456)");
}
{
const sql = Q.table("test1")
.replace([
{
a: 123,
b: 456,
},
{
a: 789,
b: 110,
},
])
.build();
utils.debug(sql);
expect(sql).to.equal("REPLACE INTO `test1` (`a`, `b`) VALUES (123, 456),\n(789, 110)");
}
});
test("update", function() {
{
Expand Down

0 comments on commit dca48ec

Please sign in to comment.