Used a more uniform of logging and added a commandline parser.
[UnsignedByte.git] / src / DAL / SelectionMask.cpp
blob1c9d647d5f456dad142c71d3584c13017c3b595b
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "SelectionMask.h"
22 #include "SavableManager.h"
23 #include "SavableManagers.h"
24 #include "TableImpl.h"
25 #include "KeyDef.h"
26 #include "FieldImpl.h"
27 #include "FieldValue.h"
28 #include "FieldValues.h"
29 #include "KeyValue.h"
30 #include "KeyImpl.h"
31 #include "Keys.h"
32 #include "Relation.h"
33 #include "Assert.h"
34 #include "Join.h"
36 SelectionMask::SelectionMask(TableImplPtr table) :
37 m_nativeTable(table),
38 m_values(ValuesPtr(new FieldValues(table))),
39 m_result(SavableManagersPtr(new SavableManagers(table))),
40 m_count(0)
42 Assert(table);
45 SelectionMask::~SelectionMask()
50 TableImplPtr SelectionMask::getTable() const
52 return m_nativeTable;
55 void SelectionMask::addField(FieldValuePtr value)
57 Assert(value);
58 Assert(isReachableTable(value->getTable()));
60 std::string fieldname = value->getTable()->getName();
61 fieldname.append(".");
62 fieldname.append(value->getField()->getName());
63 m_restrictions.push_back(fieldname);
64 m_values->addValue(value);
67 void SelectionMask::addField(FieldImplPtr field, cstring value)
69 Assert(field);
71 FieldValuePtr fieldvalue(new FieldValue(field, value));
72 addField(fieldvalue);
75 void SelectionMask::addField(FieldImplPtr field, value_type value)
77 Assert(field);
79 FieldValuePtr fieldvalue(new FieldValue(field, value));
80 addField(fieldvalue);
83 void SelectionMask::addJoin(TableImplPtr joinTable, KeyImplPtr nativeKey, KeyImplPtr foreignKey)
85 Assert(joinTable);
86 Assert(nativeKey);
87 Assert(foreignKey);
89 addJoin(m_nativeTable, joinTable, nativeKey, foreignKey);
92 void SelectionMask::addJoin(TableImplPtr nativeTable, TableImplPtr joinTable, KeyImplPtr nativeKey, KeyImplPtr foreignKey)
94 Assert(joinTable);
95 Assert(nativeKey);
96 Assert(foreignKey);
97 Assert(foreignKey->getTable() == joinTable);
98 Assert(nativeKey->getTable() == nativeTable);
100 Assert(isReachableTable(nativeTable));
102 JoinPtr join(new Join(nativeTable, joinTable, nativeKey, foreignKey));
103 addJoin(join);
106 void SelectionMask::addJoin(JoinPtr join)
108 Assert(join);
109 Assert(isReachableTable(join->getNativeKey()->getTable()));
111 m_joins.push_back(join);
112 m_foreignTables.push_back(join->getJoinTable());
113 m_values->addJoinedTable(join->getJoinTable());
116 void SelectionMask::bindSelectMulti(sqlite3* db, sqlite3_stmt* statement) const
118 Assert(db);
119 Assert(statement);
121 int n = 1;
122 for(ValueMap::const_iterator it = m_values->begin(); it != m_values->end(); it++)
124 ValuePtr value = it->second;
125 int rc = 0;
127 if(value->getField()->isText())
129 std::string stringvalue = value->getStringValue();
130 rc = sqlite3_bind_text(statement, n, stringvalue.c_str(), stringvalue.size(), SQLITE_TRANSIENT);
132 else
134 value_type intvalue = value->getIntegerValue();
135 rc = sqlite3_bind_int64(statement, n, intvalue);
138 if(rc != SQLITE_OK)
139 throw SqliteError(db);
141 n++;
145 void SelectionMask::parseRow(sqlite3_stmt* statement)
147 Assert(statement);
149 SavableManagerPtr manager = SavableManager::getnew(m_nativeTable);
151 bool locked = manager->lock();
152 Assert(locked); // new, so should always be unlocked
154 /// Begin locked section
155 manager->parseLookup(statement);
156 manager->parseSelect(statement, manager->primaryKeySize());
157 manager->cleanup();
158 /// End locked section
160 manager->unlock();
162 m_result->addManager(manager);
163 m_count = m_result->size();
166 void SelectionMask::parseCount(sqlite3_stmt* statement)
168 Assert(statement);
170 size_t count = sqlite3_column_int64(statement, 0);
171 m_count = count;
174 bool SelectionMask::isReachableTable(TableImplPtr table)
176 bool isReachableTable = false;
178 if(table == m_nativeTable)
179 isReachableTable = true;
181 for(TableImplVector::const_iterator it = m_foreignTables.begin(); it != m_foreignTables.end(); it++)
183 TableImplPtr foreignTable = *it;
184 if(table == foreignTable)
185 isReachableTable = true;
188 return isReachableTable;