Check-in [7dcf78e2d0]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:using a second DB connection on StoredProc_Execute
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7dcf78e2d0922a49fbe55189c22eef44902166e4
User & Date: sandro 2019-07-05 13:01:59
Context
2019-07-28
06:06
removing any reference to the infamous DQS misfeature - take one check-in: e24e4ef7a0 user: sandro tags: trunk
2019-07-05
13:01
using a second DB connection on StoredProc_Execute check-in: 7dcf78e2d0 user: sandro tags: trunk
2019-05-29
09:12
implementing GetDbObjectScope() and Pause() check-in: e38bdcfea5 user: sandro tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/connection_cache/alloc_cache.c.

389
390
391
392
393
394
395

396
397
398
399
400
401
402
    cache->proj6_cached_area = NULL;
    cache->is_pause_enabled = 0;
    cache->RTTOPO_handle = NULL;
    cache->cutterMessage = NULL;
    cache->storedProcError = NULL;
    cache->createRoutingError = NULL;
    cache->SqlProcLogfile = NULL;

    cache->SqlProcLog = NULL;
    cache->SqlProcContinue = 1;
    cache->SqlProcRetValue = gaia_alloc_variant ();
    cache->pool_index = -1;
    cache->gaia_proj_error_msg = NULL;
    cache->gaia_geos_error_msg = NULL;
    cache->gaia_geos_warning_msg = NULL;







>







389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    cache->proj6_cached_area = NULL;
    cache->is_pause_enabled = 0;
    cache->RTTOPO_handle = NULL;
    cache->cutterMessage = NULL;
    cache->storedProcError = NULL;
    cache->createRoutingError = NULL;
    cache->SqlProcLogfile = NULL;
    cache->SqlProcLogfileAppend = 0;
    cache->SqlProcLog = NULL;
    cache->SqlProcContinue = 1;
    cache->SqlProcRetValue = gaia_alloc_variant ();
    cache->pool_index = -1;
    cache->gaia_proj_error_msg = NULL;
    cache->gaia_geos_error_msg = NULL;
    cache->gaia_geos_warning_msg = NULL;

Changes to src/headers/spatialite_private.h.

201
202
203
204
205
206
207

208
209
210
211
212
213
214
	gaiaSequencePtr first_seq;
	gaiaSequencePtr last_seq;
	struct splite_vtable_extent *first_vtable_extent;
	struct splite_vtable_extent *last_vtable_extent;
	int ok_last_used_sequence;
	int last_used_sequence_val;
	char *SqlProcLogfile;

	FILE *SqlProcLog;
	int SqlProcContinue;
	struct gaia_variant_value *SqlProcRetValue;
	int tinyPointEnabled;
	unsigned char magic2;
	char *lastPostgreSqlError;
	int buffer_end_cap_style;







>







201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	gaiaSequencePtr first_seq;
	gaiaSequencePtr last_seq;
	struct splite_vtable_extent *first_vtable_extent;
	struct splite_vtable_extent *last_vtable_extent;
	int ok_last_used_sequence;
	int last_used_sequence_val;
	char *SqlProcLogfile;
	int SqlProcLogfileAppend;
	FILE *SqlProcLog;
	int SqlProcContinue;
	struct gaia_variant_value *SqlProcRetValue;
	int tinyPointEnabled;
	unsigned char magic2;
	char *lastPostgreSqlError;
	int buffer_end_cap_style;

Changes to src/spatialite/se_helpers.c.

4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
....
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
	  sql = "SELECT v.coverage_name, v.f_table_name, v.f_geometry_column, "
	      "c.srid FROM vector_coverages AS v "
	      "JOIN geometry_columns AS c ON (Lower(v.f_table_name) = "
	      "Lower(c.f_table_name) AND Lower(v.f_geometry_column) = "
	      "Lower(c.f_geometry_column)) "
	      "WHERE v.f_table_name IS NOT NULL AND v.f_geometry_column IS NOT NULL "
	      "UNION "
	      "SELECT v.coverage_name, w.f_table_name, w.f_geometry_column, c.srid "
	      "FROM vector_coverages AS v "
	      "JOIN views_geometry_columns AS w ON "
	      "(Lower(v.view_name) = Lower(w.view_name) AND "
	      "Lower(v.view_geometry) = Lower(w.view_geometry)) "
	      "JOIN geometry_columns AS c ON "
	      "(Lower(w.f_table_name) = Lower(c.f_table_name) AND "
	      "Lower(w.f_geometry_column) = Lower(c.f_geometry_column)) "
