Package daklib :: Package database :: Module base
[hide private]
[frames] | no frames]

Source Code for Module daklib.database.base

 1  # This program is free software; you can redistribute it and/or modify 
 2  # it under the terms of the GNU General Public License as published by 
 3  # the Free Software Foundation; either version 2 of the License, or 
 4  # (at your option) any later version. 
 5   
 6  # This program is distributed in the hope that it will be useful, 
 7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 9  # GNU General Public License for more details. 
10   
11  # You should have received a copy of the GNU General Public License 
12  # along with this program; if not, write to the Free Software 
13  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
14   
15  ################################################################################ 
16   
17  from sqlalchemy import Column, DateTime 
18  from sqlalchemy.event import listen 
19  from sqlalchemy.ext.declarative import declarative_base 
20  from sqlalchemy.schema import DDL, Table 
21  from sqlalchemy.sql import func 
22   
23  Base = declarative_base() 
24 25 26 -class BaseMethods(Base):
27 __abstract__ = True 28 29 @classmethod
30 - def get(cls, primary_key, session):
31 """ 32 This is a support function that allows getting an object by its primary 33 key. 34 35 Architecture.get(3[, session]) 36 37 instead of the more verbose 38 39 session.query(Architecture).get(3) 40 """ 41 return session.query(cls).get(primary_key)
42
43 44 -class BaseTimestamp(BaseMethods):
45 __abstract__ = True 46 47 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 48 modified = Column( 49 DateTime(timezone=True), nullable=False, server_default=func.now() 50 ) 51 52 modified_trigger_function = DDL( 53 """ 54 CREATE OR REPLACE FUNCTION tfunc_set_modified() RETURNS trigger 55 LANGUAGE plpgsql 56 AS $$ 57 BEGIN NEW.modified = now(); return NEW; END; 58 $$ 59 """ 60 ) 61 62 modified_trigger = DDL( 63 """ 64 CREATE TRIGGER %(table)s_modified BEFORE UPDATE ON %(fullname)s 65 FOR EACH ROW EXECUTE PROCEDURE tfunc_set_modified() 66 """ 67 ) 68 69 @classmethod
70 - def __table_cls__(cls, *arg, **kw):
71 table = Table(*arg, **kw) 72 listen( 73 table, 74 "after_create", 75 cls.modified_trigger_function.execute_if(dialect="postgresql"), 76 ) 77 listen( 78 table, 79 "after_create", 80 cls.modified_trigger.execute_if(dialect="postgresql"), 81 ) 82 return table
83