Created
September 2, 2011 08:08
-
-
Save ruthlessfish/1188147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// namespace | |
var WTF = {}; | |
// -------------------------------------------------------------------- | |
// database configuration | |
WTF.dbConfig = { | |
'username': 'root', | |
'password': 'root', | |
'database': 'poopy', | |
'port': '/Applications/MAMP/tmp/mysql/mysql.sock' | |
} | |
// -------------------------------------------------------------------- | |
// database class definition | |
WTF.DB = function() { | |
this.client = require('mysql').createClient({ | |
user: WTF.dbConfig.username, | |
password: WTF.dbConfig.password, | |
port: WTF.dbConfig.port | |
}); | |
this.query('USE ' + WTF.dbConfig.database); | |
}; | |
WTF.DB.prototype = { | |
query: function(sql, callback) { | |
return this.client.query(sql, callback); | |
}, | |
close: function() { | |
this.client.end(); | |
} | |
}; | |
// -------------------------------------------------------------------- | |
// query tests | |
var db = new WTF.DB(), | |
TEST_TABLE = 'test_table'; | |
db.query('DROP TABLE IF EXISTS ' + TEST_TABLE); | |
db.query('CREATE TABLE ' + TEST_TABLE + '( | |
id INT(11) AUTO_INCREMENT, ' + ' | |
title VARCHAR(255), ' + ' | |
text TEXT, ' + ' | |
created DATETIME, ' + ' | |
PRIMARY KEY (id) | |
)'); | |
db.query('INSERT INTO ' + TEST_TABLE + ' ' + ' | |
SET title = ?, | |
text = ?, | |
created = ?', ['super cool', 'this is a nice text', '2010-08-16 10:00:23']); | |
db.query('INSERT INTO ' + TEST_TABLE + ' ' + ' | |
SET title = ?, | |
text = ?, | |
created = ?', ['another entry', 'because 2 entries make a better test', '2010-08-16 12:42:15']); | |
db.query('SELECT * FROM ' + TEST_TABLE, function selectCb(err, results, fields) { | |
if (err) { | |
throw err; | |
} | |
console.log(results); | |
console.log(fields); | |
}); | |
db.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment