PyTuple *DBResultToRowList(DBQueryResult &result, const char *type) { uint32 cc = result.ColumnCount(); if(cc == 0) return(new PyTuple(0)); uint32 r; PyTuple *res = new PyTuple(2); PyList *cols = new PyList(cc); PyList *reslist = new PyList(); res->SetItem( 0, cols ); res->SetItem( 1, reslist ); //list off the column names: for(r = 0; r < cc; r++) { cols->SetItemString(r, result.ColumnName(r)); } //add a line entry for each result row: DBResultRow row; while(result.GetRow(row)) { //this could be more efficient by not building the column list each time, but cloning it instead. PyObject *o = DBRowToRow(row, type); reslist->items.push_back(o); } return res; }
PyObject *DBRowToRow(DBResultRow &row, const char *type) { PyDict *args = new PyDict(); PyObject *res = new PyObject( type, args ); //list off the column names: uint32 cc = row.ColumnCount(); PyList *header = new PyList(cc); args->SetItemString("header", header); for(uint32 r = 0; r < cc; r++) { header->SetItemString(r, row.ColumnName(r)); } //lines: PyList *rowlist = new PyList(cc); args->SetItemString("line", rowlist); //add a line entry for the row: for(uint32 r = 0; r < cc; r++) { rowlist->SetItem(r, DBColumnToPyRep(row, r)); } return res; }
PyTuple *DBResultToTupleSet(DBQueryResult &result) { uint32 cc = result.ColumnCount(); if(cc == 0) return new PyTuple(0); uint32 r; PyTuple *res = new PyTuple(2); PyList *cols = new PyList(cc); PyList *reslist = new PyList(); res->items[0] = cols; res->items[1] = reslist; //list off the column names: for(r = 0; r < cc; r++) { cols->SetItemString(r, result.ColumnName(r)); } //add a line entry for each result row: DBResultRow row; while(result.GetRow(row)) { PyList *linedata = new PyList(cc); reslist->items.push_back(linedata); for(r = 0; r < cc; r++) { linedata->SetItem(r, DBColumnToPyRep(row, r)); } } return res; }
PyObject *DBResultToIndexRowset(DBQueryResult &result, uint32 key_index) { uint32 cc = result.ColumnCount(); //start building the IndexRowset PyDict *args = new PyDict(); PyObject *res = new PyObject( new PyString( "util.IndexRowset" ), args ); if(cc == 0 || cc < key_index) return res; //list off the column names: PyList *header = new PyList(cc); args->SetItemString("header", header); for(uint32 i = 0; i < cc; i++) header->SetItemString(i, result.ColumnName(i)); //RowClass: args->SetItemString("RowClass", new PyToken("util.Row")); //idName: args->SetItemString("idName", new PyString( result.ColumnName(key_index) )); //items: PyDict *items = new PyDict(); args->SetItemString("items", items); //add a line entry for each result row: DBResultRow row; while(result.GetRow(row)) { PyRep *key = DBColumnToPyRep(row, key_index); PyList *line = new PyList(cc); for(uint32 i = 0; i < cc; i++) line->SetItem(i, DBColumnToPyRep(row, i)); items->SetItem(key, line); } return res; }
PyObject *DBResultToRowset(DBQueryResult &result) { uint32 r; uint32 cc = result.ColumnCount(); PyDict *args = new PyDict(); PyObject *res = new PyObject( new PyString( "util.Rowset" ), args ); /* check if we have a empty query result and return a empty RowSet */ if( cc == 0 ) return res; //list off the column names: PyList *header = new PyList( cc ); args->SetItemString("header", header); for(r = 0; r < cc; r++) { header->SetItemString( r, result.ColumnName(r)); } //RowClass: args->SetItemString("RowClass", new PyToken("util.Row")); //lines: PyList *rowlist = new PyList(); args->SetItemString("lines", rowlist); //add a line entry for each result row: DBResultRow row; while(result.GetRow(row)) { PyList *linedata = new PyList( cc ); rowlist->AddItem(linedata); for(r = 0; r < cc; r++) { linedata->SetItem( r, DBColumnToPyRep(row, r) ); } } return res; }