LCOV - code coverage report
Current view: top level - sql/server - rel_semantic.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 73 89 82.0 %
Date: 2021-10-13 02:24:04 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.14