LCOV - code coverage report
Current view: top level - sql/server - sql_env.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 34 97.1 %
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             : /*
      10             :  * Environment variables
      11             :  * =====================
      12             :  *
      13             :  * The processing setting of the SQL front-end can collect information
      14             :  * for postprocessing and debugging by setting a flag
      15             :  * using the SQL construct:
      16             :  * SET <variable>=<string>
      17             :  * SET <variable>=<boolean>
      18             :  * SET <variable>=<int>
      19             :  *
      20             :  * The SQL engine comes with a limited set of environment variables
      21             :  * to control its behavior.
      22             :  * The 'debug' variable takes an integer and sets the Mserver global
      23             :  * debug flag. (See MonetDB documentation.)
      24             :  *
      25             :  * By default all remaining variables are stored as strings and
      26             :  * any type analysis is up to the user. The can be freely used by the
      27             :  * SQL programmer for inclusion in his queries.
      28             :  *
      29             :  * The limited number of built-in variables defined above are
      30             :  * strongly typed the hard way.
      31             :  * Moreover, they have a counterpart representation in the
      32             :  * MVC structure to ease inspection during query processing.
      33             :  *
      34             :  * The variables can be retrieved using the table producing function var();
      35             :  */
      36             : 
      37             : #include "monetdb_config.h"
      38             : #include "sql_env.h"
      39             : #include "sql_semantic.h"
      40             : #include "sql_privileges.h"
      41             : #include "mal_exception.h"
      42             : 
      43             : #ifdef HAVE_HGE
      44             : #define VAR_UPCAST hge
      45             : #else
      46             : #define VAR_UPCAST lng
      47             : #endif
      48             : 
      49             : str
      50        1795 : sql_update_var(mvc *m, sql_schema *s, const char *name, ValPtr ptr)
      51             : {
      52        1795 :         if (strcmp(s->base.name, "sys") == 0) {
      53        1795 :                 if (strcmp(name, "debug") == 0 || strcmp(name, "current_timezone") == 0) {
      54        1535 :                         VAR_UPCAST sgn = val_get_number(ptr);
      55             : 
      56        1535 :                         if (VALisnil(ptr))
      57           2 :                                 throw(SQL,"sql.update_var", SQLSTATE(42000) "Variable '%s.%s' cannot be NULL\n", s->base.name, name);
      58        1533 :                         if (sgn <= (VAR_UPCAST) GDK_int_min)
      59           0 :                                 throw(SQL,"sql.update_var", SQLSTATE(42000) "Value too small for '%s.%s'\n", s->base.name, name);
      60        1533 :                         if (sgn > (VAR_UPCAST) GDK_int_max)
      61           1 :                                 throw(SQL,"sql.update_var", SQLSTATE(42000) "Value too large for '%s.%s'\n", s->base.name, name);
      62             : 
      63             :                         if (/* DISABLES CODE */ (0) && strcmp(name, "debug") == 0)
      64             :                                 m->debug = (int) sgn;
      65        1532 :                         else if (strcmp(name, "current_timezone") == 0)
      66        1528 :                                 m->timezone = (int) sgn;
      67         260 :                 } else if (strcmp(name, "current_schema") == 0 || strcmp(name, "current_role") == 0) {
      68         260 :                         if (VALisnil(ptr))
      69           2 :                                 throw(SQL,"sql.update_var", SQLSTATE(42000) "Variable '%s.%s' cannot be NULL\n", s->base.name, name);
      70         258 :                         if (strcmp(name, "current_schema") == 0 && !mvc_set_schema(m, ptr->val.sval))
      71           1 :                                 throw(SQL,"sql.update_var", SQLSTATE(3F000) "Schema (%s) missing\n", ptr->val.sval);
      72         257 :                         else if (strcmp(name, "current_role") == 0 && !mvc_set_role(m, ptr->val.sval))
      73           9 :                                 throw(SQL,"sql.update_var", SQLSTATE(42000) "Role (%s) missing\n", ptr->val.sval);
      74             :                 }
      75             :         }
      76             :         return NULL;
      77             : }
      78             : 
      79             : int
      80         186 : sql_create_env(mvc *m, sql_schema *s)
      81             : {
      82             :         list *res, *ops;
      83         186 :         sql_func *f = NULL;
      84             : 
      85         186 :         res = sa_list(m->sa);
      86         186 :         list_append(res, sql_create_arg(m->sa, "name", sql_bind_subtype(m->sa, "varchar", 1024, 0), ARG_OUT));
      87         186 :         list_append(res, sql_create_arg(m->sa, "value", sql_bind_subtype(m->sa, "varchar", 2048, 0), ARG_OUT));
      88             : 
      89             :         /* add function */
      90         186 :         ops = sa_list(m->sa);
      91         186 :         mvc_create_func(&f, m, NULL, s, "env", ops, res, F_UNION, FUNC_LANG_SQL, "inspect", "getEnvironment", "CREATE FUNCTION env() RETURNS TABLE( name varchar(1024), value varchar(2048)) EXTERNAL NAME inspect.\"getEnvironment\";", FALSE, FALSE, TRUE);
      92             : 
      93         186 :         res = sa_list(m->sa);
      94         186 :         list_append(res, sql_create_arg(m->sa, "schema", sql_bind_localtype("str"), ARG_OUT));
      95         186 :         list_append(res, sql_create_arg(m->sa, "name", sql_bind_localtype("str"), ARG_OUT));
      96         186 :         list_append(res, sql_create_arg(m->sa, "type", sql_bind_localtype("str"), ARG_OUT));
      97         186 :         list_append(res, sql_create_arg(m->sa, "value", sql_bind_localtype("str"), ARG_OUT));
      98             : 
      99             :         /* add function */
     100         186 :         ops = sa_list(m->sa);
     101         186 :         mvc_create_func(&f, m, NULL, s, "var", ops, res, F_UNION, FUNC_LANG_SQL, "sql", "sql_variables", "create function \"sys\".\"var\"() returns table(\"schema\" string, \"name\" string, \"type\" string, \"value\" string) external name \"sql\".\"sql_variables\";", FALSE, FALSE, TRUE);
     102         186 :         return 0;
     103             : }

Generated by: LCOV version 1.14