7
7
from hazelcast .errors import HazelcastError
8
8
from hazelcast .future import Future , ImmediateFuture , ImmediateExceptionFuture
9
9
from hazelcast .invocation import Invocation
10
- from hazelcast .serialization .compact import SchemaNotReplicatedError
10
+ from hazelcast .serialization .compact import SchemaNotReplicatedError , SchemaNotFoundError
11
11
from hazelcast .util import (
12
12
UUIDUtil ,
13
13
to_millis ,
@@ -544,18 +544,17 @@ class SqlRow:
544
544
If an integer value is passed to the ``[]`` operator, it will implicitly
545
545
call the :func:`get_object_with_index` and return the result.
546
546
547
- For any other type passed into the the ``[]`` operator, :func:`get_object`
547
+ For any other type passed into the ``[]`` operator, :func:`get_object`
548
548
will be called. Note that, :func:`get_object` expects ``str`` values.
549
549
Hence, the ``[]`` operator will raise error for any type other than integer
550
550
and string.
551
551
"""
552
552
553
- __slots__ = ("_row_metadata" , "_row" , "_deserialize_fn" )
553
+ __slots__ = ("_row_metadata" , "_row" )
554
554
555
- def __init__ (self , row_metadata , row , deserialize_fn ):
555
+ def __init__ (self , row_metadata , row ):
556
556
self ._row_metadata = row_metadata
557
557
self ._row = row
558
- self ._deserialize_fn = deserialize_fn
559
558
560
559
def get_object (self , column_name : str ) -> typing .Any :
561
560
"""Gets the value in the column indicated by the column name.
@@ -567,13 +566,6 @@ def get_object(self, column_name: str) -> typing.Any:
567
566
The type of the returned value depends on the SQL type of the column.
568
567
No implicit conversions are performed on the value.
569
568
570
- Warnings:
571
-
572
- Each call to this method might result in a deserialization if the
573
- column type for this object is :const:`SqlColumnType.OBJECT`.
574
- It is advised to assign the result of this method call to some
575
- variable and reuse it.
576
-
577
569
Args:
578
570
column_name: The column name.
579
571
@@ -583,7 +575,6 @@ def get_object(self, column_name: str) -> typing.Any:
583
575
Raises:
584
576
ValueError: If a column with the given name does not exist.
585
577
AssertionError: If the column name is not a string.
586
- HazelcastSqlError: If the object cannot be deserialized.
587
578
588
579
See Also:
589
580
:attr:`metadata`
@@ -597,21 +588,14 @@ def get_object(self, column_name: str) -> typing.Any:
597
588
index = self ._row_metadata .find_column (column_name )
598
589
if index == SqlRowMetadata .COLUMN_NOT_FOUND :
599
590
raise ValueError ("Column '%s' doesn't exist" % column_name )
600
- return self ._deserialize_fn ( self . _row [index ])
591
+ return self ._row [index ]
601
592
602
593
def get_object_with_index (self , column_index : int ) -> typing .Any :
603
594
"""Gets the value of the column by index.
604
595
605
596
The class of the returned value depends on the SQL type of the column.
606
597
No implicit conversions are performed on the value.
607
598
608
- Warnings:
609
-
610
- Each call to this method might result in a deserialization if the
611
- column type for this object is :const:`SqlColumnType.OBJECT`.
612
- It is advised to assign the result of this method call to some
613
- variable and reuse it.
614
-
615
599
Args:
616
600
column_index: Zero-based column index.
617
601
@@ -621,15 +605,14 @@ def get_object_with_index(self, column_index: int) -> typing.Any:
621
605
Raises:
622
606
IndexError: If the column index is out of bounds.
623
607
AssertionError: If the column index is not an integer.
624
- HazelcastSqlError: If the object cannot be deserialized.
625
608
626
609
See Also:
627
610
:attr:`metadata`
628
611
629
612
:attr:`SqlColumnMetadata.type`
630
613
"""
631
614
check_is_int (column_index , "Column index must be an integer" )
632
- return self ._deserialize_fn ( self . _row [column_index ])
615
+ return self ._row [column_index ]
633
616
634
617
@property
635
618
def metadata (self ) -> SqlRowMetadata :
@@ -680,23 +663,19 @@ class _IteratorBase:
680
663
__slots__ = (
681
664
"row_metadata" ,
682
665
"fetch_fn" ,
683
- "deserialize_fn" ,
684
666
"page" ,
685
667
"row_count" ,
686
668
"position" ,
687
669
"is_last" ,
688
670
)
689
671
690
- def __init__ (self , row_metadata , fetch_fn , deserialize_fn ):
672
+ def __init__ (self , row_metadata , fetch_fn ):
691
673
self .row_metadata = row_metadata
692
674
"""SqlRowMetadata: Row metadata."""
693
675
694
676
self .fetch_fn = fetch_fn
695
677
"""function: Fetches the next page. It produces a Future[_SqlPage]."""
696
678
697
- self .deserialize_fn = deserialize_fn
698
- """function: Deserializes the value."""
699
-
700
679
self .page = None
701
680
"""_SqlPage: Current page."""
702
681
@@ -729,7 +708,6 @@ def _get_current_row(self):
729
708
list: The row pointed by the current position.
730
709
"""
731
710
732
- # Deserialization happens lazily while getting the object.
733
711
return [self .page .get_value (i , self .position ) for i in range (self .page .column_count )]
734
712
735
713
@@ -766,7 +744,7 @@ def _has_next_continuation(self, future):
766
744
767
745
row = self ._get_current_row ()
768
746
self .position += 1
769
- return SqlRow (self .row_metadata , row , self . deserialize_fn )
747
+ return SqlRow (self .row_metadata , row )
770
748
771
749
def _has_next (self ):
772
750
"""Returns a Future indicating whether there are more rows
@@ -826,7 +804,7 @@ def __next__(self):
826
804
827
805
row = self ._get_current_row ()
828
806
self .position += 1
829
- return SqlRow (self .row_metadata , row , self . deserialize_fn )
807
+ return SqlRow (self .row_metadata , row )
830
808
831
809
def _has_next (self ):
832
810
while self .position == self .row_count :
@@ -1065,13 +1043,11 @@ def _get_iterator(self, should_get_blocking):
1065
1043
iterator = _BlockingIterator (
1066
1044
response .row_metadata ,
1067
1045
self ._fetch_next_page ,
1068
- self ._sql_service .deserialize_object ,
1069
1046
)
1070
1047
else :
1071
1048
iterator = _FutureProducingIterator (
1072
1049
response .row_metadata ,
1073
1050
self ._fetch_next_page ,
1074
- self ._sql_service .deserialize_object ,
1075
1051
)
1076
1052
1077
1053
# Pass the first page information to the iterator
@@ -1275,7 +1251,9 @@ def execute(self, sql, params, cursor_buffer_size, timeout, expected_result_type
1275
1251
)
1276
1252
1277
1253
invocation = Invocation (
1278
- request , connection = connection , response_handler = sql_execute_codec .decode_response
1254
+ request ,
1255
+ connection = connection ,
1256
+ response_handler = lambda m : sql_execute_codec .decode_response (m , self ._to_object ),
1279
1257
)
1280
1258
self ._invocation_service .invoke (invocation )
1281
1259
return invocation .future .continue_with (
@@ -1290,17 +1268,6 @@ def execute(self, sql, params, cursor_buffer_size, timeout, expected_result_type
1290
1268
except Exception as e :
1291
1269
return ImmediateExceptionFuture (self .re_raise (e , connection ))
1292
1270
1293
- def deserialize_object (self , obj ):
1294
- try :
1295
- return self ._serialization_service .to_object (obj )
1296
- except Exception as e :
1297
- raise HazelcastSqlError (
1298
- self .get_client_id (),
1299
- _SqlErrorCode .GENERIC ,
1300
- "Failed to deserialize query result value: %s" % try_to_get_error_message (e ),
1301
- e ,
1302
- )
1303
-
1304
1271
def fetch (self , connection , query_id , cursor_buffer_size ):
1305
1272
"""Fetches the next page of the query execution.
1306
1273
@@ -1317,7 +1284,9 @@ def fetch(self, connection, query_id, cursor_buffer_size):
1317
1284
"""
1318
1285
request = sql_fetch_codec .encode_request (query_id , cursor_buffer_size )
1319
1286
invocation = Invocation (
1320
- request , connection = connection , response_handler = sql_fetch_codec .decode_response
1287
+ request ,
1288
+ connection = connection ,
1289
+ response_handler = lambda m : sql_fetch_codec .decode_response (m , self ._to_object ),
1321
1290
)
1322
1291
self ._invocation_service .invoke (invocation )
1323
1292
return invocation .future
@@ -1376,6 +1345,19 @@ def close(self, connection, query_id):
1376
1345
self ._invocation_service .invoke (invocation )
1377
1346
return invocation .future
1378
1347
1348
+ def _to_object (self , data ):
1349
+ try :
1350
+ return self ._serialization_service .to_object (data )
1351
+ except SchemaNotFoundError as e :
1352
+ raise e
1353
+ except Exception as e :
1354
+ raise HazelcastSqlError (
1355
+ self .get_client_id (),
1356
+ _SqlErrorCode .GENERIC ,
1357
+ "Failed to deserialize query result value: %s" % try_to_get_error_message (e ),
1358
+ e ,
1359
+ )
1360
+
1379
1361
def _get_query_connection (self ):
1380
1362
try :
1381
1363
connection = self ._connection_manager .get_random_connection_for_sql ()
0 commit comments