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 - 2019 MonetDB B.V.
7 */
8
9/* (c) M.L. Kersten
10 * The order index interface routines are defined here.
11*/
12#include "monetdb_config.h"
13#include "mal_backend.h"
14#include "sql_scenario.h"
15#include "sql_result.h"
16#include "sql_gencode.h"
17#include "sql_optimizer.h"
18#include "sql_env.h"
19#include "sql_mvc.h"
20#include "sql_orderidx.h"
21#include "orderidx.h"
22#include "sql_scenario.h"
23
24str
25sql_createorderindex(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
26{
27 mvc *m = NULL;
28 str msg = getSQLContext(cntxt, mb, &m, NULL);
29 str sch,tbl,col;
30 sql_schema *s;
31 sql_table *t;
32 sql_column *c;
33 BAT *b;
34
35 if (msg != MAL_SUCCEED || (msg = checkSQLContext(cntxt)) != NULL)
36 return msg;
37
38 sch = *getArgReference_str(stk, pci, 1);
39 tbl = *getArgReference_str(stk, pci, 2);
40 col = *getArgReference_str(stk, pci, 3);
41
42#ifdef DEBUG_SQL_ORDERIDX
43 fprintf(stderr, "#orderindex layout %s.%s.%s \n", sch, tbl, col);
44#endif
45 s = mvc_bind_schema(m, sch);
46 if (s == NULL)
47 throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Unknown schema %s", sch);
48 t = mvc_bind_table(m, s, tbl);
49 if (t == NULL || !isTable(t))
50 throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Unknown table %s.%s",
51 sch, tbl);
52 c = mvc_bind_column(m, t, col);
53 if (c == NULL)
54 throw(SQL, "sql.createorderindex", SQLSTATE(42000) "Unknown column %s.%s.%s",
55 sch, tbl, col);
56 b = store_funcs.bind_col(m->session->tr, c, 0);
57 if (b == 0)
58 throw(SQL,"sql.createorderindex", SQLSTATE(HY005) "Column can not be accessed");
59 /* create the ordered index on the column */
60 msg = OIDXcreateImplementation(cntxt, newBatType(b->ttype), b, -1);
61 BBPunfix(b->batCacheid);
62 return msg;
63}
64
65str
66sql_droporderindex(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
67{
68 mvc *m = NULL;
69 str msg = getSQLContext(cntxt, mb, &m, NULL);
70 str sch,tbl,col;
71 sql_schema *s;
72 sql_table *t;
73 sql_column *c;
74 BAT *b;
75
76 if (msg != MAL_SUCCEED || (msg = checkSQLContext(cntxt)) != NULL)
77 return msg;
78
79 sch = *getArgReference_str(stk, pci, 1);
80 tbl = *getArgReference_str(stk, pci, 2);
81 col = *getArgReference_str(stk, pci, 3);
82
83#ifdef DEBUG_SQL_ORDERIDX
84 fprintf(stderr, "#orderindex layout %s.%s.%s \n", sch, tbl, col);
85#endif
86 s = mvc_bind_schema(m, sch);
87 if (s == NULL)
88 throw(SQL, "sql.droporderindex", SQLSTATE(3FOOO) "Unknown schema %s", sch);
89 t = mvc_bind_table(m, s, tbl);
90 if (t == NULL || !isTable(t))
91 throw(SQL, "sql.droporderindex", SQLSTATE(42S02) "Unknown table %s.%s", sch, tbl);
92 c = mvc_bind_column(m, t, col);
93 if (c == NULL)
94 throw(SQL, "sql.droporderindex", SQLSTATE(38000) "Unknown column %s.%s.%s", sch, tbl, col);
95 b = store_funcs.bind_col(m->session->tr, c, 0);
96 if (b == 0)
97 throw(SQL,"sql.droporderindex", SQLSTATE(38000) "Column can not be accessed");
98 msg = OIDXdropImplementation(cntxt, b);
99 BBPunfix(b->batCacheid);
100 return MAL_SUCCEED;
101}
102
103