1##############################################################################
2# Copyright (c) 2011-2018, Hajime Nakagami<nakagami@gmail.com>
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7#
8# * Redistributions of source code must retain the above copyright notice, this
9#   list of conditions and the following disclaimer.
10#
11# * Redistributions in binary form must reproduce the above copyright notice,
12#  this list of conditions and the following disclaimer in the documentation
13#  and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25#
26# Python DB-API 2.0 module for Firebird.
27##############################################################################
28import sys
29
30PYTHON_MAJOR_VER = sys.version_info[0]
31
32DEFAULT_CHARSET = 'UTF8'
33
34ISC_TIME_SECONDS_PRECISION = 10000
35MAX_CHAR_LENGTH = 32767
36BLOB_SEGMENT_SIZE = 32000
37
38DESCRIPTION_NAME = 0
39DESCRIPTION_TYPE_CODE = 1
40DESCRIPTION_DISPLAY_SIZE = 2
41DESCRIPTION_INTERNAL_SIZE = 3
42DESCRIPTION_PRECISION = 4
43DESCRIPTION_SCALE = 5
44DESCRIPTION_NULL_OK = 6
45
46isc_info_end = 1
47isc_info_truncated = 2
48isc_info_error = 3
49isc_info_data_not_ready = 4
50isc_info_length = 126
51isc_info_flag_end = 127
52
53isc_info_db_id = 4
54isc_info_reads = 5
55isc_info_writes = 6
56isc_info_fetches = 7
57isc_info_marks = 8
58isc_info_implementation = 11
59isc_info_version = 12
60isc_info_base_level = 13
61isc_info_page_size = 14
62isc_info_num_buffers = 15
63isc_info_limbo = 16
64isc_info_current_memory = 17
65isc_info_max_memory = 18
66isc_info_window_turns = 19
67isc_info_license = 20
68isc_info_allocation = 21
69isc_info_attachment_id = 22
70isc_info_read_seq_count = 23
71isc_info_read_idx_count = 24
72isc_info_insert_count = 25
73isc_info_update_count = 26
74isc_info_delete_count = 27
75isc_info_backout_count = 28
76isc_info_purge_count = 29
77isc_info_expunge_count = 30
78isc_info_sweep_interval = 31
79isc_info_ods_version = 32
80isc_info_ods_minor_version = 33
81isc_info_no_reserve = 34
82isc_info_logfile = 35
83isc_info_cur_logfile_name = 36
84isc_info_cur_log_part_offset = 37
85isc_info_num_wal_buffers = 38
86isc_info_wal_buffer_size = 39
87isc_info_wal_ckpt_length = 40
88isc_info_wal_cur_ckpt_interval = 41
89isc_info_wal_prv_ckpt_fname = 42
90isc_info_wal_prv_ckpt_poffset = 43
91isc_info_wal_recv_ckpt_fname = 44
92isc_info_wal_recv_ckpt_poffset = 45
93isc_info_wal_grpc_wait_usecs = 47
94isc_info_wal_num_io = 48
95isc_info_wal_avg_io_size = 49
96isc_info_wal_num_commits = 50
97isc_info_wal_avg_grpc_size = 51
98isc_info_forced_writes = 52
99isc_info_user_names = 53
100isc_info_page_errors = 54
101isc_info_record_errors = 55
102isc_info_bpage_errors = 56
103isc_info_dpage_errors = 57
104isc_info_ipage_errors = 58
105isc_info_ppage_errors = 59
106isc_info_tpage_errors = 60
107isc_info_set_page_buffers = 61
108isc_info_db_sql_dialect = 62
109isc_info_db_read_only = 63
110isc_info_db_size_in_pages = 64
111isc_info_att_charset = 101
112isc_info_db_class = 102
113isc_info_firebird_version = 103
114isc_info_oldest_transaction = 104
115isc_info_oldest_active = 105
116isc_info_oldest_snapshot = 106
117isc_info_next_transaction = 107
118isc_info_db_provider = 108
119isc_info_active_transactions = 109
120isc_info_active_tran_count = 110
121isc_info_creation_date = 111
122isc_info_db_file_size = 112
123
124# isc_info_sql_records items
125isc_info_req_select_count = 13
126isc_info_req_insert_count = 14
127isc_info_req_update_count = 15
128isc_info_req_delete_count = 16
129
130isc_info_svc_svr_db_info = 50
131isc_info_svc_get_license = 51
132isc_info_svc_get_license_mask = 52
133isc_info_svc_get_config = 53
134isc_info_svc_version = 54
135isc_info_svc_server_version = 55
136isc_info_svc_implementation = 56
137isc_info_svc_capabilities = 57
138isc_info_svc_user_dbpath = 58
139isc_info_svc_get_env = 59
140isc_info_svc_get_env_lock = 60
141isc_info_svc_get_env_msg = 61
142isc_info_svc_line = 62
143isc_info_svc_to_eof = 63
144isc_info_svc_timeout = 64
145isc_info_svc_get_licensed_users = 65
146isc_info_svc_limbo_trans = 66
147isc_info_svc_running = 67
148isc_info_svc_get_users = 68
149
150SQL_TYPE_TEXT = 452
151SQL_TYPE_VARYING = 448
152SQL_TYPE_SHORT = 500
153SQL_TYPE_LONG = 496
154SQL_TYPE_FLOAT = 482
155SQL_TYPE_DOUBLE = 480
156SQL_TYPE_D_FLOAT = 530
157SQL_TYPE_TIMESTAMP = 510
158SQL_TYPE_BLOB = 520
159SQL_TYPE_ARRAY = 540
160SQL_TYPE_QUAD = 550
161SQL_TYPE_TIME = 560
162SQL_TYPE_DATE = 570
163SQL_TYPE_INT64 = 580
164SQL_TYPE_INT128 = 32752
165SQL_TYPE_TIMESTAMP_TZ = 32754
166SQL_TYPE_TIME_TZ = 32756
167SQL_TYPE_DEC_FIXED = 32758  # OBSOLATED
168SQL_TYPE_DEC64 = 32760
169SQL_TYPE_DEC128 = 32762
170SQL_TYPE_BOOLEAN = 32764
171SQL_TYPE_NULL = 32766
172
173
174ISOLATION_LEVEL_READ_COMMITED_LEGACY = 0
175ISOLATION_LEVEL_READ_COMMITED = 1
176ISOLATION_LEVEL_REPEATABLE_READ = 2
177ISOLATION_LEVEL_SNAPSHOT = ISOLATION_LEVEL_REPEATABLE_READ
178ISOLATION_LEVEL_SERIALIZABLE = 3
179ISOLATION_LEVEL_READ_COMMITED_RO = 4
180
181# Database Parameter Block parameter
182isc_dpb_version1 = 1
183isc_dpb_version2 = 2
184isc_dpb_cdd_pathname = 1
185isc_dpb_allocation = 2
186isc_dpb_journal = 3
187isc_dpb_page_size = 4
188isc_dpb_num_buffers = 5
189isc_dpb_buffer_length = 6
190isc_dpb_debug = 7
191isc_dpb_garbage_collect = 8
192isc_dpb_verify = 9
193isc_dpb_sweep = 10
194isc_dpb_enable_journal = 11
195isc_dpb_disable_journal = 12
196isc_dpb_dbkey_scope = 13
197isc_dpb_number_of_users = 14
198isc_dpb_trace = 15
199isc_dpb_no_garbage_collect = 16
200isc_dpb_damaged = 17
201isc_dpb_license = 18
202isc_dpb_sys_user_name = 19
203isc_dpb_encrypt_key = 20
204isc_dpb_activate_shadow = 21
205isc_dpb_sweep_interval = 22
206isc_dpb_delete_shadow = 23
207isc_dpb_force_write = 24
208isc_dpb_begin_log = 25
209isc_dpb_quit_log = 26
210isc_dpb_no_reserve = 27
211isc_dpb_user_name = 28
212isc_dpb_password = 29
213isc_dpb_password_enc = 30
214isc_dpb_sys_user_name_enc = 31
215isc_dpb_interp = 32
216isc_dpb_online_dump = 33
217isc_dpb_old_file_size = 34
218isc_dpb_old_num_files = 35
219isc_dpb_old_file = 36
220isc_dpb_old_start_page = 37
221isc_dpb_old_start_seqno = 38
222isc_dpb_old_start_file = 39
223isc_dpb_drop_walfile = 40
224isc_dpb_old_dump_id = 41
225isc_dpb_wal_backup_dir = 42
226isc_dpb_wal_chkptlen = 43
227isc_dpb_wal_numbufs = 44
228isc_dpb_wal_bufsize = 45
229isc_dpb_wal_grp_cmt_wait = 46
230isc_dpb_lc_messages = 47
231isc_dpb_lc_ctype = 48
232isc_dpb_cache_manager = 49
233isc_dpb_shutdown = 50
234isc_dpb_online = 51
235isc_dpb_shutdown_delay = 52
236isc_dpb_reserved = 53
237isc_dpb_overwrite = 54
238isc_dpb_sec_attach = 55
239isc_dpb_disable_wal = 56
240isc_dpb_connect_timeout = 57
241isc_dpb_dummy_packet_interval = 58
242isc_dpb_gbak_attach = 59
243isc_dpb_sql_role_name = 60
244isc_dpb_set_page_buffers = 61
245isc_dpb_working_directory = 62
246isc_dpb_sql_dialect = 63
247isc_dpb_set_db_readonly = 64
248isc_dpb_set_db_sql_dialect = 65
249isc_dpb_gfix_attach = 66
250isc_dpb_gstat_attach = 67
251isc_dpb_set_db_charset = 68
252isc_dpb_gsec_attach = 69
253isc_dpb_address_path = 70
254isc_dpb_process_id = 71
255isc_dpb_no_db_triggers = 72
256isc_dpb_trusted_auth = 73
257isc_dpb_process_name = 74
258isc_dpb_trusted_role = 75
259isc_dpb_org_filename = 76
260isc_dpb_utf8_filename = 77
261isc_dpb_ext_call_depth = 78
262isc_dpb_auth_block = 79
263isc_dpb_client_version = 80
264isc_dpb_remote_protocol = 81
265isc_dpb_host_name = 82
266isc_dpb_os_user = 83
267isc_dpb_specific_auth_data = 84
268isc_dpb_auth_plugin_list = 85
269isc_dpb_auth_plugin_name = 86
270isc_dpb_config = 87
271isc_dpb_nolinger = 88
272isc_dpb_reset_icu = 89
273isc_dpb_map_attach = 90
274isc_dpb_session_time_zone = 91
275
276# Transaction Parameter Block parameter
277isc_tpb_version1 = 1
278isc_tpb_version3 = 3
279isc_tpb_consistency = 1
280isc_tpb_concurrency = 2
281isc_tpb_shared = 3
282isc_tpb_protected = 4
283isc_tpb_exclusive = 5
284isc_tpb_wait = 6
285isc_tpb_nowait = 7
286isc_tpb_read = 8
287isc_tpb_write = 9
288isc_tpb_lock_read = 10
289isc_tpb_lock_write = 11
290isc_tpb_verb_time = 12
291isc_tpb_commit_time = 13
292isc_tpb_ignore_limbo = 14
293isc_tpb_read_committed = 15
294isc_tpb_autocommit = 16
295isc_tpb_rec_version = 17
296isc_tpb_no_rec_version = 18
297isc_tpb_restart_requests = 19
298isc_tpb_no_auto_undo = 20
299isc_tpb_lock_timeout = 21
300
301
302# Service Parameter Block parameter
303isc_spb_version1 = 1
304isc_spb_current_version = 2
305isc_spb_version = isc_spb_current_version
306isc_spb_user_name = 28              # isc_dpb_user_name
307isc_spb_sys_user_name = 19          # isc_dpb_sys_user_name
308isc_spb_sys_user_name_enc = 31      # isc_dpb_sys_user_name_enc
309isc_spb_password = 29               # isc_dpb_password
310isc_spb_password_enc = 30           # isc_dpb_password_enc
311isc_spb_command_line = 105
312isc_spb_dbname = 106
313isc_spb_verbose = 107
314isc_spb_options = 108
315isc_spb_address_path = 109
316isc_spb_process_id = 110
317isc_spb_trusted_auth = 111
318isc_spb_process_name = 112
319isc_spb_trusted_role = 113
320isc_spb_connect_timeout = 57        # isc_dpb_connect_timeout
321isc_spb_dummy_packet_interval = 58  # isc_dpb_dummy_packet_interval
322isc_spb_sql_role_name = 60          # isc_dpb_sql_role_name
323
324# isc_action_svc_properties params
325isc_spb_prp_page_buffers = 5
326isc_spb_prp_sweep_interval = 6
327isc_spb_prp_shutdown_db = 7
328isc_spb_prp_deny_new_attachments = 9
329isc_spb_prp_deny_new_transactions = 10
330isc_spb_prp_reserve_space = 11
331isc_spb_prp_write_mode = 12
332isc_spb_prp_access_mode = 13
333isc_spb_prp_set_sql_dialect = 14
334isc_spb_prp_activate = 0x0100
335isc_spb_prp_db_online = 0x0200
336isc_spb_prp_force_shutdown = 41
337isc_spb_prp_attachments_shutdown = 42
338isc_spb_prp_transactions_shutdown = 43
339isc_spb_prp_shutdown_mode = 44
340isc_spb_prp_online_mode = 45
341
342# backup
343isc_spb_bkp_file = 5
344isc_spb_bkp_factor = 6
345isc_spb_bkp_length = 7
346isc_spb_bkp_ignore_checksums = 0x01
347isc_spb_bkp_ignore_limbo = 0x02
348isc_spb_bkp_metadata_only = 0x04
349isc_spb_bkp_no_garbage_collect = 0x08
350isc_spb_bkp_old_descriptions = 0x10
351isc_spb_bkp_non_transportable = 0x20
352isc_spb_bkp_convert = 0x40
353isc_spb_bkp_expand = 0x8
354
355# restore
356isc_spb_res_buffers = 9
357isc_spb_res_page_size = 10
358isc_spb_res_length = 11
359isc_spb_res_access_mode = 12
360isc_spb_res_deactivate_idx = 0x0100
361isc_spb_res_no_shadow = 0x0200
362isc_spb_res_no_validity = 0x0400
363isc_spb_res_one_at_a_time = 0x0800
364isc_spb_res_replace = 0x1000
365isc_spb_res_create = 0x2000
366isc_spb_res_use_all_space = 0x4000
367
368# trace
369isc_spb_trc_id = 1
370isc_spb_trc_name = 2
371isc_spb_trc_cfg = 3
372
373# isc_info_svc_svr_db_info params
374isc_spb_num_att = 5
375isc_spb_num_db = 6
376
377# isc_info_svc_db_stats params
378isc_spb_sts_data_pages = 0x01
379isc_spb_sts_db_log = 0x02
380isc_spb_sts_hdr_pages = 0x04
381isc_spb_sts_idx_pages = 0x08
382isc_spb_sts_sys_relations = 0x10
383isc_spb_sts_record_versions = 0x20
384isc_spb_sts_table = 0x40
385isc_spb_sts_nocreation = 0x80
386
387# isc_action_svc_repair params
388isc_spb_rpr_validate_db = 0x01
389isc_spb_rpr_sweep_db = 0x02
390isc_spb_rpr_mend_db = 0x04
391isc_spb_rpr_list_limbo_trans = 0x08
392isc_spb_rpr_check_db = 0x10
393isc_spb_rpr_ignore_checksum = 0x20
394isc_spb_rpr_kill_shadows = 0x40
395isc_spb_rpr_full = 0x80
396
397# Service Action Items
398isc_action_svc_backup = 1
399isc_action_svc_restore = 2
400isc_action_svc_repair = 3
401isc_action_svc_add_user = 4
402isc_action_svc_delete_user = 5
403isc_action_svc_modify_user = 6
404isc_action_svc_display_user = 7
405isc_action_svc_properties = 8
406isc_action_svc_add_license = 9
407isc_action_svc_remove_license = 10
408isc_action_svc_db_stats = 11
409isc_action_svc_get_ib_log = 12
410isc_action_svc_get_fb_log = 12
411isc_action_svc_nbak = 20
412isc_action_svc_nrest = 21
413isc_action_svc_trace_start = 22
414isc_action_svc_trace_stop = 23
415isc_action_svc_trace_suspend = 24
416isc_action_svc_trace_resume = 25
417isc_action_svc_trace_list = 26
418isc_action_svc_set_mapping = 27
419isc_action_svc_drop_mapping = 28
420isc_action_svc_display_user_adm = 29
421isc_action_svc_last = 30
422
423# Transaction informatino items
424isc_info_tra_id = 4
425isc_info_tra_oldest_interesting = 5
426isc_info_tra_oldest_snapshot = 6
427isc_info_tra_oldest_active = 7
428isc_info_tra_isolation = 8
429isc_info_tra_access = 9
430isc_info_tra_lock_timeout = 10
431
432# SQL information items
433isc_info_sql_select = 4
434isc_info_sql_bind = 5
435isc_info_sql_num_variables = 6
436isc_info_sql_describe_vars = 7
437isc_info_sql_describe_end = 8
438isc_info_sql_sqlda_seq = 9
439isc_info_sql_message_seq = 10
440isc_info_sql_type = 11
441isc_info_sql_sub_type = 12
442isc_info_sql_scale = 13
443isc_info_sql_length = 14
444isc_info_sql_null_ind = 15
445isc_info_sql_field = 16
446isc_info_sql_relation = 17
447isc_info_sql_owner = 18
448isc_info_sql_alias = 19
449isc_info_sql_sqlda_start = 20
450isc_info_sql_stmt_type = 21
451isc_info_sql_get_plan = 22
452isc_info_sql_records = 23
453isc_info_sql_batch_fetch = 24
454
455isc_info_sql_stmt_select = 1
456isc_info_sql_stmt_insert = 2
457isc_info_sql_stmt_update = 3
458isc_info_sql_stmt_delete = 4
459isc_info_sql_stmt_ddl = 5
460isc_info_sql_stmt_get_segment = 6
461isc_info_sql_stmt_put_segment = 7
462isc_info_sql_stmt_exec_procedure = 8
463isc_info_sql_stmt_start_trans = 9
464isc_info_sql_stmt_commit = 10
465isc_info_sql_stmt_rollback = 11
466isc_info_sql_stmt_select_for_upd = 12
467isc_info_sql_stmt_set_generator = 13
468isc_info_sql_stmt_savepoint = 14
469
470isc_arg_end = 0
471isc_arg_gds = 1
472isc_arg_string = 2
473isc_arg_cstring = 3
474isc_arg_number = 4
475isc_arg_interpreted = 5
476isc_arg_vms = 6
477isc_arg_unix = 7
478isc_arg_domain = 8
479isc_arg_dos = 9
480isc_arg_mpexl = 10
481isc_arg_mpexl_ipc = 11
482isc_arg_next_mach = 15
483isc_arg_netware = 16
484isc_arg_win32 = 17
485isc_arg_warning = 18
486isc_arg_sql_state = 19
487
488# Protocol Types (accept_type)
489ptype_batch_send = 3    # Batch sends, no asynchrony
490ptype_out_of_band = 4   # Batch sends w/ out of band notification
491ptype_lazy_send = 5     # Deferred packets delivery
492
493PROTOCOL_VERSION10 = 10
494PROTOCOL_VERSION11 = 11
495PROTOCOL_VERSION12 = 12
496PROTOCOL_VERSION13 = 13
497
498CNCT_user = 1
499CNCT_passwd = 2
500CNCT_host = 4
501CNCT_group = 5
502CNCT_user_verification = 6
503CNCT_specific_data = 7
504CNCT_plugin_name = 8
505CNCT_login = 9
506CNCT_plugin_list = 10
507CNCT_client_crypt = 11
508
509DSQL_close = 1
510DSQL_drop = 2
511
512charset_map = {
513    # DB CHAR SET NAME    :   PYTHON CODEC NAME (CANONICAL)
514    # --------------------------------------------------------------------------
515    'OCTETS':   None,   # Allow to pass through unchanged.
516    'UNICODE_FSS':   'utf_8',
517    'UTF8':   'utf_8',  # (Firebird 2.0+)
518    'SJIS_0208':   'shift_jis',
519    'EUCJ_0208':   'euc_jp',
520    'DOS737':   'cp737',
521    'DOS437':   'cp437',
522    'DOS850':   'cp850',
523    'DOS865':   'cp865',
524    'DOS860':   'cp860',
525    'DOS863':   'cp863',
526    'DOS775':   'cp775',
527    'DOS862':   'cp862',
528    'DOS864':   'cp864',
529    'ISO8859_1':   'iso8859_1',
530    'ISO8859_2':   'iso8859_2',
531    'ISO8859_3':   'iso8859_3',
532    'ISO8859_4':   'iso8859_4',
533    'ISO8859_5':   'iso8859_5',
534    'ISO8859_6':   'iso8859_6',
535    'ISO8859_7':   'iso8859_7',
536    'ISO8859_8':   'iso8859_8',
537    'ISO8859_9':   'iso8859_9',
538    'ISO8859_13':   'iso8859_13',
539    'KSC_5601':   'euc_kr',
540    'DOS852':   'cp852',
541    'DOS857':   'cp857',
542    'DOS861':   'cp861',
543    'DOS866':   'cp866',
544    'DOS869':   'cp869',
545    'WIN1250':   'cp1250',
546    'WIN1251':   'cp1251',
547    'WIN1252':   'cp1252',
548    'WIN1253':   'cp1253',
549    'WIN1254':   'cp1254',
550    'BIG_5':   'big5',
551    'GB_2312':   'gb2312',
552    'WIN1255':   'cp1255',
553    'WIN1256':   'cp1256',
554    'WIN1257':   'cp1257',
555    'KOI8R':   'koi8_r',   # (Firebird 2.0+)
556    'KOI8U':   'koi8_u',   # (Firebird 2.0+)
557    'WIN1258':   'cp1258',  # (Firebird 2.0+)
558}
559