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

You need to sign in or sign up before continuing.
Commit b9e40b31 by Sascha Depold

a lot of refactoring + specing

1 parent 789a6864
Showing with 59 additions and 37 deletions
......@@ -7,7 +7,7 @@
})
*/
var mysql = require(__dirname + "/lib/node-mysql/mysql")
exports.Sequelize = function(database, username, password) {
this.config = {
......@@ -15,7 +15,6 @@ exports.Sequelize = function(database, username, password) {
username: username,
password: password
}
this.connection = new mysql.Connection('localhost', this.config.username, this.config.password, this.config.database)
this.tables = {}
}
......@@ -32,6 +31,7 @@ exports.Sequelize.prototype = {
var table = new TableWrapper(this, this.asTableName(name), attributes)
table.attributes = attributes
this.tables[name] = {constructor: table, attributes: attributes}
table.sequelize = this
return table
},
......@@ -44,11 +44,35 @@ exports.Sequelize.prototype = {
return result
},
query: function(queryString, options) {
options = options || {}
log("Executing the query: " + queryString)
this.connection.connect()
this.connection.query(queryString, options.onSuccess, options.onError)
query: function(queryString, callback) {
var fields = []
var values = []
var self = this
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) {
this.attributes = attributes
}
table.sync = function(options) {
table.sync = function(callback) {
var fields = ["id INT"]
Helper.Hash.keys(attributes).forEach(function(name) { fields.push(name + " " + attributes[name]) })
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
sequelize.query(query, options)
sequelize.query(query, callback)
}
table.findAll = function(callback) {
var query = "SELECT * FROM " + tableName
sequelize.query(query, callback)
}
table.prototype = {
save: function(options) {
save: function(callback) {
var query = null
if(this.id == null) {
......@@ -96,7 +125,7 @@ var TableWrapper = function(sequelize, tableName, attributes) {
)
}
sequelize.query(query, options)
sequelize.query(query, callback)
}
}
......@@ -143,7 +172,7 @@ var Helper = {
case exports.Sequelize.INTEGER:
result = value; break;
default:
result = "'" + value + "'"
result = "'" + value + "'"; break;
}
return result
},
......
......@@ -18,12 +18,12 @@ vows.describe('Sequelize').addBatch({
},
'constructor': {
topic: function() {
return new Sequelize('database', 'username', 'password')
return new Sequelize('sequelize_test', 'test', 'test')
},
'sets config correctly': function(s) {
assert.equal(s.config.database, 'database')
assert.equal(s.config.username, 'username')
assert.equal(s.config.password, 'password')
assert.equal(s.config.database, 'sequelize_test')
assert.equal(s.config.username, 'test')
assert.equal(s.config.password, 'test')
},
'creates a connection object': function(s) {
assert.isObject(s.connection)
......@@ -34,7 +34,7 @@ vows.describe('Sequelize').addBatch({
},
'Sequalize#asTableName': {
topic: function() {
return new Sequelize('database', 'username', 'password')
return new Sequelize('sequelize_test', 'test', 'test')
},
'should return the correct name': function(s) {
assert.equal(s.asTableName('Name'), 'Names')
......@@ -42,32 +42,23 @@ vows.describe('Sequelize').addBatch({
},
'Sequelize#define': {
topic: function() {
var s = new Sequelize('database', 'username', 'password')
return [s, s.define('Day', { name: Sequelize.TEXT })]
var s = new Sequelize('sequelize_test', 'test', 'test')
return s.define('Day', { name: Sequelize.TEXT })
},
'should return a function': function(obj) {
var s = obj[0],
Day = obj[1]
'should return a function': function(Day) {
assert.isFunction(Day)
},
'should store attributes': function(obj) {
var s = obj[0],
Day = obj[1]
'should store attributes': function(Day) {
assert.isObject(Day.attributes)
assert.deepEqual(Day.attributes, { name: Sequelize.TEXT })
},
'should add new table to tables': function(obj) {
var s = obj[0],
Day = obj[1]
assert.include(s.tables, 'Day')
'should add new table to tables': function(Day) {
assert.include(Day.sequelize.tables, 'Day')
}
},
'Sequelize#tableNames': {
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) {
assert.deepEqual(s.tableNames, [])
......@@ -80,10 +71,12 @@ vows.describe('Sequelize').addBatch({
'Table#sync': {
topic: function() {
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) {
/* Day.sync()*/
/* Day.sync()
SequelizeHelper.log(Day.sequelize)
Day.sequelize.closeConnection()*/
}
},
'Table#drop': {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!