[GRASS-SVN] r49214 - grass/trunk/lib/db/dbmi_client

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 14 02:53:31 EST 2011


Author: martinl
Date: 2011-11-13 23:53:31 -0800 (Sun, 13 Nov 2011)
New Revision: 49214

Modified:
   grass/trunk/lib/db/dbmi_client/select.c
Log:
dbmilib: fix db_select_CatValArray() for case `key == col`


Modified: grass/trunk/lib/db/dbmi_client/select.c
===================================================================
--- grass/trunk/lib/db/dbmi_client/select.c	2011-11-14 07:51:49 UTC (rev 49213)
+++ grass/trunk/lib/db/dbmi_client/select.c	2011-11-14 07:53:31 UTC (rev 49214)
@@ -267,7 +267,7 @@
 			  const char *col, const char *where,
 			  dbCatValArray * cvarr)
 {
-    int i, type, more, nrows;
+    int i, type, more, nrows, ncols;
     char buf[1024];
     dbString stmt;
     dbCursor cursor;
@@ -288,7 +288,14 @@
     }
     db_init_string(&stmt);
 
-    sprintf(buf, "SELECT %s, %s FROM %s", key, col, tab);
+    if (strcmp(key, col) == 0) {
+	ncols = 1;
+	sprintf(buf, "SELECT %s FROM %s", key, tab);
+    }
+    else {
+	ncols = 2;
+	sprintf(buf, "SELECT %s, %s FROM %s", key, col, tab);
+    }
     db_set_string(&stmt, buf);
 
     if (where != NULL && strlen(where) > 0) {
@@ -326,16 +333,17 @@
 	return -1;
     }
 
-    column = db_get_table_column(table, 1);
-    type = db_sqltype_to_Ctype(db_get_column_sqltype(column));
-    G_debug(3, "  col type = %d", type);
+    if (ncols == 2) {
+	column = db_get_table_column(table, 1);
+	type = db_sqltype_to_Ctype(db_get_column_sqltype(column));
+	G_debug(3, "  col type = %d", type);
 
-    /*
-       if ( type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE ) {
-       G_fatal_error ( "Column type not supported by db_select_to_array()" );
-       }
-     */
-
+	/*
+	  if ( type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE ) {
+	  G_fatal_error ( "Column type not supported by db_select_to_array()" );
+	  }
+	*/
+    }
     cvarr->ctype = type;
 
     /* fetch the data */
@@ -347,8 +355,10 @@
 	value = db_get_column_value(column);
 	cvarr->value[i].cat = db_get_value_int(value);
 
-	column = db_get_table_column(table, 1);
-	value = db_get_column_value(column);
+	if (ncols == 2) {
+	    column = db_get_table_column(table, 1);
+	    value = db_get_column_value(column);
+	}
 	cvarr->value[i].isNull = value->isNull;
 	switch (type) {
 	case (DB_C_TYPE_INT):
@@ -394,7 +404,7 @@
 
     db_CatValArray_sort(cvarr);
 
-    return (nrows);
+    return nrows;
 }
 
 /*!



More information about the grass-commit mailing list