test_bind.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
31 function runTest () {
32 
33  var dbq = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
34  .createInstance(Ci.sbIDatabaseQuery);
35 
36  var ios = Cc["@mozilla.org/network/io-service;1"]
37  .createInstance(Ci.nsIIOService);
38 
39  var dir = Cc["@mozilla.org/file/directory_service;1"]
40  .createInstance(Ci.nsIProperties);
41 
42  var testdir = dir.get("ProfD", Ci.nsIFile);
43 
44  var actualdir = testdir.clone();
45  actualdir.append("db_tests");
46 
47  if(!actualdir.exists())
48  {
49  try {
50  actualdir.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
51  } catch(e) {
52  //Some failures might be handled later. Some might be ignored.
53  throw e;
54  }
55  }
56 
57  var uri = ios.newFileURI(actualdir);
58  dbq.databaseLocation = uri;
59 
60  assertEqual(dbq.databaseLocation.spec, uri.spec);
61 
62  dbq.setDatabaseGUID("test_bind");
63  dbq.addQuery("drop table bind_test");
64  dbq.addQuery("create table bind_test (utf8_column text, " +
65  "string_column text, double_column real, " +
66  "int32_column integer, int64_column integer)");
67  dbq.addQuery("insert into bind_test values " +
68  "('foo', 'bar', 1234.567, 666, 9876543210)");
69  dbq.addQuery("insert into bind_test values " +
70  "('Sigur Rs', 'gtis Byrjun', -1234.567, -666, -9876543210)");
71  dbq.execute();
72  dbq.waitForCompletion();
73  dbq.resetQuery();
74 
75  // Test bind when there is no query
76  try {
77  dbq.bindUTF8StringParameter(0, "hello world");
78  fail("No exception thrown");
79  }
80  catch(e) {
81  assertEqual(e.result, Cr.NS_ERROR_FAILURE);
82  }
83 
84  // Test each binding method
85  dbq.addQuery("select * from bind_test where utf8_column = ?");
86  dbq.bindUTF8StringParameter(0, "foo");
87  execAndAssertCount(dbq, 1);
88 
89  dbq.resetQuery();
90  dbq.addQuery("select * from bind_test where utf8_column = ?");
91  dbq.bindUTF8StringParameter(0, "Sigur Rs); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where string_column = ?"); dbq.bindStringParameter(0, "bar"); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where string_column = ?"); dbq.bindStringParameter(0, "gtis Byrjun"); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where double_column = ?"); dbq.bindDoubleParameter(0, 1234.567); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where int32_column = ?"); dbq.bindInt32Parameter(0, 666); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where int32_column = ?"); dbq.bindInt32Parameter(0, -666); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where int64_column = ?"); dbq.bindInt64Parameter(0, 9876543210); execAndAssertCount(dbq, 1); dbq.resetQuery(); dbq.addQuery("select * from bind_test where int64_column = ?"); dbq.bindInt64Parameter(0, -9876543210); execAndAssertCount(dbq, 1); // Test an insert -- this lets us test the null binding method dbq.resetQuery(); dbq.addQuery("insert into bind_test values (?, ?, ?, ?, ?)"); dbq.bindNullParameter(0); dbq.bindStringParameter(1, "bar2"); dbq.bindDoubleParameter(2, 567.0); dbq.bindInt32Parameter(3, 0); dbq.bindInt64Parameter(4, -1); dbq.execute(); dbq.waitForCompletion(); // Make sure that null was inserted dbq.resetQuery(); dbq.addQuery("select * from bind_test where utf8_column is null"); execAndAssertCount(dbq, 1); // Make sure that everything else was inserted dbq.resetQuery(); dbq.addQuery("select * from bind_test where string_column = ? and " + "double_column = ? and int32_column = ? and int64_column = ?"); dbq.bindStringParameter(0, "bar2"); dbq.bindDoubleParameter(1, 567.0); dbq.bindInt32Parameter(2, 0); dbq.bindInt64Parameter(3, -1); execAndAssertCount(dbq, 1); // Test binding multiple queries dbq.resetQuery(); dbq.addQuery("select * from bind_test where utf8_column = ?"); dbq.bindUTF8StringParameter(0, "foo"); dbq.addQuery("select * from bind_test where string_column = ?"); dbq.bindStringParameter(0, "bar"); dbq.addQuery("select * from bind_test where int32_column = ?"); dbq.bindInt32Parameter(0, 0); execAndAssertCount(dbq, 3); return Components.results.NS_OK; } function execAndAssertCount(dbq, numRows) { dbq.execute(); dbq.waitForCompletion(); assertEqual(dbq.getResultObject().getRowCount(), numRows); } ");
92  execAndAssertCount(dbq, 1);
93 
94  dbq.resetQuery();
95  dbq.addQuery("select * from bind_test where string_column = ?");
96  dbq.bindStringParameter(0, "bar");
97  execAndAssertCount(dbq, 1);
98 
99  dbq.resetQuery();
100  dbq.addQuery("select * from bind_test where string_column = ?");
101  dbq.bindStringParameter(0, "gtis Byrjun");
102  execAndAssertCount(dbq, 1);
103 
104  dbq.resetQuery();
105  dbq.addQuery("select * from bind_test where double_column = ?");
106  dbq.bindDoubleParameter(0, 1234.567);
107  execAndAssertCount(dbq, 1);
108 
109  dbq.resetQuery();
110  dbq.addQuery("select * from bind_test where int32_column = ?");
111  dbq.bindInt32Parameter(0, 666);
112  execAndAssertCount(dbq, 1);
113 
114  dbq.resetQuery();
115  dbq.addQuery("select * from bind_test where int32_column = ?");
116  dbq.bindInt32Parameter(0, -666);
117  execAndAssertCount(dbq, 1);
118 
119  dbq.resetQuery();
120  dbq.addQuery("select * from bind_test where int64_column = ?");
121  dbq.bindInt64Parameter(0, 9876543210);
122  execAndAssertCount(dbq, 1);
123 
124  dbq.resetQuery();
125  dbq.addQuery("select * from bind_test where int64_column = ?");
126  dbq.bindInt64Parameter(0, -9876543210);
127  execAndAssertCount(dbq, 1);
128 
129  // Test an insert -- this lets us test the null binding method
130  dbq.resetQuery();
131  dbq.addQuery("insert into bind_test values (?, ?, ?, ?, ?)");
132  dbq.bindNullParameter(0);
133  dbq.bindStringParameter(1, "bar2");
134  dbq.bindDoubleParameter(2, 567.0);
135  dbq.bindInt32Parameter(3, 0);
136  dbq.bindInt64Parameter(4, -1);
137  dbq.execute();
138  dbq.waitForCompletion();
139 
140  // Make sure that null was inserted
141  dbq.resetQuery();
142  dbq.addQuery("select * from bind_test where utf8_column is null");
143  execAndAssertCount(dbq, 1);
144 
145  // Make sure that everything else was inserted
146  dbq.resetQuery();
147  dbq.addQuery("select * from bind_test where string_column = ? and " +
148  "double_column = ? and int32_column = ? and int64_column = ?");
149  dbq.bindStringParameter(0, "bar2");
150  dbq.bindDoubleParameter(1, 567.0);
151  dbq.bindInt32Parameter(2, 0);
152  dbq.bindInt64Parameter(3, -1);
153  execAndAssertCount(dbq, 1);
154 
155  // Test binding multiple queries
156  dbq.resetQuery();
157  dbq.addQuery("select * from bind_test where utf8_column = ?");
158  dbq.bindUTF8StringParameter(0, "foo");
159  dbq.addQuery("select * from bind_test where string_column = ?");
160  dbq.bindStringParameter(0, "bar");
161  dbq.addQuery("select * from bind_test where int32_column = ?");
162  dbq.bindInt32Parameter(0, 0);
163  execAndAssertCount(dbq, 3);
164 
165  return Components.results.NS_OK;
166 }
167 
168 function execAndAssertCount(dbq, numRows) {
169  dbq.execute();
170  dbq.waitForCompletion();
171  assertEqual(dbq.getResultObject().getRowCount(), numRows);
172 }
173 
const Cc
function fail(aMessage)
function execAndAssertCount(dbq, numRows)
Definition: test_bind.js:168
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Test file.
Definition: test_bind.js:31
var uri
Definition: FeedWriter.js:1135
const Cr
const Ci
var ios
Definition: head_feeds.js:5