db.c
changeset 7 e4f1a551d45c
parent 4 5701b7859a31
child 10 d07309450285
equal deleted inserted replaced
6:85718bc3ff47 7:e4f1a551d45c
    43  */
    43  */
    44 int
    44 int
    45 db_attach( void )
    45 db_attach( void )
    46 {
    46 {
    47 	if ( v.db != NULL )          return( SQLITE_OK );    /* already attached */
    47 	if ( v.db != NULL )          return( SQLITE_OK );    /* already attached */
    48 	if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* name not set */
    48 	if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* db filename not set? */
    49 
    49 
    50 	debug( 2, LOC, "Attaching to database '%s'\n", v.dbname );
    50 	debug( 2, LOC, "Attaching to database '%s'\n", v.dbname );
    51 	if ( sqlite3_open( v.dbname, &v.db ) != SQLITE_OK ) {
    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) );
    52 		debug( 1, LOC, "Error when attaching to database: %s\n", sqlite3_errmsg(v.db) );
    53 		return( sqlite3_errcode(v.db) );
    53 		return( sqlite3_errcode(v.db) );
    54 	}
    54 	}
    55 
    55 
    56 	/* check DB version */
    56 	/* check DB version */
    57 	unsigned short int version = db_version();
    57 	unsigned short int version = db_version();
       
    58 	debug( 2, LOC, "Database version: %d\n", version );
    58 	if ( version != DB_VERSION ) {
    59 	if ( version != DB_VERSION ) {
    59 		debug( 1, LOC, "Database version mismatch: expected %hu, got %d.\n",
    60 		debug( 2, LOC, "Database version mismatch: expected %hu\n", DB_VERSION );
    60 				DB_VERSION, version );
       
    61 
    61 
    62 		/* We're in need of a DB initialization, or just behind.
    62 		/* We're in need of a DB initialization, or just behind.
    63 		 * Attempt to "stair step" upgrade to the current version. */
    63 		 * Attempt to "stair step" upgrade to the current version. */
    64 		if ( version < DB_VERSION ) {
    64 		if ( version < DB_VERSION ) {
    65 			return( db_upgrade(version) );
    65 			return( db_upgrade(version) );
    88 	char sql_file[30];
    88 	char sql_file[30];
    89 	char *upgrade_sql = NULL;
    89 	char *upgrade_sql = NULL;
    90 
    90 
    91 	for ( i = current_version + 1; i <= DB_VERSION; i++ ) {
    91 	for ( i = current_version + 1; i <= DB_VERSION; i++ ) {
    92 		if ( i == 1 ) {
    92 		if ( i == 1 ) {
    93 			debug( 1, LOC, "Initializing new database.\n" );
    93 			debug( 2, LOC, "Initializing new database.\n" );
    94 		}
    94 		}
    95 		else {
    95 		else {
    96 			debug( 1, LOC, "Upgrading database version from %hu to %hu\n", current_version, i );
    96 			debug( 2, LOC, "Upgrading database version from %hu to %hu\n", current_version, i );
    97 		}
    97 		}
    98 
    98 
    99 		sprintf( sql_file, "sql/%d.sql", i );
    99 		sprintf( sql_file, "sql/%d.sql", i );
   100 		upgrade_sql = slurp_file( sql_file );
   100 		upgrade_sql = slurp_file( sql_file );
   101 		if ( upgrade_sql == NULL ) return( SQLITE_ERROR );
   101 		if ( upgrade_sql == NULL ) return( SQLITE_ERROR );
   102 
   102 
   103 		/* If there is SQL to execute, do so and then reset for more */
   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 ) {
   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) );
   105 			debug( 2, LOC, "Error %s database: %s\n",
       
   106 					(i == 1 ? "initalizing" : "upgrading"), sqlite3_errmsg(v.db) );
   106 			return( sqlite3_errcode(v.db) );
   107 			return( sqlite3_errcode(v.db) );
   107 		}
   108 		}
   108 		free( upgrade_sql );
   109 		free( upgrade_sql );
   109 		upgrade_sql = NULL;
   110 		upgrade_sql = NULL;
   110 
   111 
   111 		/* update version metadata in DB if update was successful */
   112 		/* update version metadata in DB if update was successful */
   112 		current_version = i;
   113 		current_version = i;
   113 		sprintf( user_pragma, "PRAGMA user_version = %hu;", current_version );
   114 		sprintf( user_pragma, "PRAGMA user_version = %hu;", current_version );
   114 		if ( sqlite3_exec( v.db, user_pragma, NULL, NULL, NULL ) != SQLITE_OK ) {
   115 		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 			debug( 2, LOC, "Error setting version: %s\n", sqlite3_errmsg(v.db) );
   116 			return( sqlite3_errcode(v.db) );
   117 			return( sqlite3_errcode(v.db) );
   117 		}
   118 		}
   118 	}
   119 	}
   119 
   120 
   120 	return( SQLITE_OK );
   121 	return( SQLITE_OK );
   131 {
   132 {
   132 	struct sqlite3_stmt *stmt;
   133 	struct sqlite3_stmt *stmt;
   133 	int version = -1;
   134 	int version = -1;
   134 
   135 
   135 	if ( sqlite3_prepare_v2( v.db, "PRAGMA user_version", -1, &stmt, NULL ) != SQLITE_OK ) {
   136 	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 		debug( 2, LOC, "Error finding DB version: %s\n", sqlite3_errmsg(v.db) );
   137 		return( -1 );
   138 		return( -1 );
   138 	}
   139 	}
   139 
   140 
   140 	if ( sqlite3_step( stmt ) == SQLITE_ROW )
   141 	if ( sqlite3_step( stmt ) == SQLITE_ROW )
   141 		version = sqlite3_column_int( stmt, 0 );
   142 		version = sqlite3_column_int( stmt, 0 );
   142 
   143 
   143 	sqlite3_finalize( stmt );
   144 	sqlite3_finalize( stmt );
   144 	return version;
   145 	return( version );
   145 }
   146 }
   146 
   147