Package base :: Package lib :: Package drupy :: Module DrupyMySQL
[hide private]

Source Code for Module base.lib.drupy.DrupyMySQL

  1  #!/usr/bin/env python 
  2   
  3  """ 
  4    A PHP/MySQL abstraction layer for Python. 
  5   
  6    @package drupy 
  7    @see <a href='http://drupy.net'>Drupy Homepage</a> 
  8    @see <a href='http://drupal.org'>Drupal Homepage</a> 
  9    @note Drupy is a port of the Drupal project. 
 10    @author Brendon Crawford 
 11    @copyright 2008 Brendon Crawford 
 12    @contact message144 at users dot sourceforge dot net 
 13    @created 2008-05-10 
 14    @version 0.1 
 15    @note License: 
 16   
 17      This program is free software; you can redistribute it and/or 
 18      modify it under the terms of the GNU General Public License 
 19      as published by the Free Software Foundation; either version 2 
 20      of the License, or (at your option) any later version. 
 21   
 22      This program is distributed in the hope that it will be useful, 
 23      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 24      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 25      GNU General Public License for more details. 
 26   
 27      You should have received a copy of the GNU General Public License 
 28      along with this program; if not, write to: 
 29       
 30      The Free Software Foundation, Inc., 
 31      51 Franklin Street, Fifth Floor, 
 32      Boston, MA  02110-1301, 
 33      USA 
 34  """ 
 35   
 36  __version__ = "$Revision: 1 $" 
 37   
 38  import MySQLdb 
 39  from lib.drupy import DrupyPHP as p  
 40  from lib.drupy import DrupyHelper 
 41   
 42  MYSQLI_ASSOC = 0 
 43  MYSQLI_CLIENT_FOUND_ROWS = 0 
 44   
45 -class __DrupyMySQLMeta:
46 """ 47 Stores error information 48 """
49 - def __init__(self):
50 self.error_connect = [-1, 'No Error']
51
52 - def setErrorConnect(self, n, v):
53 self.error_connect = [n, v]
54
55 - def getErrorConnect(self):
56 return self.error_connect
57 58
59 -class __DrupyMySQLRow:
60 """ 61 Stores row information 62 """
63 - def __init__(self):
64 pass
65 66
67 -def mysqli_real_connect(host = None, username = None, passwd = None, 68 dbname = None, port = None, socket = '', flags = None):
69 """ 70 Opens DB Connections 71 72 @param link NoneType 73 @param host Str 74 @param username Str 75 @param passwd Str 76 @param dbname Str 77 @param port Int 78 @param socket Socket 79 @param flags Int 80 @return Instance[MySQLdb.connect] 81 """ 82 global __DB_META 83 connection = None 84 try: 85 connection = MySQLdb.connect(host, username, passwd, dbname, port) 86 except MySQLdb.Error, e: 87 __DB_META.setErrorConnect(e.args[0], e.args[1]) 88 return connection;
89 90
91 -def mysqli_connect_errno():
92 """ 93 Gets last connect errno 94 95 @return Int 96 """ 97 global __DB_META 98 return __DB_META.getErrorConnect()[0]
99 100
101 -def mysqli_connect_error():
102 """ 103 Gets last connect error 104 105 @return Str 106 """ 107 global __DB_META 108 return __DB_META.getErrorConnect()[1]
109 110
111 -def mysqli_errno(connection):
112 """ 113 Gets last db errno 114 @param connection Instance[MySQLdb.connect] 115 @return Int 116 """ 117 return connection.errno()
118 119
120 -def mysqli_error(connection):
121 """ 122 Gets last db error 123 @param connection Instance[MySQLdb.connect] 124 @return Str 125 """ 126 return connection.error()
127 128 129
130 -def mysqli_query(connection, query, resultmode = None):
131 """ 132 Runs a query 133 134 @param connection Instance[MySQLdb.connect] 135 @param query Str 136 @param resultmode Int 137 @return ??? 138 """ 139 #DrupyHelper.output(True, type(connection)) 140 cursor = MySQLdb.cursors.DictCursor(connection) 141 try: 142 cursor.execute(query) 143 except MySQLdb.Error, e: 144 pass 145 return cursor
146 147
148 -def mysqli_fetch_row(cursor):
149 """ 150 Fetches one row 151 152 @param cursor MySQLdb.cursors.DictCursor 153 @return Tuple 154 """ 155 return tuple(cursor.fetchone().values())
156 157
158 -def mysqli_fetch_assoc(cursor):
159 """ 160 Fetches row as dict 161 162 @param cursor MySQLdb.cursors.DictCursor 163 @return Dict 164 """ 165 return cursor.fetchone()
166 167 168 """ 169 Fetches row as object 170 171 @param cursor MySQLdb.cursors.DictCursor 172 @return Object[DrupyMySQL_row] 173 """
174 -def mysqli_fetch_object(cursor):
175 row = cursor.fetchone() 176 out = __DrupyMySQLRow() 177 if row != None: 178 for k,v in row.items(): 179 setattr(out, k, v) 180 return out 181 else: 182 return False
183
184 -def mysqli_real_escape_string(connection, text):
185 """ 186 Escapes string 187 @param connection MySQLdb.connection 188 @param text Str 189 @return Str 190 """ 191 if isinstance(text, str): 192 return connection.escape_string(text) 193 else: 194 return text
195 196
197 -def mysqli_fetch_array(cursor, w = None):
198 """ 199 Alias for other fetch functions 200 @param cursor MySQLdb.cursors.DictCursor 201 @return Object[DrupyMySQL_row] | Dict 202 """ 203 if w == MYSQLI_ASSOC: 204 return mysqli_fetch_assoc(cursor) 205 else: 206 return mysqli_fetch_row(cursor)
207 208
209 -def mysqli_affected_rows(cursor):
210 """ 211 Gets affexted rows 212 @param cursor MySQLdb.cursors.DictCursor 213 @return Int 214 """ 215 return int(cursor.rowcount)
216 217
218 -def mysqli_init():
219 """ 220 Dummy function 221 @return None 222 """ 223 return None
224 225 226 227 __DB_META = __DrupyMySQLMeta() 228 229 # 230 # Aliases 231 # 232 mysqli_num_rows = mysqli_affected_rows 233