merge master again (2.0-10 changelog only)
[sfa.git] / sfa / storage / alchemy.py
index dff6151..7e00116 100644 (file)
@@ -1,25 +1,23 @@
-from sqlalchemy import create_engine
+from types import StringTypes
 
 
+from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
 from sqlalchemy.orm import sessionmaker
-Session=sessionmaker ()
-session=Session(bind=engine)
-#session.configure(bind=engine)
 
 
-from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy import Column, Integer, String
 from sqlalchemy.orm import relationship, backref
 from sqlalchemy import ForeignKey
 
 from sqlalchemy import Column, Integer, String
 from sqlalchemy.orm import relationship, backref
 from sqlalchemy import ForeignKey
 
-from sfa.util.sfalogger import logger
+from sfa.util.sfalogging import logger
 
 
-Base=declarative_base()
+# this module is designed to be loaded when the configured db server is reachable
+# OTOH model can be loaded from anywhere including the client-side
 
 
-class DB:
+class Alchemy:
 
     def __init__ (self, config):
         dbname="sfa"
         # will be created lazily on-demand
 
     def __init__ (self, config):
         dbname="sfa"
         # will be created lazily on-demand
-        self.session = None
+        self._session = None
         # the former PostgreSQL.py used the psycopg2 directly and was doing
         #self.connection.set_client_encoding("UNICODE")
         # it's unclear how to achieve this in sqlalchemy, nor if it's needed at all
         # the former PostgreSQL.py used the psycopg2 directly and was doing
         #self.connection.set_client_encoding("UNICODE")
         # it's unclear how to achieve this in sqlalchemy, nor if it's needed at all
@@ -27,15 +25,16 @@ class DB:
         # we indeed have /var/lib/pgsql/data/postgresql.conf where
         # this setting is unset, it might be an angle to tweak that if need be
         # try a unix socket first - omitting the hostname does the trick
         # we indeed have /var/lib/pgsql/data/postgresql.conf where
         # this setting is unset, it might be an angle to tweak that if need be
         # try a unix socket first - omitting the hostname does the trick
-        unix_desc = "postgresql+psycopg2://%s:%s@:%s/%s"%\
+        unix_url = "postgresql+psycopg2://%s:%s@:%s/%s"%\
             (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_PORT,dbname)
         # the TCP fallback method
             (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_PORT,dbname)
         # the TCP fallback method
-        tcp_desc = "postgresql+psycopg2://%s:%s@%s:%s/%s"%\
+        tcp_url = "postgresql+psycopg2://%s:%s@%s:%s/%s"%\
             (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_HOST,config.SFA_DB_PORT,dbname)
             (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_HOST,config.SFA_DB_PORT,dbname)
-        for desc in [ unix_desc, tcp_desc ] :
+        for url in [ unix_url, tcp_url ] :
             try:
             try:
-                self.engine = create_engine (engine_desc)
+                self.engine = create_engine (url)
                 self.check()
                 self.check()
+                self.url=url
                 return
             except:
                 pass
                 return
             except:
                 pass
@@ -50,14 +49,6 @@ class DB:
     def check (self):
         self.engine.execute ("select 1").scalar()
 
     def check (self):
         self.engine.execute ("select 1").scalar()
 
-    # create schema
-    def create_schema (self):
-        return Base.metadata.create_all(self.engine)
-
-    # does a complete wipe of the schema, use with care
-    def drop_schema (self):
-        return Base.metadata.drop_all(self.engine)
-
     def session (self):
         if self._session is None:
             Session=sessionmaker ()
     def session (self):
         if self._session is None:
             Session=sessionmaker ()
@@ -69,19 +60,10 @@ class DB:
         self._session.close()
         self._session=None
 
         self._session.close()
         self._session=None
 
-    def commit (self):
-        self.session().commit()
-            
-    def insert (self, stuff, commit=False):
-        if isinstance (stuff,list):
-            self.session().add_all(stuff)
-        else:
-            self.session().add(obj)
+####################
+from sfa.util.config import Config
 
 
-    # for compat with the previous PostgreSQL stuff
-    def update (self, record):
-        self.commit()
+alchemy=Alchemy (Config())
+engine=alchemy.engine
+dbsession=alchemy.session()
 
 
-    def remove (self, record):
-        del record
-        self.commit()