Esempio n. 1
0
int main (int argc, char *argv[])
{
    spatialite_init(0);
    spatialite_cleanup();
    
    spatialite_init(1);
    spatialite_cleanup();
    
    sqlite3_reset_auto_extension();
    
    return 0;
}
Esempio n. 2
0
    sqlite_connection (const std::string& file)
        : db_(0)
    {
        spatialite_init (0);

        // sqlite3_open_v2 is available earlier but 
        // shared cache not available until >= 3.6.18
        #if SQLITE_VERSION_NUMBER >= 3006018
        int rc = sqlite3_enable_shared_cache(1);
        if (rc != SQLITE_OK)
        {
           throw mapnik::datasource_exception (sqlite3_errmsg (db_));
        }
        
        int mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_SHAREDCACHE;
        if (sqlite3_open_v2 (file.c_str(), &db_, mode, NULL))
        #else
        #warning "Mapnik's sqlite plugin is compiling against an version of sqlite older than 3.6.18 which may make rendering slow..."
        if (sqlite3_open (file.c_str(), &db_))
        #endif
        {
            std::ostringstream s;
            s << "Spatialite Plugin: ";
            throw mapnik::datasource_exception (sqlite3_errmsg (db_));
        }
        //sqlite3_enable_load_extension(db_, 1);
    }
Esempio n. 3
0
bool QgsOfflineEditing::createSpatialiteDB( const QString& offlineDbPath )
{
  int ret;
  sqlite3 *sqlite_handle;
  char *errMsg = NULL;
  QFile newDb( offlineDbPath );
  if ( newDb.exists() )
  {
    QFile::remove( offlineDbPath );
  }

  // see also QgsNewSpatialiteLayerDialog::createDb()

  QFileInfo fullPath = QFileInfo( offlineDbPath );
  QDir path = fullPath.dir();

  // Must be sure there is destination directory ~/.qgis
  QDir().mkpath( path.absolutePath( ) );

  // creating/opening the new database
  QString dbPath = newDb.fileName();
  spatialite_init( 0 );
  ret = sqlite3_open_v2( dbPath.toUtf8().constData(), &sqlite_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
  if ( ret )
  {
    // an error occurred
    QString errCause = tr( "Could not create a new database\n" );
    errCause += QString::fromUtf8( sqlite3_errmsg( sqlite_handle ) );
    sqlite3_close( sqlite_handle );
    showWarning( errCause );
    return false;
  }
  // activating Foreign Key constraints
  ret = sqlite3_exec( sqlite_handle, "PRAGMA foreign_keys = 1", NULL, 0, &errMsg );
  if ( ret != SQLITE_OK )
  {
    showWarning( tr( "Unable to activate FOREIGN_KEY constraints" ) );
    sqlite3_free( errMsg );
    sqlite3_close( sqlite_handle );
    return false;
  }
  initializeSpatialMetadata( sqlite_handle );

  // all done: closing the DB connection
  sqlite3_close( sqlite_handle );

  return true;
}
int main (int argc, char *argv[])
{
#ifndef OMIT_ICONV	/* only if ICONV is supported */
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    int row_count;

    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 databse: %s\n", sqlite3_errmsg (handle));
	sqlite3_close(handle);
	return -1;
    }
    
    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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_dbf (handle, "./shapetest1.dbf", "test1", "UTF-8", 1, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_dbf() error: %s\n", err_msg);
	sqlite3_close(handle);
	return -3;
    }
    if (row_count != 2) {
	fprintf (stderr, "unexpected row count for load_dbf: %i\n", row_count);
	sqlite3_close(handle);
	return -4;
    }
    
    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle));
	return -5;
    }
    
    spatialite_cleanup();
#endif	/* end ICONV conditional */

    return 0;
}
SpatialiteBackend::SpatialiteBackend(const QString& filename)
    : SpatialiteBase(), p(new SpatialBackendPrivate)
{
    /*
    VERY IMPORTANT:
    you must initialize the SpatiaLite extension [and related]
    BEFORE attempting to perform any other SQLite call
    */
    spatialite_init (0);

    /* showing the SQLite version */
    qDebug ("SQLite version: %s", sqlite3_libversion ());
    /* showing the SpatiaLite version */
    qDebug ("SpatiaLite version: %s", spatialite_version ());

    isTemp = false;
    theFilename = filename;
    open(theFilename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
    InitializeNew();
}
SpatialiteBackend::SpatialiteBackend()
    : SpatialiteBase(), p(new SpatialBackendPrivate)
{
    /*
    VERY IMPORTANT:
    you must initialize the SpatiaLite extension [and related]
    BEFORE attempting to perform any other SQLite call
    */
    spatialite_init (0);

    /* showing the SQLite version */
    qDebug ("SQLite version: %s", sqlite3_libversion ());
    /* showing the SpatiaLite version */
    qDebug ("SpatiaLite version: %s", spatialite_version ());

    isTemp = true;
    theFilename = HOMEDIR + "/" + QDateTime::currentDateTime().toString("yyyyMMdd-hhmmsszzz") + ".spatialite";
    open(HOMEDIR + "/temDb.spatialite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
    InitializeNew();
}
Esempio n. 7
0
int QgsSLConnect::sqlite3_open_v2( const char *filename, sqlite3 **ppDb, int flags, const char *zVfs )
{
#if defined(SPATIALITE_VERSION_GE_4_0_0)
  void *conn = spatialite_alloc_connection();
#else
  spatialite_init( 0 );
#endif

  int res = ::sqlite3_open_v2( filename, ppDb, flags, zVfs );

#if defined(SPATIALITE_VERSION_GE_4_0_0)
  if ( res == SQLITE_OK )
  {
    spatialite_init_ex( *ppDb, conn, 0 );
    mSLconns.insert( *ppDb, conn );
  }
#endif

  return res;
}
Esempio n. 8
0
bool QgsOSMDatabase::open()
{
  // load spatialite extension
  spatialite_init( 0 );

  // open database
  int res = sqlite3_open_v2( mDbFileName.toUtf8().data(), &mDatabase, SQLITE_OPEN_READWRITE, 0 );
  if ( res != SQLITE_OK )
  {
    mError = QString( "Failed to open database [%1]: %2" ).arg( res ).arg( mDbFileName );
    close();
    return false;
  }

  if ( !prepareStatements() )
  {
    close();
    return false;
  }

  return true;
}
Esempio n. 9
0
/**
 * convert current project to offline project
 * returns offline project file path
 */
bool QgsOfflineEditing::convertToOfflineProject( const QString& offlineDataPath, const QString& offlineDbFile, const QStringList& layerIds )
{
  if ( layerIds.isEmpty() )
  {
    return false;
  }
  QString dbPath = QDir( offlineDataPath ).absoluteFilePath( offlineDbFile );
  if ( createSpatialiteDB( dbPath ) )
  {
    spatialite_init( 0 );
    sqlite3* db;
    int rc = sqlite3_open( dbPath.toStdString().c_str(), &db );
    if ( rc != SQLITE_OK )
    {
      showWarning( tr( "Could not open the spatialite database" ) );
    }
    else
    {
      // create logging tables
      createLoggingTables( db );

      emit progressStarted();

      // copy selected vector layers to SpatiaLite
      for ( int i = 0; i < layerIds.count(); i++ )
      {
        emit layerProgressUpdated( i + 1, layerIds.count() );

        QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerIds.at( i ) );
        copyVectorLayer( qobject_cast<QgsVectorLayer*>( layer ), db, dbPath );
      }

      emit progressStopped();

      sqlite3_close( db );

      // save offline project
      QString projectTitle = QgsProject::instance()->title();
      if ( projectTitle.isEmpty() )
      {
        projectTitle = QFileInfo( QgsProject::instance()->fileName() ).fileName();
      }
      projectTitle += " (offline)";
      QgsProject::instance()->title( projectTitle );

      QgsProject::instance()->writeEntry( PROJECT_ENTRY_SCOPE_OFFLINE, PROJECT_ENTRY_KEY_OFFLINE_DB_PATH, dbPath );

      return true;
    }
  }

  return false;

  // Workflow:
  // copy layers to spatialite
  // create spatialite db at offlineDataPath
  // create table for each layer
  // add new spatialite layer
  // copy features
  // save as offline project
  // mark offline layers
  // remove remote layers
  // mark as offline project
}
WrapperSpatialite::WrapperSpatialite(void)
{
    spatialite_init(0);
}
Esempio n. 11
0
int
do_one_case (struct db_conn *conn, const struct test_data *data,
	     int load_extension)
{
    sqlite3 *db_handle = NULL;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;
    int read_only = 0;
    int empty_db = 0;
    int not_memory_db = 0;

    fprintf (stderr, "Test case: %s\n", data->test_case_name);
    if (strncmp
	("_RO", data->database_name + strlen (data->database_name) - 3,
	 strlen ("_RO")) == 0)
	read_only = 1;
    if (strcmp ("NEW:memory:", data->database_name) == 0)
	empty_db = 1;
    if (conn->db_handle != NULL)
      {
	  if (empty_db)
	      ;
	  else if (compare_path (conn->db_path, data->database_name, read_only))
	    {
		if (conn->read_only == read_only)
		  {
		      db_handle = conn->db_handle;
		      goto skip_init;
		  }
	    }
	  close_connection (conn);
      }

    if (conn->cache == NULL && !load_extension)
	spatialite_init (0);

    /* This hack checks if the name ends with _RO */
    if (strncmp
	("_RO", data->database_name + strlen (data->database_name) - 3,
	 strlen ("_RO")) == 0)
      {
	  fprintf (stderr, "opening read_only\n");
	  read_only = 1;
	  ret =
	      sqlite3_open_v2 (data->database_name, &db_handle,
			       SQLITE_OPEN_READONLY, NULL);
      }
    else if (empty_db)
      {
	  ret =
	      sqlite3_open_v2 (":memory:", &db_handle,
			       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
			       NULL);
      }
    else
      {
	  if (strcmp (data->database_name, ":memory:") != 0)
	      not_memory_db = 1;
	  ret =
	      sqlite3_open_v2 (data->database_name, &db_handle,
			       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
			       NULL);
      }
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open %s db: %s\n", data->database_name,
		   sqlite3_errmsg (db_handle));
	  sqlite3_close (db_handle);
	  db_handle = NULL;
	  return -1;
      }

    if (conn->cache != NULL)
	spatialite_init_ex (db_handle, conn->cache, 0);
    if (load_extension)
      {
	  if (!load_dyn_extension (db_handle))
	    {
		sqlite3_close (db_handle);
		db_handle = NULL;
		return -3;
	    }
      }
    save_connection (conn, data->database_name, db_handle, read_only, empty_db);

    if (read_only || not_memory_db)
	goto skip_init;
    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;
      }
  skip_init:

    ret =
	sqlite3_get_table (db_handle, data->sql_statement, &results, &rows,
			   &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -10;
      }
    if ((rows != data->expected_rows) || (columns != data->expected_columns))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -11;
      }
    for (i = 0; i < (data->expected_rows + 1) * data->expected_columns; ++i)
      {
	  if (results[i] != NULL && data->expected_precision[i] == 0)
	    {
		data->expected_precision[i] = strlen (results[i]);
	    }
	  if (results[i] == NULL)
	    {
		if (strcmp ("(NULL)", data->expected_results[i]) == 0)
		  {
		      /* we expected this */
		      continue;
		  }
		else
		  {
		      fprintf (stderr, "Null value at %i.\n", i);
		      fprintf (stderr, "Expected value was: %s\n",
			       data->expected_results[i]);
		      return -12;
		  }
	    }
	  else if (strlen (results[i]) == 0)
	    {
		fprintf (stderr, "zero length result at %i\n", i);
		fprintf (stderr, "Expected value was    : %s|\n",
			 data->expected_results[i]);
		return -13;
	    }
	  else if (strncmp
		   (results[i], data->expected_results[i],
		    data->expected_precision[i]) != 0)
	    {
		fprintf (stderr, "Unexpected value at %i: %s|\n", i,
			 results[i]);
		fprintf (stderr, "Expected value was   : %s|\n",
			 data->expected_results[i]);
		return -14;
	    }
      }
    sqlite3_free_table (results);

    return 0;
}
Esempio n. 12
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;
    const char *count;
    clock_t t0;
    clock_t t1;


    if (argc != 2)
      {
	  fprintf (stderr, "usage: %s test_db_path\n", argv[0]);
	  return -1;
      }


