|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "storage_test_harness.h" |
|
8 |
|
9 #include "mozStorageHelper.h" |
|
10 |
|
11 /** |
|
12 * This file test our statement scoper in mozStorageHelper.h. |
|
13 */ |
|
14 |
|
15 void |
|
16 test_automatic_reset() |
|
17 { |
|
18 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); |
|
19 |
|
20 // Need to create a table to populate sqlite_master with an entry. |
|
21 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( |
|
22 "CREATE TABLE test (id INTEGER PRIMARY KEY)" |
|
23 )); |
|
24 |
|
25 nsCOMPtr<mozIStorageStatement> stmt; |
|
26 (void)db->CreateStatement(NS_LITERAL_CSTRING( |
|
27 "SELECT * FROM sqlite_master" |
|
28 ), getter_AddRefs(stmt)); |
|
29 |
|
30 // Reality check - make sure we start off in the right state. |
|
31 int32_t state = -1; |
|
32 (void)stmt->GetState(&state); |
|
33 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY); |
|
34 |
|
35 // Start executing the statement, which will put it into an executing state. |
|
36 { |
|
37 mozStorageStatementScoper scoper(stmt); |
|
38 bool hasMore; |
|
39 do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore))); |
|
40 |
|
41 // Reality check that we are executing. |
|
42 state = -1; |
|
43 (void)stmt->GetState(&state); |
|
44 do_check_true(state == |
|
45 mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING); |
|
46 } |
|
47 |
|
48 // And we should be ready again. |
|
49 state = -1; |
|
50 (void)stmt->GetState(&state); |
|
51 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY); |
|
52 } |
|
53 |
|
54 void |
|
55 test_Abandon() |
|
56 { |
|
57 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); |
|
58 |
|
59 // Need to create a table to populate sqlite_master with an entry. |
|
60 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( |
|
61 "CREATE TABLE test (id INTEGER PRIMARY KEY)" |
|
62 )); |
|
63 |
|
64 nsCOMPtr<mozIStorageStatement> stmt; |
|
65 (void)db->CreateStatement(NS_LITERAL_CSTRING( |
|
66 "SELECT * FROM sqlite_master" |
|
67 ), getter_AddRefs(stmt)); |
|
68 |
|
69 // Reality check - make sure we start off in the right state. |
|
70 int32_t state = -1; |
|
71 (void)stmt->GetState(&state); |
|
72 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY); |
|
73 |
|
74 // Start executing the statement, which will put it into an executing state. |
|
75 { |
|
76 mozStorageStatementScoper scoper(stmt); |
|
77 bool hasMore; |
|
78 do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore))); |
|
79 |
|
80 // Reality check that we are executing. |
|
81 state = -1; |
|
82 (void)stmt->GetState(&state); |
|
83 do_check_true(state == |
|
84 mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING); |
|
85 |
|
86 // And call Abandon. We should not reset now when we fall out of scope. |
|
87 scoper.Abandon(); |
|
88 } |
|
89 |
|
90 // And we should still be executing. |
|
91 state = -1; |
|
92 (void)stmt->GetState(&state); |
|
93 do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING); |
|
94 } |
|
95 |
|
96 void (*gTests[])(void) = { |
|
97 test_automatic_reset, |
|
98 test_Abandon, |
|
99 }; |
|
100 |
|
101 const char *file = __FILE__; |
|
102 #define TEST_NAME "statement scoper" |
|
103 #define TEST_FILE file |
|
104 #include "storage_test_harness_tail.h" |