kexi

expression.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    Based on nexp.h : Parser module of Python-like language
00005    (C) 2001 Jaroslaw Staniek, MIMUW (www.mimuw.edu.pl)
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #ifndef KEXIDB_EXPRESSION_H
00024 #define KEXIDB_EXPRESSION_H
00025 
00026 #include <kexidb/field.h>
00027 
00028 #include <kdebug.h>
00029 #include "global.h"
00030 
00031 namespace KexiDB {
00032 
00034 #define KexiDBExpr_Unknown 0
00035 #define KexiDBExpr_Unary 1
00036 #define KexiDBExpr_Arithm 2
00037 #define KexiDBExpr_Logical 3
00038 #define KexiDBExpr_Relational 4
00039 #define KexiDBExpr_SpecialBinary 5
00040 #define KexiDBExpr_Const 6
00041 #define KexiDBExpr_Variable 7
00042 #define KexiDBExpr_Function 8
00043 #define KexiDBExpr_Aggregation 9
00044 #define KexiDBExpr_TableList 10
00045 
00046 KEXI_DB_EXPORT QString exprClassName(int c);
00047 
00048 class ParseInfo;
00049 class NArgExpr;
00050 class UnaryExpr;
00051 class BinaryExpr;
00052 class ConstExpr;
00053 class VariableExpr;
00054 class FunctionExpr;
00055 
00057 class KEXI_DB_EXPORT BaseExpr
00058 {
00059 public:
00060     typedef QPtrList<BaseExpr> List;
00061     typedef QPtrListIterator<BaseExpr> ListIterator;
00062 
00063     BaseExpr(int token);
00064     virtual ~BaseExpr();
00065     int token() const { return m_token; }
00066     virtual Field::Type type();
00067     BaseExpr* parent() const { return m_par; }
00068     virtual void setParent(BaseExpr *p) { m_par = p; }
00069     virtual bool validate(ParseInfo& parseInfo);
00070     virtual QString toString() = 0;
00071     
00072     inline void debug() { KexiDBDbg << debugString() << endl; }
00073     virtual QString debugString();
00076     inline QString tokenToDebugString() { return tokenToDebugString(m_token); }
00077     static QString tokenToDebugString(int token);
00078 
00080     virtual QString tokenToString();
00081 
00082     int exprClass() const { return m_cl; }
00083 
00085     NArgExpr* toNArg();
00086     UnaryExpr* toUnary();
00087     BinaryExpr* toBinary();
00088     ConstExpr* toConst();
00089     VariableExpr* toVariable();
00090     FunctionExpr* toFunction();
00091 
00092 protected:
00093     int m_cl; 
00094     BaseExpr *m_par; 
00095     int m_token;
00096 };
00097 
00099 class KEXI_DB_EXPORT NArgExpr : public BaseExpr
00100 {
00101 public:
00102     NArgExpr(int aClass, int token);
00103     virtual ~NArgExpr();
00104     void add(BaseExpr *expr);
00105     void prepend(BaseExpr *expr);
00106     BaseExpr *arg(int n);
00107     int args();
00108     virtual QString debugString();
00109     virtual QString toString();
00110     virtual bool validate(ParseInfo& parseInfo);
00111     BaseExpr::List list;
00112 };
00113 
00115 class KEXI_DB_EXPORT UnaryExpr : public BaseExpr
00116 {
00117 public:
00118     UnaryExpr(int token, BaseExpr *arg);
00119     virtual ~UnaryExpr();
00120     virtual Field::Type type();
00121     virtual QString debugString();
00122     virtual QString toString();
00123     BaseExpr *arg() const { return m_arg; }
00124     virtual bool validate(ParseInfo& parseInfo);
00125 
00126     BaseExpr *m_arg;
00127 };
00128 
00137 class KEXI_DB_EXPORT BinaryExpr : public BaseExpr
00138 {
00139 public:
00140     BinaryExpr(int aClass, BaseExpr *left_expr, int token, BaseExpr *right_expr);
00141     virtual ~BinaryExpr();
00142     virtual Field::Type type();
00143     virtual QString debugString();
00144     virtual QString toString();
00145     BaseExpr *left() const { return m_larg; }
00146     BaseExpr *right() const { return m_rarg; }
00147     virtual bool validate(ParseInfo& parseInfo);
00148 
00149     virtual QString tokenToString();
00150 
00151     BaseExpr *m_larg;
00152     BaseExpr *m_rarg;
00153 };
00154 
00159 
00160 class KEXI_DB_EXPORT ConstExpr : public BaseExpr
00161 {
00162 public:
00163     ConstExpr(int token, const QVariant& val);
00164     virtual ~ConstExpr();
00165     virtual Field::Type type();
00166     virtual QString debugString();
00167     virtual QString toString();
00168     virtual bool validate(ParseInfo& parseInfo);
00169     QVariant value;
00170 };
00171 
00173 class KEXI_DB_EXPORT VariableExpr : public BaseExpr
00174 {
00175 public:
00176     VariableExpr(const QString& _name);
00177     virtual ~VariableExpr();
00178     virtual Field::Type type();
00179     virtual QString debugString();
00180     virtual QString toString();
00184     virtual bool validate(ParseInfo& parseInfo);
00185 
00187     QString name;
00188 
00189     /* NULL by default. After succesful validate() it will point to a field,
00190      if the variable is of a form "tablename.fieldname" or "fieldname", 
00191      otherwise (eg. for asterisks) -still NULL.
00192      Only meaningfull for column expressions within a query. */
00193     Field *field;
00194 
00195     /* -1 by default. After succesful validate() it will contain a position of a table
00196      within query that needs to be bound to the field. 
00197      This value can be either be -1 if no binding is needed.
00198      This value is used in the Parser to call 
00199       QuerySchema::addField(Field* field, int bindToTable);
00200      Only meaningfull for column expressions within a query. */
00201     int tablePositionForField;
00202 
00206     TableSchema *tableForQueryAsterisk;
00207 };
00208 
00212 class KEXI_DB_EXPORT FunctionExpr : public BaseExpr
00213 {
00214 public:
00215     FunctionExpr(const QString& _name, NArgExpr* args_);
00216     virtual ~FunctionExpr();
00217     virtual Field::Type type();
00218     virtual QString debugString();
00219     virtual QString toString();
00220     virtual bool validate(ParseInfo& parseInfo);
00221 
00222     static QValueList<QCString> builtInAggregates();
00223     static bool isBuiltInAggregate(const QCString& fname);
00224 
00225     QString name;
00226     NArgExpr* args;
00227 };
00228 
00229 /*
00231 class NConstInt : public BaseExpr
00232 {
00233 public:
00234     NConstInt(int val);
00235     virtual const QString dump();
00236     int value();
00237     virtual void check();
00238 protected:
00239     int val;
00240 };
00241 
00243 class NConstBool : public BaseExpr
00244 {
00245 public:
00246     NConstBool(const char v);
00247     virtual const QString dump();
00248     const char value();
00249     virtual void check();
00250     virtual void genCode();
00251 protected:
00252     char val;
00253 };
00254 
00256 class NConstStr : public BaseExpr
00257 {
00258 public:
00259     NConstStr(const char *v);
00260     virtual const QString dump();
00261     const QString value();
00262     virtual void check();
00263     virtual void genCode();
00264 protected:
00265     QString val;
00266 };
00267 */
00268 
00269 //-----------------------------------------
00270 #if 0
00271 
00274 class KEXI_DB_EXPORT Expression
00275 {
00276     public:
00277         Expression();
00278         virtual ~Expression();
00279 
00283         int type();
00284 
00286         KexiDB::Field* field();
00287     protected:
00288         ExpressionPrivate *d; //unused
00289 
00290         Field *m_field; //owner
00291 
00292         friend class Field;
00293 };
00294 #endif
00295 
00296 } //namespace KexiDB
00297 
00298 #endif
00299 
KDE Home | KDE Accessibility Home | Description of Access Keys