/*
VERY IMPORTANT:
you must initialize the SpatialDB extension [and related]
BEFORE attempting to perform any other SQLite call
*/
    spatialite_init (0);


/* showing the SQLite version */
    printf ("SQLite version: %s\n", sqlite3_libversion ());
/* showing the SpatialDB version */
    printf ("SpatiaLite version: %s\n", spatialite_version ());
    printf ("\n\n");



/*
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;
      }



/*
we are supposing this one is an empty database,
so we have to create the Spatial Metadata
*/
    strcpy (sql, "SELECT InitSpatialMetadata()");
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
/* some 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)
      {
/* some error occurred */
	  printf ("CREATE TABLE 'test' error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  goto abort;
      }


/*
... we'll add to the test table a Geometry column of POINT type
*/
    strcpy (sql, "SELECT AddGeometryColumn('test', 'geom', 3003, 'POINT', 2)");
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
/* some 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 MBR caching
*/
    strcpy (sql, "SELECT CreateMbrCache('test', 'geom')");
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
/* some error occurred */
	  printf ("CreateMbrCache() 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 ();
/*
begining a transaction

*** this step is absolutely critical ***

the SQLite engine is a TRANSITIONAL 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)
      {
/* some 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)
      {
/* some 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 is intended to 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.3lf]\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 SpatialDB BLOB format */
		gaiaToSpatiaLiteBlobWkb (geo, &blob, &blob_size);

/* we can now destroy the geomety 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
		  {
/* 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);



/*
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)
      {
/* some 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)
      {
/* some 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)
	    {
/* some 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.4lf]\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 MBR cache Spatial Index\n", ix);
/*
now we'll perform the spatial query USING the MBR cache Spatial Index
we'll loop 3 times in order to avoid buffering-caching side effects
*/
	  strcpy (sql, "SELECT Count(*) FROM test ");
	  strcat (sql, "WHERE ROWID IN (");
	  strcat (sql, "SELECT rowid FROM cache_test_geom WHERE ");
	  strcat (sql, "mbr = FilterMbrWithin(1000400.5, 4000400.5, 1000450.5, 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)
	    {
/* some 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.4lf]\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;
      }
    printf ("\n\nsample successfully terminated\n");
    return 0;

  abort:
    sqlite3_close (handle);
    return -1;
}
Esempio n. 13
0
int main (int argc, char *argv[])
{
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    int row_count;
    char **results;
    int rows;
    int columns;

    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 -1;
    }
    
    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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/foggia/local_councils", "Councils",
			  "CP1252", 23032, "geom", 1, 0, 1, 0, &row_count,
			  err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error: %s\n", err_msg);
	sqlite3_close(handle);
	return -3;
    }
    if (row_count != 61) {
	fprintf (stderr, "unexpected number of rows loaded: %i\n", row_count);
	sqlite3_close(handle);
	return -4;
    }
    
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE MbrWithin(geom, BuildMbr(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -5;
    }
    if ((rows != 22) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -6;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -7;
    }
    if (strcmp(results[22], "Zapponeta") != 0) {
	fprintf (stderr, "unexpected result at 22: %s\n", results[22]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -8;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "SELECT CreateMbrCache('Councils', 'geom');",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "CreateMbrCache error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -9;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -14;
    }
    if ((rows != 22) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -15;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -16;
    }
    if (strcmp(results[22], "Zapponeta") != 0) {
	fprintf (stderr, "unexpected mbr result at 22: %s\n", results[22]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -17;
    }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrContains(997226.750031, 4627372.000018, 997226.750031, 4627372.000018)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT Contains: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -18;
    }
    if ((rows != 1) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -19;
    }
    if (strcmp(results[1], "Carlantino") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -20;
    }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrIntersects(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -21;
    }
    if ((rows != 35) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -22;
    }
    if (strcmp(results[1], "Apricena") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -23;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "DELETE FROM Councils WHERE lc_name = 'Zapponeta';",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DELETE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -24;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -25;
    }
    if ((rows != 21) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -26;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -27;
    }
    if (strcmp(results[21], "Vieste") != 0) {
	fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -28;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "INSERT INTO Councils (lc_name, geom) VALUES ('Quairading', GeomFromText('MULTIPOLYGON(((997226.750031 4627372.000018, 997301.750031 4627332.000018, 997402.500031 4627344.000018, 997541.500031 4627326.500018,997226.750031 4627372.000018)))', 23032));",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "INSERT error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -29;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT2: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -30;
    }
    if ((rows != 21) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -31;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -32;
    }
    if (strcmp(results[21], "Vieste") != 0) {
	fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -33;
    }
    sqlite3_free_table (results);
    
    ret = sqlite3_exec (handle, "UPDATE Councils SET geom = GeomFromText('MULTIPOLYGON(((987226.750031 4627372.000018, 997301.750031 4627331.000018, 997402.500032 4627344.000018, 997541.500031 4627326.500018, 987226.750031 4627372.000018)))', 23032) WHERE lc_name = \"Quairading\";",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "UPDATE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -34;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT3: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -35;
    }
    if ((rows != 21) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -36;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -37;
    }
    if (strcmp(results[21], "Vieste") != 0) {
	fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -38;
    }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT rowid, mbr FROM cache_Councils_geom;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -39;
    }
    if ((rows != 61) || (columns != 2)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache2 result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -40;
    }
    if (strcmp(results[2], "1") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[2]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -41;
    }
    if (strcmp(results[12], "6") != 0) {
	fprintf (stderr, "unexpected mbr result at 6: %s\n", results[12]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -42;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "DROP TABLE Councils;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE Councils error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -44;
    }

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle));
	return -45;
    }
    
    spatialite_cleanup();
    sqlite3_reset_auto_extension();
    return 0;
}
bool QgsNewSpatialiteLayerDialog::createDb()
{
  QSettings settings;
  int ret;
  sqlite3 *sqlite_handle;
  char *errMsg = NULL;

  if ( mDatabaseComboBox->currentText().isEmpty() )
    return false;

  QFile newDb( mDatabaseComboBox->currentText() );
  if ( !newDb.exists() )
  {
    QgsDebugMsg( "creating a new db" );

    QFileInfo fullPath = QFileInfo( mDatabaseComboBox->currentText() );
    QDir path = fullPath.dir();
    QgsDebugMsg( QString( "making this dir: %1" ).arg( path.absolutePath() ) );

    // Must be sure there is destination directory ~/.qgis
    QDir().mkpath( path.absolutePath( ) );

    // creating/opening the new database
    QString dbPath = newDb.fileName();
    spatialite_init( 0 );
    ret = sqlite3_open_v2( dbPath.toUtf8().constData(), &sqlite_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
    if ( ret )
    {
      // an error occurred
      QString errCause = tr( "Could not create a new database\n" );
      errCause += QString::fromUtf8( sqlite3_errmsg( sqlite_handle ) );
      sqlite3_close( sqlite_handle );
      QMessageBox::warning( 0, tr( "SpatiaLite Database" ), errCause );
      pbnFindSRID->setEnabled( false );
      return false;
    }
    // activating Foreign Key constraints
    ret = sqlite3_exec( sqlite_handle, "PRAGMA foreign_keys = 1", NULL, 0, &errMsg );
    if ( ret != SQLITE_OK )
    {
      QMessageBox::warning( 0, tr( "SpatiaLite Database" ), tr( "Unable to activate FOREIGN_KEY constraints" ) );
      sqlite3_free( errMsg );
      sqlite3_close( sqlite_handle );
      pbnFindSRID->setEnabled( false );
      return false;
    }
    initializeSpatialMetadata( sqlite_handle );

    // all done: closing the DB connection
    sqlite3_close( sqlite_handle );
  }

  QFileInfo fi( newDb );
  if ( !fi.exists() )
  {
    pbnFindSRID->setEnabled( false );
    return false;
  }

  QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";

  if ( !settings.contains( key ) )
  {
    settings.setValue( "/SpatiaLite/connections/selected", fi.fileName() + tr( "@" ) + fi.canonicalFilePath() );
    settings.setValue( key, fi.canonicalFilePath() );

    QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
  }

  pbnFindSRID->setEnabled( true );

  return true;
}
Esempio n. 15
0
int main (int argc, char *argv[])
{
    int ret;
    sqlite3 *handle;
    char *dumpname = __FILE__"dump";
    char *err_msg = NULL;
    int row_count;

    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 -1;
    }
    
    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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/merano-3d/points", "points", "CP1252", 25832, 
			  "col1", 0, 0, 1, 0, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error for shp/merano-3d/points: %s\n", err_msg);
	sqlite3_close(handle);
	return -3;
    }
    if (row_count != 20) {
	fprintf (stderr, "unexpected row count for shp/merano-3d/points: %i\n", row_count);
	sqlite3_close(handle);
	return -4;
    }

    ret = load_shapefile (handle, "./shp/merano-3d/polygons", "polygons", "CP1252", 25832, 
			  "col1", 0, 0, 1, 0, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error for shp/merano-3d/polygons: %s\n", err_msg);
	sqlite3_close(handle);
	return -5;
    }
    if (row_count != 10) {
	fprintf (stderr, "unexpected row count for shp/merano-3d/polygons: %i\n", row_count);
	sqlite3_close(handle);
	return -6;
    }

    ret = load_shapefile (handle, "./shp/merano-3d/roads", "roads", "CP1252", 25832, 
			  "col1", 0, 0, 1, 0, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error for shp/merano-3d/roads: %s\n", err_msg);
	sqlite3_close(handle);
	return -7;
    }
    if (row_count != 18) {
	fprintf (stderr, "unexpected row count for shp/merano-3d/roads: %i\n", row_count);
	sqlite3_close(handle);
	return -8;
    }

    ret = dump_shapefile (handle, "roads", "col1", dumpname, "CP1252", "LINESTRING", 1, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "dump_shapefile() error for 3d roads: %s\n", err_msg);
	sqlite3_close(handle);
	return -9;
    }
    cleanup_shapefile(dumpname);
    if (row_count != 18) {
	fprintf (stderr, "unexpected row count for 3d roads: %i\n", row_count);
	sqlite3_close(handle);
	return -10;
    }

    ret = dump_shapefile (handle, "polygons", "col1", dumpname, "CP1252", "POLYGON", 1, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "dump_shapefile() error for 3d polygons: %s\n", err_msg);
	sqlite3_close(handle);
	return -11;
    }
    cleanup_shapefile(dumpname);
    if (row_count != 10) {
	fprintf (stderr, "unexpected row count for 3d polygons: %i\n", row_count);
	sqlite3_close(handle);
	return -12;
    }

    ret = dump_shapefile (handle, "points", "col1", dumpname, "CP1252", "POINT", 1, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "dump_shapefile() error for 3d points: %s\n", err_msg);
	sqlite3_close(handle);
	return -13;
    }
    cleanup_shapefile(dumpname);
    if (row_count != 20) {
	fprintf (stderr, "unexpected row count for 3d points: %i\n", row_count);
	sqlite3_close(handle);
	return -14;
    }

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle));
	return -15;
    }
    
    spatialite_cleanup();
    sqlite3_reset_auto_extension();
    
    return 0;
}
Esempio n. 16
0
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;

    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 db: %s\n", sqlite3_errmsg (handle));
        sqlite3_close(handle);
        return -1;
    }

    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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();
#endif	/* end FreeXL conditional */

    return 0;
}
int main (int argc, char *argv[])
{
    sqlite3 *db_handle = NULL;
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;

    spatialite_init (0);

    ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
	sqlite3_close (db_handle);
	db_handle = NULL;
	return -1;
    }
    
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE dbftest USING VirtualDBF(\"shp/merano-3d/roads.dbf\", 'CP1252');", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualDBF error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -2;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE dbftest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -3;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE dbftest USING VirtualDBF('shp/merano-3d/roads.dbf', \"CP1252\");", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualDBF error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -4;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE dbftest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -5;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE dbftest USING VirtualDBF('shp/merano-3d/roads.dbf', CP1252);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualDBF error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -6;
    }
    
    for (i = 0; steps[i].sql; ++i) {
	ret = sqlite3_get_table (db_handle, steps[i].sql, &results, &rows, &columns, &err_msg);
	if (ret != SQLITE_OK) {
	    fprintf (stderr, "Error: %s\n", err_msg);
	    sqlite3_free (err_msg);
	    return -7;
	}
	if (rows != steps[i].num_rows) {
	    fprintf (stderr, "Unexpected num of rows for test %i: %i.\n", i, rows);
	    return  -8;
	}
	sqlite3_free_table (results);
    }

    ret = sqlite3_exec (db_handle, "DROP TABLE dbftest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -9;
    }

    sqlite3_close (db_handle);
    spatialite_cleanup();
    sqlite3_reset_auto_extension();
    
    return 0;
}
Esempio n. 18
0
static int
do_test (int legacy_mode)
{
#ifndef OMIT_GEOS		/* only if GEOS is supported */
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    const char *sql;
    int i;
    char **results;
    int rows;
    int columns;
    void *cache = NULL;
    if (!legacy_mode)
	cache = spatialite_alloc_connection ();
    else
	spatialite_init (0);

    ret = system ("cp sql_stmt_tests/testFDO.sqlite testFDO.sqlite");
    if (ret != 0)
      {
	  fprintf (stderr, "cannot copy testFDO.sqlite database\n");
	  return -1001;
      }

    ret =
	sqlite3_open_v2 ("testFDO.sqlite", &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open testFDO.sqlite db: %s\n",
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return -1000;
      }

    if (!legacy_mode)
	spatialite_init_ex (handle, cache, 0);

/* FDO start-up */
    sql = "SELECT AutoFDOStart()";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -1;
      }

/* testing aggregate Union() PointZ WKT "p02" */
    sql = "SELECT AsText(ST_Union(WKT_GEOMETRY)) FROM fdo_p02";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -2;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -3;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -4;
      }
    if (strcmp
	(results[1],
	 "MULTIPOINT Z(664350.17954 5171957.915655 314.52, 664642.363686 5169415.339218 294.37, 664964.447225 5170571.245732 318.25)")
	!= 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result %s\n", results[1]);
	  return -5;
      }
    sqlite3_free_table (results);

/* testing aggregate Union() PointZ WKB "p03" */
    sql =
	"SELECT AsText(ST_Union(GEOMETRY)) FROM fdo_p03 WHERE text_dil IS NULL AND OGC_FID < 3";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -6;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -7;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -8;
      }
    if (strcmp
	(results[1],
	 "MULTIPOINT Z(665216.306643 5169825.707161 296.06, 665224.506512 5169827.907054 296.16)")
	!= 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result |%s|\n",
		   results[1]);
	  return -9;
      }
    sqlite3_free_table (results);

