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());
90 value = queryStringAttributeTranslationInternal( con, record_id, Locale(), klass, name);
92 catch ( const Exception &e )
102 std::string queryStringAttributeTranslation( const data::RecordId &record_id,
103 const Locale &locale,
104 const std::string &klass,
105 const std::string &name,
106 const std::string &default_value )
108 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
111 value = queryStringAttributeTranslationInternal( con, record_id, locale, klass, name );
113 catch ( const Exception &e )
116 return default_value;
122 TranslatedText queryTranslatedStringAttribute( const data::RecordId &record_id,
123 const std::string &klass,
124 const std::string &name,
125 const TranslatedText &default_value )
127 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
128 TranslatedText value;
130 value = queryTranslatedStringAttributeInternal( con, record_id, klass, name );
132 catch ( const Exception &e )
135 return default_value;
141 bool queryBooleanAttribute( const data::RecordId &record_id,
142 const std::string &klass,
143 const std::string &name,
146 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
149 value = queryNumericAttributeInternal( con, record_id, klass, name);
151 catch ( const Exception &e )
154 return default_value;
159 int queryNumericAttribute( const data::RecordId &record_id,
160 const std::string &klass,
161 const std::string &name,
164 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
167 n = queryNumericAttributeInternal( con, record_id, klass, name);
169 catch ( const Exception &e )
172 return default_value;
179 int queryNumericAttributeInternal( sqlite3_connection &con,
180 const data::RecordId &record_id,
181 const std::string &klass,
182 const std::string &name )
184 con.executenonquery("BEGIN;");
185 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;");
187 cmd.bind(":rid", record_id);
189 cmd.bind(":tclass", klass);
190 cmd.bind(":tname", name);
192 return cmd.executeint();
195 TranslatedText queryTranslatedStringAttributeInternal( sqlite3_connection &con,
196 const data::RecordId &record_id,
197 const std::string &klass,
198 const std::string &name )
200 //con.executenonquery("PRAGMA cache_size=8000;");
201 con.executenonquery("BEGIN;");
202 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;");
204 cmd.bind(":rid", record_id);
205 cmd.bind(":lclass", "lang");
207 cmd.bind(":tclass", klass);
208 cmd.bind(":tname", name);
210 TranslatedText result;
211 sqlite3_reader reader = cmd.executereader();
214 result.setText( reader.getstring(0), Locale( reader.getstring(1) ) );
219 std::string queryStringAttributeInternal( sqlite3_connection &con,
220 const data::RecordId &record_id,
221 const std::string &klass,
222 const std::string &name )
224 return queryStringAttributeTranslationInternal( con, record_id, Locale(), klass, name);
227 std::string queryStringAttributeTranslationInternal( sqlite3_connection &con,
228 const data::RecordId &record_id,
229 const Locale &locale,
230 const std::string &klass,
231 const std::string &name )
233 //con.executenonquery("PRAGMA cache_size=8000;");
234 con.executenonquery("BEGIN;");
235 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;");
237 cmd.bind(":rid", record_id);
238 cmd.bind(":lclass", "lang");
239 if (locale == Locale() )
240 cmd.bind(":lname", "none");
242 cmd.bind(":lname", locale.code());
244 cmd.bind(":tclass", klass);
245 cmd.bind(":tname", name);
247 return cmd.executestring();
251 //////////////////////////////////////////////////////////////////////////////
252 // FORWARD TO IMPLEMENTATION
253 //////////////////////////////////////////////////////////////////////////////
255 ResolvableQuery::ResolvableQuery( const Pathname &dbdir)
256 : _pimpl(new Impl(dbdir))
260 //////////////////////////////////////////////////////////////////////////////
262 void ResolvableQuery::query( const data::RecordId &id, ProcessResolvable fnc )
264 _pimpl->query(id, fnc);
267 //////////////////////////////////////////////////////////////////////////////
269 void ResolvableQuery::query( const std::string &s, ProcessResolvable fnc )
271 _pimpl->query(s, fnc);
274 //////////////////////////////////////////////////////////////////////////////
276 int ResolvableQuery::queryNumericAttribute( const data::RecordId &record_id,
277 const std::string &klass,
278 const std::string &name,
281 return _pimpl->queryNumericAttribute(record_id, klass, name, default_value);
284 bool ResolvableQuery::queryBooleanAttribute( const data::RecordId &record_id,
285 const std::string &klass,
286 const std::string &name,
289 return _pimpl->queryNumericAttribute(record_id, klass, name, default_value);
293 std::string ResolvableQuery::queryStringAttribute( const data::RecordId &record_id,
294 const std::string &klass,
295 const std::string &name,
296 const std::string &default_value )
298 return _pimpl->queryStringAttribute(record_id, klass, name, default_value);
301 //////////////////////////////////////////////////////////////////////////////
303 std::string ResolvableQuery::queryStringAttributeTranslation( const data::RecordId &record_id,
304 const Locale &locale,
305 const std::string &klass,
306 const std::string &name,
307 const std::string &default_value )
309 return _pimpl->queryStringAttributeTranslation(record_id, locale, klass, name, default_value );
312 //////////////////////////////////////////////////////////////////////////////
314 TranslatedText ResolvableQuery::queryTranslatedStringAttribute( const data::RecordId &record_id,
315 const std::string &klass,
316 const std::string &name,
317 const TranslatedText &default_value )
319 return _pimpl->queryTranslatedStringAttribute(record_id, klass, name, default_value );
322 //////////////////////////////////////////////////////////////////////////////
324 } } // namespace zypp::cache