Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
feat: add MySQL INSERT IGNORE support
Browse files Browse the repository at this point in the history
  • Loading branch information
yibn2008 committed Sep 20, 2018
1 parent 62d59c6 commit cb49f3a
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 77 deletions.
2 changes: 1 addition & 1 deletion dist/squel-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ function _buildSquel() {
} else if (typeofValue !== "number") {
// if it's a string and we have custom string formatting turned on then use that
if ('string' === typeofValue && this.options.stringFormatter) {
return this.options.stringFormatter(value);
return this.options.stringFormatter(value, formattingOptions);
}

if (formattingOptions.dontQuote) {
Expand Down
2 changes: 1 addition & 1 deletion dist/squel-basic.min.js

Large diffs are not rendered by default.

182 changes: 112 additions & 70 deletions dist/squel.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ function _buildSquel() {
} else if (typeofValue !== "number") {
// if it's a string and we have custom string formatting turned on then use that
if ('string' === typeofValue && this.options.stringFormatter) {
return this.options.stringFormatter(value);
return this.options.stringFormatter(value, formattingOptions);
}

if (formattingOptions.dontQuote) {
Expand Down Expand Up @@ -3489,11 +3489,25 @@ squel.flavours['mssql'] = function (_squel) {

// This file contains additional Squel commands for use with MySQL

var escape = require('sql-escape-string');

squel.flavours['mysql'] = function (_squel) {
var cls = _squel.cls;

// ON DUPLICATE KEY UPDATE ...
cls.MysqlOnDuplicateKeyUpdateBlock = function (_cls$AbstractSetField3) {
// add default stringFormatter for MySQL
cls.DefaultQueryBuilderOptions.stringFormatter = function (value, formattingOptions) {
if (!value || formattingOptions.dontQuote) {
return value;
} else {
return escape(value, {
// MySQL use backslash to escape value by default
backslashSupported: true
});
}
};

// INSERT IGNORE ...
cls.MysqlIgnoreBlock = function (_cls$AbstractSetField3) {
_inherits(_class46, _cls$AbstractSetField3);

function _class46() {
Expand All @@ -3503,6 +3517,34 @@ squel.flavours['mysql'] = function (_squel) {
}

_createClass(_class46, [{
key: 'ignore',
value: function ignore() {
this._str = 'IGNORE';
}
}, {
key: '_toParamString',
value: function _toParamString() {
return {
text: this._str || '',
values: []
};
}
}]);

return _class46;
}(cls.AbstractSetFieldBlock);

// ON DUPLICATE KEY UPDATE ...
cls.MysqlOnDuplicateKeyUpdateBlock = function (_cls$AbstractSetField4) {
_inherits(_class47, _cls$AbstractSetField4);

function _class47() {
_classCallCheck(this, _class47);

return _possibleConstructorReturn(this, (_class47.__proto__ || Object.getPrototypeOf(_class47)).apply(this, arguments));
}

_createClass(_class47, [{
key: 'onDupUpdate',
value: function onDupUpdate(field, value, options) {
this._set(field, value, options);
Expand Down Expand Up @@ -3547,41 +3589,41 @@ squel.flavours['mysql'] = function (_squel) {
}
}]);

return _class46;
return _class47;
}(cls.AbstractSetFieldBlock);

// INSERT query builder.
cls.Insert = function (_cls$QueryBuilder9) {
_inherits(_class47, _cls$QueryBuilder9);
_inherits(_class48, _cls$QueryBuilder9);

function _class47(options) {
function _class48(options) {
var blocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, _class47);
_classCallCheck(this, _class48);

blocks = blocks || [new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options), new cls.InsertFieldsFromQueryBlock(options), new cls.MysqlOnDuplicateKeyUpdateBlock(options)];
blocks = blocks || [new cls.StringBlock(options, 'INSERT'), new cls.MysqlIgnoreBlock(options), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options), new cls.InsertFieldsFromQueryBlock(options), new cls.MysqlOnDuplicateKeyUpdateBlock(options)];

return _possibleConstructorReturn(this, (_class47.__proto__ || Object.getPrototypeOf(_class47)).call(this, options, blocks));
return _possibleConstructorReturn(this, (_class48.__proto__ || Object.getPrototypeOf(_class48)).call(this, options, blocks));
}

return _class47;
return _class48;
}(cls.QueryBuilder);

// REPLACE query builder.
cls.Replace = function (_cls$QueryBuilder10) {
_inherits(_class48, _cls$QueryBuilder10);
_inherits(_class49, _cls$QueryBuilder10);

function _class48(options) {
function _class49(options) {
var blocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, _class48);
_classCallCheck(this, _class49);

blocks = blocks || [new cls.StringBlock(options, 'REPLACE'), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options), new cls.InsertFieldsFromQueryBlock(options)];

return _possibleConstructorReturn(this, (_class48.__proto__ || Object.getPrototypeOf(_class48)).call(this, options, blocks));
return _possibleConstructorReturn(this, (_class49.__proto__ || Object.getPrototypeOf(_class49)).call(this, options, blocks));
}

return _class48;
return _class49;
}(cls.QueryBuilder);

_squel.replace = function (options, blocks) {
Expand All @@ -3598,19 +3640,19 @@ squel.flavours['postgres'] = function (_squel) {
cls.DefaultQueryBuilderOptions.autoQuoteAliasNames = false;
cls.DefaultQueryBuilderOptions.useAsForTableAliasNames = true;

cls.PostgresOnConflictKeyUpdateBlock = function (_cls$AbstractSetField4) {
_inherits(_class49, _cls$AbstractSetField4);
cls.PostgresOnConflictKeyUpdateBlock = function (_cls$AbstractSetField5) {
_inherits(_class50, _cls$AbstractSetField5);

function _class49() {
_classCallCheck(this, _class49);
function _class50() {
_classCallCheck(this, _class50);

return _possibleConstructorReturn(this, (_class49.__proto__ || Object.getPrototypeOf(_class49)).apply(this, arguments));
return _possibleConstructorReturn(this, (_class50.__proto__ || Object.getPrototypeOf(_class50)).apply(this, arguments));
}

_createClass(_class49, [{
_createClass(_class50, [{
key: 'onConflict',
value: function onConflict(conflictFields, fields) {
var _this55 = this;
var _this56 = this;

this._onConflict = true;
if (!conflictFields) {
Expand All @@ -3623,7 +3665,7 @@ squel.flavours['postgres'] = function (_squel) {

if (fields) {
Object.keys(fields).forEach(function (key) {
_this55._set(key, fields[key]);
_this56._set(key, fields[key]);
});
}
}
Expand Down Expand Up @@ -3676,23 +3718,23 @@ squel.flavours['postgres'] = function (_squel) {
}
}]);

return _class49;
return _class50;
}(cls.AbstractSetFieldBlock);

// RETURNING
cls.ReturningBlock = function (_cls$Block18) {
_inherits(_class50, _cls$Block18);
_inherits(_class51, _cls$Block18);

function _class50(options) {
_classCallCheck(this, _class50);
function _class51(options) {
_classCallCheck(this, _class51);

var _this56 = _possibleConstructorReturn(this, (_class50.__proto__ || Object.getPrototypeOf(_class50)).call(this, options));
var _this57 = _possibleConstructorReturn(this, (_class51.__proto__ || Object.getPrototypeOf(_class51)).call(this, options));

_this56._fields = [];
return _this56;
_this57._fields = [];
return _this57;
}

_createClass(_class50, [{
_createClass(_class51, [{
key: 'returning',
value: function returning(field) {
var alias = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
Expand Down Expand Up @@ -3781,23 +3823,23 @@ squel.flavours['postgres'] = function (_squel) {
}
}]);

return _class50;
return _class51;
}(cls.Block);

// WITH
cls.WithBlock = function (_cls$Block19) {
_inherits(_class51, _cls$Block19);
_inherits(_class52, _cls$Block19);

function _class51(options) {
_classCallCheck(this, _class51);
function _class52(options) {
_classCallCheck(this, _class52);

var _this57 = _possibleConstructorReturn(this, (_class51.__proto__ || Object.getPrototypeOf(_class51)).call(this, options));
var _this58 = _possibleConstructorReturn(this, (_class52.__proto__ || Object.getPrototypeOf(_class52)).call(this, options));

_this57._tables = [];
return _this57;
_this58._tables = [];
return _this58;
}

_createClass(_class51, [{
_createClass(_class52, [{
key: 'with',
value: function _with(alias, table) {
this._tables.push({ alias: alias, table: table });
Expand Down Expand Up @@ -3852,26 +3894,26 @@ squel.flavours['postgres'] = function (_squel) {
}
}]);

return _class51;
return _class52;
}(cls.Block);

// DISTINCT [ON]
cls.DistinctOnBlock = function (_cls$Block20) {
_inherits(_class52, _cls$Block20);
_inherits(_class53, _cls$Block20);

function _class52(options) {
_classCallCheck(this, _class52);
function _class53(options) {
_classCallCheck(this, _class53);

var _this58 = _possibleConstructorReturn(this, (_class52.__proto__ || Object.getPrototypeOf(_class52)).call(this, options));
var _this59 = _possibleConstructorReturn(this, (_class53.__proto__ || Object.getPrototypeOf(_class53)).call(this, options));

_this58._distinctFields = [];
return _this58;
_this59._distinctFields = [];
return _this59;
}

_createClass(_class52, [{
_createClass(_class53, [{
key: 'distinct',
value: function distinct() {
var _this59 = this;
var _this60 = this;

this._useDistinct = true;

Expand All @@ -3882,7 +3924,7 @@ squel.flavours['postgres'] = function (_squel) {
}

fields.forEach(function (field) {
_this59._distinctFields.push(_this59._sanitizeField(field));
_this60._distinctFields.push(_this60._sanitizeField(field));
});
}
}, {
Expand All @@ -3905,77 +3947,77 @@ squel.flavours['postgres'] = function (_squel) {
}
}]);

return _class52;
return _class53;
}(cls.Block);

// SELECT query builder.
cls.Select = function (_cls$QueryBuilder11) {
_inherits(_class53, _cls$QueryBuilder11);
_inherits(_class54, _cls$QueryBuilder11);

function _class53(options) {
function _class54(options) {
var blocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, _class53);
_classCallCheck(this, _class54);

blocks = blocks || [new cls.WithBlock(options), new cls.StringBlock(options, 'SELECT'), new cls.FunctionBlock(options), new cls.DistinctOnBlock(options), new cls.GetFieldBlock(options), new cls.FromTableBlock(options), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.GroupByBlock(options), new cls.HavingBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options), new cls.OffsetBlock(options), new cls.UnionBlock(options)];

return _possibleConstructorReturn(this, (_class53.__proto__ || Object.getPrototypeOf(_class53)).call(this, options, blocks));
return _possibleConstructorReturn(this, (_class54.__proto__ || Object.getPrototypeOf(_class54)).call(this, options, blocks));
}

return _class53;
return _class54;
}(cls.QueryBuilder);

// INSERT query builder
cls.Insert = function (_cls$QueryBuilder12) {
_inherits(_class54, _cls$QueryBuilder12);
_inherits(_class55, _cls$QueryBuilder12);

function _class54(options) {
function _class55(options) {
var blocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, _class54);
_classCallCheck(this, _class55);

blocks = blocks || [new cls.WithBlock(options), new cls.StringBlock(options, 'INSERT'), new cls.IntoTableBlock(options), new cls.InsertFieldValueBlock(options), new cls.InsertFieldsFromQueryBlock(options), new cls.PostgresOnConflictKeyUpdateBlock(options), new cls.ReturningBlock(options)];

return _possibleConstructorReturn(this, (_class54.__proto__ || Object.getPrototypeOf(_class54)).call(this, options, blocks));
return _possibleConstructorReturn(this, (_class55.__proto__ || Object.getPrototypeOf(_class55)).call(this, options, blocks));
}

return _class54;
return _class55;
}(cls.QueryBuilder);

// UPDATE query builder
cls.Update = function (_cls$QueryBuilder13) {
_inherits(_class55, _cls$QueryBuilder13);
_inherits(_class56, _cls$QueryBuilder13);

function _class55(options) {
function _class56(options) {
var blocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, _class55);
_classCallCheck(this, _class56);

blocks = blocks || [new cls.WithBlock(options), new cls.StringBlock(options, 'UPDATE'), new cls.UpdateTableBlock(options), new cls.SetFieldBlock(options), new cls.FromTableBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options), new cls.ReturningBlock(options)];

return _possibleConstructorReturn(this, (_class55.__proto__ || Object.getPrototypeOf(_class55)).call(this, options, blocks));
return _possibleConstructorReturn(this, (_class56.__proto__ || Object.getPrototypeOf(_class56)).call(this, options, blocks));
}

return _class55;
return _class56;
}(cls.QueryBuilder);

// DELETE query builder
cls.Delete = function (_cls$QueryBuilder14) {
_inherits(_class56, _cls$QueryBuilder14);
_inherits(_class57, _cls$QueryBuilder14);

function _class56(options) {
function _class57(options) {
var blocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, _class56);
_classCallCheck(this, _class57);

blocks = blocks || [new cls.WithBlock(options), new cls.StringBlock(options, 'DELETE'), new cls.TargetTableBlock(options), new cls.FromTableBlock(_extend({}, options, {
singleTable: true
})), new cls.JoinBlock(options), new cls.WhereBlock(options), new cls.OrderByBlock(options), new cls.LimitBlock(options), new cls.ReturningBlock(options)];

return _possibleConstructorReturn(this, (_class56.__proto__ || Object.getPrototypeOf(_class56)).call(this, options, blocks));
return _possibleConstructorReturn(this, (_class57.__proto__ || Object.getPrototypeOf(_class57)).call(this, options, blocks));
}

return _class56;
return _class57;
}(cls.QueryBuilder);
};
return squel;
Expand Down
4 changes: 2 additions & 2 deletions dist/squel.min.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cb49f3a

Please sign in to comment.