kexi
parser.cpp
00001 /* This file is part of the KDE project 00002 Copyright (C) 2003 Lucijan Busch <lucijan@kde.org> 00003 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <connection.h> 00022 #include <tableschema.h> 00023 #include "parser.h" 00024 #include "parser_p.h" 00025 #include "sqlparser.h" 00026 00027 extern const char * reserved_keywords[]; 00028 00029 using namespace KexiDB; 00030 00031 Parser::Parser(Connection *db) 00032 : d(new ParserPrivate) 00033 { 00034 d->db = db; 00035 } 00036 00037 Parser::~Parser() 00038 { 00039 delete d; 00040 } 00041 00042 Parser::OPCode Parser::operation() const { return (OPCode)d->operation; } 00043 00044 QString 00045 Parser::operationString() const 00046 { 00047 switch((OPCode)d->operation) { 00048 case OP_Error: 00049 return "Error"; 00050 case OP_CreateTable: 00051 return "CreateTable"; 00052 case OP_AlterTable: 00053 return "AlterTable"; 00054 case OP_Select: 00055 return "Select"; 00056 case OP_Insert: 00057 return "Insert"; 00058 case OP_Update: 00059 return "Update"; 00060 case OP_Delete: 00061 return "Delete"; 00062 default: //OP_None 00063 return "None"; 00064 } 00065 } 00066 00067 TableSchema *Parser::table() { TableSchema *t = d->table; d->table=0; return t; } 00068 00069 QuerySchema *Parser::query() { QuerySchema *s = d->select; d->select=0; return s; } 00070 00071 Connection *Parser::db() const { return d->db; } 00072 00073 ParserError Parser::error() const { return d->error; } 00074 00075 QString Parser::statement() const { return d->statement; } 00076 00077 void Parser::setOperation(OPCode op) { d->operation = op; } 00078 00079 QuerySchema *Parser::select() const { return d->select; } 00080 00081 void Parser::setError(const ParserError &err) { d->error = err; } 00082 00083 void 00084 Parser::createTable(const char *t) 00085 { 00086 if (d->table) 00087 return; 00088 00089 d->table = new KexiDB::TableSchema(t); 00090 } 00091 00092 void 00093 Parser::setQuerySchema(QuerySchema *query) 00094 { 00095 if (d->select) 00096 delete d->select; 00097 00098 d->select = query; 00099 } 00100 00101 void Parser::init() 00102 { 00103 if (d->initialized) 00104 return; 00105 #define INS(p) d->reservedKeywords.insert(p, (char*)1, 0) 00106 #include "tokens.cpp" 00107 d->initialized = true; 00108 } 00109 00110 bool Parser::isReservedKeyword(const char *str) 00111 { 00112 return d->reservedKeywords.find(str); 00113 } 00114 00115 bool 00116 Parser::parse(const QString &statement) 00117 { 00118 init(); 00119 clear(); 00120 d->statement = statement; 00121 00122 KexiDB::Parser *oldParser = parser; 00123 KexiDB::Field *oldField = field; 00124 bool res = parseData(this, statement.utf8()); 00125 parser = oldParser; 00126 field = oldField; 00127 return res; 00128 } 00129 00130 void 00131 Parser::clear() 00132 { 00133 d->clear(); 00134 } 00135 00136 //------------------------------------- 00137 00138 ParserError::ParserError() 00139 : m_at(-1) 00140 { 00141 // m_isNull = true; 00142 } 00143 00144 ParserError::ParserError(const QString &type, const QString &error, const QString &hint, int at) 00145 { 00146 m_type = type; 00147 m_error = error; 00148 m_hint = hint; 00149 m_at = at; 00150 } 00151 00152 ParserError::~ParserError() 00153 { 00154 } 00155