Fix overwrite method, and bad cleanup of comptrs

This commit is contained in:
Ben Turner 2010-05-07 15:05:42 -07:00
parent 18b42344d0
commit fdf236368d
3 changed files with 21 additions and 49 deletions

View File

@ -71,27 +71,14 @@ isupports_cast(IDBDatabaseRequest* aClassPtr)
static_cast<IDBRequest::Generator*>(aClassPtr));
}
template<class T1, class T2>
template<class T>
inline
void
SwapCOMPtrs(nsCOMPtr<T1>& a1,
nsCOMPtr<T2>& a2)
already_AddRefed<nsISupports>
do_QIAndNull(nsCOMPtr<T>& aCOMPtr)
{
nsCOMPtr<T1> temp1;
temp1.swap(a1);
nsCOMPtr<T2> temp2;
temp2.swap(a2);
a1 = do_QueryInterface(temp2);
if (temp2) {
NS_ASSERTION(a1, "QI failed!");
}
a2 = do_QueryInterface(temp1);
if (temp1) {
NS_ASSERTION(a2, "QI failed!");
}
nsCOMPtr<nsISupports> temp(do_QueryInterface(aCOMPtr));
aCOMPtr = nsnull;
return temp.forget();
}
class CloseConnectionRunnable : public nsRunnable
@ -610,24 +597,15 @@ IDBDatabaseRequest::RemoveStatement(bool aAutoIncrement)
void
IDBDatabaseRequest::FireCloseConnectionRunnable()
{
// Keep this in sync with the number of nsCOMPtrs we destroy!
static const PRUint32 kDoomedObjectCount = 5;
PRUint32 index = 0;
nsTArray<nsCOMPtr<nsISupports> > doomedObjects;
if (!doomedObjects.SetLength(kDoomedObjectCount)) {
NS_ERROR("OOM!");
}
SwapCOMPtrs(doomedObjects[index++], mConnection);
SwapCOMPtrs(doomedObjects[index++], mPutStmt);
SwapCOMPtrs(doomedObjects[index++], mPutAutoIncrementStmt);
SwapCOMPtrs(doomedObjects[index++], mPutOverwriteStmt);
SwapCOMPtrs(doomedObjects[index++], mPutOverwriteAutoIncrementStmt);
SwapCOMPtrs(doomedObjects[index++], mRemoveStmt);
SwapCOMPtrs(doomedObjects[index++], mRemoveAutoIncrementStmt);
NS_ASSERTION(index == kDoomedObjectCount, "Fix this!");
doomedObjects.AppendElement(do_QIAndNull(mConnection));
doomedObjects.AppendElement(do_QIAndNull(mPutStmt));
doomedObjects.AppendElement(do_QIAndNull(mPutAutoIncrementStmt));
doomedObjects.AppendElement(do_QIAndNull(mPutOverwriteStmt));
doomedObjects.AppendElement(do_QIAndNull(mPutOverwriteAutoIncrementStmt));
doomedObjects.AppendElement(do_QIAndNull(mRemoveStmt));
doomedObjects.AppendElement(do_QIAndNull(mRemoveAutoIncrementStmt));
mConnectionThread->Dispatch(new CloseConnectionRunnable(doomedObjects),
NS_DISPATCH_NORMAL);

View File

@ -68,10 +68,10 @@ public:
const nsAString& aKeyString,
PRInt64 aKeyInt,
bool aAutoIncrement,
bool aNoOverwrite)
bool aOverwrite)
: AsyncConnectionHelper(aDatabase, aTarget), mOSID(aObjectStoreID),
mValue(aValue), mKeyInt(aKeyInt), mAutoIncrement(aAutoIncrement),
mNoOverwrite(aNoOverwrite)
mOverwrite(aOverwrite)
{
if (!mAutoIncrement) {
if (aKeyString.IsVoid()) {
@ -93,7 +93,7 @@ private:
nsString mKeyString;
PRInt64 mKeyInt;
const bool mAutoIncrement;
const bool mNoOverwrite;
const bool mOverwrite;
};
class GetHelper : public AsyncConnectionHelper
@ -324,16 +324,11 @@ IDBObjectStoreRequest::GetIndexNames(nsIDOMDOMStringList** aIndexNames)
NS_IMETHODIMP
IDBObjectStoreRequest::Put(nsIVariant* /* aValue */,
nsIVariant* aKey,
PRBool aNoOverwrite,
PRUint8 aOptionalArgCount,
PRBool aOverwrite,
nsIIDBRequest** _retval)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
if (aOptionalArgCount < 2) {
aNoOverwrite = PR_TRUE;
}
nsString keyString;
PRInt64 keyInt;
@ -388,7 +383,7 @@ IDBObjectStoreRequest::Put(nsIVariant* /* aValue */,
nsRefPtr<PutHelper> helper =
new PutHelper(mDatabase, request, mId, jsonString, keyString, keyInt,
!!mAutoIncrement, !!aNoOverwrite);
!!mAutoIncrement, !!aOverwrite);
rv = helper->Dispatch(mDatabase->ConnectionThread());
NS_ENSURE_SUCCESS(rv, rv);
@ -468,9 +463,9 @@ PutHelper::DoDatabaseWork()
mozStorageTransaction transaction(connection, PR_FALSE);
nsCOMPtr<mozIStorageStatement> stmt =
mDatabase->PutStatement(!mNoOverwrite, mAutoIncrement);
mDatabase->PutStatement(!mOverwrite, mAutoIncrement);
NS_ENSURE_TRUE(stmt, nsIIDBDatabaseError::UNKNOWN_ERR);
if (!mAutoIncrement || mAutoIncrement && !mNoOverwrite) {
if (!mAutoIncrement || mAutoIncrement && mOverwrite) {
NS_NAMED_LITERAL_CSTRING(keyValue, "key_value");
if (mKeyString.IsVoid()) {

View File

@ -51,11 +51,10 @@ interface nsIVariant;
[scriptable, uuid(ce65fbc2-06a0-4f1c-89e4-0d9a636179c7)]
interface nsIIDBObjectStoreRequest : nsIIDBObjectStore
{
[optional_argc]
nsIIDBRequest
put (in nsIVariant value,
[optional /* null */] in nsIVariant key,
[optional /* true */] in boolean noOverwrite);
[optional /* false */] in boolean overwrite);
nsIIDBRequest
remove(in nsIVariant key);