3
3
from sqlalchemy import create_engine
4
4
from sqlalchemy .orm import sessionmaker
5
5
6
- from .ODM2 .models import Variables as Variable2 , change_schema
6
+ from .ODM2 .models import Variables as Variable2 , setSchema
7
7
#from .versionSwitcher import ODM, refreshDB #import Variable as Variable1
8
8
from .ODM1_1_1 .services import ODM #, refreshDB
9
9
import urllib
10
10
import sys
11
+ import os
11
12
12
13
13
14
LIBSPATIALITE_PATH = './libspatialite.so.5.1.0'
14
15
15
16
class SessionFactory ():
16
17
def __init__ (self , connection_string , echo , version = 1.1 ):
17
18
if 'sqlite' in connection_string :
19
+ # from sqlite3 import dbapi2 as sqlite
20
+ # put the spatialite dll on the path. If one had pyspatialite installed thn
21
+ # this would not be required... but trying to get that on a windows machine
22
+ # was way too hard to bother
23
+ # dirDLLspatialite = 'C:/bin'
24
+ # os.environ['PATH'] = dirDLLspatialite + ';' + os.environ['PATH']
25
+
26
+ # #engine = create_engine('sqlite:///D:\\temp\\test_1.db', module=sqlite, echo=False)
27
+ # engine = create_engine(connection_string, module=sqlite, echo=False)
28
+
29
+ # this enables the extension on each connection
30
+ # @event.listens_for(engine, "connect")
31
+ # def connect(dbapi_connection, connection_rec):
32
+ # dbapi_connection.enable_load_extension(True)
33
+ # dbapi_connection.execute("SELECT load_extension('libspatialite-4.dll')")
18
34
19
35
#from pysqlite2 import dbapi2 as sqlite
20
36
#import pyspatialite.dpabi as sqlite
@@ -63,21 +79,19 @@ def createConnection(self, engine, address, db=None, user=None, password=None, d
63
79
if engine == 'sqlite' :
64
80
connection_string = engine + ':///' + address
65
81
s = SessionFactory (connection_string , echo = False , version = dbtype )
66
- self . _setSchema (s .engine )
82
+ setSchema (s .engine )
67
83
return s
68
84
69
85
else :
70
86
connection_string = dbconnection .buildConnDict (dbconnection (), engine , address , db , user , password )
71
87
if self .isValidConnection (connection_string , dbtype ):
72
88
s = SessionFactory (connection_string , echo = False , version = dbtype )
73
- self . _setSchema (s .engine )
89
+ setSchema (s .engine )
74
90
return s
75
91
else :
76
92
return None
77
93
# if self.testConnection(connection_string):
78
94
79
-
80
-
81
95
@classmethod
82
96
def isValidConnection (self , connection_string , dbtype = 2.0 ):
83
97
#refreshDB(dbtype)
@@ -95,29 +109,11 @@ def isValidConnection(self, connection_string, dbtype=2.0):
95
109
else :
96
110
return False
97
111
98
- @staticmethod
99
- def _getSchema (engine ):
100
- from sqlalchemy .engine import reflection
101
-
102
- insp = reflection .Inspector .from_engine (engine )
103
-
104
- for name in insp .get_schema_names ():
105
- if 'odm2' == name .lower ():
106
- return name
107
- else :
108
- return insp .default_schema_name
109
-
110
- @classmethod
111
- def _setSchema (self , engine ):
112
-
113
- s = self ._getSchema (engine )
114
- change_schema (s )
115
-
116
112
@classmethod
117
113
def testEngine (self , connection_string ):
118
114
s = SessionFactory (connection_string , echo = False )
119
115
try :
120
- self . _setSchema (s .test_engine )
116
+ setSchema (s .test_engine )
121
117
s .test_Session ().query (Variable2 .VariableCode ).limit (1 ).first ()
122
118
123
119
except Exception as e :
@@ -164,7 +160,6 @@ def addConnection(self, conn_dict):
164
160
self ._current_connection = self ._connections [- 1 ]
165
161
166
162
167
-
168
163
def deleteConnection (self , conn_dict ):
169
164
self ._connections [:] = [x for x in self ._connections if x != conn_dict ]
170
165
0 commit comments