db.c
changeset 4 5701b7859a31
child 7 e4f1a551d45c
equal deleted inserted replaced
3:97f767832c52 4:5701b7859a31
       
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
       
     2 /*
       
     3 Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
       
     4 All rights reserved.
       
     5 Redistribution and use in source and binary forms, with or without
       
     6 modification, are permitted provided that the following conditions are met:
       
     7 
       
     8     * Redistributions of source code must retain the above copyright
       
     9       notice, this list of conditions and the following disclaimer.
       
    10 
       
    11     * Redistributions in binary form must reproduce the above copyright
       
    12       notice, this list of conditions and the following disclaimer in the
       
    13       documentation and/or other materials provided with the distribution.
       
    14 
       
    15     * Neither the name of Mahlon E. Smith nor the names of his
       
    16       contributors may be used to endorse or promote products derived
       
    17       from this software without specific prior written permission.
       
    18 
       
    19 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
       
    20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
       
    23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29 */
       
    30 
       
    31 #include "volta.h"
       
    32 #include "db.h"
       
    33 
       
    34 const unsigned short int DB_VERSION = 1;
       
    35 
       
    36 
       
    37 /*
       
    38  * Connect to the database specified in the 'v' global struct,
       
    39  * and populate v.db with a handle to it.
       
    40  *
       
    41  * If required, automatically handle initializing/upgrading a DB.
       
    42  *
       
    43  */
       
    44 int
       
    45 db_attach( void )
       
    46 {
       
    47 	if ( v.db != NULL )          return( SQLITE_OK );    /* already attached */
       
    48 	if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* name not set */
       
    49 
       
    50 	debug( 2, LOC, "Attaching to database '%s'\n", v.dbname );
       
    51 	if ( sqlite3_open( v.dbname, &v.db ) != SQLITE_OK ) {
       
    52 		debug( 1, LOC, "Error when attaching to database: %s\n", sqlite3_errmsg(v.db) );
       
    53 		return( sqlite3_errcode(v.db) );
       
    54 	}
       
    55 
       
    56 	/* check DB version */
       
    57 	unsigned short int version = db_version();
       
    58 	if ( version != DB_VERSION ) {
       
    59 		debug( 1, LOC, "Database version mismatch: expected %hu, got %d.\n",
       
    60 				DB_VERSION, version );
       
    61 
       
    62 		/* We're in need of a DB initialization, or just behind.
       
    63 		 * Attempt to "stair step" upgrade to the current version. */
       
    64 		if ( version < DB_VERSION ) {
       
    65 			return( db_upgrade(version) );
       
    66 		}
       
    67 
       
    68 		/* Something else is wack. */
       
    69 		else {
       
    70 			return( SQLITE_ERROR );
       
    71 		}
       
    72 	}
       
    73 
       
    74 	return( SQLITE_OK );
       
    75 }
       
    76 
       
    77 
       
    78 /*
       
    79  * Perform automatic stair-step upgrades of DB versions
       
    80  * to get up to the current version expected from code.
       
    81  *
       
    82  */
       
    83 int
       
    84 db_upgrade( unsigned short int current_version )
       
    85 {
       
    86 	unsigned short int i = 0;
       
    87 	char user_pragma[30];
       
    88 	char sql_file[30];
       
    89 	char *upgrade_sql = NULL;
       
    90 
       
    91 	for ( i = current_version + 1; i <= DB_VERSION; i++ ) {
       
    92 		if ( i == 1 ) {
       
    93 			debug( 1, LOC, "Initializing new database.\n" );
       
    94 		}
       
    95 		else {
       
    96 			debug( 1, LOC, "Upgrading database version from %hu to %hu\n", current_version, i );
       
    97 		}
       
    98 
       
    99 		sprintf( sql_file, "sql/%d.sql", i );
       
   100 		upgrade_sql = slurp_file( sql_file );
       
   101 		if ( upgrade_sql == NULL ) return( SQLITE_ERROR );
       
   102 
       
   103 		/* If there is SQL to execute, do so and then reset for more */
       
   104 		if ( sqlite3_exec( v.db, upgrade_sql, NULL, NULL, NULL ) != SQLITE_OK ) {
       
   105 			debug( 1, LOC, "Error upgrading database: %s\n", sqlite3_errmsg(v.db) );
       
   106 			return( sqlite3_errcode(v.db) );
       
   107 		}
       
   108 		free( upgrade_sql );
       
   109 		upgrade_sql = NULL;
       
   110 
       
   111 		/* update version metadata in DB if update was successful */
       
   112 		current_version = i;
       
   113 		sprintf( user_pragma, "PRAGMA user_version = %hu;", current_version );
       
   114 		if ( sqlite3_exec( v.db, user_pragma, NULL, NULL, NULL ) != SQLITE_OK ) {
       
   115 			debug( 1, LOC, "Error upgrading database: %s\n", sqlite3_errmsg(v.db) );
       
   116 			return( sqlite3_errcode(v.db) );
       
   117 		}
       
   118 	}
       
   119 
       
   120 	return( SQLITE_OK );
       
   121 }
       
   122 
       
   123 
       
   124 /*
       
   125  * Fetch and return the database's current version. or -1 on error.
       
   126  * The database should already be attached before calling this function.
       
   127  *
       
   128  */
       
   129 short int
       
   130 db_version( void )
       
   131 {
       
   132 	struct sqlite3_stmt *stmt;
       
   133 	int version = -1;
       
   134 
       
   135 	if ( sqlite3_prepare_v2( v.db, "PRAGMA user_version", -1, &stmt, NULL ) != SQLITE_OK ) {
       
   136 		debug( 1, LOC, "Error finding DB version: %s\n", sqlite3_errmsg(v.db) );
       
   137 		return( -1 );
       
   138 	}
       
   139 
       
   140 	if ( sqlite3_step( stmt ) == SQLITE_ROW )
       
   141 		version = sqlite3_column_int( stmt, 0 );
       
   142 
       
   143 	sqlite3_finalize( stmt );
       
   144 	return version;
       
   145 }
       
   146