本文整理匯總了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
)
],
)