33from sqlalchemy import create_engine
44from sqlalchemy .orm import sessionmaker
55
6- from .ODM2 .models import Variables as Variable2 , change_schema
6+ from .ODM2 .models import Variables as Variable2 , setSchema
77#from .versionSwitcher import ODM, refreshDB #import Variable as Variable1
88from .ODM1_1_1 .services import ODM #, refreshDB
99import urllib
1010import sys
11+ import os
1112
1213
1314LIBSPATIALITE_PATH = './libspatialite.so.5.1.0'
1415
1516class SessionFactory ():
1617 def __init__ (self , connection_string , echo , version = 1.1 ):
1718 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')")
1834
1935 #from pysqlite2 import dbapi2 as sqlite
2036 #import pyspatialite.dpabi as sqlite
@@ -63,21 +79,19 @@ def createConnection(self, engine, address, db=None, user=None, password=None, d
6379 if engine == 'sqlite' :
6480 connection_string = engine + ':///' + address
6581 s = SessionFactory (connection_string , echo = False , version = dbtype )
66- self . _setSchema (s .engine )
82+ setSchema (s .engine )
6783 return s
6884
6985 else :
7086 connection_string = dbconnection .buildConnDict (dbconnection (), engine , address , db , user , password )
7187 if self .isValidConnection (connection_string , dbtype ):
7288 s = SessionFactory (connection_string , echo = False , version = dbtype )
73- self . _setSchema (s .engine )
89+ setSchema (s .engine )
7490 return s
7591 else :
7692 return None
7793 # if self.testConnection(connection_string):
7894
79-
80-
8195 @classmethod
8296 def isValidConnection (self , connection_string , dbtype = 2.0 ):
8397 #refreshDB(dbtype)
@@ -95,29 +109,11 @@ def isValidConnection(self, connection_string, dbtype=2.0):
95109 else :
96110 return False
97111
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-
116112 @classmethod
117113 def testEngine (self , connection_string ):
118114 s = SessionFactory (connection_string , echo = False )
119115 try :
120- self . _setSchema (s .test_engine )
116+ setSchema (s .test_engine )
121117 s .test_Session ().query (Variable2 .VariableCode ).limit (1 ).first ()
122118
123119 except Exception as e :
@@ -164,7 +160,6 @@ def addConnection(self, conn_dict):
164160 self ._current_connection = self ._connections [- 1 ]
165161
166162
167-
168163 def deleteConnection (self , conn_dict ):
169164 self ._connections [:] = [x for x in self ._connections if x != conn_dict ]
170165
0 commit comments