/* testing aggregate Union() PointZ SpatiaLite "p05" */
    sql =
	"SELECT AsText(ST_Union(GEOMETRY)) FROM fdo_p05 WHERE text_dil IS NULL AND OGC_FID < 3";
    ret = sqlite3_get_table (handle, sql, &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 error: bad result: %i/%i.\n", rows,
		   columns);
	  return -11;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result %s\n", results[1]);
	  return -12;
      }
    if (strcmp
	(results[1],
	 "MULTIPOINT Z(667687.978175 5169352.045712 583.140015, 667710.008189 5169402.894615 589.849976)")
	!= 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result b|%s|\n",
		   results[1]);
	  return -13;
      }
    sqlite3_free_table (results);

/* testing Sum(GLength()) LinestringZ WKT "l05" */
    sql = "SELECT Sum(GLength(WKT_GEOMETRY)) FROM fdo_l05";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -14;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -15;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -16;
      }
    if (strncmp (results[1], "59.417763", 9) != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -17;
      }
    sqlite3_free_table (results);

/* testing Sum(GLength()) LinestringZ WKB "l06" */
    sql = "SELECT Sum(GLength(GEOMETRY)) FROM fdo_l06";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -18;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -19;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -20;
      }
    if (strncmp (results[1], "273.076064", 10) != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -21;
      }
    sqlite3_free_table (results);

