void test_SpinEventsLoopInHandleResult() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create a test table and populate it. nsCOMPtr<mozIStorageStatement> stmt; db->CreateStatement(NS_LITERAL_CSTRING( "CREATE TABLE test (id INTEGER PRIMARY KEY)" ), getter_AddRefs(stmt)); stmt->Execute(); stmt->Finalize(); db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (id) VALUES (?)" ), getter_AddRefs(stmt)); for (int32_t i = 0; i < 30; ++i) { stmt->BindInt32ByIndex(0, i); stmt->Execute(); stmt->Reset(); } stmt->Finalize(); db->CreateStatement(NS_LITERAL_CSTRING( "SELECT * FROM test" ), getter_AddRefs(stmt)); nsCOMPtr<mozIStoragePendingStatement> ps; do_check_success(stmt->ExecuteAsync(new UnownedCallback(db), getter_AddRefs(ps))); stmt->Finalize(); spin_events_loop_until_true(&UnownedCallback::sResult); }
void test_SpinEventsLoopInHandleError() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create a test table and populate it. nsCOMPtr<mozIStorageStatement> stmt; db->CreateStatement(NS_LITERAL_CSTRING( "CREATE TABLE test (id INTEGER PRIMARY KEY)" ), getter_AddRefs(stmt)); stmt->Execute(); stmt->Finalize(); db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (id) VALUES (1)" ), getter_AddRefs(stmt)); stmt->Execute(); stmt->Finalize(); // This will cause a constraint error. db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (id) VALUES (1)" ), getter_AddRefs(stmt)); nsCOMPtr<mozIStoragePendingStatement> ps; do_check_success(stmt->ExecuteAsync(new UnownedCallback(db), getter_AddRefs(ps))); stmt->Finalize(); spin_events_loop_until_true(&UnownedCallback::sError); }
void test_HasTransaction() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // First test that it holds the transaction after it should have gotten one. { mozStorageTransaction transaction(db, PR_FALSE); do_check_true(transaction.HasTransaction()); (void)transaction.Commit(); // And that it does not have a transaction after we have committed. do_check_false(transaction.HasTransaction()); } // Check that no transaction is had after a rollback. { mozStorageTransaction transaction(db, PR_FALSE); do_check_true(transaction.HasTransaction()); (void)transaction.Rollback(); do_check_false(transaction.HasTransaction()); } // Check that we do not have a transaction if one is already obtained. mozStorageTransaction outerTransaction(db, PR_FALSE); do_check_true(outerTransaction.HasTransaction()); { mozStorageTransaction innerTransaction(db, PR_FALSE); do_check_false(innerTransaction.HasTransaction()); } }
void test_SetDefaultAction() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // First we test that rollback happens when we first set it to automatically // commit. { mozStorageTransaction transaction(db, PR_TRUE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test1 (id INTEGER PRIMARY KEY)" )); transaction.SetDefaultAction(PR_FALSE); } PRBool exists = PR_TRUE; (void)db->TableExists(NS_LITERAL_CSTRING("test1"), &exists); do_check_false(exists); // Now we do the opposite and test that a commit happens when we first set it // to automatically rollback. { mozStorageTransaction transaction(db, PR_FALSE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test2 (id INTEGER PRIMARY KEY)" )); transaction.SetDefaultAction(PR_TRUE); } exists = PR_FALSE; (void)db->TableExists(NS_LITERAL_CSTRING("test2"), &exists); do_check_true(exists); }
void test_CString() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create table with a single string column. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (str STRING)" )); // Create statements to INSERT and SELECT the string. nsCOMPtr<mozIStorageStatement> insert, select; (void)db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (str) VALUES (?1)" ), getter_AddRefs(insert)); (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT str FROM test" ), getter_AddRefs(select)); // Roundtrip a string through the table, and ensure it comes out as expected. static const char sCharArray[] = "I'm not a \xff\x00\xac\xde\xbb ASCII string!"; nsCAutoString inserted(sCharArray, NS_ARRAY_LENGTH(sCharArray) - 1); do_check_true(inserted.Length() == NS_ARRAY_LENGTH(sCharArray) - 1); { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } { nsCAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); do_check_true(result == inserted); } (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); }
void test_AutoCommit() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create a table in a transaction, and make sure that it exists after the // transaction falls out of scope. This means the Commit was successful. { mozStorageTransaction transaction(db, PR_TRUE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (id INTEGER PRIMARY KEY)" )); } PRBool exists = PR_FALSE; (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); do_check_true(exists); }
void test_NULLFallback() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); nsCOMPtr<mozIStorageStatement> stmt; (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT NULL" ), getter_AddRefs(stmt)); nsCOMPtr<mozIStorageValueArray> valueArray = do_QueryInterface(stmt); do_check_true(valueArray); bool hasMore; do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)) && hasMore); do_check_eq(stmt->AsInt32(0), 0); do_check_eq(stmt->AsInt64(0), 0); do_check_eq(stmt->AsDouble(0), 0.0); PRUint32 len = 100; do_check_eq(stmt->AsSharedUTF8String(0, &len), NULL); do_check_eq(len, 0); len = 100; do_check_eq(stmt->AsSharedWString(0, &len), NULL); do_check_eq(len, 0); len = 100; do_check_eq(stmt->AsSharedBlob(0, &len), NULL); do_check_eq(len, 0); do_check_eq(stmt->IsNull(0), true); do_check_eq(valueArray->AsInt32(0), 0); do_check_eq(valueArray->AsInt64(0), 0); do_check_eq(valueArray->AsDouble(0), 0.0); len = 100; do_check_eq(valueArray->AsSharedUTF8String(0, &len), NULL); do_check_eq(len, 0); len = 100; do_check_eq(valueArray->AsSharedWString(0, &len), NULL); do_check_eq(len, 0); len = 100; do_check_eq(valueArray->AsSharedBlob(0, &len), NULL); do_check_eq(len, 0); do_check_eq(valueArray->IsNull(0), true); }
void test_asyncNULLFallback() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); nsCOMPtr<mozIStorageAsyncStatement> stmt; (void)db->CreateAsyncStatement(NS_LITERAL_CSTRING( "SELECT NULL" ), getter_AddRefs(stmt)); nsCOMPtr<mozIStoragePendingStatement> pendingStmt; do_check_true(NS_SUCCEEDED(stmt->ExecuteAsync(nullptr, getter_AddRefs(pendingStmt)))); do_check_true(pendingStmt); stmt->Finalize(); nsRefPtr<Spinner> asyncSpin(new Spinner()); db->AsyncClose(asyncSpin); asyncSpin->SpinUntilCompleted(); }
void test_Rollback() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create a table in a transaction, call Rollback, and make sure that it does // not exists after the transaction falls out of scope. { mozStorageTransaction transaction(db, PR_TRUE); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (id INTEGER PRIMARY KEY)" )); (void)transaction.Rollback(); } PRBool exists = PR_TRUE; (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); do_check_false(exists); }
void test_ASCIIString() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create table with a single string column. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (str STRING)" )); // Create statements to INSERT and SELECT the string. nsCOMPtr<mozIStorageStatement> insert, select; (void)db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (str) VALUES (?1)" ), getter_AddRefs(insert)); (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT str FROM test" ), getter_AddRefs(select)); // Roundtrip a string through the table, and ensure it comes out as expected. nsCAutoString inserted("I'm an ASCII string"); { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } nsCAutoString result; { mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); } do_check_true(result == inserted); (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); }
void test_Abandon() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Need to create a table to populate sqlite_master with an entry. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (id INTEGER PRIMARY KEY)" )); nsCOMPtr<mozIStorageStatement> stmt; (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT * FROM sqlite_master" ), getter_AddRefs(stmt)); // Reality check - make sure we start off in the right state. int32_t state = -1; (void)stmt->GetState(&state); do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY); // Start executing the statement, which will put it into an executing state. { mozStorageStatementScoper scoper(stmt); bool hasMore; do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore))); // Reality check that we are executing. state = -1; (void)stmt->GetState(&state); do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING); // And call Abandon. We should not reset now when we fall out of scope. scoper.Abandon(); } // And we should still be executing. state = -1; (void)stmt->GetState(&state); do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING); }
void test_UTFStrings() { nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); // Create table with a single string column. (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE test (str STRING)" )); // Create statements to INSERT and SELECT the string. nsCOMPtr<mozIStorageStatement> insert, select; (void)db->CreateStatement(NS_LITERAL_CSTRING( "INSERT INTO test (str) VALUES (?1)" ), getter_AddRefs(insert)); (void)db->CreateStatement(NS_LITERAL_CSTRING( "SELECT str FROM test" ), getter_AddRefs(select)); // Roundtrip a UTF8 string through the table, using UTF8 input and output. static const char sCharArray[] = "I'm a \xc3\xbb\xc3\xbc\xc3\xa2\xc3\xa4\xc3\xa7 UTF8 string!"; nsCAutoString insertedUTF8(sCharArray, NS_ARRAY_LENGTH(sCharArray) - 1); do_check_true(insertedUTF8.Length() == NS_ARRAY_LENGTH(sCharArray) - 1); NS_ConvertUTF8toUTF16 insertedUTF16(insertedUTF8); do_check_true(insertedUTF8 == NS_ConvertUTF16toUTF8(insertedUTF16)); { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, insertedUTF8))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } { nsCAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); do_check_true(result == insertedUTF8); } // Use UTF8 input and UTF16 output. { nsAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetString(0, result))); do_check_true(result == insertedUTF16); } (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); // Roundtrip the same string using UTF16 input and UTF8 output. { mozStorageStatementScoper scoper(insert); bool hasResult; do_check_true(NS_SUCCEEDED(insert->BindStringByIndex(0, insertedUTF16))); do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult))); do_check_false(hasResult); } { nsCAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result))); do_check_true(result == insertedUTF8); } // Use UTF16 input and UTF16 output. { nsAutoString result; mozStorageStatementScoper scoper(select); bool hasResult; do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult))); do_check_true(hasResult); do_check_true(NS_SUCCEEDED(select->GetString(0, result))); do_check_true(result == insertedUTF16); } (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test")); }