static int vknn_create (sqlite3 * db, void *pAux, int argc, const char *const *argv, sqlite3_vtab ** ppVTab, char **pzErr) { /* creates the virtual table for R*Tree KNN metahandling */ VirtualKnnPtr p_vt; char *buf; char *vtable; char *xname; if (pAux) pAux = pAux; /* unused arg warning suppression */ if (argc == 3) { vtable = gaiaDequotedSql ((char *) argv[2]); } else { *pzErr = sqlite3_mprintf ("[VirtualKNN module] CREATE VIRTUAL: illegal arg list {void}\n"); return SQLITE_ERROR; } p_vt = (VirtualKnnPtr) sqlite3_malloc (sizeof (VirtualKnn)); if (!p_vt) return SQLITE_NOMEM; p_vt->db = db; p_vt->pModule = &my_knn_module; p_vt->nRef = 0; p_vt->zErrMsg = NULL; p_vt->knn_ctx = vknn_create_context (); /* preparing the COLUMNs for this VIRTUAL TABLE */ xname = gaiaDoubleQuotedSql (vtable); buf = sqlite3_mprintf ("CREATE TABLE \"%s\" (f_table_name TEXT, " "f_geometry_column TEXT, ref_geometry BLOB, max_items INTEGER, " "pos INTEGER, fid INTEGER, distance DOUBLE)", xname); free (xname); free (vtable); if (sqlite3_declare_vtab (db, buf) != SQLITE_OK) { sqlite3_free (buf); *pzErr = sqlite3_mprintf ("[VirtualKNN module] CREATE VIRTUAL: invalid SQL statement \"%s\"", buf); return SQLITE_ERROR; } sqlite3_free (buf); *ppVTab = (sqlite3_vtab *) p_vt; return SQLITE_OK; }
std::string Auxiliary::dequotedSql(const char * value) { return Auxiliary::toString(gaiaDequotedSql(value), "Failed dequoting SQL."); }
static int vxpath_create (sqlite3 * db, void *pAux, int argc, const char *const *argv, sqlite3_vtab ** ppVTab, char **pzErr) { /* creates the virtual table for XPath */ VirtualXPathPtr p_vt; char *vtable = NULL; char *table = NULL; char *column = NULL; char *xname; char *sql; int okTable = 0; int okCol = 0; if (argc == 5) { vtable = gaiaDequotedSql ((char *) argv[2]); table = gaiaDequotedSql ((char *) argv[3]); column = gaiaDequotedSql ((char *) argv[4]); } else { *pzErr = sqlite3_mprintf ("[VirtualXPath module] CREATE VIRTUAL: illegal arg list {void}\n"); return SQLITE_ERROR; } vxpath_check (db, table, column, &okTable, &okCol); if (!okTable || !okCol) goto illegal; xname = gaiaDoubleQuotedSql (vtable); sql = sqlite3_mprintf ("CREATE TABLE \"%s\" (pkid INTEGER, sub INTEGER, " "parent TEXT, node TEXT, attribute TEXT, " "value TEXT, xpath_expr TEXT)", xname); free (xname); if (sqlite3_declare_vtab (db, sql) != SQLITE_OK) { sqlite3_free (sql); *pzErr = sqlite3_mprintf ("[VirtualXPath module] CREATE VIRTUAL: invalid SQL statement \"%s\"", sql); goto error; } sqlite3_free (sql); p_vt = (VirtualXPathPtr) sqlite3_malloc (sizeof (VirtualXPath)); if (!p_vt) return SQLITE_NOMEM; p_vt->db = db; p_vt->p_cache = pAux; if (p_vt->p_cache == NULL) spatialite_e ("VirtualXPath WARNING - no XML cache is available !!!\n"); p_vt->nRef = 0; p_vt->zErrMsg = NULL; p_vt->table = table; p_vt->column = column; *ppVTab = (sqlite3_vtab *) p_vt; free (vtable); return SQLITE_OK; illegal: /* something is going the wrong way */ if (!okTable == 0) *pzErr = sqlite3_mprintf ("[VirtualXPath module] table \"%s\" doesn't exists\n", table); else if (!okCol) *pzErr = sqlite3_mprintf ("[VirtualXPath module] table \"%s\" exists, but has no \"%s\" column\n", table, column); error: return SQLITE_ERROR; }
static int vbbox_create (sqlite3 * db, void *pAux, int argc, const char *const *argv, sqlite3_vtab ** ppVTab, char **pzErr) { /* creates the virtual table connected to some BoundingBox table */ char *vtable = NULL; char *table = NULL; char *col_minx = NULL; char *col_miny = NULL; char *col_maxx = NULL; char *col_maxy = NULL; char *col_srid = NULL; char *x_force_wgs84 = NULL; int ret; int i; int i2; int len; int n_rows; int n_columns; const char *col_name; const char *col_type; int force_wgs84; char **results; char *sql; char *xname; gaiaOutBuffer sql_statement; VirtualBBoxPtr p_vt = NULL; if (pAux) pAux = pAux; /* unused arg warning suppression */ gaiaOutBufferInitialize (&sql_statement); /* checking for table_name */ if (argc >= 10) { vtable = gaiaDequotedSql ((char *) argv[2]); table = gaiaDequotedSql ((char *) argv[3]); col_minx = gaiaDequotedSql ((char *) argv[4]); col_miny = gaiaDequotedSql ((char *) argv[5]); col_maxx = gaiaDequotedSql ((char *) argv[6]); col_maxy = gaiaDequotedSql ((char *) argv[7]); col_srid = gaiaDequotedSql ((char *) argv[8]); x_force_wgs84 = gaiaDequotedSql ((char *) argv[9]); } else { *pzErr = sqlite3_mprintf ("[VirtualBBox module] CREATE VIRTUAL: illegal arg list {table_name, col_minx, col_miny, col_maxx, col_maxy, srid, longlat=1|0, columns}\n"); goto error; } if (strcmp (x_force_wgs84, "0") == 0) force_wgs84 = 0; else if (strcmp (x_force_wgs84, "1") == 0) force_wgs84 = 1; else { *pzErr = sqlite3_mprintf ("[VirtualBBox module] CREATE VIRTUAL: illegal arg list {table_name, col_minx, col_miny, col_maxx, col_maxy, srid, longlat=1|0, columns}\n"); goto error; } /* retrieving the base table columns */ xname = gaiaDoubleQuotedSql (table); sql = sqlite3_mprintf ("PRAGMA table_info(\"%s\")", xname); free (xname); ret = sqlite3_get_table (db, sql, &results, &n_rows, &n_columns, NULL); sqlite3_free (sql); if (ret != SQLITE_OK) goto illegal; if (n_rows >= 1) { p_vt = (VirtualBBoxPtr) sqlite3_malloc (sizeof (VirtualBBox)); if (!p_vt) return SQLITE_NOMEM; p_vt->db = db; p_vt->p_cache = pAux; p_vt->nRef = 0; p_vt->zErrMsg = NULL; len = strlen (table); p_vt->table = sqlite3_malloc (len + 1); strcpy (p_vt->table, table); p_vt->nColumns = n_rows; p_vt->Column = sqlite3_malloc (sizeof (char *) * n_rows); p_vt->Type = sqlite3_malloc (sizeof (char *) * n_rows); p_vt->Visible = sqlite3_malloc (sizeof (char *) * n_rows); memset (p_vt->Visible, 'N', n_rows); p_vt->Value = sqlite3_malloc (sizeof (SqliteValuePtr) * n_rows); p_vt->Srid = atoi (col_srid); p_vt->ForceWGS84 = force_wgs84; #ifndef OMIT_PROJ /* including PROJ.4 */ if (p_vt->ForceWGS84) spatialite_e ("VirtualBBOX WARNING - WGS84 is requested, but PROJ4 support is currently disabled\n"); #endif /* end including PROJ.4 */ p_vt->ColSrid = NULL; p_vt->MinX = NULL; p_vt->MinY = NULL; p_vt->MaxX = NULL; p_vt->MaxY = NULL; p_vt->BBoxGeom = NULL; for (i = 0; i < n_rows; i++) { *(p_vt->Column + i) = NULL; *(p_vt->Type + i) = NULL; *(p_vt->Value + i) = value_alloc (); } for (i = 1; i <= n_rows; i++) { col_name = results[(i * n_columns) + 1]; col_type = results[(i * n_columns) + 2]; len = strlen (col_name); if (strcasecmp (col_name, col_minx) == 0) { p_vt->MinX = sqlite3_malloc (len + 1); strcpy (p_vt->MinX, col_name); } if (strcasecmp (col_name, col_miny) == 0) { p_vt->MinY = sqlite3_malloc (len + 1); strcpy (p_vt->MinY, col_name); } if (strcasecmp (col_name, col_maxx) == 0) { p_vt->MaxX = sqlite3_malloc (len + 1); strcpy (p_vt->MaxX, col_name); } if (strcasecmp (col_name, col_maxy) == 0) { p_vt->MaxY = sqlite3_malloc (len + 1); strcpy (p_vt->MaxY, col_name); } if (strcasecmp (col_name, col_srid) == 0) { p_vt->ColSrid = sqlite3_malloc (len + 1); strcpy (p_vt->ColSrid, col_name); } *(p_vt->Column + (i - 1)) = sqlite3_malloc (len + 1); strcpy (*(p_vt->Column + (i - 1)), col_name); len = strlen (col_type); *(p_vt->Type + (i - 1)) = sqlite3_malloc (len + 1); strcpy (*(p_vt->Type + (i - 1)), col_type); for (i2 = 10; i2 < argc; i2++) { char *extra_col = gaiaDequotedSql ((char *) argv[i2]); if (strcasecmp (extra_col, col_name) == 0) *(p_vt->Visible + (i - 1)) = 'Y'; free (extra_col); } } sqlite3_free_table (results); } else { sqlite3_free_table (results); goto illegal; } if (p_vt->MinX == NULL || p_vt->MinY == NULL || p_vt->MaxX == NULL || p_vt->MaxY == NULL) goto illegal; /* preparing the COLUMNs for this VIRTUAL TABLE */ xname = gaiaDoubleQuotedSql (vtable); sql = sqlite3_mprintf ("CREATE TABLE \"%s\" (Geometry Polygon", xname); free (xname); gaiaAppendToOutBuffer (&sql_statement, sql); sqlite3_free (sql); for (i = 0; i < p_vt->nColumns; i++) { if (*(p_vt->Visible + i) != 'Y') continue; xname = gaiaDoubleQuotedSql (*(p_vt->Column + i)); sql = sqlite3_mprintf (", \"%s\" %s", xname, *(p_vt->Type + i)); free (xname); gaiaAppendToOutBuffer (&sql_statement, sql); sqlite3_free (sql); } gaiaAppendToOutBuffer (&sql_statement, ")"); if (sql_statement.Error == 0 && sql_statement.Buffer != NULL) { if (sqlite3_declare_vtab (db, sql_statement.Buffer) != SQLITE_OK) { *pzErr = sqlite3_mprintf ("[VirtualBBox module] CREATE VIRTUAL: invalid SQL statement \"%s\"", sql); goto error; } gaiaOutBufferReset (&sql_statement); } else goto error; *ppVTab = (sqlite3_vtab *) p_vt; free (vtable); free (table); free (col_minx); free (col_miny); free (col_maxx); free (col_maxy); free (col_srid); free (x_force_wgs84); return SQLITE_OK; illegal: /* something is going the wrong way */ gaiaOutBufferReset (&sql_statement); if (p_vt) free_table (p_vt); *pzErr = sqlite3_mprintf ("[VirtualBBox module] '%s' isn't a valid BoundingBox table\n", table); error: if (vtable) free (vtable); if (table) free (table); if (col_minx) free (col_minx); if (col_miny) free (col_miny); if (col_maxx) free (col_maxx); if (col_maxy) free (col_maxy); if (col_srid) free (col_srid); if (x_force_wgs84) free (x_force_wgs84); gaiaOutBufferReset (&sql_statement); return SQLITE_ERROR; }