sbSQLBuilderBase.h
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 #ifndef __SBSQLBUILDER_H__
28 #define __SBSQLBUILDER_H__
29 
30 #include <sbISQLBuilder.h>
31 
32 #include <nsStringGlue.h>
33 #include <nsTArray.h>
34 #include <nsCOMArray.h>
35 #include <nsCOMPtr.h>
36 
37 #define QUOTE_CHAR '\''
38 
39 #define NS_FORWARD_SBISQLBUILDER_WITHOUT_TOSTRING_RESET(_to) \
40  NS_IMETHOD GetLimit(PRInt32 *aLimit) { return _to GetLimit(aLimit); } \
41  NS_IMETHOD SetLimit(PRInt32 aLimit) { return _to SetLimit(aLimit); } \
42  NS_IMETHOD GetLimitIsParameter(PRBool *aLimitIsParameter) { return _to GetLimitIsParameter(aLimitIsParameter); } \
43  NS_IMETHOD SetLimitIsParameter(PRBool aLimitIsParameter) { return _to SetLimitIsParameter(aLimitIsParameter); } \
44  NS_IMETHOD GetOffset(PRInt32 *aOffset) { return _to GetOffset(aOffset); } \
45  NS_IMETHOD SetOffset(PRInt32 aOffset) { return _to SetOffset(aOffset); } \
46  NS_IMETHOD GetOffsetIsParameter(PRBool *aOffsetIsParameter) { return _to GetOffsetIsParameter(aOffsetIsParameter); } \
47  NS_IMETHOD SetOffsetIsParameter(PRBool aOffsetIsParameter) { return _to SetOffsetIsParameter(aOffsetIsParameter); } \
48  NS_IMETHOD AddJoin(PRUint32 aJoinType, const nsAString & aJoinedTableName, const nsAString & aJoinedTableAlias, const nsAString & aJoinedColumnName, const nsAString & aJoinToTableName, const nsAString & aJoinToColumnName) { return _to AddJoin(aJoinType, aJoinedTableName, aJoinedTableAlias, aJoinedColumnName, aJoinToTableName, aJoinToColumnName); } \
49  NS_IMETHOD AddJoinWithIndexHint(PRUint32 aJoinType, const nsAString & aJoinedTableName, const nsAString & aJoinedTableAlias, const nsAString & aJoinedColumnName, const nsAString & aJoinToTableName, const nsAString & aJoinToColumnName, PRBool aRequiresFromTableIndexHint, PRBool aRequiresToTableIndexHint) { return _to AddJoinWithIndexHint(aJoinType, aJoinedTableName, aJoinedTableAlias, aJoinedColumnName, aJoinToTableName, aJoinToColumnName, aRequiresFromTableIndexHint, aRequiresToTableIndexHint); } \
50  NS_IMETHOD AddSubqueryJoin(PRUint32 aJoinType, sbISQLSelectBuilder *aJoinedSubquery, const nsAString & aJoinedTableAlias, const nsAString & aJoinedColumnName, const nsAString & aJoinToTableName, const nsAString & aJoinToColumnName) { return _to AddSubqueryJoin(aJoinType, aJoinedSubquery, aJoinedTableAlias, aJoinedColumnName, aJoinToTableName, aJoinToColumnName); } \
51  NS_IMETHOD AddJoinWithCriterion(PRUint32 aJoinType, const nsAString & aJoinedTableName, const nsAString & aJoinedTableAlias, sbISQLBuilderCriterion *aCriterion) { return _to AddJoinWithCriterion(aJoinType, aJoinedTableName, aJoinedTableAlias, aCriterion); } \
52  NS_IMETHOD AddSubquery(sbISQLSelectBuilder *aSubquery, const nsAString & aAlias) { return _to AddSubquery(aSubquery, aAlias); } \
53 
55 {
56 public:
58  NS_DECL_SBISQLBUILDER
59 
61  virtual ~sbSQLBuilderBase();
62 protected:
63 
64  struct sbColumnInfo
65  {
66  nsString tableName;
67  nsString columnName;
68  };
69 
70  struct sbJoinInfo
71  {
72  PRUint32 type;
73  nsString joinedTableName;
74  nsString joinedTableAlias;
75  nsString joinedColumnName;
76  nsString joinToTableName;
77  nsString joinToColumnName;
78  nsCOMPtr<sbISQLBuilderCriterion> criterion;
79  nsCOMPtr<sbISQLSelectBuilder> subquery;
82  };
83 
85  {
86  nsCOMPtr<sbISQLSelectBuilder> subquery;
87  nsString alias;
88  };
89 
90  PRInt32 mLimit;
92  PRInt32 mOffset;
94  nsTArray<sbJoinInfo> mJoins;
95  nsTArray<sbSubqueryInfo> mSubqueries;
96 
97 };
98 
99 inline nsresult
100 SB_EscapeSQL(nsAString& str)
101 {
102  nsAutoString dest;
103 
104  PRInt32 pos = str.FindChar(QUOTE_CHAR, 0);
105  PRInt32 lastPos = 0;
106  PRBool hasQuote = PR_FALSE;
107  while(pos >= 0) {
108  dest.Append(Substring(str, lastPos, pos - lastPos + 1));
109  dest.Append(QUOTE_CHAR);
110  lastPos = pos + 1;
111  pos = str.FindChar(QUOTE_CHAR, lastPos);
112  hasQuote = PR_TRUE;
113  }
114 
115  if (hasQuote) {
116  dest.Append(Substring(str, lastPos, str.Length() - lastPos));
117  str.Assign(dest);
118  }
119 
120  return NS_OK;
121 }
122 
123 #endif /* __SBSQLBUILDER_H__ */
124 
nsTArray< sbSubqueryInfo > mSubqueries
NS_DECL_ISUPPORTS NS_DECL_SBISQLBUILDER sbSQLBuilderBase()
PRPackedBool requiresJoinFromIndexUsageFix
virtual ~sbSQLBuilderBase()
return NS_OK
_dialogDatepicker pos
nsresult SB_EscapeSQL(nsAString &str)
nsCOMPtr< sbISQLSelectBuilder > subquery
#define QUOTE_CHAR
Base interface for SQL building components.
nsCOMPtr< sbISQLSelectBuilder > subquery
nsTArray< sbJoinInfo > mJoins
PRPackedBool requiresJoinToIndexUsageFix
nsCOMPtr< sbISQLBuilderCriterion > criterion