Skip to content

Commit caa32d9

Browse files
author
Stephanie Reeder
committed
moving location of code to set schema
1 parent 4d698aa commit caa32d9

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/api/ODM2/models.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -2064,12 +2064,31 @@ class TransectResultValues(Base):
20642064
TransectResultObj = relationship(TransectResults)
20652065

20662066

2067-
import inspect
2068-
import sys
20692067

2070-
def change_schema(schema):
2068+
2069+
def _changeSchema(schema):
2070+
import inspect
2071+
import sys
20712072
#get a list of all of the classes in the module
20722073
clsmembers = inspect.getmembers(sys.modules[__name__], lambda member: inspect.isclass(member) and member.__module__ == __name__)
20732074

20742075
for name, Tbl in clsmembers:
2075-
Tbl.__table__.schema = schema
2076+
import sqlalchemy.ext.declarative.api as api
2077+
if isinstance(Tbl, api.DeclarativeMeta):
2078+
Tbl.__table__.schema = schema
2079+
2080+
2081+
def _getSchema(engine):
2082+
from sqlalchemy.engine import reflection
2083+
2084+
insp=reflection.Inspector.from_engine(engine)
2085+
2086+
for name in insp.get_schema_names():
2087+
if 'odm2'== name.lower():
2088+
return name
2089+
else:
2090+
return insp.default_schema_name
2091+
2092+
def setSchema(engine):
2093+
s = _getSchema(engine)
2094+
_changeSchema(s)

src/api/ODMconnection.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,34 @@
33
from sqlalchemy import create_engine
44
from 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
88
from .ODM1_1_1.services import ODM#, refreshDB
99
import urllib
1010
import sys
11+
import os
1112

1213

1314
LIBSPATIALITE_PATH = './libspatialite.so.5.1.0'
1415

1516
class 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

Comments
 (0)