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

Commit b9e40b31 by Sascha Depold

a lot of refactoring + specing

1 parent 789a6864
Showing with 59 additions and 37 deletions
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
}) })
*/ */
var mysql = require(__dirname + "/lib/node-mysql/mysql")
exports.Sequelize = function(database, username, password) { exports.Sequelize = function(database, username, password) {
this.config = { this.config = {
...@@ -15,7 +15,6 @@ exports.Sequelize = function(database, username, password) { ...@@ -15,7 +15,6 @@ exports.Sequelize = function(database, username, password) {
username: username, username: username,
password: password password: password
} }
this.connection = new mysql.Connection('localhost', this.config.username, this.config.password, this.config.database)
this.tables = {} this.tables = {}
} }
...@@ -32,6 +31,7 @@ exports.Sequelize.prototype = { ...@@ -32,6 +31,7 @@ exports.Sequelize.prototype = {
var table = new TableWrapper(this, this.asTableName(name), attributes) var table = new TableWrapper(this, this.asTableName(name), attributes)
table.attributes = attributes table.attributes = attributes
this.tables[name] = {constructor: table, attributes: attributes} this.tables[name] = {constructor: table, attributes: attributes}
table.sequelize = this
return table return table
}, },
...@@ -44,11 +44,35 @@ exports.Sequelize.prototype = { ...@@ -44,11 +44,35 @@ exports.Sequelize.prototype = {
return result return result
}, },
query: function(queryString, options) { query: function(queryString, callback) {
options = options || {} var fields = []
log("Executing the query: " + queryString) var values = []
this.connection.connect() var self = this
this.connection.query(queryString, options.onSuccess, options.onError) var connection = require(__dirname+"/lib/nodejs-mysql-native/client").createTCPClient()
connection.auto_prepare = true
connection
.auth(this.config.database, this.config.username, this.config.password)
.addListener('authorized', function() {
Helper.log("Executing the query: " + queryString)
connection
.execute(queryString)
.addListener('row', function(r){ values.push(r) })
.addListener('field', function(f){ fields.push(f)})
.addListener('end', function() {
if(callback) {
var result = []
values.forEach(function(valueArray) {
var mapping = {}
for(var i = 0; i < fields.length; i++)
mapping[fields[i].name] = valueArray[i]
result.push(mapping)
})
callback(result)
}
})
connection.close()
})
} }
} }
...@@ -67,21 +91,26 @@ var TableWrapper = function(sequelize, tableName, attributes) { ...@@ -67,21 +91,26 @@ var TableWrapper = function(sequelize, tableName, attributes) {
this.attributes = attributes this.attributes = attributes
} }
table.sync = function(options) { table.sync = function(callback) {
var fields = ["id INT"] var fields = ["id INT"]
Helper.Hash.keys(attributes).forEach(function(name) { fields.push(name + " " + attributes[name]) }) Helper.Hash.keys(attributes).forEach(function(name) { fields.push(name + " " + attributes[name]) })
var query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + fields.join(', ') + ")" var query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + fields.join(', ') + ")"
sequelize.query(query, options) sequelize.query(query, callback)
} }
table.drop = function(options) { table.drop = function(callback) {
var query = "DROP TABLE IF EXISTS " + tableName var query = "DROP TABLE IF EXISTS " + tableName
sequelize.query(query, options) sequelize.query(query, callback)
}
table.findAll = function(callback) {
var query = "SELECT * FROM " + tableName
sequelize.query(query, callback)
} }
table.prototype = { table.prototype = {
save: function(options) { save: function(callback) {
var query = null var query = null
if(this.id == null) { if(this.id == null) {
...@@ -96,7 +125,7 @@ var TableWrapper = function(sequelize, tableName, attributes) { ...@@ -96,7 +125,7 @@ var TableWrapper = function(sequelize, tableName, attributes) {
) )
} }
sequelize.query(query, options) sequelize.query(query, callback)
} }
} }
...@@ -143,7 +172,7 @@ var Helper = { ...@@ -143,7 +172,7 @@ var Helper = {
case exports.Sequelize.INTEGER: case exports.Sequelize.INTEGER:
result = value; break; result = value; break;
default: default:
result = "'" + value + "'" result = "'" + value + "'"; break;
} }
return result return result
}, },
......
...@@ -18,12 +18,12 @@ vows.describe('Sequelize').addBatch({ ...@@ -18,12 +18,12 @@ vows.describe('Sequelize').addBatch({
}, },
'constructor': { 'constructor': {
topic: function() { topic: function() {
return new Sequelize('database', 'username', 'password') return new Sequelize('sequelize_test', 'test', 'test')
}, },
'sets config correctly': function(s) { 'sets config correctly': function(s) {
assert.equal(s.config.database, 'database') assert.equal(s.config.database, 'sequelize_test')
assert.equal(s.config.username, 'username') assert.equal(s.config.username, 'test')
assert.equal(s.config.password, 'password') assert.equal(s.config.password, 'test')
}, },
'creates a connection object': function(s) { 'creates a connection object': function(s) {
assert.isObject(s.connection) assert.isObject(s.connection)
...@@ -34,7 +34,7 @@ vows.describe('Sequelize').addBatch({ ...@@ -34,7 +34,7 @@ vows.describe('Sequelize').addBatch({
}, },
'Sequalize#asTableName': { 'Sequalize#asTableName': {
topic: function() { topic: function() {
return new Sequelize('database', 'username', 'password') return new Sequelize('sequelize_test', 'test', 'test')
}, },
'should return the correct name': function(s) { 'should return the correct name': function(s) {
assert.equal(s.asTableName('Name'), 'Names') assert.equal(s.asTableName('Name'), 'Names')
...@@ -42,32 +42,23 @@ vows.describe('Sequelize').addBatch({ ...@@ -42,32 +42,23 @@ vows.describe('Sequelize').addBatch({
}, },
'Sequelize#define': { 'Sequelize#define': {
topic: function() { topic: function() {
var s = new Sequelize('database', 'username', 'password') var s = new Sequelize('sequelize_test', 'test', 'test')
return [s, s.define('Day', { name: Sequelize.TEXT })] return s.define('Day', { name: Sequelize.TEXT })
}, },
'should return a function': function(obj) { 'should return a function': function(Day) {
var s = obj[0],
Day = obj[1]
assert.isFunction(Day) assert.isFunction(Day)
}, },
'should store attributes': function(obj) { 'should store attributes': function(Day) {
var s = obj[0],
Day = obj[1]
assert.isObject(Day.attributes) assert.isObject(Day.attributes)
assert.deepEqual(Day.attributes, { name: Sequelize.TEXT }) assert.deepEqual(Day.attributes, { name: Sequelize.TEXT })
}, },
'should add new table to tables': function(obj) { 'should add new table to tables': function(Day) {
var s = obj[0], assert.include(Day.sequelize.tables, 'Day')
Day = obj[1]
assert.include(s.tables, 'Day')
} }
}, },
'Sequelize#tableNames': { 'Sequelize#tableNames': {
topic: function() { topic: function() {
return new Sequelize('database', 'username', 'password') return new Sequelize('sequelize_test', 'test', 'test')
}, },
'should be an empty array if no tables are specified': function(s) { 'should be an empty array if no tables are specified': function(s) {
assert.deepEqual(s.tableNames, []) assert.deepEqual(s.tableNames, [])
...@@ -80,10 +71,12 @@ vows.describe('Sequelize').addBatch({ ...@@ -80,10 +71,12 @@ vows.describe('Sequelize').addBatch({
'Table#sync': { 'Table#sync': {
topic: function() { topic: function() {
var s = new Sequelize('sequelize_test', 'test', 'test') var s = new Sequelize('sequelize_test', 'test', 'test')
return s.define('Day', { name: s.TEXT }) return s.define('Day', { name: Sequelize.TEXT })
}, },
'send sync call': function(Day) { 'send sync call': function(Day) {
/* Day.sync()*/ /* Day.sync()
SequelizeHelper.log(Day.sequelize)
Day.sequelize.closeConnection()*/
} }
}, },
'Table#drop': { 'Table#drop': {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!