本文整理汇总了Python中psycopg2.extensions.AsIs方法的典型用法代码示例。如果您正苦于以下问题:Python extensions.AsIs方法的具体用法?Python extensions.AsIs怎么用?Python extensions.AsIs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psycopg2.extensions
的用法示例。
在下文中一共展示了extensions.AsIs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drop_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def drop_db(hostname="localhost", username=None, password=None, dbname=None, port=None):
"""
Drop database.
:param hostname: string
:param username: string
:param password: string
:param dbname: string
"""
cn = create_cn(hostname, password, username, "postgres", port)
# ISOLATION_LEVEL_AUTOCOMMIT = 0
# Needed for DB drop.
cn.set_isolation_level(0)
with cn.cursor() as cr:
cr.execute("drop database if exists %s", (AsIs(dbname),))
close_cn(cn)
示例2: update_schema
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def update_schema(self):
if self.owner != self.old_owner:
log.info("alter schema %s owner to %s", self.old_schema, self.owner)
with self.connection.cursor() as cursor:
cursor.execute(
"ALTER SCHEMA %s OWNER TO %s",
[AsIs(self.old_schema), AsIs(self.owner)],
)
if self.schema != self.old_schema:
log.info("alter schema %s rename to %s", self.old_schema, self.schema)
with self.connection.cursor() as cursor:
cursor.execute(
"ALTER SCHEMA %s RENAME TO %s",
[AsIs(self.old_schema), AsIs(self.schema)],
)
示例3: pg_users
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def pg_users():
uid = str(uuid.uuid4()).replace("-", "")
name = f'user_{uid}'
name2 = f'user2_{uid}'
r = Request('Create', name, name)
with r.db_connection() as connection:
with connection.cursor() as cursor:
for n in [name, name2]:
cursor.execute(
"CREATE ROLE %s LOGIN ENCRYPTED PASSWORD %s", [AsIs(n), n]
)
connection.commit()
yield (name, name2)
with connection.cursor() as cursor:
#cursor.execute("DROP OWNED BY %s CASCADE", [AsIs(name)])
for n in [name, name2]:
cursor.execute("DROP ROLE %s", [AsIs(n)])
connection.commit()
示例4: register_numpy_types
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def register_numpy_types():
# Credit: https://github.com/musically-ut/psycopg2_numpy_ext
"""Register the AsIs adapter for following types from numpy:
- numpy.int8
- numpy.int16
- numpy.int32
- numpy.int64
- numpy.float16
- numpy.float32
- numpy.float64
- numpy.float128
"""
for typ in ['int8', 'int16', 'int32', 'int64',
'float16', 'float32', 'float64', 'float128',
'bool_']:
register_adapter(np.__getattribute__(typ), AsIs)
示例5: test_run
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def test_run(self):
SqlStep(self.database, ("INSERT INTO %s VALUES('%s')", TABLE_NAME, 1),
("INSERT INTO %s VALUES('%s')", TABLE_NAME, 2),
("INSERT INTO {0} VALUES ('3')".format(TABLE_NAME))).run()
self.database.open()
self.database.execute(
"SELECT COUNT(*) FROM %s",
(AsIs(TABLE_NAME),))
count = int(self.database.cursor.fetchone()[0])
self.assertEqual(3, count)
self.database.execute(
"SELECT * FROM %s",
(AsIs(TABLE_NAME),))
values = list(self.database.cursor.fetchall())
self.assertEqual([('1',), ('2',), ('3',)], values)
示例6: test_execute
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def test_execute(self):
self.database.open()
sql = SqlStep(self.database)
sql.execute(("INSERT INTO %s VALUES('%s')", TABLE_NAME, 1))
sql.execute(("INSERT INTO %s VALUES('%s')", TABLE_NAME, 2))
sql.execute(("INSERT INTO {0} VALUES ('3')".format(TABLE_NAME)))
self.database.commit()
self.database.open()
self.database.execute(
"SELECT COUNT(*) FROM %s",
(AsIs(TABLE_NAME),))
count = int(self.database.cursor.fetchone()[0])
self.assertEqual(3, count)
self.database.execute(
"SELECT * FROM %s",
(AsIs(TABLE_NAME),))
values = list(self.database.cursor.fetchall())
self.assertEqual([('1',), ('2',), ('3',)], values)
示例7: test_insert_update
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def test_insert_update(self):
table = TargetTable(self.schema, self.database)
table.create()
table.stage_update()
self.database.execute("INSERT INTO %s VALUES('%s')",
(AsIs(TABLE_NAME), AsIs(1),))
self.database.execute("INSERT INTO %s VALUES('%s')",
(AsIs(self.schema.update_table), AsIs(2),))
self.database.execute("INSERT INTO %s VALUES('%s')",
(AsIs(self.schema.update_table), AsIs(3),))
table.insert_update()
self.database.execute(
"SELECT * FROM %s",
(AsIs(TABLE_NAME),))
values = list(self.database.cursor.fetchall())
self.assertEqual([('1',), ('2',), ('3',)], values)
示例8: _get_channels_by_user_ids
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def _get_channels_by_user_ids(user_ids):
# TODO: use database.query instead of raw SQL
with db.conn() as conn:
cur = conn.execute('''
SELECT name
FROM %(schema_name)s.user_channel
WHERE _owner_id in %(user_ids)s
LIMIT %(len)s;
''', {
'schema_name': AsIs(_get_schema_name()),
'user_ids': tuple(user_ids),
'len': len(user_ids),
}
)
results = []
for row in cur:
results.append(row[0])
return results
示例9: getReceiptList
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def getReceiptList(self):
"""
Returns a list of message receipt statuses.
"""
receipts = list()
with db.conn() as conn:
cur = conn.execute('''
SELECT receipt.user, read_at, delivered_at
FROM %(schema_name)s.receipt
WHERE
"message" = %(message_id)s AND
(read_at IS NOT NULL or delivered_at is NOT NULL)
''', {
'schema_name': AsIs(_get_schema_name()),
'message_id': self.id.key
}
)
for row in cur:
receipts.append({
'user': row['user'],
'read_at': to_rfc3339_or_none(row['read_at']),
'delivered_at': to_rfc3339_or_none(row['delivered_at'])
})
return receipts
示例10: _get_new_last_message_id
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def _get_new_last_message_id(conn, message):
# TODO rewrite with database.query
cur = conn.execute('''
SELECT _id FROM %(schema_name)s.message
WHERE
deleted = false AND
seq < %(seq)s AND
conversation = %(conversation_id)s
ORDER BY seq DESC LIMIT 1
''', {
'schema_name': AsIs(_get_schema_name()),
'seq': message['seq'],
'conversation_id': message['conversation'].recordID.key
})
row = cur.fetchone()
return None if row is None else row['_id']
示例11: total_unread
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def total_unread(user_id=None):
if user_id is None:
user_id = current_user_id()
with db.conn() as conn:
cur = conn.execute('''
SELECT COUNT(*), SUM("unread_count")
FROM %(schema_name)s.user_conversation
WHERE
"unread_count" > 0 AND
"user" = %(user_id)s
''', {
'schema_name': AsIs(_get_schema_name()),
'user_id': user_id
}
)
r = cur.first()
conversation_count = r[0]
message_count = r[1]
return {
'conversation': conversation_count,
'message': message_count
}
示例12: create_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def create_db(hostname='localhost', username=None, password=None, dbname=None, port=None):
"""Create test database.
:param hostname: string
:param username: string
:param password: string
:param dbname: string
:param port: int
:return:
"""
cn = create_cn(hostname, password, username, 'postgres', port)
# ISOLATION_LEVEL_AUTOCOMMIT = 0
# Needed for DB creation.
cn.set_isolation_level(0)
with cn.cursor() as cr:
cr.execute('drop database if exists %s', (AsIs(dbname),))
cr.execute('create database %s', (AsIs(dbname),))
cn.close()
cn = create_cn(hostname, password, username, dbname, port)
return cn
示例13: drop_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def drop_db(hostname='localhost', username=None, password=None,
dbname=None, port=None):
"""
Drop database.
:param hostname: string
:param username: string
:param password: string
:param dbname: string
"""
cn = create_cn(hostname, password, username, 'postgres', port)
# ISOLATION_LEVEL_AUTOCOMMIT = 0
# Needed for DB drop.
cn.set_isolation_level(0)
with cn.cursor() as cr:
cr.execute('drop database if exists %s', (AsIs(dbname),))
close_cn(cn)
示例14: create_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def create_db(
hostname="localhost", username=None, password=None, dbname=None, port=None
):
"""Create test database.
:param hostname: string
:param username: string
:param password: string
:param dbname: string
:param port: int
:return:
"""
cn = create_cn(hostname, password, username, "postgres", port)
# ISOLATION_LEVEL_AUTOCOMMIT = 0
# Needed for DB creation.
cn.set_isolation_level(0)
with cn.cursor() as cr:
cr.execute("drop database if exists %s", (AsIs(dbname),))
cr.execute("create database %s", (AsIs(dbname),))
cn.close()
cn = create_cn(hostname, password, username, dbname, port)
return cn
示例15: adapt_array
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import AsIs [as 别名]
def adapt_array(arr):
conn = arr.field.model_class._meta.database.get_conn()
items = adapt(arr.items)
items.prepare(conn)
return AsIs('%s::%s%s' % (
items,
arr.field.get_column_type(),
'[]' * arr.field.dimensions))