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(ValuePtr 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
)
73 addJoin(m_nativeTable
, joinTable
, nativeKey
, foreignKey
);
76 void SelectionMask::addJoin(TableImplPtr nativeTable
, TableImplPtr joinTable
, KeyImplPtr nativeKey
, KeyImplPtr foreignKey
)
81 Assert(foreignKey
->getTable() == joinTable
);
82 Assert(nativeKey
->getTable() == nativeTable
);
84 Assert(isReachableTable(nativeTable
));
86 JoinPtr
join(new Join(nativeTable
, joinTable
, nativeKey
, foreignKey
));
90 void SelectionMask::addJoin(JoinPtr 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
106 for(ValueMap::const_iterator it
= m_values
->begin(); it
!= m_values
->end(); it
++)
108 ValuePtr value
= it
->second
;
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
);
118 value_type intvalue
= value
->getIntegerValue();
119 rc
= sqlite3_bind_int64(statement
, n
, intvalue
);
123 throw SqliteError(db
);
129 void SelectionMask::parseRow(sqlite3_stmt
* 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());
142 /// End locked section
146 m_result
->addManager(manager
);
147 m_count
= m_result
->size();
150 void SelectionMask::parseCount(sqlite3_stmt
* statement
)
154 size_t count
= sqlite3_column_int64(statement
, 0);
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
;