test_join.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 sql;
34  var q;
35  var c;
36 
37  /*
38  * Tests taken from http://sqlzoo.net/3b.htm
39  */
40 
41  q = newQuery();
42  q.baseTableName = "ttms"
43  q.addColumn(null, "who");
44  q.addColumn("country", "name");
45  q.addJoin(Ci.sbISQLBuilder.JOIN_INNER, "country", null, "id", "ttms",
46  "country");
47  c = q.createMatchCriterionLong(null, "games",
48  Ci.sbISQLBuilder.MATCH_EQUALS,
49  2000);
50  q.addCriterion(c);
51  sql = "select who, country.name from ttms join country on ttms.country = country.id where games = 2000";
52  assertEqual(sql, q.toString());
53 
54 
55  q = newQuery();
56  q.baseTableName = "ttms"
57  q.addColumn(null, "who");
58  q.addColumn(null, "color");
59  q.addJoin(Ci.sbISQLBuilder.JOIN_INNER, "country", null, "id", "ttms",
60  "country");
61  c = q.createMatchCriterionString(null, "name",
62  Ci.sbISQLBuilder.MATCH_EQUALS,
63  "Sweden");
64  q.addCriterion(c);
65  sql = "select who, color from ttms join country on ttms.country = country.id where name = 'Sweden'";
66  assertEqual(sql, q.toString());
67 
68  q = newQuery();
69  q.baseTableName = "ttms"
70  q.addColumn(null, "games");
71  q.addJoin(Ci.sbISQLBuilder.JOIN_INNER, "country", null, "id", "ttms",
72  "country");
73  c = q.createMatchCriterionString(null, "name",
74  Ci.sbISQLBuilder.MATCH_EQUALS,
75  "China");
76  q.addCriterion(c);
77  c = q.createMatchCriterionString(null, "color",
78  Ci.sbISQLBuilder.MATCH_EQUALS,
79  "gold");
80  q.addCriterion(c);
81  sql = "select games from ttms join country on ttms.country = country.id where name = 'China' and color = 'gold'";
82  assertEqual(sql, q.toString());
83 
84  q = newQuery();
85  q.baseTableName = "ttws"
86  q.addColumn(null, "who");
87  q.addJoin(Ci.sbISQLBuilder.JOIN_INNER, "games", null, "yr", "ttws", "games");
88  c = q.createMatchCriterionString(null, "city",
89  Ci.sbISQLBuilder.MATCH_EQUALS,
90  "Barcelona");
91  q.addCriterion(c);
92  sql = "select who from ttws join games on ttws.games = games.yr where city = 'Barcelona'";
93  assertEqual(sql, q.toString());
94 
95  /*
96  * Test critera joins
97  */
98  q = newQuery();
99  q.baseTableName = "leftTable"
100  q.addColumn(null, "bar");
101  c = q.createMatchCriterionTable("leftTable", "a",
102  Ci.sbISQLBuilder.MATCH_EQUALS,
103  "rightTable", "b") ;
104  q.addJoinWithCriterion(Ci.sbISQLBuilder.JOIN_INNER, "right", null, c);
105  sql = "select bar from leftTable join right on leftTable.a = rightTable.b";
106  assertEqual(sql, q.toString());
107 
108  return Components.results.NS_OK;
109 
110 }
111 
112 function newQuery() {
113  return Cc["@songbirdnest.com/Songbird/SQLBuilder/Select;1"]
114  .createInstance(Ci.sbISQLSelectBuilder);
115 }
116 
const Cc
function newQuery()
Definition: test_join.js:112
function assertEqual(aExpected, aActual, aMessage)
return null
Definition: FeedWriter.js:1143
const Ci
function runTest()
Test file.
Definition: test_join.js:31