db.c
author Mahlon E. Smith <mahlon@laika.com>
Mon, 31 Oct 2011 17:17:07 -0700
changeset 13 23a242d7b7fa
parent 10 d07309450285
child 14 51eb85ae4de4
permissions -rw-r--r--
1st iteration of volta actually doing something. Process the request, find the best matching rule, and rewrite the request. Without the DB queries, volta was parsing over 750k requests a second. Currently, it's down to 129.5 with 1161 rules in place. Yikes. I may need to re-evaluate some design choices here.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     1
/* vim: set noet nosta sw=4 ts=4 ft=c : */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     2
/*
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     4
All rights reserved.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     5
Redistribution and use in source and binary forms, with or without
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     6
modification, are permitted provided that the following conditions are met:
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     7
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     8
    * Redistributions of source code must retain the above copyright
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     9
      notice, this list of conditions and the following disclaimer.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    10
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    11
    * Redistributions in binary form must reproduce the above copyright
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    12
      notice, this list of conditions and the following disclaimer in the
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    13
      documentation and/or other materials provided with the distribution.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    14
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    15
    * Neither the name of Mahlon E. Smith nor the names of his
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    16
      contributors may be used to endorse or promote products derived
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
      from this software without specific prior written permission.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    18
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    20
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    23
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    24
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    29
*/
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    30
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    31
#include "volta.h"
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
#include "db.h"
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    33
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    34
const unsigned short int DB_VERSION = 1;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    36
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    37
/*
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
 * Connect to the database specified in the 'v' global struct,
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
 * and populate v.db with a handle to it.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
 *
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    41
 * If required, automatically handle initializing/upgrading a DB.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    42
 *
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    43
 */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
