-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
enable multivalues insert #19664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable multivalues insert #19664
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -572,8 +572,29 @@ def create(self): | |
else: | ||
self._execute_create() | ||
|
||
def insert_statement(self): | ||
return self.table.insert() | ||
def insert_statement(self, data, conn): | ||
""" | ||
Generate tuple of SQLAlchemy insert statement and any arguments | ||
to be executed by connection (via `_execute_insert`). | ||
|
||
Parameters | ||
---------- | ||
conn : SQLAlchemy connectable(engine/connection) | ||
Connection to recieve the data | ||
data : list of dict | ||
The data to be inserted | ||
|
||
Returns | ||
------- | ||
SQLAlchemy statement | ||
insert statement | ||
*, optional | ||
Additional parameters to be passed when executing insert statement | ||
""" | ||
dialect = getattr(conn, 'dialect', None) | ||
if dialect and getattr(dialect, 'supports_multivalues_insert', False): | ||
return self.table.insert(data), | ||
return self.table.insert(), data | ||
|
||
def insert_data(self): | ||
if self.index is not None: | ||
|
@@ -612,8 +633,9 @@ def insert_data(self): | |
return column_names, data_list | ||
|
||
def _execute_insert(self, conn, keys, data_iter): | ||
"""Insert data into this table with database connection""" | ||
data = [{k: v for k, v in zip(keys, row)} for row in data_iter] | ||
conn.execute(self.insert_statement(), data) | ||
conn.execute(*self.insert_statement(data, conn)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here as well |
||
|
||
def insert(self, chunksize=None): | ||
keys, data_list = self.insert_data() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1665,6 +1665,29 @@ class Temporary(Base): | |
|
||
tm.assert_frame_equal(df, expected) | ||
|
||
def test_insert_multivalues(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you explicity test which backends support this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jreback I believe I've done this by adding a class variable to the below classes. Let me know if that addresses this concern |
||
# issues addressed | ||
# https://github.com/pandas-dev/pandas/issues/14315 | ||
# https://github.com/pandas-dev/pandas/issues/8953 | ||
|
||
db = sql.SQLDatabase(self.conn) | ||
df = DataFrame({'A': [1, 0, 0], 'B': [1.1, 0.2, 4.3]}) | ||
table = sql.SQLTable("test_table", db, frame=df) | ||
data = [ | ||
{'A': 1, 'B': 0.46}, | ||
{'A': 0, 'B': -2.06} | ||
] | ||
statement = table.insert_statement(data, conn=self.conn)[0] | ||
|
||
if self.supports_multivalues_insert: | ||
assert statement.parameters == data, ( | ||
'insert statement should be multivalues' | ||
) | ||
else: | ||
assert statement.parameters is None, ( | ||
'insert statement should not be multivalues' | ||
) | ||
|
||
|
||
class _TestSQLAlchemyConn(_EngineToConnMixin, _TestSQLAlchemy): | ||
|
||
|
@@ -1679,6 +1702,7 @@ class _TestSQLiteAlchemy(object): | |
|
||
""" | ||
flavor = 'sqlite' | ||
supports_multivalues_insert = True | ||
|
||
@classmethod | ||
def connect(cls): | ||
|
@@ -1727,6 +1751,7 @@ class _TestMySQLAlchemy(object): | |
|
||
""" | ||
flavor = 'mysql' | ||
supports_multivalues_insert = True | ||
|
||
@classmethod | ||
def connect(cls): | ||
|
@@ -1796,6 +1821,7 @@ class _TestPostgreSQLAlchemy(object): | |
|
||
""" | ||
flavor = 'postgresql' | ||
supports_multivalues_insert = True | ||
|
||
@classmethod | ||
def connect(cls): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a doc-string here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a parameters and returns section as well. http://numpydoc.readthedocs.io/en/latest/format.html