本文整理汇总了Python中psycopg2.extras.execute_batch方法的典型用法代码示例。如果您正苦于以下问题:Python extras.execute_batch方法的具体用法?Python extras.execute_batch怎么用?Python extras.execute_batch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psycopg2.extras
的用法示例。
在下文中一共展示了extras.execute_batch方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: executemany
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import execute_batch [as 别名]
def executemany(cur, stmt):
"""Runs executemany on the value set with the provided cursor.
This function is a wrapper around the Python Database API 'executemany'
to accommodate for psycopg2 slow 'executemany' implementation.
Parameters
----------
cur : cursor
The cursor to run the statement with
stmt : str
The SQL statement to execute on
"""
if cur is not None:
if cfg.db_type != DBType.POSTGRES.value:
cur.executemany(stmt, cfg.input_data)
else:
import psycopg2.extras as p
p.execute_batch(cur, stmt, cfg.input_data)
示例2: do_executemany
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import execute_batch [as 别名]
def do_executemany(self, cursor, statement, parameters, context=None):
if self.psycopg2_batch_mode:
extras = self._psycopg2_extras()
extras.execute_batch(cursor, statement, parameters)
else:
cursor.executemany(statement, parameters)
示例3: test_update_fallback
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import execute_batch [as 别名]
def test_update_fallback(self):
from psycopg2 import extras
batch_page_size = self.engine.dialect.executemany_batch_page_size
eng = self.engine
meth = extras.execute_batch
stmt = "UPDATE data SET y=%(yval)s WHERE data.x = %(xval)s"
expected_kwargs = {"page_size": batch_page_size}
with mock.patch.object(
extras, meth.__name__, side_effect=meth
) as mock_exec:
with eng.connect() as conn:
conn.execute(
self.tables.data.update()
.where(self.tables.data.c.x == bindparam("xval"))
.values(y=bindparam("yval")),
[
{"xval": "x1", "yval": "y5"},
{"xval": "x3", "yval": "y6"},
],
)
if eng.dialect.executemany_mode & EXECUTEMANY_BATCH:
eq_(
mock_exec.mock_calls,
[
mock.call(
mock.ANY,
stmt,
(
{"xval": "x1", "yval": "y5"},
{"xval": "x3", "yval": "y6"},
),
**expected_kwargs
)
],
)
else:
eq_(mock_exec.mock_calls, [])
示例4: test_insert
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import execute_batch [as 别名]
def test_insert(self):
from psycopg2 import extras
values_page_size = self.engine.dialect.executemany_values_page_size
batch_page_size = self.engine.dialect.executemany_batch_page_size
if self.engine.dialect.executemany_mode & EXECUTEMANY_VALUES:
meth = extras.execute_values
stmt = "INSERT INTO data (x, y) VALUES %s"
expected_kwargs = {
"template": "(%(x)s, %(y)s)",
"page_size": values_page_size,
"fetch": False,
}
elif self.engine.dialect.executemany_mode & EXECUTEMANY_BATCH:
meth = extras.execute_batch
stmt = "INSERT INTO data (x, y) VALUES (%(x)s, %(y)s)"
expected_kwargs = {"page_size": batch_page_size}
else:
assert False
with mock.patch.object(
extras, meth.__name__, side_effect=meth
) as mock_exec:
with self.engine.connect() as conn:
conn.execute(
self.tables.data.insert(),
[
{"x": "x1", "y": "y1"},
{"x": "x2", "y": "y2"},
{"x": "x3", "y": "y3"},
],
)
eq_(
conn.execute(select([self.tables.data])).fetchall(),
[
(1, "x1", "y1", 5),
(2, "x2", "y2", 5),
(3, "x3", "y3", 5),
],
)
eq_(
mock_exec.mock_calls,
[
mock.call(
mock.ANY,
stmt,
(
{"x": "x1", "y": "y1"},
{"x": "x2", "y": "y2"},
{"x": "x3", "y": "y3"},
),
**expected_kwargs
)
],
)
示例5: test_insert_no_page_size
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import execute_batch [as 别名]
def test_insert_no_page_size(self):
from psycopg2 import extras
values_page_size = self.engine.dialect.executemany_values_page_size
batch_page_size = self.engine.dialect.executemany_batch_page_size
eng = self.engine
if eng.dialect.executemany_mode & EXECUTEMANY_VALUES:
meth = extras.execute_values
stmt = "INSERT INTO data (x, y) VALUES %s"
expected_kwargs = {
"template": "(%(x)s, %(y)s)",
"page_size": values_page_size,
"fetch": False,
}
elif eng.dialect.executemany_mode & EXECUTEMANY_BATCH:
meth = extras.execute_batch
stmt = "INSERT INTO data (x, y) VALUES (%(x)s, %(y)s)"
expected_kwargs = {"page_size": batch_page_size}
else:
assert False
with mock.patch.object(
extras, meth.__name__, side_effect=meth
) as mock_exec:
with eng.connect() as conn:
conn.execute(
self.tables.data.insert(),
[
{"x": "x1", "y": "y1"},
{"x": "x2", "y": "y2"},
{"x": "x3", "y": "y3"},
],
)
eq_(
mock_exec.mock_calls,
[
mock.call(
mock.ANY,
stmt,
(
{"x": "x1", "y": "y1"},
{"x": "x2", "y": "y2"},
{"x": "x3", "y": "y3"},
),
**expected_kwargs
)
],
)
示例6: test_insert_page_size
# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import execute_batch [as 别名]
def test_insert_page_size(self):
from psycopg2 import extras
opts = self.options.copy()
opts["executemany_batch_page_size"] = 500
opts["executemany_values_page_size"] = 1000
eng = engines.testing_engine(options=opts)
if eng.dialect.executemany_mode & EXECUTEMANY_VALUES:
meth = extras.execute_values
stmt = "INSERT INTO data (x, y) VALUES %s"
expected_kwargs = {
"fetch": False,
"page_size": 1000,
"template": "(%(x)s, %(y)s)",
}
elif eng.dialect.executemany_mode & EXECUTEMANY_BATCH:
meth = extras.execute_batch
stmt = "INSERT INTO data (x, y) VALUES (%(x)s, %(y)s)"
expected_kwargs = {"page_size": 500}
else:
assert False
with mock.patch.object(
extras, meth.__name__, side_effect=meth
) as mock_exec:
with eng.connect() as conn:
conn.execute(
self.tables.data.insert(),
[
{"x": "x1", "y": "y1"},
{"x": "x2", "y": "y2"},
{"x": "x3", "y": "y3"},
],
)
eq_(
mock_exec.mock_calls,
[
mock.call(
mock.ANY,
stmt,
(
{"x": "x1", "y": "y1"},
{"x": "x2", "y": "y2"},
{"x": "x3", "y": "y3"},
),
**expected_kwargs
)
],
)