1400
1400
XTTableHPtr tab;
1402
1402
/* Find the table... */
1403
pushsr_(tab, xt_heap_release, xt_use_table(self, tab_name, TRUE));
1403
pushsr_(tab, xt_heap_release, xt_use_table(self, tab_name, FALSE, TRUE));
1405
1405
/* Clone the foreign key definitions: */
1406
1406
if (tab && tab->tab_dic.dic_table) {
1758
1758
search_key.sk_key_value.sv_flags = 0;
1759
search_key.sk_key_value.sv_record = 0;
1759
search_key.sk_key_value.sv_rec_id = 0;
1760
search_key.sk_key_value.sv_row_id = 0;
1760
1761
search_key.sk_key_value.sv_key = search_key.sk_key_buf;
1761
1762
search_key.sk_key_value.sv_length = myxt_create_foreign_key_from_row(loc_ind, search_key.sk_key_buf, before_buf, ind, &no_null);
1762
1763
search_key.sk_on_key = FALSE;
1767
1768
if (after_buf) {
1768
if (!(after_key = (xtWord1 *) xt_sys_malloc(XT_IDX_MAX_KEY_SIZE)))
1769
if (!(after_key = (xtWord1 *) xt_malloc_ns(XT_INDEX_MAX_KEY_SIZE)))
1770
1771
after_key_len = myxt_create_foreign_key_from_row(loc_ind, after_key, after_buf, ind, NULL);
1781
1782
/* Search for the key in the child (referencing) table: */
1782
if (!(ot = xt_open_table_from_pool(tr_fkey->co_table->dt_table, thread)))
1783
if (!(ot = xt_db_open_table_using_tab(tr_fkey->co_table->dt_table, thread)))
1786
1787
if (!xt_idx_search(ot, ind, &search_key))
1789
while (ot->ot_curr_rec && search_key.sk_on_key) {
1790
switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
1790
while (ot->ot_curr_rec_id && search_key.sk_on_key) {
1791
switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec_id, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
1792
if (!xt_xn_wait_for_xact(thread, xn_id))
1793
if (!xt_xn_wait_for_xact(thread, xn_id, FALSE))
1800
1801
case XT_KEY_ACTION_CASCADE:
1801
1802
if (after_buf) {
1802
1803
/* Do a cascaded update: */
1803
if (!xt_tab_load_record(ot, ot->ot_curr_rec, &after_info))
1804
if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &after_info))
1806
1807
if (!myxt_create_row_from_key(ot, ind, after_key, after_key_len, after_info.ib_db.db_data))
1822
1823
case XT_KEY_ACTION_SET_NULL:
1823
if (!xt_tab_load_record(ot, ot->ot_curr_rec, &after_info))
1824
if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &after_info))
1826
1827
myxt_set_null_row_from_key(ot, ind, after_info.ib_db.db_data);
1843
1844
/* No matching children, all OK: */
1844
xt_return_table_to_pool(ot);
1845
xt_db_return_table_to_pool_ns(ot);
1847
1848
xt_ib_free(NULL, &after_info);
1849
xt_sys_free(after_key);
1850
xt_free_ns(after_key);
1853
xt_return_table_to_pool(ot);
1854
xt_db_return_table_to_pool_ns(ot);
1856
1857
xt_ib_free(NULL, &after_info);
1858
xt_sys_free(after_key);
1859
xt_free_ns(after_key);
1870
1871
if (!tr_fkey->getIndexPtr())
1873
if (!(ot = xt_open_table_from_pool(tr_fkey->co_table->dt_table, self)))
1874
if (!(ot = xt_db_open_table_using_tab(tr_fkey->co_table->dt_table, self)))
1876
row_count = ((xtInt8) ot->ot_table->tab_row_eof >> XT_TAB_ROW_SHIFTS) - 1;
1877
row_count = ((xtInt8) ot->ot_table->tab_row_eof_id) - 1;
1877
1878
row_count -= (xtInt8) ot->ot_table->tab_row_fnum;
1879
xt_return_table_to_pool(ot);
1880
xt_db_return_table_to_pool_ns(ot);
1881
1882
if (row_count > 0)
1882
1883
xt_throw_ixterr(XT_CONTEXT, XT_ERR_ROW_IS_REFERENCED, tr_fkey->co_name);
2075
2076
search_key.sk_key_value.sv_flags = 0;
2076
search_key.sk_key_value.sv_record = 0;
2077
search_key.sk_key_value.sv_rec_id = 0;
2078
search_key.sk_key_value.sv_row_id = 0;
2077
2079
search_key.sk_key_value.sv_key = search_key.sk_key_buf;
2078
2080
search_key.sk_key_value.sv_length = myxt_create_foreign_key_from_row(loc_ind, search_key.sk_key_buf, rec_buf, ind, &no_null);
2079
2081
search_key.sk_on_key = FALSE;
2084
2086
if (before_buf) {
2085
2087
u_int before_key_len;
2086
xtWord1 before_key[XT_IDX_MAX_KEY_SIZE];
2088
xtWord1 before_key[XT_INDEX_MAX_KEY_SIZE];
2088
2090
/* If there is a before buffer, this insert was an update, so check
2089
2091
* if the key value has changed. If not, we need not do anything.
2101
2103
/* Search for the key in the parent (referenced) table: */
2102
if (!(ot = xt_open_table_from_pool(fk_ref_table->dt_table, thread)))
2104
if (!(ot = xt_db_open_table_using_tab(fk_ref_table->dt_table, thread)))
2106
2108
if (!xt_idx_search(ot, ind, &search_key))
2109
while (ot->ot_curr_rec) {
2111
while (ot->ot_curr_rec_id) {
2110
2112
if (!search_key.sk_on_key)
2113
switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
2115
switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec_id, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
2115
2117
/* We should not get a deadlock here because the thread
2116
2118
* that we are waiting for, should not doing
2117
2119
* data definition (i.e. should not be trying to
2118
2120
* get an exclusive lock on dt_ref_lock.
2120
if (!xt_xn_wait_for_xact(thread, xn_id))
2122
if (!xt_xn_wait_for_xact(thread, xn_id, FALSE))
2126
2128
/* We found a matching parent: */
2127
xt_return_table_to_pool(ot);
2129
xt_db_return_table_to_pool_ns(ot);
2130
2132
if (!xt_idx_next(ot, ind, &search_key))
2136
2138
xt_register_ixterr(XT_REG_CONTEXT, XT_ERR_NO_REFERENCED_ROW, co_name);
2139
xt_return_table_to_pool(ot);
2141
xt_db_return_table_to_pool_ns(ot);
2142
2144
xt_rwlock_unlock(&co_table->dt_ref_lock);
2360
2362
fk = dt_fkeys.itemAt(i);
2361
2363
fk->removeReference(self);
2363
if (!(tab = xt_use_table_no_lock(self, self->st_database, fk->fk_ref_tab_name, TRUE, FALSE, NULL))) {
2365
if (!(tab = xt_use_table_no_lock(self, db, fk->fk_ref_tab_name, TRUE, FALSE, NULL))) {
2364
2366
/* CHeck if the table references itself! */
2365
2367
if (xt_tab_compare_names(fk->fk_ref_tab_name, this->dt_table->tab_name) == 0) {
2366
2368
tab = this->dt_table;
2459
2461
/* Currently I allow foreign keys to be created on tables that do not yet exist!
2460
pushsr_(tab, xt_heap_release, xt_use_table(self, fk->fk_ref_tab_name, FALSE));
2462
pushsr_(tab, xt_heap_release, xt_use_table(self, fk->fk_ref_tab_name, FALSE FALSE));
2461
2463
if ((dt = tab->tab_dic.dic_table))
2462
2464
dt->checkForeignKeyReference(self, fk);
2463
2465
freer_(); // xt_heap_release(tab)
2524
2526
rec_buf.ib_free = FALSE;
2525
2527
if (!rec_ptr) {
2526
if (!xt_tab_load_record(ot, ot->ot_curr_rec, &rec_buf))
2528
if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &rec_buf))
2528
2530
rec_ptr = rec_buf.ib_db.db_data;
2550
2552
rec_buf.ib_free = FALSE;
2551
2553
if (!rec_ptr) {
2552
if (!xt_tab_load_record(ot, ot->ot_curr_rec, &rec_buf))
2554
if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &rec_buf))
2554
2556
rec_ptr = rec_buf.ib_db.db_data;
2614
2616
xt_rwlock_rdlock(&dt_ref_lock);
2615
2617
if ((tr = dt_trefs)) {
2617
if (!xt_tab_load_record(ot, ot->ot_curr_rec, &before_buf))
2619
if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &before_buf))
2619
2621
before = before_buf.ib_db.db_data;