不要怂,就是干,撸起袖子干!

Commit 655a603d by Jan Aagaard Meier

Merge pull request #637 from reedog117/mariadb

mariadb major updates
2 parents c7fb0217 8c274f0a
...@@ -15,8 +15,9 @@ env: ...@@ -15,8 +15,9 @@ env:
- DB=mysql DIALECT=postgres - DB=mysql DIALECT=postgres
- DB=mysql DIALECT=postgres-native - DB=mysql DIALECT=postgres-native
- DB=mysql DIALECT=sqlite - DB=mysql DIALECT=sqlite
- DB=mysql DIALECT=mariadb
language: node_js language: node_js
node_js: node_js:
- 0.8 - 0.8
\ No newline at end of file
...@@ -108,7 +108,7 @@ module.exports = (function() { ...@@ -108,7 +108,7 @@ module.exports = (function() {
} else if (this.poolCfg) { } else if (this.poolCfg) {
//the user has requested pooling, so create our connection pool //the user has requested pooling, so create our connection pool
this.pool = Pooling.Pool({ this.pool = Pooling.Pool({
name: 'sequelize-mariasql', name: 'sequelize-mariadb',
create: function (done) { create: function (done) {
connect.call(self, done) connect.call(self, done)
}, },
...@@ -207,38 +207,23 @@ module.exports = (function() { ...@@ -207,38 +207,23 @@ module.exports = (function() {
var disconnect = function(client) { var disconnect = function(client) {
var self = this; var self = this;
if (!this.useQueue) {
this.client = null; if(client.connected) {
client.end()
} }
client.end(function() { self.client = null
if (!self.useQueue) { self.isConnecting = false
return client.destroy();
}
var intervalObj = null return
var cleanup = function () {
var retryCt = 0
// make sure to let client finish before calling destroy
if (self && self.hasQueuedItems) {
return
}
// needed to prevent mariasql connection leak
client.destroy()
if (self && self.client) {
self.client = null
}
clearInterval(intervalObj)
}
intervalObj = setInterval(cleanup, 10)
cleanup()
return
})
} }
var connect = function(done, config) { var connect = function(done, config) {
config = config || this.config config = config || this.config
var connection = new mariasql();
var connection = new mariasql()
, self = this
this.isConnecting = true this.isConnecting = true
connection.connect({ connection.connect({
host: config.host, host: config.host,
...@@ -252,10 +237,14 @@ module.exports = (function() { ...@@ -252,10 +237,14 @@ module.exports = (function() {
connection.on('connect', function() { connection.on('connect', function() {
connection.query("SET time_zone = '+0:00'"); connection.query("SET time_zone = '+0:00'");
// client.setMaxListeners(self.maxConcurrentQueries) connection.setMaxListeners(self.maxConcurrentQueries)
this.isConnecting = false this.isConnecting = false
done(null, connection) done(null, connection)
}).on('error', function() {
disconnect.call(self, connection)
}).on('close', function() {
disconnect.call(self, connection)
}) })
} }
...@@ -295,6 +284,10 @@ module.exports = (function() { ...@@ -295,6 +284,10 @@ module.exports = (function() {
} }
var transferQueuedItems = function(count) { var transferQueuedItems = function(count) {
// prevent possible overrun condition
if( count > this.queue.length )
count = this.queue.length
for(var i = 0; i < count; i++) { for(var i = 0; i < count; i++) {
var queueItem = this.queue.shift(); var queueItem = this.queue.shift();
if (queueItem) { if (queueItem) {
...@@ -332,7 +325,7 @@ module.exports = (function() { ...@@ -332,7 +325,7 @@ module.exports = (function() {
}) })
ConnectorManager.prototype.__defineGetter__('isConnected', function() { ConnectorManager.prototype.__defineGetter__('isConnected', function() {
return this.client != null return this.client != null && this.client.connected == true
}) })
var disconnectIfNoConnections = function() { var disconnectIfNoConnections = function() {
......
...@@ -23,34 +23,131 @@ module.exports = (function() { ...@@ -23,34 +23,131 @@ module.exports = (function() {
this.options.logging('Executing: ' + this.sql) this.options.logging('Executing: ' + this.sql)
} }
var resultSet = []; var resultSet = [],
errorDetected = false,
self = this
this.client.query(this.sql) this.client.query(this.sql)
.on('result', function(results) { .on('result', function(results) {
results.on('row', function(row) { results.on('row', function(row) {
resultSet.push(row); // iterate through each property to convert
}) // strings into JS objects when possible
.on('error', function(err) { for(var prop in row) {
this.emit('error', err, this.callee) // take care of properties that shouldn't be strings
}) if( row[prop] == null) {
.on('end', function(info) { continue; // don't mess with null
//console.log(info) } else if( parseIso8601.call(this, row[prop]) ) {
}); // if string can be converted into a JS date, it probably
// is a JS date -- replace the string with the date
row[prop] = parseIso8601.call(this, row[prop] + 'Z')
} else if( !isNaN(row[prop]) ) {
// take care of strings that are really numbers
row[prop] = +(row[prop]);
}
}
resultSet.push(row)
}) })
.on('error', function(err) {
errorDetected = true
self.emit('sql', this.sql)
self.emit('error', err, this.callee)
}.bind(this))
.on('end', function(info) {
if(errorDetected) {
return
}
self.emit('sql', this.sql)
// we need to figure out whether to send the result set
// or info depending upon the type of query
if( /^show/.test(this.sql.toLowerCase()) ||
/^select/.test(this.sql.toLowerCase()) ||
/^describe/.test(this.sql.toLowerCase()) ||
( resultSet.length && /^call/.test(this.sql.toLowerCase()) ) ) {
self.emit('success', this.formatResults(resultSet))
} else {
self.emit('success', this.formatResults(info))
}
}.bind(this));
}.bind(this))
.on('error', function(err) { .on('error', function(err) {
console.log( stack ) if(errorDetected) {
//this.emit('error', err, this.callee) return
}) }
.on('end', function() { errorDetected = true
this.emit('sql', this.sql) self.emit('sql', this.sql)
this.emit('success', this.formatResults(resultSet)) self.emit('error', err, this.callee)
}.bind(this)) }.bind(this))
.on('end', function(info) {
// nothing here (query info is returned during the 'result' event)
}.bind(this)).setMaxListeners(100)
return this return this
} }
var parseIso8601 = function(CurDate) {
// note -- this function is borrowed from the following author
// Author: Jim Davis, The Depressed Press of Boston
// Library: DP_DateExtensions
// Website: www.depressedpress.com
// Check the input parameters
if ( typeof CurDate != "string" || CurDate == "" ) {
return null;
};
// Set the fragment expressions
var S = "[\\-/:.]";
var Yr = "((?:1[6-9]|[2-9][0-9])[0-9]{2})";
var Mo = S + "((?:1[012])|(?:0[1-9])|[1-9])";
var Dy = S + "((?:3[01])|(?:[12][0-9])|(?:0[1-9])|[1-9])";
var Hr = "(2[0-4]|[01]?[0-9])";
var Mn = S + "([0-5]?[0-9])";
var Sd = "(?:" + S + "([0-5]?[0-9])(?:[.,]([0-9]+))?)?";
var TZ = "(?:(Z)|(?:([\+\-])(1[012]|[0]?[0-9])(?::?([0-5]?[0-9]))?))?";
// RegEx the input
// First check: Just date parts (month and day are optional)
// Second check: Full date plus time (seconds, milliseconds and TimeZone info are optional)
var TF;
if ( TF = new RegExp("^" + Yr + "(?:" + Mo + "(?:" + Dy + ")?)?" + "$").exec(CurDate) ) {} else if ( TF = new RegExp("^" + Yr + Mo + Dy + "[Tt ]" + Hr + Mn + Sd + TZ + "$").exec(CurDate) ) {};
// If the date couldn't be parsed, return null
if ( !TF ) { return null };
// Default the Time Fragments if they're not present
if ( !TF[2] ) { TF[2] = 1 } else { TF[2] = TF[2] - 1 };
if ( !TF[3] ) { TF[3] = 1 };
if ( !TF[4] ) { TF[4] = 0 };
if ( !TF[5] ) { TF[5] = 0 };
if ( !TF[6] ) { TF[6] = 0 };
if ( !TF[7] ) { TF[7] = 0 };
if ( !TF[8] ) { TF[8] = null };
if ( TF[9] != "-" && TF[9] != "+" ) { TF[9] = null };
if ( !TF[10] ) { TF[10] = 0 } else { TF[10] = TF[9] + TF[10] };
if ( !TF[11] ) { TF[11] = 0 } else { TF[11] = TF[9] + TF[11] };
// If there's no timezone info the data is local time
if ( !TF[8] && !TF[9] ) {
return new Date(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]);
};
// If the UTC indicator is set the date is UTC
if ( TF[8] == "Z" ) {
return new Date(Date.UTC(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]));
};
// If the date has a timezone offset
if ( TF[9] == "-" || TF[9] == "+" ) {
// Get current Timezone information
var CurTZ = new Date().getTimezoneOffset();
var CurTZh = TF[10] - ((CurTZ >= 0 ? "-" : "+") + Math.floor(Math.abs(CurTZ) / 60))
var CurTZm = TF[11] - ((CurTZ >= 0 ? "-" : "+") + (Math.abs(CurTZ) % 60))
// Return the date
return new Date(TF[1], TF[2], TF[3], TF[4] - CurTZh, TF[5] - CurTZm, TF[6], TF[7]);
};
// If we've reached here we couldn't deal with the input, return null
return null;
};
return Query return Query
})() })()
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
"validator": "1.1.1", "validator": "1.1.1",
"moment": "~1.7.0", "moment": "~1.7.0",
"commander": "~0.6.0", "commander": "~0.6.0",
"generic-pool": "1.0.9", "generic-pool": "1.0.12",
"dottie": "0.0.6-1", "dottie": "0.0.6-1",
"toposort-class": "0.1.4" "toposort-class": "0.1.4"
}, },
...@@ -47,8 +47,7 @@ ...@@ -47,8 +47,7 @@
"mariasql": "~0.1.18", "mariasql": "~0.1.18",
"buster": "~0.6.0", "buster": "~0.6.0",
"watchr": "~2.2.0", "watchr": "~2.2.0",
"yuidocjs": "~0.3.36", "yuidocjs": "~0.3.36"
"mariasql": "~0.1.18"
}, },
"keywords": [ "keywords": [
"mysql", "mysql",
......
...@@ -13,6 +13,15 @@ module.exports = { ...@@ -13,6 +13,15 @@ module.exports = {
pool: { maxConnections: 5, maxIdleTime: 30} pool: { maxConnections: 5, maxIdleTime: 30}
}, },
mariadb: {
username: "root",
password: null,
database: 'sequelize_test',
host: '127.0.0.1',
port: 3306,
pool: { maxConnections: 5, maxIdleTime: 30}
},
sqlite: { sqlite: {
}, },
......
var config = require("./config/config") var config = require("./config/config")
, Sequelize = require("../index") , Sequelize = require("../index")
, dialects = ['sqlite', 'mysql', 'postgres'] , dialects = ['sqlite', 'mysql', 'mariadb', 'postgres']
describe('DAOFactory', function() { describe('DAOFactory', function() {
dialects.forEach(function(dialect) { dialects.forEach(function(dialect) {
......
var config = require("./config/config") var config = require("./config/config")
, Sequelize = require("../index") , Sequelize = require("../index")
, dialects = ['sqlite', 'mysql', 'postgres'] , dialects = ['sqlite', 'mysql', 'mariadb', 'postgres']
describe('DAO', function() { describe('DAO', function() {
dialects.forEach(function(dialect) { dialects.forEach(function(dialect) {
......
...@@ -25,7 +25,8 @@ module.exports = { ...@@ -25,7 +25,8 @@ module.exports = {
database: 'sequelize_test', database: 'sequelize_test',
host: '127.0.0.1', host: '127.0.0.1',
port: 3306, port: 3306,
pool: { maxConnections: 5, maxIdleTime: 30} pool: { maxConnections: 5, maxIdleTime: 30},
logging: console.log
}, },
sqlite: { sqlite: {
......
...@@ -168,6 +168,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -168,6 +168,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
Helpers.checkMatchForDialects(dialect, err.message, { Helpers.checkMatchForDialects(dialect, err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/, sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: /.*Duplicate\ entry.*/, mysql: /.*Duplicate\ entry.*/,
mariadb: /.*Duplicate\ entry.*/,
postgres: /.*duplicate\ key\ value.*/ postgres: /.*duplicate\ key\ value.*/
}) })
...@@ -190,6 +191,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -190,6 +191,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
Helpers.checkMatchForDialects(dialect, err.message, { Helpers.checkMatchForDialects(dialect, err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/, sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: "Column 'smth' cannot be null", mysql: "Column 'smth' cannot be null",
mariadb: "Column 'smth' cannot be null",
postgres: /.*column "smth" violates not-null.*/ postgres: /.*column "smth" violates not-null.*/
}) })
...@@ -200,6 +202,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -200,6 +202,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
Helpers.checkMatchForDialects(dialect, err.message, { Helpers.checkMatchForDialects(dialect, err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/, sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: "Duplicate entry 'foo' for key 'username'", mysql: "Duplicate entry 'foo' for key 'username'",
mariadb: "Duplicate entry 'foo' for key 'username'",
postgres: /.*duplicate key value violates unique constraint.*/ postgres: /.*duplicate key value violates unique constraint.*/
}) })
...@@ -1499,7 +1502,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1499,7 +1502,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}) })
}) })
if (dialect === "mysql") { if (dialect === "mysql" || dialect === "mariadb" ) {
it("should take schemaDelimiter into account if applicable", function(done){ it("should take schemaDelimiter into account if applicable", function(done){
var UserSpecialUnderscore = this.sequelize.define('UserSpecialUnderscore', {age: Sequelize.INTEGER}, {schema: 'hello', schemaDelimiter: '_'}) var UserSpecialUnderscore = this.sequelize.define('UserSpecialUnderscore', {age: Sequelize.INTEGER}, {schema: 'hello', schemaDelimiter: '_'})
var UserSpecialDblUnderscore = this.sequelize.define('UserSpecialDblUnderscore', {age: Sequelize.INTEGER}) var UserSpecialDblUnderscore = this.sequelize.define('UserSpecialDblUnderscore', {age: Sequelize.INTEGER})
......
...@@ -7,7 +7,7 @@ if(typeof require === 'function') { ...@@ -7,7 +7,7 @@ if(typeof require === 'function') {
var qq = function(str) { var qq = function(str) {
if (dialect == 'postgres' || dialect == 'sqlite') { if (dialect == 'postgres' || dialect == 'sqlite') {
return '"' + str + '"' return '"' + str + '"'
} else if (dialect == 'mysql') { } else if (dialect == 'mysql' || dialect == 'mariadb') {
return '`' + str + '`' return '`' + str + '`'
} else { } else {
return str return str
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!