本文整理汇总了Python中psycopg2.pool.ThreadedConnectionPool.execute方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadedConnectionPool.execute方法的具体用法?Python ThreadedConnectionPool.execute怎么用?Python ThreadedConnectionPool.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psycopg2.pool.ThreadedConnectionPool
的用法示例。
在下文中一共展示了ThreadedConnectionPool.execute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PostgresConnection
# 需要导入模块: from psycopg2.pool import ThreadedConnectionPool [as 别名]
# 或者: from psycopg2.pool.ThreadedConnectionPool import execute [as 别名]
class PostgresConnection(DBConnection):
def create_tables(self):
if not self.db.get("select table_name from information_schema.tables where table_schema = 'public' and table_name = 'account'"):
self.db.execute('''create table if not exists account (
account_id bigserial primary key,
password char(48) default null,
user_id varchar(45) not null,
unique (user_id)
);''')
if not self.db.get("select table_name from information_schema.tables where table_schema = 'public' and table_name = 'session'"):
self.db.execute('''create table if not exists session (
session_id char(22) not null primary key,
account_id bigint not null references account (account_id),
expires double precision not null,
state bytea
);''')
self.db.execute('create index session_expires on session using btree (expires);')
def __init__(self, host, port, database, username, password, min_connections=1, max_connections=10):
super(PostgresConnection, self).__init__(*args, **kwargs)
self.db = ThreadedConnectionPool(min_connections, max_connections, database=database, user=username, password=password, host=host, port=port)
self.create_tables()
def _store_session(self, session_id, session_data):
account_id = session_data['account_id']
expires = session_data['expires']
self.db.execute("delete from session where account_id = %s and expires <= %s", (account_id, time()))
self.db.execute("insert into session (account_id, expires, session_id) values (%s, %s, %s)", (account_id, expires, session_id))
def _update_expiry(self, session_id, session_data):
self.db.execute("update session set expires = %s where session_id = %s", (session_data['expires'], session_id))
def _update_password(self, user_id, account, hashed_password):
self.db.execute("update account set password = %s where account_id = %s", (hashed_password, account['account_id']))
def _instantiate_session(self, session_data, session_cache):
return PostgresSession(self.db, session_data, self._session_cache)
def _get_account(self, user_id):
return self.db.get("select account_id, password from account where user_id = %s", (user_id,))
def _store_account(self, user_id, values):
self.db.execute("insert into account (" + ', '.join([k for k in values]) + ") values (" + ','.join(['%s' for k in values]) + ")", [values[k] for k in values])
def _load_uncached_data(self, session_id):
return self.db.get("select session.session_id, session.expires, session.state, account.user_id, account.account_id from session join account on account.account_id = session.account_id where session.session_id = %s and session.expires > %s", (session_id, time()))
def _remove_session(self, session_id):
self.db.execute("delete from session where session_id = %s", (session_id,))
def clear_sessions(self, user_id):
user_id = user_id.lower()
self.db.execute("delete from session using session join account on account.account_id = session.account_id where account.user_id = %s", (user_id,))
示例2: PostgresConnection
# 需要导入模块: from psycopg2.pool import ThreadedConnectionPool [as 别名]
# 或者: from psycopg2.pool.ThreadedConnectionPool import execute [as 别名]
class PostgresConnection(DBConnection):
def create_tables(self):
if not self.db.get("select table_name from information_schema.tables where table_schema = 'public' and table_name = 'account'"):
self.db.execute('''create table if not exists account (
account_id bigserial primary key,
password char(48) default null,
user_id varchar(45) not null,
unique (user_id)
);''')
if not self.db.get("select table_name from information_schema.tables where table_schema = 'public' and table_name = 'session'"):
self.db.execute('''create table if not exists session (
session_id char(22) not null primary key,
account_id bigint not null references account (account_id),
expires double precision not null,
state bytea
);''')
self.db.execute('create index session_expires on session using btree (expires);')
def __init__(self, host, port, database, username, password, session_ttl=24*60*60*365, anon_session_ttl=24*60*60, session_renew=0, anon_session_renew=0, min_connections=1, max_connections=10):
self.db = ThreadedConnectionPool(min_connections, max_connections, database=database, user=username, password=password, host=host, port=port)
self.create_tables()
self.session_ttl = session_ttl
self.anon_session_ttl = anon_session_ttl or self.session_ttl
self.session_renew = session_renew or self.session_ttl
self.anon_session_renew = anon_session_renew or self.anon_session_ttl
def create_account(self, user_id, password, additional_values={}, **values):
if not user_id:
raise TotoException(ERROR_INVALID_USER_ID, "Invalid user ID.")
user_id = user_id.lower()
if self.db.get("select account_id from account where user_id = %s", (user_id,)):
raise TotoException(ERROR_USER_ID_EXISTS, "User ID already in use.")
values.update(additional_values)
values['user_id'] = user_id
values['password'] = secret.password_hash(password)
self.db.execute("insert into account (" + ', '.join([k for k in values]) + ") values (" + ','.join(['%s' for k in values]) + ")", [values[k] for k in values])
def _load_uncached_data(self, session_id):
return self.db.get("select session.session_id, session.expires, session.state, account.user_id, account.account_id from session join account on account.account_id = session.account_id where session.session_id = %s and session.expires > %s", (session_id, time()))
def create_session(self, user_id=None, password=None, verify_password=True):
if not user_id:
user_id = ''
user_id = user_id.lower()
account = user_id and self.db.get("select account_id, password from account where user_id = %s", (user_id,))
if user_id and (not account or (verify_password and not secret.verify_password(password, account['password']))):
raise TotoException(ERROR_USER_NOT_FOUND, "Invalid user ID or password")
session_id = PostgresSession.generate_id()
expires = time() + (user_id and self.session_ttl or self.anon_session_ttl)
session_data = {'user_id': user_id, 'expires': expires, 'session_id': session_id, 'account_id': account['account_id']}
if not self._cache_session_data(session_data):
self.db.execute("delete from session where account_id = %s and expires <= %s", (account['account_id'], time()))
self.db.execute("insert into session (account_id, expires, session_id) values (%s, %s, %s)", (account['account_id'], expires, session_id))
session = PostgresSession(self.db, session_data, self._session_cache)
session._verified = True
return session
def retrieve_session(self, session_id, hmac_data=None, data=None):
session_data = self._load_session_data(session_id)
if not session_data:
return None
user_id = session_data['user_id']
if user_id and data and hmac_data != base64.b64encode(hmac.new(str(user_id), data, hashlib.sha1).digest()):
raise TotoException(ERROR_INVALID_HMAC, "Invalid HMAC")
expires = time() + (user_id and self.session_renew or self.anon_session_renew)
if session_data['expires'] < expires:
session_data['expires'] = expires
if not self._cache_session_data(session_data):
self.db.execute("update session set expires = %s where session_id = %s", (session_data['expires'], session_id))
session = PostgresSession(self.db, session_data, self._session_cache)
session._verified = True
return session
def remove_session(self, session_id):
self.db.execute("delete from session where session_id = %s", (session_id,))
def clear_sessions(self, user_id):
user_id = user_id.lower()
self.db.execute("delete from session using session join account on account.account_id = session.account_id where account.user_id = %s", (user_id,))
def change_password(self, user_id, password, new_password):
user_id = user_id.lower()
account = self.db.get("select account_id, user_id, password from account where user_id = %s", (user_id,))
if not account or not secret.verify_password(password, account['password']):
raise TotoException(ERROR_USER_NOT_FOUND, "Invalid user ID or password")
self.db.execute("update account set password = %s where account_id = %s", (secret.password_hash(new_password), account['account_id']))
self.clear_sessions(user_id)
def generate_password(self, user_id):
user_id = user_id.lower()
account = self.db.get("select account_id, user_id from account where user_id = %s", (user_id,))
if not account:
raise TotoException(ERROR_USER_NOT_FOUND, "Invalid user ID")
pass_chars = string.ascii_letters + string.digits
new_password = ''.join([random.choice(pass_chars) for x in xrange(10)])
self.db.execute("update account set password = %s where account_id = %s", (secret.password_hash(new_password), account['account_id']))
self.clear_sessions(user_id)
return new_password