зеркало из https://github.com/mozilla/pjs.git
fix for SQLITE_SCHEMA error, as well as using multiple-named-statement binding functions
This commit is contained in:
Родитель
5b8840178c
Коммит
518d570061
|
@ -65,6 +65,12 @@ interface mozIStorageStatement : mozIStorageValueArray {
|
|||
*/
|
||||
AUTF8String getParameterName(in unsigned long aParamIndex);
|
||||
|
||||
/*
|
||||
* Indexes of named parameter
|
||||
*/
|
||||
void getParameterIndexes(in AUTF8String aParameterName,
|
||||
out unsigned long aCount,
|
||||
[array,size_is(aCount),retval] out unsigned long aIndexes);
|
||||
/*
|
||||
* Number of columns returned
|
||||
*/
|
||||
|
|
|
@ -93,6 +93,11 @@ mozStorageStatement::Initialize(mozIStorageConnection *aDBConnection, const nsAC
|
|||
{
|
||||
int srv;
|
||||
|
||||
// we can't do this if we're mid-execute
|
||||
if (mExecuting) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
sqlite3 *db = nsnull;
|
||||
// XXX - need to implement a private iid to QI for here, to make sure
|
||||
// we have a real mozStorageConnection
|
||||
|
@ -100,6 +105,12 @@ mozStorageStatement::Initialize(mozIStorageConnection *aDBConnection, const nsAC
|
|||
db = msc->GetNativeConnection();
|
||||
NS_ENSURE_TRUE(db != nsnull, NS_ERROR_NULL_POINTER);
|
||||
|
||||
// clean up old goop
|
||||
if (mDBStatement) {
|
||||
sqlite3_finalize(mDBStatement);
|
||||
mDBStatement = nsnull;
|
||||
}
|
||||
|
||||
int nRetries = 0;
|
||||
|
||||
while (nRetries < 2) {
|
||||
|
@ -124,6 +135,7 @@ mozStorageStatement::Initialize(mozIStorageConnection *aDBConnection, const nsAC
|
|||
mStatementString.Assign (aSQLStatement);
|
||||
mParamCount = sqlite3_bind_parameter_count (mDBStatement);
|
||||
mResultColumnCount = sqlite3_column_count (mDBStatement);
|
||||
mColumnNames.Clear();
|
||||
|
||||
for (unsigned int i = 0; i < mResultColumnCount; i++) {
|
||||
const void *name = sqlite3_column_name16 (mDBStatement, i);
|
||||
|
@ -194,6 +206,29 @@ mozStorageStatement::GetParameterName(PRUint32 aParamIndex, nsACString & _retval
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void getParameterIndexes(in AUTF8String aParameterName, out unsigned long aCount, [array,size_is(aCount),retval] out unsigned long aIndexes); */
|
||||
NS_IMETHODIMP
|
||||
mozStorageStatement::GetParameterIndexes(const nsACString &aParameterName, PRUint32 *aCount, PRUint32 **aIndexes)
|
||||
{
|
||||
NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized");
|
||||
NS_ENSURE_ARG_POINTER(aCount);
|
||||
NS_ENSURE_ARG_POINTER(aIndexes);
|
||||
|
||||
int *indexes, count;
|
||||
count = sqlite3_bind_parameter_indexes(mDBStatement, nsPromiseFlatCString(aParameterName).get(), &indexes);
|
||||
if (count) {
|
||||
*aIndexes = (PRUint32*) nsMemory::Alloc(sizeof(PRUint32) * count);
|
||||
for (int i = 0; i < count; i++)
|
||||
(*aIndexes)[i] = indexes[i];
|
||||
sqlite3_free_parameter_indexes(indexes);
|
||||
*aCount = count;
|
||||
} else {
|
||||
*aCount = 0;
|
||||
*aIndexes = nsnull;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* readonly attribute unsigned long columnCount; */
|
||||
NS_IMETHODIMP
|
||||
mozStorageStatement::GetColumnCount(PRUint32 *aColumnCount)
|
||||
|
@ -381,7 +416,7 @@ mozStorageStatement::Execute()
|
|||
return NS_ERROR_FAILURE; // XXX error code
|
||||
} else if (srv == SQLITE_SCHEMA) {
|
||||
nRetries++;
|
||||
sqlite3_reset (mDBStatement);
|
||||
Initialize(mDBConnection, mStatementString);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
@ -407,6 +442,7 @@ NS_IMETHODIMP
|
|||
mozStorageStatement::ExecuteStep(PRBool *_retval)
|
||||
{
|
||||
NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized");
|
||||
nsresult rv;
|
||||
|
||||
int nRetries = 0;
|
||||
|
||||
|
@ -414,7 +450,7 @@ mozStorageStatement::ExecuteStep(PRBool *_retval)
|
|||
int srv = sqlite3_step (mDBStatement);
|
||||
|
||||
#ifdef PR_LOGGING
|
||||
if ((srv == SQLITE_SCHEMA && nRetries != 0) ||
|
||||
if (((srv == SQLITE_SCHEMA || srv == SQLITE_ERROR) && nRetries != 0) ||
|
||||
(srv != SQLITE_SCHEMA &&
|
||||
srv != SQLITE_ROW &&
|
||||
srv != SQLITE_DONE))
|
||||
|
@ -437,8 +473,7 @@ mozStorageStatement::ExecuteStep(PRBool *_retval)
|
|||
*_retval = PR_FALSE;
|
||||
return NS_OK;
|
||||
} else if (srv == SQLITE_BUSY ||
|
||||
srv == SQLITE_MISUSE ||
|
||||
srv == SQLITE_ERROR)
|
||||
srv == SQLITE_MISUSE)
|
||||
{
|
||||
mExecuting = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
|
@ -452,9 +487,31 @@ mozStorageStatement::ExecuteStep(PRBool *_retval)
|
|||
}
|
||||
|
||||
// otherwise, we reset the statement and try again
|
||||
sqlite3_reset (mDBStatement);
|
||||
rv = Initialize(mDBConnection, mStatementString);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
nRetries++;
|
||||
} else if (srv != SQLITE_SCHEMA) {
|
||||
} else if (srv == SQLITE_ERROR) {
|
||||
// so we may end up with a SQLITE_SCHEMA only after
|
||||
// we finalize, because SQLite's error reporting story
|
||||
// sucks.
|
||||
if (mExecuting == PR_TRUE) {
|
||||
mExecuting = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
srv = sqlite3_finalize(mDBStatement);
|
||||
mDBStatement = nsnull;
|
||||
|
||||
rv = Initialize(mDBConnection, mStatementString);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
if (srv == SQLITE_SCHEMA) {
|
||||
nRetries++;
|
||||
} else {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
} else {
|
||||
// something that shouldn't happen happened
|
||||
NS_ERROR ("sqlite3_step returned an error code we don't know about!");
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ class mozStorageStatementRow : public mozIStorageStatementRow,
|
|||
public nsIXPCScriptable
|
||||
{
|
||||
public:
|
||||
mozStorageStatementRow(sqlite3_stmt *aStatement,
|
||||
mozStorageStatementRow(mozIStorageStatement *aStatement,
|
||||
int aNumColumns,
|
||||
const nsStringArray *aColumnNames);
|
||||
|
||||
|
@ -66,7 +66,11 @@ public:
|
|||
// nsIXPCScriptable interface
|
||||
NS_DECL_NSIXPCSCRIPTABLE
|
||||
protected:
|
||||
sqlite3_stmt *mDBStatement;
|
||||
sqlite3_stmt* NativeStatement() {
|
||||
return mStatement->GetNativeStatementPointer();
|
||||
}
|
||||
|
||||
nsCOMPtr<mozIStorageStatement> mStatement;
|
||||
int mNumColumns;
|
||||
const nsStringArray *mColumnNames;
|
||||
};
|
||||
|
@ -93,26 +97,34 @@ protected:
|
|||
static PRBool
|
||||
JSValStorageStatementBinder (JSContext *cx,
|
||||
mozIStorageStatement *aStatement,
|
||||
PRUint32 aParamIndex,
|
||||
int *aParamIndexes,
|
||||
int aNumIndexes,
|
||||
jsval val)
|
||||
{
|
||||
int i;
|
||||
if (JSVAL_IS_INT(val)) {
|
||||
int v = JSVAL_TO_INT(val);
|
||||
aStatement->BindInt32Parameter(aParamIndex, v);
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindInt32Parameter(aParamIndexes[i], v);
|
||||
} else if (JSVAL_IS_DOUBLE(val)) {
|
||||
double d = *JSVAL_TO_DOUBLE(val);
|
||||
aStatement->BindDoubleParameter(aParamIndex, d);
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindDoubleParameter(aParamIndexes[i], d);
|
||||
} else if (JSVAL_IS_STRING(val)) {
|
||||
JSString *str = JSVAL_TO_STRING(val);
|
||||
aStatement->BindWStringParameter(aParamIndex, NS_REINTERPRET_CAST(PRUnichar*, JS_GetStringChars(str)));
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindWStringParameter(aParamIndexes[i], NS_REINTERPRET_CAST(PRUnichar*, JS_GetStringChars(str)));
|
||||
} else if (JSVAL_IS_BOOLEAN(val)) {
|
||||
if (val == JSVAL_TRUE) {
|
||||
aStatement->BindInt32Parameter(aParamIndex, 1);
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindInt32Parameter(aParamIndexes[i], 1);
|
||||
} else {
|
||||
aStatement->BindInt32Parameter(aParamIndex, 0);
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindInt32Parameter(aParamIndexes[i], 0);
|
||||
}
|
||||
} else if (JSVAL_IS_NULL(val)) {
|
||||
aStatement->BindNullParameter(aParamIndex);
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindNullParameter(aParamIndexes[i]);
|
||||
} else if (JSVAL_IS_OBJECT(val)) {
|
||||
JSObject *obj = JSVAL_TO_OBJECT(val);
|
||||
// some special things
|
||||
|
@ -122,7 +134,8 @@ JSValStorageStatementBinder (JSContext *cx,
|
|||
PRInt64 msec;
|
||||
LL_D2L(msec, msecd);
|
||||
|
||||
aStatement->BindInt64Parameter(aParamIndex, msec);
|
||||
for (i = 0; i < aNumIndexes; i++)
|
||||
aStatement->BindInt64Parameter(aParamIndexes[i], msec);
|
||||
} else {
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
@ -143,14 +156,13 @@ JSValStorageStatementBinder (JSContext *cx,
|
|||
NS_IMPL_ISUPPORTS2(mozStorageStatementWrapper, mozIStorageStatementWrapper, nsIXPCScriptable)
|
||||
|
||||
mozStorageStatementWrapper::mozStorageStatementWrapper()
|
||||
: mStatement(nsnull), mDBStatement(nsnull)
|
||||
: mStatement(nsnull)
|
||||
{
|
||||
}
|
||||
|
||||
mozStorageStatementWrapper::~mozStorageStatementWrapper()
|
||||
{
|
||||
mStatement = nsnull;
|
||||
mDBStatement = nsnull;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
@ -161,15 +173,12 @@ mozStorageStatementWrapper::Initialize(mozIStorageStatement *aStatement)
|
|||
|
||||
mStatement = aStatement;
|
||||
|
||||
// fetch the actual statement out
|
||||
mDBStatement = mStatement->GetNativeStatementPointer();
|
||||
|
||||
// fetch various things we care about
|
||||
mStatement->GetParameterCount(&mParamCount);
|
||||
mStatement->GetNumColumns(&mResultColumnCount);
|
||||
|
||||
for (unsigned int i = 0; i < mResultColumnCount; i++) {
|
||||
const void *name = sqlite3_column_name16 (mDBStatement, i);
|
||||
const void *name = sqlite3_column_name16 (NativeStatement(), i);
|
||||
mColumnNames.AppendString(nsDependentString(NS_STATIC_CAST(const PRUnichar*, name)));
|
||||
}
|
||||
|
||||
|
@ -224,7 +233,7 @@ mozStorageStatementWrapper::GetRow(mozIStorageStatementRow **aRow)
|
|||
return NS_ERROR_FAILURE;
|
||||
|
||||
if (!mStatementRow) {
|
||||
mozStorageStatementRow *row = new mozStorageStatementRow(mDBStatement, mResultColumnCount, &mColumnNames);
|
||||
mozStorageStatementRow *row = new mozStorageStatementRow(mStatement, mResultColumnCount, &mColumnNames);
|
||||
if (!row)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
mStatementRow = row;
|
||||
|
@ -294,8 +303,8 @@ mozStorageStatementWrapper::Call(nsIXPConnectWrappedNative *wrapper, JSContext *
|
|||
mStatement->Reset();
|
||||
|
||||
// bind parameters
|
||||
for (PRUint32 i = 0; i < argc; i++) {
|
||||
if (!JSValStorageStatementBinder(cx, mStatement, i, argv[i])) {
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (!JSValStorageStatementBinder(cx, mStatement, &i, 1, argv[i])) {
|
||||
*_retval = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
@ -450,10 +459,10 @@ mozStorageStatementWrapper::Mark(nsIXPConnectWrappedNative *wrapper, JSContext *
|
|||
|
||||
NS_IMPL_ISUPPORTS2(mozStorageStatementRow, mozIStorageStatementRow, nsIXPCScriptable)
|
||||
|
||||
mozStorageStatementRow::mozStorageStatementRow(sqlite3_stmt *aStatement,
|
||||
mozStorageStatementRow::mozStorageStatementRow(mozIStorageStatement *aStatement,
|
||||
int aNumColumns,
|
||||
const nsStringArray *aColumnNames)
|
||||
: mDBStatement(aStatement),
|
||||
: mStatement(aStatement),
|
||||
mNumColumns(aNumColumns),
|
||||
mColumnNames(aColumnNames)
|
||||
{
|
||||
|
@ -498,18 +507,18 @@ mozStorageStatementRow::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContex
|
|||
|
||||
for (int i = 0; i < mNumColumns; i++) {
|
||||
if (jsid.Equals(*(*mColumnNames)[i])) {
|
||||
int ctype = sqlite3_column_type(mDBStatement, i);
|
||||
int ctype = sqlite3_column_type(NativeStatement(), i);
|
||||
|
||||
if (ctype == SQLITE_INTEGER || ctype == SQLITE_FLOAT) {
|
||||
double dval = sqlite3_column_double(mDBStatement, i);
|
||||
double dval = sqlite3_column_double(NativeStatement(), i);
|
||||
if (!JS_NewNumberValue(cx, dval, vp)) {
|
||||
*_retval = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
} else if (ctype == SQLITE_TEXT) {
|
||||
JSString *str = JS_NewUCStringCopyN(cx,
|
||||
(jschar*) sqlite3_column_text16(mDBStatement, i),
|
||||
sqlite3_column_bytes16(mDBStatement, i)/2);
|
||||
(jschar*) sqlite3_column_text16(NativeStatement(), i),
|
||||
sqlite3_column_bytes16(NativeStatement(), i)/2);
|
||||
if (!str) {
|
||||
*_retval = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
|
@ -517,8 +526,8 @@ mozStorageStatementRow::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContex
|
|||
*vp = STRING_TO_JSVAL(str);
|
||||
} else if (ctype == SQLITE_BLOB) {
|
||||
JSString *str = JS_NewStringCopyN(cx,
|
||||
(char*) sqlite3_column_blob(mDBStatement, i),
|
||||
sqlite3_column_bytes(mDBStatement, i));
|
||||
(char*) sqlite3_column_blob(NativeStatement(), i),
|
||||
sqlite3_column_bytes(NativeStatement(), i));
|
||||
if (!str) {
|
||||
*_retval = PR_FALSE;
|
||||
return NS_OK;
|
||||
|
@ -741,40 +750,38 @@ NS_IMETHODIMP
|
|||
mozStorageStatementParams::SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
|
||||
JSObject * obj, jsval id, jsval * vp, PRBool *_retval)
|
||||
{
|
||||
int idx = -1;
|
||||
|
||||
if (JSVAL_IS_INT(id)) {
|
||||
idx = JSVAL_TO_INT(id);
|
||||
int idx = JSVAL_TO_INT(id);
|
||||
|
||||
*_retval = JSValStorageStatementBinder (cx, mStatement, &idx, 1, *vp);
|
||||
} else if (JSVAL_IS_STRING(id)) {
|
||||
int indexCount = 0, *indexes;
|
||||
|
||||
JSString *str = JSVAL_TO_STRING(id);
|
||||
nsCAutoString name(":");
|
||||
name.Append(NS_ConvertUTF16toUTF8(nsDependentString((PRUnichar *)::JS_GetStringChars(str),
|
||||
::JS_GetStringLength(str))));
|
||||
|
||||
// check to see if there's a parameter with this name
|
||||
|
||||
idx = sqlite3_bind_parameter_index(mStatement->GetNativeStatementPointer(), name.get());
|
||||
if (idx == 0) {
|
||||
indexCount = sqlite3_bind_parameter_indexes(mStatement->GetNativeStatementPointer(), name.get(), &indexes);
|
||||
if (indexCount == 0) {
|
||||
// er, not found? How'd we get past NewResolve?
|
||||
fprintf (stderr, "********** mozStorageStatementWrapper: Couldn't find parameter %s\n", name.get());
|
||||
*_retval = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
} else {
|
||||
// drop this by 1, to account for sqlite's indexes being 1-based
|
||||
idx -= 1;
|
||||
for (int i = 0; i < indexCount; i++)
|
||||
indexes[i]--;
|
||||
|
||||
*_retval = JSValStorageStatementBinder (cx, mStatement, indexes, indexCount, *vp);
|
||||
sqlite3_free_parameter_indexes(indexes);
|
||||
}
|
||||
} else {
|
||||
*_retval = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
if (idx == -1) {
|
||||
*_retval = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
// we have a valid param index, so do the conversion from JSVal -> bind
|
||||
*_retval = JSValStorageStatementBinder (cx, mStatement, idx, *vp);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -851,6 +858,7 @@ mozStorageStatementParams::NewResolve(nsIXPConnectWrappedNative *wrapper, JSCont
|
|||
idx = sqlite3_bind_parameter_index(mStatement->GetNativeStatementPointer(), name.get());
|
||||
if (idx == 0) {
|
||||
// nope.
|
||||
fprintf (stderr, "********** mozStorageStatementWrapper: Couldn't find parameter %s\n", name.get());
|
||||
*_retval = PR_FALSE;
|
||||
return NS_OK;
|
||||
} else {
|
||||
|
|
|
@ -66,9 +66,12 @@ private:
|
|||
~mozStorageStatementWrapper();
|
||||
|
||||
protected:
|
||||
sqlite3_stmt* NativeStatement() {
|
||||
return mStatement->GetNativeStatementPointer();
|
||||
}
|
||||
|
||||
// note: pointer to the concrete statement
|
||||
nsCOMPtr<mozIStorageStatement> mStatement;
|
||||
sqlite3_stmt *mDBStatement;
|
||||
PRUint32 mParamCount;
|
||||
PRUint32 mResultColumnCount;
|
||||
nsStringArray mColumnNames;
|
||||
|
|
Загрузка…
Ссылка в новой задаче