欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何給MySQL添加自定義語法的方法示例

 更新時(shí)間:2022年08月05日 15:22:50   作者:秋風(fēng)五丈原  
本文主要介紹了如何給MySQL添加自定義語法的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1 背景

MySQL語法解析器用的bison(即yacc)來實(shí)現(xiàn)的,而詞法解析是自己來實(shí)現(xiàn)的,涉及到的token都在文件lex.h里面,然后通過Lex_input_stream 里面相關(guān)的函數(shù),解析client的sql字節(jié)流(其中會(huì)通過提前構(gòu)造好的hash表幫助快速找到對(duì)應(yīng)symbol,相關(guān)代碼在sql_lex_hash.cc里面),轉(zhuǎn)換為token,交給bison進(jìn)行語法解析。

為了給MySQL添加一個(gè)新的語法,我們必須添加新的token(如果有新增),以及增加新的語法(sql_yacc.yy)里面。本文以給create table增加一個(gè)新的options為例,來演示如何給MySQL新增一個(gè)語法。最終的效果如下:

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 新增關(guān)鍵詞(token)

文件:sql/lex.h

static const SYMBOL symbols[] = {
    /*
     Insert new SQL keywords after that commentary (by alphabetical order):
    */
    //省略部分代碼
    {SYM("GLOBAL_PARTITION", GLOBAL_PARTITION_SYM)}, //注意按照字典序進(jìn)行添加。
    //省略部分代碼
};
  

按照上面的格式添加即可

3 新增語法

文件:sql/sql_yacc.yy

該文件為bison的語法,關(guān)于bison語法可以查看這里。下面凡是注釋標(biāo)有###為新增部分,沒有標(biāo)的注釋是為了方便理解

%token<lexer.keyword> GLOBAL_PARTITION_SYM 1309            /* seancheer */  //### 聲明上一步添加的token,聲明了才可以使用,編號(hào)1309選擇一個(gè)未使用的就行
%type <global_partition_clause> global_partition_clause //### 聲明新增加的數(shù)據(jù)結(jié)構(gòu),后面會(huì)介紹

create_table_stmt:
          CREATE opt_temporary TABLE_SYM opt_if_not_exists table_ident
          '(' table_element_list ')' opt_create_table_options_etc //最后一個(gè)標(biāo)記在YYSTYPE中對(duì)應(yīng)的是create_table_tail, 后面會(huì)介紹
          {
            $$= 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, //### 賦值給對(duì)應(yīng)參數(shù),該構(gòu)造函數(shù)需要新增,后面會(huì)介紹
                                             $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, //### 賦值給對(duì)應(yīng)參數(shù),該構(gòu)造函數(shù)需要新增,后面會(huì)介紹
                                             $6.on_duplicate,
                                             $6.opt_query_expression);
//partition相關(guān)的語法                                             
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
        ;

//### 下面為重點(diǎn),新增的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添加對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu)global_partition_clause

文件:parser_yystype.h:該文件是bison(yacc)運(yùn)行的一環(huán),代替bison內(nèi)置的YYSTYPE的,當(dāng)bison對(duì)相關(guān)語法解析后,需要構(gòu)造相關(guān)的數(shù)據(jù)結(jié)構(gòu),通過對(duì)YYSTYPE的自定義,就可以實(shí)現(xiàn)構(gòu)造自定義數(shù)據(jù)結(jié)構(gòu)的目的了。添加我們自定義的數(shù)據(jù)結(jié)構(gòu)代碼如下:

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; //新加數(shù)據(jù)結(jié)構(gòu)
  
  struct {
    Mem_root_array<PT_create_table_option *> *opt_create_table_options;
    PT_partition *opt_partitioning;
    PT_global_partition *opt_global_partitioning; //同時(shí)注意添加到create_table_tail里面,因?yàn)閏reate table語法會(huì)有該操作
    On_duplicate on_duplicate;
    PT_query_primary *opt_query_expression;
  } create_table_tail;
};
static_assert(sizeof(YYSTYPE) <= 40, "YYSTYPE is too big"); //因?yàn)閟truct里面添加了一個(gè)成員變量,所以該union需要的空間也會(huì)變大,因此注意修改這一行

下面內(nèi)容介紹PT_global_partition數(shù)據(jù)結(jié)構(gòu),為了保持和MySQL習(xí)慣一致,新增加的數(shù)據(jù)結(jié)構(gòu)放在了

sql/parse_tree_nodes.cc sql/parse_tree_nodes.h sql/parse_tree_partitions.cc sql/parse_tree_partitions.h

