def __init__(self,category,name=None): if name is None: if TESTING: name = "MOAT_TEST" else: name = "MOAT" if TESTING: def trace(a,b,c): log(TRACE,a,b,c) else: trace = None self.db = Db(name, trace=trace) self.category = " ".join(Name(category)).encode("utf-8") db=self.db() try: db.Do("CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER AUTO_INCREMENT PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: try: db.Do("CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: pass self.running = True
async def run_test(x,ai): dbp = Db("db"+str(x),config="test.ini") try: await dbp.Do("drop table if exists test1") except NoData: pass await dbp.Do("""\ create table test1 ( id integer primary key not null %s, a varchar(255) not null default '', b varchar(255) null ) """ % (ai,), _empty=True) async with dbp() as db: A = await db.Do("insert into test1(a,b) values (${a},${b})", a="one",b="OneOne") B = await db.Do("insert into test1(a) values (${a})", a="two") C = await db.Do("insert into test1(b) values (${b})", b="three") async with dbp() as db: async def chk(db): a,b = await db.DoFn("select a,b from test1 where id=${id}", id=A) assert a=="one"; assert b=="OneOne" a,b = await db.DoFn("select a,b from test1 where id=${id}", id=B) assert a=="two"; assert b is None a,b = await db.DoFn("select a,b from test1 where id=${id}", id=C) assert a==""; assert b=="three" await chk(db) await db.Do("delete from test1 where id=${id}", id=B) await db.rollback() async with dbp() as db: n=0; j=A async for i, in db.DoSelect("select id from test1 order by id"): n += 1 assert i==j, (i,j) j += 1 assert n == 3 try: await db.DoFn("select id from test1") except ManyData: pass else: assert False,"Need to raise ManyData" try: async for i, in db.DoSelect("select id from test1 where id < 0"): assert False,"Returned nonsense" except NoData: pass else: assert False,"Need to raise NoData" async for i, in db.DoSelect("select id from test1 where id < 0", _empty=True): assert False,"Returned nonsense" print("Success.")
def __init__(self, category, name=None): if name is None: if TESTING: name = "MOAT_TEST" else: name = "MOAT" if TESTING: def trace(a, b, c): log(TRACE, a, b, c) else: trace = None self.db = Db(name, trace=trace) self.category = " ".join(Name(category)).encode("utf-8") db = self.db() try: db.Do( "CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER AUTO_INCREMENT PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: try: db.Do( "CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: pass self.running = True
class DbStore(object): """This object implements a simple Deferred-enabled storage""" running = False def __init__(self,category,name=None): if name is None: if TESTING: name = "MOAT_TEST" else: name = "MOAT" if TESTING: def trace(a,b,c): log(TRACE,a,b,c) else: trace = None self.db = Db(name, trace=trace) self.category = " ".join(Name(category)).encode("utf-8") db=self.db() try: db.Do("CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER AUTO_INCREMENT PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: try: db.Do("CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: pass self.running = True def close(self): self.db.close() self.db = None def get(self, key): key = " ".join(Name(key)).encode("utf-8") with self.db() as db: try: r, = db.DoFn("select value from HE_State where category=${cat} and name=${name}", cat=self.category, name=key) except NoData: raise KeyError((self.category,key)) else: r = eval(r,SafeNames,{}) return r def all(self, callback): with self.db() as db: return db.DoSelect("select name,value from HE_State where category=${cat}", cat=self.category, callback=callback) def delete(self, key): key = " ".join(Name(key)).encode("utf-8") with self.db() as db: try: db.Do("delete from HE_State where category=${cat} and name=${name}", cat=self.category,name=key) except NoData: raise KeyError((self.category,key)) def clear(self): with self.db() as db: return db.Do("delete from HE_State where category=${cat}", cat=self.category, _empty=1) def set(self, key, val): key = " ".join(Name(key)).encode("utf-8") with self.db() as db: r = db.Do("update HE_State set value=${val} where category=${cat} and name=${name}", cat=self.category,name=key,val=repr(val), _empty=1) if r == 0: db.Do("insert into HE_State (category,name,value) VALUES(${cat},${name},${val})", cat=self.category,name=key,val=repr(val))
def run_test(x, ai, *a): db = Db("db" + str(x), config="test.ini") for c in a: db.Do(c, _empty=True) db.Do("""\ create table test1 ( id integer primary key not null %s, a varchar(255) not null default '', b varchar(255) null ) """ % (ai, ), _empty=True) db.commit() A = db.Do("insert into test1(a,b) values (${a},${b})", a="one", b="OneOne") B = db.Do("insert into test1(a) values (${a})", a="two") C = db.Do("insert into test1(b) values (${b})", b="three") db.commit() def chk(db): a, b = db.DoFn("select a,b from test1 where id=${id}", id=A) assert a == "one" assert b == "OneOne" a, b = db.DoFn("select a,b from test1 where id=${id}", id=B) assert a == "two" assert b is None a, b = db.DoFn("select a,b from test1 where id=${id}", id=C) assert a == "" assert b == "three" chk(db) db.Do("delete from test1 where id=${id}", id=B) db.rollback() n = 0 j = A for i, in db.DoSelect("select id from test1 order by id"): n += 1 assert i == j j += 1 assert n == 3 try: db.DoFn("select id from test1") except ManyData: pass else: assert False, "Need to raise ManyData" try: for i, in db.DoSelect("select id from test1 where id < 0"): assert False, "Returned nonsense" except NoData: pass else: assert False, "Need to raise NoData" for i, in db.DoSelect("select id from test1 where id < 0", _empty=True): assert False, "Returned nonsense" print("Success.")
def run_test(x, ai, *a): db = Db("db" + str(x), config="test.ini") for c in a: db.Do(c, _empty=True) db.Do( """\ create table test1 ( id integer primary key not null %s, a varchar(255) not null default '', b varchar(255) null ) """ % (ai,), _empty=True, ) db.commit() A = db.Do("insert into test1(a,b) values (${a},${b})", a="one", b="OneOne") B = db.Do("insert into test1(a) values (${a})", a="two") C = db.Do("insert into test1(b) values (${b})", b="three") db.commit() def chk(db): a, b = db.DoFn("select a,b from test1 where id=${id}", id=A) assert a == "one" assert b == "OneOne" a, b = db.DoFn("select a,b from test1 where id=${id}", id=B) assert a == "two" assert b is None a, b = db.DoFn("select a,b from test1 where id=${id}", id=C) assert a == "" assert b == "three" chk(db) db.Do("delete from test1 where id=${id}", id=B) db.rollback() n = 0 j = A for (i,) in db.DoSelect("select id from test1 order by id"): n += 1 assert i == j j += 1 assert n == 3 try: db.DoFn("select id from test1") except ManyData: pass else: assert False, "Need to raise ManyData" try: for (i,) in db.DoSelect("select id from test1 where id < 0"): assert False, "Returned nonsense" except NoData: pass else: assert False, "Need to raise NoData" for (i,) in db.DoSelect("select id from test1 where id < 0", _empty=True): assert False, "Returned nonsense" print("Success.")
class DbStore(object): """This object implements a simple Deferred-enabled storage""" running = False def __init__(self, category, name=None): if name is None: if TESTING: name = "MOAT_TEST" else: name = "MOAT" if TESTING: def trace(a, b, c): log(TRACE, a, b, c) else: trace = None self.db = Db(name, trace=trace) self.category = " ".join(Name(category)).encode("utf-8") db = self.db() try: db.Do( "CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER AUTO_INCREMENT PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: try: db.Do( "CREATE TABLE HE_State (" " category varchar(50)," " name varchar(200)," " value BLOB," " id INTEGER PRIMARY KEY," " UNIQUE (category,name))", _empty=1) except Exception: pass self.running = True def close(self): self.db.close() self.db = None def get(self, key): key = " ".join(Name(key)).encode("utf-8") with self.db() as db: try: r, = db.DoFn( "select value from HE_State where category=${cat} and name=${name}", cat=self.category, name=key) except NoData: raise KeyError((self.category, key)) else: r = eval(r, SafeNames, {}) return r def all(self, callback): with self.db() as db: return db.DoSelect( "select name,value from HE_State where category=${cat}", cat=self.category, callback=callback) def delete(self, key): key = " ".join(Name(key)).encode("utf-8") with self.db() as db: try: db.Do( "delete from HE_State where category=${cat} and name=${name}", cat=self.category, name=key) except NoData: raise KeyError((self.category, key)) def clear(self): with self.db() as db: return db.Do("delete from HE_State where category=${cat}", cat=self.category, _empty=1) def set(self, key, val): key = " ".join(Name(key)).encode("utf-8") with self.db() as db: r = db.Do( "update HE_State set value=${val} where category=${cat} and name=${name}", cat=self.category, name=key, val=repr(val), _empty=1) if r == 0: db.Do( "insert into HE_State (category,name,value) VALUES(${cat},${name},${val})", cat=self.category, name=key, val=repr(val))
async def setup(self): self.db = Db(**self.root.cfg['config']['sql']['data_logger']['server'])