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 __docformat__ = "epytext en"
48
49 """
50 Datasource for Firebird RDBMS
51 """
52 import sys
53 from string import *
54
55 import kinterbasdb
56
57 import orm2.datasource
58 from orm2 import sql
59 from orm2.datatypes import common_serial
60 import datatypes
61
63
64 encodings = {"ascii": "ascii",
65 "iso8859_1": "iso-8859-1",
66 "iso8859_2": "iso-8859-2",
67 "iso8859_3": "iso-8859-3",
68 "iso8859_4": "iso-8859-4",
69 "iso8859_5": "iso-8859-5",
70 "iso8859_6": "iso-8859-6",
71 "iso8859_7": "iso-8859-7",
72 "iso8859_8": "iso-8859-8",
73 "iso8859_9": "iso-8859-9",
74 "iso8859_10": "iso-8859-10",
75 "iso8859_11": "iso-8859-11",
76 "iso8859_12": "iso-8859-12",
77 "iso8859_13": "iso-8859-13",
78 "UNICODE_FSS": "utf-8" }
79
81 """
82 This constructor supports all those key word parameters the
83 kinterbas.connect() function supports:
84
85 For details on any of these parameters see
86 U{http://kinterbasdb.sourceforge.net/dist_docs/usage.html#tutorial_connect}
87
88 @param dsn: A DSN pointing to the desired database
89 @param user: Username
90 @param password: The corresponding password
91 @param host: The database host (if no DSN given)
92 @param database: The database path (if no DSN given)
93 @param charset: Charset for this connection. Unicode strings will be
94 uncoding using this charset before they are sent to the database.
95 Note that this requires a backend encoding name (see
96 U{this site <http://www.firebirdsql.org/index.php?op=doc&id=fb_1_5_charsets&nosb=1>} for details.
97 @param dialect: Chose the SQL dialect to use. This doesn't influence
98 anything orm does (up to now).
99 """
100 orm2.datasource.datasource_base.__init__(self)
101 self._connection_spec = kw
102 self._conn = kinterbasdb.connect(**kw)
103 self._update_cursor = self._conn.cursor()
104
105
106
108 if kw.has_key("db"):
109 kw["database"] = kw["db"]
110 del kw["db"]
111
112 self = datasource(**kw)
113
114 if kw.has_key("charset"):
115 encoding = kw["charset"]
116 self._backend_encoding = self.encodings.get(encoding, "ascii")
117 else:
118 self._backend_encoding = "iso-8859-1"
119
120 return self
121
122 from_params = staticmethod(_from_params)
123
124
126 return self._backend_encoding
127
128
129 - def insert(self, dbobj, dont_select=False):
130 """
131 Firebird does not provide a mechanism that let's me query the id of
132 the last row I inserted. This has to be done *before* the INSERT.
133 """
134
135 for property in dbobj.__dbproperties__():
136 if isinstance(property, common_serial):
137 sequence_name = "GEN_PK_%s" % dbobj.__relation__
138 elif isinstance(property, datatypes.serial):
139 sequence_name = property.sequence
140 else:
141 continue
142
143 query = sql.select(sql.expression("GEN_ID(", sequence_name,
144 ", 1) AS new_id"),
145 "RDB$DATABASE")
146 cursor = self.execute(query)
147 tpl = cursor.fetchone()
148 new_id = tpl[0]
149
150 property.__set_from_result__(self, dbobj, new_id)
151
152
153 orm2.datasource.datasource_base.insert(self, dbobj, dont_select)
154