diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-11-08 14:53:26 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-11-08 14:53:26 -0500 |
| commit | 350bfdb5ea9b4f7e50746c50a46d8032cbc5a104 (patch) | |
| tree | e339d118669f85de1afb858828a7affd0b47c745 /lib/database.cpp | |
| parent | b92db337bf7cbe2ea0564be6c63c336d2bcd4567 (diff) | |
| download | verbly-350bfdb5ea9b4f7e50746c50a46d8032cbc5a104.tar.gz verbly-350bfdb5ea9b4f7e50746c50a46d8032cbc5a104.tar.bz2 verbly-350bfdb5ea9b4f7e50746c50a46d8032cbc5a104.zip | |
Created database versioning system d1.0
Also added an ANALYZE statement to the end of the datafile generation process. This generates information that allows sqlite to sometimes come up with a better query plan, and in many cases can significant speed up queries. This constitutes a minor database update, but because this is the first version that uses the database versioning system, older versions are essentially incompatible. refs #2
Diffstat (limited to 'lib/database.cpp')
| -rw-r--r-- | lib/database.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
| diff --git a/lib/database.cpp b/lib/database.cpp index a4d056d..fe64763 100644 --- a/lib/database.cpp +++ b/lib/database.cpp | |||
| @@ -1,7 +1,9 @@ | |||
| 1 | #include "database.h" | 1 | #include "database.h" |
| 2 | #include <sqlite3.h> | 2 | #include <sqlite3.h> |
| 3 | #include <stdexcept> | 3 | #include <stdexcept> |
| 4 | #include <sstream> | ||
| 4 | #include "query.h" | 5 | #include "query.h" |
| 6 | #include "version.h" | ||
| 5 | 7 | ||
| 6 | namespace verbly { | 8 | namespace verbly { |
| 7 | 9 | ||
| @@ -17,6 +19,35 @@ namespace verbly { | |||
| 17 | 19 | ||
| 18 | throw database_error("Could not open verbly datafile", errmsg); | 20 | throw database_error("Could not open verbly datafile", errmsg); |
| 19 | } | 21 | } |
| 22 | |||
| 23 | std::string queryString = "SELECT major, minor FROM version"; | ||
| 24 | |||
| 25 | sqlite3_stmt* ppstmt; | ||
| 26 | if (sqlite3_prepare_v2(ppdb_, queryString.c_str(), queryString.length(), &ppstmt, NULL) != SQLITE_OK) | ||
| 27 | { | ||
| 28 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
| 29 | sqlite3_finalize(ppstmt); | ||
| 30 | |||
| 31 | throw database_error("Error reading database version", errorMsg); | ||
| 32 | } | ||
| 33 | |||
| 34 | if (sqlite3_step(ppstmt) != SQLITE_ROW) | ||
| 35 | { | ||
| 36 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
| 37 | sqlite3_finalize(ppstmt); | ||
| 38 | |||
| 39 | throw database_error("Error reading database version", errorMsg); | ||
| 40 | } | ||
| 41 | |||
| 42 | major_ = sqlite3_column_int(ppstmt, 0); | ||
| 43 | minor_ = sqlite3_column_int(ppstmt, 1); | ||
| 44 | |||
| 45 | sqlite3_finalize(ppstmt); | ||
| 46 | |||
| 47 | if (major_ != DATABASE_MAJOR_VERSION) | ||
| 48 | { | ||
| 49 | throw database_version_mismatch(DATABASE_MAJOR_VERSION, major_); | ||
| 50 | } | ||
| 20 | } | 51 | } |
| 21 | 52 | ||
| 22 | database::database(database&& other) : database() | 53 | database::database(database&& other) : database() |
| @@ -135,4 +166,15 @@ namespace verbly { | |||
| 135 | return result; | 166 | return result; |
| 136 | } | 167 | } |
| 137 | 168 | ||
| 169 | std::string database_version_mismatch::generateMessage(int right, int wrong) | ||
| 170 | { | ||
| 171 | std::ostringstream msgbuilder; | ||
| 172 | msgbuilder << "Incompatible database version: needed "; | ||
| 173 | msgbuilder << right; | ||
| 174 | msgbuilder << ", found "; | ||
| 175 | msgbuilder << wrong; | ||
| 176 | |||
| 177 | return msgbuilder.str(); | ||
| 178 | } | ||
| 179 | |||
| 138 | }; | 180 | }; |
