media-export: Enable use of SQL functions
authorJens Georg <mail@jensge.org>
Wed, 25 Aug 2010 17:08:15 +0000 (20:08 +0300)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Thu, 26 Aug 2010 14:56:42 +0000 (17:56 +0300)
Search translation was hard-coded to use operators

src/plugins/media-export/Makefile.am
src/plugins/media-export/rygel-media-export-media-cache.vala
src/plugins/media-export/rygel-media-export-sql-function.vala [new file with mode: 0644]
src/plugins/media-export/rygel-media-export-sql-operator.vala [new file with mode: 0644]

index 772aa3a..b2aabbb 100644 (file)
@@ -25,6 +25,8 @@ librygel_media_export_la_SOURCES = rygel-media-export-plugin.vala \
                                   rygel-media-export-db-container.vala \
                                   rygel-media-export-sql-factory.vala \
                                   rygel-media-export-media-cache.vala \
+                                  rygel-media-export-sql-operator.vala \
+                                  rygel-media-export-sql-function.vala \
                                   rygel-media-export-media-cache-upgrader.vala \
                                   rygel-media-export-metadata-extractor.vala \
                                   rygel-media-export-null-container.vala \
index 75aee0b..b1aec0f 100644 (file)
@@ -728,66 +728,56 @@ public class Rygel.MediaExport.MediaCache : Object {
     private string? relational_expression_to_sql (RelationalExpression? exp,
                                                   GLib.ValueArray       args)
                                                   throws Error {
-        string sql_function = null;
         GLib.Value? v = null;
         string collate = null;
 
         string column = map_operand_to_column (exp.operand1, out collate);
+        SqlOperator operator;
 
         switch (exp.op) {
             case SearchCriteriaOp.EXISTS:
+                string sql_function;
                 if (exp.operand2 == "true") {
                     sql_function = "IS NOT NULL AND %s != ''";
                 } else {
                     sql_function = "IS NULL OR %s = ''";
                 }
+                operator = new SqlOperator (sql_function, column);
                 break;
             case SearchCriteriaOp.EQ:
-                sql_function = "=";
-                v = exp.operand2;
-                break;
             case SearchCriteriaOp.NEQ:
-                sql_function = "!=";
-                v = exp.operand2;
-                break;
             case SearchCriteriaOp.LESS:
-                sql_function = "<";
-                v = exp.operand2;
-                break;
             case SearchCriteriaOp.LEQ:
-                sql_function = "<=";
-                v = exp.operand2;
-                break;
             case SearchCriteriaOp.GREATER:
-                sql_function = ">";
-                v = exp.operand2;
-                break;
             case SearchCriteriaOp.GEQ:
-                sql_function = ">=";
                 v = exp.operand2;
+                operator = new SqlOperator.from_search_criteria_op (
+                                        exp.op,
+                                        column,
+                                        collate);
                 break;
             case SearchCriteriaOp.CONTAINS:
-                sql_function = "LIKE";
+                operator = new SqlOperator ("LIKE", column);
                 v = "%%%s%%".printf (exp.operand2);
                 break;
             case SearchCriteriaOp.DOES_NOT_CONTAIN:
-                sql_function = "NOT LIKE";
+                operator = new SqlOperator ("NOT LIKE", column);
                 v = "%%%s%%".printf (exp.operand2);
                 break;
             case SearchCriteriaOp.DERIVED_FROM:
-                sql_function = "LIKE";
-                v = "%s%%".printf (exp.operand2);
+                operator = new SqlFunction ("has_prefix", column);
+                v = exp.operand2;
                 break;
             default:
                 warning ("Unsupported op %d", exp.op);
-                break;
+                return null;
         }
 
         if (v != null) {
             args.append (v);
         }
 
-        return "(%s %s ? %s)".printf (column, sql_function, collate);
+        return operator.to_string ();
     }
 
     public Gee.List<string> get_meta_data_column_by_filter (
diff --git a/src/plugins/media-export/rygel-media-export-sql-function.vala b/src/plugins/media-export/rygel-media-export-sql-function.vala
new file mode 100644 (file)
index 0000000..0ff0a80
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2010 Jens Georg <mail@jensge.org>.
+ *
+ * Author: Jens Georg <mail@jensge.org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel 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 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+internal class Rygel.MediaExport.SqlFunction : SqlOperator {
+    public SqlFunction (string name, string arg) {
+        base (name, arg);
+    }
+
+    public override string to_string () {
+        return "%s(%s,?)".printf (name, arg);
+    }
+}
diff --git a/src/plugins/media-export/rygel-media-export-sql-operator.vala b/src/plugins/media-export/rygel-media-export-sql-operator.vala
new file mode 100644 (file)
index 0000000..3620091
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2010 Jens Georg <mail@jensge.org>.
+ *
+ * Author: Jens Georg <mail@jensge.org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel 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 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using GUPnP;
+
+internal class Rygel.MediaExport.SqlOperator : GLib.Object {
+    protected string name;
+    protected string arg;
+    protected string collate;
+
+    public SqlOperator (string name,
+                        string arg,
+                        string collate = "") {
+        this.name = name;
+        this.arg = arg;
+        this.collate = collate;
+    }
+
+    public SqlOperator.from_search_criteria_op (SearchCriteriaOp op,
+                                                string           arg,
+                                                string           collate) {
+        string sql = null;
+        switch (op) {
+            case SearchCriteriaOp.EQ:
+                sql = "=";
+                break;
+            case SearchCriteriaOp.NEQ:
+                sql = "!=";
+                break;
+            case SearchCriteriaOp.LESS:
+                sql = "<";
+                break;
+            case SearchCriteriaOp.LEQ:
+                sql = "<=";
+                break;
+            case SearchCriteriaOp.GREATER:
+                sql = ">";
+                break;
+            case SearchCriteriaOp.GEQ:
+                sql = ">=";
+                break;
+            default:
+                assert_not_reached ();
+        }
+
+        this (sql, arg, collate);
+    }
+
+    public virtual string to_string () {
+        return "(%s %s ? %s)".printf (arg, name, collate);
+    }
+}
+
+