Skip to content

Instantly share code, notes, and snippets.

@mranney
Created October 23, 2010 08:22
Show Gist options
  • Save mranney/641936 to your computer and use it in GitHub Desktop.
Save mranney/641936 to your computer and use it in GitHub Desktop.
var redis = require("redis"),
util = require("util"),
client = redis.createClient(),
commands;
redis.debug_mode = true;
// helper borrowed from node_redis
function to_array(args) {
var i;
var len = args.length;
var arr = new Array(len);
for (i = 0; i < len; i += 1) {
arr[i] = args[i];
}
return arr;
}
// new commands that we'll be adding
commands = [
"CHANGEDB",
"DUMP", "DESC",
"NORM", "DENORM"
];
// merge these in with the RedisClient prototype in both upper and lower case
commands.forEach(function (command) {
redis.RedisClient.prototype[command] = function () {
var args = to_array(arguments);
args.unshift(command); // put command at the beginning
// remove this when everything works
console.log("Sending new command " + command +
" with args " + JSON.stringify(args));
this.send_command.apply(this, args);
};
redis.RedisClient.prototype[command.toLowerCase()] =
redis.RedisClient.prototype[command];
});
// CREATE
redis.RedisClient.prototype["CREATE"] = function () {
var args = to_array(arguments), mod_args,
first_arg = args[0].toLowerCase(), command, sargs;
if (first_arg === "table") {
mod_args = "TABLE " + args[1] + " (" + args[2] + ")";
} else if (first_arg === "index") {
mod_args = "INDEX " + args[1] + " ON " + args[2] + " (" + args[3] + ")";
} else { // usage error
throw new Error("Bad args to \"CREATE\" " + args[0] +
", must be either \"TABLE\" OR \"INDEX\"");
}
// TODO - error on args.length > 4 || args.length < 2;
command = "CREATE";
sargs = mod_args.split(' ');
sargs.unshift(command); // put command at the beginning
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
console.log("Sending " + command + " with args ", util.inspect(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["create"] = redis.RedisClient.prototype["CREATE"];
// DROP
redis.RedisClient.prototype["DROP"] = function () {
var args = to_array(arguments);
var mod_args;
if (args[0].toLowerCase() === "table") {
mod_args = "TABLE " + args[1];
} else if (args[0].toLowerCase() === "index") {
mod_args = "INDEX " + args[1];
} else { // usage error
throw new Error("Bad args to \"DROP\" " + args[0] +
", must be either \"TABLE\" OR \"INDEX\"");
}
var command = "DROP";
var sargs = mod_args.split(' ');;
sargs.unshift(command); // put command at the beginning
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
console.log("Sending " + command + " with args " + JSON.stringify(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["drop"] = redis.RedisClient.prototype["DROP"];
// SELECT
redis.RedisClient.prototype["SELECT"] = function () {
var args = to_array(arguments);
var mod_args;
if (args.length != 1) { // rewrite Redisql SELECT * FROM tbl WHERE id = 4
mod_args = args[0] + " FROM " + args[1] + " WHERE " + args[2];
} else { // redis SELECT DB
mod_args = arguments;
}
var command = "SELECT";
var sargs = mod_args.split(' ');;
sargs.unshift(command); // put command at the beginning
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
console.log("Sending " + command + " with args " + JSON.stringify(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["select"] = redis.RedisClient.prototype["SELECT"];
// SCANSELECT
redis.RedisClient.prototype["SCANSELECT"] = function () {
var args = to_array(arguments);
var mod_args = args[0] + " FROM " + args[1];;
if (args.length > 3) {
mod_args += " WHERE " + args[2];
}
var command = "SCANSELECT";
var sargs = mod_args.split(' ');;
sargs.unshift(command); // put command at the beginning
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
console.log("Sending " + command + " with args " + JSON.stringify(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["scanselect"] = redis.RedisClient.prototype["SCANSELECT"];
// INSERT
redis.RedisClient.prototype["INSERT"] = function () {
var args = to_array(arguments),
mod_args = "INTO " + args[0] + " VALUES",
command = "INSERT",
sargs = mod_args.split(' ');;
sargs.unshift(command); // put command at the beginning
sargs.push(args[1]); // put val_list at end as single argument
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
// TODO - error if args length is invalid
console.log("Sending " + command + " with args " + JSON.stringify(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["insert"] = redis.RedisClient.prototype["INSERT"];
// DELETE
redis.RedisClient.prototype["DELETE"] = function () {
var args = to_array(arguments);
var mod_args = "FROM " + args[0] + " WHERE " + args[1];
var command = "DELETE";
var sargs = mod_args.split(' ');;
sargs.unshift(command); // put command at the beginning
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
console.log("Sending " + command + " with args " + JSON.stringify(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["delete"] = redis.RedisClient.prototype["DELETE"];
// UPDATE
redis.RedisClient.prototype["UPDATE"] = function () {
var args = to_array(arguments);
var mod_args = args[0] + " SET"
var sargs = mod_args.split(' ');;
sargs.push(args[1]); // push val_list at end as single argument
sargs.push("WHERE");
var wargs = args[2].split(' ');
for (var i = 0; i < wargs.length; i++) {
sargs.push(wargs[i]);
};
var command = "UPDATE";
sargs.unshift(command); // put command at the beginning
if (typeof args[args.length - 1] === "function") {
sargs.push(args[args.length - 1]);
}
console.log("Sending " + command + " with args " + JSON.stringify(sargs));
this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["update"] = redis.RedisClient.prototype["UPDATE"];
exports.createClient = function (port_arg, host_arg) {
return redis.createClient(port_arg, host_arg);
};
exports.print = redis.print;
var redis = require("./redisql"),
sys = require('sys'),
client = redis.createClient();
function last_command(err, res) {
console.log("Last result: " + res.toString());
client.quit();
}
client.flushdb(redis.print);
client.create("TABLE", "worker", "id int,division int,health int,salary TEXT, name TEXT", redis.print);
client.create("INDEX", "worker:division:index", "worker", "division", redis.print);
client.create("INDEX", "worker:health:index", "worker", "health", redis.print);
client.insert("worker", "(1,11,2,60000.66,jim)", redis.print);
client.insert("worker", "(2,22,1,30000.33,jack)", redis.print);
client.insert("worker", "(3,33,4,90000.99,bob)", redis.print);
client.insert("worker", "(4,44,3,70000.77,bill)", redis.print);
client.insert("worker", "(6,66,1,12000.99,jan)", redis.print);
client.insert("worker", "(7,66,1,11000.99,beth)", redis.print);
client.insert("worker", "(8,11,2,68888.99,mac)", redis.print);
client.insert("worker", "(9,22,1,31111.99,ken)", redis.print);
client.insert("worker", "(10,33,4,111111.99,seth)", redis.print);
client.scanselect("*", "worker", redis.print);
client.scanselect("*", "worker", "name=bill", redis.print);
client.select("*", "worker", "id=1", redis.print);
client.update("worker", "name=JIM", "id = 1", redis.print);
client.select("*", "worker", "id = 1", redis.print);
client.delete("worker", "id = 2", redis.print);
client.select("*", "worker", "id = 2", redis.print);
client.desc("worker", redis.print);
client.dump("worker", redis.print);
client.drop("index", "worker:health:index", redis.print);
client.drop("table", "worker", last_command);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment