1 /* Copyright (C) 2014 InfiniDB, Inc. 2 Copyright (C) 2016 MariaDB Corporation 3 4 This program is free software; you can redistribute it and/or 5 modify it under the terms of the GNU General Public License 6 as published by the Free Software Foundation; version 2 of 7 the License. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software 16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 17 MA 02110-1301, USA. */ 18 19 /*********************************************************************** 20 * $Id: sqlparser.h 9210 2013-01-21 14:10:42Z rdempsey $ 21 * 22 * 23 ***********************************************************************/ 24 /** @file 25 * 26 * This contains a class wrapper for the Bison parsing machinery. 27 */ 28 29 #include <stdexcept> 30 #include <my_global.h> 31 #include <my_sys.h> 32 #include "ddlpkg.h" 33 34 #if defined(_MSC_VER) && defined(xxxDDLPKGSQLPARSER_DLLEXPORT) 35 #define EXPORT __declspec(dllexport) 36 #else 37 #define EXPORT 38 #endif 39 40 namespace ddlpackage 41 { 42 43 typedef SqlStatementList ParseTree; 44 45 /** @brief SqlParser is a class interface around the Bison parser 46 * machinery for DDL. 47 * 48 * Example: 49 * 50 * @verbatim 51 52 SqlParser parser; 53 parser.Parse(sqlbuf); 54 55 or 56 57 SqlFileParser parser; 58 parser.setDefaultSchema("tpch"); 59 parser.Parse(sqlFileName); 60 61 if (parser.Good()) { 62 const ParseTree &ptree = parser.GetParseTree(); 63 cout << ptree.fList.size() << " " << "SQL statements" << endl; 64 cout << ptree << endl; 65 } 66 else { 67 cout << "Parser failed." << endl; 68 } 69 70 @endverbatim 71 */ 72 73 /* 74 Instance specific data for use by the scanner. 75 */ 76 typedef std::vector<char*> valbuf_t; 77 78 struct scan_data 79 { 80 /* Handles to the buffer that the lexer uses internally */ 81 char* scanbuf; 82 void* scanbufhandle; // This is a YY_BUFFER_STATE defined in ddl-scan.cpp 83 valbuf_t valbuf; 84 }; 85 86 struct pass_to_bison 87 { 88 ParseTree* fParseTree; 89 std::string fDBSchema; 90 void* scanner; 91 const CHARSET_INFO* default_table_charset; 92 pass_to_bisonpass_to_bison93 pass_to_bison(ParseTree* pt) : fParseTree(pt), scanner(NULL), default_table_charset(NULL) {}; 94 }; 95 96 class SqlParser 97 { 98 public: 99 EXPORT SqlParser(void); 100 101 EXPORT virtual ~SqlParser(); 102 103 EXPORT int Parse(const char* sqltext); 104 105 /** @brief Return the ParseTree if state is Good. Otherwise 106 * throw a logic_error. 107 */ 108 EXPORT const ParseTree& GetParseTree(void); 109 110 /** @brief Tells whether current state resulted from a good 111 * parse. 112 */ 113 EXPORT bool Good(void); 114 115 /** @brief Control bison debugging 116 */ 117 EXPORT void SetDebug(bool debug); 118 119 /** @brief Set the default schema to use if it is not 120 * supplied in the DDL statement 121 * 122 * @param schema the default schema 123 */ 124 EXPORT void setDefaultSchema(std::string schema); 125 126 /** @brief Set the default table charset. Can be overriden by column 127 * or table options 128 * 129 * @param default_charset the default CHARSET_INFO pointer 130 */ 131 EXPORT void setDefaultCharset(const CHARSET_INFO* default_charset); 132 133 protected: 134 ParseTree fParseTree; 135 std::string fDBSchema; 136 int fStatus; ///< return from yyparse() stored here. 137 bool fDebug; ///< Turn on bison debugging. 138 scan_data scanData; 139 pass_to_bison x; 140 }; 141 142 143 /** SqlFileParser is a testing device. 144 */ 145 class SqlFileParser : public SqlParser 146 { 147 public: 148 SqlFileParser(); 149 int Parse(const std::string& fileName); 150 }; 151 152 } 153 154 #undef EXPORT 155