diff --git a/changelog.md b/changelog.md index c50fd60..5f9a7fa 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,7 @@ Changelog ========= +## v0.3.0 - 2011-10-06 +Query working except bind and iter. ## v0.2.0 - 2011-10-02 Queue working. Callback working. Query not working ## v0.1.0 - 2011-09-30 diff --git a/docs/commands.md b/docs/commands.md new file mode 100644 index 0000000..80cdea4 --- /dev/null +++ b/docs/commands.md @@ -0,0 +1,37 @@ +The BaseX Node client is asynchronous. Most functions take an option +callback function as last argument. The callback function will be +called when the results are available with two arguments. +The first will contain any error infomation or null if there is no error +the second will hold the result. +The utility function `print` shows the syntax: + + function print(err, reply) { + if (err) { + console.log("Error: " + err); + } else { + console.dir(reply); + } + }; + +## Session +var client=Session(host, port, username, password) + hostname (default="localhost") + port (default=1984) + username (default="admin") + password (default= "admin") + + +## execute + +## create +## add +## replace +## store +## info +## close + +## query + +## Debugging +The basex module variable `debug_mode` can be set to true to +print diagnostic info to the console. \ No newline at end of file diff --git a/docs/commands.txt b/docs/commands.txt deleted file mode 100644 index 126016f..0000000 --- a/docs/commands.txt +++ /dev/null @@ -1,8 +0,0 @@ -execute -query -create -add -replace -store -info -close \ No newline at end of file diff --git a/examples/Example.js b/examples/Example.js index cf0e220..4e455e8 100644 --- a/examples/Example.js +++ b/examples/Example.js @@ -1,13 +1,21 @@ /* * This example shows how database commands can be executed. - */ var basex = require("../index"); var client = new basex.Session(); +function print(err, reply) { + if (err) { + console.log("Error: " + err); + } else { + console.dir(reply); + var t2=new Date(); + console.log("At close milliseconds:",t2-t0); + } +}; var t0=new Date(); client.execute("xquery 1 to 10",basex.print); -client.close(basex.print); +client.close(print); var t1=new Date(); // not a true time because basex commands not yet done. console.log("milliseconds:",t1-t0); diff --git a/examples/Httpserver.js b/examples/Httpserver.js index 6f88cb0..ba46fb0 100644 --- a/examples/Httpserver.js +++ b/examples/Httpserver.js @@ -7,7 +7,7 @@ function list(req, res) { res.writeHead(200, { 'Content-Type' : 'text/plain' }); - session.execute("list", function(err, r) { + session.execute("info", function(err, r) { res.write(r.result); res.end(); }) diff --git a/examples/QueryBindExample.js b/examples/QueryBindExample.js index 68e47c2..049f45b 100644 --- a/examples/QueryBindExample.js +++ b/examples/QueryBindExample.js @@ -12,14 +12,14 @@ var input = "declare variable $name external; for $i in 1 to 10 return element { var query = session.query(input); // bind variable -query.bind("name", "number"); +query.bind("name", "number",basex.print); // print results -//query.execute(basex.print); +query.execute(basex.print); // close query instance -//query.close(); +query.close(); // close session -//session.close(); +session.close(); diff --git a/examples/QueryExample.js b/examples/QueryExample.js index 0e556ff..1a29c3b 100644 --- a/examples/QueryExample.js +++ b/examples/QueryExample.js @@ -13,10 +13,13 @@ var session = new basex.Session("localhost", 1984, "admin", "admin"); var input = 'for $i in 1 to 10 return Text { $i }'; var query = session.query(input); +query.execute(basex.print); +query.info(basex.print); +query.options(basex.print); // loop through all results -while(query.more()) { - console.log(query->next()); -} +//while(query.more()) { +// console.log(query->next()); +//} // close query instance query.close(); diff --git a/index.js b/index.js index 6671b75..16b02c0 100644 --- a/index.js +++ b/index.js @@ -70,6 +70,7 @@ var BaseXStream = function(host, port, username, password) { stream.on("data", function(reply) { self.buffer += reply; if (exports.debug_mode) { + console.log("<<"); console.dir(self.buffer); }; @@ -91,7 +92,9 @@ var BaseXStream = function(host, port, username, password) { if (exports.debug_mode) { console.log("response: ",r); }; - self.current_command.callback(null,r); + if(self.current_command.callback){ + self.current_command.callback(null,r); + } self.current_command=null; self.doNext(); } @@ -231,16 +234,21 @@ var BaseXStream = function(host, port, username, password) { this.doNext=function(){ if(self.command_queue.length==0)return self.current_command=self.command_queue.shift(); + var s=self.current_command.send; + if(typeof s === "function"){ + s=s(); + }; if (exports.debug_mode) { - console.log(">>",self.current_command.send); + console.log(">>",s); }; - self.stream.write(self.current_command.send); + self.stream.write(s); self.commands_sent+=1; }; events.EventEmitter.call(this); }; //query +// send is function as needs id var Query = function(session,query){ this.session=session; this.query=query; @@ -249,7 +257,7 @@ var Query = function(session,query){ this.close=function(callback){ self.session.send_command({ - send: "\x02"+ self.id+"\x00", + send:function(){return "\x02"+ self.id+"\x00"}, parser:self.session.getResponse, callback:callback }); @@ -257,18 +265,20 @@ var Query = function(session,query){ this.bind=function(name,value,callback){ console.log("BIND",self.id); - /* + self.session.send_command({ - send: "\x03"+ self.id +"\x00"+name+"\x00"+value+"\x00", + send:function(){ + return "\x03"+ self.id +"\x00"+name+"\x00"+value+"\x00" + }, parser:self.session.getResponse, callback:callback }); - */ + }; this.iter=function(callback){ self.session.send_command({ - send: "\x04"+ self.id+"\x00", + send:function(){return "\x04"+ self.id+"\x00"}, parser:self.session.getResponse, callback:callback }); @@ -276,7 +286,7 @@ var Query = function(session,query){ this.execute=function(callback){ self.session.send_command({ - send: "\x05"+ self.id+"\x00", + send: function(){return "\x05"+ self.id+"\x00"}, parser:self.session.getResponse, callback:callback }); @@ -284,7 +294,7 @@ var Query = function(session,query){ this.info=function(callback){ self.session.send_command({ - send: "\x06"+ self.id+"\x00", + send:function(){return "\x06"+ self.id+"\x00"}, parser:self.session.getResponse, callback:callback }); @@ -292,7 +302,7 @@ var Query = function(session,query){ this.options=function(callback){ self.session.send_command({ - send: "\x07"+ self.id+"\x00", + send: function(){return "\x07"+ self.id+"\x00"}, parser:self.session.getResponse, callback:callback }); @@ -311,16 +321,6 @@ var Query = function(session,query){ }; -function to_array(args) { - var len = args.length, arr = new Array(len), i; - - for (i = 0; i < len; i += 1) { - arr[i] = args[i]; - } - - return arr; -} - function md5(str) { return crypto.createHash('md5').update(str).digest("hex"); }; diff --git a/package.json b/package.json index efebd7f..10c1c6c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "basex", - "version" : "0.2.0", - "description" : "BaseX client library", + "version" : "0.3.0", + "description" : "A BaseX (XML database) client library", "keywords": [ "xml", "xquery",