4 #include "zypp/cache/CacheTypes.h"
5 #include "zypp/cache/ResolvableQuery.h"
6 #include "zypp/Package.h"
7 #include "zypp/cache/sqlite3x/sqlite3x.hpp"
9 using namespace sqlite3x;
12 namespace zypp { namespace cache {
15 struct ResolvableQuery::Impl
19 CacheTypes _type_cache;
21 Impl( const Pathname &dbdir)
25 _fields = "id, name, version, release, epoch, arch, kind, installed_size, archive_size, install_only, build_time, install_time, repository_id";
32 data::ResObject_Ptr fromRow( sqlite3_reader &reader )
34 data::ResObject_Ptr ptr (new data::ResObject);
36 ptr->name = reader.getstring(1);
37 ptr->edition = Edition( reader.getstring(2), reader.getstring(3), reader.getint(4));
38 ptr->arch = _type_cache.archFor(reader.getint(5));
40 // TODO get the rest of the data
46 void query( const data::RecordId &id,
47 ProcessResolvable fnc )
49 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
50 //con.executenonquery("PRAGMA cache_size=8000;");
51 con.executenonquery("BEGIN;");
52 sqlite3_command cmd( con, "select " + _fields + " from resolvables where id=:id;");
54 sqlite3_reader reader = cmd.executereader();
57 fnc( id, fromRow(reader) );
59 con.executenonquery("COMMIT;");
63 void query( const std::string &s,
64 ProcessResolvable fnc )
67 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
68 //con.executenonquery("PRAGMA cache_size=8000;");
69 con.executenonquery("BEGIN;");
70 sqlite3_command cmd( con, "select " + _fields + " from resolvables where name like '%:name%';");
72 sqlite3_reader reader = cmd.executereader();
75 fnc( reader.getint64(0), fromRow(reader) );
77 con.executenonquery("COMMIT;");
81 std::string queryStringAttribute( const data::RecordId &record_id,
82 const std::string &klass,
83 const std::string &name,
84 const std::string &default_value )
86 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
88 return queryStringAttributeTranslationInternal( con, record_id, Locale(), klass, name, default_value);
90 catch ( const Exception &e )
97 std::string queryStringAttributeTranslation( const data::RecordId &record_id,
99 const std::string &klass,
100 const std::string &name,
101 const std::string &default_value )
103 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
105 return queryStringAttributeTranslationInternal( con, record_id, locale, klass, name, default_value );
107 catch ( const Exception &e )
114 TranslatedText queryTranslatedStringAttribute( const data::RecordId &record_id,
115 const std::string &klass,
116 const std::string &name,
117 const TranslatedText &default_value )
119 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
121 return queryTranslatedStringAttributeInternal( con, record_id, klass, name, default_value );
123 catch ( const Exception &e )
130 bool queryBooleanAttribute( const data::RecordId &record_id,
131 const std::string &klass,
132 const std::string &name,
135 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
137 return queryNumericAttributeInternal( con, record_id, klass, name, default_value);
139 catch ( const Exception &e )
145 int queryNumericAttribute( const data::RecordId &record_id,
146 const std::string &klass,
147 const std::string &name,
150 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
152 return queryNumericAttributeInternal( con, record_id, klass, name, default_value);
154 catch ( const Exception &e )
162 int queryNumericAttributeInternal( sqlite3_connection &con,
163 const data::RecordId &record_id,
164 const std::string &klass,
165 const std::string &name,
168 con.executenonquery("BEGIN;");
169 sqlite3_command cmd( con, "select a.value from numeric_attributes a,types t where a.weak_resolvable_id=:rid and a.attr_id=t.id and t.class=:tclass and t.name=:tname;");
171 cmd.bind(":rid", record_id);
173 cmd.bind(":tclass", klass);
174 cmd.bind(":tname", name);
176 sqlite3_reader reader = cmd.executereader();
178 return reader.getint(0);
180 return default_value;
183 TranslatedText queryTranslatedStringAttributeInternal( sqlite3_connection &con,
184 const data::RecordId &record_id,
185 const std::string &klass,
186 const std::string &name,
187 const TranslatedText &default_value )
189 //con.executenonquery("PRAGMA cache_size=8000;");
190 con.executenonquery("BEGIN;");
191 sqlite3_command cmd( con, "select a.text, l.name from text_attributes a,types l,types t where a.weak_resolvable_id=:rid and a.lang_id=l.id and a.attr_id=t.id and l.class=:lclass and t.class=:tclass and t.name=:tname;");
193 cmd.bind(":rid", record_id);
194 cmd.bind(":lclass", "lang");
196 cmd.bind(":tclass", klass);
197 cmd.bind(":tname", name);
199 TranslatedText result;
200 sqlite3_reader reader = cmd.executereader();
203 result.setText( reader.getstring(0), Locale( reader.getstring(1) ) );
207 return default_value;
210 std::string queryStringAttributeInternal( sqlite3_connection &con,
211 const data::RecordId &record_id,
212 const std::string &klass,
213 const std::string &name,
214 const std::string &default_value )
216 return queryStringAttributeTranslationInternal( con, record_id, Locale(), klass, name, default_value);
219 std::string queryStringAttributeTranslationInternal( sqlite3_connection &con,
220 const data::RecordId &record_id,
221 const Locale &locale,
222 const std::string &klass,
223 const std::string &name,
224 const std::string &default_value )
226 //con.executenonquery("PRAGMA cache_size=8000;");
227 con.executenonquery("BEGIN;");
228 sqlite3_command cmd( con, "select a.text from text_attributes a,types l,types t where a.weak_resolvable_id=:rid and a.lang_id=l.id and a.attr_id=t.id and l.class=:lclass and l.name=:lname and t.class=:tclass and t.name=:tname;");
230 cmd.bind(":rid", record_id);
231 cmd.bind(":lclass", "lang");
232 if (locale == Locale() )
233 cmd.bind(":lname", "none");
235 cmd.bind(":lname", locale.code());
237 cmd.bind(":tclass", klass);
238 cmd.bind(":tname", name);
240 sqlite3_reader reader = cmd.executereader();
242 return reader.getstring(0);
244 return default_value;
248 //////////////////////////////////////////////////////////////////////////////
249 // FORWARD TO IMPLEMENTATION
250 //////////////////////////////////////////////////////////////////////////////
252 ResolvableQuery::ResolvableQuery( const Pathname &dbdir)
253 : _pimpl(new Impl(dbdir))
257 //////////////////////////////////////////////////////////////////////////////
259 void ResolvableQuery::query( const data::RecordId &id, ProcessResolvable fnc )
261 _pimpl->query(id, fnc);
264 //////////////////////////////////////////////////////////////////////////////
266 void ResolvableQuery::query( const std::string &s, ProcessResolvable fnc )
268 _pimpl->query(s, fnc);
271 //////////////////////////////////////////////////////////////////////////////
273 int ResolvableQuery::queryNumericAttribute( const data::RecordId &record_id,
274 const std::string &klass,
275 const std::string &name,
278 return _pimpl->queryNumericAttribute(record_id, klass, name, default_value);
281 bool ResolvableQuery::queryBooleanAttribute( const data::RecordId &record_id,
282 const std::string &klass,
283 const std::string &name,
286 return _pimpl->queryNumericAttribute(record_id, klass, name, default_value);
290 std::string ResolvableQuery::queryStringAttribute( const data::RecordId &record_id,
291 const std::string &klass,
292 const std::string &name,
293 const std::string &default_value )
295 return _pimpl->queryStringAttribute(record_id, klass, name, default_value);
298 //////////////////////////////////////////////////////////////////////////////
300 std::string ResolvableQuery::queryStringAttributeTranslation( const data::RecordId &record_id,
301 const Locale &locale,
302 const std::string &klass,
303 const std::string &name,
304 const std::string &default_value )
306 return _pimpl->queryStringAttributeTranslation(record_id, locale, klass, name, default_value );
309 //////////////////////////////////////////////////////////////////////////////
311 TranslatedText ResolvableQuery::queryTranslatedStringAttribute( const data::RecordId &record_id,
312 const std::string &klass,
313 const std::string &name,
314 const TranslatedText &default_value )
316 return _pimpl->queryTranslatedStringAttribute(record_id, klass, name, default_value );
319 //////////////////////////////////////////////////////////////////////////////
321 } } // namespace zypp::cache