Skip to content

Commit

Permalink
Queries part working
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed Oct 5, 2011
1 parent 14d8499 commit e7674e3
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 41 deletions.
2 changes: 2 additions & 0 deletions 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
Expand Down
37 changes: 37 additions & 0 deletions 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.
8 changes: 0 additions & 8 deletions docs/commands.txt

This file was deleted.

12 changes: 10 additions & 2 deletions 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);
2 changes: 1 addition & 1 deletion examples/Httpserver.js
Expand Up @@ -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();
})
Expand Down
8 changes: 4 additions & 4 deletions examples/QueryBindExample.js
Expand Up @@ -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();

9 changes: 6 additions & 3 deletions examples/QueryExample.js
Expand Up @@ -13,10 +13,13 @@ var session = new basex.Session("localhost", 1984, "admin", "admin");
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.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();
Expand Down
42 changes: 21 additions & 21 deletions index.js
Expand Up @@ -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);
};

Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand All @@ -249,50 +257,52 @@ 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
});
};

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
});
};

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
});
};

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
});
};

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
});
Expand All @@ -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");
};
Expand Down
4 changes: 2 additions & 2 deletions 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",
Expand Down

0 comments on commit e7674e3

Please sign in to comment.