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.
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.
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
15################################################################################
17from sqlalchemy import Column, DateTime
18from sqlalchemy.event import listen
19from sqlalchemy.ext.declarative import declarative_base
20from sqlalchemy.schema import DDL, Table
21from sqlalchemy.sql import func
23Base = declarative_base()
26class BaseMethods(Base):
27 __abstract__ = True
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.
35 Architecture.get(3[, session])
37 instead of the more verbose
39 session.query(Architecture).get(3)
40 """
41 return session.query(cls).get(primary_key)
44class BaseTimestamp(BaseMethods):
45 __abstract__ = True
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 )
52 modified_trigger_function = DDL(
53 """
54CREATE OR REPLACE FUNCTION tfunc_set_modified() RETURNS trigger
55LANGUAGE plpgsql
56AS $$
57 BEGIN NEW.modified = now(); return NEW; END;
58$$
59 """
60 )
62 modified_trigger = DDL(
63 """
64CREATE TRIGGER %(table)s_modified BEFORE UPDATE ON %(fullname)s
65FOR EACH ROW EXECUTE PROCEDURE tfunc_set_modified()
66 """
67 )
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