(The following function is courtesy of sqlite.org, minus changes to use m_pDatabase instead of pInMemory.) ** This function is used to load the contents of a database file on disk ** into the "main" database of open database connection pInMemory, or ** to save the current contents of the database opened by pInMemory into ** a database file on disk. pInMemory is probably an in-memory database, ** b
| 288 | ** an error occurs, an SQLite error code is returned. |
| 289 | */ |
| 290 | S32 SQLiteObject::loadOrSaveDb(const char *zFilename, bool isSave) |
| 291 | { |
| 292 | S32 rc; /* Function return code */ |
| 293 | sqlite3 *pFile; /* Database connection opened on zFilename */ |
| 294 | sqlite3_backup *pBackup; /* Backup object used to copy data */ |
| 295 | sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */ |
| 296 | sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */ |
| 297 | |
| 298 | /* Open the database file identified by zFilename. Exit early if this fails |
| 299 | ** for any reason. */ |
| 300 | |
| 301 | Con::printf("calling loadOrSaveDb, isSave = %d", isSave); |
| 302 | |
| 303 | if (isSave == false) |
| 304 | {//If we're loading, have to create the memory database. |
| 305 | if (!(SQLITE_OK == sqlite3_open(":memory:", &m_pDatabase))) |
| 306 | { |
| 307 | Con::printf("Unable to open a memory database!"); |
| 308 | return 0; |
| 309 | } |
| 310 | } |
| 311 | rc = sqlite3_open(zFilename, &pFile); |
| 312 | if (rc == SQLITE_OK) { |
| 313 | |
| 314 | /* If this is a 'load' operation (isSave==0), then data is copied |
| 315 | ** from the database file just opened to database pInMemory. |
| 316 | ** Otherwise, if this is a 'save' operation (isSave==1), then data |
| 317 | ** is copied from pInMemory to pFile. Set the variables pFrom and |
| 318 | ** pTo accordingly. */ |
| 319 | pFrom = (isSave ? m_pDatabase : pFile); |
| 320 | pTo = (isSave ? pFile : m_pDatabase); |
| 321 | |
| 322 | /* Set up the backup procedure to copy from the "main" database of |
| 323 | ** connection pFile to the main database of connection pInMemory. |
| 324 | ** If something goes wrong, pBackup will be set to NULL and an error |
| 325 | ** code and message left in connection pTo. |
| 326 | ** |
| 327 | ** If the backup object is successfully created, call backup_step() |
| 328 | ** to copy data from pFile to pInMemory. Then call backup_finish() |
| 329 | ** to release resources associated with the pBackup object. If an |
| 330 | ** error occurred, then an error code and message will be left in |
| 331 | ** connection pTo. If no error occurred, then the error code belonging |
| 332 | ** to pTo is set to SQLITE_OK. |
| 333 | */ |
| 334 | pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main"); |
| 335 | if (pBackup) { |
| 336 | (void)sqlite3_backup_step(pBackup, -1); |
| 337 | (void)sqlite3_backup_finish(pBackup); |
| 338 | } |
| 339 | rc = sqlite3_errcode(pTo); |
| 340 | } |
| 341 | |
| 342 | /* Close the database connection opened on database file zFilename |
| 343 | ** and return the result of this function. */ |
| 344 | (void)sqlite3_close(pFile); |
| 345 | |
| 346 | //if (isSave == true) // Actually, cancel this, I'm sure it will happen automatically and if we don't do it here, we can also use |
| 347 | //{ // this function for periodic saves, such as after saving mission. |
no test coverage detected