................................................................................
	      "c.srid FROM vector_coverages AS v "
	      "JOIN geometry_columns AS c ON (Lower(v.f_table_name) = "
	      "Lower(c.f_table_name) AND Lower(v.f_geometry_column) = "
	      "Lower(c.f_geometry_column)) "
	      "WHERE Lower(v.coverage_name) = Lower(?) AND "
	      "v.f_table_name IS NOT NULL AND v.f_geometry_column IS NOT NULL "
	      "UNION "
	      "SELECT v.coverage_name, w.f_table_name, w.f_geometry_column, c.srid "
	      "FROM vector_coverages AS v "
	      "JOIN views_geometry_columns AS w ON "
	      "(Lower(v.view_name) = Lower(w.view_name) AND "
	      "Lower(v.view_geometry) = Lower(w.view_geometry)) "
	      "JOIN geometry_columns AS c ON "
	      "(Lower(w.f_table_name) = Lower(c.f_table_name) AND "
	      "Lower(w.f_geometry_column) = Lower(c.f_geometry_column)) "







|







 







|







4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
....
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
	  sql = "SELECT v.coverage_name, v.f_table_name, v.f_geometry_column, "
	      "c.srid FROM vector_coverages AS v "
	      "JOIN geometry_columns AS c ON (Lower(v.f_table_name) = "
	      "Lower(c.f_table_name) AND Lower(v.f_geometry_column) = "
	      "Lower(c.f_geometry_column)) "
	      "WHERE v.f_table_name IS NOT NULL AND v.f_geometry_column IS NOT NULL "
	      "UNION "
	      "SELECT v.coverage_name, v.view_name, w.view_geometry, c.srid "
	      "FROM vector_coverages AS v "
	      "JOIN views_geometry_columns AS w ON "
	      "(Lower(v.view_name) = Lower(w.view_name) AND "
	      "Lower(v.view_geometry) = Lower(w.view_geometry)) "
	      "JOIN geometry_columns AS c ON "
	      "(Lower(w.f_table_name) = Lower(c.f_table_name) AND "
	      "Lower(w.f_geometry_column) = Lower(c.f_geometry_column)) "
................................................................................
	      "c.srid FROM vector_coverages AS v "
	      "JOIN geometry_columns AS c ON (Lower(v.f_table_name) = "
	      "Lower(c.f_table_name) AND Lower(v.f_geometry_column) = "
	      "Lower(c.f_geometry_column)) "
	      "WHERE Lower(v.coverage_name) = Lower(?) AND "
	      "v.f_table_name IS NOT NULL AND v.f_geometry_column IS NOT NULL "
	      "UNION "
	      "SELECT v.coverage_name, v.view_name, v.view_geometry, c.srid "
	      "FROM vector_coverages AS v "
	      "JOIN views_geometry_columns AS w ON "
	      "(Lower(v.view_name) = Lower(w.view_name) AND "
	      "Lower(v.view_geometry) = Lower(w.view_geometry)) "
	      "JOIN geometry_columns AS c ON "
	      "(Lower(w.f_table_name) = Lower(c.f_table_name) AND "
	      "Lower(w.f_geometry_column) = Lower(c.f_geometry_column)) "

Changes to src/spatialite/virtualrouting.c.

2041
2042
2043
2044
2045
2046
2047

