[rygel] media-export: Enable use of SQL functions



commit a47f2f4ac100de251bf5161c9d7dbc952d4b2211
Author: Jens Georg <mail jensge org>
Date:   Wed Aug 25 20:08:15 2010 +0300

    media-export: Enable use of SQL functions
    
    Search translation was hard-coded to use operators

 src/plugins/media-export/Makefile.am               |    2 +
 .../rygel-media-export-media-cache.vala            |   36 ++++------
 .../rygel-media-export-sql-function.vala           |   31 ++++++++
 .../rygel-media-export-sql-operator.vala           |   73 ++++++++++++++++++++
 4 files changed, 119 insertions(+), 23 deletions(-)
---
diff --git a/src/plugins/media-export/Makefile.am b/src/plugins/media-export/Makefile.am
index 772aa3a..b2aabbb 100644
--- a/src/plugins/media-export/Makefile.am
+++ b/src/plugins/media-export/Makefile.am
@@ -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 \
diff --git a/src/plugins/media-export/rygel-media-export-media-cache.vala b/src/plugins/media-export/rygel-media-export-media-cache.vala
index 75aee0b..b1aec0f 100644
--- a/src/plugins/media-export/rygel-media-export-media-cache.vala
+++ b/src/plugins/media-export/rygel-media-export-media-cache.vala
@@ -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
index 0000000..0ff0a80
--- /dev/null
+++ b/src/plugins/media-export/rygel-media-export-sql-function.vala
@@ -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
index 0000000..3620091
--- /dev/null
+++ b/src/plugins/media-export/rygel-media-export-sql-operator.vala
@@ -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);
+    }
+}
+
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]