Skip to content

Commit e10aacb

Browse files
author
Reggie Burnett
committed
implemented get_autoincrement_value
1 parent baf81ea commit e10aacb

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/mysqlx/protocol.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from .protobuf import mysqlx_expr_pb2 as MySQLxExpr
3636
from .result import ColumnMetaData
3737
from .dbdoc import DbDoc
38-
from .expr import (Expr, ExprParser, Find, UpdateOperation, build_int_scalar,
38+
from .expr import (Expr, ExprParser, Find, UpdateOperation, build_null_scalar, build_int_scalar,
3939
build_string_scalar, build_bool_scalar, build_double_scalar)
4040
from .errors import InterfaceError, OperationalError, ProgrammingError
4141

@@ -260,8 +260,11 @@ def _process_frame(self, msg, rs):
260260
sessStateMsg = MySQLxNotice.SessionStateChanged()
261261
sessStateMsg.ParseFromString(msg.payload)
262262
if sessStateMsg.param == \
263-
MySQLxNotice.SessionStateChanged.ROWS_AFFECTED:
263+
MySQLxNotice.SessionStateChanged.ROWS_AFFECTED:
264264
rs._rows_affected = sessStateMsg.value.v_unsigned_int
265+
elif sessStateMsg.param == \
266+
MySQLxNotice.SessionStateChanged.GENERATED_INSERT_ID:
267+
rs._generated_id = sessStateMsg.value.v_unsigned_int
265268

266269
def _read_message(self, rs):
267270
while True:
@@ -302,7 +305,8 @@ def get_column_metadata(self, rs):
302305

303306
def arg_object_to_expr(self, value, allow_relational):
304307
if value is None:
305-
return Expr.build_null_scalar()
308+
return MySQLxExpr.Expr(type=MySQLxExpr.Expr.LITERAL,
309+
literal=build_null_scalar())
306310
if isinstance(value, bool):
307311
return MySQLxExpr.Expr(type=MySQLxExpr.Expr.LITERAL,
308312
literal=build_bool_scalar(value))

lib/mysqlx/result.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ def __init__(self, connection):
518518
self._protocol = self._connection.protocol
519519
self._closed = False
520520
self._rows_affected = 0
521+
self._generated_id = -1
521522
self._warnings = []
522523
if connection._active_result is not None:
523524
connection._active_result.fetch_all()
@@ -540,9 +541,8 @@ def __init__(self, connection):
540541
def get_affected_items_count(self):
541542
return self._rows_affected
542543

543-
@property
544544
def get_autoincrement_value(self):
545-
pass
545+
return self._generated_id
546546

547547

548548
class BufferingResult(BaseResult):
@@ -618,6 +618,9 @@ def __init__(self, connection):
618618
super(SqlResult, self).__init__(connection)
619619
self._has_more_results = False
620620

621+
def get_autoincrement_value(self):
622+
return self._generated_id
623+
621624
def next_result(self):
622625
if self._closed:
623626
return False

tests/test_mysqlx_crud.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,17 @@ def test_multiple_resultsets(self):
479479
self.assertEqual(False, result.next_result())
480480

481481

482+
def test_auto_inc_value(self):
483+
table_name = "{0}.test".format(self.schema_name)
484+
485+
self.node_session.sql(
486+
"CREATE TABLE {0}(id INT KEY AUTO_INCREMENT, name VARCHAR(50))".format(table_name)).execute()
487+
result = self.node_session.sql("INSERT INTO {0} VALUES (NULL, 'Fred')".format(table_name)).execute()
488+
self.assertEqual(1, result.get_autoincrement_value())
489+
table = self.schema.get_table("test")
490+
result2 = table.insert("id", "name").values(None, "Boo").execute()
491+
self.assertEqual(2, result2.get_autoincrement_value())
492+
482493
def test_column_metadata(self):
483494
table_name = "{0}.test".format(self.schema_name)
484495

0 commit comments

Comments
 (0)