1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# firebird.py
# Copyright (C) 2005,2006 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import sys, StringIO, string
import sqlalchemy.engine.default as default
# import sqlalchemy.sql as sql
import sqlalchemy.schema as schema
import sqlalchemy.ansisql as ansisql
# from sqlalchemy import *
import sqlalchemy.types as sqltypes
import sqlalchemy.exceptions as exceptions
try:
import kinterbasdb
except:
kinterbasdb = None
dbmodule = kinterbasdb
kinterbasdb.init(200) # fix this, init args should be passable via db_uri
class FBNumeric(sqltypes.Numeric):
def get_col_spec(self):
return "NUMERIC(%(precision)s, %(length)s)" % {'precision': self.precision, 'length' : self.length}
class FBInteger(sqltypes.Integer):
def get_col_spec(self):
return "INTEGER"
class FBSmallInteger(sqltypes.Smallinteger):
def get_col_spec(self):
return "SMALLINT"
class FBDateTime(sqltypes.DateTime):
def get_col_spec(self):
return "TIMESTAMP"
class FBDate(sqltypes.DateTime):
def get_col_spec(self):
return "DATE"
class FBText(sqltypes.TEXT):
def get_col_spec(self):
return "BLOB SUB_TYPE 2"
class FBString(sqltypes.String):
def get_col_spec(self):
return "VARCHAR(%(length)s)" % {'length' : self.length}
class FBChar(sqltypes.CHAR):
def get_col_spec(self):
return "CHAR(%(length)s)" % {'length' : self.length}
class FBBinary(sqltypes.Binary):
def get_col_spec(self):
return "BLOB SUB_TYPE 1"
class FBBoolean(sqltypes.Boolean):
def get_col_spec(self):
return "SMALLINT"
colspecs = {
sqltypes.Integer : FBInteger,
sqltypes.Smallinteger : FBSmallInteger,
sqltypes.Numeric : FBNumeric,
sqltypes.Float : FBNumeric,
sqltypes.DateTime : FBDateTime,
sqltypes.Date : FBDate,
sqltypes.String : FBString,
sqltypes.Binary : FBBinary,
sqltypes.Boolean : FBBoolean,
sqltypes.TEXT : FBText,
sqltypes.CHAR: FBChar,
}
def engine(*args, **params):
return FBSQLEngine(*args, **params)
def descriptor():
return {'name':'firebird',
'description':'Firebird',
'arguments':[
('host', 'Host Server Name', None),
('database', 'Database Name', None),
('user', 'Username', None),
('password', 'Password', None)
]}
class FireBirdExecutionContext(default.DefaultExecutionContext):
def supports_sane_rowcount(self):
return True
def compiler(self, statement, bindparams, **kwargs):
return FBCompiler(statement, bindparams, **kwargs)
def schemagenerator(self, **params):
return FBSchemaGenerator(self, **params)
def schemadropper(self, **params):
return FBSchemaDropper(self, **params)
def defaultrunner(self, proxy):
return FBDefaultRunner(self, proxy)
class FireBirdDialect(ansisql.ANSIDialect):
def __init__(self, module = None, **params):
self.module = module or dbmodule
self.opts = {}
ansisql.ANSIDialect.__init__(self, **params)
def create_connect_args(self, url):
# self.opts = url.translate_connect_args(['host', 'database', 'user', 'password'])
opts = url.translate_connect_args(['host', 'database', 'user', 'password', 'port'])
if opts.get('port'):
opts['host'] = "%s/%s" % (opts['host'], opts['port'])
del opts['port']
self.opts = opts
print "opts %r" % self.opts
return ([], self.opts)
def connect_args(self):
return make_connect_string(self.opts)
def create_execution_context(self):
return FireBirdExecutionContext(self)
def type_descriptor(self, typeobj):
return sqltypes.adapt_type(typeobj, colspecs)
def supports_sane_rowcount(self):
return True
def compiler(self, statement, bindparams, **kwargs):
return FBCompiler(self, statement, bindparams, **kwargs)
def schemagenerator(self, *args, **kwargs):
return FBSchemaGenerator(*args, **kwargs)
def schemadropper(self, *args, **kwargs):
return FBSchemaDropper(*args, **kwargs)
def defaultrunner(self, engine, proxy):
return FBDefaultRunner(engine, proxy)
def has_table(self, connection, table_name):
tblqry = """\
SELECT count(*)
FROM RDB$RELATION_FIELDS R
WHERE R.RDB$RELATION_NAME=?;"""
c = connection.execute(tblqry, [table_name.upper()])
row = c.fetchone()
if row[0] > 0:
return True
else:
return False
def reflecttable(self, connection, table):
#TODO: map these better
column_func = {
14 : lambda r: sqltypes.String(r['FLEN']), # TEXT
7 : lambda r: sqltypes.Integer(), # SHORT
8 : lambda r: sqltypes.Integer(), # LONG
9 : lambda r: sqltypes.Float(), # QUAD
10 : lambda r: sqltypes.Float(), # FLOAT
27 : lambda r: sqltypes.Double(), # DOUBLE
35 : lambda r: sqltypes.DateTime(), # TIMESTAMP
37 : lambda r: sqltypes.String(r['FLEN']), # VARYING
261: lambda r: sqltypes.TEXT(), # BLOB
40 : lambda r: sqltypes.Char(r['FLEN']), # CSTRING
12 : lambda r: sqltypes.Date(), # DATE
13 : lambda r: sqltypes.Time(), # TIME
16 : lambda r: sqltypes.Numeric(precision=r['FPREC'], length=r['FSCALE'] * -1) #INT64
}
tblqry = """\
SELECT DISTINCT R.RDB$FIELD_NAME AS FNAME,
R.RDB$NULL_FLAG AS NULL_FLAG,
R.RDB$FIELD_POSITION,
F.RDB$FIELD_TYPE AS FTYPE,
F.RDB$FIELD_SUB_TYPE AS STYPE,
F.RDB$FIELD_LENGTH AS FLEN,
F.RDB$FIELD_PRECISION AS FPREC,
F.RDB$FIELD_SCALE AS FSCALE
FROM RDB$RELATION_FIELDS R
JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME
WHERE F.RDB$SYSTEM_FLAG=0 and R.RDB$RELATION_NAME=?
ORDER BY R.RDB$FIELD_POSITION;"""
keyqry = """
SELECT RC.RDB$CONSTRAINT_TYPE KEYTYPE,
RC.RDB$CONSTRAINT_NAME CNAME,
RC.RDB$INDEX_NAME INAME,
SE.RDB$FIELD_NAME SENAME,
FROM RDB$RELATION_CONSTRAINTS RC
LEFT JOIN RDB$INDEX_SEGMENTS SE
ON RC.RDB$INDEX_NAME=SE.RDB$INDEX_NAME
WHERE RC.RDB$RELATION_NAME=? AND SE.RDB$FIELD_NAME=?
"""
#import pdb;pdb.set_trace()
# get all of the fields for this table
c = connection.execute(tblqry, [table.name.upper()])
found_table = False
while True:
row = c.fetchone()
if not row: break
found_table = True
args = [row['FNAME']]
kw = {}
# get the data types and lengths
args.append(column_func[row['FTYPE']](row))
# is it a foreign key (and what is it linked to)
# is it a primary key?
table.append_item(schema.Column(*args, **kw))
# does the field have indexes
if not found_table:
raise exceptions.NoSuchTableError(table.name)
def last_inserted_ids(self):
return self.context.last_inserted_ids
def do_execute(self, cursor, statement, parameters, **kwargs):
cursor.execute(statement, parameters or [])
def do_rollback(self, connection):
connection.rollback(True)
def do_commit(self, connection):
connection.commit(True)
def connection(self):
"""returns a managed DBAPI connection from this SQLEngine's connection pool."""
c = self._pool.connect()
c.supportsTransactions = 0
return c
def dbapi(self):
return self.module
class FBCompiler(ansisql.ANSICompiler):
"""firebird compiler modifies the lexical structure of Select statements to work under
non-ANSI configured Firebird databases, if the use_ansi flag is False."""
def __init__(self, dialect, statement, parameters, **kwargs):
self._outertable = None
super(FBCompiler, self).__init__(dialect, statement, parameters, **kwargs)
def visit_column(self, column):
return ansisql.ANSICompiler.visit_column(self, column)
def visit_function(self, func):
if len(func.clauses):
super(FBCompiler, self).visit_function(func)
else:
self.strings[func] = func.name
def visit_insert(self, insert):
"""inserts are required to have the primary keys be explicitly present.
mapper will by default not put them in the insert statement to comply
with autoincrement fields that require they not be present. so,
put them all in for all primary key columns."""
for c in insert.table.primary_key:
if not self.parameters.has_key(c.key):
self.parameters[c.key] = None
return ansisql.ANSICompiler.visit_insert(self, insert)
def visit_select_precolumns(self, select):
""" called when building a SELECT statment, position is just before column list
Firebird puts the limit and offset right after the select...thanks for adding the
visit_select_precolumns!!!"""
result = ""
if select.limit:
result += " FIRST %d " % select.limit
if select.offset:
result +=" SKIP %d " % select.offset
if select.distinct:
result += " DISTINCT "
return result
def limit_clause(self, select):
"""Already taken care of in the visit_select_precolumns method."""
return ""
class FBSchemaGenerator(ansisql.ANSISchemaGenerator):
def get_column_specification(self, column, override_pk=False, **kwargs):
colspec = column.name
colspec += " " + column.type.engine_impl(self.engine).get_col_spec()
default = self.get_column_default_string(column)
if default is not None:
colspec += " DEFAULT " + default
if not column.nullable:
colspec += " NOT NULL"
if column.primary_key and not override_pk:
colspec += " PRIMARY KEY"
if column.foreign_key:
colspec += " REFERENCES %s(%s)" % (column.foreign_key.column.table.name, column.foreign_key.column.name)
return colspec
def visit_sequence(self, sequence):
self.append("CREATE GENERATOR %s" % sequence.name)
self.execute()
class FBSchemaDropper(ansisql.ANSISchemaDropper):
def visit_sequence(self, sequence):
self.append("DROP GENERATOR %s" % sequence.name)
self.execute()
class FBDefaultRunner(ansisql.ANSIDefaultRunner):
def exec_default_sql(self, default):
c = sql.select([default.arg], from_obj=["rdb$database"], engine=self.engine).compile()
return self.proxy(str(c), c.get_params()).fetchone()[0]
def visit_sequence(self, seq):
return self.proxy("SELECT gen_id(" + seq.name + ", 1) FROM rdb$database").fetchone()[0]
dialect = FireBirdDialect
|