Skip to content

Commit

Permalink
implement upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jul 27, 2020
1 parent 9198441 commit bb7b495
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/grammar/constant.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,5 @@ LEFT "left" = L E F T

AS "as" = A S

UPSERT "upsert" = U P S E R T

18 changes: 11 additions & 7 deletions src/grammar/insert.pegjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
insertQuery = INSERT _ INTO _ table: tableName _* VALUES _* insertValue: valueTypes _* options: insertOptions* {
var skipDataCheck = false;
var returnValue = false;
let option = {};
options.forEach(val=>{
var key = Object.keys(val)[0];
switch(key){
case 'skipDataCheck':
skipDataCheck = val[key]; break;
option.skipDataCheck = val[key]; break;
case 'return':
returnValue = val[key]; break;
option.return = val[key]; break;
case 'upsert':
option.upsert = val[key]; break;
}
});
return {
api: 'insert',
data: {
into: table,
values: insertValue,
skipDataCheck: skipDataCheck,
return : returnValue
...option
}
}
}
Expand Down Expand Up @@ -50,7 +50,7 @@ insertWithEqual = "=" insertValue: value {
return insertValue;
}

insertOptions = option:(skipDataCheck/return)_* {
insertOptions = option:(skipDataCheck/return/upsert)_* {
return {
[option]:true
}
Expand All @@ -64,4 +64,8 @@ return = RETURN{
return 'return';
}

upsert = UPSERT{
return 'upsert';
}


222 changes: 124 additions & 98 deletions test/cases/insert/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ function randomDate(start, end) {
describe('Test insert', function () {
it('wrong table test', function (done) {
con.runSql("insert into Customer values='@val'").
catch(function (err) {
console.log(err);
var error = {
message: "Table 'Customer' does not exist",
type: 'table_not_exist'
};
expect(err).to.be.an('object').eql(error);
done();
})
catch(function (err) {
console.log(err);
var error = {
message: "Table 'Customer' does not exist",
type: 'table_not_exist'
};
expect(err).to.be.an('object').eql(error);
done();
})
});

it('insert customers', function (done) {
$.getJSON("test/static/Customers.json", function (results) {
var qry = new SqlWeb.Query("insert into Customers Values='@values'");
qry.map("@values", results);
con.runSql(qry).
then(function (results) {
expect(results).to.be.an('number').to.equal(93);
done();
}).
then(function (err) {
done(err);
})
then(function (results) {
expect(results).to.be.an('number').to.equal(93);
done();
}).
then(function (err) {
done(err);
})
});
});

Expand All @@ -36,12 +36,12 @@ describe('Test insert', function () {
var qry = new SqlWeb.Query("insert into Orders Values='@values'");
qry.map("@values", results);
con.runSql(qry).
then(function (results) {
expect(results).to.be.an('number').to.equal(196);
done();
}).catch(function (err) {
done(err);
});
then(function (results) {
expect(results).to.be.an('number').to.equal(196);
done();
}).catch(function (err) {
done(err);
});
});
});

Expand All @@ -55,12 +55,12 @@ describe('Test insert', function () {
var qry = new SqlWeb.Query("insert into Employees Values='@values'");
qry.map("@values", results);
con.runSql(qry).
then(function (results) {
expect(results).to.be.an('number').to.equal(34);
done();
}).catch(function (err) {
done(err);
});
then(function (results) {
expect(results).to.be.an('number').to.equal(34);
done();
}).catch(function (err) {
done(err);
});
});
});

Expand All @@ -87,12 +87,12 @@ describe('Test insert', function () {
var qry = new SqlWeb.Query("insert into Products Values='@values' skipDataCheck");
qry.map("@values", results);
con.runSql(qry).
then(function (results) {
expect(results).to.be.an('number').to.equal(77);
done();
}).catch(function (err) {
done(err);
});
then(function (results) {
expect(results).to.be.an('number').to.equal(77);
done();
}).catch(function (err) {
done(err);
});
});
});

Expand All @@ -101,12 +101,12 @@ describe('Test insert', function () {
var qry = new SqlWeb.Query("insert into Suppliers Values='@values' return");
qry.map("@values", results);
con.runSql(qry).
then(function (results) {
expect(results).to.be.an('array').length(29);
done();
}).catch(function (err) {
done(err);
});
then(function (results) {
expect(results).to.be.an('array').length(29);
done();
}).catch(function (err) {
done(err);
});
});
});

