LCOV - code coverage report
Current view: top level - sql/storage - store_dependency.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 102 154 66.2 %
Date: 2021-10-13 02:24:04 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "store_dependency.h"
      11             : 
      12             : static void
      13        1004 : _free(void *dummy, void *data)
      14             : {
      15             :         (void)dummy;
      16        1004 :         GDKfree(data);
      17        1004 : }
      18             : 
      19             : static sqlid
      20             : list_find_func_id(list *ids, sqlid id)
      21             : {
      22           3 :         node *n = ids->h;
      23           6 :         while (n) {
      24           6 :                 sql_func * f = n->data;
      25           6 :                 if (f->base.id == id)
      26             :                         return id;
      27             :                 else
      28           3 :                         n = n->next;
      29             :         }
      30             :         return 0;
      31             : }
      32             : 
      33             : /*Function to create a dependency*/
      34             : int
      35      286487 : sql_trans_create_dependency(sql_trans* tr, sqlid id, sqlid depend_id, sql_dependency depend_type)
      36             : {
      37      286487 :         assert(id && depend_id);
      38      286487 :         sqlstore *store = tr->store;
      39      286487 :         sql_schema * s = find_sql_schema(tr, "sys");
      40      286487 :         sql_table *t = find_sql_table(tr, s, "dependencies");
      41      286487 :         sql_column *c_id = find_sql_column(t, "id");
      42      286487 :         sql_column *c_dep_id = find_sql_column(t, "depend_id");
      43      286487 :         sql_column *c_dep_type = find_sql_column(t, "depend_type");
      44      286487 :         sht dtype = (sht) depend_type;
      45             :         int log_res = LOG_OK;
      46             : 
      47      286487 :         if (is_oid_nil(store->table_api.column_find_row(tr, c_id, &id, c_dep_id, &depend_id, c_dep_type, &dtype, NULL)))
      48      283494 :                 log_res = store->table_api.table_insert(tr, t, &id, &depend_id, &dtype);
      49             : 
      50      286487 :         return log_res;
      51             : }
      52             : 
      53             : /*Function to drop the dependencies on depend_id*/
      54             : int
      55       36796 : sql_trans_drop_dependencies(sql_trans* tr, sqlid depend_id)
      56             : {
      57       36796 :         sqlstore *store = tr->store;
      58             :         oid rid;
      59       36796 :         sql_schema * s = find_sql_schema(tr, "sys");
      60       36796 :         sql_table* deps = find_sql_table(tr, s, "dependencies");
      61       36796 :         sql_column * dep_dep_id = find_sql_column(deps, "depend_id");
      62             :         rids *rs;
      63             :         int log_res = LOG_OK;
      64             : 
      65       36796 :         rs = store->table_api.rids_select(tr, dep_dep_id, &depend_id, &depend_id, NULL);
      66       63356 :         for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid) && log_res == LOG_OK; rid = store->table_api.rids_next(rs))
      67       26560 :                 log_res = store->table_api.table_delete(tr, deps, rid);
      68       36796 :         store->table_api.rids_destroy(rs);
      69       36796 :         return log_res;
      70             : }
      71             : 
      72             : /*Function to drop the dependency between object and target, ie obj_id/depend_id*/
      73             : int
      74         305 : sql_trans_drop_dependency(sql_trans* tr, sqlid obj_id, sqlid depend_id, sql_dependency depend_type)
      75             : {
      76         305 :         sqlstore *store = tr->store;
      77             :         oid rid;
      78         305 :         sql_schema * s = find_sql_schema(tr, "sys");
      79         305 :         sql_table* deps = find_sql_table(tr, s, "dependencies");
      80         305 :         sql_column *dep_obj_id = find_sql_column(deps, "id");
      81         305 :         sql_column *dep_dep_id = find_sql_column(deps, "depend_id");
      82         305 :         sql_column *dep_dep_type = find_sql_column(deps, "depend_type");
      83         305 :         sht dtype = (sht) depend_type;
      84             :         rids *rs;
      85             :         int log_res = LOG_OK;
      86             : 
      87         305 :         rs = store->table_api.rids_select(tr, dep_obj_id, &obj_id, &obj_id, dep_dep_id, &depend_id, &depend_id, dep_dep_type, &dtype, &dtype, NULL);
      88         531 :         for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid) && log_res == LOG_OK; rid = store->table_api.rids_next(rs))
      89         226 :                 log_res = store->table_api.table_delete(tr, deps, rid);
      90         305 :         store->table_api.rids_destroy(rs);
      91         305 :         return log_res;
      92             : }
      93             : 
      94             : /*It returns a list with depend_id_1, depend_type_1, depend_id_2, depend_type_2, ....*/
      95             : list*
      96        9420 : sql_trans_get_dependencies(sql_trans* tr, sqlid id, sql_dependency depend_type, list * ignore_ids)
      97             : {
      98        9420 :         sqlstore *store = tr->store;
      99             :         void *v;
     100        9420 :         sql_schema *s = find_sql_schema(tr, "sys");
     101        9420 :         sql_table *deps = find_sql_table(tr, s, "dependencies");
     102             :         sql_column *dep_id, *dep_dep_id, *dep_dep_type, *tri_id, *table_id;
     103        9420 :         list *dep_list = list_create((fdestroy) _free);
     104             :         oid rid;
     105             :         rids *rs;
     106             : 
     107        9420 :         if (!dep_list)
     108             :                 return NULL;
     109             : 
     110        9420 :         dep_id = find_sql_column(deps, "id");
     111        9420 :         dep_dep_id = find_sql_column(deps, "depend_id");
     112        9420 :         dep_dep_type = find_sql_column(deps, "depend_type");
     113             : 
     114        9420 :         rs = store->table_api.rids_select(tr, dep_id, &id, &id, NULL);
     115        9912 :         for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)){
     116         492 :                 if (!(v = store->table_api.column_find_value(tr, dep_dep_id, rid))) {
     117           0 :                         list_destroy(dep_list);
     118           0 :                         store->table_api.rids_destroy(rs);
     119           0 :                         return NULL;
     120             :                 }
     121         492 :                 id = *(sqlid*)v;
     122         495 :                 if (!(ignore_ids  && list_find_func_id(ignore_ids, id))) {
     123         489 :                         list_append(dep_list, v);
     124         489 :                         if (!(v = store->table_api.column_find_value(tr, dep_dep_type, rid))) {
     125           0 :                                 list_destroy(dep_list);
     126           0 :                                 store->table_api.rids_destroy(rs);
     127           0 :                                 return NULL;
     128             :                         }
     129         489 :                         list_append(dep_list, v);
     130             :                 } else {
     131           3 :                         _DELETE(v);
     132             :                 }
     133             :         }
     134        9420 :         store->table_api.rids_destroy(rs);
     135             : 
     136        9420 :         if (depend_type == TABLE_DEPENDENCY) {
     137        3949 :                 sql_table *triggers = find_sql_table(tr, s, "triggers");
     138        3949 :                 table_id = find_sql_column(triggers, "table_id");
     139        3949 :                 tri_id = find_sql_column(triggers, "id");
     140             :                 depend_type = TRIGGER_DEPENDENCY;
     141             : 
     142        3949 :                 rs = store->table_api.rids_select(tr, table_id, &id, &id, NULL);
     143        3950 :                 for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
     144           1 :                         if (!(v = store->table_api.column_find_value(tr, tri_id, rid))) {
     145           0 :                                 list_destroy(dep_list);
     146           0 :                                 store->table_api.rids_destroy(rs);
     147           0 :                                 return NULL;
     148             :                         }
     149           1 :                         list_append(dep_list, v);
     150           1 :                         if (!(v = MNEW(sht))) {
     151           0 :                                 list_destroy(dep_list);
     152           0 :                                 store->table_api.rids_destroy(rs);
     153           0 :                                 return NULL;
     154             :                         }
     155           1 :                         *(sht *) v = (sht) depend_type;
     156           1 :                         list_append(dep_list, v);
     157             :                 }
     158        3949 :                 store->table_api.rids_destroy(rs);
     159             :         }
     160             :         return dep_list;
     161             : }
     162             : 
     163             : /*It checks if there are dependency between two ID's */
     164             : sqlid
     165         156 : sql_trans_get_dependency_type(sql_trans *tr, sqlid id, sql_dependency depend_type)
     166             : {
     167         156 :         sqlstore *store = tr->store;
     168             :         oid rid;
     169             :         sql_schema *s;
     170             :         sql_table *dep;
     171             :         sql_column *dep_id, *dep_dep_id, *dep_dep_type;
     172         156 :         sht dtype = (sht) depend_type;
     173             : 
     174         156 :         s = find_sql_schema(tr, "sys");
     175         156 :         dep = find_sql_table(tr, s, "dependencies");
     176         156 :         dep_id = find_sql_column(dep, "id");
     177         156 :         dep_dep_id = find_sql_column(dep, "depend_id");
     178         156 :         dep_dep_type = find_sql_column(dep, "depend_type");
     179             : 
     180         156 :         rid = store->table_api.column_find_row(tr, dep_id, &id, dep_dep_type, &dtype, NULL);
     181         156 :         if (!is_oid_nil(rid)) {
     182         136 :                 return store->table_api.column_find_sqlid(tr, dep_dep_id, rid);
     183             :         } else {
     184             :                 return -1;
     185             :         }
     186             : }
     187             : 
     188             : /*It checks if there are dependency between two ID's */
     189             : int
     190           0 : sql_trans_check_dependency(sql_trans *tr, sqlid id, sqlid depend_id, sql_dependency depend_type)
     191             : {
     192           0 :         sqlstore *store = tr->store;
     193             :         oid rid;
     194             :         sql_schema *s;
     195             :         sql_table *dep;
     196             :         sql_column *dep_id, *dep_dep_id, *dep_dep_type;
     197           0 :         sht dtype = (sht) depend_type;
     198             : 
     199           0 :         s = find_sql_schema(tr, "sys");
     200           0 :         dep = find_sql_table(tr, s, "dependencies");
     201           0 :         dep_id = find_sql_column(dep, "id");
     202           0 :         dep_dep_id = find_sql_column(dep, "depend_id");
     203           0 :         dep_dep_type = find_sql_column(dep, "depend_type");
     204             : 
     205           0 :         rid = store->table_api.column_find_row(tr, dep_id, &id, dep_dep_id, &depend_id, dep_dep_type, &dtype, NULL);
     206           0 :         if (!is_oid_nil(rid))
     207             :                 return 1;
     208           0 :         else return 0;
     209             : }
     210             : 
     211             : /*Schema on users*/
     212             : 
     213             : list *
     214           0 : sql_trans_schema_user_dependencies(sql_trans *tr, sqlid schema_id)
     215             : {
     216           0 :         sqlstore *store = tr->store;
     217             :         void *v;
     218           0 :         sql_schema * s = find_sql_schema(tr, "sys");
     219           0 :         sql_table *auths = find_sql_table(tr, s, "auths");
     220           0 :         sql_column *auth_id = find_sql_column(auths, "id");
     221             :         sql_dependency type = USER_DEPENDENCY;
     222           0 :         list *l = list_create((fdestroy) _free);
     223             :         rids *users;
     224             :         oid rid;
     225             : 
     226           0 :         if (!l || !(users = backend_schema_user_dependencies(tr, schema_id))) {
     227           0 :                 list_destroy(l);
     228           0 :                 return NULL;
     229             :         }
     230             : 
     231           0 :         for (rid = store->table_api.rids_next(users); !is_oid_nil(rid); rid = store->table_api.rids_next(users)) {
     232           0 :                 if (!(v = store->table_api.column_find_value(tr, auth_id, rid))) {
     233           0 :                         list_destroy(l);
     234           0 :                         store->table_api.rids_destroy(users);
     235           0 :                         return NULL;
     236             :                 }
     237           0 :                 list_append(l,v);
     238           0 :                 if (!(v = MNEW(sht))) {
     239           0 :                         list_destroy(l);
     240           0 :                         store->table_api.rids_destroy(users);
     241           0 :                         return NULL;
     242             :                 }
     243           0 :                 *(sht*)v = (sht) type;
     244           0 :                 list_append(l,v);
     245             :         }
     246           0 :         store->table_api.rids_destroy(users);
     247           0 :         return l;
     248             : }
     249             : 
     250             : /*owner on schemas*/
     251             : list *
     252          79 : sql_trans_owner_schema_dependencies(sql_trans *tr, sqlid owner_id)
     253             : {
     254          79 :         sqlstore *store = tr->store;
     255             :         void *v;
     256          79 :         sql_schema * s = find_sql_schema(tr, "sys");
     257          79 :         sql_table *schemas = find_sql_table(tr, s, "schemas");
     258          79 :         sql_column *schema_owner = find_sql_column(schemas, "authorization");
     259          79 :         sql_column *schema_id = find_sql_column(schemas, "id");
     260             :         sql_dependency type = SCHEMA_DEPENDENCY;
     261          79 :         list *l = list_create((fdestroy) _free);
     262             :         rids *rs;
     263             :         oid rid;
     264             : 
     265          79 :         if (!l)
     266             :                 return NULL;
     267             : 
     268          79 :         rs = store->table_api.rids_select(tr, schema_owner, &owner_id, &owner_id, NULL);
     269             : 
     270          91 :         for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
     271          12 :                 if (!(v = store->table_api.column_find_value(tr, schema_id, rid))) {
     272           0 :                         list_destroy(l);
     273           0 :                         store->table_api.rids_destroy(rs);
     274           0 :                         return NULL;
     275             :                 }
     276          12 :                 list_append(l, v);
     277          12 :                 if (!(v = MNEW(sht))) {
     278           0 :                         list_destroy(l);
     279           0 :                         store->table_api.rids_destroy(rs);
     280           0 :                         return NULL;
     281             :                 }
     282          12 :                 *(sht*)v = (sht) type;
     283          12 :                 list_append(l,v);
     284             :         }
     285          79 :         store->table_api.rids_destroy(rs);
     286          79 :         return l;
     287             : }
     288             : 
     289             : /*Function on Functions*/

Generated by: LCOV version 1.14