Skip to content

Commit 6dbbd3b

Browse files
committed
Fix timestamp handling in the MSSQL driver (#5334)
1 parent 4ea78eb commit 6dbbd3b

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

mapmssql2008.c

+24-3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ typedef struct ms_MSSQL2008_layer_info_t {
202202
char *user_srid; /* zero length = calculate, non-zero means using this value! */
203203
char *index_name; /* hopefully this isn't necessary - but if the optimizer ain't cuttin' it... */
204204
char *sort_spec; /* the sort by specification which should be applied to the generated select statement */
205+
SQLSMALLINT *itemtypes; /* storing the sql field types for further reference */
205206

206207
msODBCconn * conn; /* Connection to db */
207208
msGeometryParserInfo gpi; /* struct for the geometry parser */
@@ -603,7 +604,7 @@ static int executeSQL(msODBCconn *conn, const char * sql)
603604
}
604605

605606
/* Get columns name from query results */
606-
static int columnName(msODBCconn *conn, int index, char *buffer, int bufferLength, layerObj *layer, char pass_field_def)
607+
static int columnName(msODBCconn *conn, int index, char *buffer, int bufferLength, layerObj *layer, char pass_field_def, SQLSMALLINT *itemType)
607608
{
608609
SQLRETURN rc;
609610

@@ -613,6 +614,7 @@ static int columnName(msODBCconn *conn, int index, char *buffer, int bufferLengt
613614
SQLULEN columnSize;
614615
SQLSMALLINT decimalDigits;
615616
SQLSMALLINT nullable;
617+
msMSSQL2008LayerInfo *layerinfo = getMSSQL2008LayerInfo(layer);
616618

617619
rc = SQLDescribeCol(
618620
conn->hstmt,
@@ -631,6 +633,8 @@ static int columnName(msODBCconn *conn, int index, char *buffer, int bufferLengt
631633
else
632634
strlcpy(buffer, (const char *)columnName, SQL_COLUMN_NAME_MAX_LENGTH + 1);
633635

636+
*itemType = dataType;
637+
634638
if (pass_field_def) {
635639
char md_item_name[256];
636640
char gml_width[32], gml_precision[32];
@@ -748,6 +752,7 @@ int msMSSQL2008LayerOpen(layerObj *layer)
748752
layerinfo->index_name = NULL;
749753
layerinfo->sort_spec = NULL;
750754
layerinfo->conn = NULL;
755+
layerinfo->itemtypes = NULL;
751756

752757
layerinfo->conn = (msODBCconn *) msConnPoolRequest(layer);
753758

@@ -976,11 +981,19 @@ static int prepare_database(layerObj *layer, rectObj rect, char **query_string)
976981
char buffer[10000] = "";
977982

978983
for(t = 0; t < layer->numitems; t++) {
979-
#ifdef USE_ICONV
984+
if (layerinfo->itemtypes && (layerinfo->itemtypes[t] == SQL_BINARY || layerinfo->itemtypes[t] == SQL_VARBINARY)) {
985+
#ifdef USE_ICONV
986+
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "convert(nvarchar(max), convert(varbinary(max),[%s]),2),", layer->items[t]);
987+
#else
988+
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "convert(varchar(max), convert(varbinary(max),[%s]),2),", layer->items[t]);
989+
#endif
990+
} else {
991+
#ifdef USE_ICONV
980992
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "convert(nvarchar(max), [%s]),", layer->items[t]);
981993
#else
982994
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "convert(varchar(max), [%s]),", layer->items[t]);
983995
#endif
996+
}
984997
}
985998

986999
if (layerinfo->geometry_format == MSSQLGEOMETRY_NATIVE)
@@ -1202,6 +1215,11 @@ int msMSSQL2008LayerClose(layerObj *layer)
12021215
layerinfo->geom_table = NULL;
12031216
}
12041217

1218+
if(layerinfo->itemtypes) {
1219+
msFree(layerinfo->itemtypes);
1220+
layerinfo->itemtypes = NULL;
1221+
}
1222+
12051223
setMSSQL2008LayerInfo(layer, NULL);
12061224
msFree(layerinfo);
12071225
}
@@ -2035,6 +2053,7 @@ int msMSSQL2008LayerGetItems(layerObj *layer)
20352053
layer->numitems = cols - 1; /* dont include the geometry column */
20362054

20372055
layer->items = msSmallMalloc(sizeof(char *) * (layer->numitems + 1)); /* +1 in case there is a problem finding goeometry column */
2056+
layerinfo->itemtypes = msSmallMalloc(sizeof(SQLSMALLINT) * (layer->numitems + 1));
20382057
/* it will return an error if there is no geometry column found, */
20392058
/* so this isnt a problem */
20402059

@@ -2048,13 +2067,15 @@ int msMSSQL2008LayerGetItems(layerObj *layer)
20482067

20492068
for(t = 0; t < cols; t++) {
20502069
char colBuff[256];
2070+
SQLSMALLINT itemType;
20512071

2052-
columnName(layerinfo->conn, t + 1, colBuff, sizeof(colBuff), layer, pass_field_def);
2072+
columnName(layerinfo->conn, t + 1, colBuff, sizeof(colBuff), layer, pass_field_def, &itemType);
20532073

20542074
if(strcmp(colBuff, layerinfo->geom_column) != 0) {
20552075
/* this isnt the geometry column */
20562076
layer->items[item_num] = (char *) msSmallMalloc(strlen(colBuff) + 1);
20572077
strcpy(layer->items[item_num], colBuff);
2078+
layerinfo->itemtypes[item_num] = itemType;
20582079
item_num++;
20592080
} else {
20602081
found_geom = 1;

0 commit comments

Comments
 (0)