~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/datasources/drivers/oracle/Schema/Creation/Creation.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# $Id: $
 
22
 
 
23
import os
 
24
from gnue.common.datasources.drivers.DBSIG2.Schema.Creation import \
 
25
    Creation as Base
 
26
 
 
27
 
 
28
# =============================================================================
 
29
# Class implementing schema creation for Oracle
 
30
# =============================================================================
 
31
 
 
32
class Creation (Base.Creation):
 
33
 
 
34
  MAX_NAME_LENGTH = 31
 
35
  ALTER_MULTIPLE  = False
 
36
  _PK_PRECISION   = 10
 
37
 
 
38
 
 
39
  # ---------------------------------------------------------------------------
 
40
  # Create a new database
 
41
  # ---------------------------------------------------------------------------
 
42
 
 
43
  def createDatabase (self):
 
44
    """
 
45
    """
 
46
    raise gException, \
 
47
        _("This feature is currently not supported by this driver")
 
48
 
 
49
 
 
50
  # ---------------------------------------------------------------------------
 
51
  # Handle special defaults
 
52
  # ---------------------------------------------------------------------------
 
53
 
 
54
  def _defaultwith (self, code, tableName, fieldDef, forAlter):
 
55
    """
 
56
    This function creates a sequence for 'serials' and sets the default for
 
57
    'timestamps' to 'now ()'
 
58
 
 
59
    @param code: code-tuple to merge the result in
 
60
    @param tableName: name of the table
 
61
    @param fieldDef: dictionary describing the field with the default
 
62
    @param forAlter: TRUE if the definition is used in a table modification
 
63
    """
 
64
    if fieldDef ['defaultwith'] == 'serial':
 
65
      seq = self._getSequenceName (tableName, fieldDef)
 
66
      code [0].append (u"CREATE SEQUENCE %s MAXVALUE %s NOCYCLE%s" \
 
67
          % (seq, "9" * self._PK_PRECISION, self.END_COMMAND))
 
68
 
 
69
      body = u"IF :new.%(field)s IS NULL THEN" \
 
70
              " SELECT %(seq)s.nextval INTO :new.%(field)s FROM dual;" \
 
71
              "END IF;" \
 
72
            % {'field': fieldDef ['name'],
 
73
               'seq': seq}
 
74
      self.__addTrigger (tableName, fieldDef ['name'], code, body)
 
75
 
 
76
 
 
77
    elif fieldDef ['defaultwith'] == 'timestamp':
 
78
      if fieldDef ['type'] == 'date':
 
79
        sysdate = "TRUNC (sysdate)"
 
80
      else:
 
81
        sysdate = "sysdate"
 
82
 
 
83
      body = u"IF :new.%(field)s IS NULL THEN" \
 
84
              " :new.%(field)s := %(date)s;" \
 
85
              "END IF;" \
 
86
            % {'field': fieldDef ['name'],
 
87
               'date' : sysdate}
 
88
      self.__addTrigger (tableName, fieldDef ['name'], code, body)
 
89
 
 
90
 
 
91
  # ---------------------------------------------------------------------------
 
92
  # Create a trigger code and add it to the epilogue of the given code
 
93
  # ---------------------------------------------------------------------------
 
94
 
 
95
  def __addTrigger (self, tablename, fieldname, code, body):
 
96
    code [2].append (u"CREATE OR REPLACE TRIGGER t__%(table)s_%(field)s_pre"
 
97
           "  BEFORE INSERT ON %(table)s"
 
98
           "  FOR EACH ROW WHEN (new.%(field)s IS NULL)"
 
99
           "  BEGIN %(body)s END; /"
 
100
          % {'table': tablename,
 
101
             'field': fieldname,
 
102
             'body': body})
 
103
 
 
104
 
 
105
 
 
106
  # ---------------------------------------------------------------------------
 
107
  # A key is an integer
 
108
  # ---------------------------------------------------------------------------
 
109
 
 
110
  def key (self, fieldDefinition):
 
111
    """
 
112
    Native datatype for a 'key'-field is 'int8'
 
113
 
 
114
    @param fieldDefinition: dictionary describing the field
 
115
    @return: string with the native datatype 'int8'
 
116
    """
 
117
    return "number (%s)" % self._PK_PRECISION
 
118
 
 
119
 
 
120
  # ---------------------------------------------------------------------------
 
121
  # String becomes varchar2
 
122
  # ---------------------------------------------------------------------------
 
123
 
 
124
  def string (self, fieldDefinition):
 
125
    """
 
126
    """
 
127
    length = fieldDefinition.get ('length', 99999)
 
128
    if length <= 2000:
 
129
      return "varchar2 (%s) % length"
 
130
    else:
 
131
      return "blob"
 
132
 
 
133
 
 
134
  # ---------------------------------------------------------------------------
 
135
  # Create an apropriate type for a number
 
136
  # ---------------------------------------------------------------------------
 
137
 
 
138
  def number (self, fieldDefinition):
 
139
    """
 
140
    This function returns an apropriate type for a number according to the
 
141
    given length and precision.
 
142
 
 
143
    @param fieldDefinition: dictionary describing the field
 
144
    @return: string with the native datatype
 
145
    """
 
146
    scale  = fieldDefinition.get ('precision', 0)
 
147
    length = fieldDefinition.get ('length', 0)
 
148
 
 
149
    if scale == 0:
 
150
      return "number (%s)" % length
 
151
    else:
 
152
      return "number (%s,%s)" % (length, scale)
 
153
 
 
154
 
 
155
  # ---------------------------------------------------------------------------
 
156
  # Native datatype for boolean is boolean
 
157
  # ---------------------------------------------------------------------------
 
158
 
 
159
  def boolean (self, fieldDefinition):
 
160
    """
 
161
    This funciton returns the native data type for a boolean, which is
 
162
    'boolean'
 
163
 
 
164
    @param fieldDefinition: dictionary describing the field
 
165
    @return: 'boolean'
 
166
    """
 
167
    nullable = fieldDefinition.get ('nullable', True)
 
168
 
 
169
    if nullable:
 
170
      return "number (1) CHECK (%(field)s IS NULL OR %(field)s IN (0,1))" \
 
171
          % {'field': fieldDefinition ['name']}
 
172
    else:
 
173
      return "number (1) CHECK (%s IN (0,1))" % fieldDefinition ['name']
 
174
 
 
175
 
 
176
  # ---------------------------------------------------------------------------
 
177
  # Native datatype for datetime
 
178
  # ---------------------------------------------------------------------------
 
179
 
 
180
  def datetime (self, fieldDefinition):
 
181
    """
 
182
    This function returns the native type for a datetime value
 
183
 
 
184
    @param fieldDefinition: dictionary describing the field
 
185
    @return: 'date'
 
186
    """
 
187
    return "date"
 
188
 
 
189
 
 
190
  # ---------------------------------------------------------------------------
 
191
  # Native datatype for tim
 
192
  # ---------------------------------------------------------------------------
 
193
 
 
194
  def time (self, fieldDefinition):
 
195
    """
 
196
    This function returns the native type for a time value
 
197
 
 
198
    @param fieldDefinition: dictionary describing the field
 
199
    @return: 'date'
 
200
    """
 
201
    return "date"