oqlmisc.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB 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    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include "oql_p.h"
00026 
00027 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00028 //
00029 // oqmlCastIdent methods
00030 //
00031 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00032 
00033 namespace eyedb {
00034 
00035 oqmlCastIdent::oqmlCastIdent(const char * _name, const char *_modname) :
00036 oqmlNode(oqmlCASTIDENT)
00037 {
00038   name = strdup(_name);
00039   modname = strdup(_modname);
00040   printf("castident %s %s\n", name, modname);
00041 }
00042 
00043 oqmlCastIdent::~oqmlCastIdent()
00044 {
00045   free(name);
00046   free(modname);
00047 }
00048 
00049 oqmlStatus *oqmlCastIdent::compile(Database *db, oqmlContext *ctx)
00050 {
00051   return new oqmlStatus("cannot eval cast ident '%s:%s'.", name, modname);
00052 }
00053 
00054 oqmlStatus *oqmlCastIdent::eval(Database *db, oqmlContext *ctx, oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00055 {
00056   return new oqmlStatus("cannot eval cast ident '%s:%s'.", name, modname);
00057 }
00058 
00059 void oqmlCastIdent::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00060 {
00061   *at = eval_type;
00062 }
00063 
00064 oqmlBool oqmlCastIdent::isConstant() const
00065 {
00066   return oqml_False;
00067 }
00068 
00069 const char *oqmlCastIdent::getName(const char **_modname) const
00070 {
00071   if (_modname)
00072     *_modname = modname;
00073   return name;
00074 }
00075 
00076 std::string
00077 oqmlCastIdent::toString(void) const
00078 {
00079   return std::string(name) + "@" + modname;
00080 }
00081 
00082 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00083 //
00084 // oqmlClassOf methods
00085 //
00086 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00087 
00088 oqmlClassOf::oqmlClassOf(oqmlNode *_ql): oqmlNode(oqmlCLASSOF)
00089 {
00090   ql = _ql;
00091   eval_type.type = oqmlATOM_STRING;
00092   eval_type.comp = oqml_True;
00093 }
00094 
00095 oqmlClassOf::~oqmlClassOf()
00096 {
00097 }
00098 
00099 oqmlStatus *oqmlClassOf::compile(Database *db, oqmlContext *ctx)
00100 {
00101   oqmlStatus *s;
00102 
00103   s = ql->compile(db, ctx);
00104 
00105   if (s)
00106     return s;
00107 
00108   return oqmlSuccess;
00109 }
00110 
00111 oqmlStatus *oqmlClassOf::eval(Database *db, oqmlContext *ctx, oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00112 {
00113   oqmlStatus *s;
00114   oqmlAtomList *al;
00115 
00116   *alist = new oqmlAtomList;
00117 
00118   s = ql->eval(db, ctx, &al);
00119 
00120   if (s)
00121     return s;
00122 
00123   oqmlAtom *a = al->first;
00124   while (a)
00125     {
00126       oqmlAtom *next = a->next;
00127       const Class *cls = 0;
00128 
00129       if (OQML_IS_OID(a))
00130         {       
00131           Bool found;
00132           const Oid &oid = OQML_ATOM_OIDVAL(a);
00133           if (!oid.isValid())
00134             cls = 0;
00135           else
00136             {
00137               Status status = db->getObjectClass(oid, (Class *&)cls);
00138               if (status)
00139                 return new oqmlStatus(this, status);
00140             }
00141         }
00142       else if (OQML_IS_OBJ(a)) {
00143         OQL_CHECK_OBJ(a);
00144         cls = (OQML_ATOM_OBJVAL(a) ? OQML_ATOM_OBJVAL(a)->getClass() : 0);
00145       }
00146       else if (a->as_null())
00147         cls = 0;
00148       else
00149         return oqmlStatus::expected(this, "oid or object",
00150                                     a->type.getString());
00151       if (!cls)
00152         //      (*alist)->append(new oqmlAtom_oid(Oid()));
00153         (*alist)->append(new oqmlAtom_string(""));
00154       else
00155         (*alist)->append(new oqmlAtom_string((char *)cls->getName()));
00156 
00157       a = next;
00158     }
00159 
00160   return oqmlSuccess;
00161 }
00162 
00163 void oqmlClassOf::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00164 {
00165   *at = eval_type;
00166 }
00167 
00168 oqmlBool oqmlClassOf::isConstant() const
00169 {
00170   return oqml_False;
00171 }
00172 
00173 std::string
00174 oqmlClassOf::toString(void) const
00175 {
00176   return std::string("classof(") + ql->toString() + ")";
00177 }
00178 
00179 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00180 //
00181 // oqmlCast methods
00182 //
00183 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00184 
00185 oqmlCast::oqmlCast(const char * _ident, oqmlNode * _qright) : oqmlNode(oqmlCAST)
00186 {
00187   ident = strdup(_ident);
00188   qright = _qright;
00189 }
00190 
00191 oqmlCast::~oqmlCast()
00192 {
00193   delete ident;
00194 }
00195 
00196 oqmlStatus *oqmlCast::compile(Database *db, oqmlContext *ctx)
00197 {
00198   return oqmlSuccess;
00199 }
00200 
00201 oqmlStatus *oqmlCast::eval(Database *db, oqmlContext *ctx, oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00202 {
00203   return oqmlSuccess;
00204 }
00205 
00206 void oqmlCast::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00207 {
00208   *at = eval_type;
00209 }
00210 
00211 oqmlBool oqmlCast::isConstant() const
00212 {
00213   return oqml_False;
00214 }
00215 
00216 std::string
00217 oqmlCast::toString(void) const
00218 {
00219   return "";
00220 }
00221 
00222 
00223 #if 0
00224 // -----------------------------------------------------------------------
00225 //
00226 // static utility functions
00227 //
00228 // -----------------------------------------------------------------------
00229 
00230 static oqmlStatus *
00231 oqml_CMtime_compile(Database *db, oqmlContext *ctx, oqmlNode *ql)
00232 {
00233   oqmlStatus *s;
00234   oqmlAtomType at;
00235 
00236   s = ql->compile(db, ctx);
00237   if (s)
00238     return s;
00239 
00240   ql->evalType(db, ctx, &at);
00241 
00242   if (at.type != oqmlATOM_UNKNOWN_TYPE && at.type != oqmlATOM_OID)
00243     return new oqmlStatus("invalid argument for ctime(oid)");
00244 
00245   return oqmlSuccess;
00246 }
00247 
00248 static oqmlStatus *
00249 oqml_CMtime_eval(Database *db, oqmlContext *ctx, oqmlNode *ql,
00250                 time_t *ctime, time_t *mtime)
00251 {
00252   oqmlAtomList *al;
00253   oqmlStatus *s;
00254 
00255   s = ql->eval(db, ctx, &al);
00256   if (s)
00257     return s;
00258 
00259   if (al->cnt != 1 || al->first->type.type != oqmlATOM_OID)
00260     return new oqmlStatus("invalid argument for ctime(oid)");
00261 
00262   Oid *oid = &((oqmlAtom_oid *)al->first)->oid;
00263 
00264   ObjectHeader hdr;
00265 
00266   RPCStatus rpc_status;
00267 
00268   if ((rpc_status = objectHeaderRead(db->getDbHandle(), oid->getOid(),
00269                                          &hdr)) ==
00270       RPCSuccess)
00271     {
00272       if (ctime)
00273         *ctime = hdr.ctime;
00274       if (mtime)
00275         *mtime = hdr.mtime;
00276     }
00277 
00278   return oqmlSuccess;
00279 }
00280 
00281 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00282 //
00283 // oqmlCTime methods
00284 //
00285 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00286 
00287 oqmlCTime::oqmlCTime(oqmlNode * _ql) : oqmlNode(oqmlCTIME)
00288 {
00289   ql = _ql;
00290 }
00291 
00292 oqmlCTime::~oqmlCTime()
00293 {
00294 }
00295 
00296 oqmlStatus *oqmlCTime::compile(Database *db, oqmlContext *ctx)
00297 {
00298   return oqml_CMtime_compile(db, ctx, ql);
00299 }
00300 
00301 oqmlStatus *oqmlCTime::eval(Database *db, oqmlContext *ctx, oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00302 {
00303   Oid oid;
00304   time_t ctime;
00305   oqmlStatus *s = oqml_CMtime_eval(db, ctx, ql, &ctime, 0);
00306 
00307   if (s)
00308     return s;
00309 
00310   *alist = new oqmlAtomList(new oqmlAtom_int(ctime));
00311   return oqmlSuccess;
00312 }
00313 
00314 void oqmlCTime::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00315 {
00316   at->type = oqmlATOM_INT;
00317   at->cls = 0;
00318   at->comp = oqml_False;
00319 }
00320 
00321 oqmlBool oqmlCTime::isConstant() const
00322 {
00323   return OQMLBOOL(ql->isConstant());
00324 }
00325 
00326 std::string
00327 oqmlCTime::toString(void) const
00328 {
00329   return std::string("ctime(") + ql->toString() + ")";
00330 }
00331 
00332 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00333 //
00334 // oqmlMTime methods
00335 //
00336 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00337 
00338 oqmlMTime::oqmlMTime(oqmlNode * _ql) : oqmlNode(oqmlMTIME)
00339 {
00340   ql = _ql;
00341 }
00342 
00343 oqmlMTime::~oqmlMTime()
00344 {
00345 }
00346 
00347 oqmlStatus *oqmlMTime::compile(Database *db, oqmlContext *ctx)
00348 {
00349   return oqml_CMtime_compile(db, ctx, ql);
00350 }
00351 
00352 oqmlStatus *oqmlMTime::eval(Database *db, oqmlContext *ctx, oqmlAtomList **alist, oqmlComp *, oqmlAtom *)
00353 {
00354   Oid oid;
00355   time_t mtime;
00356   oqmlStatus *s = oqml_CMtime_eval(db, ctx, ql, 0, &mtime);
00357 
00358   if (s)
00359     return s;
00360 
00361   *alist = new oqmlAtomList(new oqmlAtom_int(mtime));
00362   return oqmlSuccess;
00363 }
00364 
00365 void oqmlMTime::evalType(Database *db, oqmlContext *ctx, oqmlAtomType *at)
00366 {
00367   at->type = oqmlATOM_INT;
00368   at->cls = 0;
00369   at->comp = oqml_False;
00370 }
00371 
00372 oqmlBool oqmlMTime::isConstant() const
00373 {
00374   return OQMLBOOL(ql->isConstant());
00375 }
00376 
00377 std::string
00378 oqmlMTime::toString(void) const
00379 {
00380   return std::string("mtime(") + ql->toString() + ")";
00381 }
00382 #endif
00383 }

Generated on Mon Dec 22 18:16:06 2008 for eyedb by  doxygen 1.5.3