| 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 | #ifndef SQL_CATALOG_H |
| 10 | #define SQL_CATALOG_H |
| 11 | |
| 12 | #include "sql_mem.h" |
| 13 | #include "sql_list.h" |
| 14 | #include "sql_querytype.h" |
| 15 | #include "stream.h" |
| 16 | |
| 17 | #define tr_none 0 |
| 18 | #define tr_readonly 1 |
| 19 | #define tr_writable 2 |
| 20 | #define tr_serializable 4 |
| 21 | #define tr_append 8 |
| 22 | |
| 23 | #define ACT_NO_ACTION 0 |
| 24 | #define ACT_CASCADE 1 |
| 25 | #define ACT_RESTRICT 2 |
| 26 | #define ACT_SET_NULL 3 |
| 27 | #define ACT_SET_DEFAULT 4 |
| 28 | |
| 29 | #define DROP_RESTRICT 0 |
| 30 | #define DROP_CASCADE 1 |
| 31 | #define DROP_CASCADE_START 2 |
| 32 | |
| 33 | #define PRIV_SELECT 1 |
| 34 | #define PRIV_UPDATE 2 |
| 35 | #define PRIV_INSERT 4 |
| 36 | #define PRIV_DELETE 8 |
| 37 | #define PRIV_EXECUTE 16 |
| 38 | #define PRIV_GRANT 32 |
| 39 | #define PRIV_TRUNCATE 64 |
| 40 | /* global privs */ |
| 41 | #define PRIV_COPYFROMFILE 1 |
| 42 | #define PRIV_COPYINTOFILE 2 |
| 43 | |
| 44 | typedef enum sql_dependency { |
| 45 | SCHEMA_DEPENDENCY = 1, |
| 46 | TABLE_DEPENDENCY = 2, |
| 47 | COLUMN_DEPENDENCY = 3, |
| 48 | KEY_DEPENDENCY = 4, |
| 49 | VIEW_DEPENDENCY = 5, |
| 50 | USER_DEPENDENCY = 6, |
| 51 | FUNC_DEPENDENCY = 7, |
| 52 | TRIGGER_DEPENDENCY = 8, |
| 53 | OWNER_DEPENDENCY = 9, |
| 54 | INDEX_DEPENDENCY = 10, |
| 55 | FKEY_DEPENDENCY = 11, |
| 56 | SEQ_DEPENDENCY = 12, |
| 57 | PROC_DEPENDENCY = 13, |
| 58 | BEDROPPED_DEPENDENCY = 14, /*The object must be dropped when the dependent object is dropped independently of the DROP type.*/ |
| 59 | TYPE_DEPENDENCY = 15 |
| 60 | } sql_dependency; |
| 61 | |
| 62 | #define NO_DEPENDENCY 0 |
| 63 | #define HAS_DEPENDENCY 1 |
| 64 | #define CICLE_DEPENDENCY 2 |
| 65 | #define DEPENDENCY_CHECK_ERROR 3 |
| 66 | #define DEPENDENCY_CHECK_OK 0 |
| 67 | |
| 68 | #define ROLE_PUBLIC 1 |
| 69 | #define ROLE_SYSADMIN 2 |
| 70 | #define USER_MONETDB 3 |
| 71 | |
| 72 | #define ISO_READ_UNCOMMITED 1 |
| 73 | #define ISO_READ_COMMITED 2 |
| 74 | #define ISO_READ_REPEAT 3 |
| 75 | #define ISO_SERIALIZABLE 4 |
| 76 | |
| 77 | #define SCALE_NONE 0 |
| 78 | #define SCALE_FIX 1 /* many numerical functions require equal |
| 79 | scales/precision for all their inputs */ |
| 80 | #define SCALE_NOFIX 2 |
| 81 | #define SCALE_MUL 3 /* multiplication gives the sum of scales */ |
| 82 | #define SCALE_DIV 4 /* div on the other hand reduces the scales */ |
| 83 | #define DIGITS_ADD 5 /* some types grow under functions (concat) */ |
| 84 | #define INOUT 6 /* output type equals input type */ |
| 85 | #define SCALE_EQ 7 /* user defined functions need equal scales */ |
| 86 | #define SCALE_DIGITS_FIX 8 /* the geom module requires the types and functions to have the same scale and digits */ |
| 87 | |
| 88 | /* Warning TR flags are a bitmask */ |
| 89 | #define TR_NEW 1 |
| 90 | #define TR_RENAMED 2 |
| 91 | |
| 92 | #define RDONLY 0 |
| 93 | #define RD_INS 1 |
| 94 | #define RD_UPD_ID 2 |
| 95 | #define RD_UPD_VAL 3 |
| 96 | #define QUICK 4 |
| 97 | |
| 98 | /* the following list of macros are used by rel_rankop function */ |
| 99 | #define UNBOUNDED_PRECEDING_BOUND 0 |
| 100 | #define UNBOUNDED_FOLLOWING_BOUND 1 |
| 101 | #define CURRENT_ROW_BOUND 2 |
| 102 | |
| 103 | #define FRAME_ROWS 0 /* number of rows (preceding/following) */ |
| 104 | #define FRAME_RANGE 1 /* logical range (based on the ordering column). |
| 105 | Example: |
| 106 | RANGE BETWEEN INTERVAL '1' MONTH PRECEDING |
| 107 | AND INTERVAL '1' MONTH FOLLOWING */ |
| 108 | #define FRAME_GROUPS 2 |
| 109 | #define FRAME_ALL 3 /* special case of FRAME_RANGE, cover the entire partition */ |
| 110 | |
| 111 | /* the following list of macros are used by SQLwindow_bound function */ |
| 112 | #define BOUND_FIRST_HALF_PRECEDING 0 |
| 113 | #define BOUND_FIRST_HALF_FOLLOWING 1 |
| 114 | #define BOUND_SECOND_HALF_PRECEDING 2 |
| 115 | #define BOUND_SECOND_HALF_FOLLOWING 3 |
| 116 | #define CURRENT_ROW_PRECEDING 4 |
| 117 | #define CURRENT_ROW_FOLLOWING 5 |
| 118 | |
| 119 | #define EXCLUDE_NONE 0 /* nothing excluded (also the default) */ |
| 120 | #define EXCLUDE_CURRENT_ROW 1 /* exclude the current row */ |
| 121 | #define EXCLUDE_GROUP 2 /* exclude group */ |
| 122 | #define EXCLUDE_TIES 3 /* exclude group but not the current row */ |
| 123 | |
| 124 | /* The following macros are used in properties field of sql_table */ |
| 125 | #define PARTITION_RANGE 1 |
| 126 | #define PARTITION_LIST 2 |
| 127 | #define PARTITION_COLUMN 4 |
| 128 | #define PARTITION_EXPRESSION 8 |
| 129 | |
| 130 | #define STORAGE_MAX_VALUE_LENGTH 2048 |
| 131 | |
| 132 | #define cur_user 1 |
| 133 | #define cur_role 2 |
| 134 | |
| 135 | #define sql_max(i1,i2) ((i1)<(i2))?(i2):(i1) |
| 136 | |
| 137 | #define dt_schema "%dt%" |
| 138 | #define isDeclaredSchema(s) (strcmp(s->base.name, dt_schema) == 0) |
| 139 | |
| 140 | extern const char *TID; |
| 141 | |
| 142 | typedef enum temp_t { |
| 143 | SQL_PERSIST = 0, |
| 144 | SQL_LOCAL_TEMP = 1, |
| 145 | SQL_GLOBAL_TEMP = 2, |
| 146 | SQL_DECLARED_TABLE = 3, /* variable inside a stored procedure */ |
| 147 | SQL_MERGE_TABLE = 4, |
| 148 | SQL_STREAM = 5, |
| 149 | SQL_REMOTE = 6, |
| 150 | SQL_REPLICA_TABLE = 7 |
| 151 | } temp_t; |
| 152 | |
| 153 | typedef enum comp_type { |
| 154 | cmp_gt = 0, |
| 155 | cmp_gte = 1, |
| 156 | cmp_lte = 2, |
| 157 | cmp_lt = 3, |
| 158 | cmp_equal = 4, |
| 159 | cmp_notequal = 5, |
| 160 | |
| 161 | cmp_filter = 6, |
| 162 | cmp_or = 7, |
| 163 | cmp_in = 8, /* in value list */ |
| 164 | cmp_notin = 9, /* not in value list */ |
| 165 | cmp_equal_nil = 10, /* case equi join, with nil = nil */ |
| 166 | |
| 167 | mark_in = 11, /* mark joins */ |
| 168 | mark_notin = 12, |
| 169 | mark_exists = 13, |
| 170 | mark_notexists = 14, |
| 171 | |
| 172 | /* The followin cmp_* are only used within stmt (not sql_exp) */ |
| 173 | cmp_all = 15, /* special case for crossproducts */ |
| 174 | cmp_project = 16, /* special case for projection joins */ |
| 175 | cmp_joined = 17, /* special case already joined */ |
| 176 | cmp_left = 18, /* special case equi join, keep left order */ |
| 177 | cmp_left_project = 19 /* last step of outer join */ |
| 178 | } comp_type; |
| 179 | |
| 180 | /* for ranges we keep the requirment for symmetric */ |
| 181 | #define CMP_SYMMETRIC 8 |
| 182 | #define CMP_BETWEEN 16 |
| 183 | |
| 184 | #define is_theta_exp(e) ((e) == cmp_gt || (e) == cmp_gte || (e) == cmp_lte ||\ |
| 185 | (e) == cmp_lt || (e) == cmp_equal || (e) == cmp_notequal) |
| 186 | |
| 187 | #define is_complex_exp(e) ((e&CMPMASK) == cmp_or || (e) == cmp_in || (e) == cmp_notin || (e&CMPMASK) == cmp_filter) |
| 188 | |
| 189 | typedef enum commit_action_t { |
| 190 | CA_COMMIT, /* commit rows, only for persistent tables */ |
| 191 | CA_DELETE, /* delete rows */ |
| 192 | CA_PRESERVE, /* preserve rows */ |
| 193 | CA_DROP /* drop table */ |
| 194 | } ca_t; |
| 195 | |
| 196 | typedef int sqlid; |
| 197 | |
| 198 | typedef struct sql_base { |
| 199 | int wtime; |
| 200 | int rtime; |
| 201 | int stime; |
| 202 | int allocated; |
| 203 | int flags; |
| 204 | int refcnt; |
| 205 | sqlid id; |
| 206 | char *name; |
| 207 | } sql_base; |
| 208 | |
| 209 | #define newFlagSet(x) ((x & TR_NEW) == TR_NEW) |
| 210 | #define removeNewFlag(x) ((x)->base.flags &= ~TR_NEW) |
| 211 | #define isNew(x) (newFlagSet((x)->base.flags)) |
| 212 | |
| 213 | #define setRenamedFlag(x) ((x)->base.flags |= TR_RENAMED) |
| 214 | #define removeRenamedFlag(x) ((x)->base.flags &= ~TR_RENAMED) |
| 215 | #define isRenamed(x) (((x)->base.flags & TR_RENAMED) == TR_RENAMED) |
| 216 | |
| 217 | extern void base_init(sql_allocator *sa, sql_base * b, sqlid id, int flags, const char *name); |
| 218 | |
| 219 | typedef struct changeset { |
| 220 | sql_allocator *sa; |
| 221 | fdestroy destroy; |
| 222 | struct list *set; |
| 223 | struct list *dset; |
| 224 | node *nelm; |
| 225 | } changeset; |
| 226 | |
| 227 | extern void cs_new(changeset * cs, sql_allocator *sa, fdestroy destroy); |
| 228 | extern void cs_destroy(changeset * cs); |
| 229 | extern void cs_add(changeset * cs, void *elm, int flag); |
| 230 | extern void *cs_add_with_validate(changeset * cs, void *elm, int flag, fvalidate cmp); |
| 231 | extern void cs_add_before(changeset * cs, node *n, void *elm); |
| 232 | extern void cs_del(changeset * cs, node *elm, int flag); |
| 233 | extern void cs_move(changeset *from, changeset *to, void *data); |
| 234 | extern void *cs_transverse_with_validate(changeset * cs, void *elm, fvalidate cmp); |
| 235 | extern int cs_size(changeset * cs); |
| 236 | extern node *cs_find_name(changeset * cs, const char *name); |
| 237 | extern node *cs_find_id(changeset * cs, sqlid id); |
| 238 | extern node *cs_first_node(changeset * cs); |
| 239 | extern node *cs_last_node(changeset * cs); |
| 240 | extern void cs_remove_node(changeset * cs, node *n); |
| 241 | |
| 242 | typedef void *backend_code; |
| 243 | typedef size_t backend_stack; |
| 244 | |
| 245 | typedef struct sql_trans { |
| 246 | char *name; |
| 247 | int stime; /* start of transaction */ |
| 248 | int wstime; /* first write transaction time stamp */ |
| 249 | int rtime; /* timestamp of latest read performed in transaction*/ |
| 250 | int wtime; /* timestamp of latest write performed in transaction*/ |
| 251 | int schema_number; /* schema timestamp */ |
| 252 | int schema_updates; /* set on schema changes */ |
| 253 | int active; /* active transaction */ |
| 254 | int status; /* status of the last query */ |
| 255 | list *dropped; /* protection against recursive cascade action*/ |
| 256 | list *moved_tables; |
| 257 | |
| 258 | changeset schemas; |
| 259 | |
| 260 | sql_allocator *sa; /* transaction allocator */ |
| 261 | |
| 262 | struct sql_trans *parent; /* multilevel transaction support */ |
| 263 | backend_stack stk; |
| 264 | } sql_trans; |
| 265 | |
| 266 | typedef struct sql_schema { |
| 267 | sql_base base; |
| 268 | sqlid auth_id; |
| 269 | sqlid owner; |
| 270 | bit system; /* system or user schema */ |
| 271 | // TODO? int type; /* persistent, session local, transaction local */ |
| 272 | |
| 273 | changeset tables; |
| 274 | changeset types; |
| 275 | changeset funcs; |
| 276 | changeset seqs; |
| 277 | list *keys; /* Names for keys, idxs and triggers are */ |
| 278 | list *idxs; /* global, but these objects are only */ |
| 279 | list *triggers; /* useful within a table */ |
| 280 | |
| 281 | char *internal; /* optional internal module name */ |
| 282 | sql_trans *tr; |
| 283 | } sql_schema; |
| 284 | |
| 285 | typedef enum sql_class { |
| 286 | EC_ANY, |
| 287 | EC_TABLE, |
| 288 | EC_BIT, |
| 289 | EC_CHAR, |
| 290 | EC_STRING, |
| 291 | EC_BLOB, |
| 292 | EC_POS, |
| 293 | EC_NUM, |
| 294 | EC_MONTH, |
| 295 | EC_SEC, |
| 296 | EC_DEC, |
| 297 | EC_FLT, |
| 298 | EC_TIME, |
| 299 | EC_DATE, |
| 300 | EC_TIMESTAMP, |
| 301 | EC_GEOM, |
| 302 | EC_EXTERNAL, |
| 303 | EC_MAX /* evaluated to the max value, should be always kept at the bottom */ |
| 304 | } sql_class; |
| 305 | |
| 306 | #define has_tz(e,n) (EC_TEMP(e) && \ |
| 307 | ((e == EC_TIME && strcmp(n, "timetz") == 0) || \ |
| 308 | (e == EC_TIMESTAMP && strcmp(n, "timestamptz") == 0)) ) |
| 309 | #define type_has_tz(t) has_tz((t)->type->eclass, (t)->type->sqlname) |
| 310 | #define EC_VARCHAR(e) (e==EC_CHAR||e==EC_STRING) |
| 311 | #define EC_INTERVAL(e) (e==EC_MONTH||e==EC_SEC) |
| 312 | #define EC_NUMBER(e) (e==EC_POS||e==EC_NUM||EC_INTERVAL(e)||e==EC_DEC||e==EC_FLT) |
| 313 | #define EC_COMPUTE(e) (e==EC_NUM||e==EC_FLT) |
| 314 | #define EC_BOOLEAN(e) (e==EC_BIT||e==EC_NUM||e==EC_FLT) |
| 315 | #define EC_TEMP(e) (e==EC_TIME||e==EC_DATE||e==EC_TIMESTAMP) |
| 316 | #define EC_TEMP_FRAC(e) (e==EC_TIME||e==EC_TIMESTAMP) |
| 317 | #define EC_FIXED(e) (e==EC_BIT||e==EC_CHAR||e==EC_POS||e==EC_NUM||EC_INTERVAL(e)||e==EC_DEC||EC_TEMP(e)) |
| 318 | |
| 319 | typedef struct sql_type { |
| 320 | sql_base base; |
| 321 | |
| 322 | char *sqlname; |
| 323 | unsigned int digits; |
| 324 | unsigned int scale; /* indicates how scale is used in functions */ |
| 325 | int localtype; /* localtype, need for coersions */ |
| 326 | unsigned char radix; |
| 327 | unsigned int bits; |
| 328 | sql_class eclass; /* types are grouped into equivalence classes */ |
| 329 | sql_schema *s; |
| 330 | } sql_type; |
| 331 | |
| 332 | typedef struct sql_alias { |
| 333 | char *name; |
| 334 | char *alias; |
| 335 | } sql_alias; |
| 336 | |
| 337 | #define ARG_IN 1 |
| 338 | #define ARG_OUT 0 |
| 339 | |
| 340 | typedef struct sql_subtype { |
| 341 | sql_type *type; |
| 342 | unsigned int digits; |
| 343 | unsigned int scale; |
| 344 | } sql_subtype; |
| 345 | |
| 346 | /* sql_func need type transform rules types are equal if underlying |
| 347 | * types are equal + scale is equal if types do not mach we try type |
| 348 | * conversions which means for simple 1 arg functions |
| 349 | */ |
| 350 | |
| 351 | typedef struct sql_arg { |
| 352 | char *name; |
| 353 | bte inout; |
| 354 | sql_subtype type; |
| 355 | } sql_arg; |
| 356 | |
| 357 | typedef enum sql_ftype { |
| 358 | F_FUNC = 1, |
| 359 | F_PROC = 2, |
| 360 | F_AGGR = 3, |
| 361 | F_FILT = 4, |
| 362 | F_UNION = 5, |
| 363 | F_ANALYTIC = 6, |
| 364 | F_LOADER = 7 |
| 365 | } sql_ftype; |
| 366 | |
| 367 | #define IS_FUNC(f) (f->type == F_FUNC) |
| 368 | #define IS_PROC(f) (f->type == F_PROC) |
| 369 | #define IS_AGGR(f) (f->type == F_AGGR) |
| 370 | #define IS_FILT(f) (f->type == F_FILT) |
| 371 | #define IS_UNION(f) (f->type == F_UNION) |
| 372 | #define IS_ANALYTIC(f) (f->type == F_ANALYTIC) |
| 373 | #define IS_LOADER(f) (f->type == F_LOADER) |
| 374 | |
| 375 | typedef enum sql_flang { |
| 376 | FUNC_LANG_INT = 0, /* internal */ |
| 377 | FUNC_LANG_MAL = 1, /* create sql external mod.func */ |
| 378 | FUNC_LANG_SQL = 2, /* create ... sql function/procedure */ |
| 379 | FUNC_LANG_R = 3, /* create .. language R */ |
| 380 | FUNC_LANG_C = 4, /* create .. language C */ |
| 381 | FUNC_LANG_J = 5, /* create .. language JAVASCRIPT (not implemented) */ |
| 382 | /* this should probably be done in a better way */ |
| 383 | FUNC_LANG_PY = 6, /* create .. language PYTHON */ |
| 384 | FUNC_LANG_MAP_PY = 7, /* create .. language PYTHON_MAP */ |
| 385 | FUNC_LANG_PY2 = 8, /* create .. language PYTHON2 */ |
| 386 | FUNC_LANG_MAP_PY2 = 9, /* create .. language PYTHON2_MAP */ |
| 387 | FUNC_LANG_PY3 = 10, /* create .. language PYTHON3 */ |
| 388 | FUNC_LANG_MAP_PY3 = 11, /* create .. language PYTHON3_MAP */ |
| 389 | FUNC_LANG_CPP = 12 /* create .. language CPP */ |
| 390 | } sql_flang; |
| 391 | |
| 392 | #define LANG_EXT(l) (l>FUNC_LANG_SQL) |
| 393 | |
| 394 | typedef struct sql_func { |
| 395 | sql_base base; |
| 396 | |
| 397 | char *imp; |
| 398 | char *mod; |
| 399 | sql_ftype type; |
| 400 | list *ops; /* param list */ |
| 401 | list *res; /* list of results */ |
| 402 | int nr; |
| 403 | int sql; /* 0 native implementation |
| 404 | 1 sql |
| 405 | 2 sql instantiated proc |
| 406 | */ |
| 407 | sql_flang lang; |
| 408 | char *query; /* sql code */ |
| 409 | bit side_effect; |
| 410 | bit varres; /* variable output result */ |
| 411 | bit vararg; /* variable input arguments */ |
| 412 | bit system; /* system function */ |
| 413 | int fix_scale; |
| 414 | /* |
| 415 | SCALE_NOFIX/SCALE_NONE => nothing |
| 416 | SCALE_FIX => input scale fixing, |
| 417 | SCALE_ADD => leave inputs as is and do add scales |
| 418 | example numerical multiplication |
| 419 | SCALE_SUB => first input scale, fix with second scale |
| 420 | result scale is equal to first input |
| 421 | example numerical division |
| 422 | DIGITS_ADD => result digits, sum of args |
| 423 | example string concat |
| 424 | */ |
| 425 | sql_schema *s; |
| 426 | sql_allocator *sa; |
| 427 | void *rel; /* implementation */ |
| 428 | } sql_func; |
| 429 | |
| 430 | typedef struct sql_subfunc { |
| 431 | sql_func *func; |
| 432 | list *res; |
| 433 | list *coltypes; /* we need this for copy into from loader */ |
| 434 | list *colnames; /* we need this for copy into from loader */ |
| 435 | char *sname, *tname; /* we need this for create table from loader */ |
| 436 | } sql_subfunc; |
| 437 | |
| 438 | typedef struct sql_subaggr { |
| 439 | sql_func *aggr; |
| 440 | list *res; |
| 441 | } sql_subaggr; |
| 442 | |
| 443 | typedef enum key_type { |
| 444 | pkey, |
| 445 | ukey, |
| 446 | fkey |
| 447 | } key_type; |
| 448 | |
| 449 | typedef struct sql_kc { |
| 450 | struct sql_column *c; |
| 451 | int trunc; /* 0 not truncated, >0 colum is truncated */ |
| 452 | } sql_kc; |
| 453 | |
| 454 | typedef enum idx_type { |
| 455 | hash_idx, |
| 456 | join_idx, |
| 457 | oph_idx, /* order preserving hash */ |
| 458 | no_idx, /* no idx, ie no storage */ |
| 459 | imprints_idx, |
| 460 | ordered_idx, |
| 461 | new_idx_types |
| 462 | } idx_type; |
| 463 | |
| 464 | #define hash_index(t) (t == hash_idx || t == oph_idx ) |
| 465 | #define idx_has_column(t) (hash_index(t) || t == join_idx) |
| 466 | #define oid_index(t) (t == join_idx) |
| 467 | #define non_updatable_index(t) (t == ordered_idx || t == no_idx) |
| 468 | |
| 469 | typedef struct sql_idx { |
| 470 | sql_base base; |
| 471 | idx_type type; /* unique */ |
| 472 | struct list *columns; /* list of sql_kc */ |
| 473 | struct sql_table *t; |
| 474 | struct sql_key *key; /* key */ |
| 475 | struct sql_idx *po; /* the outer transactions idx */ |
| 476 | void *data; |
| 477 | } sql_idx; |
| 478 | |
| 479 | /* fkey consists of two of these */ |
| 480 | typedef struct sql_key { /* pkey, ukey, fkey */ |
| 481 | sql_base base; |
| 482 | key_type type; /* pkey, ukey, fkey */ |
| 483 | sql_idx *idx; /* idx to accelerate key check */ |
| 484 | |
| 485 | struct list *columns; /* list of sql_kc */ |
| 486 | struct sql_table *t; |
| 487 | int drop_action; /* only needed for alter drop key */ |
| 488 | } sql_key; |
| 489 | |
| 490 | typedef struct sql_ukey { /* pkey, ukey */ |
| 491 | sql_key k; |
| 492 | list *keys; |
| 493 | } sql_ukey; |
| 494 | |
| 495 | typedef struct sql_fkey { /* fkey */ |
| 496 | sql_key k; |
| 497 | /* no action, restrict (default), cascade, set null, set default */ |
| 498 | int on_delete; |
| 499 | int on_update; |
| 500 | struct sql_ukey *rkey; /* only set for fkey and rkey */ |
| 501 | } sql_fkey; |
| 502 | |
| 503 | typedef struct sql_trigger { |
| 504 | sql_base base; |
| 505 | sht time; /* before or after */ |
| 506 | sht orientation; /* row or statement */ |
| 507 | sht event; /* insert, delete, update, truncate */ |
| 508 | /* int action_order; TODO, order within the set of triggers */ |
| 509 | struct list *columns; /* update trigger on list of (sql_kc) columns */ |
| 510 | |
| 511 | struct sql_table *t; |
| 512 | char *old_name; /* name referencing the old values */ |
| 513 | char *new_name; /* name referencing the new values */ |
| 514 | |
| 515 | char *condition; /* when search condition, ie query */ |
| 516 | char *statement; /* action, ie list of sql statements */ |
| 517 | } sql_trigger; |
| 518 | |
| 519 | typedef struct sql_sequence { |
| 520 | sql_base base; |
| 521 | lng start; |
| 522 | lng minvalue; |
| 523 | lng maxvalue; |
| 524 | lng increment; |
| 525 | lng cacheinc; |
| 526 | bit cycle; |
| 527 | bit bedropped; /*Drop the SEQUENCE if you are dropping the column, e.g., SERIAL COLUMN".*/ |
| 528 | sql_schema *s; |
| 529 | } sql_sequence; |
| 530 | |
| 531 | typedef struct sql_column { |
| 532 | sql_base base; |
| 533 | sql_subtype type; |
| 534 | int colnr; |
| 535 | bit null; |
| 536 | char *def; |
| 537 | char unique; /* NOT UNIQUE, UNIQUE, SUB_UNIQUE */ |
| 538 | int drop_action; /* only used for alter statements */ |
| 539 | char *storage_type; |
| 540 | int sorted; /* for DECLARED (dupped tables) we keep order info */ |
| 541 | size_t dcount; |
| 542 | char *min; |
| 543 | char *max; |
| 544 | |
| 545 | struct sql_table *t; |
| 546 | struct sql_column *po; /* the outer transactions column */ |
| 547 | void *data; |
| 548 | } sql_column; |
| 549 | |
| 550 | typedef enum table_types { |
| 551 | tt_table = 0, /* table */ |
| 552 | tt_view = 1, /* view */ |
| 553 | tt_merge_table = 3, /* multiple tables form one table */ |
| 554 | tt_stream = 4, /* stream */ |
| 555 | tt_remote = 5, /* stored on a remote server */ |
| 556 | tt_replica_table = 6 /* multiple replica of the same table */ |
| 557 | } table_types; |
| 558 | |
| 559 | #define TABLE_TYPE_DESCRIPTION(tt,properties) \ |
| 560 | (tt == tt_table)?"TABLE":(tt == tt_view)?"VIEW":(tt == tt_merge_table && !properties)?"MERGE TABLE": \ |
| 561 | (tt == tt_stream)?"STREAM TABLE":(tt == tt_remote)?"REMOTE TABLE": \ |
| 562 | (tt == tt_merge_table && (properties & PARTITION_LIST) == PARTITION_LIST)?"LIST PARTITION TABLE": \ |
| 563 | (tt == tt_merge_table && (properties & PARTITION_RANGE) == PARTITION_RANGE)?"RANGE PARTITION TABLE":"REPLICA TABLE" |
| 564 | |
| 565 | #define isTable(x) (x->type==tt_table) |
| 566 | #define isView(x) (x->type==tt_view) |
| 567 | #define isNonPartitionedTable(x) (x->type==tt_merge_table && !x->properties) |
| 568 | #define isRangePartitionTable(x) (x->type==tt_merge_table && (x->properties & PARTITION_RANGE) == PARTITION_RANGE) |
| 569 | #define isListPartitionTable(x) (x->type==tt_merge_table && (x->properties & PARTITION_LIST) == PARTITION_LIST) |
| 570 | #define isPartitionedByColumnTable(x) (x->type==tt_merge_table && (x->properties & PARTITION_COLUMN) == PARTITION_COLUMN) |
| 571 | #define isPartitionedByExpressionTable(x) (x->type==tt_merge_table && (x->properties & PARTITION_EXPRESSION) == PARTITION_EXPRESSION) |
| 572 | #define isMergeTable(x) (x->type==tt_merge_table) |
| 573 | #define isStream(x) (x->type==tt_stream) |
| 574 | #define isRemote(x) (x->type==tt_remote) |
| 575 | #define isReplicaTable(x) (x->type==tt_replica_table) |
| 576 | #define isKindOfTable(x) (isTable(x) || isMergeTable(x) || isRemote(x) || isReplicaTable(x)) |
| 577 | #define isPartition(x) (isTable(x) && x->p) |
| 578 | |
| 579 | #define TABLE_WRITABLE 0 |
| 580 | #define TABLE_READONLY 1 |
| 581 | #define TABLE_APPENDONLY 2 |
| 582 | |
| 583 | typedef struct sql_part_value { |
| 584 | sql_subtype tpe; |
| 585 | ptr value; |
| 586 | size_t length; |
| 587 | } sql_part_value; |
| 588 | |
| 589 | typedef struct sql_part { |
| 590 | sql_base base; |
| 591 | struct sql_table *t; /* cached value of the merge table */ |
| 592 | sql_subtype tpe; /* the column/expression type */ |
| 593 | int with_nills; |
| 594 | union { |
| 595 | list *values; /* partition by values/list */ |
| 596 | struct sql_range { /* partition by range */ |
| 597 | ptr minvalue; |
| 598 | ptr maxvalue; |
| 599 | size_t minlength; |
| 600 | size_t maxlength; |
| 601 | } range; |
| 602 | } part; |
| 603 | } sql_part; |
| 604 | |
| 605 | typedef struct sql_expression { |
| 606 | sql_subtype type; /* the returning sql_subtype of the expression */ |
| 607 | char *exp; /* the expression itself */ |
| 608 | list *cols; /* list of colnr of the columns of the table used in the expression */ |
| 609 | } sql_expression; |
| 610 | |
| 611 | typedef struct sql_table { |
| 612 | sql_base base; |
| 613 | sht type; /* table, view, etc */ |
| 614 | sht access; /* writable, readonly, appendonly */ |
| 615 | bit system; /* system or user table */ |
| 616 | bit bootstrap; /* system table created during bootstrap */ |
| 617 | bte properties; /* used for merge_tables */ |
| 618 | temp_t persistence; /* persistent, global or local temporary */ |
| 619 | ca_t commit_action; /* on commit action */ |
| 620 | char *query; /* views may require some query */ |
| 621 | int sz; |
| 622 | |
| 623 | sql_ukey *pkey; |
| 624 | changeset columns; |
| 625 | changeset idxs; |
| 626 | changeset keys; |
| 627 | changeset triggers; |
| 628 | changeset members; |
| 629 | int drop_action; /* only needed for alter drop table */ |
| 630 | |
| 631 | int cleared; /* cleared in the current transaction */ |
| 632 | void *data; |
| 633 | struct sql_schema *s; |
| 634 | struct sql_table *po; /* the outer transactions table */ |
| 635 | |
| 636 | struct sql_table *p; /* The table is part of this merge table */ |
| 637 | union { |
| 638 | struct sql_column *pcol; /* If it is partitioned on a column */ |
| 639 | struct sql_expression *pexp; /* If it is partitioned by an expression */ |
| 640 | } part; |
| 641 | } sql_table; |
| 642 | |
| 643 | typedef struct sql_moved_table { |
| 644 | sql_schema *from; |
| 645 | sql_schema *to; |
| 646 | sql_table *t; |
| 647 | } sql_moved_table; |
| 648 | |
| 649 | typedef struct res_col { |
| 650 | char *tn; |
| 651 | char *name; |
| 652 | sql_subtype type; |
| 653 | bat b; |
| 654 | int mtype; |
| 655 | ptr *p; |
| 656 | } res_col; |
| 657 | |
| 658 | typedef struct res_table { |
| 659 | int id; |
| 660 | oid query_id; |
| 661 | sql_query_t query_type; |
| 662 | int nr_cols; |
| 663 | int cur_col; |
| 664 | const char *tsep; |
| 665 | const char *rsep; |
| 666 | const char *ssep; |
| 667 | const char *ns; |
| 668 | res_col *cols; |
| 669 | bat order; |
| 670 | struct res_table *next; |
| 671 | } res_table; |
| 672 | |
| 673 | typedef struct sql_session { |
| 674 | sql_trans *tr; /* active transaction */ |
| 675 | |
| 676 | char *schema_name; |
| 677 | sql_schema *schema; |
| 678 | |
| 679 | char ac_on_commit; /* if 1, auto_commit should be enabled on |
| 680 | commit, rollback, etc. */ |
| 681 | char auto_commit; |
| 682 | int level; /* TRANSACTION isolation level */ |
| 683 | int status; /* status, ok/error */ |
| 684 | backend_stack stk; |
| 685 | } sql_session; |
| 686 | |
| 687 | extern void schema_destroy(sql_schema *s); |
| 688 | extern void table_destroy(sql_table *t); |
| 689 | extern void column_destroy(sql_column *c); |
| 690 | extern void key_destroy(sql_key *k); |
| 691 | extern void idx_destroy(sql_idx * i); |
| 692 | |
| 693 | extern int base_key(sql_base *b); |
| 694 | extern node *list_find_name(list *l, const char *name); |
| 695 | extern node *list_find_id(list *l, sqlid id); |
| 696 | extern node *list_find_base_id(list *l, sqlid id); |
| 697 | |
| 698 | extern sql_key *find_sql_key(sql_table *t, const char *kname); |
| 699 | |
| 700 | extern sql_idx *find_sql_idx(sql_table *t, const char *kname); |
| 701 | |
| 702 | extern sql_column *find_sql_column(sql_table *t, const char *cname); |
| 703 | |
| 704 | extern sql_part *find_sql_part(sql_table *t, const char *tname); |
| 705 | |
| 706 | extern sql_table *find_sql_table(sql_schema *s, const char *tname); |
| 707 | extern sql_table *find_sql_table_id(sql_schema *s, sqlid id); |
| 708 | extern node *find_sql_table_node(sql_schema *s, sqlid id); |
| 709 | |
| 710 | extern sql_sequence *find_sql_sequence(sql_schema *s, const char *sname); |
| 711 | |
| 712 | extern sql_schema *find_sql_schema(sql_trans *t, const char *sname); |
| 713 | extern sql_schema *find_sql_schema_id(sql_trans *t, sqlid id); |
| 714 | extern node *find_sql_schema_node(sql_trans *t, sqlid id); |
| 715 | |
| 716 | extern sql_type *find_sql_type(sql_schema * s, const char *tname); |
| 717 | extern sql_type *sql_trans_bind_type(sql_trans *tr, sql_schema *s, const char *name); |
| 718 | extern node *find_sql_type_node(sql_schema *s, sqlid id); |
| 719 | |
| 720 | extern sql_func *find_sql_func(sql_schema * s, const char *tname); |
| 721 | extern list *find_all_sql_func(sql_schema * s, const char *tname, sql_ftype type); |
| 722 | extern sql_func *sql_trans_bind_func(sql_trans *tr, const char *name); |
| 723 | extern sql_func *sql_trans_find_func(sql_trans *tr, sqlid id); |
| 724 | extern node *find_sql_func_node(sql_schema *s, sqlid id); |
| 725 | |
| 726 | extern void *sql_values_list_element_validate_and_insert(void *v1, void *v2, int* res); |
| 727 | extern void *sql_range_part_validate_and_insert(void *v1, void *v2); |
| 728 | extern void *sql_values_part_validate_and_insert(void *v1, void *v2); |
| 729 | |
| 730 | typedef struct { |
| 731 | BAT *b; |
| 732 | char* name; |
| 733 | void* def; |
| 734 | } sql_emit_col; |
| 735 | |
| 736 | #endif /* SQL_CATALOG_H */ |
| 737 | |