sbSQLInsertBuilder.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 "sbSQLInsertBuilder.h"
28 
32 
33 NS_IMETHODIMP
34 sbSQLInsertBuilder::AddColumn(const nsAString& aColumnName)
35 {
36  sbColumnInfo* ci = mOutputColumns.AppendElement();
37  NS_ENSURE_TRUE(ci, NS_ERROR_OUT_OF_MEMORY);
38 
39  ci->tableName = EmptyString();
40  ci->columnName = aColumnName;
41 
42  return NS_OK;
43 }
44 
45 NS_IMETHODIMP
46 sbSQLInsertBuilder::AddValueString(const nsAString& aValue)
47 {
48  sbValueItem* vi = mValueList.AppendElement();
49  NS_ENSURE_TRUE(vi, NS_ERROR_OUT_OF_MEMORY);
50 
51  vi->type = eString;
52  vi->stringValue = aValue;
53 
54  return NS_OK;
55 }
56 
57 NS_IMETHODIMP
58 sbSQLInsertBuilder::AddValueLong(PRInt32 aValue)
59 {
60  sbValueItem* vi = mValueList.AppendElement();
61  NS_ENSURE_TRUE(vi, NS_ERROR_OUT_OF_MEMORY);
62 
63  vi->type = eInteger32;
64  vi->int32Value = aValue;
65 
66  return NS_OK;
67 }
68 
69 NS_IMETHODIMP
70 sbSQLInsertBuilder::AddValueNull()
71 {
72  sbValueItem* vi = mValueList.AppendElement();
73  NS_ENSURE_TRUE(vi, NS_ERROR_OUT_OF_MEMORY);
74 
75  vi->type = eIsNull;
76 
77  return NS_OK;
78 }
79 
80 NS_IMETHODIMP
81 sbSQLInsertBuilder::AddValueParameter()
82 {
83  sbValueItem* vi = mValueList.AppendElement();
84  NS_ENSURE_TRUE(vi, NS_ERROR_OUT_OF_MEMORY);
85 
86  vi->type = eIsParameter;
87 
88  return NS_OK;
89 }
90 
91 NS_IMETHODIMP
92 sbSQLInsertBuilder::AddValueRaw(const nsAString& aValue)
93 {
94  sbValueItem* vi = mValueList.AppendElement();
95  NS_ENSURE_TRUE(vi, NS_ERROR_OUT_OF_MEMORY);
96 
97  vi->type = eIsRaw;
98  vi->stringValue = aValue;
99 
100  return NS_OK;
101 }
102 
103 NS_IMETHODIMP
104 sbSQLInsertBuilder::GetIntoTableName(nsAString& aIntoTableName)
105 {
106  aIntoTableName = mIntoTableName;
107  return NS_OK;
108 }
109 NS_IMETHODIMP
110 sbSQLInsertBuilder::SetIntoTableName(const nsAString& aIntoTableName)
111 {
112  mIntoTableName = aIntoTableName;
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP
117 sbSQLInsertBuilder::GetSelect(sbISQLSelectBuilder** aSelect)
118 {
119  NS_ADDREF(*aSelect = mSelect);
120  return NS_OK;
121 }
122 NS_IMETHODIMP
123 sbSQLInsertBuilder::SetSelect(sbISQLSelectBuilder* aSelect)
124 {
125  mSelect = aSelect;
126  return NS_OK;
127 }
128 
129 NS_IMETHODIMP
131 {
132  mSelect = nsnull;
133  mValueList.Clear();
134  mOutputColumns.Clear();
135  return NS_OK;
136 }
137 
138 NS_IMETHODIMP
139 sbSQLInsertBuilder::ToString(nsAString& _retval)
140 {
141  nsresult rv;
142  nsAutoString buff;
143 
144  buff.AssignLiteral("insert into ");
145 
146  buff.Append(mIntoTableName);
147 
148  PRUint32 len = mOutputColumns.Length();
149  if(len > 0) {
150  buff.AppendLiteral(" (");
151  for (PRUint32 i = 0; i < len; i++) {
152  const sbColumnInfo& ci = mOutputColumns[i];
153  buff.Append(ci.columnName);
154  if (i + 1 < len) {
155  buff.AppendLiteral(", ");
156  }
157  }
158  buff.AppendLiteral(")");
159  }
160 
161  if (mSelect) {
162  nsAutoString sql;
163  rv = mSelect->ToString(sql);
164  NS_ENSURE_SUCCESS(rv, rv);
165  buff.AppendLiteral(" ");
166  buff.Append(sql);
167  }
168  else {
169  buff.AppendLiteral(" values (");
170  len = mValueList.Length();
171  for (PRUint32 i = 0; i < len; i++) {
172  const sbValueItem& vi = mValueList[i];
173 
174  switch(vi.type) {
175  case eIsNull:
176  buff.AppendLiteral("null");
177  break;
178  case eIsParameter:
179  buff.AppendLiteral("?");
180  break;
181  case eString:
182  {
183  nsAutoString escapedValue(vi.stringValue);
184  SB_EscapeSQL(escapedValue);
185 
186  buff.AppendLiteral("'");
187  buff.Append(escapedValue);
188  buff.AppendLiteral("'");
189  break;
190  }
191  case eInteger32:
192  buff.AppendInt(vi.int32Value);
193  break;
194  case eIsRaw:
195  buff.Append(vi.stringValue);
196  break;
197  }
198 
199  if (i + 1 < len) {
200  buff.AppendLiteral(", ");
201  }
202  }
203 
204  buff.AppendLiteral(")");
205  }
206 
207  _retval.Assign(buff);
208  return NS_OK;
209 }
210 
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
nsresult SB_EscapeSQL(nsAString &str)
NS_DECL_SBISQLINSERTBUILDER NS_IMETHOD ToString(nsAString &_retval)
Interface for building SELECT statements.
NS_IMPL_ISUPPORTS_INHERITED1(sbSQLInsertBuilder, sbSQLBuilderBase, sbISQLInsertBuilder) NS_IMETHODIMP sbSQLInsertBuilder
_getSelectedPageStyle s i
Interface for building INSERT statements.