Package library :: Package pyjamas :: Package gears :: Package database :: Module Database
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.gears.database.Database

  1  """ 
  2  * Copyright 2008 Google Inc. 
  3  * 
  4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  5  * use this file except in compliance with the License. You may obtain a copy of 
  6  * the License at 
  7  * 
  8  * http:#www.apache.org/licenses/LICENSE-2.0 
  9  * 
 10  * Unless required by applicable law or agreed to in writing, software 
 11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 13  * License for the specific language governing permissions and limitations under 
 14  * the License. 
 15  """ 
 16   
 17   
 18   
 19   
 20   
 21   
 22  """* 
 23  * An in-browser SQL database. Gears provides a SQL database using SQLite 
 24  * syntax. For details, see the <a href="http:#www.sqlite.org/lang.html">SQLite 
 25  * syntax.</a> 
 26  * 
 27  * Note that this class (and its related classes) intentionally do NOT implement 
 28  * the JDBC interface, since the database provided by Gears does necessarily 
 29  * implement all those semantics. It may be possible to add a JDBC layer on top 
 30  * of this, but it's unclear whether that would really be useful. 
 31  """ 
 32   
 33  from __pyjamas__ import JS 
 34   
35 -class GearsDatabase:
36
37 - def __init__(self, db):
38 39 self.db = db
40 41 """* 42 * Closes the database connection, if any, currently associated with this 43 * Database instance. Calling Database.close() is not required. 44 * 45 * @throws DatabaseException if an error occurs 46 """
47 - def close(self):
48 #try: 49 self.uncheckedClose()
50 #except ex: 51 # raise DatabaseException(ex.getDescription(), ex) 52 53 """* 54 * Executes the specified SQL statement and returns a {@link ResultSet} 55 * containing the results. 56 * 57 * Substitute zero or more bind parameters from <code>args</code> into 58 * <code>sqlStatement</code> and execute the resulting SQL statement. There 59 * must be exactly as many items in <code>args</code> as there are ? place 60 * holders in <code>sqlStatement</code>. <code>args</code> can be omitted 61 * if there are no place holders. The results of executing the statement are 62 * returned in a ResultSet. 63 * 64 * Note that if multiple processes (including Workers) attempt to write to the 65 * database at the same time, one can fail. It is up to the application to 66 * retry in these situations. 67 * 68 * @param sqlStatement SQL statement to execute; may use '?' place holders 69 * @param args values for the place holders in the <code>sqlStatement</code> 70 * @return {@link ResultSet} associated with the SQL statement 71 * @throws DatabaseException if the SQL statement fails or if multiple Workers 72 * attempt to write to the database at the same time 73 """
74 - def execute(self, sqlStatement, *args):
75 #try: 76 if args: 77 return self.execute_args(sqlStatement, args) 78 else: 79 return self.execute_args(sqlStatement)
80 #except ex: 81 # raise DatabaseException(ex.getDescription(), ex) 82 83 """* 84 * Returns the ID of the last row inserted into a table. This is a global 85 * counter, and is unique over all rows in all tables. 86 * 87 * @return the ID of the last row inserted, or 0 if no rows have been inserted 88 * on this connection 89 """
90 - def getLastInsertRowId(self):
91 JS(""" 92 return this['db']['lastInsertRowId']; 93 """)
94 95 """* 96 * Returns the number of database rows that were changed, inserted, or deleted 97 * by the most recently completed INSERT, UPDATE, or DELETE statement on this 98 * Database instance. 99 * 100 * Note that an unconstrained delete of all rows in a table (DELETE FROM 101 * table) will return zero rather than the number of rows that were originally 102 * present in the table; if you need the number of rows, use DELETE FROM table 103 * WHERE 1 instead, though be aware that this is slower than an unconstrained 104 * delete. 105 * 106 * @return the number of database rows impacted by the last INSERT, UPDATE or * 107 * DELETE statement on this Database instance 108 """
109 - def getRowsAffected(self):
110 JS(""" 111 return this['db']['rowsAffected']; 112 """)
113 114 """* 115 * Opens a database with the specified <code>name</code>. Note that this 116 * name is local to the application's origin. 117 * 118 * @param name name of the database (None - Opens an unnamed database.) 119 """
120 - def open(self, name=None):
121 if name is None: 122 JS(" this['db']['open'](); ") 123 else: 124 JS(" this['db']['open'](@{{name}}); ")
125 126
127 - def execute_args(self, sqlStatement, args):
128 JS(""" 129 if (typeof @{{args}} == 'undefined') { 130 return this['db']['execute'](@{{sqlStatement}}); 131 } else { 132 return this['db']['execute'](@{{sqlStatement}}, @{{args}}['l']); 133 } 134 """)
135 136
137 - def uncheckedClose(self):
138 JS(""" 139 this['db']['close'](); 140 """)
141