MXS-1249: Add json functions

The json function are treated as read-only when the server version
is >= 10.2.3.
https://mariadb.com/kb/en/mariadb/json-functions/
This commit is contained in:
Johan Wikman 2017-06-19 18:16:44 +03:00
parent 4696c7e662
commit 43895b6f28
3 changed files with 76 additions and 10 deletions

View File

@ -1,5 +1,14 @@
/*
* @licence@
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2020-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include "builtin_functions.h"
@ -368,6 +377,42 @@ static const char* BUILTIN_FUNCTIONS[] =
const size_t N_BUILTIN_FUNCTIONS = sizeof(BUILTIN_FUNCTIONS) / sizeof(BUILTIN_FUNCTIONS[0]);
// The functions have been taken from:
// https://mariadb.com/kb/en/mariadb/json-functions
static const char* BUILTIN_10_2_3_FUNCTIONS[] =
{
"json_array",
"json_array_append",
"json_array_insert",
"json_compact",
"json_contains",
"json_contains_path",
"json_depth",
"json_detailed",
"json_exists",
"json_extract",
"json_insert",
"json_keys",
"json_length",
"json_loose",
"json_merge",
"json_object",
"json_query",
"json_quote",
"json_remove"
"json_replace",
"json_search",
"json_set",
"json_type",
"json_unquote",
"json_valid",
"json_value"
};
const size_t N_BUILTIN_10_2_3_FUNCTIONS =
sizeof(BUILTIN_10_2_3_FUNCTIONS) / sizeof(BUILTIN_10_2_3_FUNCTIONS[0]);
// NOTE: sort_compare and search_compare are not identical, so don't
// NOTE: optimize either of them away.
static int sort_compare(const void* key, const void* value)
@ -389,6 +434,7 @@ void init_builtin_functions()
ss_dassert(!unit.inited);
qsort(BUILTIN_FUNCTIONS, N_BUILTIN_FUNCTIONS, sizeof(char*), sort_compare);
qsort(BUILTIN_10_2_3_FUNCTIONS, N_BUILTIN_10_2_3_FUNCTIONS, sizeof(char*), sort_compare);
unit.inited = true;
}
@ -399,11 +445,22 @@ void finish_builtin_functions()
unit.inited = false;
}
bool is_builtin_readonly_function(const char* key)
bool is_builtin_readonly_function(const char* key, uint32_t major, uint32_t minor, uint32_t patch)
{
ss_dassert(unit.inited);
char* value = bsearch(key, BUILTIN_FUNCTIONS, N_BUILTIN_FUNCTIONS, sizeof(char*), search_compare);
if (!value)
{
if ((major > 10) ||
((major == 10) && (minor > 2)) ||
((major == 10) && (minor == 2) && (patch >= 3)))
{
value = bsearch(key, BUILTIN_10_2_3_FUNCTIONS, N_BUILTIN_10_2_3_FUNCTIONS,
sizeof(char*), search_compare);
}
}
return value ? true : false;
}

View File

@ -1,11 +1,19 @@
#ifndef BUILTIN_FUNCTIONS_H
#define BUILTIN_FUNCTIONS_H
/**
* @LICENCE@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2020-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@ -14,10 +22,8 @@ extern "C" {
void init_builtin_functions();
void finish_builtin_functions();
bool is_builtin_readonly_function(const char* zToken);
bool is_builtin_readonly_function(const char* zToken, uint32_t major, uint32_t minor, uint32_t patch);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1105,7 +1105,10 @@ static void update_field_infos(QC_SQLITE_INFO* info,
{
info->type_mask |= (QUERY_TYPE_READ | QUERY_TYPE_MASTER_READ);
}
else if (!is_builtin_readonly_function(zToken))
else if (!is_builtin_readonly_function(zToken,
this_thread.version_major,
this_thread.version_minor,
this_thread.version_patch))
{
info->type_mask |= QUERY_TYPE_WRITE;
}