Package orm2 :: Package adapters :: Package mysql :: Module datasource
[hide private]
[frames] | no frames]

Source Code for Module orm2.adapters.mysql.datasource

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  ##  This file is part of orm, The Object Relational Membrane Version 2. 
  5  ## 
  6  ##  Copyright 2002-2006 by Diedrich Vorberg <diedrich@tux4web.de> 
  7  ## 
  8  ##  All Rights Reserved 
  9  ## 
 10  ##  For more Information on orm see the README file. 
 11  ## 
 12  ##  This program is free software; you can redistribute it and/or modify 
 13  ##  it under the terms of the GNU General Public License as published by 
 14  ##  the Free Software Foundation; either version 2 of the License, or 
 15  ##  (at your option) any later version. 
 16  ## 
 17  ##  This program is distributed in the hope that it will be useful, 
 18  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ##  GNU General Public License for more details. 
 21  ## 
 22  ##  You should have received a copy of the GNU General Public License 
 23  ##  along with this program; if not, write to the Free Software 
 24  ##  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  ## 
 26  ##  I have added a copy of the GPL in the file COPYING 
 27   
 28  # Changelog 
 29  # --------- 
 30  # 
 31  # $Log: datasource.py,v $ 
 32  # Revision 1.6  2007/12/30 00:58:18  diedrich 
 33  # Added encoding param to mysql datasource. 
 34  # 
 35  # Revision 1.5  2006/09/05 16:54:00  diedrich 
 36  # Of course, Python's database API doesn't support ? as a placeholder 
 37  # but %s. This also means that all %s must be escaped in input SQL. This 
 38  # remains untested for firebird. 
 39  # 
 40  # Revision 1.4  2006/05/13 17:23:41  diedrich 
 41  # Massive docstring update. 
 42  # 
 43  # Revision 1.3  2006/05/02 13:32:01  diedrich 
 44  # Ported mysql adapter from orm v.1 
 45  # 
 46  # Revision 1.2  2006/02/25 00:20:20  diedrich 
 47  # - Added and tested the ability to use multiple column primary keys. 
 48  # - Some small misc bugs. 
 49  # 
 50  # Revision 1.1.1.1  2005/11/20 14:55:46  diedrich 
 51  # Initial import 
 52  # 
 53  # 
 54   
 55  __docformat__ = "epytext en" 
 56   
 57  """ 
 58  This datasource module a datasource class for MySQL databases. 
 59  It relies on the  Module to connect to the backend. See 
 60  U{http://sourceforge.net/projects/mysql-python} for details. 
 61  """ 
 62   
 63  # Python 
 64  from types import * 
 65  from string import * 
 66   
 67  # MySQL 
 68  import MySQLdb # nicht als erstes Modul importieren! 
 69  from _mysql import * 
 70   
 71  # orm 
 72  import orm2.datasource 
 73  from orm2 import sql 
 74  from orm2.datatypes import common_serial 
 75  import datatypes 
 76   
