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 ***************************************************************************/
23 * @file SelectionMask.h
24 * This file contains the SelectionMask class.
31 typedef std::vector
<KeysPtr
> KeysVector
; /**< The type of multiple key bucket pointers. */
34 * This class is meant to be used to filter queries to the database.
36 * When used in conjunction with <code>selectMulti</code> it allows selecting specific row's from the database from a native table.
37 * Through <code>addJoin</code> additional foreign tables may be joined into the query.
38 * With <code>addField</code> a restriction for that specific field may be applied.
39 * Finally the result of the query may be retreived with <code>getResult</code>.
41 * @see SqliteMgr::selectMulti
49 /** Constructs a SelectionMask for this specific table. */
50 SelectionMask(TableImplPtr table
);
52 /** Destructor, a noop. */
55 /** Returns the native table this SelectionMask is for. */
56 TableImplPtr
getTable() const;
58 /** Adds a field restriction, the row's returned will all have the specified value. */
59 void addField(ValuePtr value
);
61 /** Adds a field restriction, the row's returned will all have the specified value. */
62 void addField(FieldImplPtr field
, cstring value
);
64 /** Adds a field restriction, the row's returned will all have the specified value. */
65 void addField(FieldImplPtr field
, value_type value
);
68 * Adds a join to the specified table from the native table using the specified native and foreign keys.
69 * When creating a join from an already joined table, specify that table.
71 * @param joinTable The table to join on.
72 * @param nativeKey The native key to use in the join, this key should belong to the native table.
73 * @param foreignKey The foreign key to use in the join, this key should belong to the specified foreign table.
77 void addJoin(TableImplPtr joinTable
, KeyImplPtr nativeKey
, KeyImplPtr foreignKey
);
80 * Adds a join to the specified table from the specified table using the specified native and foreign keys.
82 * @param nativeTable The table to join from, this table should have been added with addJoin before.
83 * @param joinTable The table to join on.
84 * @param nativeKey The native key to use in the join, this key should belong to the native table.
85 * @param foreignKey The foreign key to use in the join, this key should belong to the specified foreign table.
89 void addJoin(TableImplPtr nativeTable
, TableImplPtr joinTable
, KeyImplPtr nativeKey
, KeyImplPtr foreignKey
);
91 /** Adds the specified join, this join should be from a table that is either the native table or joined previously. */
92 void addJoin(JoinPtr join
);
95 /** Bind the specified statement's values. */
96 void bindSelectMulti(sqlite3
* db
, sqlite3_stmt
* statement
) const;
98 /** Parse a row from the statement and store it. */
99 void parseRow(sqlite3_stmt
* statement
);
101 /** Parse a count from the statement and store it. */
102 void parseCount(sqlite3_stmt
* statement
);
105 /** Returns the size of the fields restricted on. */
106 size_t size() { return m_restrictions
.size(); }
108 /** Returns an iterator to the first restriction fields. */
109 Strings::const_iterator
begin() { return m_restrictions
.begin(); }
111 /** Returns the 'end' iterator of the restriction fields. */
112 Strings::const_iterator
end() { return m_restrictions
.end(); }
115 /** Returns the size of the joins. */
116 size_t joinssize() { return m_joins
.size(); }
118 /** Returns an iterator to the first join. */
119 Joins::const_iterator
joinsbegin() { return m_joins
.begin(); }
121 /** Returns the 'end' iterator of the joins. */
122 Joins::const_iterator
joinsend() { return m_joins
.end(); }
125 /** Returns the result of the lookup. */
126 SavableManagersPtr
getResult() { return m_result
; }
128 /** Returns the row count. */
129 size_t getCount() { return m_count
; }
132 /** Returns wether the specified table has been joined (or is equal to the native table) or not. */
133 bool isReachableTable(TableImplPtr table
);
135 TableImplPtr m_nativeTable
; /**< The native table for this selection. */
136 ValuesPtr m_values
; /**< The value restrictions for this selection. */
137 TableImplVector m_foreignTables
; /**< The foreign tables for this selection. */
138 Joins m_joins
; /**< The joins for this selection. */
139 Strings m_restrictions
; /**< The fields this selection restricts. */
141 SavableManagersPtr m_result
; /**< The result of the selection. */
142 size_t m_count
; /**< The amount of entries that matches. */