int
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
db_attach( void )
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    46
{
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
	if ( v.db != NULL )          return( SQLITE_OK );    /* already attached */
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    48
	if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* db filename not set? */
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
	debug( 2, LOC, "Attaching to database '%s'\n", v.dbname );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    51
	if ( sqlite3_open( v.dbname, &v.db ) != SQLITE_OK ) {
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
		debug( 1, LOC, "Error when attaching to database: %s\n", sqlite3_errmsg(v.db) );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
		return( sqlite3_errcode(v.db) );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
	}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
	/* check DB version */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
	unsigned short int version = db_version();
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    58
	debug( 2, LOC, "Database version: %d\n", version );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
	if ( version != DB_VERSION ) {
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    60
		debug( 2, LOC, "Database version mismatch: expected %hu\n", DB_VERSION );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
		/* We're in need of a DB initialization, or just behind.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
		 * Attempt to "stair step" upgrade to the current version. */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
		if ( version < DB_VERSION ) {
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
			return( db_upgrade(version) );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    68
		/* Something else is wack. */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
		else {
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
			return( SQLITE_ERROR );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
	}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    74
	/* initialize prepared statements */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    75
	if ( prepare_statements() != 0 ) return SQLITE_ERROR;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    76
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
	return( SQLITE_OK );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
/*
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
 * Perform automatic stair-step upgrades of DB versions
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
 * to get up to the current version expected from code.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
 *
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
 */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
int
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
db_upgrade( unsigned short int current_version )
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    88
{
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    89
	unsigned short int i = 0;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
	char user_pragma[30];
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
	char sql_file[30];
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
	char *upgrade_sql = NULL;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    94
	for ( i = current_version + 1; i <= DB_VERSION; i++ ) {
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
		if ( i == 1 ) {
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    96
			debug( 2, LOC, "Initializing new database.\n" );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    97
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    98
		else {
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    99
			debug( 2, LOC, "Upgrading database version from %hu to %hu\n", current_version, i );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   101
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
		sprintf( sql_file, "sql/%d.sql", i );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   103
		upgrade_sql = slurp_file( sql_file );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   104
		if ( upgrade_sql == NULL ) return( SQLITE_ERROR );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   105
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   106
		/* If there is SQL to execute, do so and then reset for more */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   107
		if ( sqlite3_exec( v.db, upgrade_sql, NULL, NULL, NULL ) != SQLITE_OK ) {
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   108
			debug( 2, LOC, "Error %s database: %s\n",
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   109
					(i == 1 ? "initalizing" : "upgrading"), sqlite3_errmsg(v.db) );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
			return( sqlite3_errcode(v.db) );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
		}
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 7
diff changeset
   112
		free( upgrade_sql ), upgrade_sql = NULL;
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   113
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   114
		/* update version metadata in DB if update was successful */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
		current_version = i;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   116
		sprintf( user_pragma, "PRAGMA user_version = %hu;", current_version );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   117
		if ( sqlite3_exec( v.db, user_pragma, NULL, NULL, NULL ) != SQLITE_OK ) {
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   118
			debug( 2, LOC, "Error setting version: %s\n", sqlite3_errmsg(v.db) );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   119
			return( sqlite3_errcode(v.db) );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   120
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   121
	}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   122
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   123
	return( SQLITE_OK );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   124
}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   125
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   126
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   127
/*
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   128
 * Fetch and return the database's current version. or -1 on error.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   129
 * The database should already be attached before calling this function.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   130
 *
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   131
 */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   132
short int
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   133
db_version( void )
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   134
{
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   135
	struct sqlite3_stmt *stmt;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   136
	int version = -1;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   137
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   138
	if ( sqlite3_prepare_v2( v.db, "PRAGMA user_version", -1, &stmt, NULL ) != SQLITE_OK ) {
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   139
		debug( 2, LOC, "Error finding DB version: %s\n", sqlite3_errmsg(v.db) );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   140
		return( -1 );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   141
	}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   142
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   143
	if ( sqlite3_step( stmt ) == SQLITE_ROW )
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   144
		version = sqlite3_column_int( stmt, 0 );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   145
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   146
	sqlite3_finalize( stmt );
7
e4f1a551d45c Fine tuning for database initialization/upgrades.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   147
	return( version );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   148
}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   149
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   150
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   151
/*
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   152
 * Initialize the DB statements, returning 0 on success.
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   153
 *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   154
 */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   155
unsigned short int
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   156
prepare_statements( void )
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   157
{
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   158
	unsigned short int rv = 0;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   159
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   160
	rv = rv + sqlite3_prepare_v2( v.db, DBSQL_GET_REWRITE_RULE, -1, &v.db_stmt.get_rewrite_rule, NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   161
	if ( rv != 0 )
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   162
		debug( 2, LOC, "Error preparing DB statement \"%s\": %s\n",
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   163
				DBSQL_GET_REWRITE_RULE, sqlite3_errmsg(v.db) );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   164
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   165
	rv = rv + sqlite3_prepare_v2( v.db, DBSQL_MATCH_REQUEST, -1, &v.db_stmt.match_request, NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   166
	if ( rv != 0 )
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   167
		debug( 2, LOC, "Error preparing DB statement \"%s\": %s\n",
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   168
				DBSQL_MATCH_REQUEST, sqlite3_errmsg(v.db) );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   169
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   170
	return( rv );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   171
}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   172
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   173
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   174
/*
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   175
 * Initialize and return a pointer to a new rewrite object.
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   176
 *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   177
 */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   178
rewrite *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   179
init_rewrite( void )
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   180
{
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   181
	rewrite *p_rewrite = NULL;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   182
	if ( (p_rewrite = malloc( sizeof(rewrite) )) == NULL ) {
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   183
		debug( 5, LOC, "Unable to allocate memory for rewrite struct: %s\n", strerror(errno) );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   184
		return( NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   185
	}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   186
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   187
	p_rewrite->scheme  = NULL;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   188
	p_rewrite->host    = NULL;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   189
	p_rewrite->path    = NULL;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   190
	p_rewrite->port    = 0;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   191
	p_rewrite->redir   = 0;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   192
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   193
	return( p_rewrite );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   194
}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   195
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   196
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   197
#define COPY_REWRITE_ROW( INDEX ) copy_string_token( \
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   198
			(char *)sqlite3_column_text( v.db_stmt.get_rewrite_rule, INDEX ),\
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   199
			sqlite3_column_bytes( v.db_stmt.get_rewrite_rule, INDEX ))
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   200
/*
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   201
 * Given a request struct pointer, try and find the best matching
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   202
 * rewrite rule, returning a pointer to a rewrite struct.
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   203
 *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   204
 */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   205
rewrite *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   206
prepare_rewrite( request *p_request )
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   207
{
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   208
	if ( p_request == NULL ) return( NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   209
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   210
	unsigned short int rewrite_id = 0;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   211
	rewrite *p_rewrite = init_rewrite();
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   212
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   213
	sqlite3_bind_text( v.db_stmt.match_request, 3, p_request->tld,       -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   214
	sqlite3_bind_text( v.db_stmt.match_request, 1, p_request->scheme,    -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   215
	sqlite3_bind_text( v.db_stmt.match_request, 2, p_request->host,      -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   216
	sqlite3_bind_text( v.db_stmt.match_request, 3, p_request->tld,       -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   217
	sqlite3_bind_text( v.db_stmt.match_request, 4, p_request->path,      -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   218
	sqlite3_bind_int(  v.db_stmt.match_request, 5, p_request->port );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   219
	/*
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   220
	sqlite3_bind_text( v.db_stmt.match_request, 6, NULL, -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   221
	sqlite3_bind_text( v.db_stmt.match_request, 6, p_request->client_ip, -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   222
	*/
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   223
	sqlite3_bind_text( v.db_stmt.match_request, 7, p_request->user,      -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   224
	sqlite3_bind_text( v.db_stmt.match_request, 8, p_request->method,    -1, SQLITE_STATIC );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   225
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   226
	switch ( sqlite3_step( v.db_stmt.match_request )) {
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   227
		case SQLITE_ROW:
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   228
			rewrite_id = sqlite3_column_int( v.db_stmt.match_request, 0 );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   229
			break;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   230
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   231
		case SQLITE_DONE:
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   232
			break;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   233
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   234
		default:
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   235
			return( NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   236
	}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   237
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   238
	/* FIXME: CHECK for rewrite_rule being NULL on successful match, emit warning, continue */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   239
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   240
	/* return early if we didn't get a matching request */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   241
	if ( rewrite_id == 0 ) return( NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   242
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   243
	/* pull the rewrite data, populate the struct.  only one
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   244
	 * row should ever be returned for this. */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   245
	sqlite3_bind_int( v.db_stmt.get_rewrite_rule, 1, rewrite_id );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   246
	switch ( sqlite3_step( v.db_stmt.get_rewrite_rule )) {
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   247
		case SQLITE_ROW:
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   248
			p_rewrite->scheme = COPY_REWRITE_ROW( 1 );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   249
			p_rewrite->host   = COPY_REWRITE_ROW( 2 );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   250
			p_rewrite->path   = COPY_REWRITE_ROW( 3 );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   251
			p_rewrite->port   = sqlite3_column_int( v.db_stmt.get_rewrite_rule, 4 );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   252
			p_rewrite->redir  = sqlite3_column_int( v.db_stmt.get_rewrite_rule, 5 );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   253
			break;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   254
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   255
		case SQLITE_DONE:
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   256
			break;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   257
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   258
		default:
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   259
			return( NULL );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   260
	}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   261
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   262
	return( p_rewrite );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   263
}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   264
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   265
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   266
/*
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   267
 * Release memory used by the rewrite struct and
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   268
 * reset prepared statements.
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   269
 *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   270
 */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   271
void
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   272
finish_rewrite( rewrite *p_rewrite )
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   273
{
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   274
	sqlite3_reset( v.db_stmt.get_rewrite_rule );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   275
	sqlite3_reset( v.db_stmt.match_request );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   276
	sqlite3_clear_bindings( v.db_stmt.get_rewrite_rule );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   277
	sqlite3_clear_bindings( v.db_stmt.match_request );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   278
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   279
	if ( p_rewrite == NULL ) return;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   280
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   281
	free( p_rewrite->scheme );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   282
	free( p_rewrite->host );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   283
	free( p_rewrite->path );
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   284
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   285
	free( p_rewrite ), p_rewrite = NULL;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   286
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   287
	return;
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   288
}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   289