77 -class datasource(orm2.datasource.datasource_base):
78 79 escaped_chars = ( ('"', r'\"',), 80 ("'", r'\"',), 81 ("%", "%%",), ) 82 83 encodings = {"usa7": "us-ascii", 84 "big5": "big5", 85 "gbk": "gbk", 86 "sjis": "sjis", 87 "cp932": "sjis", 88 "cp932": "sjis", 89 "cp932": "sjis", 90 "gb2312": "euc-cn", 91 "ujis": "euc-jp", 92 "euc-kr": "euc-kr", 93 "latin1": "iso8859-1", 94 "latin1-de": "iso8859-1", 95 "german1": "iso8859-1", 96 "danish": "iso8859-1", 97 "latin2": "iso8859-2", 98 "czech": "iso8859-2", 99 "hungarian": "iso8859-2", 100 "croat": "iso8859-2", 101 "greek": "iso8859-7", 102 "hebrew": "iso8859-8", 103 "latin5": "iso8859-9", 104 "latvian": "iso8859-13", 105 "latvian1": "iso8859-13", 106 "estonia": "iso8859-13", 107 "dos": "cp437", 108 "pclatin2": "cp852", 109 "cp866": "cp866", 110 "koi8-ru": "koi8-r", 111 "tis620": "tis620", 112 "win1250": "cp1250", 113 "win1250ch": "cp1250", 114 "win1251": "cp1251", 115 "cp1251": "cp1251", 116 "win1251ukr": "cp1251", 117 "cp1257": "cp1257", 118 "macroman": "macroman", 119 "macce": "maccentraleurope", 120 "utf8": "utf-8", 121 "ucs2": "unicodebig"} 122
123 - def __init__(self, **kwargs):
124 """ 125 This constructor accepts all those keyword args the MySQL module's 126 connect function knows about. They're passed on 'as is'. 127 128 The only exception is 'encoding' which lets you determine which 129 encoding orm uses to send data to the backend. It defaults to 130 utf-8. The set_character_set() function is used on the database 131 connection to set this. Encodings must be referred to by their 132 MySQL name. 133 134 From the MySQL-Python documentation: 135 136 Create a connection to the database. It is strongly recommended 137 that you only use keyword parameters. 'NULL pointer' indicates that 138 NULL will be passed to mysql_real_connect(); the value in parenthesis 139 indicates how MySQL interprets the NULL. Consult the MySQL C API 140 documentation for more information. 141 142 - host -- string, host to connect to or NULL pointer (localhost) 143 - user -- string, user to connect as or NULL pointer (your username) 144 - passwd -- string, password to use or NULL pointer (no password) 145 - db -- string, database to use or NULL (no DB selected) 146 - port -- integer, TCP/IP port to connect to or default MySQL port 147 - unix_socket -- string, location of unix_socket to use or use TCP 148 - client_flags -- integer, flags to use or 0 (see MySQL docs) 149 - conv -- conversion dictionary, see MySQLdb.converters 150 - connect_time -- number of seconds to wait before the connection 151 attempt fails. 152 - compress -- if set, compression is enabled 153 - init_command -- command which is run once the connection is created 154 - read_default_file -- see the MySQL documentation for 155 mysql_options() 156 - read_default_group -- see the MySQL documentation for 157 mysql_options() 158 - cursorclass -- class object, used to create cursors or 159 cursors.Cursor. 160 This parameter MUST be specified as a keyword parameter. 161 162 """ 163 orm2.datasource.datasource_base.__init__(self) 164 if kwargs.has_key("encoding"): 165 encoding = kwargs["encoding"] 166 del kwargs["encoding"] 167 else: 168 encoding = "UTF8" 169 170 if encoding == "utf-8": 171 encoding = "UTF8" 172 173 kwargs["charset"] = encoding 174 175 self._connectionSpec = kwargs 176 self._conn = MySQLdb.connect(**kwargs) 177 self._update_cursor = self._conn.cursor()
178
179 - def _from_params(params):
180 """ 181 Construct a mysql datasource object from an orm connection 182 string. 183 184 A connection string for the mysql adapter may contain all 185 keywords defined in orm2.datasource plus all those that are 186 described as keyword parameters for __init__ above. 187 """ 188 189 if params.has_key("password"): 190 passwd = params["password"] 191 del params["password"] 192 params["passwd"] = passwd 193 194 if params.has_key("dbname"): 195 dbname = params["dbname"] 196 del params["dbname"] 197 params["db"] = dbname 198 199 return datasource(**params)
200 from_params = staticmethod(_from_params) 201
202 - def identifyer_quotes(self, name):
203 return '`%s`' % name
204 205 #def escape_string(self, string): 206 # return self._conn.escape_string(string) 207 # I'm using my own implementation, because MySQLdb uses 208 # mysql_real_escape_string(), which does charset conversion. Yet, this 209 # doesn't make sense on binary data for instance... 210
211 - def backend_encoding(self):
212 name = self._conn.character_set_name() 213 return self.encodings[lower(name)]
214
215 - def select_after_insert_where(self, dbobj):
216 if dbobj.__primary_key__ is None: raise PrimaryKeyNotKnown() 217 218 primary_key_attributes = tuple(dbobj.__primary_key__.attributes()) 219 220 if len(primary_key_attributes) == 1: 221 primary_key_attribute = primary_key_attributes[0] 222 else: 223 primary_key_attribute = None 224 225 if isinstance(primary_key_attribute, datatypes.auto_increment) or \ 226 isinstance(primary_key_attribute, common_serial): 227 where = sql.where(primary_key_attribute.column, 228 " = LAST_INSERT_ID()") 229 230 elif dbobj.__primary_key__.isset(): 231 # If we know the primary key value, we use it to identify 232 # the new row. 233 where = dbobj.__primary_key__.where() 234 235 else: 236 raise PrimaryKeyNotKnown() 237 238 return where
239