LCOV - code coverage report
Current view: top level - sql/server - rel_semantic.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 84 99 84.8 %
Date: 2024-12-20 21:24:02 Functions: 2 2 100.0 %

          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             : #include "monetdb_config.h"
      14             : #include "sql_parser.h"
      15             : #include "sql_symbol.h"
      16             : #include "rel_semantic.h"
      17             : #include "rel_select.h"
      18             : #include "rel_updates.h"
      19             : #include "rel_trans.h"
      20             : #include "rel_schema.h"
      21             : #include "rel_psm.h"
      22             : #include "rel_sequence.h"
      23             : #include "rel_exp.h"
      24             : #include "sql_privileges.h"
      25             : 
      26             : #include <unistd.h>
      27             : #include <string.h>
      28             : #include <ctype.h>
      29             : 
      30             : sql_rel *
      31      198471 : rel_parse(mvc *m, sql_schema *s, const char *query, char emode)
      32             : {
      33      198471 :         sql_rel *rel = NULL;
      34      198471 :         buffer *b;
      35      198471 :         bstream *bs;
      36      198471 :         stream *buf;
      37      198471 :         char *n;
      38      198471 :         size_t len = _strlen(query);
      39      198471 :         sql_schema *c = cur_schema(m);
      40      198471 :         sql_query *qc = NULL;
      41             : 
      42      198471 :         if ((b = malloc(sizeof(buffer))) == NULL)
      43             :                 return NULL;
      44      198471 :         if ((n = malloc(len + 1 + 1)) == NULL) {
      45           0 :                 free(b);
      46           0 :                 return sql_error(m, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
      47             :         }
      48      198471 :         snprintf(n, len + 2, "%s\n", query);
      49      198471 :         len++;
      50      198471 :         buffer_init(b, n, len);
      51      198471 :         buf = buffer_rastream(b, "sqlstatement");
      52      198471 :         if(buf == NULL) {
      53           0 :                 buffer_destroy(b);
      54           0 :                 return sql_error(m, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
      55             :         }
      56      198471 :         bs = bstream_create(buf, b->len);
      57      198471 :         if(bs == NULL) {
      58           0 :                 buffer_destroy(b);
      59           0 :                 return sql_error(m, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
      60             :         }
      61      198471 :         mvc o = *m;
      62      198471 :         scanner_init( &m->scanner, bs, NULL);
      63      198471 :         m->scanner.mode = LINE_1;
      64      198471 :         bstream_next(m->scanner.rs);
      65             : 
      66      198471 :         m->qc = NULL;
      67      198471 :         m->emode = emode;
      68      198471 :         if (s)
      69      198470 :                 m->session->schema = s;
      70      198471 :         m->params = NULL;
      71      198471 :         m->sym = NULL;
      72      198471 :         m->errstr[0] = '\0';
      73      198471 :         m->session->status = 0;
      74             :         /* via views we give access to protected objects */
      75      198471 :         assert(emode == m_instantiate || emode == m_deps || emode == m_prepare);
      76      198471 :         m->user_id = USER_MONETDB;
      77             : 
      78      198471 :         (void) sqlparse(m);     /* blindly ignore errors */
      79      198471 :         qc = query_create(m);
      80      198471 :         rel = rel_semantic(qc, m->sym);
      81             : 
      82      198471 :         buffer_destroy(b);
      83      198471 :         bstream_destroy(m->scanner.rs);
      84             : 
      85      198471 :         m->sym = NULL;
      86      198471 :         o.frames = m->frames;        /* may have been realloc'ed */
      87      198471 :         o.sizeframes = m->sizeframes;
      88      198471 :         if (m->session->status || m->errstr[0]) {
      89          13 :                 int status = m->session->status;
      90             : 
      91          13 :                 strcpy(o.errstr, m->errstr);
      92          13 :                 *m = o;
      93          13 :                 m->session->status = status;
      94             :         } else {
      95      198458 :                 unsigned int label = m->label, nid = m->nid;
      96             : 
      97      198458 :                 while (m->topframes > o.topframes)
      98           0 :                         clear_frame(m, m->frames[--m->topframes]);
      99      198458 :                 *m = o;
     100      198458 :                 m->label = label;
     101      198458 :                 m->nid = nid;
     102             :         }
     103      198471 :         m->session->schema = c;
     104      198471 :         return rel;
     105             : }
     106             : 
     107             : sql_rel *
     108      792069 : rel_semantic(sql_query *query, symbol *s)
     109             : {
     110      792069 :         mvc *sql = query->sql;
     111      792069 :         if (!s)
     112             :                 return NULL;
     113             : 
     114      792069 :         switch (s->token) {
     115             : 
     116        3162 :         case TR_COMMIT:
     117             :         case TR_SAVEPOINT:
     118             :         case TR_RELEASE:
     119             :         case TR_ROLLBACK:
     120             :         case TR_START:
     121             :         case TR_MODE:
     122        3162 :                 return rel_transactions(query, s);
     123             : 
     124      219038 :         case SQL_CREATE_SCHEMA:
     125             :         case SQL_DROP_SCHEMA:
     126             : 
     127             :         case SQL_DECLARE_TABLE:
     128             :         case SQL_CREATE_TABLE:
     129             :         case SQL_CREATE_VIEW:
     130             :         case SQL_DROP_TABLE:
     131             :         case SQL_DROP_VIEW:
     132             :         case SQL_ALTER_TABLE:
     133             : 
     134             :         case SQL_COMMENT:
     135             : 
     136             :         case SQL_GRANT:
     137             :         case SQL_REVOKE:
     138             :         case SQL_GRANT_ROLES:
     139             :         case SQL_REVOKE_ROLES:
     140             : 
     141             :         case SQL_CREATE_ROLE:
     142             :         case SQL_DROP_ROLE:
     143             : 
     144             :         case SQL_CREATE_INDEX:
     145             :         case SQL_DROP_INDEX:
     146             : 
     147             :         case SQL_CREATE_USER:
     148             :         case SQL_DROP_USER:
     149             :         case SQL_ALTER_USER:
     150             : 
     151             :         case SQL_RENAME_COLUMN:
     152             :         case SQL_RENAME_SCHEMA:
     153             :         case SQL_RENAME_TABLE:
     154             :         case SQL_RENAME_USER:
     155             :         case SQL_SET_TABLE_SCHEMA:
     156             : 
     157             :         case SQL_CREATE_TYPE:
     158             :         case SQL_DROP_TYPE:
     159      219038 :                 return rel_schemas(query, s);
     160             : 
     161         406 :         case SQL_CREATE_SEQ:
     162             :         case SQL_ALTER_SEQ:
     163             :         case SQL_DROP_SEQ:
     164         406 :                 return rel_sequences(query, s);
     165             : 
     166      260364 :         case SQL_CREATE_FUNC:
     167             :         case SQL_DROP_FUNC:
     168             :         case SQL_DECLARE:
     169             :         case SQL_CALL:
     170             :         case SQL_SET:
     171             : 
     172             :         case SQL_CREATE_TABLE_LOADER:
     173             : 
     174             :         case SQL_CREATE_TRIGGER:
     175             :         case SQL_DROP_TRIGGER:
     176             : 
     177             :         case SQL_ANALYZE:
     178      260364 :                 return rel_psm(query, s);
     179             : 
     180      149325 :         case SQL_INSERT:
     181             :         case SQL_UPDATE:
     182             :         case SQL_DELETE:
     183             :         case SQL_TRUNCATE:
     184             :         case SQL_MERGE:
     185             :         case SQL_COPYFROM:
     186             :         case SQL_COPYINTO:
     187             :         case SQL_BINCOPYFROM:
     188             :         case SQL_BINCOPYINTO:
     189             :         case SQL_COPYLOADER:
     190      149325 :                 return rel_updates(query, s);
     191             : 
     192         383 :         case SQL_WITH:
     193         383 :                 return rel_with_query(query, s);
     194             : 
     195         182 :         case SQL_MULSTMT: {
     196         182 :                 dnode *d;
     197         182 :                 sql_rel *r = NULL;
     198             : 
     199         182 :                 if (!stack_push_frame(sql, "%MUL"))
     200           0 :                         return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     201         558 :                 for (d = s->data.lval->h; d; d = d->next) {
     202         378 :                         symbol *sym = d->data.sym;
     203         378 :                         sql_rel *nr = rel_semantic(query, sym);
     204             : 
     205         378 :                         if (!nr) {
     206           2 :                                 stack_pop_frame(sql);
     207           2 :                                 return NULL;
     208             :                         }
     209         376 :                         if (r)
     210         194 :                                 r = rel_list(sql->sa, r, nr);
     211             :                         else
     212             :                                 r = nr;
     213             :                 }
     214         180 :                 stack_pop_frame(sql);
     215         180 :                 return r;
     216             :         }
     217           0 :         case SQL_PREP:
     218             :         {
     219           0 :                 dnode *d = s->data.lval->h;
     220           0 :                 symbol *sym = d->data.sym;
     221           0 :                 sql_rel *r = rel_semantic(query, sym);
     222             : 
     223           0 :                 if (!r)
     224             :                         return NULL;
     225             :                 return r;
     226             :         }
     227             : 
     228      159209 :         case SQL_SELECT:
     229             :         case SQL_JOIN:
     230             :         case SQL_UNION:
     231             :         case SQL_EXCEPT:
     232             :         case SQL_INTERSECT:
     233             :         case SQL_VALUES:
     234      159209 :                 return rel_selects(query, s);
     235             : 
     236           0 :         default:
     237           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "Symbol type not found");
     238             :         }
     239             : }

Generated by: LCOV version 1.14