-
Notifications
You must be signed in to change notification settings - Fork 61
/
messages.pyx
2687 lines (2506 loc) · 116 KB
/
messages.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#------------------------------------------------------------------------------
# Copyright (c) 2020, 2024, Oracle and/or its affiliates.
#
# This software is dual-licensed to you under the Universal Permissive License
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
# either license.
#
# If you elect to accept the software under the Apache License, Version 2.0,
# the following applies:
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# messages.pyx
#
# Cython file defining the various messages that are sent to the database and
# the responses that are received by the client (embedded in thin_impl.pyx).
#------------------------------------------------------------------------------
@cython.freelist(20)
cdef class _OracleErrorInfo:
cdef:
uint32_t num
uint16_t cursor_id
uint64_t pos
uint64_t rowcount
str message
Rowid rowid
list batcherrors
cdef class Message:
cdef:
BaseThinConnImpl conn_impl
_OracleErrorInfo error_info
uint8_t message_type
uint8_t function_code
uint32_t call_status
uint16_t end_to_end_seq_num
bint end_of_response
bint error_occurred
bint flush_out_binds
bint resend
bint retry
object warning
cdef int _check_and_raise_exception(self) except -1:
"""
Checks to see if an error has occurred. If one has, an error object is
created and then the appropriate exception raised. Note that if a "dead
connection" error is detected, the connection is forced closed
immediately.
"""
if self.error_occurred:
error = errors._Error(self.error_info.message,
code=self.error_info.num,
offset=self.error_info.pos)
if error.is_session_dead:
self.conn_impl._protocol._force_close()
raise error.exc_type(error)
cdef int _initialize(self, BaseThinConnImpl conn_impl) except -1:
"""
Initializes the message to contain the connection and a place to store
error information. For DRCP, the status of the connection may change
after the first round-trip to the database so this information needs to
be preserved. Child classes may have their own initialization. In order
to avoid overhead using the constructor, a special hook method is used
instead.
"""
if conn_impl._protocol._transport is None:
errors._raise_err(errors.ERR_NOT_CONNECTED)
self.conn_impl = conn_impl
self.message_type = TNS_MSG_TYPE_FUNCTION
self.error_info = _OracleErrorInfo.__new__(_OracleErrorInfo)
self._initialize_hook()
cdef int _initialize_hook(self) except -1:
"""
A hook that is used by subclasses to perform any necessary
initialization specific to that class.
"""
pass
cdef int _preprocess(self) except -1:
pass
cdef int _process_error_info(self, ReadBuffer buf) except -1:
cdef:
uint32_t num_bytes, i, offset, num_offsets
_OracleErrorInfo info = self.error_info
uint16_t temp16, num_errors, error_code
uint8_t first_byte, flags
int16_t error_pos
str error_msg
buf.read_ub4(&self.call_status) # end of call status
buf.skip_ub2() # end to end seq#
buf.skip_ub4() # current row number
buf.skip_ub2() # error number
buf.skip_ub2() # array elem error
buf.skip_ub2() # array elem error
buf.read_ub2(&info.cursor_id) # cursor id
buf.read_sb2(&error_pos) # error position
buf.skip_ub1() # sql type (19c and earlier)
buf.skip_ub1() # fatal?
buf.skip_ub1() # flags
buf.skip_ub1() # user cursor options
buf.skip_ub1() # UPI parameter
buf.read_ub1(&flags)
if flags & 0x20:
self.warning = errors._create_warning(errors.WRN_COMPILATION_ERROR)
buf.read_rowid(&info.rowid) # rowid
buf.skip_ub4() # OS error
buf.skip_ub1() # statement number
buf.skip_ub1() # call number
buf.skip_ub2() # padding
buf.skip_ub4() # success iters
buf.read_ub4(&num_bytes) # oerrdd (logical rowid)
if num_bytes > 0:
buf.skip_raw_bytes_chunked()
# batch error codes
buf.read_ub2(&num_errors) # batch error codes array
if num_errors > 0:
info.batcherrors = []
buf.read_ub1(&first_byte)
for i in range(num_errors):
if first_byte == TNS_LONG_LENGTH_INDICATOR:
buf.skip_ub4() # chunk length ignored
buf.read_ub2(&error_code)
info.batcherrors.append(errors._Error(code=error_code))
if first_byte == TNS_LONG_LENGTH_INDICATOR:
buf.skip_raw_bytes(1) # ignore end marker
# batch error offsets
buf.read_ub4(&num_offsets) # batch error row offset array
if num_offsets > 0:
if num_offsets > 65535:
errors._raise_err(errors.ERR_TOO_MANY_BATCH_ERRORS)
buf.read_ub1(&first_byte)
for i in range(num_offsets):
if first_byte == TNS_LONG_LENGTH_INDICATOR:
buf.skip_ub4() # chunk length ignored
buf.read_ub4(&offset)
if i < num_errors:
info.batcherrors[i].offset = offset
if first_byte == TNS_LONG_LENGTH_INDICATOR:
buf.skip_raw_bytes(1) # ignore end marker
# batch error messages
buf.read_ub2(&temp16) # batch error messages array
if temp16 > 0:
buf.skip_raw_bytes(1) # ignore packed size
for i in range(temp16):
buf.skip_ub2() # skip chunk length
info.batcherrors[i].message = \
buf.read_str(CS_FORM_IMPLICIT).rstrip()
info.batcherrors[i]._make_adjustments()
buf.skip_raw_bytes(2) # ignore end marker
buf.read_ub4(&info.num) # error number (extended)
buf.read_ub8(&info.rowcount) # row number (extended)
# fields added in Oracle Database 20c
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_20_1:
buf.skip_ub4() # sql type
buf.skip_ub4() # server checksum
# error message
if info.num != 0:
self.error_occurred = True
if error_pos > 0:
info.pos = error_pos
info.message = buf.read_str(CS_FORM_IMPLICIT).rstrip()
# an error message marks the end of a response if no explicit end of
# response is available
if not buf._caps.supports_end_of_response:
self.end_of_response = True
cdef int _process_message(self, ReadBuffer buf,
uint8_t message_type) except -1:
if message_type == TNS_MSG_TYPE_ERROR:
self._process_error_info(buf)
elif message_type == TNS_MSG_TYPE_WARNING:
self._process_warning_info(buf)
elif message_type == TNS_MSG_TYPE_STATUS:
buf.read_ub4(&self.call_status)
buf.read_ub2(&self.end_to_end_seq_num)
if not buf._caps.supports_end_of_response:
self.end_of_response = True
elif message_type == TNS_MSG_TYPE_PARAMETER:
self._process_return_parameters(buf)
elif message_type == TNS_MSG_TYPE_SERVER_SIDE_PIGGYBACK:
self._process_server_side_piggyback(buf)
elif message_type == TNS_MSG_TYPE_END_OF_RESPONSE:
self.end_of_response = True
else:
errors._raise_err(errors.ERR_MESSAGE_TYPE_UNKNOWN,
message_type=message_type,
position=buf._pos - 1)
cdef int _process_return_parameters(self, ReadBuffer buf) except -1:
raise NotImplementedError()
cdef int _process_server_side_piggyback(self, ReadBuffer buf) except -1:
cdef:
uint16_t num_elements, i, temp16
uint32_t num_bytes, flags
uint8_t opcode
buf.read_ub1(&opcode)
if opcode == TNS_SERVER_PIGGYBACK_LTXID:
buf.read_ub4(&num_bytes)
if num_bytes > 0:
buf.skip_raw_bytes(num_bytes)
elif opcode == TNS_SERVER_PIGGYBACK_QUERY_CACHE_INVALIDATION \
or opcode == TNS_SERVER_PIGGYBACK_TRACE_EVENT:
pass
elif opcode == TNS_SERVER_PIGGYBACK_OS_PID_MTS:
buf.read_ub2(&temp16)
buf.skip_raw_bytes_chunked()
elif opcode == TNS_SERVER_PIGGYBACK_SYNC:
buf.skip_ub2() # skip number of DTYs
buf.skip_ub1() # skip length of DTYs
buf.read_ub2(&num_elements)
buf.skip_ub1() # skip length
for i in range(num_elements):
buf.read_ub2(&temp16)
if temp16 > 0: # skip key
buf.skip_raw_bytes_chunked()
buf.read_ub2(&temp16)
if temp16 > 0: # skip value
buf.skip_raw_bytes_chunked()
buf.skip_ub2() # skip flags
buf.skip_ub4() # skip overall flags
elif opcode == TNS_SERVER_PIGGYBACK_EXT_SYNC:
buf.skip_ub2() # skip number of DTYs
buf.skip_ub1() # skip length of DTYs
elif opcode == TNS_SERVER_PIGGYBACK_AC_REPLAY_CONTEXT:
buf.skip_ub2() # skip number of DTYs
buf.skip_ub1() # skip length of DTYs
buf.skip_ub4() # skip flags
buf.skip_ub4() # skip error code
buf.skip_ub1() # skip queue
buf.read_ub4(&num_bytes) # skip replay context
if num_bytes > 0:
buf.skip_raw_bytes_chunked()
elif opcode == TNS_SERVER_PIGGYBACK_SESS_RET:
buf.skip_ub2()
buf.skip_ub1()
buf.read_ub2(&num_elements)
if num_elements > 0:
buf.skip_ub1()
for i in range(num_elements):
buf.read_ub2(&temp16)
if temp16 > 0: # skip key
buf.skip_raw_bytes_chunked()
buf.read_ub2(&temp16)
if temp16 > 0: # skip value
buf.skip_raw_bytes_chunked()
buf.skip_ub2() # skip flags
buf.read_ub4(&flags) # session flags
if flags & TNS_SESSGET_SESSION_CHANGED:
if self.conn_impl._drcp_establish_session:
self.conn_impl._statement_cache.clear_open_cursors()
self.conn_impl._drcp_establish_session = False
buf.skip_ub4() # session id
buf.skip_ub2() # serial number
else:
errors._raise_err(errors.ERR_UNKNOWN_SERVER_PIGGYBACK,
opcode=opcode)
cdef int _process_warning_info(self, ReadBuffer buf) except -1:
cdef:
uint16_t num_bytes, error_num
str message
buf.read_ub2(&error_num) # error number
buf.read_ub2(&num_bytes) # length of error message
buf.skip_ub2() # flags
if error_num != 0 and num_bytes > 0:
message = buf.read_str(CS_FORM_IMPLICIT).rstrip()
self.warning = errors._Error(message, code=error_num,
iswarning=True)
cdef int _write_function_code(self, WriteBuffer buf) except -1:
buf.write_uint8(self.message_type)
buf.write_uint8(self.function_code)
buf.write_seq_num()
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_23_1_EXT_1:
buf.write_ub8(0) # token number
cdef int _write_message(self, WriteBuffer buf) except -1:
self._write_function_code(buf)
cdef int _write_piggyback_code(self, WriteBuffer buf,
uint8_t code) except -1:
buf.write_uint8(TNS_MSG_TYPE_PIGGYBACK)
buf.write_uint8(code)
buf.write_seq_num()
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_23_1_EXT_1:
buf.write_ub8(0) # token number
cdef int postprocess(self) except -1:
pass
async def postprocess_async(self):
pass
cdef int process(self, ReadBuffer buf) except -1:
cdef uint8_t message_type
self.end_of_response = False
self.flush_out_binds = False
self._preprocess()
while not self.end_of_response:
buf.save_point()
buf.read_ub1(&message_type)
self._process_message(buf, message_type)
cdef int send(self, WriteBuffer buf) except -1:
buf.start_request(TNS_PACKET_TYPE_DATA)
self._write_message(buf)
buf.end_request()
cdef class MessageWithData(Message):
cdef:
BaseThinDbObjectTypeCache type_cache
BaseThinCursorImpl cursor_impl
array.array bit_vector_buf
const char_type *bit_vector
bint arraydmlrowcounts
uint32_t row_index
uint32_t num_execs
uint16_t num_columns_sent
list dmlrowcounts
bint batcherrors
list out_var_impls
bint in_fetch
bint parse_only
object cursor
uint32_t offset
cdef int _adjust_fetch_info(self,
ThinVarImpl prev_var_impl,
FetchInfoImpl fetch_info) except -1:
"""
When a query is re-executed but the data type of a column has changed
the server returns the type information of the new type. However, if
the data type returned now is a CLOB or BLOB and the data type
previously returned was CHAR/VARCHAR/RAW (or the equivalent long
types), then the server returns the data as LONG (RAW), similarly to
what happens when a define is done to return CLOB/BLOB as string/bytes.
Detect this situation and adjust the fetch type appropriately.
"""
cdef:
FetchInfoImpl prev_fetch_info = prev_var_impl._fetch_info
uint8_t csfrm = prev_var_impl.dbtype._csfrm
uint8_t type_num
if fetch_info.dbtype._ora_type_num == TNS_DATA_TYPE_CLOB \
and prev_fetch_info.dbtype._ora_type_num in \
(TNS_DATA_TYPE_CHAR, TNS_DATA_TYPE_VARCHAR,
TNS_DATA_TYPE_LONG):
type_num = TNS_DATA_TYPE_LONG
fetch_info.dbtype = DbType._from_ora_type_and_csfrm(type_num,
csfrm)
elif fetch_info.dbtype._ora_type_num == TNS_DATA_TYPE_BLOB \
and prev_fetch_info.dbtype._ora_type_num in \
(TNS_DATA_TYPE_RAW, TNS_DATA_TYPE_LONG_RAW):
type_num = TNS_DATA_TYPE_LONG_RAW
fetch_info.dbtype = DbType._from_ora_type_and_csfrm(type_num,
csfrm)
cdef object _create_cursor_from_describe(self, ReadBuffer buf,
object cursor=None):
cdef BaseThinCursorImpl cursor_impl
if cursor is None:
cursor = self.cursor.connection.cursor()
cursor_impl = cursor._impl
cursor_impl._statement = self.conn_impl._get_statement()
cursor_impl._more_rows_to_fetch = True
cursor_impl._statement._is_query = True
self._process_describe_info(buf, cursor, cursor_impl)
return cursor
cdef int _get_bit_vector(self, ReadBuffer buf,
ssize_t num_bytes) except -1:
"""
Gets the bit vector from the buffer and stores it for later use by the
row processing code. Since it is possible that the packet buffer may be
overwritten by subsequent packet retrieval, the bit vector must be
copied. An array is stored and a pointer to the underlying memory is
used for performance reasons.
"""
cdef const char_type *ptr = buf.read_raw_bytes(num_bytes)
if self.bit_vector_buf is None:
self.bit_vector_buf = array.array('B')
array.resize(self.bit_vector_buf, num_bytes)
self.bit_vector = <const char_type*> self.bit_vector_buf.data.as_chars
memcpy(<void*> self.bit_vector, ptr, num_bytes)
cdef bint _is_duplicate_data(self, uint32_t column_num):
"""
Returns a boolean indicating if the given column contains data
duplicated from the previous row. When duplicate data exists, the
server sends a bit vector. Bits that are set indicate that data is sent
with the row data; bits that are not set indicate that data should be
duplicated from the previous row.
"""
cdef int byte_num, bit_num
if self.bit_vector == NULL:
return False
byte_num = column_num // 8
bit_num = column_num % 8
return self.bit_vector[byte_num] & (1 << bit_num) == 0
cdef int _write_bind_params(self, WriteBuffer buf, list params) except -1:
cdef:
bint has_data = False
list bind_var_impls
BindInfo bind_info
bind_var_impls = []
for bind_info in params:
if not bind_info._is_return_bind:
has_data = True
bind_var_impls.append(bind_info._bind_var_impl)
self._write_column_metadata(buf, bind_var_impls)
# write parameter values unless statement contains only returning binds
if has_data:
for i in range(self.num_execs):
buf.write_uint8(TNS_MSG_TYPE_ROW_DATA)
self._write_bind_params_row(buf, params, i)
cdef int _preprocess(self) except -1:
cdef:
Statement statement = self.cursor_impl._statement
BindInfo bind_info
if statement._is_returning and not self.parse_only:
self.out_var_impls = []
for bind_info in statement._bind_info_list:
if not bind_info._is_return_bind:
continue
self.out_var_impls.append(bind_info._bind_var_impl)
elif statement._is_query:
self._preprocess_query()
cdef int _preprocess_query(self) except -1:
"""
Actions that takes place before query data is processed.
"""
cdef:
BaseThinCursorImpl cursor_impl = self.cursor_impl
Statement statement = cursor_impl._statement
object type_handler, conn
ThinVarImpl var_impl
bint uses_fetch_info
ssize_t i, num_vals
# set values to indicate the start of a new fetch operation
self.in_fetch = True
cursor_impl._more_rows_to_fetch = True
cursor_impl._buffer_rowcount = cursor_impl._buffer_index = 0
# if no fetch variables exist, nothing further to do at this point; the
# processing that follows will take the metadata returned by the server
# and use it to create new fetch variables
if statement._fetch_var_impls is None:
return 0
# if the type handler set on the cursor or connection does not match
# the one that was used during the last fetch, rebuild the fetch
# variables in order to take the new type handler into account
conn = self.cursor.connection
type_handler = cursor_impl._get_output_type_handler(&uses_fetch_info)
if type_handler is not statement._last_output_type_handler:
for i, var_impl in enumerate(cursor_impl.fetch_var_impls):
cursor_impl._create_fetch_var(conn, self.cursor, type_handler,
uses_fetch_info, i,
var_impl._fetch_info)
statement._last_output_type_handler = type_handler
# the list of output variables is equivalent to the fetch variables
self.out_var_impls = cursor_impl.fetch_var_impls
# resize fetch variables, if necessary, to allow room in each variable
# for the fetch array size
for var_impl in self.out_var_impls:
if var_impl.num_elements >= cursor_impl._fetch_array_size:
continue
num_vals = (cursor_impl._fetch_array_size - var_impl.num_elements)
var_impl.num_elements = cursor_impl._fetch_array_size
var_impl._values.extend([None] * num_vals)
cdef int _process_bit_vector(self, ReadBuffer buf) except -1:
cdef ssize_t num_bytes
buf.read_ub2(&self.num_columns_sent)
num_bytes = self.cursor_impl._num_columns // 8
if self.cursor_impl._num_columns % 8 > 0:
num_bytes += 1
self._get_bit_vector(buf, num_bytes)
cdef object _process_column_data(self, ReadBuffer buf,
ThinVarImpl var_impl, uint32_t pos):
cdef:
uint8_t num_bytes, ora_type_num, csfrm
const char* encoding_errors = NULL
bytes encoding_errors_bytes
ThinDbObjectTypeImpl typ_impl
BaseThinCursorImpl cursor_impl
object column_value = None
ThinDbObjectImpl obj_impl
FetchInfoImpl fetch_info
int32_t actual_num_bytes
uint32_t buffer_size
Rowid rowid
fetch_info = var_impl._fetch_info
if fetch_info is not None:
ora_type_num = fetch_info.dbtype._ora_type_num
csfrm = fetch_info.dbtype._csfrm
buffer_size = fetch_info.buffer_size
else:
ora_type_num = var_impl.dbtype._ora_type_num
csfrm = var_impl.dbtype._csfrm
buffer_size = var_impl.buffer_size
if var_impl.bypass_decode:
ora_type_num = TNS_DATA_TYPE_RAW
if buffer_size == 0 and self.in_fetch \
and ora_type_num not in (TNS_DATA_TYPE_LONG,
TNS_DATA_TYPE_LONG_RAW,
TNS_DATA_TYPE_UROWID):
column_value = None # column is null by describe
elif ora_type_num == TNS_DATA_TYPE_VARCHAR \
or ora_type_num == TNS_DATA_TYPE_CHAR \
or ora_type_num == TNS_DATA_TYPE_LONG:
if csfrm == CS_FORM_NCHAR:
buf._caps._check_ncharset_id()
if var_impl.encoding_errors is not None:
encoding_errors_bytes = var_impl.encoding_errors.encode()
encoding_errors = encoding_errors_bytes
column_value = buf.read_str(csfrm, encoding_errors)
elif ora_type_num == TNS_DATA_TYPE_RAW \
or ora_type_num == TNS_DATA_TYPE_LONG_RAW:
column_value = buf.read_bytes()
elif ora_type_num == TNS_DATA_TYPE_NUMBER:
column_value = buf.read_oracle_number(var_impl._preferred_num_type)
elif ora_type_num == TNS_DATA_TYPE_DATE \
or ora_type_num == TNS_DATA_TYPE_TIMESTAMP \
or ora_type_num == TNS_DATA_TYPE_TIMESTAMP_LTZ \
or ora_type_num == TNS_DATA_TYPE_TIMESTAMP_TZ:
column_value = buf.read_date()
elif ora_type_num == TNS_DATA_TYPE_ROWID:
if not self.in_fetch:
column_value = buf.read_str(CS_FORM_IMPLICIT)
else:
buf.read_ub1(&num_bytes)
if num_bytes == 0 or num_bytes == TNS_NULL_LENGTH_INDICATOR:
column_value = None
else:
buf.read_rowid(&rowid)
column_value = _encode_rowid(&rowid)
elif ora_type_num == TNS_DATA_TYPE_UROWID:
if not self.in_fetch:
column_value = buf.read_str(CS_FORM_IMPLICIT)
else:
column_value = buf.read_urowid()
elif ora_type_num == TNS_DATA_TYPE_BINARY_DOUBLE:
column_value = buf.read_binary_double()
elif ora_type_num == TNS_DATA_TYPE_BINARY_FLOAT:
column_value = buf.read_binary_float()
elif ora_type_num == TNS_DATA_TYPE_BINARY_INTEGER:
column_value = buf.read_oracle_number(NUM_TYPE_INT)
if column_value is not None:
column_value = int(column_value)
elif ora_type_num == TNS_DATA_TYPE_CURSOR:
buf.skip_ub1() # length (fixed value)
if not self.in_fetch:
column_value = var_impl._values[pos]
column_value = self._create_cursor_from_describe(buf, column_value)
cursor_impl = column_value._impl
buf.read_ub2(&cursor_impl._statement._cursor_id)
elif ora_type_num == TNS_DATA_TYPE_BOOLEAN:
column_value = buf.read_bool()
elif ora_type_num == TNS_DATA_TYPE_INTERVAL_DS:
column_value = buf.read_interval_ds()
elif ora_type_num == TNS_DATA_TYPE_INTERVAL_YM:
column_value = buf.read_interval_ym()
elif ora_type_num in (TNS_DATA_TYPE_CLOB,
TNS_DATA_TYPE_BLOB,
TNS_DATA_TYPE_BFILE):
column_value = buf.read_lob_with_length(self.conn_impl,
var_impl.dbtype)
elif ora_type_num == TNS_DATA_TYPE_JSON:
column_value = buf.read_oson()
elif ora_type_num == TNS_DATA_TYPE_VECTOR:
column_value = buf.read_vector()
elif ora_type_num == TNS_DATA_TYPE_INT_NAMED:
typ_impl = var_impl.objtype
if typ_impl is None:
column_value = buf.read_xmltype(self.conn_impl)
else:
obj_impl = buf.read_dbobject(typ_impl)
if obj_impl is not None:
if not self.in_fetch:
column_value = var_impl._values[pos]
if column_value is not None:
column_value._impl = obj_impl
else:
column_value = PY_TYPE_DB_OBJECT._from_impl(obj_impl)
else:
errors._raise_err(errors.ERR_DB_TYPE_NOT_SUPPORTED,
name=var_impl.dbtype.name)
if not self.in_fetch:
buf.read_sb4(&actual_num_bytes)
if actual_num_bytes < 0 and ora_type_num == TNS_DATA_TYPE_BOOLEAN:
column_value = None
elif actual_num_bytes != 0 and column_value is not None:
unit_type = "bytes" if isinstance(column_value, bytes) \
else "characters"
errors._raise_err(errors.ERR_COLUMN_TRUNCATED,
col_value_len=len(column_value),
unit=unit_type, actual_len=actual_num_bytes)
elif ora_type_num == TNS_DATA_TYPE_LONG \
or ora_type_num == TNS_DATA_TYPE_LONG_RAW:
buf.skip_sb4() # null indicator
buf.skip_ub4() # return code
if column_value is not None:
if var_impl._conv_func is not None:
column_value = var_impl._conv_func(column_value)
return column_value
cdef FetchInfoImpl _process_column_info(self, ReadBuffer buf,
BaseThinCursorImpl cursor_impl):
cdef:
uint32_t num_bytes, uds_flags, num_annotations, i
ThinDbObjectTypeImpl typ_impl
str schema, name, key, value
uint8_t data_type, csfrm
FetchInfoImpl fetch_info
int8_t precision, scale
uint8_t nulls_allowed
int cache_num
bytes oid
buf.read_ub1(&data_type)
fetch_info = FetchInfoImpl()
buf.skip_ub1() # flags
buf.read_sb1(&precision)
fetch_info.precision = precision
buf.read_sb1(&scale)
fetch_info.scale = scale
buf.read_ub4(&fetch_info.buffer_size)
buf.skip_ub4() # max number of array elements
buf.skip_ub8() # cont flags
buf.read_ub4(&num_bytes) # OID
if num_bytes > 0:
oid = buf.read_bytes()
buf.skip_ub2() # version
buf.skip_ub2() # character set id
buf.read_ub1(&csfrm) # character set form
fetch_info.dbtype = DbType._from_ora_type_and_csfrm(data_type, csfrm)
buf.read_ub4(&fetch_info.size)
if data_type == TNS_DATA_TYPE_RAW:
fetch_info.size = fetch_info.buffer_size
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_12_2:
buf.skip_ub4() # oaccolid
buf.read_ub1(&nulls_allowed)
fetch_info.nulls_allowed = nulls_allowed
buf.skip_ub1() # v7 length of name
buf.read_ub4(&num_bytes)
if num_bytes > 0:
fetch_info.name = buf.read_str(CS_FORM_IMPLICIT)
buf.read_ub4(&num_bytes)
if num_bytes > 0:
schema = buf.read_str(CS_FORM_IMPLICIT)
buf.read_ub4(&num_bytes)
if num_bytes > 0:
name = buf.read_str(CS_FORM_IMPLICIT)
buf.skip_ub2() # column position
buf.read_ub4(&uds_flags)
fetch_info.is_json = uds_flags & TNS_UDS_FLAGS_IS_JSON
fetch_info.is_oson = uds_flags & TNS_UDS_FLAGS_IS_OSON
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_23_1:
buf.read_ub4(&num_bytes)
if num_bytes > 0:
fetch_info.domain_schema = buf.read_str(CS_FORM_IMPLICIT)
buf.read_ub4(&num_bytes)
if num_bytes > 0:
fetch_info.domain_name = buf.read_str(CS_FORM_IMPLICIT)
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_23_1_EXT_3:
buf.read_ub4(&num_annotations)
if num_annotations > 0:
buf.skip_ub1()
fetch_info.annotations = {}
buf.read_ub4(&num_annotations)
buf.skip_ub1()
for i in range(num_annotations):
buf.skip_ub4() # length of key
key = buf.read_str(CS_FORM_IMPLICIT)
buf.read_ub4(&num_bytes)
if num_bytes > 0:
value = buf.read_str(CS_FORM_IMPLICIT)
else:
value = ""
fetch_info.annotations[key] = value
buf.skip_ub4() # flags
buf.skip_ub4() # flags
if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_23_4:
buf.read_ub4(&fetch_info.vector_dimensions)
buf.read_ub1(&fetch_info.vector_format)
buf.read_ub1(&fetch_info.vector_flags)
if data_type == TNS_DATA_TYPE_INT_NAMED:
if self.type_cache is None:
cache_num = self.conn_impl._dbobject_type_cache_num
self.type_cache = get_dbobject_type_cache(cache_num)
typ_impl = self.type_cache.get_type_for_info(oid, schema, None,
name)
if typ_impl.is_xml_type:
fetch_info.dbtype = DB_TYPE_XMLTYPE
else:
fetch_info.objtype = typ_impl
return fetch_info
cdef int _process_describe_info(self, ReadBuffer buf,
object cursor,
BaseThinCursorImpl cursor_impl) except -1:
cdef:
Statement stmt = cursor_impl._statement
list prev_fetch_var_impls
object type_handler, conn
FetchInfoImpl fetch_info
uint32_t num_bytes, i
bint uses_fetch_info
str message
buf.skip_ub4() # max row size
buf.read_ub4(&cursor_impl._num_columns)
prev_fetch_var_impls = stmt._fetch_var_impls
cursor_impl._init_fetch_vars(cursor_impl._num_columns)
if cursor_impl._num_columns > 0:
buf.skip_ub1()
type_handler = cursor_impl._get_output_type_handler(&uses_fetch_info)
conn = self.cursor.connection
for i in range(cursor_impl._num_columns):
fetch_info = self._process_column_info(buf, cursor_impl)
if prev_fetch_var_impls is not None \
and i < len(prev_fetch_var_impls):
self._adjust_fetch_info(prev_fetch_var_impls[i], fetch_info)
if fetch_info.dbtype._ora_type_num in (TNS_DATA_TYPE_BLOB,
TNS_DATA_TYPE_CLOB,
TNS_DATA_TYPE_JSON,
TNS_DATA_TYPE_VECTOR):
stmt._requires_define = True
stmt._no_prefetch = True
cursor_impl._create_fetch_var(conn, self.cursor, type_handler,
uses_fetch_info, i, fetch_info)
buf.read_ub4(&num_bytes)
if num_bytes > 0:
buf.skip_raw_bytes_chunked() # current date
buf.skip_ub4() # dcbflag
buf.skip_ub4() # dcbmdbz
buf.skip_ub4() # dcbmnpr
buf.skip_ub4() # dcbmxpr
buf.read_ub4(&num_bytes)
if num_bytes > 0:
buf.skip_raw_bytes_chunked() # dcbqcky
stmt._fetch_info_impls = cursor_impl.fetch_info_impls
stmt._fetch_vars = cursor_impl.fetch_vars
stmt._fetch_var_impls = cursor_impl.fetch_var_impls
stmt._num_columns = cursor_impl._num_columns
stmt._last_output_type_handler = type_handler
cdef int _process_error_info(self, ReadBuffer buf) except -1:
cdef:
BaseThinCursorImpl cursor_impl = self.cursor_impl
BaseThinConnImpl conn_impl = self.conn_impl
object exc_type
Message._process_error_info(self, buf)
if self.error_info.cursor_id != 0:
cursor_impl._statement._cursor_id = self.error_info.cursor_id
if not cursor_impl._statement._is_plsql and not self.in_fetch:
cursor_impl.rowcount = self.error_info.rowcount
elif self.in_fetch and self.row_index > 0:
cursor_impl._statement._requires_define = False
cursor_impl._lastrowid = self.error_info.rowid
cursor_impl._batcherrors = self.error_info.batcherrors
if self.batcherrors and cursor_impl._batcherrors is None:
cursor_impl._batcherrors = []
if self.error_info.num == TNS_ERR_NO_DATA_FOUND and self.in_fetch:
self.error_info.num = 0
cursor_impl._more_rows_to_fetch = False
cursor_impl._last_row_index = 0
cursor_impl._statement._requires_define = False
self.error_occurred = False
elif self.error_info.num == TNS_ERR_ARRAY_DML_ERRORS:
self.error_info.num = 0
self.error_occurred = False
elif self.retry:
self.retry = False
elif cursor_impl._statement._is_query \
and self.error_info.num in (TNS_ERR_VAR_NOT_IN_SELECT_LIST,
TNS_ERR_INCONSISTENT_DATA_TYPES):
self.retry = True
conn_impl._statement_cache.clear_cursor(cursor_impl._statement)
elif self.error_info.num != 0 and self.error_info.cursor_id != 0:
if self.error_info.num not in errors.ERR_INTEGRITY_ERROR_CODES:
conn_impl._statement_cache.clear_cursor(cursor_impl._statement)
cdef int _process_implicit_result(self, ReadBuffer buf) except -1:
cdef:
BaseThinCursorImpl child_cursor_impl
uint32_t i, num_results
object child_cursor
uint8_t num_bytes
self.cursor_impl._implicit_resultsets = []
buf.read_ub4(&num_results)
for i in range(num_results):
buf.read_ub1(&num_bytes)
buf.skip_raw_bytes(num_bytes)
child_cursor = self._create_cursor_from_describe(buf)
child_cursor_impl = child_cursor._impl
buf.read_ub2(&child_cursor_impl._statement._cursor_id)
self.cursor_impl._implicit_resultsets.append(child_cursor)
cdef int _process_io_vector(self, ReadBuffer buf) except -1:
"""
An I/O vector is sent by the database in response to a PL/SQL execute.
It indicates whether binds are IN only, IN/OUT or OUT only.
"""
cdef:
uint16_t i, num_bytes, temp16
uint32_t temp32, num_binds
BindInfo bind_info
buf.skip_ub1() # flag
buf.read_ub2(&temp16) # num requests
buf.read_ub4(&temp32) # num iters
num_binds = temp32 * 256 + temp16
buf.skip_ub4() # num iters this time
buf.skip_ub2() # uac buffer length
buf.read_ub2(&num_bytes) # bit vector for fast fetch
if num_bytes > 0:
buf.skip_raw_bytes(num_bytes)
buf.read_ub2(&num_bytes) # rowid
if num_bytes > 0:
buf.skip_raw_bytes(num_bytes)
self.out_var_impls = []
for i in range(num_binds): # bind directions
bind_info = self.cursor_impl._statement._bind_info_list[i]
buf.read_ub1(&bind_info.bind_dir)
if bind_info.bind_dir == TNS_BIND_DIR_INPUT:
continue
self.out_var_impls.append(bind_info._bind_var_impl)
cdef int _process_message(self, ReadBuffer buf,
uint8_t message_type) except -1:
if message_type == TNS_MSG_TYPE_ROW_HEADER:
self._process_row_header(buf)
elif message_type == TNS_MSG_TYPE_ROW_DATA:
self._process_row_data(buf)
elif message_type == TNS_MSG_TYPE_FLUSH_OUT_BINDS:
self.flush_out_binds = True
self.end_of_response = True
elif message_type == TNS_MSG_TYPE_DESCRIBE_INFO:
buf.skip_raw_bytes_chunked()
self._process_describe_info(buf, self.cursor, self.cursor_impl)
self.out_var_impls = self.cursor_impl.fetch_var_impls
elif message_type == TNS_MSG_TYPE_ERROR:
self._process_error_info(buf)
elif message_type == TNS_MSG_TYPE_BIT_VECTOR:
self._process_bit_vector(buf)
elif message_type == TNS_MSG_TYPE_IO_VECTOR:
self._process_io_vector(buf)
elif message_type == TNS_MSG_TYPE_IMPLICIT_RESULTSET:
self._process_implicit_result(buf)
else:
Message._process_message(self, buf, message_type)
cdef int _process_return_parameters(self, ReadBuffer buf) except -1:
cdef:
uint16_t keyword_num, num_params, num_bytes
uint32_t num_rows, i
uint64_t rowcount
bytes key_value
list rowcounts
buf.read_ub2(&num_params) # al8o4l (ignored)
for i in range(num_params):
buf.skip_ub4()
buf.read_ub2(&num_bytes) # al8txl (ignored)
if num_bytes > 0:
buf.skip_raw_bytes(num_bytes)
buf.read_ub2(&num_params) # num key/value pairs
for i in range(num_params):
buf.read_ub2(&num_bytes) # key
if num_bytes > 0:
key_value = buf.read_bytes()
buf.read_ub2(&num_bytes) # value
if num_bytes > 0:
buf.skip_raw_bytes_chunked()
buf.read_ub2(&keyword_num) # keyword num
if keyword_num == TNS_KEYWORD_NUM_CURRENT_SCHEMA:
self.conn_impl._current_schema = key_value.decode()
elif keyword_num == TNS_KEYWORD_NUM_EDITION:
self.conn_impl._edition = key_value.decode()
buf.read_ub2(&num_bytes) # registration
if num_bytes > 0:
buf.skip_raw_bytes(num_bytes)
if self.arraydmlrowcounts:
buf.read_ub4(&num_rows)
rowcounts = self.cursor_impl._dmlrowcounts = []
for i in range(num_rows):
buf.read_ub8(&rowcount)
rowcounts.append(rowcount)
cdef int _process_row_data(self, ReadBuffer buf) except -1:
cdef:
uint32_t num_rows, pos
ThinVarImpl var_impl
ssize_t i, j
object value
list values
for i, var_impl in enumerate(self.out_var_impls):
if var_impl.is_array:
buf.read_ub4(&var_impl.num_elements_in_array)
for pos in range(var_impl.num_elements_in_array):
value = self._process_column_data(buf, var_impl, pos)
var_impl._values[pos] = value
elif self.cursor_impl._statement._is_returning:
buf.read_ub4(&num_rows)
values = [None] * num_rows
for j in range(num_rows):
values[j] = self._process_column_data(buf, var_impl, j)
var_impl._values[self.row_index] = values
elif self._is_duplicate_data(i):
if self.row_index == 0 and var_impl.outconverter is not None:
value = var_impl._last_raw_value
else:
value = var_impl._values[self.cursor_impl._last_row_index]
var_impl._values[self.row_index] = value
else:
value = self._process_column_data(buf, var_impl,
self.row_index)
var_impl._values[self.row_index] = value
self.row_index += 1
if self.in_fetch:
self.cursor_impl._last_row_index = self.row_index - 1
self.cursor_impl._buffer_rowcount = self.row_index
self.bit_vector = NULL
cdef int _process_row_header(self, ReadBuffer buf) except -1:
cdef uint32_t num_bytes
buf.skip_ub1() # flags
buf.skip_ub2() # num requests
buf.skip_ub4() # iteration number
buf.skip_ub4() # num iters
buf.skip_ub2() # buffer length
buf.read_ub4(&num_bytes)
if num_bytes > 0:
buf.skip_ub1() # skip repeated length
self._get_bit_vector(buf, num_bytes)
buf.read_ub4(&num_bytes)
if num_bytes > 0:
buf.skip_raw_bytes_chunked() # rxhrid
cdef int _write_column_metadata(self, WriteBuffer buf,
list bind_var_impls) except -1:
cdef:
uint32_t buffer_size, cont_flag, lob_prefetch_length
ThinDbObjectTypeImpl typ_impl
uint8_t ora_type_num, flag
ThinVarImpl var_impl
for var_impl in bind_var_impls:
ora_type_num = var_impl.dbtype._ora_type_num
buffer_size = var_impl.buffer_size
if ora_type_num in (TNS_DATA_TYPE_ROWID, TNS_DATA_TYPE_UROWID):
ora_type_num = TNS_DATA_TYPE_VARCHAR
buffer_size = TNS_MAX_UROWID_LENGTH
flag = TNS_BIND_USE_INDICATORS
if var_impl.is_array:
flag |= TNS_BIND_ARRAY
cont_flag = 0
lob_prefetch_length = 0
if ora_type_num in (TNS_DATA_TYPE_BLOB,
TNS_DATA_TYPE_CLOB):
cont_flag = TNS_LOB_PREFETCH_FLAG
elif ora_type_num == TNS_DATA_TYPE_JSON:
cont_flag = TNS_LOB_PREFETCH_FLAG
buffer_size = lob_prefetch_length = TNS_JSON_MAX_LENGTH
elif ora_type_num == TNS_DATA_TYPE_VECTOR:
cont_flag = TNS_LOB_PREFETCH_FLAG
buffer_size = lob_prefetch_length = TNS_VECTOR_MAX_LENGTH
buf.write_uint8(ora_type_num)