DatabasePreparedStatement.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 
28 
29 // INCLUDES ===================================================================
30 #include "DatabaseQuery.h"
31 #include "DatabaseEngine.h"
32 
33 #include <nsCOMPtr.h>
34 #include <nsServiceManagerUtils.h>
35 #include <nsComponentManagerUtils.h>
36 #include <nsStringGlue.h>
37 #include <nsIConsoleService.h>
38 #include <nsIScriptError.h>
39 
40 #include <prmem.h>
41 #include <prtypes.h>
42 
43 // The maximum characters to output in a single PR_LOG call
44 #define MAX_PRLOG 400
45 
47 
49  : mStatement(nsnull), mSql(sql)
50 {
51 }
52 
54 {
55  if (mStatement) {
56  // this should always be safe if mStatement is defined.
57  // if it does, it means we have a bad mStatement pointer.
58  // error codes returned here are okay, since they either reiterate
59  // errors caused by bad statements being compiled, or indicate
60  // that the statement was aborted during execution.
61  // see: http://sqlite.org/c3ref/finalize.html
62  sqlite3_finalize(mStatement);
63  mStatement = nsnull;
64  }
65 }
66 
67 NS_IMETHODIMP CDatabasePreparedStatement::GetQueryString(nsAString &_retval)
68 {
69  if (mSql.Length() > 0) {
70  _retval = mSql;
71  }
72  else if (mStatement) {
73  const char* sql = sqlite3_sql(mStatement);
74  _retval = NS_ConvertUTF8toUTF16(sql);
75  }
76  else {
77  _retval = EmptyString();
78  }
79  return NS_OK;
80 }
81 
82 sqlite3_stmt* CDatabasePreparedStatement::GetStatement(sqlite3 *db)
83 {
84  if (!db) {
85  NS_WARNING("GetStatement called without a database pointer.");
86  return nsnull;
87  }
88 
89  // either reset and return the existing statement, or compile it first and return that.
90  if (mStatement) {
91  if (db != sqlite3_db_handle(mStatement)) {
92  NS_WARNING("GetStatement() called with a different DB than the one originally used compile it!.");
93  return nsnull;
94  }
95  //Always reset the statement before sending it out for reuse.
96  int retDB = 0;
97  retDB = sqlite3_reset(mStatement);
98  retDB = sqlite3_clear_bindings(mStatement);
99  }
100  else {
101  if (mSql.Length() == 0) {
102  NS_WARNING("GetStatement() called on a PreparedStatement with no SQL.");
103  return nsnull;
104  }
105 
106  const char *pzTail = nsnull;
107  nsCString sqlStr = NS_ConvertUTF16toUTF8(mSql);
108  int retDB = sqlite3_prepare_v2(db, sqlStr.get(), sqlStr.Length(),
109  &mStatement, &pzTail);
110  if (retDB != SQLITE_OK) {
111  const char *szErr = sqlite3_errmsg(db);
112 
113  nsString log;
114  log.AppendLiteral("SQLite compile step: \n");
115  log.Append(mSql);
116  log.AppendLiteral("\ncaused the error\n");
117  log.Append(NS_ConvertUTF8toUTF16(szErr));
118  log.AppendLiteral("\n");
119 
120  nsresult rv;
121  nsCOMPtr<nsIConsoleService> consoleService = do_GetService("@mozilla.org/consoleservice;1", &rv);
122 
123  nsCOMPtr<nsIScriptError> scriptError = do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
124  if (scriptError) {
125  nsresult rv = scriptError->Init(log.get(),
126  EmptyString().get(),
127  EmptyString().get(),
128  0, // No line number
129  0, // No column number
130  0, // An error message.
131  "DBEngine:StatementCompilation");
132  if (NS_SUCCEEDED(rv)) {
133  rv = consoleService->LogMessage(scriptError);
134  }
135  }
136 
137  return nsnull;
138  }
139  // Henceforth, the sqlite_stmt will be responsible for holding onto the sql, not this object.
140  mSql = EmptyString();
141  }
142  return mStatement;
143 }
return NS_OK
function log(s)
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
Songbird Database Prepared Query.
sqlite3_stmt * GetStatement(sqlite3 *db)
A prepared database statement.
Songbird Database Object Definition.