Changeset View
Changeset View
Standalone View
Standalone View
src/KDbQuerySchema_p.cpp
Show All 12 Lines | 1 | /* This file is part of the KDE project | |||
---|---|---|---|---|---|
13 | 13 | | |||
14 | You should have received a copy of the GNU Library General Public License | 14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "KDbQuerySchema_p.h" | 20 | #include "KDbQuerySchema_p.h" | ||
21 | #include "KDbConnection.h" | ||||
22 | #include "KDbConnection_p.h" | ||||
21 | #include "KDbOrderByColumn.h" | 23 | #include "KDbOrderByColumn.h" | ||
22 | 24 | | |||
23 | KDbQuerySchema::Private::Private(KDbQuerySchema* q, Private* copy) | 25 | KDbQuerySchemaPrivate::KDbQuerySchemaPrivate(KDbQuerySchema* q, KDbQuerySchemaPrivate* copy) | ||
24 | : query(q) | 26 | : query(q) | ||
25 | , masterTable(nullptr) | 27 | , masterTable(nullptr) | ||
26 | , fakeRecordIdField(nullptr) | 28 | , fakeRecordIdField(nullptr) | ||
27 | , fakeRecordIdCol(nullptr) | 29 | , fakeRecordIdCol(nullptr) | ||
28 | , conn(nullptr) | | |||
29 | , maxIndexWithAlias(-1) | 30 | , maxIndexWithAlias(-1) | ||
30 | , visibility(64) | 31 | , visibility(64) | ||
31 | , fieldsExpanded(nullptr) | | |||
32 | , visibleFieldsExpanded(nullptr) | | |||
33 | , internalFields(nullptr) | | |||
34 | , fieldsExpandedWithInternalAndRecordId(nullptr) | | |||
35 | , visibleFieldsExpandedWithInternalAndRecordId(nullptr) | | |||
36 | , fieldsExpandedWithInternal(nullptr) | | |||
37 | , visibleFieldsExpandedWithInternal(nullptr) | | |||
38 | , orderByColumnList(nullptr) | 32 | , orderByColumnList(nullptr) | ||
39 | , autoincFields(nullptr) | 33 | , autoincFields(nullptr) | ||
40 | , columnsOrder(nullptr) | | |||
41 | , columnsOrderWithoutAsterisks(nullptr) | | |||
42 | , columnsOrderExpanded(nullptr) | | |||
43 | , pkeyFieldsOrder(nullptr) | 34 | , pkeyFieldsOrder(nullptr) | ||
44 | , pkeyFieldCount(0) | 35 | , pkeyFieldCount(0) | ||
45 | , tablesBoundToColumns(64, -1) // will be resized if needed | 36 | , tablesBoundToColumns(64, -1) // will be resized if needed | ||
46 | , ownedVisibleColumns(nullptr) | | |||
47 | , regenerateExprAliases(false) | 37 | , regenerateExprAliases(false) | ||
48 | { | 38 | { | ||
49 | visibility.fill(false); | 39 | visibility.fill(false); | ||
50 | if (copy) { | 40 | if (copy) { | ||
51 | // deep copy | 41 | // deep copy | ||
52 | *this = *copy; | 42 | *this = *copy; | ||
53 | // <clear, so computeFieldsExpanded() will re-create it> | 43 | // <clear, so computeFieldsExpanded() will re-create it> | ||
54 | fieldsExpanded = nullptr; | | |||
55 | visibleFieldsExpanded = nullptr; | | |||
56 | internalFields = nullptr; | | |||
57 | columnsOrder = nullptr; | | |||
58 | columnsOrderWithoutAsterisks = nullptr; | | |||
59 | columnsOrderExpanded = nullptr; | | |||
60 | orderByColumnList = nullptr; | 44 | orderByColumnList = nullptr; | ||
61 | autoincFields = nullptr; | 45 | autoincFields = nullptr; | ||
62 | autoIncrementSqlFieldsList.clear(); | 46 | autoIncrementSqlFieldsList.clear(); | ||
63 | columnInfosByNameExpanded.clear(); | | |||
64 | columnInfosByName.clear(); | | |||
65 | ownedVisibleColumns = nullptr; | | |||
66 | fieldsExpandedWithInternalAndRecordId = nullptr; | | |||
67 | visibleFieldsExpandedWithInternalAndRecordId = nullptr; | | |||
68 | fieldsExpandedWithInternal = nullptr; | | |||
69 | visibleFieldsExpandedWithInternal = nullptr; | | |||
70 | pkeyFieldsOrder = nullptr; | 47 | pkeyFieldsOrder = nullptr; | ||
71 | fakeRecordIdCol = nullptr; | 48 | fakeRecordIdCol = nullptr; | ||
72 | fakeRecordIdField = nullptr; | 49 | fakeRecordIdField = nullptr; | ||
73 | conn = nullptr; | | |||
74 | ownedVisibleColumns = nullptr; | | |||
75 | // </clear, so computeFieldsExpanded() will re-create it> | 50 | // </clear, so computeFieldsExpanded() will re-create it> | ||
76 | if (!copy->whereExpr.isNull()) { | 51 | if (!copy->whereExpr.isNull()) { | ||
77 | whereExpr = copy->whereExpr.clone(); | 52 | whereExpr = copy->whereExpr.clone(); | ||
78 | } | 53 | } | ||
79 | // "*this = *copy" causes copying pointers; pull of them without destroying, | 54 | // "*this = *copy" causes copying pointers; pull of them without destroying, | ||
80 | // will be deep-copied in the KDbQuerySchema ctor. | 55 | // will be deep-copied in the KDbQuerySchema ctor. | ||
81 | asterisks.setAutoDelete(false); | 56 | asterisks.setAutoDelete(false); | ||
82 | asterisks.clear(); | 57 | asterisks.clear(); | ||
83 | asterisks.setAutoDelete(true); | 58 | asterisks.setAutoDelete(true); | ||
84 | } | 59 | } | ||
85 | else { | 60 | else { | ||
86 | orderByColumnList = new KDbOrderByColumnList; | 61 | orderByColumnList = new KDbOrderByColumnList; | ||
87 | } | 62 | } | ||
88 | } | 63 | } | ||
89 | 64 | | |||
90 | KDbQuerySchema::Private::~Private() | 65 | KDbQuerySchemaPrivate::~KDbQuerySchemaPrivate() | ||
91 | { | 66 | { | ||
67 | if (recentConnection) { | ||||
68 | recentConnection->d->insertFieldsExpanded(query, nullptr); | ||||
69 | } | ||||
92 | delete orderByColumnList; | 70 | delete orderByColumnList; | ||
93 | delete autoincFields; | 71 | delete autoincFields; | ||
94 | delete columnsOrder; | | |||
95 | delete columnsOrderWithoutAsterisks; | | |||
96 | delete columnsOrderExpanded; | | |||
97 | delete pkeyFieldsOrder; | 72 | delete pkeyFieldsOrder; | ||
98 | delete fakeRecordIdCol; | 73 | delete fakeRecordIdCol; | ||
99 | delete fakeRecordIdField; | 74 | delete fakeRecordIdField; | ||
100 | delete ownedVisibleColumns; | | |||
101 | if (fieldsExpanded) { | | |||
102 | qDeleteAll(*fieldsExpanded); | | |||
103 | delete fieldsExpanded; | | |||
104 | } | | |||
105 | if (internalFields) { | | |||
106 | qDeleteAll(*internalFields); | | |||
107 | delete internalFields; | | |||
108 | } | | |||
109 | delete fieldsExpandedWithInternalAndRecordId; | | |||
110 | delete visibleFieldsExpandedWithInternalAndRecordId; | | |||
111 | delete fieldsExpandedWithInternal; | | |||
112 | delete visibleFieldsExpandedWithInternal; | | |||
113 | } | | |||
114 | | ||||
115 | //static | | |||
116 | KDbQuerySchema* KDbQuerySchema::Private::createQuery(KDbConnection *conn) | | |||
117 | { | | |||
118 | return new KDbQuerySchema(conn); | | |||
119 | } | 75 | } | ||
120 | 76 | | |||
121 | void KDbQuerySchema::Private::clear() | 77 | void KDbQuerySchemaPrivate::clear() | ||
122 | { | 78 | { | ||
123 | columnAliases.clear(); | 79 | columnAliases.clear(); | ||
124 | tableAliases.clear(); | 80 | tableAliases.clear(); | ||
125 | asterisks.clear(); | 81 | asterisks.clear(); | ||
126 | relations.clear(); | 82 | relations.clear(); | ||
127 | masterTable = nullptr; | 83 | masterTable = nullptr; | ||
128 | tables.clear(); | 84 | tables.clear(); | ||
129 | clearCachedData(); | 85 | clearCachedData(); | ||
130 | delete pkeyFieldsOrder; | 86 | delete pkeyFieldsOrder; | ||
131 | pkeyFieldsOrder = nullptr; | 87 | pkeyFieldsOrder = nullptr; | ||
132 | visibility.fill(false); | 88 | visibility.fill(false); | ||
133 | tablesBoundToColumns = QVector<int>(64, -1); // will be resized if needed | 89 | tablesBoundToColumns = QVector<int>(64, -1); // will be resized if needed | ||
134 | tablePositionsForAliases.clear(); | 90 | tablePositionsForAliases.clear(); | ||
135 | columnPositionsForAliases.clear(); | 91 | columnPositionsForAliases.clear(); | ||
136 | } | 92 | } | ||
137 | 93 | | |||
138 | void KDbQuerySchema::Private::clearCachedData() | 94 | void KDbQuerySchemaPrivate::clearCachedData() | ||
139 | { | 95 | { | ||
140 | if (orderByColumnList) { | 96 | if (orderByColumnList) { | ||
141 | orderByColumnList->clear(); | 97 | orderByColumnList->clear(); | ||
142 | } | 98 | } | ||
143 | if (fieldsExpanded) { | 99 | if (recentConnection) { | ||
144 | delete columnsOrder; | 100 | recentConnection->d->insertFieldsExpanded(query, nullptr); | ||
145 | columnsOrder = nullptr; | 101 | } | ||
146 | delete columnsOrderWithoutAsterisks; | | |||
147 | columnsOrderWithoutAsterisks = nullptr; | | |||
148 | delete columnsOrderExpanded; | | |||
149 | columnsOrderExpanded = nullptr; | | |||
150 | delete autoincFields; | 102 | delete autoincFields; | ||
151 | autoincFields = nullptr; | 103 | autoincFields = nullptr; | ||
152 | autoIncrementSqlFieldsList.clear(); | 104 | autoIncrementSqlFieldsList.clear(); | ||
153 | columnInfosByNameExpanded.clear(); | | |||
154 | columnInfosByName.clear(); | | |||
155 | delete ownedVisibleColumns; | | |||
156 | ownedVisibleColumns = nullptr; | | |||
157 | qDeleteAll(*fieldsExpanded); | | |||
158 | delete fieldsExpanded; | | |||
159 | fieldsExpanded = nullptr; | | |||
160 | delete visibleFieldsExpanded; // NO qDeleteAll, items not owned | | |||
161 | visibleFieldsExpanded = nullptr; | | |||
162 | if (internalFields) { | | |||
163 | qDeleteAll(*internalFields); | | |||
164 | delete internalFields; | | |||
165 | internalFields = nullptr; | | |||
166 | } | | |||
167 | delete fieldsExpandedWithInternalAndRecordId; | | |||
168 | fieldsExpandedWithInternalAndRecordId = nullptr; | | |||
169 | delete visibleFieldsExpandedWithInternalAndRecordId; | | |||
170 | visibleFieldsExpandedWithInternalAndRecordId = nullptr; | | |||
171 | delete fieldsExpandedWithInternal; | | |||
172 | fieldsExpandedWithInternal = nullptr; | | |||
173 | delete visibleFieldsExpandedWithInternal; | | |||
174 | visibleFieldsExpandedWithInternal = nullptr; | | |||
175 | } | | |||
176 | } | 105 | } | ||
177 | 106 | | |||
178 | void KDbQuerySchema::Private::setColumnAlias(int position, const QString& alias) | 107 | void KDbQuerySchemaPrivate::setColumnAlias(int position, const QString& alias) | ||
179 | { | 108 | { | ||
180 | if (alias.isEmpty()) { | 109 | if (alias.isEmpty()) { | ||
181 | columnAliases.remove(position); | 110 | columnAliases.remove(position); | ||
182 | maxIndexWithAlias = -1; | 111 | maxIndexWithAlias = -1; | ||
183 | } else { | 112 | } else { | ||
184 | setColumnAliasInternal(position, alias); | 113 | setColumnAliasInternal(position, alias); | ||
185 | } | 114 | } | ||
186 | } | 115 | } | ||
187 | 116 | | |||
188 | void KDbQuerySchema::Private::tryRegenerateExprAliases() | 117 | void KDbQuerySchemaPrivate::tryRegenerateExprAliases() | ||
189 | { | 118 | { | ||
190 | if (!regenerateExprAliases) | 119 | if (!regenerateExprAliases) | ||
191 | return; | 120 | return; | ||
192 | //regenerate missing aliases for experessions | 121 | //regenerate missing aliases for experessions | ||
193 | int colNum = 0; //used to generate a name | 122 | int colNum = 0; //used to generate a name | ||
194 | QString columnAlias; | 123 | QString columnAlias; | ||
195 | int p = -1; | 124 | int p = -1; | ||
196 | foreach(KDbField* f, *query->fields()) { | 125 | foreach(KDbField* f, *query->fields()) { | ||
Show All 9 Lines | 127 | if (f->isExpression() && columnAliases.value(p).isEmpty()) { | |||
206 | } while (-1 != tablePositionForAlias(columnAlias)); | 135 | } while (-1 != tablePositionForAlias(columnAlias)); | ||
207 | 136 | | |||
208 | setColumnAliasInternal(p, columnAlias); | 137 | setColumnAliasInternal(p, columnAlias); | ||
209 | } | 138 | } | ||
210 | } | 139 | } | ||
211 | regenerateExprAliases = false; | 140 | regenerateExprAliases = false; | ||
212 | } | 141 | } | ||
213 | 142 | | |||
214 | void KDbQuerySchema::Private::setColumnAliasInternal(int position, const QString& alias) | 143 | void KDbQuerySchemaPrivate::setColumnAliasInternal(int position, const QString& alias) | ||
215 | { | 144 | { | ||
216 | columnAliases.insert(position, alias.toLower()); | 145 | columnAliases.insert(position, alias.toLower()); | ||
217 | columnPositionsForAliases.insert(alias.toLower(), position); | 146 | columnPositionsForAliases.insert(alias.toLower(), position); | ||
218 | maxIndexWithAlias = qMax(maxIndexWithAlias, position); | 147 | maxIndexWithAlias = qMax(maxIndexWithAlias, position); | ||
219 | } | 148 | } |