Package orm2 :: Package adapters :: Package pgsql :: Module datatypes
[hide private]
[frames] | no frames]

Source Code for Module orm2.adapters.pgsql.datatypes

  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 gpl.txt. 
 27   
 28   
 29  # Changelog 
 30  # --------- 
 31  # 
 32  # $Log: datatypes.py,v $ 
 33  # Revision 1.10  2006/10/07 22:06:24  diedrich 
 34  # Use datasource's psycopg_version to figure out how to handly bytea literals. 
 35  # 
 36  # Revision 1.9  2006/09/06 23:20:04  diedrich 
 37  # Fixed bytea 
 38  # 
 39  # Revision 1.8  2006/09/04 15:55:32  diedrich 
 40  # Added blob datatype that uses Python's buffer class and 
 41  # sql.direct_literal to pass binary data to psycopg's cursors directly. 
 42  # 
 43  # Revision 1.7  2006/07/08 17:10:32  diedrich 
 44  # Added bytea as alias to text 
 45  # 
 46  # Revision 1.6  2006/06/10 18:05:09  diedrich 
 47  # - Rewrote widget handling 
 48  # 
 49  # Revision 1.5  2006/05/13 17:23:41  diedrich 
 50  # Massive docstring update. 
 51  # 
 52  # Revision 1.4  2006/05/10 21:55:41  diedrich 
 53  # Changed default for validators parameter to () rather than None 
 54  # 
 55  # Revision 1.3  2005/12/31 18:32:12  diedrich 
 56  # - Updated year in copyright header ;) 
 57  # - Fixed the serial type 
 58  # 
 59  # Revision 1.2  2005/12/18 22:35:46  diedrich 
 60  # - Inheritance 
 61  # - pgsql adapter 
 62  # - first unit tests 
 63  # - some more comments 
 64  # 
 65  # Revision 1.1  2005/11/21 19:50:23  diedrich 
 66  # Initial commit 
 67  # 
 68  # 
 69  # 
 70  __docformat__ = "epytext en" 
 71   
 72  """ 
 73  This module implements datatype classes that are specific to PostgreSQL. 
 74  """ 
 75   
 76  # Python 
 77  import sys 
 78  import string 
 79  from types import * 
 80   
 81  # orm 
 82  from orm2 import sql 
 83  from orm2.datatypes import * 
 84  from orm2.util.fixedpoint import FixedPoint 
 85   
86 -class serial(integer):
87 """ 88 Datatype class for PostgreSQL serial columns 89 """
90 - def __init__(self, column=None, 91 sequence_name=None):
92 """ 93 @param sequence_name: The SQL identifyer of the sequence used for 94 this serial. It defaults to the one created by the backend. 95 """ 96 integer.__init__(self, column=column, title=None, 97 validators=(), has_default=True) 98 self.sequence_name = sequence_name
99
100 - def __select_after_insert__(self, dbobj):
101 # When we've already got a value, we can't be inserted again. 102 103 if self.isset(dbobj): 104 tpl = ( self.attribute_name, 105 self.dbclass.__name__, 106 repr(dbobj.__primary_key__), ) 107 108 raise ObjectAlreadyInserted( 109 "Attribute %s of '%s' (%s) has already been set." % tpl) 110 111 return True
112
113 - def __set__(self, dbobj, value):
114 if self.isset(dbobj): 115 raise ORMException( "A serial property is not mutable, " + \ 116 "once it is set on object creation" ) 117 else: 118 integer.__set__(self, dbobj, value)
119 120 121
122 -class bytea_literal(sql.literal):
123 - def __init__(self, bindata):
124 if type(bindata) != BufferType: 125 self.bindata = buffer(bindata) 126 else: 127 self.bindata = bindata
128
129 - def __sql__(self, runner):
130 if runner.ds.psycopg_version[0] == "1": 131 from psycopg import Binary 132 runner.params.append(Binary(str(self.bindata))) 133 elif runner.ds.psycopg_version[0] == "2": 134 runner.params.append(self.bindata) 135 else: 136 raise Exception("I don't know you psycopg") 137 138 return "%s"
139
140 -class bytea(datatype):
141 python_class = str 142 sql_literal_class = bytea_literal
143 144 145 146 blob = bytea 147 148 # Local variables: 149 # mode: python 150 # ispell-local-dictionary: "english" 151 # End: 152