/* nthist_dbtoobj
* for processing the response to ajax queries
* convert database output to an array of javascript object
* expects one or more of
* [o|t]<name>
* output of "psql -A -d<database> -c<query>"
* where o indicates object, one row is processed
* and t indicates array of objects and every row is processed
*
*/
function nthist_dbtoobj( rsp ) {
var r={};
var current_type = null;
var current_object = null;
var myarr = [];
var tmpo = {};
var prps = null;
var lines = rsp.split( '\n' );
for( var J = 0; J < lines.length; J++ ){
/* end of object or array of objects */
if( ! lines[J].replace( /.[0-9]+ rows*./, "" ) ) {
if( myarr.length ) {
r[ current_object ] = myarr;
}
current_type = null;
prps = null;
myarr = [];
continue;
}
/* not a new object -- data for object */
if( current_type && prps ){
tmpo = {};
data = lines[J].split( '|' );
for( var K = 0; K < prps.length; K++ )
{
if( current_type === "o" ) {
r[ current_object ][ prps[ K ] ] = data[ K ];
} else {
tmpo[ prps[ K ] ] = data[ K ];
}
}
if( current_type == "t" ) {
myarr.push( tmpo );
}
}
/* these are the properties */
if( current_type && ! prps ){
prps = lines[J].split( '|' );
}
/* */
if( ! current_type ){
current_type = lines[J].substr( 0, 1);
current_object = lines[J].substr( 1 );
if( current_type == "t" ) {
r[ current_object ] = [];
} else {
r[ current_object ] = {};
}
}
}
return r;
}