/* testing Sum(GLength()) LinestringZ SpatiaLite "l07" */
    sql = "SELECT Sum(GLength(GEOMETRY)) FROM fdo_l07";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -22;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -23;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -24;
      }
    if (strncmp (results[1], "219.459808", 10) != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -25;
      }
    sqlite3_free_table (results);

/* testing Sum(Area()) PolygonZ WKT "f04" */
    sql = "SELECT Sum(Area(WKT_GEOMETRY)) FROM fdo_f04";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -26;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -27;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -28;
      }
    if (strncmp (results[1], "9960.931239", 11) != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -29;
      }
    sqlite3_free_table (results);

/* testing Sum(Area()) PolygonZ WKB "f05" */
    sql = "SELECT Sum(Area(GEOMETRY)) FROM fdo_f05";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -30;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -31;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -32;
      }
    if (strncmp (results[1], "69972.113393", 12) != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -33;
      }
    sqlite3_free_table (results);

/* testing Sum(Area()) PolygonZ SpatiaLite "f06" */
    sql = "SELECT Sum(Area(GEOMETRY)) FROM fdo_f06";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -34;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -35;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -36;
      }
    if (strncmp (results[1], "1125.064396", 11) != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -37;
      }
    sqlite3_free_table (results);

/* testing IsValid() LinestringZ WKB "l06" */
    sql = "SELECT IsValid(GEOMETRY) FROM fdo_l06";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -38;
      }
    if ((rows != 12) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -39;
      }
    for (i = 1; i <= rows; i++)
      {
	  if (results[i] == NULL)
	    {
		fprintf (stderr, "Unexpected error: NULL result\n");
		return -40;
	    }
	  if (strcmp (results[i], "0") == 0)
	    {
		const char *geos_msg;
		if (legacy_mode)
		    geos_msg = gaiaGetGeosErrorMsg ();
		else
		    geos_msg = gaiaGetGeosErrorMsg_r (cache);
		if (geos_msg == NULL)
		  {
		      if (legacy_mode)
			  geos_msg = gaiaGetGeosWarningMsg ();
		      else
			  geos_msg = gaiaGetGeosWarningMsg_r (cache);
		  }
		if (geos_msg == NULL)
		  {
		      fprintf (stderr, "Unexpected error: invalid result\n");
		      return -41;
		  }
	    }
      }
    sqlite3_free_table (results);

/* testing IsValid() PolygonZ WKT "f04" */
    sql = "SELECT IsValid(WKT_GEOMETRY) FROM fdo_f04";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -42;
      }
    if ((rows != 16) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -43;
      }
    for (i = 1; i <= rows; i++)
      {
	  if (results[i] == NULL)
	    {
		fprintf (stderr, "Unexpected error: NULL result\n");
		return -44;
	    }
	  if (strcmp (results[i], "0") == 0)
	    {
		const char *geos_msg;
		if (legacy_mode)
		    geos_msg = gaiaGetGeosErrorMsg ();
		else
		    geos_msg = gaiaGetGeosErrorMsg_r (cache);
		if (geos_msg == NULL)
		  {
		      if (legacy_mode)
			  geos_msg = gaiaGetGeosWarningMsg ();
		      else
			  geos_msg = gaiaGetGeosWarningMsg_r (cache);
		  }
		if (geos_msg == NULL)
		  {
		      fprintf (stderr, "Unexpected error: invalid result\n");
		      return -45;
		  }
	    }
      }
    sqlite3_free_table (results);

/* testing IsValid() PolygonZ WKB "f05" */
    sql = "SELECT IsValid(GEOMETRY) FROM fdo_f05";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -46;
      }
    if ((rows != 13) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -47;
      }
    for (i = 1; i <= rows; i++)
      {
	  if (results[i] == NULL)
	    {
		fprintf (stderr, "Unexpected error: NULL result\n");
		return -48;
	    }
	  if (strcmp (results[i], "0") == 0)
	    {
		const char *geos_msg;
		if (legacy_mode)
		    geos_msg = gaiaGetGeosErrorMsg ();
		else
		    geos_msg = gaiaGetGeosErrorMsg_r (cache);
		if (geos_msg == NULL)
		  {
		      if (legacy_mode)
			  geos_msg = gaiaGetGeosWarningMsg ();
		      else
			  geos_msg = gaiaGetGeosWarningMsg_r (cache);
		  }
		if (geos_msg == NULL)
		  {
		      fprintf (stderr, "Unexpected error: invalid result\n");
		      return -49;
		  }
	    }
      }
    sqlite3_free_table (results);

/* testing DOUBLE and TEXT columns */
    sql = "SELECT datum, hoehe FROM fdo_p05 WHERE OGC_FID = 5";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -50;
      }
    if ((rows != 1) || (columns != 2))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -51;
      }
    if (results[2] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -52;
      }
    if (strcmp (results[2], "1997/03/07") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -53;
      }
    if (results[3] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -54;
      }
    if (strcmp (results[3], "277.55") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -54;
      }
    sqlite3_free_table (results);

/* FDO shut-down */
    sql = "SELECT AutoFDOStop()";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -55;
      }

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "sqlite3_close() error: %s\n",
		   sqlite3_errmsg (handle));
	  return -56;
      }

    spatialite_cleanup_ex (cache);
    ret = unlink ("testFDO.sqlite");
    if (ret != 0)
      {
	  fprintf (stderr, "cannot remove testFDO database\n");
	  return -57;
      }