2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
	  /* Dijsktra loop */
	  n = routing_dequeue (heap);
	  destination = check_multiTo (n, multiSolution->MultiTo);
	  if (destination != NULL)
	    {
		/* reached one of the multiple destinations */
		RouteLinkPtr *result;

		int cnt = 0;
		int to = destination->InternalIndex;
		n = e->Nodes + to;
		while (n->PreviousNode != NULL)
		  {
		      /* counting how many Links are into the Shortest Path solution */
		      cnt++;
		      n = n->PreviousNode;
		  }
		/* allocating the solution */
		result = malloc (sizeof (RouteLinkPtr) * cnt);
		k = cnt - 1;
		n = e->Nodes + to;
		while (n->PreviousNode != NULL)
		  {
		      /* inserting a Link into the solution */
		      result[k] = n->xLink;
		      n = n->PreviousNode;
		      k--;
		  }
		solution =
		    add2multiSolution (multiSolution, multiSolution->From,
				       destination);
		build_solution (handle, options, graph, solution, result, cnt);
		/* testing for end (all destinations already reached) */







>


|
|



|




|
|


|
|







2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
	  /* Dijsktra loop */
	  n = routing_dequeue (heap);
	  destination = check_multiTo (n, multiSolution->MultiTo);
	  if (destination != NULL)
	    {
		/* reached one of the multiple destinations */
		RouteLinkPtr *result;
		RoutingNodePtr nn;
		int cnt = 0;
		int to = destination->InternalIndex;
		nn = e->Nodes + to;
		while (nn->PreviousNode != NULL)
		  {
		      /* counting how many Links are into the Shortest Path solution */
		      cnt++;
		      nn = nn->PreviousNode;
		  }
		/* allocating the solution */
		result = malloc (sizeof (RouteLinkPtr) * cnt);
		k = cnt - 1;
		nn = e->Nodes + to;
		while (nn->PreviousNode != NULL)
		  {
		      /* inserting a Link into the solution */
		      result[k] = nn->xLink;
		      nn = nn->PreviousNode;
		      k--;
		  }
		solution =
		    add2multiSolution (multiSolution, multiSolution->From,
				       destination);
		build_solution (handle, options, graph, solution, result, cnt);
		/* testing for end (all destinations already reached) */

Changes to src/stored_procedures/stored_procedures.c.

56
57
58
59
60
61
62

63
64
65
66
67
68
69
....
1959
1960
1961
1962
1963
1964
1965
1966







































































































































































1967
1968
1969
1970
1971
1972
1973
1974
1975



1976









1977
1978
1979






1980
1981
1982
1983
1984
1985
1986
....
2191
2192
2193
2194
2195
2196
2197




































2198
2199
2200
2201
2202
2203
2204
....
2238
2239
2240
2241
2242
2243
2244

2245
2246
#include <spatialite/sqlite.h>
#include <spatialite/gaiageo.h>
#include <spatialite/gaiaaux.h>
#include <spatialite/gg_const.h>
#include <spatialite/gg_formats.h>
#include <spatialite/stored_procedures.h>
#include <spatialite_private.h>

#include <spatialite/debug.h>

#ifdef _WIN32
#define strcasecmp	_stricmp
#endif /* not WIN32 */

/* 64 bit integer: portable format for printf() */
................................................................................
      {
	  *out++ = *in++;
	  max--;
      }
    *out = '\0';
    return fail;
}








































































































































































SQLPROC_DECLARE int
gaia_sql_proc_execute (sqlite3 * handle, const void *ctx, const char *sql)
{
/* executing an already cooked SQL Body */
    const char *pSql = sql;
    sqlite3_stmt *stmt;
    int retval = 0;
    int n_stmts = 0;
    FILE *log = NULL;



    struct splite_internal_cache *cache = (struct splite_internal_cache *) ctx;










    if (cache != NULL)
      {






	  cache->SqlProcContinue = 1;
	  gaia_set_variant_null (cache->SqlProcRetValue);
	  log = cache->SqlProcLog;
      }

    if (log != NULL)
      {
................................................................................
		   "--==     SQL session end   =   %s   =   %d statement%s executed\n",
		   now, n_stmts, (n_stmts == 1) ? " was" : "s were");
	  sqlite3_free (now);
	  fprintf (log,
		   "--=========================================================================================\n\n\n");
	  fflush (log);
      }




































    return retval;
}

SQLPROC_DECLARE int
gaia_sql_proc_logfile (const void *ctx, const char *filepath, int append)
{
/* enabling/disabling the Logfile */
................................................................................
	fclose (cache->SqlProcLog);

/* resetting the current Logfile */
    len = strlen (filepath);
    cache->SqlProcLogfile = malloc (len + 1);
    strcpy (cache->SqlProcLogfile, filepath);
    cache->SqlProcLog = log;

    return 1;
}







>







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|







>
>
>
|
>
>
>
>
>
>
>
>
>



>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
....
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
....
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
....
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
#include <spatialite/sqlite.h>
#include <spatialite/gaiageo.h>
#include <spatialite/gaiaaux.h>
#include <spatialite/gg_const.h>
#include <spatialite/gg_formats.h>
#include <spatialite/stored_procedures.h>
#include <spatialite_private.h>
#include <spatialite.h>
#include <spatialite/debug.h>

#ifdef _WIN32
#define strcasecmp	_stricmp
#endif /* not WIN32 */

/* 64 bit integer: portable format for printf() */
................................................................................
      {
	  *out++ = *in++;
	  max--;
      }
    *out = '\0';
    return fail;
}

