Skip to content

Commit

Permalink
better response parser, test against basex 7
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed Oct 8, 2011
1 parent d040b2e commit 13f191e
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -30,7 +30,7 @@ Test it. This assumes BaseX is running on the host
## Installing BaseX
Requires Java.
1. Download from http://basex.org/products/download/all-downloads/
tested against version 6.7.1
tested against BaseX version 7.0
1. run the basexserver script


Expand Down
2 changes: 2 additions & 0 deletions changelog.md
@@ -1,3 +1,5 @@
## v0.4.0 - 2011-10-09
better response parsing, run against BaseX 7
## v0.3.1 - 2011-10-06
Fixes.
## v0.3.0 - 2011-10-05
Expand Down
2 changes: 1 addition & 1 deletion examples/AddExample.js
Expand Up @@ -5,7 +5,7 @@
var basex = require("../index");
// create session
var client = new basex.Session("localhost", 1984, "admin", "admin");

basex.debug_mode = true;
// create new database
client.execute("create db database", basex.print);

Expand Down
3 changes: 1 addition & 2 deletions examples/Example.js
Expand Up @@ -8,9 +8,8 @@ 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);
console.log("At close ",t2-t0," milliseconds");
}
};
var t0=new Date();
Expand Down
2 changes: 2 additions & 0 deletions examples/Httpserver.js
Expand Up @@ -6,7 +6,9 @@ var http = require('http');
function list(req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
session.execute("info", function(err, r) {
res.write("<pre>");
res.write(r.result);
res.write("</pre>");
res.end();
})
};
Expand Down
8 changes: 5 additions & 3 deletions examples/QueryBindExample.js
Expand Up @@ -12,13 +12,15 @@ var input = "declare variable $name external; for $i in 1 to 10 return element {
var query = session.query(input);

// bind variable
query.bind("name", "nodex",basex.print);
query.bind("name", "nodex","",basex.print);

// print results
query.execute(basex.print);

// do it again
query.bind("name", "again","",basex.print);
query.execute(basex.print);
query.info(basex.print);
query.iter(basex.print);

// close query instance
query.close();

Expand Down
6 changes: 3 additions & 3 deletions examples/QueryExample.js
Expand Up @@ -5,16 +5,16 @@
*
*/
var basex = require("../index");

basex.debug_mode = true;
// create session
var session = new basex.Session("localhost", 1984, "admin", "admin");

// create query instance
var input = 'for $i in 1 to 10 return <xml>Text { $i }</xml>';
var query = session.query(input);

query.execute(basex.print);
query.info(basex.print);
query.iter(basex.print);
//query.info(basex.print);
//query.options(basex.print);
// loop through all results
//while(query.more()) {
Expand Down
11 changes: 0 additions & 11 deletions examples/books.out.xml

This file was deleted.