四個(gè)文件里,理論上可以放在任何地方。可根據(jù)自身需求添加對(duì)應(yīng)數(shù)據(jù)結(jié)構(gòu):

文件:sql/parse_tree_partitions.h sql/parse_tree_partitions.cc

/**
新增數(shù)據(jù)結(jié)構(gòu)
*/
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;
};

//模仿其原生的實(shí)現(xiàn)方式即可
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對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu),將新增的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), //添加構(gòu)造函數(shù),主要是為了增加對(duì)PT_global_partition的賦值操作
  on_duplicate(on_duplicate),
  opt_query_expression(opt_query_expression),
  opt_like_clause(nullptr) {}
  
//在其對(duì)應(yīng)的函數(shù)中增加相關(guān)邏輯,調(diào)用對(duì)應(yīng)的初始化函數(shù)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;
    }
}

 到此這篇關(guān)于如何給MySQL添加自定義語法的方法示例的文章就介紹到這了,更多相關(guān)MySQL添加自定義語法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mysql中事務(wù)ACID的實(shí)現(xiàn)原理詳解

    Mysql中事務(wù)ACID的實(shí)現(xiàn)原理詳解

    這篇文章主要給大家介紹了關(guān)于Mysql中事務(wù)ACID實(shí)現(xiàn)原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mysql具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • mysql回表致索引失效案例講解

    mysql回表致索引失效案例講解

    這篇文章主要介紹了mysql回表致索引失效案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • windows下如何解決mysql secure_file_priv null問題

    windows下如何解決mysql secure_file_priv null問題

    這篇文章主要介紹了windows下如何解決mysql secure_file_priv null問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • MySQL 5.6.14 win32安裝方法(zip版)

    MySQL 5.6.14 win32安裝方法(zip版)

    這篇文章主要介紹了MySQL 5.6.14 win32安裝方法(zip版)的相關(guān)資料,非常不錯(cuò),需要的朋友可以參考下
    2016-08-08
  • Mysql auto_increment 重新計(jì)數(shù)(讓id從1開始)

    Mysql auto_increment 重新計(jì)數(shù)(讓id從1開始)

    當(dāng)清空一個(gè)表的時(shí)候,重新插入數(shù)據(jù),發(fā)現(xiàn)auto_increment屬性的字段計(jì)數(shù)不是從1開始的時(shí)候,可以使用以下命令
    2012-12-12
  • MySQL性能參數(shù)詳解之Max_connect_errors 使用介紹

    MySQL性能參數(shù)詳解之Max_connect_errors 使用介紹

    這篇文章主要介紹了MySQL性能參數(shù)詳解之Max_connect_errors 使用介紹,需要的朋友可以參考下
    2016-05-05
  • Mysql查詢優(yōu)化之IN子查詢優(yōu)化方法詳解

    Mysql查詢優(yōu)化之IN子查詢優(yōu)化方法詳解

    項(xiàng)目中有需要,使用MySQL的in子查詢,查詢符合in子查詢集合中條件的數(shù)據(jù),但是沒想到的是,MySQL的in子查詢會(huì)如此的慢,讓人無法接受,下面這篇文章主要給大家介紹了關(guān)于Mysql查詢優(yōu)化之IN子查詢優(yōu)化的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • MySQL 數(shù)據(jù)庫的臨時(shí)文件究竟儲(chǔ)存在哪里

    MySQL 數(shù)據(jù)庫的臨時(shí)文件究竟儲(chǔ)存在哪里

    MySQL使用環(huán)境變量TMPDIR的值作為保存臨時(shí)文件的目錄的路徑名。
    2009-02-02
  • Windows環(huán)境下MySQL 8.0 的安裝、配置與卸載

    Windows環(huán)境下MySQL 8.0 的安裝、配置與卸載

    這篇文章主要介紹了Windows環(huán)境下MySQL 8.0 的安裝、配置與卸載步驟,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 有效查詢MySQL表中重復(fù)數(shù)據(jù)的方法和技巧分享

    有效查詢MySQL表中重復(fù)數(shù)據(jù)的方法和技巧分享

    在MySQL數(shù)據(jù)庫中,偶爾會(huì)遇到需要查找表中出現(xiàn)的重復(fù)數(shù)據(jù)的情況,這種情況下,我們可以通過編寫一些SQL查詢語句輕松地找到并處理這些重復(fù)行,本文將介紹一些常見的方法和技巧,幫助你有效地查詢MySQL表中的重復(fù)數(shù)據(jù),需要的朋友可以參考下
    2023-10-10

最新評(píng)論