Index: maporaclespatial.c =================================================================== --- maporaclespatial.c (revision 10573) +++ maporaclespatial.c (working copy) @@ -147,6 +147,8 @@ item_text_array_query *items_query; /* items buffer */ SDOGeometryObj *obj[ARRAY_SIZE]; /* spatial object buffer */ SDOGeometryInd *ind[ARRAY_SIZE]; /* object indicator (null) buffer */ + + int uniqueidindex; /*allows to keep which attribute id index is used as unique id*/ } msOracleSpatialStatement; typedef @@ -1895,7 +1897,6 @@ int success, i; int function = 0; int version = 0; - int existunique = MS_FALSE; char query_str[6000]; char *table_name; char geom_column_name[100], unique[100], srid[100]; @@ -1905,6 +1906,9 @@ OCIBind *bnd1p = NULL, *bnd2p = NULL; + int existunique = MS_FALSE; + int rownumisuniquekey = MS_FALSE; + int numitemsinselect = 0; /* get layerinfo */ msOracleSpatialLayerInfo *layerinfo = (msOracleSpatialLayerInfo *)layer->layerinfo; @@ -1946,13 +1950,24 @@ return MS_FAILURE; } + /*rownum will be the first in the select if no UNIQUE key is defined or + will be added at the end of the select*/ + rownumisuniquekey = MS_TRUE; /*Define the unique*/ if (unique[0] == '\0') strcpy( unique, "rownum" ); + else + rownumisuniquekey = MS_FALSE; - /* If no SRID is provided, set it to -1 (NULL) for binding */ - if (strcmp(srid,"NULL") == 0) - strcpy(srid,"-1"); + /* Check if the unique field is already in the items list */ + existunique = MS_FALSE; + for( i=0; i < layer->numitems; ++i ) { + if (strcasecmp(unique, layer->items[i])==0) { + sthand->uniqueidindex = i; + existunique = MS_TRUE; + break; + } + } /* Check if the unique field is already in the items list */ for( i=0; i < layer->numitems; ++i ) { @@ -1961,26 +1976,31 @@ break; } } + + /* If no SRID is provided, set it to -1 (NULL) for binding */ + if (strcmp(srid,"NULL") == 0) + strcpy(srid,"-1"); + - if (existunique) - { - if (strcasecmp(unique, "rownum") == 0) - sprintf( query_str, "SELECT "); - else - sprintf( query_str, "SELECT %s, ", "rownum"); - } - else - { - if (strcasecmp(unique, "rownum") == 0) - sprintf( query_str, "SELECT %s,", unique ); - else - sprintf( query_str, "SELECT %s, %s,", "rownum", unique ); - } - + + sprintf( query_str, "SELECT "); + numitemsinselect = layer->numitems; /* allocate enough space for items */ if (layer->numitems >= 0) { - sthand->items = (item_text_array *)malloc( sizeof(item_text_array) * (layer->numitems+1) ); + /*we will always add a rownumber in the selection*/ + numitemsinselect++; + + /*if unique key in the data is specfied but is not part of the current item lists, + we should add it to the select*/ + if(existunique == MS_FALSE && rownumisuniquekey == MS_FALSE) + numitemsinselect++; + + /*the unique index is there was no uniqueid given or the uniqueid given was not part of the items lists*/ + if (existunique == MS_FALSE) + sthand->uniqueidindex = layer->numitems; + + sthand->items = (item_text_array *)malloc( sizeof(item_text_array) * (numitemsinselect) ); if (sthand->items == NULL) { msSetError( MS_ORACLESPATIALERR,"Cannot allocate layerinfo->items buffer","msOracleSpatialLayerWhichShapes()" ); @@ -1988,20 +2008,27 @@ return MS_FAILURE; } - items = (OCIDefine **)malloc(sizeof(OCIDefine *)*(layer->numitems+1)); + items = (OCIDefine **)malloc(sizeof(OCIDefine *)*(numitemsinselect)); if (items == NULL) { msSetError( MS_ORACLESPATIALERR,"Cannot allocate items buffer","msOracleSpatialLayerWhichShapes()" ); free(table_name); return MS_FAILURE; } - memset(items ,0,sizeof(OCIDefine *)*(layer->numitems+1)); + memset(items ,0,sizeof(OCIDefine *)*(numitemsinselect)); } /* define SQL query */ for( i=0; i < layer->numitems; ++i ) sprintf( query_str + strlen(query_str), "%s, ", layer->items[i] ); + /*we add the uniqueid if it was not part of the current item list*/ + if(existunique == MS_FALSE && rownumisuniquekey == MS_FALSE) + sprintf( query_str + strlen(query_str), "%s,", unique); + + /*we always want to add rownum is the selection to allow paging to work*/ + sprintf( query_str + strlen(query_str), "%s, ", "rownum"); + sprintf( query_str + strlen(query_str), "%s FROM %s", geom_column_name, table_name ); osFilteritem(layer, function, query_str, 1); @@ -2068,7 +2095,7 @@ if (success && layer->numitems >= 0) { - for( i=0; i <= layer->numitems && success; ++i ) + for( i=0; i < numitemsinselect && success; ++i ) success = TRY( hand, OCIDefineByPos( sthand->stmthp, &items[i], hand->errhp, (ub4)i+1, (dvoid *)sthand->items[i], (sb4)TEXT_SIZE, SQLT_STR, (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)OCI_DEFAULT ) ); } @@ -2173,7 +2200,7 @@ ind = sthand->ind[ sthand->row ]; /* get the items for the shape */ - shape->index = atol( (char *)(sthand->items[0][ sthand->row ])); /* Primary Key */ + shape->index = atol( (char *)(sthand->items[sthand->uniqueidindex][ sthand->row ])); /* Primary Key */ shape->tileindex = sthand->row_num; /* Index into cursor */ shape->numvalues = layer->numitems; @@ -2186,7 +2213,7 @@ for( i=0; i < shape->numvalues; ++i ) { - shape->values[i] = (char *)malloc(strlen((char *)sthand->items[i+1][ sthand->row ])+1); + shape->values[i] = (char *)malloc(strlen((char *)sthand->items[i][ sthand->row ])+1); if (shape->values[i] == NULL) { msSetError( MS_ORACLESPATIALERR, "No memory avaliable to allocate the items", "msOracleSpatialLayerNextShape()" ); @@ -2194,8 +2221,8 @@ } else { - strcpy(shape->values[i], (char *)sthand->items[i+1][ sthand->row ]); - shape->values[i][strlen((char *)sthand->items[i+1][ sthand->row ])] = '\0'; + strcpy(shape->values[i], (char *)sthand->items[i][ sthand->row ]); + shape->values[i][strlen((char *)sthand->items[i][ sthand->row ])] = '\0'; } }