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

Source Code for Module orm2.adapters.firebird.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.3  2006/09/05 16:54:00  diedrich 
 33  # Of course, Python's database API doesn't support ? as a placeholder 
 34  # but %s. This also means that all %s must be escaped in input SQL. This 
 35  # remains untested for firebird. 
 36  # 
 37  # Revision 1.2  2006/05/13 17:23:41  diedrich 
 38  # Massive docstring update. 
 39  # 
 40  # Revision 1.1  2006/05/10 21:53:28  diedrich 
 41  # Initial commit 
 42  # 
 43  # 
 44  # 
 45  # 
 46   
 47  __docformat__ = "epytext en" 
 48   
 49  """ 
 50  Datasource for Firebird RDBMS 
 51  """ 
 52  import sys 
 53  from string import * 
 54   
 55  import kinterbasdb 
 56   
 57  import orm2.datasource 
 58  from orm2 import sql 
 59  from orm2.datatypes import common_serial 
 60  import datatypes 
 61   
62 -class datasource(orm2.datasource.datasource_base):
63 64 encodings = {"ascii": "ascii", 65 "iso8859_1": "iso-8859-1", 66 "iso8859_2": "iso-8859-2", 67 "iso8859_3": "iso-8859-3", 68 "iso8859_4": "iso-8859-4", 69 "iso8859_5": "iso-8859-5", 70 "iso8859_6": "iso-8859-6", 71 "iso8859_7": "iso-8859-7", 72 "iso8859_8": "iso-8859-8", 73 "iso8859_9": "iso-8859-9", 74 "iso8859_10": "iso-8859-10", 75 "iso8859_11": "iso-8859-11", 76 "iso8859_12": "iso-8859-12", 77 "iso8859_13": "iso-8859-13", 78 "UNICODE_FSS": "utf-8" } 79
80 - def __init__(self, **kw):
81 """ 82 This constructor supports all those key word parameters the 83 kinterbas.connect() function supports: 84 85 For details on any of these parameters see 86 U{http://kinterbasdb.sourceforge.net/dist_docs/usage.html#tutorial_connect} 87 88 @param dsn: A DSN pointing to the desired database 89 @param user: Username 90 @param password: The corresponding password 91 @param host: The database host (if no DSN given) 92 @param database: The database path (if no DSN given) 93 @param charset: Charset for this connection. Unicode strings will be 94 uncoding using this charset before they are sent to the database. 95 Note that this requires a backend encoding name (see 96 U{this site <http://www.firebirdsql.org/index.php?op=doc&id=fb_1_5_charsets&nosb=1>} for details. 97 @param dialect: Chose the SQL dialect to use. This doesn't influence 98 anything orm does (up to now). 99 """ 100 orm2.datasource.datasource_base.__init__(self) 101 self._connection_spec = kw 102 self._conn = kinterbasdb.connect(**kw) 103 self._update_cursor = self._conn.cursor()
104 105 106
107 - def _from_params(kw):
108 if kw.has_key("db"): 109 kw["database"] = kw["db"] 110 del kw["db"] 111 112 self = datasource(**kw) 113 114 if kw.has_key("charset"): 115 encoding = kw["charset"] 116 self._backend_encoding = self.encodings.get(encoding, "ascii") 117 else: 118 self._backend_encoding = "iso-8859-1" # probably the most common 119 120 return self
121 122 from_params = staticmethod(_from_params) 123 124
125 - def backend_encoding(self):
126 return self._backend_encoding
127 128
129 - def insert(self, dbobj, dont_select=False):
130 """ 131 Firebird does not provide a mechanism that let's me query the id of 132 the last row I inserted. This has to be done *before* the INSERT. 133 """ 134 135 for property in dbobj.__dbproperties__(): 136 if isinstance(property, common_serial): 137 sequence_name = "GEN_PK_%s" % dbobj.__relation__ 138 elif isinstance(property, datatypes.serial): 139 sequence_name = property.sequence 140 else: 141 continue 142 143 query = sql.select(sql.expression("GEN_ID(", sequence_name, 144 ", 1) AS new_id"), 145 "RDB$DATABASE") 146 cursor = self.execute(query) 147 tpl = cursor.fetchone() 148 new_id = tpl[0] 149 150 property.__set_from_result__(self, dbobj, new_id) 151 152 153 orm2.datasource.datasource_base.insert(self, dbobj, dont_select)
154