1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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"
26 #include "FieldImpl.h"
27 #include "FieldValue.h"
28 #include "FieldValues.h"
36 SelectionMask::SelectionMask(TableImplPtr table
) :
38 m_values(ValuesPtr(new FieldValues(table
))),
39 m_result(SavableManagersPtr(new SavableManagers(table
))),
45 SelectionMask::~SelectionMask()
50 TableImplPtr
SelectionMask::getTable() const
55 void SelectionMask::addField(FieldValuePtr 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
)
71 FieldValuePtr
fieldvalue(new FieldValue(field
, value
));
75 void SelectionMask::addField(FieldImplPtr field
, value_type value
)
79 FieldValuePtr
fieldvalue(new FieldValue(field
, value
));
83 void SelectionMask::addJoin(TableImplPtr joinTable
, KeyImplPtr nativeKey
, KeyImplPtr foreignKey
)
89 addJoin(m_nativeTable
, joinTable
, nativeKey
, foreignKey
);
92 void SelectionMask::addJoin(TableImplPtr nativeTable
, TableImplPtr joinTable
, KeyImplPtr nativeKey
, KeyImplPtr foreignKey
)
97 Assert(foreignKey
->getTable() == joinTable
);
98 Assert(nativeKey
->getTable() == nativeTable
);
100 Assert(isReachableTable(nativeTable
));
102 JoinPtr
join(new Join(nativeTable
, joinTable
, nativeKey
, foreignKey
));
106 void SelectionMask::addJoin(JoinPtr 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
122 for(ValueMap::const_iterator it
= m_values
->begin(); it
!= m_values
->end(); it
++)
124 ValuePtr value
= it
->second
;
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
);
134 value_type intvalue
= value
->getIntegerValue();
135 rc
= sqlite3_bind_int64(statement
, n
, intvalue
);
139 throw SqliteError(db
);
145 void SelectionMask::parseRow(sqlite3_stmt
* 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());
158 /// End locked section
162 m_result
->addManager(manager
);
163 m_count
= m_result
->size();
166 void SelectionMask::parseCount(sqlite3_stmt
* statement
)
170 size_t count
= sqlite3_column_int64(statement
, 0);
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
;