static sqlite3 *
do_clone_mem_db (sqlite3 * origin, void *cache, int mode)
{
/* opening a new connection by clonig a MEMORY MAIN DB */
    int ret;
    sqlite3_backup *backup;
    sqlite3 *handle;

    ret = sqlite3_open_v2 (":memory:", &handle, mode, NULL);
    if (ret != SQLITE_OK)
      {
	  spatialite_e ("SqlProcExec: sqlite3_open_v2 error: %s\n",
			sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return NULL;
      }
    backup = sqlite3_backup_init (handle, "main", origin, "main");
    if (!backup)
	goto stop;
    while (1)
      {
	  ret = sqlite3_backup_step (backup, 1024);
	  if (ret == SQLITE_DONE)
	      break;
      }
    ret = sqlite3_backup_finish (backup);
    if (ret != SQLITE_OK)
	goto stop;
    spatialite_internal_init (handle, cache);
    return handle;

  stop:
    sqlite3_close (handle);
    return NULL;
}

static int
do_clone_memory_db (sqlite3 * main_handle, sqlite3 * handle,
		    const char *db_name)
{
/* cloning back an eventual MEMORY DB */
    int ret;
    sqlite3_backup *backup;
    const char *db_path = sqlite3_db_filename (main_handle, db_name);
    if (db_path != NULL)
      {
	  if (strlen (db_path) > 0)
	      return 1;		/* not a MEMORY DB - quitting */
      }
    backup = sqlite3_backup_init (main_handle, db_name, handle, db_name);
    if (!backup)
	return 0;
    while (1)
      {
	  ret = sqlite3_backup_step (backup, 1024);
	  if (ret == SQLITE_DONE)
	      break;
      }
    ret = sqlite3_backup_finish (backup);
    if (ret != SQLITE_OK)
	return 0;
    return 1;
}

static sqlite3 *
do_open_new_connection (sqlite3 * origin, void *cache)
{
/* opening a new connection to the MAIN DB */
    int ret;
    sqlite3 *handle;
    int mem_db = 0;
    const char *db_path = sqlite3_db_filename (origin, "main");
    int rd_only = sqlite3_db_readonly (origin, "main");
    int mode = SQLITE_OPEN_READWRITE;
    if (rd_only)
	mode = SQLITE_OPEN_READONLY;
    if (db_path == NULL)
	mem_db = 1;
    else if (*db_path == '\0')
	mem_db = 1;
    if (mem_db)
	return do_clone_mem_db (origin, cache, mode);

/* creating a new connection */
    ret = sqlite3_open_v2 (db_path, &handle, mode, NULL);
    if (ret != SQLITE_OK)
      {
	  spatialite_e ("SqlProcExec: sqlite3_open_v2 error: %s\n",
			sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return NULL;
      }
    spatialite_internal_init (handle, cache);
    return handle;
}

static int
do_attach_all (sqlite3 * origin, sqlite3 * handle)
{
/* attaching all required database-files to the new connection */
    int ret;
    int i;
    char **results;
    int rows;
    int columns;

/* listing all database-files attached to the initial connection */
    ret =
	sqlite3_get_table (origin, "PRAGMA database_list", &results, &rows,
			   &columns, NULL);
    if (ret != SQLITE_OK)
	return 0;
    if (rows < 1)
	;
    else
      {
	  for (i = 1; i <= rows; i++)
	    {
		int mem_db = 0;
		char *sql;
		char *xdb_name;
		const char *db_name = results[(i * columns) + 1];
		const char *db_path = results[(i * columns) + 2];
		if (strcasecmp (db_name, "main") == 0)
		    continue;	/* ignoring MAIN */
		if (db_path == NULL)
		    mem_db = 1;
		else if (*db_path == '\0')
		    mem_db = 1;
		if (mem_db && strcasecmp (db_name, "temp") == 0)
		    goto skip_temp;		/* "temp" is already attached by default */
		xdb_name = gaiaDoubleQuotedSql (db_name);
		if (mem_db)
		    sql =
			sqlite3_mprintf ("ATTACH DATABASE %Q AS \"%s\"",
					 ":memory:", xdb_name);
		else
		    sql =
			sqlite3_mprintf ("ATTACH DATABASE %Q AS \"%s\"",
					 db_path, xdb_name);
		free (xdb_name);
		ret = sqlite3_exec (handle, sql, NULL, NULL, NULL);
		sqlite3_free (sql);
		if (ret != SQLITE_OK)
		  {
		      spatialite_e ("SqlProcExec: ATTACH DATABASE error: %s\n",
				    sqlite3_errmsg (handle));
		      sqlite3_free_table (results);
		      return 0;
		  }
	      skip_temp:
		if (mem_db)
		  {
		      if (!do_clone_memory_db (handle, origin, db_name))
			{
			    spatialite_e
				("SqlProcExec: ATTACH DATABASE error: %s\n",
				 sqlite3_errmsg (handle));
			    return 0;
			}
		  }
	    }
      }
    sqlite3_free_table (results);
    return 1;
}

SQLPROC_DECLARE int
gaia_sql_proc_execute (sqlite3 * main_handle, const void *ctx, const char *sql)
{
/* executing an already cooked SQL Body */
    const char *pSql = sql;
    sqlite3_stmt *stmt;
    int retval = 0;
    int n_stmts = 0;
    FILE *log = NULL;
    int ret;
    struct gaia_variant_value *ret_value;
    struct splite_internal_cache *main_cache =
	(struct splite_internal_cache *) ctx;

/* opening a new connection */
    struct splite_internal_cache *cache = spatialite_alloc_connection ();
    sqlite3 *handle = do_open_new_connection (main_handle, cache);
    if (handle == NULL)
	return 0;
/* attaching all the required database-files */
    if (!do_attach_all (main_handle, handle))
	return 0;

    if (cache != NULL)
      {
	  gaia_sql_proc_logfile (cache, main_cache->SqlProcLogfile,
				 main_cache->SqlProcLogfileAppend);
	  cache->gpkg_mode = main_cache->gpkg_mode;
	  cache->gpkg_amphibious_mode = main_cache->gpkg_amphibious_mode;
	  cache->decimal_precision = main_cache->decimal_precision;
	  cache->is_pause_enabled = main_cache->is_pause_enabled;
	  cache->SqlProcContinue = 1;
	  gaia_set_variant_null (cache->SqlProcRetValue);
	  log = cache->SqlProcLog;
      }

    if (log != NULL)
      {
................................................................................
		   "--==     SQL session end   =   %s   =   %d statement%s executed\n",
		   now, n_stmts, (n_stmts == 1) ? " was" : "s were");
	  sqlite3_free (now);
	  fprintf (log,
		   "--=========================================================================================\n\n\n");
	  fflush (log);
      }
/* updating the actual MEMORY DB (if any) */
    do_clone_memory_db (main_handle, handle, "main");

/* terminating the new connection */
    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
	spatialite_e ("SqlProcExec: sqlite3_close() error: %s\n",
		      sqlite3_errmsg (handle));

/* copying an eventual RETVALUE */
    ret_value = cache->SqlProcRetValue;
    switch (ret_value->dataType)
      {
      case SQLITE_INTEGER:
	  gaia_set_variant_int64 (main_cache->SqlProcRetValue,
				  ret_value->intValue);
	  break;
      case SQLITE_FLOAT:
	  gaia_set_variant_double (main_cache->SqlProcRetValue,
				   ret_value->dblValue);
	  break;
      case SQLITE_TEXT:
	  gaia_set_variant_text (main_cache->SqlProcRetValue,
				 ret_value->textValue, ret_value->size);
	  break;
      case SQLITE_BLOB:
	  gaia_set_variant_blob (main_cache->SqlProcRetValue,
				 ret_value->blobValue, ret_value->size);
	  break;
      case SQLITE_NULL:
      default:
	  gaia_set_variant_null (main_cache->SqlProcRetValue);
	  break;

      };
    spatialite_internal_cleanup (cache);
    return retval;
}

SQLPROC_DECLARE int
gaia_sql_proc_logfile (const void *ctx, const char *filepath, int append)
{
/* enabling/disabling the Logfile */
................................................................................
	fclose (cache->SqlProcLog);

/* resetting the current Logfile */
    len = strlen (filepath);
    cache->SqlProcLogfile = malloc (len + 1);
    strcpy (cache->SqlProcLogfile, filepath);
    cache->SqlProcLog = log;
    cache->SqlProcLogfileAppend = append;
    return 1;
}

Changes to test/check_stored_proc.c.

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
....
1170
1171
1172
1173
1174
1175
1176
































1177
1178
1179
1180
1181
1182
1183
....
1195
1196
1197
1198
1199
1200
1201
































1202
1203
1204
1205
1206
1207
1208
....
1296
1297
1298
1299
1300
1301
1302
































1303
1304
1305
1306
1307
1308
1309
....
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469




































1470
1471
1472
1473
1474
1475
1476
....
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
....
1764
1765
1766
1767
1768
1769
1770































































































































































































1771
1772
1773
1774
1775
1776
1777
....
1890
1891
1892
1893
1894
1895
1896




1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
	"--\n-- another comment\n--\n"
	"CREATE TABLE @output_2@ AS\n"
	"SELECT @col_4@, @col_5@ FROM @table_2@ WHERE @col_6@ = @value_2@;\n\n"
	".echo off\n\n" "--\n-- end comment\n--\n\n'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #1 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -12;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #1 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -13;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #1 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -14;
	  return 0;
      }
    sqlite3_free_table (results);

