Hello!
I've added a datatype "dict" to the list of possible DbiResults. "dicts"
returns a list of dicts, whereas "dict" returns a dict. The keys for the first
level is a simple counter. So instead of
{user_id 1 user_name Max} {user_id 2 user_name Lisa}
1 {user_id 1 user_name Max} 2 {user_id 2 user_name Lisa}
There is a slight performance hit compared to "dicts", but the result is more
useful, as you can do the following:
dict for {k v} $result {
set user_id [dict get $v user_id]
dict set result $k user_url "user?user_id=$user_id"
}
We are using dict result sets in our templating system frequently. With the
patch we could switch to ns_dbi very easily and get the performance bonus with
only a few hours of work.
Regards,
Wolfgang
# HG changeset patch
# User wiwo
# Date 1391706185 -3600
# Node ID 6c9d6b0d3ca97d78f631c765aa6421217107f2c2
# Parent c81a764b2dadc0c4fbf799484c12d3a4103e216d
added dict type as return for dbi_rows
diff -r c81a764b2dad -r 6c9d6b0d3ca9 nsdbi.h
--- a/nsdbi.h Sat Feb 01 11:41:41 2014 +0100
+++ b/nsdbi.h Thu Feb 06 18:03:05 2014 +0100
@@ -76,7 +76,8 @@
typedef enum {
Dbi_ResultFlat = 0,
Dbi_ResultSets,
- Dbi_ResultDicts
+ Dbi_ResultDicts,
+ Dbi_ResultDict
} Dbi_resultFormat;
diff -r c81a764b2dad -r 6c9d6b0d3ca9 tclcmds.c
--- a/tclcmds.c Sat Feb 01 11:41:41 2014 +0100
+++ b/tclcmds.c Thu Feb 06 18:03:05 2014 +0100
@@ -134,6 +134,7 @@
{"flat", Dbi_ResultFlat},
{"sets", Dbi_ResultSets},
{"dicts", Dbi_ResultDicts},
+ {"dict", Dbi_ResultDict},
{NULL, 0}
};
@@ -590,7 +591,7 @@
CONST char *colName;
Tcl_Obj *resObj, *valueObj, *colListObj, *queryObj;
Tcl_Obj *poolObj = NULL, *valuesObj = NULL, *colsNameObj = NULL;
- Tcl_Obj *templateObj = NULL, *defaultObj = NULL, *dictObj = NULL;
+ Tcl_Obj *templateObj = NULL, *defaultObj = NULL, *dictObj = NULL, *idxObj = NULL;
Ns_Time *timeoutPtr = NULL;
int end, status, maxRows = -1, adp = 0, missingIsNull = 0;
Dbi_quotingLevel quote = Dbi_QuoteNone;
@@ -649,12 +650,22 @@
resObj = Tcl_GetObjResult(interp);
numCols = Dbi_NumColumns(handle);
+ long rownum = 0;
while ((status = NextRow(interp, handle, &end)) == TCL_OK && !end) {
Ns_Set *set = NULL;
+ rownum++;
switch (resultFormat) {
case Dbi_ResultFlat: break;
case Dbi_ResultDicts: dictObj = Tcl_NewListObj(0,NULL); break;
+ case Dbi_ResultDict:
+ idxObj = Tcl_NewLongObj(rownum);
+ if (Tcl_ListObjAppendElement(interp, resObj, idxObj) != TCL_OK) {
+ Tcl_DecrRefCount(idxObj);
+ goto error;
+ }
+ dictObj = Tcl_NewListObj(0,NULL);
+ break;
case Dbi_ResultSets: set = Ns_SetCreate("r"); break;
}
@@ -670,7 +681,7 @@
goto error;
}
- if (resultFormat == Dbi_ResultDicts) {
+ if (resultFormat == Dbi_ResultDicts || resultFormat == Dbi_ResultDict) {
Tcl_Obj *nameObj = Tcl_NewStringObj(colName, -1);
if (Tcl_ListObjAppendElement(interp, dictObj, nameObj) != TCL_OK) {
@@ -698,6 +709,7 @@
switch (resultFormat) {
case Dbi_ResultFlat: break;
case Dbi_ResultSets: Ns_TclEnterSet(interp, set, 0); break;
+ case Dbi_ResultDict:
case Dbi_ResultDicts:
if (Tcl_ListObjAppendElement(interp, resObj, dictObj) != TCL_OK) {
Tcl_DecrRefCount(dictObj);
------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121051231&iu=/4140/ostg.clktrk
_______________________________________________
naviserver-devel mailing list
naviserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/naviserver-devel