29 |
# |
# |
30 |
# Supported attributes (via connections.conf or <database> tag) |
# Supported attributes (via connections.conf or <database> tag) |
31 |
# |
# |
32 |
# host= This is the SAP-DB host for your connection (required) |
# host= This is the SAP-DB host for your connection (optional) |
33 |
# dbname= This is the SAP-DB database to use (required) |
# dbname= This is the SAP-DB database to use (required) |
34 |
# |
# timeout= Command timeout in seconds (optional) |
35 |
|
# isolation= Isolation level (options) |
36 |
|
# sqlmode= INTERNAl or ORACLE (optional) |
37 |
|
# sqlsubmode= ODBC or empty (optional) |
38 |
|
# |
39 |
|
|
40 |
|
_exampleConfig = """ |
41 |
|
# This connection uses the SAP DB driver |
42 |
|
# We will be connecting to the SAP DB server on |
43 |
|
# "localhost" to a database called "TST". |
44 |
|
[sapdb] |
45 |
|
comment = XYZ Development Database |
46 |
|
provider = sapdb |
47 |
|
dbname = TST |
48 |
|
# host = localhost # (optional) |
49 |
|
# sqlmode = INTERNAL # (default) or ORACLE |
50 |
|
# sqlsubmode = ODBC # (for compatibility with the SAP DB ODBC driver) |
51 |
|
# timeout = 900 # (command timeout in seconds) |
52 |
|
# isolation = 1 # 0, 1 (default), 10, 15, 2, 20, 3, 30 |
53 |
|
""" |
54 |
#### THIS IS AN UNTESTED DRIVER #### |
#### THIS IS AN UNTESTED DRIVER #### |
55 |
#### Any volunteers? #### |
#### Any volunteers? #### |
56 |
|
|
57 |
|
import string |
58 |
from string import lower |
from string import lower |
59 |
import sys |
import sys |
60 |
from gnue.common import GDebug, GDataObjects, GConnections |
from gnue.common import GDebug, GDataObjects, GConnections |
68 |
raise GConnections.AdapterNotInstalled, \ |
raise GConnections.AdapterNotInstalled, \ |
69 |
_("Driver not installed: sapdbapi for SAP-DB 7.x \n[%s]") % message |
_("Driver not installed: sapdbapi for SAP-DB 7.x \n[%s]") % message |
70 |
|
|
|
|
|
71 |
class SAP_RecordSet(DBSIG_RecordSet): |
class SAP_RecordSet(DBSIG_RecordSet): |
72 |
pass |
pass |
73 |
|
|
90 |
def connect(self, connectData={}): |
def connect(self, connectData={}): |
91 |
GDebug.printMesg(1,"SAP database driver initializing") |
GDebug.printMesg(1,"SAP database driver initializing") |
92 |
try: |
try: |
93 |
self._dataConnection = SIG2api.connect( \ |
options = {'autocommit': 'off'} |
94 |
user=connectData['_username'], \ |
for gnueName, sapdbName in [('sqlmode', 'sqlmode'), |
95 |
password=connectData['_password'], \ |
('timeout', 'timeout'), |
96 |
database=connectData['dbname'], \ |
('isolation', 'isolation'), |
97 |
host=connectData['host'], \ |
('sqlsubmode', 'component')]: |
98 |
autocommit="off") |
if connectData.has_key (gnueName): |
99 |
|
options [sapdbName] = connectData [gnueName] |
100 |
|
self._dataConnection = apply (SIG2api.connect, |
101 |
|
(connectData['_username'], connectData['_password'], |
102 |
|
connectData['dbname'], connectData.get ('host', '')), |
103 |
|
options) |
104 |
|
#self._dataConnection = SIG2api.connect( \ |
105 |
|
# user=connectData['_username'], \ |
106 |
|
# password=connectData['_password'], \ |
107 |
|
# database=connectData['dbname'], \ |
108 |
|
# host=connectData.get ('host', ''), \ |
109 |
|
# autocommit="off") |
110 |
except self._DatabaseError, value: |
except self._DatabaseError, value: |
111 |
raise GDataObjects.LoginError, value |
raise GDataObjects.LoginError, value |
112 |
|
|
133 |
|
|
134 |
where_user = "" |
where_user = "" |
135 |
if type == None: |
if type == None: |
136 |
where_type = '' |
where_type = "where TYPE <> 'SYSTEM' and TYPE <> 'SYNONYM' " |
137 |
else: |
else: |
138 |
where_type = "where TYPE='%s'" % string.upper(type) |
where_type = "where TYPE='%s'" % string.upper(type) |
139 |
|
|
175 |
statement = \ |
statement = \ |
176 |
"select owner||'.'||tablename||'.'||type, " + \ |
"select owner||'.'||tablename||'.'||type, " + \ |
177 |
"owner||'.'||tablename table_name, " + \ |
"owner||'.'||tablename table_name, " + \ |
178 |
"type table_type " + \ |
"type table_type, " + \ |
179 |
"from domain.tables where owner='%s'%s" \ |
"owner, tablename " + \ |
180 |
|
"from domain.tables where tablename='%s'%s" \ |
181 |
% (name, schema) + \ |
% (name, schema) + \ |
182 |
"order by tablename " |
"order by tablename " |
183 |
|
|
189 |
list = [] |
list = [] |
190 |
for rs in cursor.fetchall(): |
for rs in cursor.fetchall(): |
191 |
list.append(GDataObjects.Schema(attrs={'id':string.lower(rs[0]), 'name':rs[1], |
list.append(GDataObjects.Schema(attrs={'id':string.lower(rs[0]), 'name':rs[1], |
192 |
'type':rs[2]}, |
'type':rs[2], 'sapdbId': (rs [3], rs [4])}, |
193 |
getChildSchema=self.__getFieldSchema)) |
getChildSchema=self.__getFieldSchema)) |
194 |
|
|
195 |
cursor.close() |
cursor.close() |
208 |
# TODO: it will always report such as TEXT-like fields. |
# TODO: it will always report such as TEXT-like fields. |
209 |
|
|
210 |
schema, name, type = string.split(parent.id,'.') |
schema, name, type = string.split(parent.id,'.') |
211 |
|
owner, basename = parent.sapdbId |
212 |
cursor = self._dataConnection.cursor() |
cursor = self._dataConnection.cursor() |
213 |
|
|
214 |
# if type == 'synonym': |
# if type == 'synonym': |
225 |
statement = \ |
statement = \ |
226 |
"select owner||'.'||tablename||'.'||columnname, " + \ |
"select owner||'.'||tablename||'.'||columnname, " + \ |
227 |
"columnname, datatype, 'Y', len, dec " + \ |
"columnname, datatype, 'Y', len, dec " + \ |
228 |
"from domain.columns" + \ |
"from domain.columns " + \ |
229 |
"where owner = '%s' and tablename = '%s' " % (schema, name) + \ |
"where owner = '%s' and tablename = '%s' " % (owner, basename) + \ |
230 |
"order by pos" |
'order by "POS"' |
231 |
|
|
232 |
GDebug.printMesg(5,statement) |
GDebug.printMesg(5,statement) |
233 |
|
|
301 |
###################################### |
###################################### |
302 |
|
|
303 |
# |
# |
304 |
# All datasouce "types" and corresponding DataObject class |
# All datasource "types" and corresponding DataObject class |
305 |
# |
# |
306 |
supportedDataObjects = { |
supportedDataObjects = { |
307 |
'object': SAP_DataObject_Object, |
'object': SAP_DataObject_Object, |
308 |
'sql': SAP_DataObject_SQL |
'sql': SAP_DataObject_SQL |
309 |
} |
} |
310 |
|
|
311 |
|
def createConnection (conn, **overrides): |
312 |
|
from gnue.common.GConnections import GConnections |
313 |
|
connections = GConnections (r'D:\Python22\etc\connections.conf') |
314 |
|
parameters = connections.getConnectionParameters (conn).copy () |
315 |
|
dataObject = connections.getDataObject (conn, 'object') |
316 |
|
parameters.update (overrides) |
317 |
|
dataObject.connect (parameters) |
318 |
|
return dataObject |
319 |
|
|
320 |
|
def testConnection (): |
321 |
|
conn, user, pwd = sys.argv [1:4] |
322 |
|
connection = createConnection (conn, _username = user, _password = pwd) |
323 |
|
connection.getSchemaList () |
324 |
|
|
325 |
|
if __name__ == "__main__": |
326 |
|
testConnection () |