Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /* (c) M.L. Kersten
14 : * The order index interface routines are defined here.
15 : */
16 : #include "monetdb_config.h"
17 : #include "mal_backend.h"
18 : #include "sql_scenario.h"
19 : #include "sql_result.h"
20 : #include "sql_gencode.h"
21 : #include "sql_optimizer.h"
22 : #include "sql_env.h"
23 : #include "sql_mvc.h"
24 : #include "sql_orderidx.h"
25 : #include "orderidx.h"
26 : #include "sql_scenario.h"
27 :
28 : str
29 0 : sql_createorderindex(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
30 : {
31 0 : mvc *m = NULL;
32 0 : str msg = getSQLContext(cntxt, mb, &m, NULL);
33 0 : str sch,tbl,col;
34 0 : sql_schema *s;
35 0 : sql_table *t;
36 0 : sql_column *c;
37 0 : BAT *b = NULL, *nb = NULL;
38 :
39 0 : if (msg != MAL_SUCCEED || (msg = checkSQLContext(cntxt)) != NULL)
40 0 : return msg;
41 :
42 0 : sch = *getArgReference_str(stk, pci, 1);
43 0 : tbl = *getArgReference_str(stk, pci, 2);
44 0 : col = *getArgReference_str(stk, pci, 3);
45 0 : if (strNil(sch))
46 0 : throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Schema name cannot be NULL");
47 0 : if (strNil(tbl))
48 0 : throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Table name cannot be NULL");
49 0 : if (strNil(col))
50 0 : throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Column name cannot be NULL");
51 :
52 0 : if (!(s = mvc_bind_schema(m, sch)))
53 0 : throw(SQL, "sql.createorderindex", SQLSTATE(3FOOO) "Unknown schema %s", sch);
54 0 : if (!mvc_schema_privs(m, s))
55 0 : throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
56 0 : if (!(t = mvc_bind_table(m, s, tbl)))
57 0 : throw(SQL, "sql.createorderindex", SQLSTATE(42S02) "Unknown table %s.%s", sch, tbl);
58 0 : if (!isTable(t))
59 0 : throw(SQL, "sql.createorderindex", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
60 0 : if (!(c = mvc_bind_column(m, t, col)))
61 0 : throw(SQL, "sql.createorderindex", SQLSTATE(38000) "Unknown column %s.%s.%s", sch, tbl, col);
62 0 : sqlstore *store = m->session->tr->store;
63 0 : if (!(b = store->storage_api.bind_col(m->session->tr, c, RDONLY)))
64 0 : throw(SQL,"sql.createorderindex", SQLSTATE(HY005) "Column can not be accessed");
65 0 : if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
66 0 : BBPunfix(b->batCacheid);
67 0 : if (!(b = BATdescriptor(nb->batCacheid)))
68 0 : throw(SQL,"sql.createorderindex", SQLSTATE(HY005) "Column can not be accessed");
69 : }
70 : /* create the ordered index on the column */
71 0 : msg = OIDXcreateImplementation(cntxt, newBatType(b->ttype), b, -1);
72 0 : BBPunfix(b->batCacheid);
73 0 : return msg;
74 : }
75 :
76 : str
77 0 : sql_droporderindex(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
78 : {
79 0 : mvc *m = NULL;
80 0 : str msg = getSQLContext(cntxt, mb, &m, NULL);
81 0 : str sch,tbl,col;
82 0 : sql_schema *s;
83 0 : sql_table *t;
84 0 : sql_column *c;
85 0 : BAT *b = NULL, *nb = NULL;
86 :
87 0 : if (msg != MAL_SUCCEED || (msg = checkSQLContext(cntxt)) != NULL)
88 0 : return msg;
89 :
90 0 : sch = *getArgReference_str(stk, pci, 1);
91 0 : tbl = *getArgReference_str(stk, pci, 2);
92 0 : col = *getArgReference_str(stk, pci, 3);
93 0 : if (strNil(sch))
94 0 : throw(SQL, "sql.droporderindex", SQLSTATE(42000) "Schema name cannot be NULL");
95 0 : if (strNil(tbl))
96 0 : throw(SQL, "sql.droporderindex", SQLSTATE(42000) "Table name cannot be NULL");
97 0 : if (strNil(col))
98 0 : throw(SQL, "sql.droporderindex", SQLSTATE(42000) "Column name cannot be NULL");
99 :
100 0 : if (!(s = mvc_bind_schema(m, sch)))
101 0 : throw(SQL, "sql.droporderindex", SQLSTATE(3FOOO) "Unknown schema %s", sch);
102 0 : if (!mvc_schema_privs(m, s))
103 0 : throw(SQL, "sql.droporderindex", SQLSTATE(42000) "Access denied for %s to schema '%s'", get_string_global_var(m, "current_user"), s->base.name);
104 0 : if (!(t = mvc_bind_table(m, s, tbl)))
105 0 : throw(SQL, "sql.droporderindex", SQLSTATE(42S02) "Unknown table %s.%s", sch, tbl);
106 0 : if (!isTable(t))
107 0 : throw(SQL, "sql.droporderindex", SQLSTATE(42000) "%s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->base.name);
108 0 : if (!(c = mvc_bind_column(m, t, col)))
109 0 : throw(SQL, "sql.droporderindex", SQLSTATE(38000) "Unknown column %s.%s.%s", sch, tbl, col);
110 0 : sqlstore *store = m->session->tr->store;
111 0 : if (!(b = store->storage_api.bind_col(m->session->tr, c, RDONLY)))
112 0 : throw(SQL,"sql.droporderindex", SQLSTATE(HY005) "Column can not be accessed");
113 0 : if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
114 0 : BBPunfix(b->batCacheid);
115 0 : if (!(b = BATdescriptor(nb->batCacheid)))
116 0 : throw(SQL,"sql.droporderindex", SQLSTATE(HY005) "Column can not be accessed");
117 : }
118 0 : OIDXdestroy(b);
119 0 : BBPunfix(b->batCacheid);
120 0 : return msg;
121 : }
|