1 /*
2 * Copyright (C) 2009-2010 Geometer Plus <contact@geometerplus.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20
21 #include <ZLStringUtil.h>
22
23 #include "SQLiteDataBase.h"
24
25 #include "SQLiteConnection.h"
26 #include "SQLiteCommand.h"
27
28 //----------- Transaction subclass -----------------
29
Transaction(SQLiteDataBase & db)30 SQLiteDataBase::Transaction::Transaction(SQLiteDataBase &db) : myDataBase(db), mySuccess(false), myStarted(false), myDepth((unsigned int)-1) {
31 }
32
~Transaction()33 SQLiteDataBase::Transaction::~Transaction() {
34 if (myStarted) {
35 end(mySuccess);
36 }
37 }
38
start()39 bool SQLiteDataBase::Transaction::start() {
40 myDepth = myDataBase.myTransactionDepth;
41 if (myDepth == 0) {
42 myStarted = myDataBase.myBeginTransaction->execute();
43 } else {
44 //((DBTextValue &) *myDataBase.myMakeSavePoint->parameter("@name").value()).setValue(name());
45 //myStarted = myDataBase.myMakeSavePoint->execute();
46 myStarted = true;
47 }
48 if (myStarted) {
49 ++myDataBase.myTransactionDepth;
50 }
51 return myStarted;
52 }
53
end(bool success)54 void SQLiteDataBase::Transaction::end(bool success) {
55 --myDataBase.myTransactionDepth;
56 if (myDepth == 0) {
57 if (success) {
58 myDataBase.myCommitTransaction->execute();
59 } else {
60 myDataBase.myRollbackTransaction->execute();
61 }
62 } else {
63 if (success) {
64 //((DBTextValue &) *myDataBase.myCommitSavePoint->parameter("@name").value()).setValue(name());
65 //myDataBase.myCommitSavePoint->execute();
66 } else {
67 //((DBTextValue &) *myDataBase.myRollbackSavePoint->parameter("@name").value()).setValue(name());
68 //myDataBase.myRollbackSavePoint->execute();
69 }
70 }
71 }
72
name() const73 std::string SQLiteDataBase::Transaction::name() const {
74 std::string name = "tran";
75 ZLStringUtil::appendNumber(name, myDepth);
76 return name;
77 }
78
79 //----------- End Transaction subclass -----------------
80
81
82
SQLiteDataBase(const std::string & path)83 SQLiteDataBase::SQLiteDataBase(const std::string &path) : DataBase( new SQLiteConnection(path) ), myTransactionDepth(0) {
84 myBeginTransaction = SQLiteFactory::createCommand("BEGIN IMMEDIATE TRANSACTION", connection());
85 myCommitTransaction = SQLiteFactory::createCommand("COMMIT TRANSACTION", connection());
86 myRollbackTransaction = SQLiteFactory::createCommand("ROLLBACK TRANSACTION", connection());
87 myMakeSavePoint = SQLiteFactory::createCommand("SAVEPOINT @name", connection(), "@name", DBValue::DBTEXT);
88 myCommitSavePoint = SQLiteFactory::createCommand("RELEASE @name", connection(), "@name", DBValue::DBTEXT);
89 myRollbackSavePoint = SQLiteFactory::createCommand("ROLLBACK TO @name; RELEASE @name", connection(), "@name", DBValue::DBTEXT);
90 }
91
~SQLiteDataBase()92 SQLiteDataBase::~SQLiteDataBase() {
93 if (connection().isOpened()) {
94 connection().close();
95 }
96 }
97
executeAsTransaction(DBRunnable & runnable)98 bool SQLiteDataBase::executeAsTransaction(DBRunnable &runnable) {
99 Transaction tran(*this);
100 if (tran.start() && runnable.run()) {
101 tran.setSuccessful();
102 return true;
103 }
104 return false;
105 }
106
107
108