2 #include "zypp/cache/CacheTypes.h"
3 #include "zypp/cache/ResolvableQuery.h"
4 #include "zypp/cache/sqlite3x/sqlite3x.hpp"
6 using namespace sqlite3x;
9 namespace zypp { namespace cache {
12 struct ResolvableQuery::Impl
16 CacheTypes _type_cache;
18 Impl( const Pathname &dbdir)
22 _fields = "id, name, version, release, epoch, arch, kind, installed_size, archive_size, install_only, build_time, install_time, repository_id";
30 data::ResObject_Ptr fromRow( sqlite3_reader &reader )
32 data::ResObject_Ptr ptr (new data::ResObject);
34 ptr->name = reader.getstring(1);
35 ptr->edition = Edition( reader.getstring(2), reader.getstring(3), reader.getint(4));
36 ptr->arch = _type_cache.archFor(reader.getint(5));
38 // TODO get the rest of the data
44 void query( const data::RecordId &id,
45 ProcessResolvable fnc )
47 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
48 //con.executenonquery("PRAGMA cache_size=8000;");
49 con.executenonquery("BEGIN;");
50 sqlite3_command cmd( con, "select " + _fields + " from resolvables where id=:id;");
52 sqlite3_reader reader = cmd.executereader();
55 fnc( id, fromRow(reader) );
57 con.executenonquery("COMMIT;");
61 void query( const std::string &s,
62 ProcessResolvable fnc )
65 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
66 //con.executenonquery("PRAGMA cache_size=8000;");
67 con.executenonquery("BEGIN;");
68 sqlite3_command cmd( con, "select " + _fields + " from resolvables where name like '%:name%';");
70 sqlite3_reader reader = cmd.executereader();
73 fnc( reader.getint64(0), fromRow(reader) );
75 con.executenonquery("COMMIT;");
79 std::string queryStringAttribute( const data::RecordId &record_id,
80 const std::string &klass,
81 const std::string &name )
83 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
84 return queryStringAttributeTranslationInternal( con, record_id, Locale(), klass, name);
88 std::string queryStringAttributeTranslation( const data::RecordId &record_id,
90 const std::string &klass,
91 const std::string &name )
93 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
94 return queryStringAttributeTranslationInternal( con, record_id, locale, klass, name );
98 TranslatedText queryTranslatedStringAttribute( const data::RecordId &record_id,
99 const std::string &klass,
100 const std::string &name )
102 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
103 return queryTranslatedStringAttributeInternal( con, record_id, klass, name );
107 int queryNumericAttribute( const data::RecordId &record_id,
108 const std::string &klass,
109 const std::string &name )
111 sqlite3_connection con((_dbdir + "zypp.db").asString().c_str());
112 return queryNumericAttributeInternal( con, record_id, klass, name);
117 int queryNumericAttributeInternal( sqlite3_connection &con,
118 const data::RecordId &record_id,
119 const std::string &klass,
120 const std::string &name )
122 con.executenonquery("BEGIN;");
123 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;");
125 cmd.bind(":rid", record_id);
127 cmd.bind(":tclass", klass);
128 cmd.bind(":tname", name);
130 return cmd.executeint();
133 TranslatedText queryTranslatedStringAttributeInternal( sqlite3_connection &con,
134 const data::RecordId &record_id,
135 const std::string &klass,
136 const std::string &name )
138 //con.executenonquery("PRAGMA cache_size=8000;");
139 con.executenonquery("BEGIN;");
140 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;");
142 cmd.bind(":rid", record_id);
143 cmd.bind(":lclass", "lang");
145 cmd.bind(":tclass", klass);
146 cmd.bind(":tname", name);
148 TranslatedText result;
149 sqlite3_reader reader = cmd.executereader();
152 result.setText( reader.getstring(0), Locale( reader.getstring(1) ) );
157 std::string queryStringAttributeInternal( sqlite3_connection &con,
158 const data::RecordId &record_id,
159 const std::string &klass,
160 const std::string &name )
162 return queryStringAttributeTranslationInternal( con, record_id, Locale(), klass, name);
165 std::string queryStringAttributeTranslationInternal( sqlite3_connection &con,
166 const data::RecordId &record_id,
167 const Locale &locale,
168 const std::string &klass,
169 const std::string &name )
171 //con.executenonquery("PRAGMA cache_size=8000;");
172 con.executenonquery("BEGIN;");
173 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;");
175 cmd.bind(":rid", record_id);
176 cmd.bind(":lclass", "lang");
177 if (locale == Locale() )
178 cmd.bind(":lname", "none");
180 cmd.bind(":lname", locale.code());
182 cmd.bind(":tclass", klass);
183 cmd.bind(":tname", name);
185 return cmd.executestring();
189 //////////////////////////////////////////////////////////////////////////////
190 // FORWARD TO IMPLEMENTATION
191 //////////////////////////////////////////////////////////////////////////////
193 ResolvableQuery::ResolvableQuery( const Pathname &dbdir)
194 : _pimpl(new Impl(dbdir))
198 //////////////////////////////////////////////////////////////////////////////
200 void ResolvableQuery::query( const data::RecordId &id, ProcessResolvable fnc )
202 _pimpl->query(id, fnc);
205 //////////////////////////////////////////////////////////////////////////////
207 void ResolvableQuery::query( const std::string &s, ProcessResolvable fnc )
209 _pimpl->query(s, fnc);
212 //////////////////////////////////////////////////////////////////////////////
214 int ResolvableQuery::queryNumericAttribute( const data::RecordId &record_id,
215 const std::string &klass,
216 const std::string &name )
218 return _pimpl->queryNumericAttribute(record_id, klass, name);
221 bool ResolvableQuery::queryBooleanAttribute( const data::RecordId &record_id,
222 const std::string &klass,
223 const std::string &name )
225 return _pimpl->queryNumericAttribute(record_id, klass, name);
229 std::string ResolvableQuery::queryStringAttribute( const data::RecordId &record_id,
230 const std::string &klass,
231 const std::string &name )
233 return _pimpl->queryStringAttribute(record_id, klass, name);
236 //////////////////////////////////////////////////////////////////////////////
238 std::string ResolvableQuery::queryStringAttributeTranslation( const data::RecordId &record_id,
239 const Locale &locale,
240 const std::string &klass,
241 const std::string &name )
243 return _pimpl->queryStringAttributeTranslation(record_id, locale, klass, name);
246 //////////////////////////////////////////////////////////////////////////////
248 TranslatedText ResolvableQuery::queryTranslatedStringAttribute( const data::RecordId &record_id,
249 const std::string &klass,
250 const std::string &name )
252 return _pimpl->queryTranslatedStringAttribute(record_id, klass, name);
255 //////////////////////////////////////////////////////////////////////////////
257 } } // namespace zypp::cache