/* registering a second Stored Procedure */
    sql = "SELECT StoredProc_Register('proc_2', 'this is title two', "
	"SqlProc_FromText('SELECT @col_1@, @col_2@ FROM @table_1@ WHERE @col_3@ = @value_1@;'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #2 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -15;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #2 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -16;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #2 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -17;
	  return 0;
      }
    sqlite3_free_table (results);

/* updating Stored Procedure title - expected failure */
................................................................................

/* registering a Stored Procedure from filepath */
    sql = "SELECT StoredProc_Register('proc_file', 'this is title three', "
	"SqlProc_FromFile('./sqlproc_sample.txt'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #3 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -40;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #3 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -41;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #3 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -42;
	  return 0;
      }
    sqlite3_free_table (results);

    return 1;
................................................................................
	  fprintf (stderr, "StoredVar_Register() #4 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -100;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
































	  fprintf (stderr,
		   "StoredVar_Register() #4 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -101;
	  return 0;
      }
................................................................................
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "SqlProc_Execute() #4 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -103;
	  return 0;
































      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "SqlProc_Execute() #4 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
................................................................................
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "SqlProc_Return() #2 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -112;
	  return 0;
































      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "SqlProc_Return() #2 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
................................................................................

/* registering several Stored Procedure */
    sql = "SELECT StoredProc_Register('proc_ret1', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(NULL)'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #4 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -123;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret2', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(1234567890)'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #5 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -124;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret3', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(1234.5)'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #6 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -125;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret4', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(''test me now'')'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #7 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -126;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret5', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(x''0102030405060708090a0b0c0d0e0f'')'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #8 error: %s\n", err_msg);




































	  sqlite3_free (err_msg);
	  *retcode = -127;
	  return 0;
      }

/* testing StoredProc_Return(NULL) */
    sql = "SELECT StoredProc_Execute('proc_ret1');";
................................................................................
    const char *sql;
    int ret;
    char *err_msg = NULL;
    char **results;
    int rows;
    int columns;

