-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsql.js
40 lines (34 loc) · 1.1 KB
/
sql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var mysql = require('mysql')
module.exports = {
createDatabaseAndTable: function(databaseName, tableName) {
return [
'CREATE DATABASE IF NOT EXISTS ' + databaseName + ';'
, 'USE ' + databaseName + ';'
, 'CREATE TABLE IF NOT EXISTS ' + tableName + ' ('
, '`id` int(11) NOT NULL AUTO_INCREMENT,'
, '`value` blob,'
, '`key` blob,'
, 'PRIMARY KEY (`id`),'
, 'UNIQUE KEY `key` (`key`(767))'
, ') ENGINE=InnoDB DEFAULT CHARSET=utf8;'
].join('\n')
}
, insertInto: function(tableName, key, value) {
var escaped = mysql.escape({
key: key
, value: value
})
return [
'INSERT INTO ' + tableName + ' SET ' + escaped
, 'ON DUPLICATE KEY UPDATE ' + escaped
].join('\n')
}
, selectByKey: function(tableName, key) {
key = mysql.escape(key)
return 'SELECT `value` FROM ' + tableName + ' WHERE `key`=' + key
}
, deleteFrom: function(tableName, key) {
key = mysql.escape(key)
return 'DELETE FROM ' + tableName + ' WHERE `key`=' + key
}
}