{
SKGError err;
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase", err);
if (iFileDb && iMemoryDb) {
SKGTRACEL(20) << "Input parameter [iFileDb]=[" << iFileDb->databaseName() << ']' << endl;
SKGTRACEL(20) << "Input parameter [iMemoryDb]=[" << iMemoryDb->databaseName() << ']' << endl;
SKGTRACEL(10) << "Input parameter [iFromFileToMemory]=[" << (iFromFileToMemory ? "FILE->MEMORY" : "MEMORY->FILE") << ']' << endl;
QString dbFileName
=iFileDb
->databaseName
();
// Copy the tables
SKGStringListList listTables;
int nb=0;
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYTABLES", err);
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql, tbl_name FROM sqlite_master WHERE type='table' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listTables);
nb=listTables.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val
=listTables.
at(i
).
at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
//Attach db
if (err.isSucceeded()) {
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-ATTACH", err);
err = SKGServices::executeSqliteOrder(iMemoryDb, "ATTACH DATABASE '" + dbFileName + "' as source");
}
//Copy records
if (err.isSucceeded()) {
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPY", err);
err = SKGServices::executeSqliteOrder(iMemoryDb, "BEGIN");
if (err.isSucceeded()) {
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val
=listTables.
at(i
).
at(1);
if (iFromFileToMemory) err=SKGServices::executeSqliteOrder(iMemoryDb, "insert into main."+val+" select * from source."+val);
else err=SKGServices::executeSqliteOrder(iMemoryDb, "insert into source."+val+" select * from main."+val);
}
}
SKGServices::executeSqliteOrder(iMemoryDb, "COMMIT");
}
//Detach
SKGError err2;
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-DETACH", err);
err2=SKGServices::executeSqliteOrder(iMemoryDb, "DETACH DATABASE source");
if (err.isSucceeded() && err2.isFailed()) err=err2;
}
//Optimisation
if (err.isSucceeded())
{
QString optimisation
="PRAGMA case_sensitive_like=true;;" "PRAGMA journal_mode=MEMORY;;"
"PRAGMA temp_store=MEMORY;;"
"PRAGMA locking_mode=EXCLUSIVE;;"
"PRAGMA synchronous = OFF;;"
;
err = SKGServices::executeSqliteOrder(iFromFileToMemory ? iMemoryDb: iFileDb, optimisation);
}
// Copy the triggers
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYTRIGGER", err);
SKGStringListList listSqlOrder;
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql FROM sqlite_master WHERE type='trigger' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listSqlOrder);
int nb=listSqlOrder.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val
=listSqlOrder.
at(i
).
at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
// Copy the indexes
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYINDEX", err);
SKGStringListList listSqlOrder;
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql FROM sqlite_master WHERE type='index' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listSqlOrder);
int nb=listSqlOrder.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val
=listSqlOrder.
at(i
).
at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
// Copy the views
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYVIEW", err);
SKGStringListList listSqlOrder;
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql FROM sqlite_master WHERE type='view' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listSqlOrder);
int nb=listSqlOrder.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val
=listSqlOrder.
at(i
).
at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
//Check if created file exists
if (err.
isSucceeded() && !iFromFileToMemory
&& !QFile(dbFileName
).
exists()) { //Set error message
err.setReturnCode(ERR_FAIL);
err.setMessage(i18nc("An error message", "Creation file [%1] failed",dbFileName));
}
}
if (err.
isFailed()) err.
addError(SQLLITEERROR
+ ERR_FAIL, i18nc
("Error message",
"%1 failed",
QString("copySqliteDatabase::copySqliteDatabase()")));
return err;
}
SKGError SKGServices::copySqliteDatabase(QSqlDatabase* iFileDb, QSqlDatabase* iMemoryDb, bool iFromFileToMemory)
{
SKGError err;
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase", err);
if (iFileDb && iMemoryDb) {
SKGTRACEL(20) << "Input parameter [iFileDb]=[" << iFileDb->databaseName() << ']' << endl;
SKGTRACEL(20) << "Input parameter [iMemoryDb]=[" << iMemoryDb->databaseName() << ']' << endl;
SKGTRACEL(10) << "Input parameter [iFromFileToMemory]=[" << (iFromFileToMemory ? "FILE->MEMORY" : "MEMORY->FILE") << ']' << endl;
QString dbFileName=iFileDb->databaseName();
// Copy the tables
SKGStringListList listTables;
int nb=0;
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYTABLES", err);
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql, tbl_name FROM sqlite_master WHERE type='table' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listTables);
nb=listTables.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val=listTables.at(i).at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
//Attach db
if (err.isSucceeded()) {
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-ATTACH", err);
err = SKGServices::executeSqliteOrder(iMemoryDb, "ATTACH DATABASE '" + dbFileName + "' as source");
}
//Copy records
if (err.isSucceeded()) {
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPY", err);
err = SKGServices::executeSqliteOrder(iMemoryDb, "BEGIN");
if (err.isSucceeded()) {
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val=listTables.at(i).at(1);
if (iFromFileToMemory) err=SKGServices::executeSqliteOrder(iMemoryDb, "insert into main."+val+" select * from source."+val);
else err=SKGServices::executeSqliteOrder(iMemoryDb, "insert into source."+val+" select * from main."+val);
}
}
SKGServices::executeSqliteOrder(iMemoryDb, "COMMIT");
}
//Detach
SKGError err2;
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-DETACH", err);
err2=SKGServices::executeSqliteOrder(iMemoryDb, "DETACH DATABASE source");
if (err.isSucceeded() && err2.isFailed()) err=err2;
}
//Optimisation
if (err.isSucceeded())
{
QString optimisation="PRAGMA case_sensitive_like=true;;"
"PRAGMA journal_mode=MEMORY;;"
"PRAGMA temp_store=MEMORY;;"
"PRAGMA locking_mode=EXCLUSIVE;;"
"PRAGMA synchronous = OFF;;"
;
err = SKGServices::executeSqliteOrder(iFromFileToMemory ? iMemoryDb: iFileDb, optimisation);
}
// Copy the triggers
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYTRIGGER", err);
SKGStringListList listSqlOrder;
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql FROM sqlite_master WHERE type='trigger' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listSqlOrder);
int nb=listSqlOrder.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val=listSqlOrder.at(i).at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
// Copy the indexes
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYINDEX", err);
SKGStringListList listSqlOrder;
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql FROM sqlite_master WHERE type='index' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listSqlOrder);
int nb=listSqlOrder.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val=listSqlOrder.at(i).at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
// Copy the views
if (err.isSucceeded())
{
SKGTRACEINRC(10, "SKGServices::copySqliteDatabase-COPYVIEW", err);
SKGStringListList listSqlOrder;
err=SKGServices::executeSelectSqliteOrder((iFromFileToMemory ? iFileDb: iMemoryDb),
"SELECT sql FROM sqlite_master WHERE type='view' AND sql NOT NULL and name NOT IN ('sqlite_stat1', 'sqlite_sequence')",
listSqlOrder);
int nb=listSqlOrder.count();
for (int i=1; err.isSucceeded() && i<nb; ++i) { //Forget header
QString val=listSqlOrder.at(i).at(0);
err=SKGServices::executeSqliteOrder((iFromFileToMemory ? iMemoryDb: iFileDb), val);
}
}
//Check if created file exists
if (err.isSucceeded() && !iFromFileToMemory && !QFile(dbFileName).exists()) {
//Set error message
err.setReturnCode(ERR_FAIL);
err.setMessage(i18nc("An error message", "Creation file [%1] failed",dbFileName));
}
}
if (err.isFailed()) err.addError(SQLLITEERROR + ERR_FAIL, i18nc("Error message", "%1 failed", QString("copySqliteDatabase::copySqliteDatabase()")));
return err;
}
To copy to clipboard, switch view to plain text mode
Bookmarks