2323)
2424from sqlitecloud .exceptions import (
2525 SQLiteCloudException ,
26- raise_sqlitecloud_error_with_extended_code ,
26+ get_sqlitecloud_error_with_extended_code ,
2727)
2828from sqlitecloud .resultset import (
2929 SQLITECLOUD_RESULT_TYPE ,
@@ -117,11 +117,7 @@ def send_blob(self, blob: bytes, conn: SQLiteCloudConnect) -> SQLiteCloudResult:
117117 """
118118 Send a blob to the SQLite Cloud server.
119119 """
120- try :
121- conn .isblob = True
122- return self ._internal_run_command (conn , blob )
123- finally :
124- conn .isblob = False
120+ return self ._internal_run_command (conn , self ._internal_serialize_command (blob ))
125121
126122 def is_connected (
127123 self , connection : SQLiteCloudConnect , main_socket : bool = True
@@ -314,7 +310,9 @@ def upload_database(
314310 command = f"UPLOAD DATABASE '{ dbname } ' { keyarg } { keyvalue } "
315311
316312 # execute command on server side
317- result = self ._internal_run_command (connection , command )
313+ result = self ._internal_run_command (
314+ connection , self ._internal_serialize_command (command )
315+ )
318316 if not result .data [0 ]:
319317 raise SQLiteCloudException (
320318 "An error occurred while initializing the upload of the database."
@@ -350,7 +348,9 @@ def upload_database(
350348 # Upload completed
351349 break
352350 except Exception as e :
353- self ._internal_run_command (connection , "UPLOAD ABORT" )
351+ self ._internal_run_command (
352+ connection , self ._internal_serialize_command ("UPLOAD ABORT" )
353+ )
354354 raise e
355355
356356 def download_database (
@@ -377,7 +377,10 @@ def download_database(
377377 """
378378 exists_cmd = " IF EXISTS" if if_exists else ""
379379 result = self ._internal_run_command (
380- connection , f"DOWNLOAD DATABASE { dbname } { exists_cmd } ;"
380+ connection ,
381+ self ._internal_serialize_command (
382+ f"DOWNLOAD DATABASE { dbname } { exists_cmd } ;"
383+ ),
381384 )
382385
383386 if result .nrows == 0 :
@@ -394,7 +397,9 @@ def download_database(
394397
395398 try :
396399 while progress_size < db_size :
397- result = self ._internal_run_command (connection , "DOWNLOAD STEP" )
400+ result = self ._internal_run_command (
401+ connection , self ._internal_serialize_command ("DOWNLOAD STEP" )
402+ )
398403
399404 # res is BLOB, decode it
400405 data = result .data [0 ]
@@ -408,7 +413,9 @@ def download_database(
408413 if data_len == 0 :
409414 break
410415 except Exception as e :
411- self ._internal_run_command (connection , "DOWNLOAD ABORT" )
416+ self ._internal_run_command (
417+ connection , self ._internal_serialize_command ("DOWNLOAD ABORT" )
418+ )
412419 raise e
413420
414421 def _internal_config_apply (
@@ -488,12 +495,6 @@ def _internal_socket_write(
488495 command (bytes): The command to send.
489496 main_socket (bool): If True, write to the main socket, otherwise write to the pubsub socket.
490497 """
491- # try:
492- # if "ATTACH DATABASE" in command.decode() or '"test_schema".table_info' in command.decode():
493- # pdb.set_trace()
494- # except:
495- # pass
496-
497498 # write buffer
498499 if len (command ) == 0 :
499500 return
@@ -594,30 +595,36 @@ def _internal_parse_number(
594595 sqlitecloud_number = SQLiteCloudNumber ()
595596 sqlitecloud_number .value = 0
596597 extvalue = 0
597- isext = False
598+ offcode = 0
599+ isext = 0
598600 blen = len (buffer )
599601
600602 # from 1 to skip the first command type character
601603 for i in range (index , blen ):
602604 c = chr (buffer [i ])
603605
604- # check for optional extended error code (ERRCODE:EXTERRCODE)
606+ # check for optional extended error code (ERRCODE:EXTERRCODE:OFFCODE )
605607 if c == ":" :
606- isext = True
608+ isext += 1
607609 continue
608610
609611 # check for end of value
610612 if c == " " :
611613 sqlitecloud_number .cstart = i + 1
612614 sqlitecloud_number .extcode = extvalue
615+ sqlitecloud_number .offcode = offcode
613616 return sqlitecloud_number
614617
615618 val = int (c ) if c .isdigit () else 0
616619
617- # compute numeric value
618- if isext :
620+ if isext == 1 :
621+ # XERRCODE
619622 extvalue = (extvalue * 10 ) + val
623+ elif isext == 2 :
624+ # OFFCODE
625+ offcode = (offcode * 10 ) + val
620626 else :
627+ # generic value or ERRCODE
621628 sqlitecloud_number .value = (sqlitecloud_number .value * 10 ) + val
622629
623630 sqlitecloud_number .value = 0
@@ -706,7 +713,7 @@ def _internal_parse_buffer(
706713 return SQLiteCloudResult (tag , clone )
707714
708715 elif cmd == SQLITECLOUD_CMD .ERROR .value :
709- # -LEN ERRCODE:EXTCODE ERRMSG
716+ # -LEN ERRCODE:EXTCODE:OFFCODE ERRMSG
710717 sqlite_number = self ._internal_parse_number (buffer )
711718 len_ = sqlite_number .value
712719 cstart = sqlite_number .cstart
@@ -721,9 +728,9 @@ def _internal_parse_buffer(
721728 len_ -= cstart2
722729 errmsg = clone [cstart2 :]
723730
724- raise raise_sqlitecloud_error_with_extended_code (
731+ raise get_sqlitecloud_error_with_extended_code (
725732 errmsg .decode (), errcode , xerrcode
726- )
733+ )( errmsg . decode (), errcode , xerrcode )
727734
728735 elif cmd in [SQLITECLOUD_CMD .ROWSET .value , SQLITECLOUD_CMD .ROWSET_CHUNK .value ]:
729736 # CMD_ROWSET: *LEN 0:VERSION ROWS COLS DATA
0 commit comments