author | Mahlon E. Smith <mahlon@laika.com> |
Wed, 14 Sep 2011 16:49:28 -0700 | |
changeset 4 | 5701b7859a31 |
child 7 | e4f1a551d45c |
permissions | -rw-r--r-- |
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 */ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
48 |
if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* name not set */ |
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(); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
58 |
if ( version != DB_VERSION ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
59 |
debug( 1, LOC, "Database version mismatch: expected %hu, got %d.\n", |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
60 |
DB_VERSION, version ); |
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 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
74 |
return( SQLITE_OK ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
75 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
76 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
77 |
|
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 |
* 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
|
80 |
* 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
|
81 |
* |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
82 |
*/ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
83 |
int |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
84 |
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
|
85 |
{ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
86 |
unsigned short int i = 0; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
87 |
char user_pragma[30]; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
88 |
char sql_file[30]; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
89 |
char *upgrade_sql = NULL; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
90 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
91 |
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
|
92 |
if ( i == 1 ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
93 |
debug( 1, LOC, "Initializing new database.\n" ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
94 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
95 |
else { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
96 |
debug( 1, LOC, "Upgrading database version from %hu to %hu\n", current_version, i ); |
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 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
99 |
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
|
100 |
upgrade_sql = slurp_file( sql_file ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
101 |
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
|
102 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
103 |
/* 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
|
104 |
if ( sqlite3_exec( v.db, upgrade_sql, NULL, NULL, NULL ) != SQLITE_OK ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
105 |
debug( 1, LOC, "Error upgrading 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
|
106 |
return( sqlite3_errcode(v.db) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
107 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
108 |
free( upgrade_sql ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
109 |
upgrade_sql = NULL; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
110 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
111 |
/* 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
|
112 |
current_version = i; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
113 |
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
|
114 |
if ( sqlite3_exec( v.db, user_pragma, NULL, NULL, NULL ) != SQLITE_OK ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
115 |
debug( 1, LOC, "Error upgrading 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
|
116 |
return( sqlite3_errcode(v.db) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
117 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
118 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
119 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
120 |
return( SQLITE_OK ); |
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 |
|
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 |
* 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
|
126 |
* 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
|
127 |
* |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
128 |
*/ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
129 |
short int |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
130 |
db_version( void ) |
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 |
struct sqlite3_stmt *stmt; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
133 |
int version = -1; |
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 |
if ( sqlite3_prepare_v2( v.db, "PRAGMA user_version", -1, &stmt, NULL ) != SQLITE_OK ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
136 |
debug( 1, LOC, "Error finding DB version: %s\n", sqlite3_errmsg(v.db) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
137 |
return( -1 ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
138 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
139 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
140 |
if ( sqlite3_step( stmt ) == SQLITE_ROW ) |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
141 |
version = sqlite3_column_int( stmt, 0 ); |
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 |
sqlite3_finalize( stmt ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff
changeset
|
144 |
return version; |
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 |