SQLite:: Add some convenience
This commit is contained in:
		
							parent
							
								
									6cb4bdf152
								
							
						
					
					
						commit
						f294623d1d
					
				
					 4 changed files with 33 additions and 28 deletions
				
			
		|  | @ -181,24 +181,20 @@ LocalStore::LocalStore(const Params & params) | ||||||
| 
 | 
 | ||||||
|         if (curSchema < 8) { |         if (curSchema < 8) { | ||||||
|             SQLiteTxn txn(state->db); |             SQLiteTxn txn(state->db); | ||||||
|             if (sqlite3_exec(state->db, "alter table ValidPaths add column ultimate integer", 0, 0, 0) != SQLITE_OK) |             state->db.exec("alter table ValidPaths add column ultimate integer"); | ||||||
|                 throwSQLiteError(state->db, "upgrading database schema"); |             state->db.exec("alter table ValidPaths add column sigs text"); | ||||||
|             if (sqlite3_exec(state->db, "alter table ValidPaths add column sigs text", 0, 0, 0) != SQLITE_OK) |  | ||||||
|                 throwSQLiteError(state->db, "upgrading database schema"); |  | ||||||
|             txn.commit(); |             txn.commit(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (curSchema < 9) { |         if (curSchema < 9) { | ||||||
|             SQLiteTxn txn(state->db); |             SQLiteTxn txn(state->db); | ||||||
|             if (sqlite3_exec(state->db, "drop table FailedPaths", 0, 0, 0) != SQLITE_OK) |             state->db.exec("drop table FailedPaths"); | ||||||
|                 throwSQLiteError(state->db, "upgrading database schema"); |  | ||||||
|             txn.commit(); |             txn.commit(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (curSchema < 10) { |         if (curSchema < 10) { | ||||||
|             SQLiteTxn txn(state->db); |             SQLiteTxn txn(state->db); | ||||||
|             if (sqlite3_exec(state->db, "alter table ValidPaths add column ca text", 0, 0, 0) != SQLITE_OK) |             state->db.exec("alter table ValidPaths add column ca text"); | ||||||
|                 throwSQLiteError(state->db, "upgrading database schema"); |  | ||||||
|             txn.commit(); |             txn.commit(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | @ -286,8 +282,7 @@ void LocalStore::openDB(State & state, bool create) | ||||||
|     if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK) |     if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK) | ||||||
|         throwSQLiteError(db, "setting timeout"); |         throwSQLiteError(db, "setting timeout"); | ||||||
| 
 | 
 | ||||||
|     if (sqlite3_exec(db, "pragma foreign_keys = 1;", 0, 0, 0) != SQLITE_OK) |     db.exec("pragma foreign_keys = 1"); | ||||||
|         throwSQLiteError(db, "enabling foreign keys"); |  | ||||||
| 
 | 
 | ||||||
|     /* !!! check whether sqlite has been built with foreign key
 |     /* !!! check whether sqlite has been built with foreign key
 | ||||||
|        support */ |        support */ | ||||||
|  | @ -297,8 +292,7 @@ void LocalStore::openDB(State & state, bool create) | ||||||
|        all.  This can cause database corruption if the system |        all.  This can cause database corruption if the system | ||||||
|        crashes. */ |        crashes. */ | ||||||
|     string syncMode = settings.fsyncMetadata ? "normal" : "off"; |     string syncMode = settings.fsyncMetadata ? "normal" : "off"; | ||||||
|     if (sqlite3_exec(db, ("pragma synchronous = " + syncMode + ";").c_str(), 0, 0, 0) != SQLITE_OK) |     db.exec("pragma synchronous = " + syncMode); | ||||||
|         throwSQLiteError(db, "setting synchronous mode"); |  | ||||||
| 
 | 
 | ||||||
|     /* Set the SQLite journal mode.  WAL mode is fastest, so it's the
 |     /* Set the SQLite journal mode.  WAL mode is fastest, so it's the
 | ||||||
|        default. */ |        default. */ | ||||||
|  | @ -326,8 +320,7 @@ void LocalStore::openDB(State & state, bool create) | ||||||
|         const char * schema = |         const char * schema = | ||||||
| #include "schema.sql.hh" | #include "schema.sql.hh" | ||||||
|             ; |             ; | ||||||
|         if (sqlite3_exec(db, (const char *) schema, 0, 0, 0) != SQLITE_OK) |         db.exec(schema); | ||||||
|             throwSQLiteError(db, "initialising database schema"); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -1297,9 +1290,7 @@ void LocalStore::upgradeStore7() | ||||||
| void LocalStore::vacuumDB() | void LocalStore::vacuumDB() | ||||||
| { | { | ||||||
|     auto state(_state.lock()); |     auto state(_state.lock()); | ||||||
| 
 |     state->db.exec("vacuum"); | ||||||
|     if (sqlite3_exec(state->db, "vacuum;", 0, 0, 0) != SQLITE_OK) |  | ||||||
|         throwSQLiteError(state->db, "vacuuming SQLite database"); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -78,21 +78,16 @@ public: | ||||||
|         Path dbPath = getCacheDir() + "/nix/binary-cache-v5.sqlite"; |         Path dbPath = getCacheDir() + "/nix/binary-cache-v5.sqlite"; | ||||||
|         createDirs(dirOf(dbPath)); |         createDirs(dirOf(dbPath)); | ||||||
| 
 | 
 | ||||||
|         if (sqlite3_open_v2(dbPath.c_str(), &state->db.db, |         state->db = SQLite(dbPath); | ||||||
|                 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0) != SQLITE_OK) |  | ||||||
|             throw Error(format("cannot open store cache ‘%s’") % dbPath); |  | ||||||
| 
 | 
 | ||||||
|         if (sqlite3_busy_timeout(state->db, 60 * 60 * 1000) != SQLITE_OK) |         if (sqlite3_busy_timeout(state->db, 60 * 60 * 1000) != SQLITE_OK) | ||||||
|             throwSQLiteError(state->db, "setting timeout"); |             throwSQLiteError(state->db, "setting timeout"); | ||||||
| 
 | 
 | ||||||
|         // We can always reproduce the cache.
 |         // We can always reproduce the cache.
 | ||||||
|         if (sqlite3_exec(state->db, "pragma synchronous = off", 0, 0, 0) != SQLITE_OK) |         state->db.exec("pragma synchronous = off"); | ||||||
|             throwSQLiteError(state->db, "making database asynchronous"); |         state->db.exec("pragma main.journal_mode = truncate"); | ||||||
|         if (sqlite3_exec(state->db, "pragma main.journal_mode = truncate", 0, 0, 0) != SQLITE_OK) |  | ||||||
|             throwSQLiteError(state->db, "setting journal mode"); |  | ||||||
| 
 | 
 | ||||||
|         if (sqlite3_exec(state->db, schema, 0, 0, 0) != SQLITE_OK) |         state->db.exec(schema); | ||||||
|             throwSQLiteError(state->db, "initialising database schema"); |  | ||||||
| 
 | 
 | ||||||
|         state->insertCache.create(state->db, |         state->insertCache.create(state->db, | ||||||
|             "insert or replace into BinaryCaches(url, timestamp, storeDir, wantMassQuery, priority) values (?, ?, ?, ?, ?)"); |             "insert or replace into BinaryCaches(url, timestamp, storeDir, wantMassQuery, priority) values (?, ?, ?, ?, ?)"); | ||||||
|  |  | ||||||
|  | @ -35,6 +35,13 @@ namespace nix { | ||||||
|         throw SQLiteError(format("%1%: %2%") % f.str() % sqlite3_errmsg(db)); |         throw SQLiteError(format("%1%: %2%") % f.str() % sqlite3_errmsg(db)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | SQLite::SQLite(const Path & path) | ||||||
|  | { | ||||||
|  |     if (sqlite3_open_v2(path.c_str(), &db, | ||||||
|  |             SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0) != SQLITE_OK) | ||||||
|  |         throw Error(format("cannot open SQLite database ‘%s’") % path); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| SQLite::~SQLite() | SQLite::~SQLite() | ||||||
| { | { | ||||||
|     try { |     try { | ||||||
|  | @ -45,6 +52,12 @@ SQLite::~SQLite() | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void SQLite::exec(const std::string & stmt) | ||||||
|  | { | ||||||
|  |     if (sqlite3_exec(db, stmt.c_str(), 0, 0, 0) != SQLITE_OK) | ||||||
|  |         throwSQLiteError(db, format("executing SQLite statement ‘%s’") % stmt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void SQLiteStmt::create(sqlite3 * db, const string & s) | void SQLiteStmt::create(sqlite3 * db, const string & s) | ||||||
| { | { | ||||||
|     checkInterrupt(); |     checkInterrupt(); | ||||||
|  |  | ||||||
|  | @ -13,10 +13,16 @@ namespace nix { | ||||||
| /* RAII wrapper to close a SQLite database automatically. */ | /* RAII wrapper to close a SQLite database automatically. */ | ||||||
| struct SQLite | struct SQLite | ||||||
| { | { | ||||||
|     sqlite3 * db; |     sqlite3 * db = 0; | ||||||
|     SQLite() { db = 0; } |     SQLite() { } | ||||||
|  |     SQLite(const Path & path); | ||||||
|  |     SQLite(const SQLite & from) = delete; | ||||||
|  |     SQLite& operator = (const SQLite & from) = delete; | ||||||
|  |     SQLite& operator = (SQLite && from) { db = from.db; from.db = 0; return *this; } | ||||||
|     ~SQLite(); |     ~SQLite(); | ||||||
|     operator sqlite3 * () { return db; } |     operator sqlite3 * () { return db; } | ||||||
|  | 
 | ||||||
|  |     void exec(const std::string & stmt); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| /* RAII wrapper to create and destroy SQLite prepared statements. */ | /* RAII wrapper to create and destroy SQLite prepared statements. */ | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue