diff options
author | Carlos Garnacho <carlosg@gnome.org> | 2020-03-01 15:43:39 +0100 |
---|---|---|
committer | Carlos Garnacho <carlosg@gnome.org> | 2020-03-02 13:22:53 +0100 |
commit | 81508cb02eda42c20421069758cd25898f81f45b (patch) | |
tree | 7c63528e617a3cb7445eca991f2d35c4af6c56fe | |
parent | c34aa247d8ded5171635a87830bb2327f2416304 (diff) | |
download | tracker-81508cb02eda42c20421069758cd25898f81f45b.tar.gz |
libtracker-bus: Implement TrackerSparqlStatement
Make it possible to create prepared statements on bus connections.
Closes: https://gitlab.gnome.org/GNOME/tracker/issues/179
-rw-r--r-- | src/libtracker-bus/meson.build | 1 | ||||
-rw-r--r-- | src/libtracker-bus/tracker-bus-statement.vala | 93 | ||||
-rw-r--r-- | src/libtracker-bus/tracker-bus.vala | 4 |
3 files changed, 98 insertions, 0 deletions
diff --git a/src/libtracker-bus/meson.build b/src/libtracker-bus/meson.build index 775afc73f..91fd74c34 100644 --- a/src/libtracker-bus/meson.build +++ b/src/libtracker-bus/meson.build @@ -2,6 +2,7 @@ libtracker_bus = static_library('tracker-bus', 'tracker-bus.vala', 'tracker-namespace.vala', 'tracker-bus-fd-cursor.vala', + 'tracker-bus-statement.vala', '../libtracker-common/libtracker-common.vapi', tracker_common_enum_header, c_args: tracker_c_args + [ diff --git a/src/libtracker-bus/tracker-bus-statement.vala b/src/libtracker-bus/tracker-bus-statement.vala new file mode 100644 index 000000000..e5d78b933 --- /dev/null +++ b/src/libtracker-bus/tracker-bus-statement.vala @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2020, Red Hat Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * Author: Carlos Garnacho <carlosg@gnome.org> + */ + +public class Tracker.Bus.Statement : Tracker.Sparql.Statement { + private DBusConnection bus; + private string query; + private string dbus_name; + private string object_path; + private HashTable<string,GLib.Variant> arguments; + + private const string ENDPOINT_IFACE = "org.freedesktop.Tracker1.Endpoint"; + + public Statement (DBusConnection bus, string dbus_name, string object_path, string query) { + Object (); + this.bus = bus; + this.dbus_name = dbus_name; + this.object_path = object_path; + this.query = query; + this.arguments = new HashTable<string, GLib.Variant> (str_hash, str_equal); + } + + public override void bind_boolean (string name, bool value) { + this.arguments.insert (name, new GLib.Variant.boolean (value)); + } + + public override void bind_double (string name, double value) { + this.arguments.insert (name, new GLib.Variant.double (value)); + } + + public override void bind_int (string name, int64 value) { + this.arguments.insert (name, new GLib.Variant.int64 (value)); + } + + public override void bind_string (string name, string value) { + this.arguments.insert (name, new GLib.Variant.string (value)); + } + + public override void clear_bindings () { + this.arguments.remove_all (); + } + + private VariantBuilder? get_arguments () { + if (this.arguments.size () == 0) + return null; + + VariantBuilder builder = new VariantBuilder (new VariantType ("a{sv}")); + HashTableIter<string, Variant> iter = HashTableIter<string, Variant> (this.arguments); + unowned string arg; + unowned GLib.Variant value; + + while (iter.next (out arg, out value)) + builder.add ("{sv}", arg, value); + + return builder; + } + + public override Sparql.Cursor execute (GLib.Cancellable? cancellable) throws Sparql.Error, GLib.Error, GLib.IOError, GLib.DBusError { + // use separate main context for sync operation + var context = new MainContext (); + var loop = new MainLoop (context, false); + context.push_thread_default (); + AsyncResult async_res = null; + execute_async.begin (cancellable, (o, res) => { + async_res = res; + loop.quit (); + }); + loop.run (); + context.pop_thread_default (); + return execute_async.end (async_res); + } + + public async override Sparql.Cursor execute_async (GLib.Cancellable? cancellable) throws Sparql.Error, GLib.Error, GLib.IOError, GLib.DBusError { + return yield Tracker.Bus.Connection.perform_query_call (bus, dbus_name, object_path, query, get_arguments (), cancellable); + } +} diff --git a/src/libtracker-bus/tracker-bus.vala b/src/libtracker-bus/tracker-bus.vala index 9b5453ecf..1a0d01db4 100644 --- a/src/libtracker-bus/tracker-bus.vala +++ b/src/libtracker-bus/tracker-bus.vala @@ -123,6 +123,10 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection { return yield perform_query_call (bus, dbus_name, object_path, sparql, null, cancellable); } + public override Sparql.Statement? query_statement (string sparql, GLib.Cancellable? cancellable = null) throws Sparql.Error { + return new Bus.Statement (bus, dbus_name, object_path, sparql); + } + void send_update (string method, UnixInputStream input, Cancellable? cancellable, AsyncReadyCallback? callback) throws GLib.Error, GLib.IOError { var message = new DBusMessage.method_call (dbus_name, object_path, ENDPOINT_IFACE, method); var fd_list = new UnixFDList (); |