Expand All @@ -115,55 +115,55 @@ describe('Test insert', function () {
var qry = new SqlWeb.Query("insert into Customers Values='@values'");
qry.map("@values", [{}]);
con.runSql(qry).
catch(function (err) {
console.log(err);
var error = {
"message": "Null value is not allowed for column 'CustomerName'",
"type": "null_value"
};
expect(err).to.be.an('object').eql(error);
done();
});
catch(function (err) {
console.log(err);
var error = {
"message": "Null value is not allowed for column 'CustomerName'",
"type": "null_value"
};
expect(err).to.be.an('object').eql(error);
done();
});
});

it('not null test for last column', function (done) {
con.runSql("insert into Shippers values ({ShipperName: 'dsfgb'})").
catch(function (err) {
console.log(err);
var error = {
"message": "Null value is not allowed for column 'Phone'",
"type": "null_value"
};
expect(err).to.be.an('object').eql(error);
done();
});
catch(function (err) {
console.log(err);
var error = {
"message": "Null value is not allowed for column 'Phone'",
"type": "null_value"
};
expect(err).to.be.an('object').eql(error);
done();
});
});

it('wrong data type test - string', function (done) {
con.runSql("insert into Shippers values ({ShipperName: 'dsfgb',Phone: 91234})").
catch(function (err) {
var error = {
"message": "Supplied value for column 'Phone' have wrong data type",
"type": "wrong_data_type"
};
expect(err).to.be.an('object').eql(error);
done();
});
catch(function (err) {
var error = {
"message": "Supplied value for column 'Phone' have wrong data type",
"type": "wrong_data_type"
};
expect(err).to.be.an('object').eql(error);
done();
});
});

it('wrong data type test - number', function (done) {
con.runSql("insert into Products values ({ ProductName: 'dfb', SupplierID: 5, CategoryID: 10, Price: '1123', Unit: 12333 })").
then(function (result) {
done(result);
}).
catch(function (err) {
var error = {
"message": "Supplied value for column 'Unit' have wrong data type",
"type": "wrong_data_type"
};
expect(err).to.be.an('object').eql(error);
done();
});
then(function (result) {
done(result);
}).
catch(function (err) {
var error = {
"message": "Supplied value for column 'Unit' have wrong data type",
"type": "wrong_data_type"
};
expect(err).to.be.an('object').eql(error);
done();
});
});

it('undefined column insert', function (done) {
Expand All @@ -173,31 +173,57 @@ describe('Test insert', function () {
Address: 'ewrtgb'
};
con.runSql("insert into Shippers values ({ShipperName: 'dsfgb', Phone: '91234', Address: 'ewrtgb'}) return").
then(function (results) {
var returned_value = results[0];
value['ShipperID'] = returned_value.ShipperID;
expect(returned_value).to.be.an('object').eql(value);
done();
}).
catch(function (err) {
done(err);
});
then(function (results) {
var returned_value = results[0];
value['ShipperID'] = returned_value.ShipperID;
expect(returned_value).to.be.an('object').eql(value);
done();
}).
catch(function (err) {
done(err);
});
});

it('EnableSearch column test', function (done) {

con.runSql("insert into Customers values ({CustomerName: 'dfb', ContactName: 'Anders', Address: 'ewrgt', City: '1123', PostalCode: 'frfd', Country: 'fesgt', Email: 1234})").
then(function (results) {
expect(results).to.be.an('number').to.equal(3);
done();
}).
catch(function (err) {
var error = {
"message": "Supplied value for column 'Email' have wrong data type",
"type": "wrong_data_type"
};
expect(err).to.be.an('object').eql(error);
done();
});
then(function (results) {
expect(results).to.be.an('number').to.equal(3);
done();
}).
catch(function (err) {
var error = {
"message": "Supplied value for column 'Email' have wrong data type",
"type": "wrong_data_type"
};
expect(err).to.be.an('object').eql(error);
done();
});
});

it('insert customer using upsert - already inserted data', function (done) {
const selectCustomerWithId91 = () => {
return con.select({
from: 'Customers',
where: {
CustomerID: 91
}
})
}
selectCustomerWithId91().then(function (customers) {
expect(customers[0]).to.haveOwnProperty('CustomerName').equal('Wolski');
con.runSql("insert into Customers values ({CustomerID:91,CustomerName: 'Jon Snow', ContactName: 'Zbyszek', Address: 'ul. Filtrowa 68', City: 'Walla', PostalCode: 'frfd', Country: 'Poland'}) upsert").then(function (results) {
// console.log("upsert results", results);
expect(results).to.be.equal(1);
selectCustomerWithId91().then(function (newCustomers) {
expect(newCustomers).to.be.an('array').length(1);
expect(newCustomers[0]).to.haveOwnProperty('CustomerName').equal('Jon Snow');
done();
}).catch(done);
}).catch(function (err) {
done(err);
})

})
});
});

0 comments on commit bb7b495

Please sign in to comment.