int main (int argc, char *argv[]) { int result = 0; int ret; sqlite3 *db_handle; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ /* opening and initializing the "memory" test DB */ ret = sqlite3_open_v2 ("symbolizers.sqlite", &db_handle, SQLITE_OPEN_READONLY, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_open_v2() error: %s\n", sqlite3_errmsg (db_handle)); return -1; } spatialite_init_ex (db_handle, cache, 0); /* tests */ ret = -100; if (!test_symbolizer (db_handle, "points", "label_1", &ret)) return ret; ret = -200; if (!test_symbolizer (db_handle, "lines", "label_2", &ret)) return ret; ret = -300; if (!test_symbolizer (db_handle, "points", "label_3", &ret)) return ret; ret = -400; if (!test_style (db_handle, "lines", "text_style", &ret)) return ret; /* closing the DB */ sqlite3_close (db_handle); spatialite_shutdown (); return result; }
int main (int argc, char *argv[]) { int ret; if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ fprintf (stderr, "********* testing in current mode\n"); ret = do_test (0); if (ret != 0) return ret; fprintf (stderr, "********* testing in legacy mode\n"); ret = do_test (1); if (ret != 0) return ret; spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { int ret; sqlite3 *handle; char *err_msg = NULL; char **results; int rows; int columns; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -2; } ret = sqlite3_exec (handle, "CREATE TABLE Point_Test (Name TEXT, Description TEXT)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "CREATE TABLE error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -3; } ret = sqlite3_get_table (handle, "SELECT AddGeometryColumn(26, 'geomZ', 4326, 'POINT', 'XYZ', 0)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error: %s\n", err_msg); sqlite3_free (err_msg); return -4; } if ((rows != 1) || (columns != 1)) { fprintf (stderr, "Unexpected result AddGeometryColumn int arg1 bad result: %i/%i.\n", rows, columns); return -5; } if (strcmp (results[1], "0") != 0) { fprintf (stderr, "Unexpected result: AddGeometryColumn with non-text arg1 passed: %s.\n", results[1]); return -6; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT AddGeometryColumn('Point_Test', 8, 4326, 'POINT', 'XYZ', 0)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error: %s\n", err_msg); sqlite3_free (err_msg); return -7; } if ((rows != 1) || (columns != 1)) { fprintf (stderr, "Unexpected result AddGeometryColumn int arg2 bad result: %i/%i.\n", rows, columns); return -8; } if (strcmp (results[1], "0") != 0) { fprintf (stderr, "Unexpected result: AddGeometryColumn with non-text arg2 passed: %s.\n", results[1]); return -9; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT AddGeometryColumn('Point_Test', 'geomZ', 'sometext', 'POINT', 'XYZ', 0)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error: %s\n", err_msg); sqlite3_free (err_msg); return -10; } if ((rows != 1) || (columns != 1)) { fprintf (stderr, "Unexpected result AddGeometryColumn text arg3 bad result: %i/%i.\n", rows, columns); return -11; } if (strcmp (results[1], "0") != 0) { fprintf (stderr, "Unexpected result: AddGeometryColumn with non-int arg3 passed: %s.\n", results[1]); return -12; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT AddGeometryColumn('Point_Test', 'geomZ', 4326, 'POINT', 'XYZ', 0)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error: %s\n", err_msg); sqlite3_free (err_msg); return -13; } if ((rows != 1) || (columns != 1)) { fprintf (stderr, "Unexpected result AddGeometryColumn bad result: %i/%i.\n", rows, columns); return -14; } if (strcmp (results[1], "1") != 0) { fprintf (stderr, "Unexpected error: AddGeometryColumn with good args failed: %s.\n", results[1]); return -15; } sqlite3_free_table (results); ret = sqlite3_exec (handle, "INSERT INTO Point_Test (Name, Description, geomZ) VALUES ('Point 1', 'Some point', GeomFromText('POINTZ(136 -33 365)', 4326))", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "INSERT POINT XYZ error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -16; } ret = sqlite3_get_table (handle, "SELECT DiscardGeometryColumn('Point_Test', 'geomZ')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error: %s\n", err_msg); sqlite3_free (err_msg); return -17; } if ((rows != 1) || (columns != 1)) { fprintf (stderr, "Unexpected result DiscardGeometryColumn bad result: %i/%i.\n", rows, columns); return -18; } if (strcmp (results[1], "1") != 0) { fprintf (stderr, "Unexpected error: DiscardGeometryColumn failed: %s.\n", results[1]); return -19; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT RecoverGeometryColumn('Point_Test', 'geomZ', 4326, 'POINT', 'XYZ')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error: %s\n", err_msg); sqlite3_free (err_msg); return -20; } if ((rows != 1) || (columns != 1)) { fprintf (stderr, "Unexpected result RecoverGeometryColumn bad result: %i/%i.\n", rows, columns); return -21; } if (strcmp (results[1], "1") != 0) { fprintf (stderr, "Unexpected error: RecoverGeometryColumn failed: %s.\n", results[1]); return -22; } sqlite3_free_table (results); ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -23; } spatialite_cleanup_ex (cache); spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { int ret; sqlite3 *handle; sqlite3_stmt *stmt; gaiaGeomCollPtr geom; char sql[256]; int i; int ic; char **results; int n_rows; int n_columns; char *err_msg = NULL; int len; char *table_name; char **p_geotables = NULL; int n_geotables = 0; int row_no; const void *blob; int blob_size; int geom_type; double measure; void *cache; if (argc != 2) { fprintf (stderr, "usage: %s test_db_path\n", argv[0]); return -1; } /* trying to connect the test DB: - this demo was designed in order to connect the standard TEST-2.3.SQLITE sample DB - but you can try to use any SQLite/SpatiaLite DB at your will Please notice: we'll establish a READ ONLY connection */ ret = sqlite3_open_v2 (argv[1], &handle, SQLITE_OPEN_READONLY, NULL); if (ret != SQLITE_OK) { printf ("cannot open '%s': %s\n", argv[1], sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } /* VERY IMPORTANT: you must initialize the SpatiaLite extension [and related] BEFORE attempting to perform any other SQLite call ========================================================== Please note: starting since 4.1.0 this is completely canged: - a separate memory block (internal cache) is required by each single connection - allocating/freeing this block falls under the responsibility of the program handling the connection - in multithreaded programs a connection can never be share by different threads; the internal-cache block must be allocated by the same thread holding the connection */ cache = spatialite_alloc_connection (); spatialite_init_ex (handle, cache, 0); /* showing the SQLite version */ printf ("SQLite version: %s\n", sqlite3_libversion ()); /* showing the SpatiaLite version */ printf ("SpatiaLite version: %s\n", spatialite_version ()); printf ("\n\n"); /* SQL query #1 we'll retrieve GEOMETRY tables from Spatial Metadata we are assuming this query will return only few rows, so this time we'll use the sqlite3_get_table() interface this interface is very simple to use the result set is returned as a rectangular array [rows/columns] allocated in a temporary memory storage so, this interface is well suited for small sized result sets, but performs badly when accessing a large sized resul set as a side effect, each column value is returned as text, and isn't possible at all to retrieve true column types (INTEGER, FLOAT ...) */ strcpy (sql, "SELECT DISTINCT f_table_name FROM geometry_columns ORDER BY 1"); ret = sqlite3_get_table (handle, sql, &results, &n_rows, &n_columns, &err_msg); if (ret != SQLITE_OK) { /* some error occurred */ printf ("query#1 SQL error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } if (n_rows > 1) { /* first row always contains column names and is meaningless in this context */ n_geotables = n_rows; /* allocating a dynamic pointer array to store geotable names */ p_geotables = malloc (sizeof (char *) * n_geotables); for (i = 1; i <= n_rows; i++) { /* now we'll fetch one row at each time [and we have only one column to fetch] this one is is a simplified demo; but when writing a real application you always must check for NULL values !!!! */ table_name = results[(i * n_columns) + 0]; /* and we'll store each geotable name into the dynamic pointer array */ len = strlen (table_name); p_geotables[i - 1] = malloc (len + 1); strcpy (p_geotables[i - 1], table_name); } /* we can now free the table results */ sqlite3_free_table (results); } for (i = 0; i < n_geotables; i++) { /* now we'll scan each geotable we've found in Spatial Metadata */ printf ("========= table '%s' ========================\n", p_geotables[i]); /* SQL query #2 we'll retrieve any column from the current geotable we are assuming this query will return lots of rows, so we have to use sqlite3_prepare_v2() interface this interface is a more complex one, but is well suited in order to access huge sized result sets and true value type control is supported */ sprintf (sql, "SELECT * FROM %s", p_geotables[i]); ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL); if (ret != SQLITE_OK) { /* some error occurred */ printf ("query#2 SQL error: %s\n", sqlite3_errmsg (handle)); goto abort; } /* the sqlite3_prepare_v2() call simply parses the SQL statement, checking for syntax validity, allocating internal structs etc but no result set row is really yet available */ /* we'll now save the #columns within the result set */ n_columns = sqlite3_column_count (stmt); row_no = 0; while (1) { /* this is an infinite loop, intended to fetch any row */ /* we are now trying to fetch the next available row */ ret = sqlite3_step (stmt); if (ret == SQLITE_DONE) { /* there are no more rows to fetch - we can stop looping */ break; } if (ret == SQLITE_ROW) { /* ok, we've just fetched a valid row to process */ row_no++; printf ("row #%d\n", row_no); for (ic = 0; ic < n_columns; ic++) { /* and now we'll fetch column values for each column we'll then get: - the column name - a column value, that can be of type: SQLITE_NULL, SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT or SQLITE_BLOB, according to internal DB storage type */ printf ("\t%-10s = ", sqlite3_column_name (stmt, ic)); switch (sqlite3_column_type (stmt, ic)) { case SQLITE_NULL: printf ("NULL"); break; case SQLITE_INTEGER: printf ("%d", sqlite3_column_int (stmt, ic)); break; case SQLITE_FLOAT: printf ("%1.4f", sqlite3_column_double (stmt, ic)); break; case SQLITE_TEXT: printf ("'%s'", sqlite3_column_text (stmt, ic)); break; case SQLITE_BLOB: blob = sqlite3_column_blob (stmt, ic); blob_size = sqlite3_column_bytes (stmt, ic); /* checking if this BLOB actually is a GEOMETRY */ geom = gaiaFromSpatiaLiteBlobWkb (blob, blob_size); if (!geom) { /* for sure this one is not a GEOMETRY */ printf ("BLOB [%d bytes]", blob_size); } else { geom_type = gaiaGeometryType (geom); if (geom_type == GAIA_UNKNOWN) printf ("EMPTY or NULL GEOMETRY"); else { char *geom_name; if (geom_type == GAIA_POINT) geom_name = "POINT"; if (geom_type == GAIA_LINESTRING) geom_name = "LINESTRING"; if (geom_type == GAIA_POLYGON) geom_name = "POLYGON"; if (geom_type == GAIA_MULTIPOINT) geom_name = "MULTIPOINT"; if (geom_type == GAIA_MULTILINESTRING) geom_name = "MULTILINESTRING"; if (geom_type == GAIA_MULTIPOLYGON) geom_name = "MULTIPOLYGON"; if (geom_type == GAIA_GEOMETRYCOLLECTION) geom_name = "GEOMETRYCOLLECTION"; printf ("%s SRID=%d", geom_name, geom->Srid); if (geom_type == GAIA_LINESTRING || geom_type == GAIA_MULTILINESTRING) { #ifndef OMIT_GEOS /* GEOS is required */ gaiaGeomCollLength (geom, &measure); printf (" length=%1.2f", measure); #else printf (" length=?? [no GEOS support available]"); #endif /* GEOS enabled/disabled */ } if (geom_type == GAIA_POLYGON || geom_type == GAIA_MULTIPOLYGON) { #ifndef OMIT_GEOS /* GEOS is required */ gaiaGeomCollArea (geom, &measure); printf (" area=%1.2f", measure); #else printf ("area=?? [no GEOS support available]"); #endif /* GEOS enabled/disabled */ } } /* we have now to free the GEOMETRY */ gaiaFreeGeomColl (geom); } break; }; printf ("\n"); } if (row_no >= 5) { /* we'll exit the loop after the first 5 rows - this is only a demo :-) */ break; } } else { /* some unexpected error occurred */ printf ("sqlite3_step() error: %s\n", sqlite3_errmsg (handle)); sqlite3_finalize (stmt); goto abort; } } /* we have now to finalize the query [memory cleanup] */ sqlite3_finalize (stmt); printf ("\n\n"); } /* disconnecting the test DB */ ret = sqlite3_close (handle); if (ret != SQLITE_OK) { printf ("close() error: %s\n", sqlite3_errmsg (handle)); return -1; } /* freeing the internal-cache memory block */ spatialite_cleanup_ex (cache); printf ("\n\nsample successfully terminated\n"); /* we have to free the dynamic pointer array used to store geotable names */ for (i = 0; i < n_geotables; i++) { /* freeing each tablename */ free (p_geotables[i]); } free (p_geotables); spatialite_shutdown(); return 0; abort: sqlite3_close (handle); /* freeing the internal-cache memory block */ spatialite_cleanup_ex (cache); if (p_geotables) { /* we have to free the dynamic pointer array used to store geotable names */ for (i = 0; i < n_geotables; i++) { /* freeing each tablename */ free (p_geotables[i]); } free (p_geotables); } spatialite_shutdown(); return -1; }
int main (int argc, char *argv[]) { sqlite3 *db_handle; int ret; const char *sql; void *cache = NULL; char *sql_err = NULL; if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ do_unlink_all (); /* directly testing GPKG */ ret = system ("cp ./gpkg_test.gpkg copy-gpkg_test.gpkg"); if (ret != 0) { fprintf (stderr, "cannot copy gpkg_test.gpkg database\n"); return -1; } cache = spatialite_alloc_connection (); ret = sqlite3_open_v2 ("./copy-gpkg_test.gpkg", &db_handle, SQLITE_OPEN_READWRITE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open '%s': %s\n", "copy-gpkg_test.gpkg", sqlite3_errmsg (db_handle)); do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } spatialite_init_ex (db_handle, cache, 0); if (!test_table (db_handle, "pt2d")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_table (db_handle, "ln3dz")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_table (db_handle, "pg2dm")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_table (db_handle, "mpt3dzm")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_table (db_handle, "mln2dm")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_table (db_handle, "mpg3dz")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_table (db_handle, "gc3dz")) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } /* activating Auto GPKG Wrapping */ sql = "SELECT AutoGPKGStart()"; ret = sqlite3_exec (db_handle, sql, NULL, NULL, &sql_err); if (ret != SQLITE_OK) { fprintf (stderr, "AutoGPKGStart error: %s\n", sql_err); sqlite3_free (sql_err); do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } /* testing the Virtual Tables */ if (!test_vtable (db_handle, "pt2d", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "ln3dz", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "pg2dm", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "mpt3dzm", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "mln2dm", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "mpg3dz", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "gc3dz", 0)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable (db_handle, "test_pk", 1)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } if (!test_vtable_out (db_handle)) { do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } /* quitting Auto GPKG Wrapping */ sql = "SELECT AutoGPKGStop()"; ret = sqlite3_exec (db_handle, sql, NULL, NULL, &sql_err); if (ret != SQLITE_OK) { fprintf (stderr, "AutoGPKGStop error: %s\n", sql_err); sqlite3_free (sql_err); do_unlink_all (); sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); return -1; } sqlite3_close (db_handle); spatialite_cleanup_ex (cache); spatialite_shutdown (); do_unlink_all (); return 0; }
int main (int argc, char *argv[]) { int ret; sqlite3 *handle; sqlite3_stmt *stmt; char sql[256]; char *err_msg = NULL; double x; double y; int pk; int ix; int iy; gaiaGeomCollPtr geo = NULL; unsigned char *blob; int blob_size; int i; char **results; int n_rows; int n_columns; char *count; clock_t t0; clock_t t1; void *cache; if (argc != 2) { fprintf (stderr, "usage: %s test_db_path\n", argv[0]); return -1; } /* trying to connect the test DB: - this demo is intended to create a new, empty database */ ret = sqlite3_open_v2 (argv[1], &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { printf ("cannot open '%s': %s\n", argv[1], sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } cache = spatialite_alloc_connection (); spatialite_init_ex (handle, cache, 0); /* showing the SQLite version */ printf ("SQLite version: %s\n", sqlite3_libversion ()); /* showing the SpatiaLite version */ printf ("SpatiaLite version: %s\n", spatialite_version ()); printf ("\n\n"); /* we are supposing this one is an empty database, so we have to create the Spatial Metadata */ strcpy (sql, "SELECT InitSpatialMetadata(1)"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } /* now we can create the test table for simplicity we'll define only one column, the primary key */ strcpy (sql, "CREATE TABLE test ("); strcat (sql, "PK INTEGER NOT NULL PRIMARY KEY)"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("CREATE TABLE 'test' error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } /* ... we'll add a Geometry column of POINT type to the test table */ strcpy (sql, "SELECT AddGeometryColumn('test', 'geom', 3003, 'POINT', 2)"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("AddGeometryColumn() error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } /* and finally we'll enable this geo-column to have a Spatial Index based on R*Tree */ strcpy (sql, "SELECT CreateSpatialIndex('test', 'geom')"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("CreateSpatialIndex() error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } printf ("\nnow we are going to insert 1 million POINTs; wait, please ...\n\n"); t0 = clock (); /* beginning a transaction *** this step is absolutely critical *** the SQLite engine is a TRANSACTIONAL one the whole batch of INSERTs has to be performed as an unique transaction, otherwise performance will be surely very poor */ strcpy (sql, "BEGIN"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("BEGIN error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } /* preparing to populate the test table we'll use a Prepared Statement we can reuse in order to insert each row */ strcpy (sql, "INSERT INTO test (pk, geom) VALUES (?, ?)"); ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL); if (ret != SQLITE_OK) { /* an error occurred */ printf ("INSERT SQL error: %s\n", sqlite3_errmsg (handle)); goto abort; } pk = 0; for (ix = 0; ix < 1000; ix++) { x = 1000000.0 + (ix * 10.0); for (iy = 0; iy < 1000; iy++) { /* this double loop will insert 1 million rows into the the test table */ y = 4000000.0 + (iy * 10.0); pk++; if ((pk % 25000) == 0) { t1 = clock (); printf ("insert row: %d\t\t[elapsed time: %1.3f]\n", pk, (double) (t1 - t0) / CLOCKS_PER_SEC); } /* preparing the geometry to insert */ geo = gaiaAllocGeomColl (); geo->Srid = 3003; gaiaAddPointToGeomColl (geo, x, y); /* transforming this geometry into the SpatiaLite BLOB format */ gaiaToSpatiaLiteBlobWkb (geo, &blob, &blob_size); /* we can now destroy the geometry object */ gaiaFreeGeomColl (geo); /* resetting Prepared Statement and bindings */ sqlite3_reset (stmt); sqlite3_clear_bindings (stmt); /* binding parameters to Prepared Statement */ sqlite3_bind_int64 (stmt, 1, pk); sqlite3_bind_blob (stmt, 2, blob, blob_size, free); /* performing actual row insert */ ret = sqlite3_step (stmt); if (ret == SQLITE_DONE || ret == SQLITE_ROW) ; else { /* an unexpected error occurred */ printf ("sqlite3_step() error: %s\n", sqlite3_errmsg (handle)); sqlite3_finalize (stmt); goto abort; } } } /* we have now to finalize the query [memory cleanup] */ sqlite3_finalize (stmt); /* committing the transaction *** this step is absolutely critical *** if we don't confirm the still pending transaction, any update will be lost */ strcpy (sql, "COMMIT"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("COMMIT error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } /* now we'll optimize the table */ strcpy (sql, "ANALYZE test"); ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("ANALYZE error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } for (ix = 0; ix < 3; ix++) { printf ("\nperforming test#%d - not using Spatial Index\n", ix); /* now we'll perform the spatial query WITHOUT using the Spatial Index we'll loop 3 times in order to avoid buffering-caching side effects */ strcpy (sql, "SELECT Count(*) FROM test "); strcat (sql, "WHERE MbrWithin(geom, BuildMbr("); strcat (sql, "1000400.5, 4000400.5, "); strcat (sql, "1000450.5, 4000450.5))"); t0 = clock (); ret = sqlite3_get_table (handle, sql, &results, &n_rows, &n_columns, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("NoSpatialIndex SQL error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } count = ""; for (i = 1; i <= n_rows; i++) { count = results[(i * n_columns) + 0]; } t1 = clock (); printf ("Count(*) = %d\t\t[elapsed time: %1.4f]\n", atoi (count), (double) (t1 - t0) / CLOCKS_PER_SEC); /* we can now free the table results */ sqlite3_free_table (results); } for (ix = 0; ix < 3; ix++) { printf ("\nperforming test#%d - using the R*Tree Spatial Index\n", ix); /* now we'll perform the spatial query USING the R*Tree Spatial Index we'll loop 3 times in order to avoid buffering-caching side effects */ strcpy (sql, "SELECT Count(*) FROM test "); strcat (sql, "WHERE MbrWithin(geom, BuildMbr("); strcat (sql, "1000400.5, 4000400.5, "); strcat (sql, "1000450.5, 4000450.5)) AND ROWID IN ("); strcat (sql, "SELECT pkid FROM idx_test_geom WHERE "); strcat (sql, "xmin > 1000400.5 AND "); strcat (sql, "xmax < 1000450.5 AND "); strcat (sql, "ymin > 4000400.5 AND "); strcat (sql, "ymax < 4000450.5)"); /* YES, this query is a very unhappy one the idea is simply to simulate exactly the same conditions as above */ t0 = clock (); ret = sqlite3_get_table (handle, sql, &results, &n_rows, &n_columns, &err_msg); if (ret != SQLITE_OK) { /* an error occurred */ printf ("SpatialIndex SQL error: %s\n", err_msg); sqlite3_free (err_msg); goto abort; } count = ""; for (i = 1; i <= n_rows; i++) { count = results[(i * n_columns) + 0]; } t1 = clock (); printf ("Count(*) = %d\t\t[elapsed time: %1.4f]\n", atoi (count), (double) (t1 - t0) / CLOCKS_PER_SEC); /* we can now free the table results */ sqlite3_free_table (results); } /* disconnecting the test DB */ ret = sqlite3_close (handle); if (ret != SQLITE_OK) { printf ("close() error: %s\n", sqlite3_errmsg (handle)); return -1; } spatialite_cleanup_ex (cache); printf ("\n\nsample successfully terminated\n"); return 0; abort: sqlite3_close (handle); spatialite_cleanup_ex (cache); spatialite_shutdown(); return -1; }
int main (int argc, char *argv[]) { int ret; sqlite3 *handle; char *err_msg = NULL; char **results; int rows; int columns; unsigned char *blob; int blob_len; char *hexBlob; unsigned char *xml; int len; char *sql; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata(1, 'WGS84')", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Unexpected InitSpatialMetadata result: %i, (%s)\n", ret, err_msg); sqlite3_free (err_msg); return -2; } #ifdef ENABLE_LIBXML2 /* only if LIBXML2 is supported */ ret = sqlite3_get_table (handle, "SELECT CreateStylingTables(1)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error CreateStylingTables: %s\n", err_msg); sqlite3_free (err_msg); return -3; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -4; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #0 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -5; } sqlite3_free_table (results); blob = load_blob ("empty.png", &blob_len); if (blob == NULL) return -6; hexBlob = build_hex_blob (blob, blob_len); free (blob); if (hexBlob == NULL) return -7; sql = sqlite3_mprintf ("SELECT RegisterExternalGraphic('url-A', x%Q)", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterExternalGraphic #1: %s\n", err_msg); sqlite3_free (err_msg); return -8; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -9; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #1 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -10; } sqlite3_free_table (results); sql = sqlite3_mprintf ("SELECT RegisterExternalGraphic('url-A', x%Q, 'title', 'abstract', 'file_name')", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); free (hexBlob); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterExternalGraphic #2: %s\n", err_msg); sqlite3_free (err_msg); return -11; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -12; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #2 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -13; } sqlite3_free_table (results); xml = load_xml ("thunderstorm_mild.svg", &len); if (xml == NULL) return -14; gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL); free (xml); if (blob == NULL) { fprintf (stderr, "this is not a well-formed XML !!!\n"); return -15; } hexBlob = build_hex_blob (blob, blob_len); free (blob); if (hexBlob == NULL) return -16; sql = sqlite3_mprintf ("SELECT RegisterExternalGraphic('url-B', x%Q, 'title', 'abstract', 'file_name')", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterExternalGraphic #3: %s\n", err_msg); sqlite3_free (err_msg); return -17; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -18; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #3 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -19; } sqlite3_free_table (results); sql = sqlite3_mprintf ("SELECT RegisterExternalGraphic('url-B', x%Q)", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); free (hexBlob); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterExternalGraphic #4: %s\n", err_msg); sqlite3_free (err_msg); return -20; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -21; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #4 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -22; } sqlite3_free_table (results); ret = sqlite3_exec (handle, "CREATE TABLE table1 (id INTEGER PRIMARY KEY AUTOINCREMENT)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error Create Table table1: %s\n", err_msg); sqlite3_free (err_msg); return -23; } ret = sqlite3_get_table (handle, "SELECT AddGeometryColumn('table1', 'geom', 4326, 'POINT', 'XY')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error AddGeometryColumn: %s\n", err_msg); sqlite3_free (err_msg); return -24; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -25; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #5 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -26; } sqlite3_free_table (results); xml = load_xml ("stazioni_se.xml", &len); if (xml == NULL) return -27; gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL); free (xml); if (blob == NULL) { fprintf (stderr, "this is not a well-formed XML !!!\n"); return -28; } hexBlob = build_hex_blob (blob, blob_len); free (blob); if (hexBlob == NULL) return -29; sql = sqlite3_mprintf ("SELECT RegisterVectorStyledLayer('table1', 'geom', x%Q)", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterVectorStyledLayer #6: %s\n", err_msg); sqlite3_free (err_msg); return -30; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -31; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #6 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -32; } sqlite3_free_table (results); sql = sqlite3_mprintf ("SELECT RegisterVectorStyledLayer('table1', 'geom', 0, x%Q)", hexBlob); free (hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterVectorStyledLayer #7: %s\n", err_msg); sqlite3_free (err_msg); return -33; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -34; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #7 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -35; } sqlite3_free_table (results); xml = load_xml ("raster_se.xml", &len); if (xml == NULL) return -36; gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL); free (xml); if (blob == NULL) { fprintf (stderr, "this is not a well-formed XML !!!\n"); return -37; } hexBlob = build_hex_blob (blob, blob_len); free (blob); if (hexBlob == NULL) return -38; sql = sqlite3_mprintf ("SELECT RegisterRasterStyledLayer('srtm', x%Q)", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterRasterStyledLayer #8: %s\n", err_msg); sqlite3_free (err_msg); return -39; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -40; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #8 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -41; } sqlite3_free_table (results); sql = sqlite3_mprintf ("SELECT RegisterRasterStyledLayer('srtm', 0, x%Q)", hexBlob); free (hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterRasterStyledLayer #9: %s\n", err_msg); sqlite3_free (err_msg); return -42; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -43; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #9 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -44; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT RegisterStyledGroup('group', 'srtm')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterStyledGroup #10: %s\n", err_msg); sqlite3_free (err_msg); return -45; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -46; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #10 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -47; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT RegisterStyledGroup('group', 'table1', 'geom')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterStyledGroup #11: %s\n", err_msg); sqlite3_free (err_msg); return -48; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -49; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #12 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -50; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT RegisterStyledGroup('group', 'srtm', 4)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterStyledGroup #13: %s\n", err_msg); sqlite3_free (err_msg); return -51; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -52; } if (strcmp (results[1 * columns + 0], "0") != 0) { fprintf (stderr, "Unexpected #13 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -53; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT RegisterStyledGroup('group', 'table1', 'geom', 1)", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterStyledGroup #14: %s\n", err_msg); sqlite3_free (err_msg); return -54; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -55; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #14 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -56; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT SetStyledGroupInfos('group', 'title', 'abstract')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error SetStyledGroupInfos #15: %s\n", err_msg); sqlite3_free (err_msg); return -57; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -58; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #15 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -59; } sqlite3_free_table (results); ret = sqlite3_get_table (handle, "SELECT SetStyledGroupInfos('group-bis', 'title', 'abstract')", &results, &rows, &columns, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "Error SetStyledGroupInfos #16: %s\n", err_msg); sqlite3_free (err_msg); return 60; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -61; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #16 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -62; } sqlite3_free_table (results); xml = load_xml ("sld_sample.xml", &len); if (xml == NULL) return -63; gaiaXmlToBlob (cache, xml, len, 1, NULL, &blob, &blob_len, NULL, NULL); free (xml); if (blob == NULL) { fprintf (stderr, "this is not a well-formed XML !!!\n"); return -64; } hexBlob = build_hex_blob (blob, blob_len); free (blob); if (hexBlob == NULL) return -65; sql = sqlite3_mprintf ("SELECT RegisterGroupStyle('group', x%Q)", hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterGroupStyle #1: %s\n", err_msg); sqlite3_free (err_msg); return -66; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -67; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #1 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -68; } sqlite3_free_table (results); sql = sqlite3_mprintf ("SELECT RegisterGroupStyle('group', 0, x%Q)", hexBlob); free (hexBlob); ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg); sqlite3_free (sql); if (ret != SQLITE_OK) { fprintf (stderr, "Error RegisterGroupStyle #2: %s\n", err_msg); sqlite3_free (err_msg); return -69; } if ((rows != 1) || (columns != 1)) { sqlite3_free_table (results); fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows, columns); return -70; } if (strcmp (results[1 * columns + 0], "1") != 0) { fprintf (stderr, "Unexpected #2 result (got %s, expected 1)", results[1 * columns + 0]); sqlite3_free_table (results); return -71; } sqlite3_free_table (results); #endif ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -57; } spatialite_cleanup_ex (cache); spatialite_shutdown (); return 0; }
static void OGRSQLiteDriverUnload(CPL_UNUSED GDALDriver* poDriver) { #ifdef SPATIALITE_412_OR_LATER spatialite_shutdown(); #endif }
int main (int argc, char *argv[]) { #ifndef OMIT_FREEXL /* only if FreeXL is supported */ int ret; sqlite3 *handle; char *err_msg = NULL; unsigned int row_count; int rcnt; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -2; } ret = load_XL (handle, "./testcase1.xls", "test1", 0, 0, &row_count, err_msg); if (!ret) { fprintf (stderr, "load_XL() error: %s\n", err_msg); sqlite3_close (handle); return -3; } if (row_count != 17) { fprintf (stderr, "load_XL() unexpected row count: %u\n", row_count); sqlite3_close (handle); return -4; } ret = load_XL (handle, "./testcase1.xls", "test2", 1, 1, &row_count, err_msg); if (!ret) { fprintf (stderr, "load_XL() error sheet 2: %s\n", err_msg); sqlite3_close (handle); return -5; } if (row_count != 19) { fprintf (stderr, "load_XL() unexpected row count sheet 2: %u\n", row_count); sqlite3_close (handle); return -6; } check_duplicated_rows (handle, "test1", &rcnt); if (rcnt != 0) { fprintf (stderr, "check_duplicated_rows() unexpected duplicate count: %d\n", rcnt); sqlite3_close (handle); return -8; } check_duplicated_rows (handle, "test2", &rcnt); if (rcnt != 2) { fprintf (stderr, "check_duplicated_rows() unexpected duplicate count sheet 2: %d\n", rcnt); sqlite3_close (handle); return -10; } remove_duplicated_rows (handle, "test1"); remove_duplicated_rows (handle, "test2"); ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -11; } spatialite_cleanup_ex (cache); #endif /* end FreeXL conditional */ spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { int ret; sqlite3 *handle; char *err_msg = NULL; sqlite3_int64 log_pk; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -2; } ret = sqlite3_exec (handle, "SELECT HasProj()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasProj() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -3; } ret = sqlite3_exec (handle, "SELECT HasGeos()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasGeos() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -4; } ret = sqlite3_exec (handle, "SELECT HasGeosAdvanced()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasGeosAdvanced() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -5; } ret = sqlite3_exec (handle, "SELECT HasGeosReentrant()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasGeosReentrant() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -6; } ret = sqlite3_exec (handle, "SELECT HasGeosOnlyReentrant()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasGeosOnlyReentrant() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -7; } ret = sqlite3_exec (handle, "SELECT HasIconv()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasIconv() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -8; } ret = sqlite3_exec (handle, "SELECT HasMathSql()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasMathSql() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -9; } ret = sqlite3_exec (handle, "SELECT HasGeoCallbacks()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasGeoCallbacks() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -10; } ret = sqlite3_exec (handle, "SELECT HasFreeXL()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasFreeXL() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -11; } ret = sqlite3_exec (handle, "SELECT HasEpsg()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasEpsg() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -12; } ret = sqlite3_exec (handle, "SELECT HasGeosTrunk()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasGeoTrunk() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -13; } ret = sqlite3_exec (handle, "SELECT HasLwGeom()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasLwGeom() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -14; } ret = sqlite3_exec (handle, "SELECT HasLibXml2()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "HasLibXml2() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -15; } gaiaInsertIntoSqlLog (handle, "test", "sql_statement_ok", &log_pk); gaiaUpdateSqlLog (handle, log_pk, 1, NULL); gaiaInsertIntoSqlLog (handle, "test", "sql_statement_no", &log_pk); gaiaUpdateSqlLog (handle, log_pk, 0, "some error message"); ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -16; } spatialite_cleanup_ex (cache); ret = checkCache (); if (ret != 0) return ret; spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { #ifndef OMIT_ICONV /* only if ICONV is supported */ int ret; sqlite3 *handle; char *err_msg = NULL; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ /* testing current style metadata layout >= v.4.0.0 */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory database: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -2; } ret = do_test (handle, 0); if (ret != 0) { fprintf (stderr, "error while testing current style metadata layout\n"); return ret; } ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -19; } spatialite_cleanup_ex (cache); /* testing legacy style metadata layout <= v.3.1.0 */ cache = spatialite_alloc_connection (); ret = system ("cp test-legacy-3.0.1.sqlite copy-utf8_1ex-legacy-3.0.1.sqlite"); if (ret != 0) { fprintf (stderr, "cannot copy legacy v.3.0.1 database\n"); return -1; } ret = sqlite3_open_v2 ("copy-utf8_1ex-legacy-3.0.1.sqlite", &handle, SQLITE_OPEN_READWRITE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open legacy v.3.0.1 database: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = do_test (handle, 1); if (ret != 0) { fprintf (stderr, "error while testing legacy style metadata layout\n"); return ret; } ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -19; } spatialite_cleanup_ex (cache); ret = unlink ("copy-utf8_1ex-legacy-3.0.1.sqlite"); if (ret != 0) { fprintf (stderr, "cannot remove legacy v.3.0.1 database\n"); return -20; } #endif /* end ICONV conditional */ spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { #ifndef OMIT_ICONV /* only if ICONV is supported */ int ret; sqlite3 *handle; char *dbfname = __FILE__ "test.dbf"; char *err_msg = NULL; int row_count; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory database: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); sqlite3_close (handle); return -2; } ret = load_shapefile (handle, "./shp/new-caledonia/points", "points", "CP1252", 4326, "col1", 1, 0, 1, 0, &row_count, err_msg); if (!ret) { fprintf (stderr, "load_shapefile() error for shp/new-caledonia/points: %s\n", err_msg); sqlite3_close (handle); return -3; } if (row_count != 10) { fprintf (stderr, "unexpected row count for shp/new-caledonia/points: %i\n", row_count); sqlite3_close (handle); return -4; } ret = load_shapefile (handle, "./shp/new-caledonia/railways", "railways", "CP1252", 4326, "col1", 1, 0, 1, 0, &row_count, err_msg); if (!ret) { fprintf (stderr, "load_shapefile() error for shp/new-caledonia/railways: %s\n", err_msg); sqlite3_close (handle); return -5; } if (row_count != 13) { fprintf (stderr, "unexpected row count for shp/new-caledonia/points: %i\n", row_count); sqlite3_close (handle); return -6; } ret = load_shapefile (handle, "./shp/new-caledonia/buildings", "buildings", "CP1252", 4326, "col1", 1, 0, 1, 0, &row_count, err_msg); if (!ret) { fprintf (stderr, "load_shapefile() error for shp/new-caledonia/buildings: %s\n", err_msg); sqlite3_close (handle); return -7; } if (row_count != 10) { fprintf (stderr, "unexpected row count for shp/new-caledonia/buildings: %i\n", row_count); sqlite3_close (handle); return -8; } ret = dump_dbf (handle, "points", dbfname, "CP1252", err_msg); if (!ret) { fprintf (stderr, "dump_dbf() error for points: %s\n", err_msg); sqlite3_close (handle); return -9; } unlink (dbfname); ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -9; } spatialite_cleanup_ex (cache); #endif /* end ICONV conditional */ spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { #ifndef OMIT_ICONV /* only if ICONV is supported */ int ret; sqlite3 *handle; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory database: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); ret = do_test (handle, cache); if (ret != 0) return ret; ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -61; } spatialite_cleanup_ex (cache); /* testing again in legacy mode */ spatialite_init (0); ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory database: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -62; } ret = do_test (handle, NULL); if (ret != 0) return ret; ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -63; spatialite_cleanup (); } #endif /* end ICONV conditional */ spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { int result = 0; int ret; char *err_msg = NULL; sqlite3 *db_handle; void *cache = spatialite_alloc_connection (); void *priv_data = rl2_alloc_private (); char *old_SPATIALITE_SECURITY_ENV = NULL; if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ old_SPATIALITE_SECURITY_ENV = getenv ("SPATIALITE_SECURITY"); #ifdef _WIN32 putenv ("SPATIALITE_SECURITY=relaxed"); #else /* not WIN32 */ setenv ("SPATIALITE_SECURITY", "relaxed", 1); #endif /* opening and initializing the "memory" test DB */ ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_open_v2() error: %s\n", sqlite3_errmsg (db_handle)); return -1; } spatialite_init_ex (db_handle, cache, 0); rl2_init (db_handle, priv_data, 0); ret = sqlite3_exec (db_handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg); sqlite3_free (err_msg); return -2; } ret = sqlite3_exec (db_handle, "SELECT CreateRasterCoveragesTable()", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "CreateRasterCoveragesTable() error: %s\n", err_msg); sqlite3_free (err_msg); return -3; } /* SRTM NILE-DOUBLE (GRID) tests */ ret = -100; if (!test_coverage (db_handle, RL2_SAMPLE_INT8, TILE_256, &ret)) return ret; ret = -120; if (!test_coverage (db_handle, RL2_SAMPLE_INT8, TILE_1024, &ret)) return ret; ret = -140; if (!test_coverage (db_handle, RL2_SAMPLE_UINT8, TILE_256, &ret)) return ret; ret = -160; if (!test_coverage (db_handle, RL2_SAMPLE_UINT8, TILE_1024, &ret)) return ret; ret = -180; if (!test_coverage (db_handle, RL2_SAMPLE_INT16, TILE_256, &ret)) return ret; ret = -200; if (!test_coverage (db_handle, RL2_SAMPLE_INT16, TILE_1024, &ret)) return ret; ret = -220; if (!test_coverage (db_handle, RL2_SAMPLE_UINT16, TILE_256, &ret)) return ret; ret = -240; if (!test_coverage (db_handle, RL2_SAMPLE_UINT16, TILE_1024, &ret)) return ret; ret = -260; if (!test_coverage (db_handle, RL2_SAMPLE_INT32, TILE_256, &ret)) return ret; ret = -280; if (!test_coverage (db_handle, RL2_SAMPLE_INT32, TILE_1024, &ret)) return ret; ret = -300; if (!test_coverage (db_handle, RL2_SAMPLE_UINT32, TILE_256, &ret)) return ret; ret = -320; if (!test_coverage (db_handle, RL2_SAMPLE_UINT32, TILE_1024, &ret)) return ret; ret = -340; if (!test_coverage (db_handle, RL2_SAMPLE_FLOAT, TILE_256, &ret)) return ret; ret = -360; if (!test_coverage (db_handle, RL2_SAMPLE_FLOAT, TILE_1024, &ret)) return ret; ret = -380; if (!test_coverage (db_handle, RL2_SAMPLE_DOUBLE, TILE_256, &ret)) return ret; ret = -400; if (!test_coverage (db_handle, RL2_SAMPLE_DOUBLE, TILE_1024, &ret)) return ret; /* closing the DB */ sqlite3_close (db_handle); spatialite_cleanup_ex (cache); rl2_cleanup_private (priv_data); spatialite_shutdown (); if (old_SPATIALITE_SECURITY_ENV) { #ifdef _WIN32 char *env = sqlite3_mprintf ("SPATIALITE_SECURITY=%s", old_SPATIALITE_SECURITY_ENV); putenv (env); sqlite3_free (env); #else /* not WIN32 */ setenv ("SPATIALITE_SECURITY", old_SPATIALITE_SECURITY_ENV, 1); #endif } else { #ifdef _WIN32 putenv ("SPATIALITE_SECURITY="); #else /* not WIN32 */ unsetenv ("SPATIALITE_SECURITY"); #endif } return result; }
int main (int argc, char *argv[]) { int ret; sqlite3 *handle; void *cache = spatialite_alloc_connection (); if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (handle)); sqlite3_close (handle); return -1; } spatialite_init_ex (handle, cache, 0); #ifdef ENABLE_LIBXML2 /* only if LIBXML2 is supported */ if (!check_parse (cache, "books.xml")) { fprintf (stderr, "unable to parse \"books.xml\"\n"); return -2; } if (!check_parse (cache, "opera.xml")) { fprintf (stderr, "unable to parse \"opera.xml\"\n"); return -3; } if (!check_parse (cache, "movies.xml")) { fprintf (stderr, "unable to parse \"movies.xml\"\n"); return -4; } if (!check_validate (cache, "books.xml")) { fprintf (stderr, "unable to validate \"books.xml\"\n"); return -5; } if (!check_validate (cache, "opera.xml")) { fprintf (stderr, "unable to validate \"opera.xml\"\n"); return -6; } if (!check_validate (cache, "movies.xml")) { fprintf (stderr, "unable to validate \"movies.xml\"\n"); return -7; } if (!check_extended (cache, "inspire-data-example.xml", ISO_METADATA)) { fprintf (stderr, "unable to parse \"inspire-data-example.xml\"\n"); return -8; } if (!check_extended (cache, "stazioni_se.xml", SLD_SE_STYLE)) { fprintf (stderr, "unable to parse \"stazioni_se.xml\"\n"); return -9; } if (!check_extended (cache, "thunderstorm_mild.svg", SVG)) { fprintf (stderr, "unable to parse \"thunderstorm_mild.svg\"\n"); return -10; } if (!check_bad_xml (cache)) { fprintf (stderr, "unable to test not well-formed XML\n"); return -11; } if (!check_bad_schema (cache)) { fprintf (stderr, "unable to test invalid Schema\n"); return -12; } if (!check_mline_gpx (handle, cache, "000323485.gpx")) { fprintf (stderr, "unable to test \"000323485.gpx\"\n"); return -13; } if (!check_mline_gpx (handle, cache, "Gpx-sample.gpx")) { fprintf (stderr, "unable to test \"Gpx-sample.gpx\"\n"); return -14; } #endif ret = sqlite3_close (handle); if (ret != SQLITE_OK) { fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle)); return -10; } spatialite_cleanup_ex (cache); spatialite_shutdown (); return 0; }
int main (int argc, char *argv[]) { int result = 0; void *cache = spatialite_alloc_connection (); struct db_conn conn; conn.db_path = NULL; conn.db_handle = NULL; conn.cache = cache; /* testing in current mode */ if (argc == 1) { result = run_all_testcases (&conn, 0); } else { result = run_specified_testcases (argc, argv, &conn, 0); } if (result != 0) { /* it looks like if MinGW applies some wrong assumption */ /* some negative values are incorrectly reported to be OK */ /* forcing -1 seems to resolve this issue */ result = -1; } close_connection (&conn); spatialite_cleanup_ex (conn.cache); conn.cache = NULL; if (result == 0) { /* testing again in legacy mode */ fprintf (stderr, "\n****************** testing again in legacy mode\n\n"); if (argc == 1) { result = run_all_testcases (&conn, 0); } else { result = run_specified_testcases (argc, argv, &conn, 0); } close_connection (&conn); } if (result == 0) { /* testing again in load_extension mode */ fprintf (stderr, "\n****************** testing again in load_extension mode\n\n"); if (argc == 1) { result = run_all_testcases (&conn, 1); } else { result = run_specified_testcases (argc, argv, &conn, 1); } close_connection (&conn); } spatialite_shutdown (); return result; }