/* * PROGRAM: JRD Data Definition Utility * MODULE: dyn_util.epp * DESCRIPTION: Dynamic data definition - utility functions * * The contents of this file are subject to the Interbase Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy * of the License at http://www.Inprise.com/IPL.html * * Software distributed under the License is distributed on an * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express * or implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code was created by Inprise Corporation * and its predecessors. Portions created by Inprise Corporation are * Copyright (C) Inprise Corporation. * * All Rights Reserved. * Contributor(s): ______________________________________. * * 2002-02-24 Sean Leyne - Code Cleanup of old Win 3.1 port (WINDOWS_ONLY) * */ #include "firebird.h" #include #include #include "../jrd/common.h" #include #include "../jrd/jrd.h" #include "../jrd/tra.h" #include "../jrd/scl.h" #include "../jrd/drq.h" #include "../jrd/flags.h" #include "../jrd/ibase.h" #include "../jrd/lls.h" #include "../jrd/all.h" #include "../jrd/met.h" #include "../jrd/btr.h" #include "../jrd/intl.h" #include "../jrd/dyn.h" #include "../jrd/ods.h" #include "../jrd/all_proto.h" #include "../jrd/blb_proto.h" #include "../jrd/cmp_proto.h" #include "../jrd/dyn_proto.h" #include "../jrd/dyn_md_proto.h" #include "../jrd/dyn_ut_proto.h" #include "../jrd/err_proto.h" #include "../jrd/exe_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/inf_proto.h" #include "../jrd/intl_proto.h" #include "../jrd/isc_f_proto.h" #include "../jrd/thd.h" #include "../jrd/vio_proto.h" using namespace Jrd; DATABASE DB = STATIC "ODS.RDB"; static const SCHAR gen_id_blr1[] = { blr_version5, blr_begin, blr_message, 0, 1, 0, blr_int64, 0, blr_begin, blr_send, 0, blr_begin, blr_assignment, blr_gen_id }; static const SCHAR gen_id_blr2[] = { blr_literal, blr_long, 0, 1, 0, 0, 0, blr_parameter, 0, 0, 0, blr_end, blr_end, blr_end, blr_eoc }; static const UCHAR prot_blr[] = { blr_version5, blr_begin, blr_message, 1, 1, 0, blr_short, 0, blr_message, 0, 2, 0, blr_cstring, 32, 0, blr_cstring, 32, 0, blr_receive, 0, blr_begin, blr_send, 1, blr_begin, blr_assignment, blr_prot_mask, blr_parameter, 0, 0, 0, blr_parameter, 0, 1, 0, blr_parameter, 1, 0, 0, blr_end, blr_end, blr_end, blr_eoc }; SINT64 DYN_UTIL_gen_unique_id(thread_db* tdbb, Global* gbl, SSHORT id, const SCHAR* generator_name, jrd_req** request) { /************************************** * * D Y N _ U T I L _g e n _ u n i q u e _ i d * ************************************** * * Functional description * Generate a unique id using a generator. * **************************************/ SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* req_handle = CMP_find_request(tdbb, id, DYN_REQUESTS); if (!req_handle) { /* 32 bytes allocated for size of of a metadata name */ const size_t blr_size = sizeof(gen_id_blr1) + sizeof(gen_id_blr2) + 1 + MAX_SQL_IDENTIFIER_SIZE; SCHAR blr[blr_size]; fb_assert(strlen(generator_name) < MAX_SQL_IDENTIFIER_SIZE); char* p = blr; memcpy(p, gen_id_blr1, sizeof(gen_id_blr1)); p += sizeof(gen_id_blr1); *p++ = strlen(generator_name); strcpy(p, generator_name); p += p[-1]; memcpy(p, gen_id_blr2, sizeof(gen_id_blr2)); p += sizeof(gen_id_blr2); req_handle = CMP_compile2(tdbb, reinterpret_cast(blr), TRUE); } *request = req_handle; SINT64 value; EXE_start(tdbb, req_handle, dbb->dbb_sys_trans); EXE_receive(tdbb, req_handle, 0, sizeof(value), (UCHAR*)&value); EXE_unwind(tdbb, req_handle); *request = NULL; if (!DYN_REQUEST(id)) { DYN_REQUEST(id) = req_handle; } return value; } void DYN_UTIL_generate_constraint_name( thread_db* tdbb, Global* gbl, TEXT* buffer) { /************************************** * * D Y N _ U T I L _g e n e r a t e _ c o n s t r a i n t _ n a m e * ************************************** * * Functional description * Generate a name unique to RDB$RELATION_CONSTRAINTS. * **************************************/ SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* request = NULL; SSHORT id = -1; try { bool found = false; do { id = drq_g_nxt_con; sprintf(buffer, "INTEG_%" QUADFORMAT "d", (SINT64) DYN_UTIL_gen_unique_id(tdbb, gbl, drq_g_nxt_con, "RDB$CONSTRAINT_NAME", &request)); request = CMP_find_request(tdbb, drq_f_nxt_con, DYN_REQUESTS); id = drq_f_nxt_con; found = false; FOR(REQUEST_HANDLE request) FIRST 1 X IN RDB$RELATION_CONSTRAINTS WITH X.RDB$CONSTRAINT_NAME EQ buffer if (!DYN_REQUEST(drq_f_nxt_con)) DYN_REQUEST(drq_f_nxt_con) = request; found = true; END_FOR; if (!DYN_REQUEST(drq_f_nxt_con)) DYN_REQUEST(drq_f_nxt_con) = request; request = NULL; } while (found); } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, id); DYN_error_punt(true, 131, NULL, NULL, NULL, NULL, NULL); /* msg 131: "Generation of constraint name failed" */ } } void DYN_UTIL_generate_field_name( thread_db* tdbb, Global* gbl, TEXT* buffer) { /************************************** * * D Y N _ U T I L _g e n e r a t e _ f i e l d _ n a m e * ************************************** * * Functional description * Generate a name unique to RDB$FIELDS. * **************************************/ SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* request = NULL; SSHORT id = -1; try { bool found = false; do { id = drq_g_nxt_fld; sprintf(buffer, "RDB$%" QUADFORMAT "d", (SINT64) DYN_UTIL_gen_unique_id(tdbb, gbl, drq_g_nxt_fld, "RDB$FIELD_NAME", &request)); request = CMP_find_request(tdbb, drq_f_nxt_fld, DYN_REQUESTS); id = drq_f_nxt_fld; found = false; FOR(REQUEST_HANDLE request) FIRST 1 X IN RDB$FIELDS WITH X.RDB$FIELD_NAME EQ buffer if (!DYN_REQUEST(drq_f_nxt_fld)) DYN_REQUEST(drq_f_nxt_fld) = request; found = true; END_FOR; if (!DYN_REQUEST(drq_f_nxt_fld)) DYN_REQUEST(drq_f_nxt_fld) = request; request = NULL; } while (found); } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, id); DYN_error_punt(true, 81, NULL, NULL, NULL, NULL, NULL); /* msg 81: "Generation of field name failed" */ } } void DYN_UTIL_generate_field_position( thread_db* tdbb, Global* gbl, const TEXT* relation_name, SLONG* field_pos) { /************************************** * * D Y N _ U T I L _g e n e r a t e _ f i e l d _ p o s i t i o n * ************************************** * * Functional description * Generate a field position if not specified * **************************************/ SLONG field_position = -1; if (!relation_name) return; SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* request = NULL; try { request = CMP_find_request(tdbb, drq_l_fld_pos, DYN_REQUESTS); FOR(REQUEST_HANDLE request) X IN RDB$RELATION_FIELDS WITH X.RDB$RELATION_NAME EQ relation_name if (!DYN_REQUEST(drq_l_fld_pos)) DYN_REQUEST(drq_l_fld_pos) = request; if (X.RDB$FIELD_POSITION.NULL) continue; field_position = MAX(X.RDB$FIELD_POSITION, field_position); END_FOR; *field_pos = field_position; } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, -1); DYN_error_punt(true, 162, NULL, NULL, NULL, NULL, NULL); /* msg 162: "Looking up field position failed" */ } } void DYN_UTIL_generate_index_name(thread_db* tdbb, Global* gbl, TEXT* buffer, UCHAR verb) { /************************************** * * D Y N _ U T I L _g e n e r a t e _ i n d e x _ n a m e * ************************************** * * Functional description * Generate a name unique to RDB$INDICES. * **************************************/ SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* request = NULL; SSHORT id = -1; try { bool found = false; do { const SCHAR* format; if (verb == isc_dyn_def_primary_key) format = "RDB$PRIMARY%" QUADFORMAT "d"; else if (verb == isc_dyn_def_foreign_key) format = "RDB$FOREIGN%" QUADFORMAT "d"; else format = "RDB$%" QUADFORMAT "d"; id = drq_g_nxt_idx; sprintf(buffer, format, (SINT64) DYN_UTIL_gen_unique_id(tdbb, gbl, drq_g_nxt_idx, "RDB$INDEX_NAME", &request)); request = CMP_find_request(tdbb, drq_f_nxt_idx, DYN_REQUESTS); id = drq_f_nxt_idx; found = false; FOR(REQUEST_HANDLE request) FIRST 1 X IN RDB$INDICES WITH X.RDB$INDEX_NAME EQ buffer if (!DYN_REQUEST(drq_f_nxt_idx)) DYN_REQUEST(drq_f_nxt_idx) = request; found = true; END_FOR; if (!DYN_REQUEST(drq_f_nxt_idx)) DYN_REQUEST(drq_f_nxt_idx) = request; request = NULL; } while (found); } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, id); DYN_error_punt(true, 82, NULL, NULL, NULL, NULL, NULL); /* msg 82: "Generation of index name failed" */ } } void DYN_UTIL_generate_trigger_name( thread_db* tdbb, Global* gbl, TEXT* buffer) { /************************************** * * D Y N _ U T I L _g e n e r a t e _ t r i g g e r _ n a m e * ************************************** * * Functional description * Generate a name unique to RDB$TRIGGERS. * **************************************/ SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* request = NULL; SSHORT id = -1; try { bool found = false; do { id = drq_g_nxt_trg; sprintf(buffer, "CHECK_%" QUADFORMAT "d", (SINT64) DYN_UTIL_gen_unique_id(tdbb, gbl, drq_g_nxt_trg, "RDB$TRIGGER_NAME", &request)); request = CMP_find_request(tdbb, drq_f_nxt_trg, DYN_REQUESTS); id = drq_f_nxt_trg; found = false; FOR(REQUEST_HANDLE request) FIRST 1 X IN RDB$TRIGGERS WITH X.RDB$TRIGGER_NAME EQ buffer if (!DYN_REQUEST(drq_f_nxt_trg)) DYN_REQUEST(drq_f_nxt_trg) = request; found = true; END_FOR; if (!DYN_REQUEST(drq_f_nxt_trg)) DYN_REQUEST(drq_f_nxt_trg) = request; request = NULL; } while (found); } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, id); DYN_error_punt(true, 83, NULL, NULL, NULL, NULL, NULL); /* msg 83: "Generation of trigger name failed" */ } } bool DYN_UTIL_get_prot(thread_db* tdbb, Global* gbl, const SCHAR* rname, const SCHAR* fname, SecurityClass::flags_t* prot_mask) { /************************************** * * D Y N _ U T I L _g e t _ p r o t * ************************************** * * Functional description * Get protection mask for relation or relation_field * **************************************/ struct { SqlIdentifier relation_name; SqlIdentifier field_name; } in_msg; SET_TDBB(tdbb); jrd_req* request = CMP_find_request(tdbb, drq_l_prot_mask, DYN_REQUESTS); try { if (!request) { request = CMP_compile2(tdbb, prot_blr, TRUE); } gds__vtov(rname, in_msg.relation_name, sizeof(in_msg.relation_name)); gds__vtov(fname, in_msg.field_name, sizeof(in_msg.field_name)); EXE_start(tdbb, request, gbl->gbl_transaction); EXE_send(tdbb, request, 0, sizeof(in_msg), (UCHAR*)&in_msg); EXE_receive(tdbb, request, 1, sizeof(USHORT), (UCHAR*)prot_mask); DYN_rundown_request(request, drq_l_prot_mask); } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, drq_l_prot_mask); return false; } return true; } void DYN_UTIL_store_check_constraints(thread_db* tdbb, Global* gbl, const TEXT* constraint_name, const TEXT* trigger_name) { /************************************** * * D Y N _ U T I L _s t o r e _ c h e c k _ c o n s t r a i n t s * ************************************** * * Functional description * **************************************/ SET_TDBB(tdbb); Database* dbb = tdbb->tdbb_database; jrd_req* request = CMP_find_request(tdbb, drq_s_chk_con, DYN_REQUESTS); try { STORE(REQUEST_HANDLE request TRANSACTION_HANDLE gbl->gbl_transaction) CHK IN RDB$CHECK_CONSTRAINTS strcpy(CHK.RDB$CONSTRAINT_NAME, constraint_name); strcpy(CHK.RDB$TRIGGER_NAME, trigger_name); END_STORE; if (!DYN_REQUEST(drq_s_chk_con)) { DYN_REQUEST(drq_s_chk_con) = request; } } // try catch (const std::exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); DYN_rundown_request(request, drq_s_chk_con); DYN_error_punt(true, 122, NULL, NULL, NULL, NULL, NULL); /* msg 122: "STORE RDB$CHECK_CONSTRAINTS failed" */ } }