Bug 327317 r=vladimir Create IndexExists function

This commit is contained in:
brettw%gmail.com 2006-02-28 20:57:39 +00:00
parent f0f0a5c7ce
commit 160ca2a46c
2 changed files with 46 additions and 1 deletions

View File

@ -49,7 +49,7 @@ interface nsIFile;
* creating prepared statements, executing SQL, and examining database
* errors.
*/
[scriptable, uuid(e045e635-94af-48af-9a8d-80f1a88a35f3)]
[scriptable, uuid(4c459c8a-f548-42b7-8d7e-42ea1aed3876)]
interface mozIStorageConnection : nsISupports {
/*
* Initialization and status
@ -113,6 +113,15 @@ interface mozIStorageConnection : nsISupports {
*/
boolean tableExists(in AUTF8String aTableName);
/**
* Check if the given index exists.
*
* @param aIndexName The index to check
* @returns TRUE if the index exists, FALSE otherwise.
*/
boolean indexExists(in AUTF8String aIndexName);
/*
* Transactions
*/

View File

@ -255,6 +255,42 @@ mozStorageConnection::TableExists(const nsACString& aSQLStatement, PRBool *_retv
return NS_OK;
}
NS_IMETHODIMP
mozStorageConnection::IndexExists(const nsACString& aIndexName, PRBool* _retval)
{
NS_ENSURE_ARG_POINTER(mDBConn);
nsCString query("SELECT name FROM sqlite_master WHERE type = 'index' AND name ='");
query.Append(aIndexName);
query.AppendLiteral("'");
sqlite3_stmt *stmt = nsnull;
int srv = sqlite3_prepare(mDBConn, query.get(), query.Length(), &stmt, nsnull);
if (srv != SQLITE_OK) {
HandleSqliteError(query.get());
return NS_ERROR_FAILURE; // XXX error code
}
PRBool exists = PR_FALSE;
srv = sqlite3_step(stmt);
// we just care about the return value from step
sqlite3_finalize(stmt);
if (srv == SQLITE_ROW) {
exists = PR_TRUE;
} else if (srv == SQLITE_DONE) {
exists = PR_FALSE;
} else if (srv == SQLITE_ERROR) {
HandleSqliteError("IndexExists finalize");
return NS_ERROR_FAILURE;
}
*_retval = exists;
return NS_OK;
}
/**
** Transactions
**/