/
Driver.py
179 lines (152 loc) · 7.32 KB
/
Driver.py
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
#!
# -*- coding: utf-8 -*-
"""
╔════════════════════════════════════════════════════════════════════════════════════╗
║ ║
║ Copyright (c) 2020 https://prrvchr.github.io ║
║ ║
║ Permission is hereby granted, free of charge, to any person obtaining ║
║ a copy of this software and associated documentation files (the "Software"), ║
║ to deal in the Software without restriction, including without limitation ║
║ the rights to use, copy, modify, merge, publish, distribute, sublicense, ║
║ and/or sell copies of the Software, and to permit persons to whom the Software ║
║ is furnished to do so, subject to the following conditions: ║
║ ║
║ The above copyright notice and this permission notice shall be included in ║
║ all copies or substantial portions of the Software. ║
║ ║
║ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ║
║ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES ║
║ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ║
║ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ║
║ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ║
║ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE ║
║ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ║
║ ║
╚════════════════════════════════════════════════════════════════════════════════════╝
"""
import uno
import unohelper
from com.sun.star.lang import XServiceInfo
from com.sun.star.logging.LogLevel import INFO
from com.sun.star.logging.LogLevel import SEVERE
from com.sun.star.sdbc import SQLException
from com.sun.star.sdbc import XDriver
from com.sun.star.sdbcx import XCreateCatalog
from com.sun.star.sdbcx import XDataDefinitionSupplier
from com.sun.star.sdbcx import XDropCatalog
from vcard import getDataSource
from vcard import getDriverPropertyInfos
from vcard import getLogger
from vcard import getLogException
from vcard import getUrl
from vcard import g_identifier
from vcard import g_protocol
from vcard import g_scheme
from vcard import g_defaultlog
import traceback
# pythonloader looks for a static g_ImplementationHelper variable
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationName = f'{g_identifier}.Driver'
class Driver(unohelper.Base,
XCreateCatalog,
XDataDefinitionSupplier,
XDriver,
XDropCatalog,
XServiceInfo):
def __init__(self, ctx):
self._ctx = ctx
self._supportedProtocol = g_protocol
self._logger = getLogger(ctx, g_defaultlog)
self._logger.logprb(INFO, 'Driver', '__init__()', 1101)
__datasource = None
@property
def DataSource(self):
if Driver.__datasource is None:
Driver.__datasource = self._getDataSource()
return Driver.__datasource
# XCreateCatalog
def createCatalog(self, info):
pass
# XDataDefinitionSupplier
def getDataDefinitionByConnection(self, connection):
return connection
def getDataDefinitionByURL(self, url, infos):
connection = self.connect(url, infos)
return connection
# XDriver
def connect(self, url, infos):
cls, mtd = 'Driver', 'connect()'
try:
self._logger.logprb(INFO, cls, mtd, 1111, url)
protocols = url.strip().split(':')
if len(protocols) < 4 or not all(protocols):
raise getLogException(self._logger, self, 1000, 1112, cls, mtd, url)
location = ':'.join(protocols[3:]).strip('/')
scheme, server = self._getUrlParts(location)
if not server:
raise getLogException(self._logger, self, 1000, 1112, cls, mtd, url)
username, password = self._getUserCredential(infos)
if not username or not password:
raise getLogException(self._logger, self, 1001, 1113, cls, mtd)
connection = self.DataSource.getConnection(self, scheme, server, username, password)
version = self.DataSource.DataBase.Version
name = connection.getMetaData().getUserName()
self._logger.logprb(INFO, cls, mtd, 1115, version, name)
return connection
except SQLException as e:
raise e
except Exception as e:
self._logger.logprb(SEVERE, cls, mtd, 1116, e, traceback.format_exc())
def acceptsURL(self, url):
accept = url.startswith(self._supportedProtocol)
return accept
def getPropertyInfo(self, url, infos):
properties = ()
if self.acceptsURL(url):
properties = getDriverPropertyInfos()
return properties
def getMajorVersion(self):
return 1
def getMinorVersion(self):
return 0
# XDropCatalog
def dropCatalog(self, name, info):
pass
# XServiceInfo
def supportsService(self, service):
return g_ImplementationHelper.supportsService(g_ImplementationName, service)
def getImplementationName(self):
return g_ImplementationName
def getSupportedServiceNames(self):
return g_ImplementationHelper.getSupportedServiceNames(g_ImplementationName)
# Private getter methods
def _getDataSource(self):
cls, mtd = 'Driver', '_getDataSource()'
return getDataSource(self._ctx, self._logger, self, cls, mtd)
def _getUrlParts(self, location):
url = getUrl(self._ctx, location, g_scheme)
if url is None:
cls, mtd = 'Driver', '_getUrlParts()'
raise getLogException(self._logger, self, 1000, 1131, cls, mtd, location)
scheme = url.Protocol
server = url.Server
if not location.startswith(scheme):
scheme = g_scheme
return scheme, server
def _getUserCredential(self, infos):
user = ''
pwd = ''
for info in infos:
if info.Name == 'user':
user = info.Value.strip()
elif info.Name == 'password':
pwd = info.Value.strip()
if user and pwd:
break
return user, pwd
g_ImplementationHelper.addImplementation(Driver,
g_ImplementationName,
(g_ImplementationName,
'com.sun.star.sdbc.Driver',
'com.sun.star.sdbcx.Driver'))