#endif /* end GEOS conditional */

    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;
}
Esempio n. 20
0
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
{
    static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};

    PyObject* database;
    int detect_types = 0;
    PyObject* isolation_level = NULL;
    PyObject* factory = NULL;
    int check_same_thread = 1;
    int cached_statements = 100;
    double timeout = 5.0;
    int rc;
    PyObject* class_attr = NULL;
    PyObject* class_attr_str = NULL;
    int is_apsw_connection = 0;
    PyObject* database_utf8;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
                                     &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
    {
        return -1;
    }

    self->initialized = 1;
    spatialite_init(0);
    self->begin_statement = NULL;

    self->statement_cache = NULL;
    self->statements = NULL;
    self->cursors = NULL;

    Py_INCREF(Py_None);
    self->row_factory = Py_None;

    Py_INCREF(&PyUnicode_Type);
    self->text_factory = (PyObject*)&PyUnicode_Type;

    if (PyString_Check(database) || PyUnicode_Check(database)) {
        if (PyString_Check(database)) {
            database_utf8 = database;
            Py_INCREF(database_utf8);
        } else {
            database_utf8 = PyUnicode_AsUTF8String(database);
            if (!database_utf8) {
                return -1;
            }
        }

        Py_BEGIN_ALLOW_THREADS
        rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
        Py_END_ALLOW_THREADS

        Py_DECREF(database_utf8);

        if (rc != SQLITE_OK) {
            _pysqlite_seterror(self->db, NULL);
            return -1;
        }
    } else {
        /* Create a pysqlite connection from a APSW connection */
        class_attr = PyObject_GetAttrString(database, "__class__");
        if (class_attr) {
            class_attr_str = PyObject_Str(class_attr);
            if (class_attr_str) {
                if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
                    /* In the APSW Connection object, the first entry after
                     * PyObject_HEAD is the sqlite3* we want to get hold of.
                     * Luckily, this is the same layout as we have in our
                     * pysqlite_Connection */
                    self->db = ((pysqlite_Connection*)database)->db;

                    Py_INCREF(database);
                    self->apsw_connection = database;
                    is_apsw_connection = 1;
                }
            }
        }
        Py_XDECREF(class_attr_str);
        Py_XDECREF(class_attr);

        if (!is_apsw_connection) {
            PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
            return -1;
        }
    }

    if (!isolation_level) {
        isolation_level = PyString_FromString("");
        if (!isolation_level) {
            return -1;
        }
    } else {
        Py_INCREF(isolation_level);
    }
    self->isolation_level = NULL;
    pysqlite_connection_set_isolation_level(self, isolation_level);
    Py_DECREF(isolation_level);

    self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
    if (PyErr_Occurred()) {
        return -1;
    }

    self->created_statements = 0;
    self->created_cursors = 0;

    /* Create lists of weak references to statements/cursors */
    self->statements = PyList_New(0);
    self->cursors = PyList_New(0);
    if (!self->statements || !self->cursors) {
        return -1;
    }

    /* By default, the Cache class INCREFs the factory in its initializer, and
     * decrefs it in its deallocator method. Since this would create a circular
     * reference here, we're breaking it by decrementing self, and telling the
     * cache class to not decref the factory (self) in its deallocator.
     */
    self->statement_cache->decref_factory = 0;
    Py_DECREF(self);

    self->inTransaction = 0;
    self->detect_types = detect_types;
    self->timeout = timeout;
    (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
#ifdef WITH_THREAD
    self->thread_ident = PyThread_get_thread_ident();
#endif
    self->check_same_thread = check_same_thread;

    self->function_pinboard = PyDict_New();
    if (!self->function_pinboard) {
        return -1;
    }

    self->collations = PyDict_New();
    if (!self->collations) {
        return -1;
    }

    self->Warning               = pysqlite_Warning;
    self->Error                 = pysqlite_Error;
    self->InterfaceError        = pysqlite_InterfaceError;
    self->DatabaseError         = pysqlite_DatabaseError;
    self->DataError             = pysqlite_DataError;
    self->OperationalError      = pysqlite_OperationalError;
    self->IntegrityError        = pysqlite_IntegrityError;
    self->InternalError         = pysqlite_InternalError;
    self->ProgrammingError      = pysqlite_ProgrammingError;
    self->NotSupportedError     = pysqlite_NotSupportedError;

    return 0;
}
int main (int argc, char *argv[])
{
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    char **results;
    int rows;
    int columns;

    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 db: %s\n", sqlite3_errmsg (handle));
	sqlite3_close(handle);
	return -1;
    }
    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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();
    
    return 0;
}
Esempio n. 22
0
int main (int argc, char *argv[])
{
    FILE *fl;
    int sz = 0;
    int rd;
    int ok;
    int i;
    char tag_name[128];
    char human[8192];
    gaiaExifTagPtr pT;
    sqlite3_int64 val64;
    const char *result;
    unsigned char *blob = NULL;
    gaiaExifTagListPtr tag_list = NULL;
    double longitude;
    double latitude;

    spatialite_init (0);
    
    fl = fopen("sql_stmt_tests/DSC_1467.JPG", "rb");
    if (!fl) {
	fprintf(stderr, "cannot open EXIF-JPEG image: DSC_1467.JPG\n");
	return -1;
    }
    if (fseek(fl, 0, SEEK_END) == 0)
        sz = ftell(fl);
    if (sz <= 14) {
	fprintf(stderr, "invalid size EXIF-JPEG image: DSC_1467.JPG\n");
	return -2;
    }
    
    blob = (unsigned char *) malloc(sz);
    rewind(fl);
    rd = fread(blob, 1, sz, fl);
    if (rd != sz) {
	fprintf(stderr, "read error EXIF-JPEG image: DSC_1467.JPG\n");
	return -3;
    }
    
    tag_list = gaiaGetExifTags(blob, sz);
    if (tag_list == NULL) {
	fprintf(stderr, "cannot extract EXIF tags from JPEG image: DSC_1467.JPG\n");
	return -4;
    }

    val64 = get_pixel_x(tag_list, &ok);
    if (!ok) {
	fprintf(stderr, "DSC_1467.JPG: PixelX tag not found\n");
	return -5;
    }
    if (val64 != 2128) {
	fprintf(stderr, "DSC_1467.JPG: PixelX unexpected value: %d\n", (int)val64);
	return -6;
    }

    val64 = get_pixel_y(tag_list, &ok); 
    if (!ok) {
	fprintf(stderr, "DSC_1467.JPG: PixelY tag not found\n");
	return -7;
    }
    if (val64 != 1416) {
	fprintf(stderr, "DSC_1467.JPG: PixelY unexpected value: %d\n", (int)val64);
	return -8;
    }
  
    get_make(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "DSC_1467.JPG: Make tag not found\n");
	return -9;
    }
    if (strcmp(result, "NIKON CORPORATION") != 0) {
	fprintf(stderr, "DSC_1467.JPG: Make unexpected value: %s|\n", result);
	return -10;
    };

    get_model(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "DSC_1467.JPG: Model tag not found\n");
	return -11;
    }
    if (strcmp(result, "NIKON D700") != 0) {
	fprintf(stderr, "DSC_1467.JPG: Model unexpected value: %s|\n", result);
	return -12;
    };

    get_date(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "DSC_1467.JPG: Date tag not found\n");
	return -13;
    }
    if (strcmp(result, "2011:03:26 11:01:13") != 0) {
	fprintf(stderr, "DSC_1467.JPG: Date unexpected value: %s|\n", result);
	return -14;
    };
            
    for (i = 0; i < gaiaGetExifTagsCount(tag_list); i++)
    {
        pT = gaiaGetExifTagByPos(tag_list, i);
        if (pT)
        {
            gaiaExifTagGetName(pT, tag_name, 128);
            gaiaExifTagGetValueType(pT);
            gaiaIsExifGpsTag(pT);
            gaiaExifTagGetValueType(pT);
            gaiaExifTagGetNumValues(pT);
            gaiaExifTagGetHumanReadable (pT, human, 8190, &ok);
        }
    }

    pT = gaiaGetExifTagById (tag_list, 0x0112);
    if (pT == NULL) {
	fprintf(stderr, "DSC_1467.JPG: tag Orientation not found: %s|\n", result);
	return -15;
    };

    pT = gaiaGetExifTagByName (tag_list, "YCbCrPositioning");
    if (pT == NULL) {
	fprintf(stderr, "DSC_1467.JPG: tag YCbCrPositioning not found: %s|\n", result);
	return -16;
    };

    for (i = 0x0000; i < 0xffff; i++)
    {
        gaiaExifTag tag;
        tag.Gps = 0;
        tag.TagId = i;
        gaiaExifTagGetName(&tag, tag_name, 128);
    }
    for (i = 0x0000; i < 0xffff; i++)
    {
        gaiaExifTag tag;
        tag.Gps = 1;
        tag.TagId = i;
        gaiaExifTagGetName(&tag, tag_name, 128);
    }

    if (blob)
        free(blob);
    if (tag_list)
        gaiaExifTagsFree(tag_list);
    fclose(fl);
       
    fl = fopen("sql_stmt_tests/DSCN0042.JPG", "rb");
    if (!fl) {
	fprintf(stderr, "cannot open EXIF-JPEG image: DSCN0042.JPG\n");
	return -100;
    }
    if (fseek(fl, 0, SEEK_END) == 0)
        sz = ftell(fl);
    if (sz <= 14) {
	fprintf(stderr, "invalid size EXIF-JPEG image: DSCN0042.JPG\n");
	return -17;
    }
    
    blob = (unsigned char *) malloc(sz);
    rewind(fl);
    rd = fread(blob, 1, sz, fl);
    if (rd != sz) {
	fprintf(stderr, "read error EXIF-JPEG image: DSCN0042.JPG\n");
	return -18;
    }
    
    tag_list = gaiaGetExifTags(blob, sz);
    if (tag_list == NULL) {
	fprintf(stderr, "cannot extract EXIF tags from JPEG image: DSCN0042.JPG\n");
	return -19;
    }

    val64 = get_pixel_x(tag_list, &ok);
    if (!ok) {
	fprintf(stderr, "DSCN0042.JPG: PixelX tag not found\n");
	return -20;
    }
    if (val64 != 640) {
	fprintf(stderr, "DSCN0042.JPG: PixelX unexpected value: %d\n", (int)val64);
	return -21;
    }

    val64 = get_pixel_y(tag_list, &ok); 
    if (!ok) {
	fprintf(stderr, "DSCN0042.JPG: PixelY tag not found\n");
	return -22;
    }
    if (val64 != 480) {
	fprintf(stderr, "DSCN0042.JPG: PixelY unexpected value: %d\n", (int)val64);
	return -23;
    }
  
    get_make(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "DSCN0042.JPG: Make tag not found\n");
	return -24;
    }
    if (strcmp(result, "NIKON") != 0) {
	fprintf(stderr, "DSCN0042.JPG: Make unexpected value: %s|\n", result);
	return -25;
    };

    get_model(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "DSCN0042.JPG: Model tag not found\n");
	return -26;
    }
    if (strcmp(result, "COOLPIX P6000") != 0) {
	fprintf(stderr, "DSCN0042.JPG: Model unexpected value: %s|\n", result);
	return -27;
    };

    get_date(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "DSCN0042.JPG: Date tag not found\n");
	return -28;
    }
    if (strcmp(result, "2008:10:22 17:00:07") != 0) {
	fprintf(stderr, "DSCN0042.JPG: Date unexpected value: %s|\n", result);
	return -29;
    };
            
    for (i = 0; i < gaiaGetExifTagsCount(tag_list); i++)
    {
        pT = gaiaGetExifTagByPos(tag_list, i);
        if (pT)
        {
            gaiaExifTagGetName(pT, tag_name, 128);
            gaiaExifTagGetValueType(pT);
            gaiaIsExifGpsTag(pT);
            gaiaExifTagGetValueType(pT);
            gaiaExifTagGetNumValues(pT);
            gaiaExifTagGetHumanReadable (pT, human, 8190, &ok);
        }
    }

    pT = gaiaGetExifTagById (tag_list, 0x0112);
    if (pT == NULL) {
	fprintf(stderr, "DSCN0042.JPG: tag Orientation not found: %s|\n", result);
	return -30;
    };

    pT = gaiaGetExifTagByName (tag_list, "YCbCrPositioning");
    if (pT == NULL) {
	fprintf(stderr, "DSCN0042.JPG: tag YCbCrPositioning not found: %s|\n", result);
	return -31;
    };

    for (i = 0x0000; i < 0xffff; i++)
    {
        gaiaExifTag tag;
        tag.Gps = 0;
        tag.TagId = i;
        gaiaExifTagGetName(&tag, tag_name, 128);
    }
    for (i = 0x0000; i < 0xffff; i++)
    {
        gaiaExifTag tag;
        tag.Gps = 1;
        tag.TagId = i;
        gaiaExifTagGetName(&tag, tag_name, 128);
    }

    if (!gaiaGetGpsLatLong(blob, sz, human, 8192)) {
	fprintf(stderr, "cannot extract GPS coords from JPEG image: DSCN0042.JPG\n");
	return -32;
    }
    if (strcmp(human, "N 43.00 27.00 52.04 / E 11.00 52.00 53.32") != 0) {
	fprintf(stderr, "DSCN0042.JPG: GPS coords unexpected value: %s|\n", human);
	return -33;
    };
    if (!gaiaGetGpsLatLong(blob, sz, human, 20)) {
	fprintf(stderr, "cannot extract GPS coords(20) from JPEG image: DSCN0042.JPG\n");
	return -34;
    }
    if (strcmp(human, "N 43.00 27.00 52.04 ") != 0) {
	fprintf(stderr, "DSCN0042.JPG: GPS coords(20) unexpected value: %s|\n", human);
	return -35;
    };
    if (!gaiaGetGpsCoords(blob, sz, &longitude, &latitude)) {
	fprintf(stderr, "cannot extract GPS long/lat from JPEG image: DSCN0042.JPG\n");
	return -36;
    }
    if (longitude != 11.881478 || latitude != 43.464455) {
	fprintf(stderr, "DSCN0042.JPG: GPS long/lat unexpected values: %1.9f %1.9f|\n", longitude, latitude);
	return -37;
    };

    if (blob)
        free(blob);
    if (tag_list)
        gaiaExifTagsFree(tag_list);
    fclose(fl);
       
    fl = fopen("sql_stmt_tests/La_folla_durante_il_Palio.jpg", "rb");
    if (!fl) {
	fprintf(stderr, "cannot open EXIF-JPEG image: La_folla_durante_il_Palio.jpg\n");
	return -50;
    }
    if (fseek(fl, 0, SEEK_END) == 0)
        sz = ftell(fl);
    if (sz <= 14) {
	fprintf(stderr, "invalid size EXIF-JPEG image: La_folla_durante_il_Palio.jpg\n");
	return -51;
    }
    
    blob = (unsigned char *) malloc(sz);
    rewind(fl);
    rd = fread(blob, 1, sz, fl);
    if (rd != sz) {
	fprintf(stderr, "read error EXIF-JPEG image: La_folla_durante_il_Palio.jpg\n");
	return -52;
    }
    
    tag_list = gaiaGetExifTags(blob, sz);
    if (tag_list == NULL) {
	fprintf(stderr, "cannot extract EXIF tags from JPEG image: La_folla_durante_il_Palio.jpg\n");
	return -53;
    }

    val64 = get_pixel_x(tag_list, &ok);
    if (!ok) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: PixelX tag not found\n");
	return -54;
    }
    if (val64 != 1280) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: PixelX unexpected value: %d\n", (int)val64);
	return -55;
    }

    val64 = get_pixel_y(tag_list, &ok); 
    if (!ok) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: PixelY tag not found\n");
	return -56;
    }
    if (val64 != 960) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: PixelY unexpected value: %d\n", (int)val64);
	return -57;
    }
  
    get_make(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: Make tag not found\n");
	return -58;
    }
    if (strcmp(result, "Nokia") != 0) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: Make unexpected value: %s|\n", result);
	return -59;
    };

    get_model(tag_list, &result, &ok);
    if (!ok) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: Model tag not found\n");
	return -60;
    }
    if (strcmp(result, "6630") != 0) {
	fprintf(stderr, "La_folla_durante_il_Palio.jpg: Model unexpected value: %s|\n", result);
	return -61;
    };
    
    for (i = 0; i < gaiaGetExifTagsCount(tag_list); i++)
    {
        pT = gaiaGetExifTagByPos(tag_list, i);
        if (pT)
        {
            gaiaExifTagGetName(pT, tag_name, 128);
            gaiaExifTagGetValueType(pT);
            gaiaIsExifGpsTag(pT);
            gaiaExifTagGetValueType(pT);
            gaiaExifTagGetNumValues(pT);
            gaiaExifTagGetHumanReadable (pT, human, 8190, &ok);
        }
    }

    for (i = 0x0000; i < 0xffff; i++)
    {
        gaiaExifTag tag;
        tag.Gps = 0;
        tag.TagId = i;
        gaiaExifTagGetName(&tag, tag_name, 128);
    }

    if (blob)
        free(blob);
    if (tag_list)
        gaiaExifTagsFree(tag_list);
    fclose(fl);
    
    spatialite_cleanup();
    
    return 0;
}
bool QgsNewSpatialiteLayerDialog::apply()
{
    // Build up the sql statement for creating the table
    QString sql = QString( "create table %1(" ).arg( quotedIdentifier( leLayerName->text() ) );
    QString delim = "";

    if ( checkBoxPrimaryKey->isChecked() )
    {
        sql += "pkuid integer primary key autoincrement,";
    }

    QTreeWidgetItemIterator it( mAttributeView );
    while ( *it )
    {
        sql += delim + QString( "%1 %2" ).arg( quotedIdentifier(( *it )->text( 0 ) ) ).arg(( *it )->text( 1 ) );

        delim = ",";

        ++it;
    }

    // complete the create table statement
    sql += ")";

    QgsDebugMsg( QString( "Creating table in database %1" ).arg( mDatabaseComboBox->currentText() ) );

    QgsDebugMsg( sql ); // OK

    QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
                         .arg( quotedValue( leLayerName->text() ) )
                         .arg( quotedValue( leGeometryColumn->text() ) )
                         .arg( mCrsId )
                         .arg( quotedValue( selectedType() ) );
    QgsDebugMsg( sqlAddGeom ); // OK

    QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" )
                             .arg( quotedValue( leLayerName->text() ) )
                             .arg( quotedValue( leGeometryColumn->text() ) );
    QgsDebugMsg( sqlCreateIndex ); // OK

    spatialite_init( 0 );

    sqlite3 *db;
    int rc = sqlite3_open( mDatabaseComboBox->currentText().toUtf8(), &db );
    if ( rc != SQLITE_OK )
    {
        QMessageBox::warning( this,
                              tr( "SpatiaLite Database" ),
                              tr( "Unable to open the database: %1" ).arg( mDatabaseComboBox->currentText() ) );
    }
    else
    {
        char * errmsg;
        rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
        if ( rc != SQLITE_OK )
        {
            QMessageBox::warning( this,
                                  tr( "Error Creating SpatiaLite Table" ),
                                  tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( leLayerName->text() ).arg( errmsg ) );
            sqlite3_free( errmsg );
        }
        else
        {
            // create the geometry column and the spatial index
            rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg );
            if ( rc != SQLITE_OK )
            {
                QMessageBox::warning( this,
                                      tr( "Error Creating Geometry Column" ),
                                      tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
                sqlite3_free( errmsg );
            }
            else
            {
                // create the spatial index
                rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg );
                if ( rc != SQLITE_OK )
                {
                    QMessageBox::warning( this,
                                          tr( "Error Creating Spatial Index" ),
                                          tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
                    sqlite3_free( errmsg );
                }

                QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(%3) sql=" )
                        .arg( mDatabaseComboBox->currentText() )
                        .arg( leLayerName->text() )
                        .arg( leGeometryColumn->text() ), leLayerName->text(), "spatialite" );
                if ( layer->isValid() )
                {
                    // register this layer with the central layers registry
                    QList<QgsMapLayer *> myList;
                    myList << layer;
                    //addMapLayers returns a list of all successfully added layers
                    //so we compare that to our original list.
                    if ( myList == QgsMapLayerRegistry::instance()->addMapLayers( myList ) )
                        return true;
                }
                else
                {
                    QgsDebugMsg( leLayerName->text() + " is an invalid layer - not loaded" );
                    QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( leLayerName->text() ) );
                    delete layer;
                }
            }
        }
    }

    return false;
}
int main (int argc, char *argv[])
{
#ifndef OMIT_FREEXL		/* only if FreeXL is supported */
    sqlite3 *db_handle = NULL;
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;

    spatialite_init (0);

    ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
	sqlite3_close (db_handle);
	db_handle = NULL;
	return -1;
    }
    
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE xltest USING VirtualXL(\"testcase1.xls\");", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -2;
    }

    asprintf(&sql_statement, "select col_2, col_4, col_5, col_7, rowid from xltest WHERE col_2 = \"Canal Creek\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -10;
    }
    if ((rows != 2) || (columns != 5)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -11;
    }
    if (strcmp(results[0], "col_2") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -12;
    }
    if (strcmp(results[5], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: name5() bad result: %s.\n", results[5]);
	return  -13;
    }
    if (strncmp(results[6], "-27.86667", 9) != 0) {
	fprintf (stderr, "Unexpected error: lat1() bad result: %s.\n", results[6]);
	return  -14;
    }
    if (strncmp(results[7], "151.51667", 9) != 0) {
	fprintf (stderr, "Unexpected error: lon2() bad result: %s.\n", results[7]);
	return  -15;
    }
    if (strcmp(results[10], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: name10() bad result: %s.\n", results[10]);
	return  -16;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (db_handle, "BEGIN;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "BEGIN error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -21;
    }

    ret = sqlite3_exec (db_handle, "DELETE FROM xltest WHERE col_14 > 100000;", NULL, NULL, &err_msg);
    if (ret != SQLITE_READONLY) {
	fprintf (stderr, "UPDATE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -21;
    }
    sqlite3_free (err_msg);

    ret = sqlite3_exec (db_handle, "ROLLBACK;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "ROLLBACK error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -22;
    }
 
    for (i = 0; i < NUMSTEPS; ++i) {
	ret = sqlite3_get_table (db_handle, steps[i].sql, &results, &rows, &columns, &err_msg);
	if (ret != SQLITE_OK) {
	    fprintf (stderr, "Error: %s\n", err_msg);
	    sqlite3_free (err_msg);
	    return -23;
	}
	if (rows != steps[i].num_rows) {
	    fprintf (stderr, "Unexpected num of rows for test %i: %i.\n", i, rows);
	    return  -24;
	}
	sqlite3_free_table (results);
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE xltest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -25;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nosuchworksheet USING VirtualXL(\"testcase1.xls\", 3);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -26;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE nosuchworksheet;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -27;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nosuchfile USING VirtualXL(\"not_a_file.xls\", 3);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -28;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE nosuchfile;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -29;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE sheet2 USING VirtualXL(\"testcase1.xls\", 1, 1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -30;
    }
    asprintf(&sql_statement, "select row_no, place, lat, lon, rowid from sheet2 WHERE place = \"Canal Creek\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -31;
    }
    if ((rows != 4) || (columns != 5)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -32;
    }
    if (strcmp(results[0], "row_no") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result: %s.\n", results[0]);
	return  -33;
    }
    if (strcmp(results[6], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result place: %s.\n", results[6]);
	return  -34;
    }
    if (strncmp(results[7], "-27.86667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lat: %s.\n", results[7]);
	return  -35;
    }
    if (strncmp(results[8], "151.51667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lon: %s.\n", results[8]);
	return  -36;
    }
    if (strcmp(results[11], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result place2: %s.\n", results[11]);
	return  -37;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select row_no, place, lat, lon, rowid from sheet2 WHERE row_no = 16");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -38;
    }
    if ((rows != 1) || (columns != 5)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -39;
    }
    if (strcmp(results[0], "row_no") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result: %s.\n", results[0]);
	return  -40;
    }
    if (strcmp(results[6], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result place: %s.\n", results[6]);
	return  -41;
    }
    if (strncmp(results[7], "-27.86667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lat: %s.\n", results[7]);
	return  -42;
    }
    if (strncmp(results[8], "151.51667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lon: %s.\n", results[8]);
	return  -43;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (db_handle, "DROP TABLE sheet2;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -44;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE noquote USING VirtualXL(testcase1.xls);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -45;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE noquote;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -46;
    }
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE noheader USING VirtualXL(\"testcase1.xls\", 0, 0);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -47;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE noheader;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -48;
    }
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nofile USING VirtualXL();", NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR) {
	fprintf (stderr, "VirtualXL unexpected result: %i\n", ret);
	return -49;
    }
    sqlite3_free (err_msg);

    sqlite3_close (db_handle);
    spatialite_cleanup();
#endif	/* end FreeXL conditional */
    
    return 0;
}
Esempio n. 25
0
int main (int argc, char *argv[])
{
#ifndef OMIT_ICONV	/* only if ICONV is supported */
    int ret;
    sqlite3 *handle;
    char *kmlname = __FILE__"test.kml";
    char *geojsonname = __FILE__"test.geojson"; 
    char *err_msg = NULL;
    int row_count;

    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 -1;
    }
    
    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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/taiwan/hystoric", "hystoric", "UTF-8", 4326, 
			  "col1", 1, 0, 1, 0, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error for shp/taiwan/hystoric: %s\n", err_msg);
	sqlite3_close(handle);
	return -3;
    }
    if (row_count != 15) {
	fprintf (stderr, "unexpected row count for shp/taiwan/hystoric: %i\n", row_count);
	sqlite3_close(handle);
	return -4;
    }

    ret = load_shapefile (handle, "./shp/taiwan/leisure", "leisure", "UTF-8", 4326, 
			  "col1", 1, 0, 1, 0, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error for shp/taiwan/leisure: %s\n", err_msg);
	sqlite3_close(handle);
	return -5;
    }
    if (row_count != 5) {
	fprintf (stderr, "unexpected row count for shp/taiwan/leisure: %i\n", row_count);
	sqlite3_close(handle);
	return -6;
    }

    ret = load_shapefile (handle, "./shp/taiwan/route", "route", "UTF-8", 4326, 
			  "col1", 1, 0, 1, 0, &row_count, err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error for shp/taiwan/route: %s\n", err_msg);
	sqlite3_close(handle);
	return -7;
    }
    if (row_count != 4) {
	fprintf (stderr, "unexpected row count for shp/taiwan/route: %i\n", row_count);
	sqlite3_close(handle);
	return -8;
    }

#ifndef OMIT_PROJ	/* only if PROJ is supported */
    if (is_kml_constant (handle, "route", "name")) {
	fprintf(stderr, "unexpected result for is_kml_constant (1)\n");
	return -9;
    }
    if (! is_kml_constant (handle, "route", "foo")) {
	fprintf(stderr, "unexpected result for is_kml_constant (2)\n");
	return -10;
    }
    
    ret = dump_kml (handle, "route", "col1", kmlname, NULL, NULL, 10);
    if (!ret) {
        fprintf (stderr, "dump_kml (1) error for shp/taiwan/route: %s\n", err_msg);
	sqlite3_close(handle);
	return -11;
    }
    unlink(kmlname);

    ret = dump_kml (handle, "route", "col1", kmlname, "name", NULL, 10);
    if (!ret) {
        fprintf (stderr, "dump_kml (2) error for shp/taiwan/route: %s\n", err_msg);
	sqlite3_close(handle);
	return -12;
    }
    unlink(kmlname);

    ret = dump_kml (handle, "route", "col1", kmlname, "theta", NULL, 10);
    if (!ret) {
        fprintf (stderr, "dump_kml (3) error for shp/taiwan/route: %s\n", err_msg);
	sqlite3_close(handle);
	return -13;
    }
    unlink(kmlname);

    ret = dump_kml (handle, "route", "col1", kmlname, "name", "sub_type", 10);
    if (!ret) {
        fprintf (stderr, "dump_kml (4) error for shp/taiwan/route: %s\n", err_msg);
	sqlite3_close(handle);
	return -14;
    }
    unlink(kmlname);

    ret = dump_kml (handle, "route", "col1", kmlname, "theta", "beta", 10);
    if (!ret) {
        fprintf (stderr, "dump_kml (5) error for shp/taiwan/route: %s\n", err_msg);
	sqlite3_close(handle);
	return -15;
    }
    unlink(kmlname);
#endif	/* end PROJ conditional */

    ret = dump_geojson(handle, "route", "col1", geojsonname, 10, 5);
    if (!ret) {
        fprintf (stderr, "dump_geojson() error for shp/taiwan/route: %s\n", err_msg);
       sqlite3_close(handle);
       return -16;
    }
    unlink(geojsonname);

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle));
	return -17;
    }
    
    spatialite_cleanup();
#endif	/* end ICONV conditional */

    return 0;
}
int main (int argc, char *argv[])
{
    sqlite3 *db_handle = NULL;
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;
    const char *sql;

    spatialite_init (0);

    ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
	sqlite3_close (db_handle);
	db_handle = NULL;
	return -1;
    }
    
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape('shp/merano-3d/roads', CP1252, 25832);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -2;
    }

    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -3;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape(\"shp/merano-3d/roads\", 'CP1252', -1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -4;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -5;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE unquoted USING VirtualShape(shapetest1, UTF8, -1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -6;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE unquoted;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -7;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape('shp/merano-3d/roads', \"CP1252\", 25832);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -8;
    }

    for (i = 0; steps[i].sql; ++i) {
	ret = sqlite3_get_table (db_handle, steps[i].sql, &results, &rows, &columns, &err_msg);
	if (ret != SQLITE_OK) {
	    fprintf (stderr, "Error: %s\n", err_msg);
	    sqlite3_free (err_msg);
	    return -9;
	}
	if (rows != steps[i].num_rows) {
	    fprintf (stderr, "Unexpected num of rows for test %i: %i.\n", i, rows);
	    return  -10;
	}
	sqlite3_free_table (results);
    }

    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -11;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nosuchfile USING VirtualShape(nosuchfile, UTF8, -1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -12;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE nosuchfile;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -13;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE toofewargs USING VirtualShape(\"shapetest1\", UTF8);", NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR) {
	fprintf (stderr, "VirtualShape unexpected result: %i\n", ret);
	return -14;
    }
    sqlite3_free (err_msg);

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE toomanyargs USING VirtualShape(\"shapetest1\", UTF8, 4386, 1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR) {
	fprintf (stderr, "VirtualShape unexpected result: %i\n", ret);
	return -15;
    }
    sqlite3_free (err_msg);
    
    sqlite3_close (db_handle);
    spatialite_cleanup();
    sqlite3_reset_auto_extension();
    
    return 0;
}
Esempio n. 27
0
int main (int argc, char *argv[])
{
#ifndef OMIT_ICONV	/* only if ICONV is supported */
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    int row_count;
    char **results;
    int rows;
    int columns;
    int pt;

    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 -1;
    }
    
    ret = sqlite3_exec (handle, "SELECT InitSpatialMetadata()", 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/foggia/local_councils", "Councils",
			  "CP1252", 23032, "geom", 1, 0, 1, 0, &row_count,
			  err_msg);
    if (!ret) {
        fprintf (stderr, "load_shapefile() error: %s\n", err_msg);
	sqlite3_close(handle);
	return -3;
    }
    if (row_count != 61) {
	fprintf (stderr, "unexpected number of rows loaded: %i\n", row_count);
	sqlite3_close(handle);
	return -4;
    }
    
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE MbrWithin(geom, BuildMbr(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -5;
    }
    if ((rows != 22) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -6;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -7;
    }
    if (strcmp(results[22], "Zapponeta") != 0) {
	fprintf (stderr, "unexpected result at 22: %s\n", results[22]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -8;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "SELECT CreateMbrCache('Councils', 'geom');",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "CreateMbrCache error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -9;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -14;
    }
    if ((rows != 22) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -15;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -16;
    }
    if (strcmp(results[22], "Zapponeta") != 0) {
	fprintf (stderr, "unexpected mbr result at 22: %s\n", results[22]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -17;
    }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrContains(997226.750031, 4627372.000018, 997226.750031, 4627372.000018)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT Contains: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -18;
    }
    if ((rows != 1) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -19;
    }
    if (strcmp(results[1], "Carlantino") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -20;
    }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrIntersects(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -21;
    }
    if ((rows != 35) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -22;
    }
    if (strcmp(results[1], "Apricena") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -23;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "DELETE FROM Councils WHERE lc_name = 'Zapponeta';",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DELETE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -24;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -25;
    }
    if ((rows != 21) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -26;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -27;
    }
    if (strcmp(results[21], "Vieste") != 0) {
	fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -28;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "INSERT INTO Councils (lc_name, geom) VALUES ('Quairading', GeomFromText('MULTIPOLYGON(((997226.750031 4627372.000018, 997301.750031 4627332.000018, 997402.500031 4627344.000018, 997541.500031 4627326.500018,997226.750031 4627372.000018)))', 23032));",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "INSERT error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -29;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT2: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -30;
    }
    if ((rows != 21) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -31;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -32;
    }
    if (strcmp(results[21], "Vieste") != 0) {
	fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -33;
    }
    sqlite3_free_table (results);
    
    ret = sqlite3_exec (handle, "UPDATE Councils SET geom = GeomFromText('MULTIPOLYGON(((987226.750031 4627372.000018, 997301.750031 4627331.000018, 997402.500032 4627344.000018, 997541.500031 4627326.500018, 987226.750031 4627372.000018)))', 23032) WHERE lc_name = \"Quairading\";",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "UPDATE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -34;
    }

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT3: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -35;
    }
    if ((rows != 21) || (columns != 1)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -36;
    }
    if (strcmp(results[1], "Ascoli Satriano") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -37;
    }
    if (strcmp(results[21], "Vieste") != 0) {
	fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -38;
    }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret = sqlite3_get_table (handle, "SELECT rowid, mbr FROM cache_Councils_geom;",
			     &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -39;
    }
    if ((rows != 61) || (columns != 2)) {
	fprintf (stderr, "Unexpected error: select lc_name bad cache2 result: %i/%i.\n", rows, columns);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return  -40;
    }
    if (strcmp(results[2], "1") != 0) {
	fprintf (stderr, "unexpected mbr result at 1: %s\n", results[2]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -41;
    }
    if (strcmp(results[12], "6") != 0) {
	fprintf (stderr, "unexpected mbr result at 6: %s\n", results[12]);
	sqlite3_free_table (results);
	sqlite3_close(handle);
	return -42;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "DROP TABLE Councils;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE Councils error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -44;
    }

/* creating and feeding a Point table */
    ret = sqlite3_exec (handle, "CREATE TABLE pt (id INTEGER);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "CREATE TABLE pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -45;
    }
    ret = sqlite3_exec (handle, "SELECT AddGeometryColumn('pt', 'g', 4326, 1, 'XY');", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -46;
    }
    ret = sqlite3_exec (handle, "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 2.5);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -47;
    }
    ret = sqlite3_exec (handle, "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 'XY', 0.5);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -48;
    }
    ret = sqlite3_exec (handle, "SELECT AddGeometryColumn('pt', 'g', 4326, 'DUMMY', 'XY');", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -49;
    }
    ret = sqlite3_exec (handle, "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 'DUMMY');", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -50;
    }
    ret = sqlite3_exec (handle, "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 2);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -51;
    }
    ret = sqlite3_exec (handle, "SELECT CreateMbrCache('pt', 'g');", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "CreateMbrCache pt.g error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -52;
    }
    for (pt = 0; pt < 10000; pt++)
    {
    /* inserting Points */
        char sql[1024];
        sprintf(sql, "INSERT INTO pt (id, g) VALUES (%d, GeomFromText('POINT(%1.2f %1.2f)', 4326));", pt, 11.0 + (pt / 10000.0), 43.0 + (pt / 10000.0));
        ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);             
        if (ret != SQLITE_OK) {
            fprintf (stderr, "INSERT INTO pt error: %s\n", err_msg);
            sqlite3_free (err_msg);
            return -53;
        }
    }
    for (pt = 5000; pt < 6000; pt++)
    {
    /* updating Points */
        char sql[1024];
        sprintf(sql, "UPDATE pt SET g  = GeomFromText('POINT(%1.2f %1.2f)', 4326) WHERE id = %d;", 12.0 + (pt / 10000.0), 42.0 + (pt / 10000.0), pt);
        ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);             
        if (ret != SQLITE_OK) {
            fprintf (stderr, "UPDATE pt error: %s\n", err_msg);
            sqlite3_free (err_msg);
            return -54;
        }
    }
    for (pt = 7000; pt < 8000; pt++)
    {
    /* deleting Points */
        char sql[1024];
        sprintf(sql, "DELETE FROM pt WHERE id = %d;", pt);
        ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);             
        if (ret != SQLITE_OK) {
            fprintf (stderr, "UPDATE pt error: %s\n", err_msg);
            sqlite3_free (err_msg);
            return -55;
        }
    }

    ret = sqlite3_exec (handle, "SELECT CreateMbrCache(1, 'geom');",
                        NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "invalid CreateMbrCache error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -56;
    }
    ret = sqlite3_exec (handle, "SELECT CreateMbrCache('Councils', 2);",
                        NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "invalid CreateMbrCache error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -57;
    }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin('a', 2, 3, 4);",
                        NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -58;
    }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin(1, 'a', 3, 4);",
                        NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -59;
    }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin(1, 2, 'a', 4);",
                        NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -60;
    }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin(1, 2, 3, 'a');",
                        NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
        sqlite3_free (err_msg);
        return -61;
    }

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK) {
        fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (handle));
	return -62;
    }
    
    spatialite_cleanup();