/* registering several Stored Procedure */
    sql = "SELECT StoredProc_Register('proc_loop', 'this is a title', "
	"SqlProc_FromText('CREATE TABLE IF NOT EXISTS @TABLE@ (value INTEGER NOT NULL); "
	"INSERT INTO @TABLE@ VALUES (random()); "
	"SELECT CASE count(*) >= @LIMIT@ WHEN 1 THEN SqlProc_Return(0) ELSE SqlProc_Return(1) END FROM @TABLE@;'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #9 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -149;
	  return 0;
      }

/* testing StoredProc_ExecuteLoop() */
    sql =
................................................................................
      }
    if (atoi (*(results + 1)) != 250)
      {
	  fprintf (stderr, "StoredProc_ExecuteLoop() #2 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -155;
	  return 0;































































































































































































      }
    sqlite3_free_table (results);

    return 1;
}

#endif
................................................................................
/*tests: level 8 */
    if (!do_level8_tests (handle, &retcode))
	goto end;

/*tests: level 9 */
    if (!do_level9_tests (handle, &retcode))
	goto end;





  end:

#else
    if (old_SPATIALITE_SECURITY_ENV != NULL)
	old_SPATIALITE_SECURITY_ENV = NULL;	/* silencing stupid compiler warnings */
#endif /* end ICONV */

    sqlite3_close (handle);
    spatialite_cleanup_ex (cache);
    spatialite_shutdown ();
    return retcode;
}







|







|







|












|







|







|







 







|







|







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|









|









|









|





|



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>













210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
....
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
....
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
....
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
....
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
....
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
....
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
....
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
	"--\n-- another comment\n--\n"
	"CREATE TABLE @output_2@ AS\n"
	"SELECT @col_4@, @col_5@ FROM @table_2@ WHERE @col_6@ = @value_2@;\n\n"
	".echo off\n\n" "--\n-- end comment\n--\n\n'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #2 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -12;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #2 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -13;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #2 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -14;
	  return 0;
      }
    sqlite3_free_table (results);

/* registering a second Stored Procedure */
    sql = "SELECT StoredProc_Register('proc_2', 'this is title two', "
	"SqlProc_FromText('SELECT @col_1@, @col_2@ FROM @table_1@ WHERE @col_3@ = @value_1@;'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #3 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -15;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #3 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -16;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #3 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -17;
	  return 0;
      }
    sqlite3_free_table (results);

/* updating Stored Procedure title - expected failure */
................................................................................

/* registering a Stored Procedure from filepath */
    sql = "SELECT StoredProc_Register('proc_file', 'this is title three', "
	"SqlProc_FromFile('./sqlproc_sample.txt'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #4 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -40;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #4 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -41;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #4 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -42;
	  return 0;
      }
    sqlite3_free_table (results);

    return 1;
................................................................................
	  fprintf (stderr, "StoredVar_Register() #4 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -100;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {

/* enabling a SQL Logfile - append mode */
	  sql = "SELECT SqlProc_SetLogfile('./sql_logfile', 1)";
	  ret =
	      sqlite3_get_table (handle, sql, &results, &rows, &columns,
				 &err_msg);
	  if (ret != SQLITE_OK)
	    {
		fprintf (stderr, "SqlProc_SetLogfile() #3 error: %s\n",
			 err_msg);
		sqlite3_free (err_msg);
		*retcode = -91;
		return 0;
	    }
	  if (rows != 1 || columns != 1)
	    {
		fprintf (stderr,
			 "SqlProc_SetLogfile() #3 error: rows=%d columns=%d\n",
			 rows, columns);
		sqlite3_free_table (results);
		*retcode = -92;
		return 0;
	    }
	  if (atoi (*(results + 1)) != 1)
	    {
		fprintf (stderr,
			 "SqlProc_SetLogfile() #3 unexpected failure\n");
		sqlite3_free_table (results);
		*retcode = -93;
		return 0;
	    }
	  sqlite3_free_table (results);
	  fprintf (stderr,
		   "StoredVar_Register() #4 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -101;
	  return 0;
      }
................................................................................
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "SqlProc_Execute() #4 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -103;
	  return 0;

/* enabling a SQL Logfile - append mode */
	  sql = "SELECT SqlProc_SetLogfile('./sql_logfile', 1)";
	  ret =
	      sqlite3_get_table (handle, sql, &results, &rows, &columns,
				 &err_msg);
	  if (ret != SQLITE_OK)
	    {
		fprintf (stderr, "SqlProc_SetLogfile() #4 error: %s\n",
			 err_msg);
		sqlite3_free (err_msg);
		*retcode = -91;
		return 0;
	    }
	  if (rows != 1 || columns != 1)
	    {
		fprintf (stderr,
			 "SqlProc_SetLogfile() #4 error: rows=%d columns=%d\n",
			 rows, columns);
		sqlite3_free_table (results);
		*retcode = -92;
		return 0;
	    }
	  if (atoi (*(results + 1)) != 1)
	    {
		fprintf (stderr,
			 "SqlProc_SetLogfile() #4 unexpected failure\n");
		sqlite3_free_table (results);
		*retcode = -93;
		return 0;
	    }
	  sqlite3_free_table (results);
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "SqlProc_Execute() #4 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
................................................................................
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "SqlProc_Return() #2 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -112;
	  return 0;

/* enabling a SQL Logfile - append mode */
	  sql = "SELECT SqlProc_SetLogfile('./sql_logfile', 1)";
	  ret =
	      sqlite3_get_table (handle, sql, &results, &rows, &columns,
				 &err_msg);
	  if (ret != SQLITE_OK)
	    {
		fprintf (stderr, "SqlProc_SetLogfile() #5 error: %s\n",
			 err_msg);
		sqlite3_free (err_msg);
		*retcode = -91;
		return 0;
	    }
	  if (rows != 1 || columns != 1)
	    {
		fprintf (stderr,
			 "SqlProc_SetLogfile() #5 error: rows=%d columns=%d\n",
			 rows, columns);
		sqlite3_free_table (results);
		*retcode = -92;
		return 0;
	    }
	  if (atoi (*(results + 1)) != 1)
	    {
		fprintf (stderr,
			 "SqlProc_SetLogfile() #5 unexpected failure\n");
		sqlite3_free_table (results);
		*retcode = -93;
		return 0;
	    }
	  sqlite3_free_table (results);
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "SqlProc_Return() #2 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
................................................................................

/* registering several Stored Procedure */
    sql = "SELECT StoredProc_Register('proc_ret1', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(NULL)'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #5 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -123;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret2', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(1234567890)'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #6 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -124;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret3', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(1234.5)'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #7 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -125;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret4', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(''test me now'')'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #8 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -126;
	  return 0;
      }
    sql = "SELECT StoredProc_Register('proc_ret5', 'this is a title', "
	"SqlProc_FromText('SELECT StoredProc_Return(x''0102030405060708090a0b0c0d0e0f'')'));";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #9 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -127;
	  return 0;
      }

