2
# This file is part of GNU Enterprise.
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.
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.
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.
19
# Copyright 2001-2005 Free Software Foundation
24
from gnue.common.datasources.drivers.DBSIG2.Schema.Creation import \
28
# =============================================================================
29
# Class implementing schema creation for Oracle
30
# =============================================================================
32
class Creation (Base.Creation):
35
ALTER_MULTIPLE = False
39
# ---------------------------------------------------------------------------
40
# Create a new database
41
# ---------------------------------------------------------------------------
43
def createDatabase (self):
47
_("This feature is currently not supported by this driver")
50
# ---------------------------------------------------------------------------
51
# Handle special defaults
52
# ---------------------------------------------------------------------------
54
def _defaultwith (self, code, tableName, fieldDef, forAlter):
56
This function creates a sequence for 'serials' and sets the default for
57
'timestamps' to 'now ()'
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
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))
69
body = u"IF :new.%(field)s IS NULL THEN" \
70
" SELECT %(seq)s.nextval INTO :new.%(field)s FROM dual;" \
72
% {'field': fieldDef ['name'],
74
self.__addTrigger (tableName, fieldDef ['name'], code, body)
77
elif fieldDef ['defaultwith'] == 'timestamp':
78
if fieldDef ['type'] == 'date':
79
sysdate = "TRUNC (sysdate)"
83
body = u"IF :new.%(field)s IS NULL THEN" \
84
" :new.%(field)s := %(date)s;" \
86
% {'field': fieldDef ['name'],
88
self.__addTrigger (tableName, fieldDef ['name'], code, body)
91
# ---------------------------------------------------------------------------
92
# Create a trigger code and add it to the epilogue of the given code
93
# ---------------------------------------------------------------------------
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,
106
# ---------------------------------------------------------------------------
107
# A key is an integer
108
# ---------------------------------------------------------------------------
110
def key (self, fieldDefinition):
112
Native datatype for a 'key'-field is 'int8'
114
@param fieldDefinition: dictionary describing the field
115
@return: string with the native datatype 'int8'
117
return "number (%s)" % self._PK_PRECISION
120
# ---------------------------------------------------------------------------
121
# String becomes varchar2
122
# ---------------------------------------------------------------------------
124
def string (self, fieldDefinition):
127
length = fieldDefinition.get ('length', 99999)
129
return "varchar2 (%s) % length"
134
# ---------------------------------------------------------------------------
135
# Create an apropriate type for a number
136
# ---------------------------------------------------------------------------
138
def number (self, fieldDefinition):
140
This function returns an apropriate type for a number according to the
141
given length and precision.
143
@param fieldDefinition: dictionary describing the field
144
@return: string with the native datatype
146
scale = fieldDefinition.get ('precision', 0)
147
length = fieldDefinition.get ('length', 0)
150
return "number (%s)" % length
152
return "number (%s,%s)" % (length, scale)
155
# ---------------------------------------------------------------------------
156
# Native datatype for boolean is boolean
157
# ---------------------------------------------------------------------------
159
def boolean (self, fieldDefinition):
161
This funciton returns the native data type for a boolean, which is
164
@param fieldDefinition: dictionary describing the field
167
nullable = fieldDefinition.get ('nullable', True)
170
return "number (1) CHECK (%(field)s IS NULL OR %(field)s IN (0,1))" \
171
% {'field': fieldDefinition ['name']}
173
return "number (1) CHECK (%s IN (0,1))" % fieldDefinition ['name']
176
# ---------------------------------------------------------------------------
177
# Native datatype for datetime
178
# ---------------------------------------------------------------------------
180
def datetime (self, fieldDefinition):
182
This function returns the native type for a datetime value
184
@param fieldDefinition: dictionary describing the field
190
# ---------------------------------------------------------------------------
191
# Native datatype for tim
192
# ---------------------------------------------------------------------------
194
def time (self, fieldDefinition):
196
This function returns the native type for a time value
198
@param fieldDefinition: dictionary describing the field