#endif	/* end ICONV conditional */

    return 0;
}
int main (int argc, char *argv[])
{
    sqlite3 *db_handle = NULL;
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;

    spatialite_init (0);

    ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
	sqlite3_close (db_handle);
	db_handle = NULL;
	return -1;
    }
    
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape(\"shapetest1\", UTF-8, 4326);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -2;
    }

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 < 20;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -3;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -4;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -5;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -6;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -7;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -8;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 <= 19;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -10;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -11;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -12;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -13;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -14;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -15;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 = 20;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -16;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -17;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -18;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n", results[3]);
	return  -19;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer2() bad result: %s.\n", results[4]);
	return  -20;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -21;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 > 2;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -22;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -23;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -24;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees2 bad result: %s.\n", results[3]);
	return  -25;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer4() bad result: %s.\n", results[4]);
	return  -26;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -27;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 >= 20;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -28;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -29;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -30;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees3 bad result: %s.\n", results[3]);
	return  -31;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer5() bad result: %s.\n", results[4]);
	return  -32;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -33;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 < \"p\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -34;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -35;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -36;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n", results[3]);
	return  -37;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -38;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -39;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 <= \"p\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -40;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -41;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -42;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n", results[3]);
	return  -43;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -44;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -45;
    }
    sqlite3_free_table (results);

    
    ret = sqlite3_exec (db_handle, "BEGIN;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "BEGIN error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -46;
    }

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 > \"p\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -48;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -49;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -50;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -51;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -52;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -53;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (db_handle, "DELETE FROM shapetest WHERE testcase2 = 2;", NULL, NULL, &err_msg);
    if (ret != SQLITE_READONLY) {
	fprintf (stderr, "UPDATE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -46;
    }
    sqlite3_free (err_msg);
    
    ret = sqlite3_exec (db_handle, "ROLLBACK;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "ROLLBACK error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -47;
    }

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 >= \"p\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -54;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -55;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -56;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -57;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -58;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -59;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 = \"windward\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -54;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -55;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -56;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -57;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -58;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -59;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID = 1;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -60;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -61;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -62;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -63;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -64;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -65;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID < 2;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -66;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -67;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -68;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -69;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -70;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -71;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID <= 1;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -72;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -73;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -74;
    }
    if (strcmp(results[3], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[3]);
	return  -75;
    }
    if (strcmp(results[4], "2") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -76;
    }
    if (strcmp(results[5], "POINT(3480766.311245 4495355.740524)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -77;
    }
    sqlite3_free_table (results);
    
    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID > 1;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -78;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -79;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -80;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n", results[3]);
	return  -81;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -82;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -83;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID >= 2;");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -84;
    }
    if ((rows != 1) || (columns != 3)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -85;
    }
    if (strcmp(results[0], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -86;
    }
    if (strcmp(results[3], "orde lees") != 0) {
	fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n", results[3]);
	return  -87;
    }
    if (strcmp(results[4], "20") != 0) {
	fprintf (stderr, "Unexpected error: integer() bad result: %s.\n", results[4]);
	return  -88;
    }
    if (strcmp(results[5], "POINT(3482470.825574 4495691.054818)") != 0) {
	fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n", results[5]);
	return  -89;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select PKUID, testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 LIKE \"wind\%\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -90;
    }
    if ((rows != 1) || (columns != 4)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -91;
    }
    if (strcmp(results[0], "PKUID") != 0) {
	fprintf (stderr, "Unexpected error: header uid bad result: %s.\n", results[0]);
	return  -92;
    }
    if (strcmp(results[1], "testcase1") != 0) {
	fprintf (stderr, "Unexpected error: header bad result: %s.\n", results[1]);
	return  -93;
    }
    if (strcmp(results[4], "1") != 0) {
	fprintf (stderr, "Unexpected error: windward PK bad result: %s.\n", results[4]);
	return  -93;
    }
    if (strcmp(results[5], "windward") != 0) {
	fprintf (stderr, "Unexpected error: windward bad result: %s.\n", results[5]);
	return  -94;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -49;
    }

    sqlite3_close (db_handle);
    spatialite_cleanup();
    sqlite3_reset_auto_extension();
    
    return 0;
}