/* enabling a SQL Logfile - append mode */
    sql = "SELECT SqlProc_SetLogfile('./sql_logfile', 1)";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "SqlProc_SetLogfile() #6 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -91;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "SqlProc_SetLogfile() #6 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -92;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "SqlProc_SetLogfile() #6 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -93;
	  return 0;
      }
    sqlite3_free_table (results);
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #10 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -127;
	  return 0;
      }

/* testing StoredProc_Return(NULL) */
    sql = "SELECT StoredProc_Execute('proc_ret1');";
................................................................................
    const char *sql;
    int ret;
    char *err_msg = NULL;
    char **results;
    int rows;
    int columns;

/* registering a Stored Procedure */
    sql = "SELECT StoredProc_Register('proc_loop', 'this is a title', "
	"SqlProc_FromText('CREATE TABLE IF NOT EXISTS @TABLE@ (value INTEGER NOT NULL); "
	"INSERT INTO @TABLE@ VALUES (random()); "
	"SELECT CASE count(*) >= @LIMIT@ WHEN 1 THEN SqlProc_Return(0) ELSE SqlProc_Return(1) END FROM @TABLE@;'))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #11 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -149;
	  return 0;
      }

/* testing StoredProc_ExecuteLoop() */
    sql =
................................................................................
      }
    if (atoi (*(results + 1)) != 250)
      {
	  fprintf (stderr, "StoredProc_ExecuteLoop() #2 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -155;
	  return 0;
      }
    sqlite3_free_table (results);

    return 1;
}

static int
do_level10_tests (sqlite3 * handle, int *retcode)
{
/* performing Level 10 tests - ATTACHED DBs */
    const char *sql;
    int ret;
    char *err_msg = NULL;
    char **results;
    int rows;
    int columns;

/* attaching an external database */
    ret =
	sqlite3_exec (handle, "ATTACH DATABASE './elba-sezcen.sqlite' AS elba",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "ATTACH DATABASE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -160;
      }

/* attaching a :memory: database */
    ret =
	sqlite3_exec (handle, "ATTACH DATABASE ':memory:' AS mem", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "ATTACH DATABASE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -161;
      }

/* creating a Temporary Table */
    ret = sqlite3_exec (handle, "CREATE TEMPORARY TABLE tmp_sez AS "
			"SELECT sez2001, NULL AS area, NULL AS perimeter, geometry "
			"FROM elba.sezcen_2001", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE TEMPORARY TABLE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -162;
      }

/* creating a Table on the "mem" DB */
    ret = sqlite3_exec (handle, "CREATE TABLE mem.sez AS "
			"SELECT sez2011, NULL AS area, NULL AS perimeter, geometry "
			"FROM elba.sezcen_2011", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE MEM TABLE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -163;
      }

/* creating a dummy Table */
    ret = sqlite3_exec (handle, "CREATE TABLE dummy_sez AS "
			"SELECT sez2001, NULL AS area, NULL AS perimeter, geometry "
			"FROM elba.sezcen_2001", 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 -164;
      }

/* enabling a SQL Logfile - append mode */
    sql = "SELECT SqlProc_SetLogfile('./sql_attach_logfile', 1)";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "SqlProc_SetLogfile() #7 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -165;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "SqlProc_SetLogfile() #7 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -166;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "SqlProc_SetLogfile() #7 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -167;
	  return 0;
      }
    sqlite3_free_table (results);

