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

Source Code for Module orm2.adapters.gadfly.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.7  2006/09/04 15:53:00  diedrich 
 33  # The special treatment gadfly used to need was made obsolete by 
 34  # sql.sql's ability to handle the '?' syntax for cursor.execute(). 
 35  # 
 36  # Revision 1.6  2006/06/09 09:07:39  diedrich 
 37  # Removed differences between gadfly ds and the others with regard to 
 38  # fetchone() and fetchall() which is now handled by dbobject.result 
 39  # 
 40  # Revision 1.5  2006/05/13 17:23:41  diedrich 
 41  # Massive docstring update. 
 42  # 
 43  # Revision 1.4  2006/05/13 14:55:58  diedrich 
 44  # Use dbobject.__select_columns__() in select() instead of doing the 
 45  # same thing by hand. 
 46  # 
 47  # Revision 1.3  2006/04/15 23:15:07  diedrich 
 48  # Split up select() in select() and run_select() 
 49  # 
 50  # Revision 1.2  2006/01/01 20:45:38  diedrich 
 51  # The gadfly datasource got its own execute() method (un_escaper 
 52  # mechanism, see docstrings). 
 53  # 
 54  # Revision 1.1  2005/12/31 18:30:30  diedrich 
 55  # Initial commit 
 56  # 
 57  # 
 58  # 
 59  # 
 60   
 61  """ 
 62  This datasouce module uses the Python-only, filesystem based SQL 
 63  backend called gadfly by Aaron Watters, currently maintained by 
 64  Richard Jones <richard@users.sf.net>. It is available at 
 65  U{http://gadfly.sourceforge.net}. 
 66  """ 
 67   
 68  __author__ = "Diedrich Vorberg <diedrich@tux4web.de>" 
 69  __version__ = "$Revision: 1.8 $"[11:-2] 
 70   
 71  # Python 
 72  from types import * 
 73  from sets import Set 
 74  import re 
 75   
 76  # orm 
 77  from orm2.datasource import datasource_base 
 78  from orm2.debug import sqllog, debug 
 79  from orm2.exceptions import * 
 80  from orm2.datatypes import common_serial 
 81  from orm2 import sql 
 82  from orm2.util import stupid_dict 
 83   
 84  import orm2.datasource 
 85   
 86  from gadfly import gadfly 
 87   
88 -class datasource(datasource_base):
89 """ 90 An orm database adapter for gadfly. 91 """ 92 no_fetchone = True 93
94 - def __init__(self, dbname="tmp", directory="/tmp", 95 encoding = "iso-8859-1"):
96 """ 97 The default values will create a database called tmp in your 98 /tmp directory. Gadfly will create a number of files called dbname.* 99 there. 100 """ 101 orm2.datasource.datasource_base.__init__(self) 102 103 self._conn = gadfly() 104 self._conn.startup(dbname, directory) 105 self.encoding = encoding 106 107 self._update_cursor = self.cursor()
108
109 - def _from_params(params):
110 database_name = params.get("dbname", "tmp") 111 database_directory = params.get("directory", "/tmp") 112 encoding = params.get("encoding", "iso-8859-1") 113 114 return datasource(database_name, database_directory, encoding)
115 116 from_params = staticmethod(_from_params) 117
118 - def backend_encoding(self):
119 return self.encoding
120
121 - def insert(self, dbobj, dont_select=False):
122 """ 123 The gadfly backend does not provide a mechanism to create unique 124 keys for new rows. Values for the common_serial() datatype must be 125 determined by the insert() function. It will query the maximum value 126 of the id column and increment it. 127 """ 128 # does the dbobj have a common_serial property? 129 query_id = False 130 for property in dbobj.__dbproperties__(): 131 if isinstance(property, common_serial): 132 common_serial_property = property 133 query_id = True 134 break 135 136 if query_id: 137 query = "SELECT COUNT(*) FROM %s" % dbobj.__relation__ 138 cursor = self.execute(query) 139 tpl = cursor.fetchone() 140 count = tpl[0] 141 142 if count == 0: 143 new_id = 1 144 else: 145 query = "SELECT MAX(id) FROM %s" % dbobj.__relation__ 146 cursor = self.execute(query) 147 tpl = cursor.fetchone() 148 max_id = tpl[0] 149 150 new_id = max_id + 1 151 152 common_serial_property.__set_from_result__(self, dbobj, new_id) 153 154 datasource_base.insert(self, dbobj, dont_select)
155
156 - def select_one(self, dbclass, *clauses):
157 """ 158 Gadfly doesn't support the LIMIT clause. 159 """ 160 result = self.select(dbclass, *clauses) 161 result = list(result) 162 163 if len(result) == 0: 164 return None 165 else: 166 return result[0]
167 168
169 - def select_after_insert(self, dbobj):
170 """ 171 The gadfly backend neither supports default values for columns 172 not owns a mechanism to provide unique keys for new rows. So the 173 select_after_insert() mechanism is useless. 174 """ 175 pass
176
177 - def select_after_insert_where(self, dbobj):
178 """ 179 See select_after_insert() above. 180 """ 181 raise NotImplemented()
182