1 | /* |
---|
2 | Copyright (C) 2012 |
---|
3 | Alejandro Mujica (amujica@cenditel.gob.ve) |
---|
4 | Erwin Paredes (eparedes@cenditel.gob.ve) |
---|
5 | José Ruiz (jruiz@cenditel.gob.ve) |
---|
6 | Rodolfo Rangel (rrangel@cenditel.gob.ve) |
---|
7 | Julie Vera (jvera@cenditel.gob.ve) |
---|
8 | |
---|
9 | CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en |
---|
10 | Tecnologías Libres |
---|
11 | |
---|
12 | Este programa es software libre; Usted puede usarlo bajo los términos de la |
---|
13 | licencia de software GPL versión 2.0 de la Free Software Foundation. |
---|
14 | |
---|
15 | Este programa se distribuye con la esperanza de que sea útil, pero SIN |
---|
16 | NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o |
---|
17 | ADECUACIÓN A UN PROPÓSITO PARTICULAR. |
---|
18 | Consulte la licencia GPL para más detalles. Usted debe recibir una copia |
---|
19 | de la GPL junto con este programa; si no, escriba a la Free Software |
---|
20 | Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | Autor: Alejandro J. Mujica |
---|
25 | Fecha de creación: 25/07/2013 |
---|
26 | Este archivo contiene la definición de la clase StrQuery. |
---|
27 | */ |
---|
28 | |
---|
29 | # ifndef StrQuery_H |
---|
30 | # define StrQuery_H |
---|
31 | |
---|
32 | # include <iostream> |
---|
33 | # include <string> |
---|
34 | |
---|
35 | /** Representa una cadena de consulta que se construye dinámicamente por las |
---|
36 | * operaciones select, from, y where. |
---|
37 | * |
---|
38 | * @author Alejandro J. Mujica |
---|
39 | */ |
---|
40 | class StrQuery { |
---|
41 | |
---|
42 | std::string select; |
---|
43 | |
---|
44 | std::string from; |
---|
45 | |
---|
46 | std::string where; |
---|
47 | |
---|
48 | std::string orderBy; |
---|
49 | |
---|
50 | std::string orderByOption; |
---|
51 | |
---|
52 | public: |
---|
53 | StrQuery(); |
---|
54 | |
---|
55 | StrQuery(const StrQuery &); |
---|
56 | |
---|
57 | StrQuery(StrQuery &&); |
---|
58 | |
---|
59 | /// Limpia la cadena. |
---|
60 | void clear(); |
---|
61 | |
---|
62 | /// Consulta la sección select. |
---|
63 | const std::string & getSelect() const; |
---|
64 | |
---|
65 | /// Asigna la sección select (l-values) |
---|
66 | void setSelect(const std::string &); |
---|
67 | |
---|
68 | /// Asigna la sección select (r-values) |
---|
69 | void setSelect(std::string &&); |
---|
70 | |
---|
71 | /// Consulta la sección from. |
---|
72 | const std::string & getFrom() const; |
---|
73 | |
---|
74 | /// Asigna la sección from (l-values) |
---|
75 | void setFrom(const std::string &); |
---|
76 | |
---|
77 | /// Asigna la sección from (r-values) |
---|
78 | void setFrom(std::string &&); |
---|
79 | |
---|
80 | /// Consulta la sección where. |
---|
81 | const std::string & getWhere() const; |
---|
82 | |
---|
83 | /// Asigna la sección where (l-values) |
---|
84 | void setWhere(const std::string &); |
---|
85 | |
---|
86 | /// Asigna la sección where (r-values) |
---|
87 | void setWhere(std::string &&); |
---|
88 | |
---|
89 | /// Consulta la sección orderBy. |
---|
90 | const std::string & getOrderBy() const; |
---|
91 | |
---|
92 | /// Asigna la sección orderBy (l-values) |
---|
93 | void setOrderBy(const std::string &); |
---|
94 | |
---|
95 | /// Asigna la sección orderBy (r-values) |
---|
96 | void setOrderBy(std::string &&); |
---|
97 | |
---|
98 | /// Consulta la sección orderByOption. |
---|
99 | const std::string & getOrderByOption() const; |
---|
100 | |
---|
101 | /// Asigna la sección orderByOption (l-values) |
---|
102 | void setOrderByOption(const std::string &); |
---|
103 | |
---|
104 | /// Asigna la sección orderByOption (r-values) |
---|
105 | void setOrderByOption(std::string &&); |
---|
106 | |
---|
107 | /// Añade elemento a la sección select. |
---|
108 | StrQuery & addSelect(const std::string &); |
---|
109 | |
---|
110 | /// Añade elemento a la sección from. |
---|
111 | StrQuery & addFrom(const std::string &); |
---|
112 | |
---|
113 | /// Añade elemento a la sección where. |
---|
114 | StrQuery & addWhere(const std::string &, |
---|
115 | const std::string & connector = "AND"); |
---|
116 | |
---|
117 | /// Añade elemento a la sección orderBy. |
---|
118 | StrQuery & addOrderBy(const std::string &); |
---|
119 | |
---|
120 | |
---|
121 | StrQuery & operator = (const StrQuery &); |
---|
122 | |
---|
123 | StrQuery & operator = (StrQuery &&); |
---|
124 | |
---|
125 | /// Casting a std::string |
---|
126 | operator std::string () { |
---|
127 | |
---|
128 | std::string str; |
---|
129 | |
---|
130 | str.append(select); |
---|
131 | str.append(" "); |
---|
132 | str.append(from); |
---|
133 | |
---|
134 | if (not where.empty()) { |
---|
135 | str.append(" "); |
---|
136 | str.append(where); |
---|
137 | } |
---|
138 | |
---|
139 | if (not orderBy.empty()) { |
---|
140 | str.append(" "); |
---|
141 | str.append(orderBy); |
---|
142 | |
---|
143 | if (not orderByOption.empty()) { |
---|
144 | str.append(" "); |
---|
145 | str.append(orderByOption); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | return str; |
---|
150 | } |
---|
151 | |
---|
152 | friend std::ostream & operator << (std::ostream & out, |
---|
153 | const StrQuery & strQuery) { |
---|
154 | out << strQuery.getSelect() |
---|
155 | << " " << strQuery.getFrom(); |
---|
156 | |
---|
157 | if (not strQuery.getWhere().empty()) |
---|
158 | out << " " << strQuery.getWhere(); |
---|
159 | |
---|
160 | if (not strQuery.getOrderBy().empty()) { |
---|
161 | out << " " << strQuery.getOrderBy(); |
---|
162 | |
---|
163 | if (not strQuery.getOrderByOption().empty()) |
---|
164 | out << " " << strQuery.getOrderByOption(); |
---|
165 | } |
---|
166 | |
---|
167 | return out; |
---|
168 | } |
---|
169 | }; |
---|
170 | |
---|
171 | # endif // StrQuery_H |
---|
172 | |
---|