/* registering a Stored Procedure */
    sql = "SELECT StoredProc_Register('attached', 'this is a title', "
	"SqlProc_FromText('--\n-- comment\n--\n"
	"SELECT DropTable(NULL, ''dummy_sez'', 1);\n\n"
	"--\n-- a second comment\n--\n"
	"UPDATE temp.tmp_sez SET area = ST_Area(geometry), perimeter = ST_Perimeter(geometry);\n\n"
	"--\n-- a third comment\n--\n"
	"CREATE TABLE dummy_sez (\n"
	"\tsection INTEGER NOT NULL PRIMARY KEY,\n"
	"\tarea DOUBLE NOT NULL,\n"
	"\tperimeter DOUBLE NOT NULL);\n\n"
	"SELECT AddGeometryColumn(''dummy_sez'', ''geom'', 32632, ''POLYGON'', ''XY'');\n\n"
	"--\n-- fourth comment\n--\n"
	"INSERT INTO dummy_sez\n"
	"SELECT e.sez2001, t.area, t.perimeter, e.geometry\nFROM elba.sezcen_2001 AS e\n"
	"JOIN temp.tmp_sez AS t ON (e.sez2001 = t.sez2001)\n"
	"WHERE t.area > @MAX@;\n\n"
	"--\n-- fifth comment\n--\n"
	"SELECT section, area, perimeter, geom\nFROM dummy_sez;\n\n"
	"SELECT DropTable(''MAIN'', ''dummy_sez'');\n\n"
	"UPDATE mem.sez SET area = ST_Area(geometry), perimeter = ST_Perimeter(geometry);\n\n"
	"--\n-- a sixth comment\n--\n"
	"CREATE TABLE dummy_sez (\n"
	"\tsection INTEGER NOT NULL PRIMARY KEY,\n"
	"\tarea DOUBLE NOT NULL,\n"
	"\tperimeter DOUBLE NOT NULL);\n\n"
	"SELECT AddGeometryColumn(''dummy_sez'', ''geom'', 32632, ''MULTIPOLYGON'', ''XY'');\n\n"
	"--\n-- fourth comment\n--\n"
	"INSERT INTO dummy_sez\n"
	"SELECT e.sez2011, m.area, m.perimeter, e.geometry\nFROM elba.sezcen_2011 AS e\n"
	"JOIN mem.sez AS m ON (e.sez2011 = m.sez2011)\n"
	"WHERE m.area < @MAX@;\n\n"
	"--\n-- seventh comment\n--\n"
	"SELECT section, area, perimeter, geom\nFROM dummy_sez;\n\n"
	"SELECT DropTable(NULL, ''dummy_sez'');\n\n" "--\n-- end job\n--\n'))";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Register() #12 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -168;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Register() #12 error: rows=%d columns=%d\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  *retcode = -169;
	  return 0;
      }
    if (atoi (*(results + 1)) != 1)
      {
	  fprintf (stderr, "StoredProc_Register() #12 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -170;
	  return 0;
      }
    sqlite3_free_table (results);

/* testing StoredProc_Execute() */
    sql = "SELECT StoredProc_Execute('attached', '@MAX@=4000000');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "StoredProc_Execute() #3 error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  *retcode = -171;
	  return 0;
      }
    if (rows != 1 || columns != 1)
      {
	  fprintf (stderr,
		   "StoredProc_Execute() #3 error: rows=%d columns=%d\n", rows,
		   columns);
	  sqlite3_free_table (results);
	  *retcode = -172;
	  return 0;
      }
    if (*(results + 1) != NULL)
      {
	  fprintf (stderr, "StoredProc_Execute() #3 unexpected failure\n");
	  sqlite3_free_table (results);
	  *retcode = -173;
	  return 0;
      }
    sqlite3_free_table (results);

    return 1;
}

#endif
................................................................................
/*tests: level 8 */
    if (!do_level8_tests (handle, &retcode))
	goto end;

/*tests: level 9 */
    if (!do_level9_tests (handle, &retcode))
	goto end;

/*tests: level 10 */
    if (!do_level10_tests (handle, &retcode))
	goto end;

  end:

#else
    if (old_SPATIALITE_SECURITY_ENV != NULL)
	old_SPATIALITE_SECURITY_ENV = NULL;	/* silencing stupid compiler warnings */
#endif /* end ICONV */

    sqlite3_close (handle);
    spatialite_cleanup_ex (cache);
    spatialite_shutdown ();
    return retcode;
}

Changes to test/elba-sezcen.sqlite.

cannot compute difference between binary files