def check(is_auto_set=True, is_reference=True, is_value_default=False, **kargs): namespace = model(is_auto_set=is_auto_set, is_reference=is_reference, is_value_default=is_value_default, **kargs) SAdb.config.getopt() sa = SAdb() sa.open(recreate=True) b = sa.bind(namespace, builder=orm.Builder, fieldtypemap=fieldtypemap, base_klas=Base) C = namespace['C'] c = C() if not is_value_default: c.code = 'alabala' if is_reference: Name = namespace['Name'] if not is_auto_set: c.ime = Name() c.ime.nime = 'aa' else: c.ime = 'aa' s = sa.session() s.save(c) s.flush() cexp = str(c) s.expire(c) c = None s.close() s = sa.session() r = C.code == 'alabala' # print C.code res = s.query(C).filter(C.code == 'alabala').all() if _debug: print res assert len(res) == 1, str(res) + ', case: ' + namespace['case'] r = str(res[0]) assert r == cexp, '\n'.join([namespace['case'], r, '!=', cexp]) if is_reference: res = s.query(Name).one() assert res.nime == 'aa' sa.destroy(True)
def reset(me, namespace, counterBase=None, recreate=True, get_config=False): me.sa = SAdb() me.sa.open(recreate=True) import sqlalchemy fieldtypemap = { Number: dict(type=sqlalchemy.Integer, ), Text: dict(type=sqlalchemy.String(30), ), } me.sa.bind(namespace, base_klas=_Base, builder=o2r.Builder, fieldtypemap=fieldtypemap) me.session = me.sa.session() if counterBase: counterBase.fill(*me.sa.klasi.iterkeys())
def check( is_auto_set =True, is_reference =True, is_value_default =False, **kargs): namespace = model( is_auto_set =is_auto_set, is_reference =is_reference, is_value_default =is_value_default, **kargs) SAdb.config.getopt() sa = SAdb() sa.open( recreate=True) b = sa.bind( namespace, builder= orm.Builder, fieldtypemap= fieldtypemap, base_klas= Base ) C = namespace[ 'C'] c = C() if not is_value_default: c.code = 'alabala' if is_reference: Name = namespace['Name'] if not is_auto_set: c.ime = Name() c.ime.nime = 'aa' else: c.ime = 'aa' s = sa.session() s.save( c) s.flush() cexp = str( c) s.expire( c) c = None s.close() s = sa.session() r = C.code == 'alabala' # print C.code res = s.query( C).filter( C.code == 'alabala' ).all() if _debug: print res assert len(res) == 1, str(res) +', case: '+ namespace['case'] r = str(res[0]) assert r==cexp, '\n'.join( [ namespace['case'], r, '!=', cexp] ) if is_reference: res = s.query( Name).one() assert res.nime == 'aa' sa.destroy( True)
from dbcook.usage.samanager import SAdb SAdb.config.getopt() class A( Base): oid = Int() time = Int() z = Txt() from sqlalchemy import * fieldtypemap = { Txt: dict( type= String(100), ), Int: dict( type= Integer, ), } sa = SAdb()# echo=True) print sa.config sa.open( recreate=True) types = locals() b = sa.bind( types, builder= o2r.Builder, fieldtypemap= fieldtypemap, base_klas= Base ) all = [ A( oid=1, time=12, z='a12-last'), A( oid=1, time=2, z='a2' ), A( oid=1, time=6, z='a6' ), A( oid=2, time=2, z='b1' ), A( oid=2, time=4, z='b-last' ), A( oid=3, time=5, z='c5 time-dup' ), A( oid=3, time=5, z='c-last' ),
#fixers = { B_Fea: B_Fea.fixer, } def fixer( o): f = getattr( o, 'fixer', None) if callable(f): f() return True if 0: for klas,func in fixers.iteritems(): if isinstance( o, klas): func(o) return True from dbcook.usage.samanager import SAdb, sqlalchemy SAdb.Builder = o2r.Builder SAdb.config.getopt() sadb = SAdb() fieldtypemap = { Text: dict( type= sqlalchemy.String(100), ), Int: dict( type= sqlalchemy.Integer, ), } sadb.open( recreate=True) sadb.bind( locals(), fieldtypemap, base_klas=Base )#, debug='mapper') def populate(): a = A() a.name = 'ala' b1 = B() b1.name = 'bala' a.children.append( b1)
fieldtypemap = { Text: dict(type=sqlalchemy.String(100)), } def str1(me): return reflector.obj2str(me, Base, builder.column4ID.name) Base.__repr__ = Base.__str__ = str1 from svd_util.attr import setattr_kargs Base.__init__ = setattr_kargs from dbcook.usage.samanager import SAdb SAdb.Builder = Builder SAdb.config.getopt() sa = SAdb() sa.open() sa.bind(locals(), fieldtypemap) population = populate() session = sa.session() sa.saveall(session, population) session.flush() session.close() for klas in [Employee, Engineer, Manager, Hacker, Dept]: for q in [ sa.query_ALL_instances, sa.query_BASE_instances, sa.query_SUB_instances ]: print '====', klas, q.__name__
fieldtypemap = { Text: dict( type= sqlalchemy.String(100)), } def str1( me): return reflector.obj2str( me, Base, builder.column4ID.name) Base.__repr__ = Base.__str__ = str1 from svd_util.attr import setattr_kargs Base.__init__ = setattr_kargs from dbcook.usage.samanager import SAdb SAdb.Builder = Builder SAdb.config.getopt() sa = SAdb() sa.open() sa.bind( locals(), fieldtypemap ) population = populate() session = sa.session() sa.saveall( session, population) session.flush() session.close() for klas in [Employee, Engineer, Manager, Hacker, Dept]: for q in [ sa.query_ALL_instances, sa.query_BASE_instances, sa.query_SUB_instances]: print '====', klas, q.__name__ #r = session.query( klas) r = q( session, klas ) #.all() if not r: print r
class Dept( Base): manager = o2r.Reference( Manager) name = Text() director = o2r.Reference( 'Director') #employees = Collection/one2many(???) TODO ####### endof model-definition import sqlalchemy fieldtypemap = { Text: dict( type= sqlalchemy.String(100), ), Int : dict( type= sqlalchemy.Integer, ), } sa = SAdb() sa.open( recreate=True) types = locals() b = sa.bind( types, builder= o2r.Builder, fieldtypemap= fieldtypemap, base_klas= Base ) #if b.generator: # print '====generated SA set-up' # print b.generator.out # print '========= eo generated SA set-up' def populate(): a = Employee() a.name = 'anna' a.age = 30
fmap = fieldtypemapper.FieldTypeMapper( typemap_upper, typemap_lower) saw = sa2static from dbcook.usage.samanager import SAdb if 'model' in sys.argv: class Model( Entity): DBCOOK_no_mapping = True pass else: Model=Entity class Name( Element): name = TextD() class Darzhava( Model): #by ISO 3166 name = saw.Type4Reference( Name) s = SAdb( 'sqlite') s.open( recreate=False) #from dbcook.usage.sa_hack4repeatability import hack4repeat #hack4repeat() s.bind( locals(), fieldtypemap=fmap, builder=saw.Builder, base_klas= Element, force_ordered=True) #s.query_all_tables() session = s.session() else: from sqlalchemy import * from sqlalchemy import Text as String from sqlalchemy.orm import * meta = MetaData( 'sqlite:///proba1.db') meta.bind.echo= 'echo' in sys.argv))
a.name = 'pesho' try: a.enum = 'aaaaaaa' except ValueError: pass else: raise ValueError, 'A.enum allows random values' a.enum = 'c' a.a_int = 344 a.a_flt = 5.67 a.a_bool = True return locals() return locals() namespace = test_types() SAdb.config.getopt() sa = SAdb() sa.open() sa.bind( namespace, fieldtype_mapper, builder=Builder, base_klas= Base, ) A = namespace[ 'A'] populate = namespace.get( 'populate', None) if populate: populate_namespace = populate() session = sa.session() sa.saveall( session, populate_namespace) session.flush() session = sa.session() query = session.query( A) print '\n'.join( str(r) for r in query ) r = session.query( A).first()