1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <ZLibrary.h>
#include <ZLFile.h>
#include "../DBRunnables.h"
#include "../../sqldb/implsqlite/SQLiteFactory.h"
static const std::string FIND_FILE_ID =
"SELECT file_id FROM Files" \
" WHERE name = @name AND coalesce(parent_id, 0) = @parent_id;";
static const std::string ADD_FILE =
"INSERT INTO Files (name, parent_id, size)" \
" VALUES(@name, nullif(@parent_id, 0), nullif(@size, 0));" \
" SELECT last_insert_rowid() AS file_id;";
FindFileIdRunnable::FindFileIdRunnable(DBConnection &connection) {
myFindFileId = SQLiteFactory::createCommand(FIND_FILE_ID, connection, "@name", DBValue::DBTEXT, "@parent_id", DBValue::DBINT);
myAddFile = SQLiteFactory::createCommand(ADD_FILE, connection, "@name", DBValue::DBTEXT, "@parent_id", DBValue::DBINT, "@size", DBValue::DBINT);
}
bool FindFileIdRunnable::run() {
const std::string resolvedPath = ZLFile(myFileName).resolvedPath();
const std::string physPath = ZLFile(resolvedPath).physicalFilePath();
const std::string dirName = physPath.substr(0, physPath.rfind(ZLibrary::FileNameDelimiter));
DBTextValue &findName = (DBTextValue &) *myFindFileId->parameter("@name").value();
DBIntValue &findParent = (DBIntValue &) *myFindFileId->parameter("@parent_id").value();
DBTextValue &addName = (DBTextValue &) *myAddFile->parameter("@name").value();
DBIntValue &addParent = (DBIntValue &) *myAddFile->parameter("@parent_id").value();
((DBIntValue &) *myAddFile->parameter("@size").value()) = 0;
std::size_t index = dirName.length() + 1;
findName = dirName;
findParent = 0;
while (true) {
shared_ptr<DBValue> physId = myFindFileId->executeScalar();
if (physId.isNull() || physId->type() != DBValue::DBINT || ((DBIntValue &) *physId).value() == 0) {
if (!myAdd) {
return false;
}
addName = findName.value();
addParent = findParent.value();
physId = myAddFile->executeScalar();
if (physId.isNull() || physId->type() != DBValue::DBINT || ((DBIntValue &) *physId).value() == 0) {
return false;
}
}
if (index == 0) {
myFileId = ((DBIntValue &) *physId).value();
return true;
}
std::size_t index2 = resolvedPath.find(BooksDBQuery::ArchiveEntryDelimiter, index);
findName = resolvedPath.substr(index, index2 - index);
index = index2 + 1;
findParent = ((DBIntValue &) *physId).value();
}
}
void FindFileIdRunnable::setFileName(const std::string &fileName, bool add) {
myFileName = fileName;
myAdd = add;
myFileId = 0;
}
int FindFileIdRunnable::fileId() const {
return myFileId;
}
SaveFileEntriesRunnable::SaveFileEntriesRunnable(DBConnection &connection) {
myAddFile = SQLiteFactory::createCommand(ADD_FILE, connection, "@name", DBValue::DBTEXT, "@parent_id", DBValue::DBINT, "@size", DBValue::DBINT);
myFindFileId = new FindFileIdRunnable(connection);
myDeleteFileEntries = new DeleteFileEntriesRunnable(connection);
}
bool SaveFileEntriesRunnable::run() {
myFindFileId->setFileName(myFileName, true);
if (!myFindFileId->run()) {
return false;
}
myDeleteFileEntries->setFileId(myFindFileId->fileId());
if (!myDeleteFileEntries->run()) {
return false;
}
DBTextValue &addName = (DBTextValue &) *myAddFile->parameter("@name").value();
((DBIntValue &) *myAddFile->parameter("@parent_id").value()) = myFindFileId->fileId();
((DBIntValue &) *myAddFile->parameter("@size").value()) = 0;
for (std::vector<std::string>::const_iterator it = myEntries.begin(); it != myEntries.end(); ++it) {
const std::string &entry = (*it);
if (entry.empty()) {
continue;
}
addName = entry;
if (!myAddFile->execute()) {
return false;
}
}
return true;
}
|