1 | import re |
---|
2 | from .._compat import integer_types, long |
---|
3 | from .base import SQLAdapter |
---|
4 | from . import adapters |
---|
5 | |
---|
6 | |
---|
7 | @adapters.register_for("sapdb") |
---|
8 | class SAPDB(SQLAdapter): |
---|
9 | dbengine = "sapdb" |
---|
10 | drivers = ("sapdb",) |
---|
11 | |
---|
12 | REGEX_URI = ( |
---|
13 | "^(?P<user>[^:@]+)(:(?P<password>[^@]*))?" |
---|
14 | r"@(?P<host>[^:/]+|\[[^\]]+\])/(?P<db>[^?]+)$" |
---|
15 | ) |
---|
16 | |
---|
17 | def _initialize_(self): |
---|
18 | super(SAPDB, self)._initialize_() |
---|
19 | ruri = self.uri.split("://", 1)[1] |
---|
20 | m = re.match(self.REGEX_URI, ruri) |
---|
21 | if not m: |
---|
22 | raise SyntaxError("Invalid URI string in DAL") |
---|
23 | user = self.credential_decoder(m.group("user")) |
---|
24 | password = self.credential_decoder(m.group("password")) |
---|
25 | if password is None: |
---|
26 | password = "" |
---|
27 | host = m.group("host") |
---|
28 | db = m.group("db") |
---|
29 | self.driver_args.update(user=user, password=password, database=db, host=host) |
---|
30 | |
---|
31 | def connector(self): |
---|
32 | self.driver.connect(**self.driver_args) |
---|
33 | |
---|
34 | def lastrowid(self, table): |
---|
35 | self.execute("select %s.NEXTVAL from dual" % table._sequence_name) |
---|
36 | return long(self.cursor.fetchone()[0]) |
---|
37 | |
---|
38 | def create_sequence_and_triggers(self, query, table, **args): |
---|
39 | self.execute("CREATE SEQUENCE %s;" % table._sequence_name) |
---|
40 | self.execute( |
---|
41 | "ALTER TABLE %s ALTER COLUMN %s SET DEFAULT NEXTVAL('%s');" |
---|
42 | % (table._rname, table._id._rname, table._sequence_name) |
---|
43 | ) |
---|
44 | self.execute(query) |
---|