summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_connection_executeAsync.js
blob: e56d98e55520ab3a9ef7773c6f94dcbbc17d8c88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * This file tests the functionality of mozIStorageConnection::executeAsync for
 * both mozIStorageStatement and mozIStorageAsyncStatement.
 *
 * A single database connection is used for the entirety of the test, which is
 * a legacy thing, but we otherwise use the modern promise-based driver and
 * async helpers.
 */

const INTEGER = 1;
const TEXT = "this is test text";
const REAL = 3.23;
const BLOB = [1, 2];

add_task(function* test_first_create_and_add() {
  // synchronously open the database and let gDBConn hold onto it because we
  // use this database
  let db = getOpenedDatabase();
  // synchronously set up our table *that will be used for the rest of the file*
  db.executeSimpleSQL(
    "CREATE TABLE test (" +
      "id INTEGER, " +
      "string TEXT, " +
      "number REAL, " +
      "nuller NULL, " +
      "blober BLOB" +
    ")"
  );

  let stmts = [];
  stmts[0] = db.createStatement(
    "INSERT INTO test (id, string, number, nuller, blober) VALUES (?, ?, ?, ?, ?)"
  );
  stmts[0].bindByIndex(0, INTEGER);
  stmts[0].bindByIndex(1, TEXT);
  stmts[0].bindByIndex(2, REAL);
  stmts[0].bindByIndex(3, null);
  stmts[0].bindBlobByIndex(4, BLOB, BLOB.length);
  stmts[1] = getOpenedDatabase().createAsyncStatement(
    "INSERT INTO test (string, number, nuller, blober) VALUES (?, ?, ?, ?)"
  );
  stmts[1].bindByIndex(0, TEXT);
  stmts[1].bindByIndex(1, REAL);
  stmts[1].bindByIndex(2, null);
  stmts[1].bindBlobByIndex(3, BLOB, BLOB.length);

  // asynchronously execute the statements
  let execResult = yield executeMultipleStatementsAsync(
    db,
    stmts,
    function(aResultSet) {
      ok(false, 'we only did inserts so we should not have gotten results!');
    });
  equal(Ci.mozIStorageStatementCallback.REASON_FINISHED, execResult,
        'execution should have finished successfully.');

  // Check that the result is in the table
  let stmt = db.createStatement(
    "SELECT string, number, nuller, blober FROM test WHERE id = ?"
  );
  stmt.bindByIndex(0, INTEGER);
  try {
    do_check_true(stmt.executeStep());
    do_check_eq(TEXT, stmt.getString(0));
    do_check_eq(REAL, stmt.getDouble(1));
    do_check_true(stmt.getIsNull(2));
    let count = { value: 0 };
    let blob = { value: null };
    stmt.getBlob(3, count, blob);
    do_check_eq(BLOB.length, count.value);
    for (let i = 0; i < BLOB.length; i++)
      do_check_eq(BLOB[i], blob.value[i]);
  }
  finally {
    stmt.finalize();
  }

  // Make sure we have two rows in the table
  stmt = db.createStatement(
    "SELECT COUNT(1) FROM test"
  );
  try {
    do_check_true(stmt.executeStep());
    do_check_eq(2, stmt.getInt32(0));
  }
  finally {
    stmt.finalize();
  }

  stmts[0].finalize();
  stmts[1].finalize();
});

add_task(function* test_last_multiple_bindings_on_statements() {
  // This tests to make sure that we pass all the statements multiply bound
  // parameters when we call executeAsync.
  const AMOUNT_TO_ADD = 5;
  const ITERATIONS = 5;

  let stmts = [];
  let db = getOpenedDatabase();
  let sqlString = "INSERT INTO test (id, string, number, nuller, blober) " +
                    "VALUES (:int, :text, :real, :null, :blob)";
  // We run the same statement twice, and should insert 2 * AMOUNT_TO_ADD.
  for (let i = 0; i < ITERATIONS; i++) {
    // alternate the type of statement we create
    if (i % 2)
      stmts[i] = db.createStatement(sqlString);
    else
      stmts[i] = db.createAsyncStatement(sqlString);

    let params = stmts[i].newBindingParamsArray();
    for (let j = 0; j < AMOUNT_TO_ADD; j++) {
      let bp = params.newBindingParams();
      bp.bindByName("int", INTEGER);
      bp.bindByName("text", TEXT);
      bp.bindByName("real", REAL);
      bp.bindByName("null", null);
      bp.bindBlobByName("blob", BLOB, BLOB.length);
      params.addParams(bp);
    }
    stmts[i].bindParameters(params);
  }

  // Get our current number of rows in the table.
  let currentRows = 0;
  let countStmt = getOpenedDatabase().createStatement(
    "SELECT COUNT(1) AS count FROM test"
  );
  try {
    do_check_true(countStmt.executeStep());
    currentRows = countStmt.row.count;
  }
  finally {
    countStmt.reset();
  }

  // Execute asynchronously.
  let execResult = yield executeMultipleStatementsAsync(
    db,
    stmts,
    function(aResultSet) {
      ok(false, 'we only did inserts so we should not have gotten results!');
    });
  equal(Ci.mozIStorageStatementCallback.REASON_FINISHED, execResult,
        'execution should have finished successfully.');

  // Check to make sure we added all of our rows.
  try {
    do_check_true(countStmt.executeStep());
    do_check_eq(currentRows + (ITERATIONS * AMOUNT_TO_ADD),
                countStmt.row.count);
  }
  finally {
    countStmt.finalize();
  }

  stmts.forEach(stmt => stmt.finalize());

  // we are the last test using this connection and since it has gone async
  // we *must* call asyncClose on it.
  yield asyncClose(db);
  gDBConn = null;
});

// If you add a test down here you will need to move the asyncClose or clean
// things up a little more.