MCPcopy Index your code
hub / github.com/MapServer/MapServer / msPostGISRetrieveVersion

Function msPostGISRetrieveVersion

mappostgis.c:1010–1066  ·  view source on GitHub ↗

** Get the PostGIS version number from the database as integer. ** Versions are multiplied out as with PgSQL: 1.5.2 -> 10502, 2.0.0 -> 20000. */

Source from the content-addressed store, hash-verified

1008** Versions are multiplied out as with PgSQL: 1.5.2 -> 10502, 2.0.0 -> 20000.
1009*/
1010static int
1011msPostGISRetrieveVersion(PGconn *pgconn) {
1012 static char* sql = "SELECT postgis_version()";
1013 int version = 0;
1014 size_t strSize;
1015 char *strVersion = NULL;
1016 char *ptr;
1017 char *strParts[3] = { NULL, NULL, NULL };
1018 int i = 0, j = 0;
1019 int factor = 10000;
1020 PGresult *pgresult = NULL;
1021
1022 if ( ! pgconn ) {
1023 msSetError(MS_QUERYERR, "No open connection.", "msPostGISRetrieveVersion()");
1024 return MS_FAILURE;
1025 }
1026
1027 pgresult = PQexecParams(pgconn, sql,0, NULL, NULL, NULL, NULL, 0);
1028
1029 if ( !pgresult || PQresultStatus(pgresult) != PGRES_TUPLES_OK) {
1030 msSetError(MS_QUERYERR, "Error executing SQL: %s", "msPostGISRetrieveVersion()", sql);
1031 return MS_FAILURE;
1032 }
1033
1034 if (PQgetisnull(pgresult, 0, 0)) {
1035 PQclear(pgresult);
1036 msSetError(MS_QUERYERR,"Null result returned.","msPostGISRetrieveVersion()");
1037 return MS_FAILURE;
1038 }
1039
1040 strSize = PQgetlength(pgresult, 0, 0) + 1;
1041 strVersion = (char*)msSmallMalloc(strSize);
1042 strlcpy(strVersion, PQgetvalue(pgresult, 0, 0), strSize);
1043 PQclear(pgresult);
1044
1045 ptr = strVersion;
1046 strParts[j++] = strVersion;
1047 while( ptr != '\0' && j < 3 ) {
1048 if ( *ptr == '.' ) {
1049 *ptr = '\0';
1050 strParts[j++] = ptr + 1;
1051 }
1052 if ( *ptr == ' ' ) {
1053 *ptr = '\0';
1054 break;
1055 }
1056 ptr++;
1057 }
1058
1059 for( i = 0; i < j; i++ ) {
1060 version += factor * atoi(strParts[i]);
1061 factor = factor / 100;
1062 }
1063 free(strVersion);
1064
1065 return version;
1066}
1067

Callers 1

msPostGISLayerOpenFunction · 0.85

Calls 3

msSetErrorFunction · 0.85
msSmallMallocFunction · 0.85
strlcpyFunction · 0.85

Tested by

no test coverage detected