93 changes: 58 additions & 35 deletions index.js
Expand Up @@ -36,6 +36,7 @@ var BaseXStream = function(host, port, username, password) {
this.current_cmd=null;
this.reset = function() {
this.state = states.DISCONNECTED;
this.closefn=null;
this.buffer = "";
this.command_queue = new Queue(); // holds commands to send

Expand All @@ -60,7 +61,7 @@ var BaseXStream = function(host, port, username, password) {
stream.on("connect", function() {
self.state = states.CONNECTING;
if (exports.debug_mode) {
console.log("connected");
console.log("stream connected");
};
});

Expand All @@ -75,7 +76,7 @@ var BaseXStream = function(host, port, username, password) {
self.parser();
self.state = states.AUTHORIZE;
} else if (self.state == states.AUTHORIZE) {
if (!0 == self.read().charCodeAt(0))
if (!self.ok())
throw "Access denied.";
self.state = states.CONNECTED;
if (exports.debug_mode) {
Expand All @@ -99,15 +100,21 @@ var BaseXStream = function(host, port, username, password) {
});

stream.on("error", function(msg) {
console.log("error", msg);
console.log("stream error", msg);
});

stream.on("close", function() {
console.log("close");
if (exports.debug_mode) {
console.log("stream close");
};
if(self.closefn){
self.closefn();
self.closefn=null;
};
});

stream.on("end", function() {
console.log("end");
//console.log("stream end");
});

stream.on("drain", function() {
Expand All @@ -129,6 +136,9 @@ var BaseXStream = function(host, port, username, password) {
self.buffer = self.buffer.substr(1);
return ip;
};
this.ok=function(){
return self.read()==CHR0;
};
// read upto null
this.readline = function() {
var p = self.buffer.indexOf(CHR0);
Expand All @@ -140,16 +150,31 @@ var BaseXStream = function(host, port, username, password) {
};
this.getResponse = function() {
if (-1 != self.buffer.indexOf(CHR0)) {
var reply = {
result : self.readline(),
info : self.readline()
};
self.read();
return reply;
var result=self.readline();
var info =self.readline();
var ok=self.ok();
return {result:result,info:info};
}

};
// read line and byte
this.getResponse2 = function() {
if (-1 != self.buffer.indexOf(CHR0)) {
var reply=self.readline();
var ok=self.ok();
return {result:reply,ok:ok}
}

};
this.readiter = function() {
console.log("reaqditer");
if (-1 != self.buffer.indexOf(CHR0)) {
var ok=self.ok();
var reply=self.readline();
return {result:reply,ok:ok}
}

};

// add command and returns the result:
this.execute = function(cmd, callback) {
self.send_command({
Expand All @@ -166,31 +191,31 @@ var BaseXStream = function(host, port, username, password) {
this.create = function(name, input,callback) {
self.send_command({
send : "\x08" + name+ CHR0+input,
parser : self.getResponse,
parser : self.getResponse2,
callback : callback
});
};
// Adds a document to the current database from an input stream:
this.add = function(name, target, input,callback) {
self.send_command({
send : "\x09" + name + CHR0+target+CHR0+input,
parser : self.getResponse,
parser : self.getResponse2,
callback : callback
});
};
// Replaces a document with the specified input stream:
this.replace = function(path, input,callback) {
self.send_command({
send : "\x0C" + path + CHR0+input,
parser : self.getResponse,
parser : self.getResponse2,
callback : callback
});
};
// Stores raw data at the specified path:
this.store = function(path, input,callback) {
self.send_command({
send : "\x0D" + path + CHR0+input,
parser : self.getResponse,
parser : self.getResponse2,
callback : callback
});
};
Expand All @@ -212,17 +237,12 @@ var BaseXStream = function(host, port, username, password) {
callback : callback
});
};
// end the session
// end the session, sash callback, for stream end
this.close = function(callback) {
self.closefn=callback;
self.send_command({
send : "exit" + CHR0,
parser : self.getResponse,
callback : function(err,reply){
console.log("close",reply);
self.stream.end();
//@TODO wrong here
if(callback)callback(err,reply);
}
});
};
// -------end of commands ---------
Expand All @@ -242,6 +262,7 @@ var BaseXStream = function(host, port, username, password) {
if(typeof s === "function"){
s=s();
};
assert.equal(self.buffer.length,0,"buffer not empty:"+self.buffer);
self.send(s);
};
events.EventEmitter.call(this);
Expand All @@ -258,17 +279,18 @@ var Query = function(session,query){
this.close=function(callback){
self.session.send_command({
send:function(){return "\x02"+ self.id},
parser:self.session.getResponse,
parser:self.session.getResponse2,
callback:callback
});
};

this.bind=function(name,value,callback){
this.bind=function(name,value,type,callback){
var type="";
self.session.send_command({
send:function(){
return "\x03"+ self.id +"\x00"+name+"\x00"+value+"\x00"
return "\x03"+ self.id +"\x00"+name+"\x00"+value+"\x00"+type
},
parser:self.session.getResponse,
parser:self.session.getResponse2,
callback:callback
});

Expand All @@ -277,43 +299,44 @@ var Query = function(session,query){
this.iter=function(callback){
self.session.send_command({
send:function(){return "\x04"+ self.id},
parser:self.session.getResponse,
parser:self.session.readiter,
callback:callback
});
};

this.execute=function(callback){
self.session.send_command({
send: function(){return "\x05"+ self.id},
parser:self.session.getResponse,
parser:self.session.getResponse2,
callback:callback
});
};

this.info=function(callback){
self.session.send_command({
send:function(){return "\x06"+ self.id},
parser:self.session.getResponse,
parser:self.session.getResponse2,
callback:callback
});
};

this.options=function(callback){
self.session.send_command({
send: function(){return "\x07"+ self.id},
parser:self.session.getResponse,
callback:callback
parser:self.session.getResponse2,
callback:callback
});
};

// request id
session.send_command({
send : CHR0+query ,
parser : self.session.getResponse,
parser : self.session.getResponse2,
callback : function(err,reply){
self.id=reply.result;
console.log("query id: ",query);
console.log("ID: ",self.id);
if (exports.debug_mode) {
console.log("Query id: ",self.id,", query: ",query);
};
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,5 +1,5 @@
{ "name" : "basex",
"version" : "0.3.1",
"version" : "0.4.0",
"description" : "A BaseX (XML database) client library",
"keywords": [
"xml",
Expand Down

0 comments on commit 13f191e

Please sign in to comment.