sbSQLBuilderCriterion.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #include "sbSQLBuilderCriterion.h"
28 #include "sbSQLBuilderBase.h"
29 
30 #include "prprf.h"
31 
34 
35 sbSQLBuilderCriterionBase::sbSQLBuilderCriterionBase(const nsAString& aTableName,
36  const nsAString& aColumnName,
37  PRUint32 aMatchType,
39  sbISQLBuilderCriterion* aRight) :
40  mTableName(aTableName),
41  mColumnName(aColumnName),
42  mMatchType(aMatchType),
43  mLeft(aLeft),
44  mRight(aRight)
45 {
46 }
47 
48 void
50 {
51  switch(mMatchType) {
53  aStr.AppendLiteral(" = ");
54  break;
56  aStr.AppendLiteral(" != ");
57  break;
59  aStr.AppendLiteral(" > ");
60  break;
62  aStr.AppendLiteral(" >= ");
63  break;
65  aStr.AppendLiteral(" < ");
66  break;
68  aStr.AppendLiteral(" <= ");
69  break;
71  aStr.AppendLiteral(" like ");
72  break;
74  aStr.AppendLiteral(" not like ");
75  break;
77  aStr.AppendLiteral(" match ");
78  break;
79  default:
80  NS_NOTREACHED("Unknown Match Type");
81  }
82 }
83 
84 void
86 {
87  if (!mTableName.IsEmpty()) {
88  aStr.Append(mTableName);
89  aStr.AppendLiteral(".");
90  }
91  aStr.Append(mColumnName);
92 }
93 
94 void
95 sbSQLBuilderCriterionBase::AppendLogicalTo(const nsAString& aOperator,
96  nsAString& aStr)
97 {
98  aStr.AppendLiteral("(");
99 
100  sbISQLBuilderCriterion* criterion = mLeft;
101  nsAutoString str;
102  static_cast<sbSQLBuilderCriterionBase*>(criterion)->ToString(str);
103  aStr.Append(str);
104 
105  aStr.AppendLiteral(" ");
106  aStr.Append(aOperator);
107  aStr.AppendLiteral(" ");
108 
109  criterion = mRight;
110  str.Truncate();
111  static_cast<sbSQLBuilderCriterionBase*>(criterion)->ToString(str);
112  aStr.Append(str);
113 
114  aStr.AppendLiteral(")");
115 }
116 
117 // sbSQLBuilderCriterionString
120 
121 sbSQLBuilderCriterionString::sbSQLBuilderCriterionString(const nsAString& aTableName,
122  const nsAString& aColumnName,
123  PRUint32 aMatchType,
124  const nsAString& aValue) :
125  sbSQLBuilderCriterionBase(aTableName, aColumnName, aMatchType, nsnull, nsnull),
126  mValue(aValue)
127 {
128 }
129 
130 NS_IMETHODIMP
131 sbSQLBuilderCriterionString::ToString(nsAString& _retval)
132 {
133  AppendTableColumnTo(_retval);
134 
135  AppendMatchTo(_retval);
136 
137  nsAutoString escapedValue(mValue);
138  SB_EscapeSQL(escapedValue);
139 
140  _retval.AppendLiteral("'");
141  _retval.Append(escapedValue);
142  _retval.AppendLiteral("'");
145  _retval.AppendLiteral(" ESCAPE '\\'");
146  }
147  return NS_OK;
148 }
149 
150 // sbSQLBuilderCriterionBetweenString
153 
155  const nsAString& aColumnName,
156  const nsAString& aLeftValue,
157  const nsAString& aRightValue,
158  PRBool aNegate) :
159  sbSQLBuilderCriterionBase(aTableName, aColumnName, 0, nsnull, nsnull),
160  mLeftValue(aLeftValue),
161  mRightValue(aRightValue),
162  mNegate(aNegate)
163 {
164 }
165 
166 NS_IMETHODIMP
168 {
169  AppendTableColumnTo(_retval);
170 
171  if (mNegate) {
172  _retval.AppendLiteral(" not ");
173  }
174 
175  _retval.AppendLiteral(" between ");
176 
177  nsAutoString escapedLeftValue(mLeftValue);
178  SB_EscapeSQL(escapedLeftValue);
179 
180  nsAutoString escapedRightValue(mRightValue);
181  SB_EscapeSQL(escapedRightValue);
182 
183  _retval.AppendLiteral("'");
184  _retval.Append(mLeftValue);
185  _retval.AppendLiteral("' and '");
186  _retval.Append(mRightValue);
187  _retval.AppendLiteral("'");
188 
189  return NS_OK;
190 }
191 
192 // sbSQLBuilderCriterionLong
195 
196 sbSQLBuilderCriterionLong::sbSQLBuilderCriterionLong(const nsAString& aTableName,
197  const nsAString& aColumnName,
198  PRUint32 aMatchType,
199  PRInt32 aValue) :
200  sbSQLBuilderCriterionBase(aTableName, aColumnName, aMatchType, nsnull, nsnull),
201  mValue(aValue)
202 {
203 }
204 
205 NS_IMETHODIMP
206 sbSQLBuilderCriterionLong::ToString(nsAString& _retval)
207 {
208  AppendTableColumnTo(_retval);
209 
210  AppendMatchTo(_retval);
211 
212  nsAutoString stringValue;
213  stringValue.AppendInt(mValue);
214  _retval.Append(stringValue);
215  return NS_OK;
216 }
217 
218 // sbSQLBuilderCriterionLongLong
221 
223  const nsAString& aColumnName,
224  PRUint32 aMatchType,
225  PRInt64 aValue) :
226  sbSQLBuilderCriterionBase(aTableName, aColumnName, aMatchType, nsnull, nsnull),
227  mValue(aValue)
228 {
229 }
230 
231 NS_IMETHODIMP
232 sbSQLBuilderCriterionLongLong::ToString(nsAString& _retval)
233 {
234  AppendTableColumnTo(_retval);
235 
236  AppendMatchTo(_retval);
237 
238  // Unfortunately nsAString has no AppendInt64...
239  char out[32] = {0};
240  PR_snprintf(out, 32, "%lld", mValue);
241  _retval.Append(NS_ConvertUTF8toUTF16(out));
242  return NS_OK;
243 }
244 
245 // sbSQLBuilderCriterionNull
248 
249 sbSQLBuilderCriterionNull::sbSQLBuilderCriterionNull(const nsAString& aTableName,
250  const nsAString& aColumnName,
251  PRUint32 aMatchType) :
252  sbSQLBuilderCriterionBase(aTableName, aColumnName, aMatchType, nsnull, nsnull)
253 {
254 }
255 
256 NS_IMETHODIMP
257 sbSQLBuilderCriterionNull::ToString(nsAString& _retval)
258 {
259  AppendTableColumnTo(_retval);
260 
262  _retval.AppendLiteral(" is null");
263  }
264  else {
265  _retval.AppendLiteral(" is not null");
266  }
267  return NS_OK;
268 }
269 
270 // sbSQLBuilderCriterionParameter
273 
275  const nsAString& aColumnName,
276  PRUint32 aMatchType) :
277  sbSQLBuilderCriterionBase(aTableName, aColumnName, aMatchType, nsnull, nsnull)
278 {
279 }
280 
281 NS_IMETHODIMP
283 {
284  AppendTableColumnTo(_retval);
285 
286  AppendMatchTo(_retval);
287 
288  _retval.AppendLiteral("?");
289  return NS_OK;
290 }
291 
292 // sbSQLBuilderCriterionTable
295 
296 sbSQLBuilderCriterionTable::sbSQLBuilderCriterionTable(const nsAString& aLeftTableName,
297  const nsAString& aLeftColumnName,
298  PRUint32 aMatchType,
299  const nsAString& aRightTableName,
300  const nsAString& aRightColumnName) :
301  sbSQLBuilderCriterionBase(aLeftTableName, aLeftColumnName, aMatchType, nsnull, nsnull),
302  mRightTableName(aRightTableName),
303  mRightColumnName(aRightColumnName)
304 {
305 }
306 
307 NS_IMETHODIMP
308 sbSQLBuilderCriterionTable::ToString(nsAString& _retval)
309 {
310  AppendTableColumnTo(_retval);
311 
312  AppendMatchTo(_retval);
313 
314  if (!mRightTableName.IsEmpty()) {
315  _retval.Append(mRightTableName);
316  _retval.AppendLiteral(".");
317  }
318  _retval.Append(mRightColumnName);
319 
320  return NS_OK;
321 }
322 
323 // sbSQLBuilderCriterionAnd
326 
328  sbISQLBuilderCriterion* aRight) :
329  sbSQLBuilderCriterionBase(EmptyString(), EmptyString(), 0, aLeft, aRight)
330 {
331 }
332 
333 NS_IMETHODIMP
334 sbSQLBuilderCriterionAnd::ToString(nsAString& _retval)
335 {
336  AppendLogicalTo(NS_LITERAL_STRING("and"), _retval);
337  return NS_OK;
338 }
339 
340 // sbSQLBuilderCriterionOr
343 
345  sbISQLBuilderCriterion* aRight) :
346  sbSQLBuilderCriterionBase(EmptyString(), EmptyString(), 0, aLeft, aRight)
347 {
348  mLeft = aLeft;
349  mRight = aRight;
350 }
351 
352 NS_IMETHODIMP
353 sbSQLBuilderCriterionOr::ToString(nsAString& _retval)
354 {
355  AppendLogicalTo(NS_LITERAL_STRING("or"), _retval);
356  return NS_OK;
357 }
358 
359 // sbSQLBuilderCriterionIn
363 
364 sbSQLBuilderCriterionIn::sbSQLBuilderCriterionIn(const nsAString& aTableName,
365  const nsAString& aColumnName) :
366  sbSQLBuilderCriterionBase(aTableName, aColumnName, 0, nsnull, nsnull)
367 {
368 }
369 
370 NS_IMETHODIMP
371 sbSQLBuilderCriterionIn::AddString(const nsAString& aValue)
372 {
373  sbInItem* ii = mInItems.AppendElement();
374  NS_ENSURE_TRUE(ii, NS_ERROR_OUT_OF_MEMORY);
375 
376  ii->type = eString;
377  ii->stringValue = aValue;
378 
379  return NS_OK;
380 }
381 
382 NS_IMETHODIMP
383 sbSQLBuilderCriterionIn::AddLong(PRInt32 aValue)
384 {
385  sbInItem* ii = mInItems.AppendElement();
386  NS_ENSURE_TRUE(ii, NS_ERROR_OUT_OF_MEMORY);
387 
388  ii->type = eInteger32;
389  ii->int32Value = aValue;
390 
391  return NS_OK;
392 }
393 
394 NS_IMETHODIMP
395 sbSQLBuilderCriterionIn::AddSubquery(sbISQLSelectBuilder* aSubquery)
396 {
397  sbInItem* ii = mInItems.AppendElement();
398  NS_ENSURE_TRUE(ii, NS_ERROR_OUT_OF_MEMORY);
399 
400  ii->type = eSubquery;
401  ii->subquery = aSubquery;
402 
403  return NS_OK;
404 }
405 
406 NS_IMETHODIMP
407 sbSQLBuilderCriterionIn::Clear()
408 {
409  mInItems.Clear();
410 
411  return NS_OK;
412 }
413 
414 NS_IMETHODIMP
415 sbSQLBuilderCriterionIn::ToString(nsAString& _retval)
416 {
417  AppendTableColumnTo(_retval);
418 
419  _retval.AppendLiteral(" in (");
420 
421  PRUint32 len = mInItems.Length();
422  for (PRUint32 i = 0; i < len; i++) {
423  const sbInItem& ii = mInItems[i];
424 
425  switch(ii.type) {
426  case eIsNull:
427  /* not implemented */
428  break;
429  case eString:
430  {
431  nsAutoString escapedValue(ii.stringValue);
432  SB_EscapeSQL(escapedValue);
433 
434  _retval.AppendLiteral("'");
435  _retval.Append(escapedValue);
436  _retval.AppendLiteral("'");
437  break;
438  }
439  case eInteger32:
440  _retval.AppendInt(ii.int32Value);
441  break;
442  case eSubquery:
443  {
444  nsresult rv;
445  nsAutoString sql;
446  rv = ii.subquery->ToString(sql);
447  NS_ENSURE_SUCCESS(rv, rv);
448  _retval.Append(sql);
449  break;
450  }
451  }
452 
453  if (i + 1 < len) {
454  _retval.AppendLiteral(", ");
455  }
456  }
457 
458  _retval.AppendLiteral(")");
459 
460  return NS_OK;
461 }
462 
return NS_OK
const unsigned long MATCH_NOTLIKE
onPageChanged aValue
Definition: FeedWriter.js:1395
NS_IMPL_ISUPPORTS_INHERITED1(sbSQLBuilderCriterionIn, sbSQLBuilderCriterionBase, sbISQLBuilderCriterionIn) sbSQLBuilderCriterionIn
nsCOMPtr< sbISQLBuilderCriterion > mLeft
void AppendMatchTo(nsAString &aStr)
NS_IMPL_ISUPPORTS_INHERITED0(sbSQLBuilderCriterionString, sbSQLBuilderCriterionBase) sbSQLBuilderCriterionString
void AppendLogicalTo(const nsAString &aOperator, nsAString &aStr)
const unsigned long MATCH_LESSEQUAL
static nsresult ToString(const nsDiscriminatedUnion &data, nsACString &outString)
Definition: sbVariant.cpp:861
nsresult SB_EscapeSQL(nsAString &str)
restoreDimensions aLeft
Interface for "in" SQL critera.
Interface for building SELECT statements.
nsCOMPtr< sbISQLBuilderCriterion > mRight
const unsigned long MATCH_LESS
const unsigned long MATCH_MATCH
const unsigned long MATCH_LIKE
const unsigned long MATCH_GREATEREQUAL
NS_IMPL_THREADSAFE_ISUPPORTS1(sbSQLBuilderCriterionBase, sbISQLBuilderCriterion) sbSQLBuilderCriterionBase
Interface for SQL critera. This interface has no public methods as it is used only as a return type f...
const unsigned long MATCH_NOTEQUALS
const unsigned long MATCH_GREATER
_getSelectedPageStyle s i
void AppendTableColumnTo(nsAString &aStr)
const unsigned long MATCH_EQUALS