build,core,plugins: Port to GDBus and GVariant
[profile/ivi/rygel.git] / src / plugins / tracker / rygel-tracker-selection-query.vala
1 /*
2  * Copyright (C) 20010 Nokia Corporation.
3  *
4  * Author: Zeeshan Ali <zeenix@gmail.com>
5  *
6  * This file is part of Rygel.
7  *
8  * Rygel is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Rygel is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 using Gee;
24
25 /**
26  * Represents Tracker SPARQL Selection query
27  */
28 public class Rygel.Tracker.SelectionQuery : Query {
29     public ArrayList<string> variables;
30     public ArrayList<string> filters;
31
32     public string order_by;
33     public int offset;
34     public int max_count;
35
36     public string[,] result;
37
38     public SelectionQuery (ArrayList<string>  variables,
39                            QueryTriplets      triplets,
40                            ArrayList<string>? filters,
41                            string?            order_by = null,
42                            int                offset = 0,
43                            int                max_count = -1) {
44         base (triplets);
45
46         if (filters != null) {
47             this.filters = filters;
48         } else {
49             this.filters = new ArrayList<string> ();
50         }
51
52         this.variables = variables;
53         this.order_by = order_by;
54         this.offset = offset;
55         this.max_count = max_count;
56     }
57
58     public SelectionQuery.clone (SelectionQuery query) {
59         this (copy_str_list (query.variables),
60               new QueryTriplets.clone (query.triplets),
61               copy_str_list (query.filters),
62               query.order_by,
63               query.offset,
64               query.max_count);
65     }
66
67     public override async void execute (ResourcesIface resources)
68                                         throws IOError {
69         var str = this.to_string ();
70
71         debug ("Executing SPARQL query: %s", str);
72
73         result = yield resources.sparql_query (str);
74     }
75
76     public override string to_string () {
77         var query = "SELECT ";
78
79         foreach (var variable in this.variables) {
80             query += " " + variable;
81         }
82
83         query += " WHERE { " + base.to_string ();
84
85         if (this.filters.size > 0) {
86             query += " FILTER (";
87             for (var i = 0; i < this.filters.size; i++) {
88                 query += this.filters[i];
89
90                 if (i < this.filters.size - 1) {
91                     query += " && ";
92                 }
93             }
94             query += ")";
95         }
96
97         query += " }";
98
99         if (this.order_by != null) {
100             query += " ORDER BY " + order_by;
101         }
102
103         if (this.offset > 0) {
104             query += " OFFSET " + this.offset.to_string ();
105         }
106
107         if (this.max_count > 0) {
108             query += " LIMIT " + this.max_count.to_string ();
109         }
110
111         return query;
112     }
113
114     private static ArrayList<string> copy_str_list (Gee.List<string> str_list) {
115         var copy = new ArrayList<string> ();
116
117         copy.add_all (str_list);
118
119         return copy;
120     }
121 }