Skip to content

Commit e653b9b

Browse files
committed
Implement order by option for the mssql driver (#5008)
1 parent fa230a6 commit e653b9b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

mapmssql2008.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ typedef struct ms_MSSQL2008_layer_info_t {
151151
char *urid_name; /* name of user-specified unique identifier or OID */
152152
char *user_srid; /* zero length = calculate, non-zero means using this value! */
153153
char *index_name; /* hopefully this isn't necessary - but if the optimizer ain't cuttin' it... */
154+
char *sort_spec; /* the sort by specification which should be applied to the generated select statement */
154155

155156
msODBCconn * conn; /* Connection to db */
156157
msGeometryParserInfo gpi; /* struct for the geometry parser */
@@ -451,7 +452,7 @@ static char *strstrIgnoreCase(const char *haystack, const char *needle)
451452
return (char *) (match < 0 ? NULL : haystack + match);
452453
}
453454

454-
static int msMSSQL2008LayerParseData(layerObj *layer, char **geom_column_name, char **geom_column_type, char **table_name, char **urid_name, char **user_srid, char **index_name, int debug);
455+
static int msMSSQL2008LayerParseData(layerObj *layer, char **geom_column_name, char **geom_column_type, char **table_name, char **urid_name, char **user_srid, char **index_name, char **sort_spec, int debug);
455456

456457
/* Close connection and handles */
457458
static void msMSSQL2008CloseConnection(void *conn_handle)
@@ -626,6 +627,7 @@ int msMSSQL2008LayerOpen(layerObj *layer)
626627
layerinfo->urid_name = NULL;
627628
layerinfo->user_srid = NULL;
628629
layerinfo->index_name = NULL;
630+
layerinfo->sort_spec = NULL;
629631
layerinfo->conn = NULL;
630632

631633
layerinfo->conn = (msODBCconn *) msConnPoolRequest(layer);
@@ -688,7 +690,7 @@ int msMSSQL2008LayerOpen(layerObj *layer)
688690

689691
setMSSQL2008LayerInfo(layer, layerinfo);
690692

691-
if (msMSSQL2008LayerParseData(layer, &layerinfo->geom_column, &layerinfo->geom_column_type, &layerinfo->geom_table, &layerinfo->urid_name, &layerinfo->user_srid, &layerinfo->index_name, layer->debug) != MS_SUCCESS) {
693+
if (msMSSQL2008LayerParseData(layer, &layerinfo->geom_column, &layerinfo->geom_column_type, &layerinfo->geom_table, &layerinfo->urid_name, &layerinfo->user_srid, &layerinfo->index_name, &layerinfo->sort_spec, layer->debug) != MS_SUCCESS) {
692694
msSetError( MS_QUERYERR, "Could not parse the layer data", "msMSSQL2008LayerOpen()");
693695
return MS_FAILURE;
694696
}
@@ -920,6 +922,10 @@ static int prepare_database(layerObj *layer, rectObj rect, char **query_string)
920922
}
921923
}
922924

925+
if (layerinfo->sort_spec) {
926+
strcat(query_string_temp, layerinfo->sort_spec);
927+
}
928+
923929
msFree(data_source);
924930
msFree(f_table_name);
925931
msFree(columns_wanted);
@@ -1021,6 +1027,11 @@ int msMSSQL2008LayerClose(layerObj *layer)
10211027
layerinfo->index_name = NULL;
10221028
}
10231029

1030+
if(layerinfo->sort_spec) {
1031+
msFree(layerinfo->sort_spec);
1032+
layerinfo->sort_spec = NULL;
1033+
}
1034+
10241035
if(layerinfo->sql) {
10251036
msFree(layerinfo->sql);
10261037
layerinfo->sql = NULL;
@@ -1995,9 +2006,9 @@ int msMSSQL2008LayerRetrievePK(layerObj *layer, char **urid_name, char* table_na
19952006
* column name, table name and name of a column to serve as a
19962007
* unique record id
19972008
*/
1998-
static int msMSSQL2008LayerParseData(layerObj *layer, char **geom_column_name, char **geom_column_type, char **table_name, char **urid_name, char **user_srid, char **index_name, int debug)
2009+
static int msMSSQL2008LayerParseData(layerObj *layer, char **geom_column_name, char **geom_column_type, char **table_name, char **urid_name, char **user_srid, char **index_name, char **sort_spec, int debug)
19992010
{
2000-
char *pos_opt, *pos_scn, *tmp, *pos_srid, *pos_urid, *pos_geomtype, *pos_geomtype2, *pos_indexHint, *data;
2011+
char *pos_opt, *pos_scn, *tmp, *pos_srid, *pos_urid, *pos_geomtype, *pos_geomtype2, *pos_indexHint, *data, *pos_order;
20012012
int slength;
20022013

20032014
data = msStrdup(layer->data);
@@ -2116,6 +2127,13 @@ static int msMSSQL2008LayerParseData(layerObj *layer, char **geom_column_name, c
21162127
}
21172128
}
21182129

2130+
/* Find the order by */
2131+
pos_order = strstrIgnoreCase(pos_opt, " order by ");
2132+
2133+
if (pos_order) {
2134+
*sort_spec = msStrdup(pos_order);
2135+
}
2136+
21192137
if(debug) {
21202138
msDebug("msMSSQL2008LayerParseData: unique column = %s, srid='%s', geom_column_name = %s, table_name=%s\n", *urid_name, *user_srid, *geom_column_name, *table_name);
21212139
}

0 commit comments

Comments
 (0)