41 #include <nsServiceManagerUtils.h>
42 #include <nsStringGlue.h>
44 #include <nsAutoLock.h>
45 #include <nsNetUtil.h>
47 #include <nsIServiceManager.h>
48 #include <nsThreadUtils.h>
49 #include <nsSupportsArray.h>
50 #include <nsIClassInfoImpl.h>
51 #include <nsIProgrammingLanguage.h>
52 #include <nsIProxyObjectManager.h>
58 #include <nsPrintfCString.h>
61 #define DATABASEQUERY_DEFAULT_CALLBACK_SLOTS (2)
64 static PRLogModuleInfo* sDatabaseQueryLog = nsnull;
65 #define LOG(args) PR_LOG(sDatabaseQueryLog, PR_LOG_ALWAYS, args)
92 CDatabaseQuery::CDatabaseQuery()
93 : m_pLock(PR_NewLock())
94 , m_IsAborting(PR_FALSE)
95 , m_IsExecuting(PR_FALSE)
96 , m_AsyncQuery(PR_FALSE)
97 , m_CurrentQuery((PRUint32)-1)
99 , m_pQueryRunningMonitor(nsAutoMonitor::NewMonitor("CDatabaseQuery.m_pdbQueryRunningMonitor"))
100 , m_QueryHasCompleted(PR_FALSE)
102 , m_RollingLimitColumnIndex(0)
103 , m_RollingLimitResult(0)
106 if (!sDatabaseQueryLog)
107 sDatabaseQueryLog = PR_NewLogModule(
"sbDatabaseQuery");
128 NS_ENSURE_SUCCESS(rv, rv);
137 NS_ENSURE_ARG_POINTER(aDatabaseLocation);
138 if (!NS_IsMainThread()) {
139 NS_WARNING(
"CDatabaseQuery::GetDatabaseLocation is main thread only, "
140 "since it constructs an nsStandardURL object");
141 return NS_ERROR_FAILURE;
145 *aDatabaseLocation = nsnull;
151 NS_ENSURE_SUCCESS(rv, rv);
158 NS_IMETHODIMP CDatabaseQuery::SetDatabaseLocation(nsIURI * aDatabaseLocation)
160 NS_ENSURE_ARG_POINTER(aDatabaseLocation);
161 nsresult rv = NS_ERROR_UNEXPECTED;
163 PRBool isFile = PR_FALSE;
164 if(NS_SUCCEEDED(aDatabaseLocation->SchemeIs(
"file", &isFile)) &&
168 rv = aDatabaseLocation->GetSpec(spec);
169 NS_ENSURE_SUCCESS(rv, rv);
177 rv = NS_ERROR_NOT_IMPLEMENTED;
193 NS_IMETHODIMP CDatabaseQuery::SetAsyncQuery(PRBool bAsyncQuery)
201 NS_IMETHODIMP CDatabaseQuery::IsAyncQuery(PRBool *_retval)
203 NS_ENSURE_ARG_POINTER(_retval);
212 NS_ENSURE_ARG_POINTER(dbPersistCB);
213 nsCOMPtr<sbIDatabaseSimpleQueryCallback> proxiedCallback;
218 NS_PROXY_ASYNC | NS_PROXY_ALWAYS,
219 getter_AddRefs(proxiedCallback));
220 NS_ENSURE_SUCCESS(rv, rv);
231 NS_ENSURE_ARG_POINTER(dbPersistCB);
240 NS_IMETHODIMP CDatabaseQuery::SetDatabaseGUID(
const nsAString &dbGUID)
249 NS_IMETHODIMP CDatabaseQuery::GetDatabaseGUID(nsAString &_retval)
258 NS_IMETHODIMP CDatabaseQuery::AddQuery(
const nsAString &strQuery)
261 nsCOMPtr<sbIDatabasePreparedStatement> preparedStatement;
262 nsresult rv = PrepareQuery(strQuery, getter_AddRefs(preparedStatement));
263 NS_ENSURE_SUCCESS(rv, rv);
265 rv = AddPreparedStatement(preparedStatement);
266 NS_ENSURE_SUCCESS(rv, rv);
276 NS_ENSURE_TRUE(preparedStatement, NS_ERROR_OUT_OF_MEMORY);
277 preparedStatement.forget(_retval);
284 NS_ENSURE_ARG_POINTER(preparedStatement);
297 NS_IMETHODIMP CDatabaseQuery::GetQueryCount(PRUint32 *_retval)
299 NS_ENSURE_ARG_POINTER(_retval);
310 NS_ENSURE_ARG_POINTER(_retval);
318 rv = NS_ERROR_ILLEGAL_VALUE;
330 NS_ENSURE_ARG_POINTER(_retval);
339 rv = NS_ERROR_ILLEGAL_VALUE;
347 NS_IMETHODIMP CDatabaseQuery::ResetQuery()
353 NS_ASSERTION( !
m_IsExecuting,
"Resetting a query that is executing!!!!!");
355 "Resetting a query that is not complete!!!!!");
379 NS_ENSURE_ARG_POINTER(_retval);
389 NS_IMETHODIMP CDatabaseQuery::GetLastError(PRInt32 *_retval)
391 NS_ENSURE_ARG_POINTER(_retval);
398 NS_IMETHODIMP CDatabaseQuery::SetLastError(PRInt32 dbError)
405 NS_IMETHODIMP CDatabaseQuery::Execute(PRInt32 *_retval)
407 LOG((
"DBQ:[0x%x] Execute - ENTRY POINT",
this));
408 NS_ENSURE_ARG_POINTER(_retval);
421 NS_ENSURE_SUCCESS(rv, rv);
423 if(*_retval != SQLITE_OK)
428 return NS_ERROR_FAILURE;
431 LOG((
"DBQ:[0x%x] Execute - EXIT POINT\n\n",
this));
437 NS_IMETHODIMP CDatabaseQuery::WaitForCompletion(PRInt32 *_retval)
439 NS_ENSURE_ARG_POINTER(_retval);
446 NS_ASSERTION( !
m_IsExecuting,
"Query marked completed but still executing.");
454 NS_IMETHODIMP CDatabaseQuery::IsExecuting(PRBool *_retval)
456 NS_ENSURE_ARG_POINTER(_retval);
466 NS_IMETHODIMP CDatabaseQuery::CurrentQuery(PRUint32 *_retval)
468 NS_ENSURE_ARG_POINTER(_retval);
478 NS_IMETHODIMP CDatabaseQuery::Abort(PRBool *_retval)
480 NS_ENSURE_ARG_POINTER(_retval);
485 PRInt32 nWaitResult = 0;
492 WaitForCompletion(&nWaitResult);
500 NS_IMETHODIMP CDatabaseQuery::GetRollingLimit(PRUint64 *aRollingLimit)
502 NS_ENSURE_ARG_POINTER(aRollingLimit);
510 NS_IMETHODIMP CDatabaseQuery::SetRollingLimit(PRUint64 aRollingLimit)
518 NS_IMETHODIMP CDatabaseQuery::GetRollingLimitColumnIndex(PRUint32 *aRollingLimitColumnIndex)
520 NS_ENSURE_ARG_POINTER(aRollingLimitColumnIndex);
528 NS_IMETHODIMP CDatabaseQuery::SetRollingLimitColumnIndex(PRUint32 aRollingLimitColumnIndex)
536 NS_IMETHODIMP CDatabaseQuery::GetRollingLimitResult(PRUint32 *aRollingLimitResult)
538 NS_ENSURE_ARG_POINTER(aRollingLimitResult);
546 NS_IMETHODIMP CDatabaseQuery::SetRollingLimitResult(PRUint32 aRollingLimitResult)
554 NS_IMETHODIMP CDatabaseQuery::BindUTF8StringParameter(PRUint32 aParamIndex,
560 nsresult rv = EnsureLastQueryParameter(aParamIndex);
561 NS_ENSURE_SUCCESS(rv, rv);
569 NS_IMETHODIMP CDatabaseQuery::BindStringParameter(PRUint32 aParamIndex,
570 const nsAString &aValue)
574 nsresult rv = EnsureLastQueryParameter(aParamIndex);
575 NS_ENSURE_SUCCESS(rv, rv);
584 NS_IMETHODIMP CDatabaseQuery::BindDoubleParameter(PRUint32 aParamIndex,
589 nsresult rv = EnsureLastQueryParameter(aParamIndex);
590 NS_ENSURE_SUCCESS(rv, rv);
598 NS_IMETHODIMP CDatabaseQuery::BindInt32Parameter(PRUint32 aParamIndex,
603 nsresult rv = EnsureLastQueryParameter(aParamIndex);
604 NS_ENSURE_SUCCESS(rv, rv);
612 NS_IMETHODIMP CDatabaseQuery::BindInt64Parameter(PRUint32 aParamIndex,
617 nsresult rv = EnsureLastQueryParameter(aParamIndex);
618 NS_ENSURE_SUCCESS(rv, rv);
626 NS_IMETHODIMP CDatabaseQuery::BindNullParameter(PRUint32 aParamIndex)
630 nsresult rv = EnsureLastQueryParameter(aParamIndex);
631 NS_ENSURE_SUCCESS(rv, rv);
638 nsresult CDatabaseQuery::EnsureLastQueryParameter(PRUint32 aParamIndex)
std::vector< CQueryParameter > bindParameterArray_t
#define DATABASEQUERY_DEFAULT_CALLBACK_SLOTS
std::deque< bindParameterArray_t > m_BindParameters
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
#define SONGBIRD_DATABASEENGINE_CONTRACTID
PRBool m_QueryHasCompleted
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
nsCString m_LocationURIString
NS_DECL_ISUPPORTS NS_DECL_SBIDATABASEQUERY NS_DECL_NSICLASSINFO nsresult Init()
nsresult GetDatabaseLocation(nsACString &aURISpec)
virtual ~CDatabaseQuery()
bindParameterArray_t * GetQueryParameters(PRUint32 aQueryIndex)
PRMonitor * m_pQueryRunningMonitor
void SetResultObject(CDatabaseResult *aResultObject)
nsRefPtr< CDatabaseResult > m_QueryResult
nsCString utf8StringValue
nsresult PopQuery(sbIDatabasePreparedStatement **_retval)
nsInterfaceHashtableMT< nsISupportsHashKey, sbIDatabaseSimpleQueryCallback > m_CallbackList
A callback object used to inform client code that a query has completed.
An object containing the results of a database SELECT query.
nsCOMPtr< sbIDatabaseEngine > mDatabaseEngine
PRUint32 m_RollingLimitColumnIndex
An object responsible for executing SQL queries on the database.
Songbird Database Prepared Query.
PRUint32 m_RollingLimitResult
std::deque< nsCOMPtr< sbIDatabasePreparedStatement > > m_DatabaseQueryList
NS_INTERFACE_MAP_END NS_IMPL_CI_INTERFACE_GETTER1(CDatabaseQuery, sbIDatabaseQuery) CDatabaseQuery
CDatabaseResult * GetResultObject()
A prepared database statement.
Songbird Database Object Definition.
bindParameterArray_t * PopQueryParameters()