如何给MySQL添加自定义语法 ?
1 背景
MySQL语法解析器用的bison(即yacc)来实现的,而词法解析是自己来实现的,涉及到的token都在文件lex.h
里面,然后通过Lex_input_stream
里面相关的函数,解析client的sql字节流(其中会通过提前构造好的hash表帮助快速找到对应symbol,相关代码在sql_lex_hash.cc
里面,转换为token,交给bison进行语法解析。
为了给MySQL添加一个新的语法,我们必须添加新的token(如果有新增),以及增加新的语法(sql_yacc.yy)里面。本文以给create table增加一个新的options为例,来演示如何给MySQL新增一个语法。最终的效果如下:
create table t1 (
id int primary key,
name varchar(100)
) global_partition by hash(id) partitions 10; //global_partition by为新增语法,global_partition为新增token
涉及到的修改文件如下:
sql/lex.h //token
sql/parse_tree_nodes.cc
sql/parse_tree_nodes.h
sql/parse_tree_partitions.cc
sql/parse_tree_partitions.h
sql/parser_yystype.h
sql/sql_yacc.yy
2 新增关键词(token)
文件:sql/lex.h
static const SYMBOL symbols[] = {
/*
Insert new SQL keywords after that commentary (by alphabetical order):
*/
//省略部分代码
{SYM("GLOBAL_PARTITION", GLOBAL_PARTITION_SYM)}, //注意按照字典序进行添加。
//省略部分代码
};
按照上面的格式添加即可
3 新增语法
文件:sql/sql_yacc.yy
该文件为bison的语法,关于bison语法可以查看这里。下面凡是注释标有###为新增部分,没有标的注释是为了方便理解
%token<lexer.keyword> GLOBAL_PARTITION_SYM 1309 /* seancheer */ //### 声明上一步添加的token,声明了才可以使用,编号1309选择一个未使用的就行
%type <global_partition_clause> global_partition_clause //### 声明新增加的数据结构,后面会介绍
create_table_stmt:
CREATE opt_temporary TABLE_SYM opt_if_not_exists table_ident
'(' table_element_list ')' opt_create_table_options_etc //最后一个标记在YYSTYPE中对应的是create_table_tail, 后面会介绍
{
$$= NEW_PTN PT_create_table_stmt(YYMEM_ROOT, $1, $2, $4, $5,
$7,
$9.opt_create_table_options,
$9.opt_partitioning,
$9.opt_global_partitioning, //### 赋值给对应参数,该构造函数需要新增,后面会介绍
$9.on_duplicate,
$9.opt_query_expression);
}
| CREATE opt_temporary TABLE_SYM opt_if_not_exists table_ident
opt_create_table_options_etc
{
$$= NEW_PTN PT_create_table_stmt(YYMEM_ROOT, $1, $2, $4, $5,
NULL,
$6.opt_create_table_options,
$6.opt_partitioning,
$6.opt_global_partitioning, //### 赋值给对应参数,该构造函数需要新增,后面会介绍
$6.on_duplicate,
$6.opt_query_expression);
//partition相关的语法
opt_create_partitioning_etc:
partition_clause opt_duplicate_as_qe //这里是原生的partition表语法
{
$$= $2;
$$.opt_partitioning= $1;
}
| global_partition_clause opt_duplicate_as_qe //### 此处是新增的global_partition语法,
{
$$= $2;
$$.opt_global_partitioning= $1;
}
| opt_duplicate_as_qe
;
//### 下面为重点,新增的global_partition语法,可以看到,用到了新增的token
global_partition_clause:
GLOBAL_PARTITION_SYM BY part_type_def opt_num_parts
{
$$= NEW_PTN PT_global_partition($3, @4, $4);
}
;
4 类似于PT_partition
添加对应的数据结构global_partition_clause
文件:parser_yystype.h
:该文件是bison(yacc)运行的一环,代替bison内置的YYSTYPE
的,当bison对相关语法解析后,需要构造相关的数据结构,通过对YYSTYPE的自定义,就可以实现构造自定义数据结构的目的了。添加我们自定义的数据结构代码如下:
union YYSTYPE {
PT_sub_partition *opt_sub_part;
PT_part_type_def *part_type_def;
PT_partition *partition_clause;
PT_global_partition *global_partition_clause; //新加数据结构
struct {
Mem_root_array<PT_create_table_option *> *opt_create_table_options;
PT_partition *opt_partitioning;
PT_global_partition *opt_global_partitioning; //同时注意添加到create_table_tail里面,因为create table语法会有该操作
On_duplicate on_duplicate;
PT_query_primary *opt_query_expression;
} create_table_tail;
};
static_assert(sizeof(YYSTYPE) <= 40, "YYSTYPE is too big"); //因为struct里面添加了一个成员变量,所以该union需要的空间也会变大,因此注意修改这一行
下面内容介绍PT_global_partition
数据结构,为了保持和MySQL习惯一致,新增加的数据结构放在了
sql/parse_tree_nodes.cc sql/parse_tree_nodes.h sql/parse_tree_partitions.cc sql/parse_tree_partitions.h
四个文件里,理论上可以放在任何地方。可根据自身需求添加对应数据结构:
文件:sql/parse_tree_partitions.h
sql/parse_tree_partitions.cc
/**
新增数据结构
*/
class PT_global_partition : public Parse_tree_node {
typedef Parse_tree_node super;
PT_part_type_def *const part_type_def;
const POS part_defs_pos;
uint num_parts;
public:
partition_info part_info;
public:
PT_global_partition(PT_part_type_def *part_type_def, const POS &part_defs_pos,
uint opt_num_parts)
: part_type_def(part_type_def),
part_defs_pos(part_defs_pos),
num_parts(opt_num_parts) {}
bool contextualize(Parse_context *pc) override;
};
//模仿其原生的实现方式即可
bool PT_global_partition::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
Partition_parse_context part_pc(pc->thd, &part_info, false);
if (part_type_def->contextualize(&part_pc)) return true;
if (part_info.part_type != partition_type::HASH) {
//only support hash partition for shard key
my_error(ER_PARTITIONS_MUST_BE_DEFINED_ERROR, MYF(0), "NOT HASH");
return true;
}
uint count_curr_parts = part_info.partitions.elements;
if (part_info.num_parts != 0) {
if (part_info.num_parts != count_curr_parts) {
error(&part_pc, part_defs_pos,
ER_THD(pc->thd, ER_PARTITION_WRONG_NO_PART_ERROR));
return true;
}
} else if (count_curr_parts > 0)
part_info.num_parts = count_curr_parts;
return false;
}
文件:sql/parse_tree_nodes.cc
sql/parse_tree_nodes.h
接下来修改create table对应的数据结构,将新增的PT_global_partition
添加到create table里面
class PT_create_table_stmt final : public PT_table_ddl_stmt_base {
PT_partition *opt_partitioning;
PT_global_partition *opt_global_partitioning; //添加成员变量
PT_create_table_stmt(
MEM_ROOT *mem_root, PT_hint_list *opt_hints, bool is_temporary,
bool only_if_not_exists, Table_ident *table_name,
const Mem_root_array<PT_table_element *> *opt_table_element_list,
const Mem_root_array<PT_create_table_option *> *opt_create_table_options,
PT_partition *opt_partitioning,
PT_global_partition *opt_global_partitioning, On_duplicate on_duplicate,
PT_query_primary *opt_query_expression)
: PT_table_ddl_stmt_base(mem_root),
m_opt_hints(opt_hints),
is_temporary(is_temporary),
only_if_not_exists(only_if_not_exists),
table_name(table_name),
opt_table_element_list(opt_table_element_list),
opt_create_table_options(opt_create_table_options),
opt_partitioning(opt_partitioning),
opt_global_partitioning(opt_global_partitioning), //添加构造函数,主要是为了增加对PT_global_partition的赋值操作
on_duplicate(on_duplicate),
opt_query_expression(opt_query_expression),
opt_like_clause(nullptr) {}
//在其对应的函数中增加相关逻辑,调用对应的初始化函数contextualize
Sql_cmd *PT_create_table_stmt::make_cmd(THD *thd) {
if (opt_global_partitioning){
if (opt_global_partitioning->contextualize(&pc)) return nullptr;
lex->part_info = &opt_global_partitioning->part_info;
}
}
如何给MySQL添加自定义语法 ?的更多相关文章
- 对MySQL DELETE语法的详细解析
以下的文章主要描述的是MySQL DELETE语法的详细解析,首先我们是从单表语法与多表语法的示例开始的,假如你对MySQL DELETE语法的相关内容十分感兴趣的话,你就可以浏览以下的文章对其有个更 ...
- MySQL ALTER语法的运用方法 && 操作索引和字段
语法:alter_specification: ADD [COLUMN] create_definition [FIRST | AFTER column_name ] or ADD INDEX [in ...
- MySQL基本语法(一):和SQL Server语法的差异小归纳
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- MySQL索引语法+使用场景
MySQL索引语法 建表时添加索引 建表同时建立单索引 CREATE TABLE t_user1(id INT , userName VARCHAR(20), PASSWORD VARCHAR(20) ...
- MySQL PHP 语法
MySQL PHP 语法 MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,MySQL在PHP的web开发中是应用最广泛. 在本教程中我们大部分实例 ...
- MySQL数据库语法(一)
MySQL数据库语法 数据库管理系统(DBMS)的概述 什么是DBMS:数据的仓库 方便查询 可存储的数据量大 保证数据的完整.一致 安全可靠 DBMS的发展:今天主流数据库为关系型数据库管理系统(R ...
- MySQL数据库语法-多表查询练习一
MySQL数据库语法-多表查询练习一 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍的多表查询的外键约束,以及如何使用外链接和内连接查询数据信息. 一.数据表和测试 ...
- MYSQL从入门到放弃系列:mysql基础语法
Mysql基本语法 启动MySQL net start mysql 连接与断开服务器 mysql -h 地址 -P 端口 -u 用户名 -p 密码 跳过权限验证登录MySQL mysqld --ski ...
- 三、MySQL PHP 语法
MySQL PHP 语法 MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,Mysql在PHP的web开发中是应用最广泛. 在本教程中我们大部分实例 ...
随机推荐
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- Fastflow——基于golang的轻量级工作流框架
Fastflow 是什么?用一句话来定义它:一个 基于golang协程.支持水平扩容的分布式高性能工作流框架. 它具有以下特点: 易用性:工作流模型基于 DAG 来定义,同时还提供开箱即用的 API, ...
- MTK 虚拟 sensor bring up (pick up) sensor2.0
pick up bring up sensor2.0 1.SCP侧的配置 (1) 放置驱动pickup.c (2) 添加底层驱动文件编译开关 (3) 加入编译文件 (4) 增加数据上报方式 (5)修改 ...
- 硬件开发笔记(四):硬件开发基本流程,制作一个USB转RS232的模块(三):设计原理图
前者 前面建立好的基础的元器件,下面开始设计原理图. 需求 USB转RS232,输出RS232 可以选择性输出5V的TTL 可以选择性输出3.3V的TTL 设计原理图 步骤一:CH340G ...
- Spring-Cloud-Alibaba系列教程(一)Nacos初识
前言 在2020年即将开启SpringCloudAlibaba的专题,希望2020年共同学习进步. 学习资料 文档 Naco文档 程序猿DD Spring Cloud Aliabab专题 专题博客 视 ...
- 【ASP.NET Core】配置应用程序地址的N多种方法
下面又到了老周误人子弟的时间,今天要误大伙的话题是:找找有多少种方法可以设置 ASP.NET Core 应用的地址,即 URL. 精彩马上开始! 1.UseUrls 方法 这是一个扩展方法,参数是可变 ...
- 图解MySQL逻辑备份的实现流程
1. 摘要 数据作为一家公司的重要资产,其重要程度不言而喻.数据库为数据提供存取服务,担任着重要的角色,如果因数据误删.服务器故障.病毒入侵等原因导致数据丢失或服务不可用,会对公司造成重大损失,所以数 ...
- Java 基础常见知识点&面试题总结(上),2022 最新版!| JavaGuide
你好,我是 Guide.秋招即将到来,我对 JavaGuide 的内容进行了重构完善,公众号同步一下最新更新,希望能够帮助你. 基础概念与常识 Java 语言有哪些特点? 简单易学: 面向对象(封装, ...
- Lucene从入门到实战
Lucene 在了解Lucene之前,我们先了解下全文数据查询. 全文数据查询 我们的数据一般分为两种:结构化数据和非结构化数据 结构化数据:有固定格式或有限长度的数据,如数据库中的数据.元数据 非结 ...
- WinForms拖控件拖到天荒地老
更新记录: 2022年4月15日:本文迁移自Panda666原博客,原发布时间:2021年4月18日. 2022年4月15日:更新自动生成Web CURD工具. 说明 Winforms的控件拖起来是真 ...