async def _make_connection(): try: self.connection = AsyncDB('pg', dsn=default_dsn) await self.connection.connection() except Exception as err: print(err) raise Exception(err)
async def test_connect(driver, params, event_loop): db = AsyncDB(driver, params=params, loop=event_loop) await db.connection() print('CONNECTED: ', db.is_connected() is True) result, error = await db.test_connection() print(result, error) print(type(result) == list) db.use('library') # set database to work # # making a simple query: # result, error = await db.query('SELECT * from events') # print(error) # for row in result: # print(row) # creation and insertion: await db.execute("CREATE TABLE tests (id int, name text, PRIMARY KEY(id))") ql = "INSERT INTO tests (id, name) VALUES(?, ?)" # prepare the statement: prepared, error = await db.prepare(ql) print(": prepare the statement: ", prepared) print(": Executing Insert of many entries: ") result, error = await db.execute(prepared, (2, "def")) result, error = await db.execute(prepared, (3, "ghi")) result, error = await db.execute(prepared, (4, "jkl")) examples = [(5, "mno"), (6, "pqr"), (7, "stu")] for example in examples: result, error = await db.execute(prepared, example) print(result, error) result, error = await db.query("SELECT * FROM tests") for row in result: print(row) await db.close()
def get_connection(self, dsn: str = None): """ Getting the database connection and driver based on parameters """ driver = self.Meta.driver if self.Meta.driver else "pg" if driver: # logging.debug('Getting data from Database: {}'.format(driver)) # working with app labels try: app = self.Meta.app_label if self.Meta.app_label else None except AttributeError: app = None if app: # TODO: get formula to got app database list # db = DATABASES[app] db = {} try: params = { "user": db["USER"], "password": db["PASSWORD"], "host": db["HOST"], "port": db["PORT"], "database": db["NAME"], } if "SCHEMA" in db: params["schema"] = db["SCHEMA"] except KeyError: pass elif hasattr(self.Meta, "credentials"): params = self.Meta.credentials self.Meta.connection = AsyncDB(driver, params=params) elif dsn is not None: # print('HERE ', dsn) self.Meta.connection = AsyncDB(driver, dsn=dsn) return self.Meta.connection
async def test_pool_by_params(driver, event_loop): db = AsyncDB(driver, params=params, loop=event_loop) assert db.is_connected() is False await db.connection() pytest.assume(db.is_connected() is True) await db.close() assert db.is_closed() is True
async def test_connect(driver, event_loop): db = AsyncDB(driver, params=params, loop=event_loop) await db.connection() pytest.assume(db.is_connected() is True) result, error = await db.test_connection() pytest.assume(type(result) == list) await db.close()
async def test_connect(driver, event_loop): db = AsyncDB(driver, params=params, loop=event_loop) await db.connection() pytest.assume(db.is_connected() is True) result, error = await db.test_connection('bigtest') pytest.assume(not error) assert result == 'bigtest' await db.close()
async def test_connect(driver, params, event_loop): db = AsyncDB(driver, params=params, loop=event_loop) await db.connection() print('CONNECTED: ', db.is_connected() is True) result, error = await db.test_connection() print(result, error) print(type(result) == list) await db.close()
def adb(): try: db = AsyncDB('postgres', dsn=asyncpg_url) if db: db.connect() return asyncORM(db=db) except Exception as err: raise Exception(err)
def create_problem_data(proid): db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) cur = db.cursor() sqlstr = ('INSERT INTO "PMOD_TEST_MODE" ("proid", "modeid", ' '"content", "testmodeid") VALUES (%s, %s, %s, %s);') sqlarr = (proid, 1, '', None) cur.execute(sqlstr, sqlarr)
async def test_connect(driver, event_loop): db = AsyncDB(driver, params=PARAMS, loop=event_loop) await db.connection() pytest.assume(db.is_connected() is True) result, error = await db.test_connection() pytest.assume(type(result) == dict) pytest.assume(result['version'] == '4.4.2') await db.close() pytest.assume(db.is_connected() is False)
async def test_connect(driver, event_loop): db = AsyncDB(driver, params=PARAMS, loop=event_loop) await db.connection() pytest.assume(db.is_connected() is True) result, error = await db.test_connection() pytest.assume(type(result) == list) row = result[0] pytest.assume(row['release_version'] == '3.11.8') await db.close() pytest.assume(db.is_connected() is False)
def __init__(self,mod_idendesc,get_link_fn): self.DATATYPE_CHALLENGE = 1 self.DATATYPE_STATUS = 2 self.DATASTSTUS_PEND = 1 self.DATASTSTUS_WAIT = 2 self.DATASTSTUS_DONE = 3 self._idendesc = mod_idendesc self.get_link = get_link_fn self.db = AsyncDB(config.CORE_DBNAME,config.CORE_DBUSER, config.CORE_DBPASSWORD) self.collector_namemap = {}
async def test_pool_connect(event_loop): db = AsyncDB(DRIVER, params=params, loop=event_loop) pytest.assume(db.is_connected() is False) await db.connection() pytest.assume(db.is_connected() == True) result, error = await db.execute("SELECT 1") print(result) pytest.assume(result == 'SELECT 1') result, error = await db.test_connection() print(result, error) row = result[0] pytest.assume(row[0] == 1) await db.close() assert (not db.get_connection())
def __init__(self, mod_idendesc, get_link_fn, proid): self._proid = proid self._idendesc = mod_idendesc self.get_link = get_link_fn self._proinfo = mod.ProblemMg.get_problem_info_by_proid(self._proid) self._accessid = mod.ProblemMg.get_accessid_by_proid(self._proid) self.db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) self._reg_path = 'pro/' + str(self._proid) + '/' Proxy.instance.register_call( self._reg_path, 'view', self.view) Proxy.instance.register_call( self._reg_path, 'add_mode', self.add_mode) Proxy.instance.register_call( self._reg_path, 'del_mode', self.del_mode) Proxy.instance.register_call( self._reg_path, 'set_mode', self.set_mode) Proxy.instance.register_call( self._reg_path, 'get_mode', self.get_mode) Proxy.instance.register_call( self._reg_path, 'list_mode', self.list_mode) Proxy.instance.register_call( self._reg_path, 'add_testmode', self.add_testmode) Proxy.instance.register_call( self._reg_path, 'del_testmode', self.del_testmode) Proxy.instance.register_call( self._reg_path, 'set_testmode', self.set_testmode) Proxy.instance.register_call( self._reg_path, 'get_testmode', self.get_testmode) Proxy.instance.register_call( self._reg_path, 'list_testmode', self.list_testmode) Proxy.instance.register_call( self._reg_path, 'create_testdata', self.create_testdata) Proxy.instance.register_call( self._reg_path, 'delete_testdata', self.delete_testdata) Proxy.instance.register_call( self._reg_path, 'get_testdata', self.get_testdata) Proxy.instance.register_call( self._reg_path, 'list_testdata', self.list_testdata) Proxy.instance.register_call( self._reg_path, 'set_testdata', self.set_testdata) Proxy.instance.register_call( self._reg_path, 'set_testmode_testdata', self.set_testdata) Proxy.instance.register_call( self._reg_path, 'get_testmode_testdata', self.get_testdata)
def delete_problem_data(proid): db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) cur = db.cursor() sqlstr = ('DELETE FROM "PMOD_TEST_MODE" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr) sqlstr = ('DELETE FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr) sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr)
def __init__(self, mod_idendesc, get_link_fn, sqid): self._sqid = sqid self._idendesc = mod_idendesc self.get_link = get_link_fn self._accessid = mod.SquareMg.get_accessid_by_sqid(self._sqid) self.db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) self._reg_path = 'sq/' + str(self._sqid) + '/' Proxy.instance.register_call(self._reg_path, 'list_jurank', self.list_jurank) Proxy.instance.register_call(self._reg_path, 'update_result', self.update_result)
def __init__(self, pubkey, privkey=None): super().__init__() self.set_verifykey(pubkey) if privkey != None: self.set_signkey(privkey) TOJAuth.instance = self TOJAuth.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, config.CORE_DBPASSWORD)
async def test_huge_query(event_loop): sql = 'SELECT * FROM trocplaces.stores' check_conn = None db = AsyncDB(DRIVER, params=params, loop=event_loop) async with await db.connection() as conn: result, error = await conn.execute("SET TIMEZONE TO 'America/New_York'") pytest.assume(not error) result, error = await conn.query(sql) pytest.assume(not error) pytest.assume(result is not None) check_conn = conn pytest.assume(check_conn is not None)
def __init__(self, dsn="", session: AbstractSession = None, loop=None): if loop: self._loop = loop else: self._loop = asyncio.get_event_loop() self._result = {} self._session_key = "" self._session_id = None self._loop.set_exception_handler(nav_exception_handler) # TODO: define other session backend self._cache = redis = AsyncDB("redis", dsn=dsn) self._session = session
def __init__(self, mod_idendesc, get_link_fn): TestdataMg.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, config.CORE_DBPASSWORD) TestdataMg._idendesc = mod_idendesc self.get_link = get_link_fn Proxy.instance.register_call('core/testdata/', 'add_testdata', self.add_testdata) Proxy.instance.register_call('core/testdata/', 'update_testdata', self.update_testdata) Proxy.instance.register_call('core/testdata/', 'get_testdata', self.get_testdata) Proxy.instance.register_call('core/testdata/', 'del_testdata', self.del_testdata)
def __init__(self, mod_idendesc, get_link_fn): Notice.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, config.CORE_DBPASSWORD) Notice._idendesc = mod_idendesc self.get_link = get_link_fn Proxy.instance.register_call('core/notice/', 'list_notice', self.list_notice) Proxy.instance.register_call('core/notice/', 'read_notice', self.read_notice) Proxy.instance.register_call('core/notice/', 'del_notice', self.del_notice) Proxy.instance.register_call('core/notice/', 'get_unseen_count', self.get_unseen_count)
def __init__(self, mod_idendesc, get_link_fn, sqid): self._sqid = sqid; self._idendesc = mod_idendesc self.get_link = get_link_fn self._accessid = mod.SquareMg.get_accessid_by_sqid(self._sqid) self.db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) self._reg_path = 'sq/' + str(self._sqid) + '/' Proxy.instance.register_call( self._reg_path,'list_jurank',self.list_jurank) Proxy.instance.register_call( self._reg_path,'update_result',self.update_result)
def __init__(self, mod_idendesc, get_link_fn): Mail.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, config.CORE_DBPASSWORD) Mail._idendesc = mod_idendesc self.get_link = get_link_fn Proxy.instance.register_call( 'core/mail/', 'send_mail', self.send_mail) Proxy.instance.register_call( 'core/mail/', 'recv_mail', self.recv_mail) Proxy.instance.register_call( 'core/mail/', 'list_mail', self.list_mail) Proxy.instance.register_call( 'core/mail/', 'del_mail', self.del_mail) Proxy.instance.register_call( 'core/mail/', 'get_mail_count', self.get_mail_count)
def __init__(self, mod_idendesc, get_link_fn): UserMg.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, config.CORE_DBPASSWORD) UserMg._idendesc = mod_idendesc self.get_link = get_link_fn Proxy.instance.register_call( 'core/user/', 'register', self.register) Proxy.instance.register_call( 'core/user/', 'login', self.login) Proxy.instance.register_call( 'core/user/', 'cookie_login', self.cookie_login) Proxy.instance.register_call( 'core/user/', 'get_user_info', self.get_user_info) Proxy.instance.register_call( 'core/user/', 'set_user_info', self.set_user_info) Proxy.instance.register_call( 'core/user/', 'change_user_password', self.change_user_password) Proxy.instance.register_call( 'core/user/', 'list_auth', self.list_auth)
def __init__(self, mod_idendesc, get_link_fn): ProblemMg.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER, config.CORE_DBPASSWORD) ProblemMg._idendesc = mod_idendesc self.get_link = get_link_fn self._pmod_list = {} Proxy.instance.register_filter('pro/', self.pmod_filter) Proxy.instance.register_call('core/problem/', 'create_problem', self.create_problem) Proxy.instance.register_call('core/problem/', 'delete_problem', self.delete_problem) Proxy.instance.register_call('core/problem/', 'set_problem', self.imc_set_problem) Proxy.instance.register_call('core/problem/', 'list_problem', self.list_problem) Proxy.instance.register_call('core/problem/', 'get_problem_info', self.get_problem_info) Proxy.instance.register_call('core/problem/', 'list_pmod', self.list_pmod)
class pmod_test(Problem): _pmod_name = 'pmod_test' def __init__(self, mod_idendesc, get_link_fn, proid): self._proid = proid self._idendesc = mod_idendesc self.get_link = get_link_fn self._proinfo = mod.ProblemMg.get_problem_info_by_proid(self._proid) self._accessid = mod.ProblemMg.get_accessid_by_proid(self._proid) self.db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) self._reg_path = 'pro/' + str(self._proid) + '/' Proxy.instance.register_call( self._reg_path, 'view', self.view) Proxy.instance.register_call( self._reg_path, 'add_mode', self.add_mode) Proxy.instance.register_call( self._reg_path, 'del_mode', self.del_mode) Proxy.instance.register_call( self._reg_path, 'set_mode', self.set_mode) Proxy.instance.register_call( self._reg_path, 'get_mode', self.get_mode) Proxy.instance.register_call( self._reg_path, 'list_mode', self.list_mode) Proxy.instance.register_call( self._reg_path, 'add_testmode', self.add_testmode) Proxy.instance.register_call( self._reg_path, 'del_testmode', self.del_testmode) Proxy.instance.register_call( self._reg_path, 'set_testmode', self.set_testmode) Proxy.instance.register_call( self._reg_path, 'get_testmode', self.get_testmode) Proxy.instance.register_call( self._reg_path, 'list_testmode', self.list_testmode) Proxy.instance.register_call( self._reg_path, 'create_testdata', self.create_testdata) Proxy.instance.register_call( self._reg_path, 'delete_testdata', self.delete_testdata) Proxy.instance.register_call( self._reg_path, 'get_testdata', self.get_testdata) Proxy.instance.register_call( self._reg_path, 'list_testdata', self.list_testdata) Proxy.instance.register_call( self._reg_path, 'set_testdata', self.set_testdata) Proxy.instance.register_call( self._reg_path, 'set_testmode_testdata', self.set_testdata) Proxy.instance.register_call( self._reg_path, 'get_testmode_testdata', self.get_testdata) def unload(self, force): Proxy.instance.unregister_call( self._reg_path, 'view') Proxy.instance.unregister_call( self._reg_path, 'add_mode') Proxy.instance.unregister_call( self._reg_path, 'del_mode') Proxy.instance.unregister_call( self._reg_path, 'set_mode') Proxy.instance.unregister_call( self._reg_path, 'get_mode') Proxy.instance.unregister_call( self._reg_path, 'list_mode') Proxy.instance.unregister_call( self._reg_path, 'add_testmode') Proxy.instance.unregister_call( self._reg_path, 'del_testmode') Proxy.instance.unregister_call( self._reg_path, 'set_testmode') Proxy.instance.unregister_call( self._reg_path, 'get_testmode') Proxy.instance.unregister_call( self._reg_path, 'create_testdata') Proxy.instance.unregister_call( self._reg_path, 'delete_testdata') Proxy.instance.unregister_call( self._reg_path, 'get_testdata') Proxy.instance.unregister_call( self._reg_path, 'list_testdata') Proxy.instance.unregister_call( self._reg_path, 'set_testdata') Proxy.instance.unregister_call( self._reg_path, 'set_testmode_testdata') Proxy.instance.unregister_call( self._reg_path, 'get_testmode_testdata') @staticmethod @TOJAuth.check_access(mod.ProblemMg._accessid, TOJAuth.ACCESS_CREATE) def create_problem_data(proid): db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) cur = db.cursor() sqlstr = ('INSERT INTO "PMOD_TEST_MODE" ("proid", "modeid", ' '"content", "testmodeid") VALUES (%s, %s, %s, %s);') sqlarr = (proid, 1, '', None) cur.execute(sqlstr, sqlarr) @staticmethod @TOJAuth.check_access(mod.ProblemMg._accessid, TOJAuth.ACCESS_DELETE) def delete_problem_data(proid): db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) cur = db.cursor() sqlstr = ('DELETE FROM "PMOD_TEST_MODE" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr) sqlstr = ('DELETE FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr) sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s;') sqlarr = (proid, ) cur.execute(sqlstr, sqlarr) @imc.async.caller def view(self): with TOJAuth.change_current_iden(self._idendesc): mode = self._get_mode_by_modeid(1) if mode == None: return 'Emodeid' testmode = self._get_testmode_info(mode['testmodeid']) ret = { 'content':mode['content'], 'timelimit':testmode['timelimit'], 'memlimit':testmode['memlimit'] } return ret @imc.async.caller def add_mode(self, content, testmodeid): if( (content != None and type(content) != str) or (testmodeid != None and type(testmodeid) != int) ): return 'Eparameter' if testmodeid != None and not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' self._add_mode(None, content, testmodeid) return 'Success' def _add_mode(self, modeid, content, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqltab = ('INSERT INTO "PMOD_TEST_MODE" (') sqlcol = ('"proid", "content", "testmodeid"') sqlval = (') VALUES (%s, %s, %s') sqlend = (');') sqlarr = [self._proid, content, testmodeid] if modeid != None: sqlcol = sqlcol + ', "modeid"' sqlval = sqlval + ', %s' sqlarr.append(modeid) sqlstr = sqltab + sqlcol + sqlval + sqlend cur.execute(sqlstr, sqlarr) @imc.async.caller def del_mode(self, modeid): if( type(modeid) != int ): return 'Eparameter' if modeid == 1 or not self._does_modeid_exist(modeid): return 'Emodeid' self._del_mode(modeid) return 'Success' def _del_mode(self, modeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('DELETE FROM "PMOD_TEST_MODE" WHERE "proid" = %s AND ' '"modeid" = %s;') sqlarr = (self._proid, modeid) cur.execute(sqlstr, sqlarr) @imc.async.caller def set_mode(self, modeid, content, testmodeid): if( type(modeid) != int or (content != None and type(content) != str) or (testmodeid != None and type(testmodeid) != int) ): return 'Eparameter' if not self._does_modeid_exist(modeid): return 'Emodeid' if testmodeid != None and not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' self._set_mode(modeid, content, testmodeid) return 'Success' def _set_mode(self, modeid, content, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PMOD_TEST_MODE" SET "content" = %s, ' '"testmodeid" = %s WHERE "proid" = %s AND "modeid" = %s;') sqlarr = (content, testmodeid, self._proid, modeid) cur.execute(sqlstr, sqlarr) @imc.async.caller def get_mode(self, modeid): if( type(modeid) != int ): return 'Eparameter' mode = self._get_mode_by_modeid(modeid) if mode == None: return 'Emodeid' return mode @imc.async.caller def list_mode(self): mode_list = self._list_mode() return mode_list def _list_mode(self): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "modeid", "testmodeid" FROM "PMOD_TEST_MODE" ' 'WHERE "proid" = %s ORDER BY "modeid" ASC;') sqlarr = (self._proid, ) cur.execute(sqlstr, sqlarr) mode_list = [] for data in cur: obj = {} obj['modeid'] = data[0] obj['testmodeid'] = data[1] mode_list.append(obj) return mode_list @imc.async.caller def add_testmode(self, testmodename, timelimit, memlimit): if( type(testmodename) != str or (timelimit != None and type(timelimit) != int) or (memlimit != None and type(memlimit) != int) ): return 'Eparameter' if timelimit != None and timelimit < 0: return 'Etimelimit' if memlimit != None and memlimit < 0: return 'Ememlimit' self._add_testmode(testmodename, timelimit, memlimit) return 'Success' def _add_testmode(self, testmodename, timelimit, memlimit): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('INSERT INTO "PMOD_TEST_TESTMODE" ("proid", "testmodename", ' '"timelimit", "memlimit") VALUES (%s, %s, %s, %s);') sqlarr = (self._proid, testmodename, timelimit, memlimit) cur.execute(sqlstr, sqlarr) @imc.async.caller def del_testmode(self, testmodeid): if( type(testmodeid) != int ): return 'Eparameter' if not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' self._del_testmode(testmodeid) return 'Success' def _del_testmode(self, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('DELETE FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) @imc.async.caller def set_testmode(self, testmodeid, testmodename, timelimit, memlimit): if( type(testmodeid) != int or type(testmodename) != str or (timelimit != None and type(timelimit) != int) or (memlimit != None and type(memlimit) != int) ): return 'Eparameter' if not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' if timelimit != None and timelimit < 0: return 'Etimelimit' if memlimit != None and memlimit < 0: return 'Ememlimit' self._set_testmode(testmodeid, testmodename, timelimit, memlimit) return 'Success' def _set_testmode(self, testmodeid, testmodename, timelimit, memlimit): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('UPDATE "PMOD_TEST_TESTMODE" SET "testmodename" = %s, ' '"timelimit" = %s, "memlimit" = %s WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (testmodename, timelimit, memlimit, self._proid, testmodeid) cur.execute(sqlstr, sqlarr) @imc.async.caller def get_testmode(self, testmodeid): if( type(testmodeid) != int ): return 'Eparameter' if not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' testmode = self._get_testmode(testmodeid) return testmode def _get_testmode(self, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "testmodeid", "testmodename", "timelimit", ' '"memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) testmode = None for data in cur: testmode = {} testmode['testmodeid'] = data[0] testmode['testmodename'] = data[1] testmode['timelimit'] = data[2] testmode['memlimit'] = data[3] return testmode @imc.async.caller def list_testmode(self): testmode_list = self._list_testmode() return testmode_list def _list_testmode(self): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "testmodeid", "testmodename", "timelimit", ' '"memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s ' 'ORDER BY "testmodeid" ASC;') sqlarr = (self._proid, ) cur.execute(sqlstr, sqlarr) testmode_list = [] for data in cur: obj = {} obj['testmodeid'] = data[0] obj['testmodename'] = data[1] obj['timelimit'] = data[2] obj['memlimit'] = data[3] testmode_list.append(obj) return testmode_list @imc.async.caller def create_testdata(self, info, filekey, expire = None): if expire != None: expire = com.isoptime(expire) if expire == None: return 'Eparameter' if( type(info) != str or type(filekey) != str ): return 'Eparameter' testid = self._create_testdata(info, filekey, expire) if testid == None: return 'Eupload' return {'testid': testid} def _create_testdata(self, info, filekey, expire): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) # Upload file blobname = 'TEST_BLOBNAME' with TOJAuth.change_current_iden(self._idendesc): testid = mod.TestdataMg.instance._add_testdata( blobname, expire, self._proid, info) return testid @imc.async.caller def delete_testdata(self, testid): if( type(testid) != int ): return 'Eparameter' with TOJAuth.change_current_iden(self._idendesc): test = mod.TestdataMg.instance._get_testdata(testid) if test == None: return 'Etestid' if test['proid'] != self._proid: return 'Eother_proid' self._delete_testdata(testid) return 'Success' def _delete_testdata(self, testid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_DELETE) with TOJAuth.change_current_iden(self._idendesc): mod.TestdataMg.instance._del_testdata(testid) @imc.async.caller def get_testdata(self, testid): if( type(testid) != int ): return 'Eparameter' TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) with TOJAuth.change_current_iden(self._idendesc): test = mod.TestdataMg.instance._get_testdata(testid) if test == None: return 'Etestid' if test['proid'] != self._proid: return 'Eother_proid' del test['blobname'] del test['proid'] return test @imc.async.caller def list_testdata(self): testdata_list = self._list_testdata() return testdata_list def _list_testdata(self): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) with TOJAuth.change_current_iden(self._idendesc): testdata_list = mod.TestdataMg.instance._list_testdata(self._proid) for test in testdata_list: del test['blobname'] del test['proid'] return testdata_list @imc.async.caller def set_testdata(self, testid, info, filekey = None, expire = None): if expire != None: expire = com.isoptime(expire) if expire == None: return 'Eparameter' if( type(testid) != int or type(info) != str or (filekey != None and type(filekey) != str) ): return 'Eparameter' with TOJAuth.change_current_iden(self._idendesc): test = mod.TestdataMg.instance._get_testdata(testid) if test == None: return 'Etestid' if test['proid'] != self._proid: return 'Eother_proid' result = self._set_testdata(testid, info, filekey, expire) if result == None: return 'Efailed' if result == False: return 'Eupload' return 'Success' def _set_testdata(self, testid, info, filekey, expire): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) with TOJAuth.change_current_iden(self._idendesc): test = mod.TestdataMg.instance._get_testdata(testid) blobname = test['blobname'] if test['proid'] != self._proid: return None if filekey != None: # Upload file # Update blob 'blobname' # If failed return False with TOJAuth.change_current_iden(self._idendesc): mod.TestdataMg.instance._update_testdata( testid, blobname, expire, self._proid, info) return True @imc.async.caller def set_testmode_testdata(self, testmodeid, testdata): if( type(testmodeid) != int or type(testdata) != list ): return 'Eparameter' for test in testdata: if type(test) != list: return 'Eparameter' if( 'testid' not in test or type(test['testid']) != int or 'timelimit' not in test or type(test['timelimit']) != int or 'memlimit' not in test or type(test['memlimit']) != int or 'subtask' not in test or type(test['subtask']) != int ): return 'Eparameter' if test['timelimit'] != None and test['timelimit'] < 0: return 'Etimelimit' if test['memlimit'] != None and test['memlimit'] < 0: return 'Ememlimit' if not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' self._set_testdata(testmodeid, testdata) return 'Success' def _set_testmode_testdata(self, testmodeid, testdata): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s AND ' '"testmodeid" = %s;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) if len(testdata) == 0: return sqltab = ('INSERT INTO "PMOD_TEST_TESTDATA" ("proid", "testmodeid", ' '"order", "testid", "timelimit", "memlimit", "subtask") ' 'VALUES') sqlval = () sqlend = (';') sqlarr = [] cnt = 0 for test in testdata: if cnt == 0: sqlval = sqlval + ' ' else: sqlval = sqlval + ', ' cnt = cnt + 1 sqlval = sqlval + '(%s, %s, %s, %s, %s, %s, %s)' sqlarr.append(self._proid) sqlarr.append(testmodeid) sqlarr.append(cnt) sqlarr.append(test['testid']) sqlarr.append(test['timelimit']) sqlarr.append(test['memlimit']) sqlarr.append(test['subtask']) sqlstr = sqltab + sqlval + sqlend cur.execute(sqlstr, sqlarr) @imc.async.caller def get_testmode_testdata(self, testmodeid): if( type(testmodeid) != int ): return 'Eparameter' if not self._does_testmodeid_exist(testmodeid): return 'Etestmodeid' testdata = self._get_testdata(testmodeid) return testdata def _get_testmode_testdata(self, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "order", "testid", "timelimit", "memlimit", ' '"subtask" FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s AND ' '"testmodeid" = %s ORDER BY "order" ASC;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) testdata = [] for data in cur: obj = {} obj['order'] = data[0] obj['testid'] = data[1] obj['timelimit'] = data[2] obj['memlimit'] = data[3] obj['subtask'] = data[4] testdata.append(obj) return testdata def _does_modeid_exist(self, modeid): mode = self._get_mode_by_modeid(modeid) return mode != None def _get_mode_by_modeid(self, modeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "proid", "modeid", "content", "testmodeid" FROM ' '"PMOD_TEST_MODE" WHERE "proid" = %s AND "modeid" = %s;') sqlarr = (self._proid, modeid) cur.execute(sqlstr, sqlarr) mode = None for data in cur: mode = {} mode['proid'] = data[0] mode['modeid'] = data[1] mode['content'] = data[2] mode['testmodeid'] = data[3] return mode def _does_testmodeid_exist(self, testmodeid): testmode_info = self._get_testmode_info(testmodeid) return testmode_info != None def _get_testmode_info(self, testmodeid): TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE) cur = self.db.cursor() sqlstr = ('SELECT "proid", "testmodeid", "testmodename", "timelimit",' ' "memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s ' 'AND "testmodeid" = %s;') sqlarr = (self._proid, testmodeid) cur.execute(sqlstr, sqlarr) testmode_info = None for data in cur: testmode_info = {} testmode_info['proid'] = data[0] testmode_info['testmodeid'] = data[1] testmode_info['testmodename'] = data[2] testmode_info['timelimit'] = data[3] testmode_info['memlimit'] = data[4] return testmode_info
"host": "localhost", "port": "3306", "database": "test", "DEBUG": True, } # With mysqlPool pool = mysqlPool(loop=loop, params=params) loop.run_until_complete(pool.connect()) db = loop.run_until_complete(pool.acquire()) conn = db.get_connection() loop.run_until_complete(pool.release()) pool.terminate() # With AsyncDB pool = AsyncDB("mysql", loop=loop, params=params) conn = loop.run_until_complete(pool.connection()) cursor = loop.run_until_complete(pool.cursor()) result, error = loop.run_until_complete(pool.execute("TRUNCATE TABLE color")) print("-- execute", result) data = [ ("Red", "#ff0000"), ("Green", "#00ff00"), ("Blue", "#0000ff"), ("Cyan", "#00ffff"), ("Magenta", "#ff00ff"), ("Yellow", "#ffff00"), ("Black", "#000000"), ("White", "#ffffff"),
async def conn(event_loop): db = AsyncDB('pg', dsn=asyncpg_url, loop=event_loop) await db.connection() yield db await db.close()
class sqmod_test(Square): _sqmod_name = 'sqmod_test' def __init__(self, mod_idendesc, get_link_fn, sqid): self._sqid = sqid; self._idendesc = mod_idendesc self.get_link = get_link_fn self._accessid = mod.SquareMg.get_accessid_by_sqid(self._sqid) self.db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, config.MOD_DBPASSWORD) self._reg_path = 'sq/' + str(self._sqid) + '/' Proxy.instance.register_call( self._reg_path,'list_jurank',self.list_jurank) Proxy.instance.register_call( self._reg_path,'update_result',self.update_result) def unload(self): pass def join_square(self,uid): return mod.SquareMg.JOIN_ACCEPT def quit_square(self,uid): pass @staticmethod def create_square_data(): pass @staticmethod def delete_square_data(): pass @imc.async.caller def list_jurank(self): #TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_READ) cur = self.db.cursor(); cur.execute('SELECT "name","song","score","maxcombo" FROM "SQMOD_TEST_JURANK" ORDER BY "score" DESC') ret = [] for data in cur: ret.append({ 'name':data[0], 'song':data[1], 'score':data[2], 'maxcombo':data[3], }) return ret @imc.async.caller def update_result(self,name,song,score,maxcombo): #TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_READ) cur = self.db.cursor(); cur.execute('SELECT "score" FROM "SQMOD_TEST_JURANK" WHERE "name"=%s AND "song"=%s', (name,song)) if cur.rowcount == 1: if cur.fetchone()[0] > score: return 'Success' cur.upsert('SQMOD_TEST_JURANK', {'name':name,'song':song}, {'score':score,'maxcombo':maxcombo}) client_links = self.get_link('client') subpath = 'sq/' + str(self._sqid) + '/' for link in client_links: Proxy.instance.call_async( link + subpath, 'update_jurank', 10000, None) return 'Success'
async def test_pool_by_params(driver, event_loop): db = AsyncDB(driver, params=params, loop=event_loop) assert db.is_connected() is False
async def conn(event_loop): db = AsyncDB(DRIVER, params=params, loop=event_loop) await db.connection() yield db await db.close()
("SVO", "Moscow", "Russia"), ] airports = "INSERT INTO airports VALUES(?, ?, ?)" await conn.executemany(airports, data) a_country = "United States" a_city = "Moscow" query = "SELECT * FROM airports WHERE country=? OR city=?" async with await conn.fetch(query, (a_country, a_city)) as result: async for row in result: print(row) # using prepare print('Using Cursor Objects: ') b_country = 'France' b_city = 'London' async with conn.cursor(query, (b_country, b_city)) as cursor: print("using iterator: ") async for row in cursor: print(row) # its an iterable print("Using Context Manager: ") async with cursor: print(await cursor.fetchall()) # this returns a cursor based object if __name__ == "__main__": loop = asyncio.get_event_loop() loop.set_exception_handler(default_exception_handler) driver = AsyncDB("sqlite", params={"database": ":memory:"}, loop=loop) asyncio.run(connect(driver))