Added 'length' to area.
[UnsignedByte.git] / src / DAL / SelectionMask.cpp
blobe0168c25708b5104c9cbe8b74dc6cb1912f5b84c
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(ValuePtr 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::addJoin(TableImplPtr joinTable, KeyImplPtr nativeKey, KeyImplPtr foreignKey)
69 Assert(joinTable);
70 Assert(nativeKey);
71 Assert(foreignKey);
73 addJoin(m_nativeTable, joinTable, nativeKey, foreignKey);
76 void SelectionMask::addJoin(TableImplPtr nativeTable, TableImplPtr joinTable, KeyImplPtr nativeKey, KeyImplPtr foreignKey)
78 Assert(joinTable);
79 Assert(nativeKey);
80 Assert(foreignKey);
81 Assert(foreignKey->getTable() == joinTable);
82 Assert(nativeKey->getTable() == nativeTable);
84 Assert(isReachableTable(nativeTable));
86 JoinPtr join(new Join(nativeTable, joinTable, nativeKey, foreignKey));
87 addJoin(join);
90 void SelectionMask::addJoin(JoinPtr join)
92 Assert(join);
93 Assert(isReachableTable(join->getNativeKey()->getTable()));
95 m_joins.push_back(join);
96 m_foreignTables.push_back(join->getJoinTable());
97 m_values->addJoinedTable(join->getJoinTable());
100 void SelectionMask::bindSelectMulti(sqlite3* db, sqlite3_stmt* statement) const
102 Assert(db);
103 Assert(statement);
105 int n = 1;
106 for(ValueMap::const_iterator it = m_values->begin(); it != m_values->end(); it++)
108 ValuePtr value = it->second;
109 int rc = 0;
111 if(value->getField()->isText())
113 std::string stringvalue = value->getStringValue();
114 rc = sqlite3_bind_text(statement, n, stringvalue.c_str(), stringvalue.size(), SQLITE_TRANSIENT);
116 else
118 value_type intvalue = value->getIntegerValue();
119 rc = sqlite3_bind_int64(statement, n, intvalue);
122 if(rc != SQLITE_OK)
123 throw SqliteError(db);
125 n++;
129 void SelectionMask::parseRow(sqlite3_stmt* statement)
131 Assert(statement);
133 SavableManagerPtr manager = SavableManager::getnew(m_nativeTable);
135 bool locked = manager->lock();
136 Assert(locked); // new, so should always be unlocked
138 /// Begin locked section
139 manager->parseLookup(statement);
140 manager->parseSelect(statement, manager->primaryKeySize());
141 manager->cleanup();
142 /// End locked section
144 manager->unlock();
146 m_result->addManager(manager);
147 m_count = m_result->size();
150 void SelectionMask::parseCount(sqlite3_stmt* statement)
152 Assert(statement);
154 size_t count = sqlite3_column_int64(statement, 0);
155 m_count = count;
158 bool SelectionMask::isReachableTable(TableImplPtr table)
160 bool isReachableTable = false;
162 if(table == m_nativeTable)
163 isReachableTable = true;
165 for(TableImplVector::const_iterator it = m_foreignTables.begin(); it != m_foreignTables.end(); it++)
167 TableImplPtr foreignTable = *it;
168 if(table == foreignTable)
169 isReachableTable = true;
172 return isReachableTable;