<s> """<STR_LIT>""" <EOL> import collections <EOL> import datetime as dt <EOL> import itertools <EOL> import logging <EOL> import re <EOL> import future <EOL> from future . utils import viewvalues <EOL> import sqlalchemy as sa <EOL> from sqlalchemy import orm <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> ZERODATE = dt . datetime ( dt . MINYEAR , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> Base = declarative_base ( ) <EOL> log = logging . getLogger ( __name__ ) <EOL> class Database ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tables = None ) : <EOL> self . tables = { } <EOL> self . modified = None <EOL> self . reflecting = False <EOL> self . sa_metadata = sa . MetaData ( ) <EOL> if tables is None : <EOL> tables = [ ] <EOL> self . update_tables ( tables ) <EOL> def isempty ( self ) : <EOL> return bool ( self . tables ) <EOL> def update_tables ( self , tables ) : <EOL> """<STR_LIT>""" <EOL> for t in tables : <EOL> self . isempty = False <EOL> self . tables [ t . name ] = t <EOL> if self . modified is None : <EOL> self . modified = t . modified <EOL> self . modified = min ( self . modified , t . modified , <EOL> key = lambda x : '<STR_LIT>' if x is None else x ) <EOL> @ property <EOL> def views ( self ) : <EOL> for t in viewvalues ( self . tables ) : <EOL> if t . isview : <EOL> yield t <EOL> def tablenames ( self ) : <EOL> return list ( self . tables ) <EOL> @ property <EOL> def columns ( self ) : <EOL> for t in viewvalues ( self . tables ) : <EOL> for c in t . columns : <EOL> yield c <EOL> def fieldnames ( self , table = None , dotted = False ) : <EOL> ret = set ( ) <EOL> if table is None : <EOL> for t in viewvalues ( self . tables ) : <EOL> if dotted : <EOL> ret . update ( [ '<STR_LIT>' % ( t . name , c . name ) for c in t . columns ] ) <EOL> else : <EOL> ret . update ( [ c . name for c in t . columns ] ) <EOL> return ret <EOL> if table not in self . tables : <EOL> return set ( ) <EOL> t = self . tables [ table ] <EOL> if dotted : <EOL> return { '<STR_LIT>' % ( t . name , c . name ) for c in t . columns } <EOL> return { c . name for c in t . columns } <EOL> def get_joins ( self , tbl1 , tbl2 ) : <EOL> if tbl1 not in self . tables or tbl2 not in self . tables : <EOL> return set ( ) <EOL> t1 = self . tables [ tbl1 ] <EOL> t2 = self . tables [ tbl2 ] <EOL> joins = set ( ) <EOL> for src , tgt in [ ( t1 , t2 ) , ( t2 , t1 ) ] : <EOL> for c in src . columns : <EOL> if ( c . referenced_column and <EOL> c . referenced_column . table . name == tgt . name ) : <EOL> joins . add ( ForeignKey ( <EOL> src . name , ( c . name , ) , <EOL> tgt . name , ( c . referenced_column . name , ) ) ) <EOL> return joins <EOL> def tables_referencing ( self , tbl ) : <EOL> if tbl not in self . tables : <EOL> return set ( ) <EOL> reftables = set ( ) <EOL> for c in self . tables [ tbl ] . columns : <EOL> reftables . update ( { col . table . name for col in c . referenced_by } ) <EOL> return reftables <EOL> def fields_referencing ( self , tbl , column = None ) : <EOL> if tbl not in self . tables : <EOL> raise StopIteration ( ) <EOL> for c in self . tables [ tbl ] . columns : <EOL> for r in c . referenced_by : <EOL> if column is None or column == r . referenced_column . name : <EOL> yield ForeignKey ( r . table . name , ( r . name , ) , tbl , ( c . name , ) ) <EOL> def foreign_keys ( self , tbl ) : <EOL> if tbl not in self . tables : <EOL> raise StopIteration ( ) <EOL> for c in self . tables [ tbl ] . columns : <EOL> if c . referenced_column : <EOL> yield ForeignKey ( tbl , ( c . name , ) , <EOL> c . referenced_column . table . name , <EOL> ( c . referenced_column . name , ) ) <EOL> def all_joins ( self , tbl ) : <EOL> return itertools . chain ( self . foreign_keys ( tbl ) , <EOL> self . fields_referencing ( tbl ) ) <EOL> def insert_statement ( self , tbl ) : <EOL> if tbl not in self . tables : <EOL> return '<STR_LIT>' <EOL> t = self . tables [ tbl ] <EOL> sql = '<STR_LIT>' <EOL> columns = '<STR_LIT:U+002CU+0020>' . join ( c . name for c in t . columns ) <EOL> defaults = '<STR_LIT:U+002CU+0020>' . join ( sql_default ( c ) for c in t . columns ) <EOL> return sql . format ( table = tbl , columns = columns , defaults = defaults ) <EOL> @ property <EOL> def age ( self ) : <EOL> """<STR_LIT>""" <EOL> return dt . datetime . now ( ) - ( self . modified or ZERODATE ) <EOL> def indexes ( self , tbl ) : <EOL> if tbl not in self . tables : <EOL> raise StopIteration ( ) <EOL> for index in self . tables [ tbl ] . indexes : <EOL> yield index <EOL> fkclass = collections . namedtuple ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> class ForeignKey ( fkclass ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . table , '<STR_LIT:U+002C>' . join ( self . columns ) , <EOL> self . reftable , '<STR_LIT:U+002C>' . join ( self . refcolumns ) ) <EOL> def as_join ( self , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> tables = [ self . reftable , self . table ] <EOL> if reverse : <EOL> tables . reverse ( ) <EOL> tables = tuple ( tables ) <EOL> joinstr = '<STR_LIT>' % tables <EOL> sep = '<STR_LIT>' <EOL> for idx , col in enumerate ( self . columns ) : <EOL> joinstr += sep + '<STR_LIT>' % ( <EOL> self . reftable , self . refcolumns [ idx ] , <EOL> self . table , col ) <EOL> sep = '<STR_LIT>' <EOL> return joinstr <EOL> restr = re . compile ( r'<STR_LIT>' , re . I ) <EOL> renumeric = re . compile ( r'<STR_LIT>' <EOL> '<STR_LIT>' , re . I ) <EOL> redate = re . compile ( r'<STR_LIT>' , re . I ) <EOL> def sql_default ( column ) : <EOL> """<STR_LIT>""" <EOL> if column . default_value : <EOL> return "<STR_LIT>" % column . default_value <EOL> if column . nullable : <EOL> return '<STR_LIT>' <EOL> typ = str ( column . type ) . lower ( ) . strip ( ) <EOL> value = '<STR_LIT>' <EOL> if redate . search ( typ ) : <EOL> log . debug ( '<STR_LIT>' , typ ) <EOL> head = typ . split ( ) [ <NUM_LIT:0> ] <EOL> if head == '<STR_LIT:date>' : <EOL> value = "<STR_LIT>" <EOL> elif head == '<STR_LIT:time>' : <EOL> value = "<STR_LIT>" <EOL> elif head in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> value = "<STR_LIT>" <EOL> elif restr . search ( typ ) : <EOL> log . debug ( '<STR_LIT>' , typ ) <EOL> value = "<STR_LIT>" <EOL> elif renumeric . search ( typ ) : <EOL> log . debug ( '<STR_LIT>' , typ ) <EOL> value = "<STR_LIT:0>" <EOL> else : <EOL> log . debug ( '<STR_LIT>' , typ ) <EOL> return value <EOL> class TimesMixin ( object ) : <EOL> created = sa . Column ( sa . DateTime , default = dt . datetime . now ) <EOL> modified = sa . Column ( sa . DateTime , default = dt . datetime . now , <EOL> onupdate = dt . datetime . now ) <EOL> class Table ( Base , TimesMixin ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> name = sa . Column ( sa . String , index = True , unique = True ) <EOL> isview = sa . Column ( sa . Boolean , default = False , nullable = False ) <EOL> def column ( self , name ) : <EOL> for column in self . columns : <EOL> if column . name == name : <EOL> return column <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % <EOL> ( name , self . name ) ) <EOL> class Column ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> sa . UniqueConstraint ( '<STR_LIT>' , '<STR_LIT:name>' ) , <EOL> ) <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> table_id = sa . Column ( sa . Integer , sa . ForeignKey ( '<STR_LIT>' ) ) <EOL> name = sa . Column ( sa . String , index = True ) <EOL> type = sa . Column ( sa . String ) <EOL> referenced_column_id = sa . Column ( sa . Integer , sa . ForeignKey ( '<STR_LIT>' ) ) <EOL> constraint_name = sa . Column ( sa . String , nullable = True ) <EOL> primary_key = sa . Column ( sa . Boolean ) <EOL> nullable = sa . Column ( sa . Boolean ) <EOL> default_value = sa . Column ( sa . String , nullable = True ) <EOL> table = orm . relationship ( '<STR_LIT>' , backref = '<STR_LIT>' , order_by = name ) <EOL> referenced_column = orm . relationship ( <EOL> '<STR_LIT>' , backref = '<STR_LIT>' , remote_side = [ id ] ) <EOL> index_column_table = sa . Table ( <EOL> '<STR_LIT>' , Base . metadata , <EOL> sa . Column ( '<STR_LIT>' , sa . Integer , sa . ForeignKey ( '<STR_LIT>' ) ) , <EOL> sa . Column ( '<STR_LIT>' , sa . Integer , sa . ForeignKey ( '<STR_LIT>' ) ) ) <EOL> class Index ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> name = sa . Column ( sa . String , index = True ) <EOL> unique = sa . Column ( sa . Boolean ) <EOL> table_id = sa . Column ( sa . Integer , sa . ForeignKey ( '<STR_LIT>' ) ) <EOL> table = orm . relationship ( '<STR_LIT>' , backref = '<STR_LIT>' , order_by = name ) <EOL> columns = orm . relationship ( '<STR_LIT>' , secondary = lambda : index_column_table , <EOL> backref = '<STR_LIT>' ) </s>
