oqllib.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 #include "eyedblib/butils.h"
00027 
00028 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00029 //
00030 // oqml librairy functions
00031 //
00032 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00033 
00034 namespace eyedb {
00035 
00036   oqmlStatus *
00037   oqml_get_class(Database *db, const Oid &oid, Class **cls)
00038   {
00039     Status is;
00040     Bool found;
00041 
00042     if (!oid.isValid())
00043       {
00044         *cls = 0;
00045         return new oqmlStatus("NULL oid");
00046         //return oqmlSuccess;
00047       }
00048 
00049     is = db->getObjectClass(oid, *cls);
00050               
00051     if (is)
00052       return new oqmlStatus(is);
00053               
00054     return oqmlSuccess;
00055   }
00056 
00057   oqmlStatus *
00058   oqml_scan(oqmlContext *ctx, Iterator *q, Class *cls, oqmlAtomList *alist,
00059             const oqmlATOMTYPE t)
00060   {
00061     Status is;
00062     if ((is = q->getStatus()) == Success)
00063       {
00064         Oid oid;
00065         Bool found;
00066       
00067         for (;;)
00068           {
00069             // should use: Value v; q->next(v)
00070             IteratorAtom qatom;
00071             OQML_CHECK_INTR();
00072             is = q->scanNext(&found, &qatom);
00073           
00074             if (is)
00075               {
00076                 delete q;
00077                 return new oqmlStatus(is);
00078               }
00079 
00080             if (!found)
00081               break;
00082           
00083             if (qatom.type == IteratorAtom_IDR)
00084               alist->append(oqmlAtom::make_atom(qatom.data.idr, t, cls));
00085             else
00086               alist->append(oqmlAtom::make_atom(qatom, cls));
00087 
00088             OQML_CHECK_MAX_ATOMS(alist, ctx, delete q);
00089           }
00090 
00091         delete q;
00092         return oqmlSuccess;
00093       }
00094 
00095     delete q;
00096     return new oqmlStatus(is);
00097   }
00098 
00099   static const char *operationName[__oqml__last__];
00100   static oqmlBool _init = oqml_False;
00101 
00102   void
00103   oqmlNode::init()
00104   {
00105     operationName[oqmlTRUE] = "true atom";
00106     operationName[oqmlFALSE] = "false atom";
00107     operationName[oqmlCHAR] = "character atom";
00108     operationName[oqmlINT] = "integer atom";
00109     operationName[oqmlFLOAT] = "float atom";
00110     operationName[oqmlIDENT] = "identifier atom";
00111     operationName[oqmlNULL] = "null atom";
00112     operationName[oqmlOID] = "oid atom";
00113     operationName[oqmlOBJECT] = "object atom";
00114     operationName[oqmlSTRING] = "string atom";
00115     operationName[oqmlNIL] = "nil atom"; // ??
00116 
00117     /* standard operators */
00118     operationName[oqmlAAND] = "operator &";
00119     operationName[oqmlADD] = "operator +";
00120     operationName[oqmlAOR] = "operator |";
00121     operationName[oqmlARRAY] = "operator []";
00122     operationName[oqmlASSIGN] = "operator :=";
00123     operationName[oqmlCOMMA] = "operator ,";
00124     operationName[oqmlDIFF] = "operator !=";
00125     operationName[oqmlDIV] = "operator /";
00126     operationName[oqmlINF] = "operator <";
00127     operationName[oqmlINFEQ] = "operator <=";
00128     operationName[oqmlLAND] = "operator &&";
00129     operationName[oqmlLNOT] = "operator !";
00130     operationName[oqmlLOR] = "operator ||";
00131     operationName[oqmlMOD] = "operator %";
00132     operationName[oqmlMUL] = "operator *";
00133     operationName[oqmlREGCMP] = "operator ~";
00134     operationName[oqmlREGDIFF] = "operator !~";
00135     operationName[oqmlREGICMP] = "operator ~~";
00136     operationName[oqmlREGIDIFF] = "operator !~~";
00137     operationName[oqmlSHL] = "operator <<";
00138     operationName[oqmlSHR] = "operator >>";
00139     operationName[oqmlSUB] = "operator -";
00140     operationName[oqmlSUP] = "operator >";
00141     operationName[oqmlSUPEQ] = "operator >=";
00142     operationName[oqmlTILDE] = "operator ~";
00143     operationName[oqmlEQUAL] = "operator ==";
00144     operationName[oqmlDOT] = "path expression";
00145     operationName[oqmlXOR] = "operator ^";
00146 
00147     /* set operators */
00148     operationName[oqmlUNION]     = "union expression";
00149     operationName[oqmlINTERSECT] = "intersect expression";
00150     operationName[oqmlEXCEPT]    = "except expression";
00151 
00152     /* extended operators */
00153     operationName[oqmlISSET] = "isset operator";
00154     operationName[oqmlSET] = "set operator";
00155     operationName[oqmlUNSET] = "unset operator";
00156     operationName[oqmlTYPEOF] = "typeof operator";
00157     operationName[oqmlCLASSOF] = "classof operator";
00158     operationName[oqmlEVAL] = "eval operator";
00159     operationName[oqmlTHROW] = "throw operator";
00160     operationName[oqmlRETURN] = "return operator";
00161     operationName[oqmlUNVAL] = "unval operator";
00162     operationName[oqmlREFOF] = "refof operator";
00163     operationName[oqmlVALOF] = "valof operator";
00164     operationName[oqmlIMPORT] = "import operator";
00165     operationName[oqmlPRINT] = "print operator";
00166 
00167     /* cast operators */
00168     operationName[oqmlSTRINGOP] = "string operator";
00169     operationName[oqmlINTOP] = "int operator";
00170     operationName[oqmlCHAROP] = "char operator";
00171     operationName[oqmlFLOATOP] = "float operator";
00172     operationName[oqmlOIDOP] = "oid operator";
00173     operationName[oqmlIDENTOP] = "ident operator";
00174 
00175     /* list management */
00176     operationName[oqmlLISTCOLL] = "list function";
00177     operationName[oqmlSETCOLL] = "set function";
00178     operationName[oqmlBAGCOLL] = "bag function";
00179     operationName[oqmlARRAYCOLL] = "array function";
00180     operationName[oqmlCOUNT] = "count function"; // not yet implemented
00181     operationName[oqmlFLATTEN] = "flatten function"; // not yet implemented
00182 
00183     /* functions and methods */
00184     operationName[oqmlFUNCTION] = "function definition";
00185     operationName[oqmlCALL] = "function call";
00186     operationName[oqmlMTHCALL] = "method call";
00187 
00188     /* flow controls */
00189     operationName[oqmlIF] = "if/then operator";
00190     operationName[oqmlFOREACH] = "for operator";
00191     operationName[oqmlFORDO] = "for operator";
00192     operationName[oqmlWHILE] = "while operator";
00193     operationName[oqmlDOWHILE] = "do/while operator";
00194     operationName[oqmlBREAK] = "break operator";
00195 
00196     /* collection management */
00197     operationName[oqmlCOLLECTION] = "collection function";
00198     operationName[oqmlCONTENTS] = "contents operator";
00199     operationName[oqmlADDTO] = "add/to operator";
00200     operationName[oqmlAPPEND] = "append operator";
00201     operationName[oqmlSUPPRESSFROM] = "suppress operator";
00202     operationName[oqmlUNSETINAT] = "unset/in/at operator";
00203     operationName[oqmlSETINAT] = "set/in/at operator";
00204     operationName[oqmlELEMENT] = "element operator";
00205     operationName[oqmlELEMENTAT] = "element operator";
00206     operationName[oqmlEMPTY] = "empty operator";
00207     operationName[oqmlIN] = "in operator";
00208     operationName[oqmlEXISTS] = "exists operator";
00209     operationName[oqmlFOR] = "for operator";
00210 
00211     operationName[oqmlBODYOF] = "bodyof operator";
00212     operationName[oqmlSCOPEOF] = "scopeof operator";
00213     operationName[oqmlSTRUCTOF] = "structof operator";
00214     operationName[oqmlPUSH] = "push operator";
00215     operationName[oqmlPOP] = "pop operator";
00216     operationName[oqmlSTRUCT] = "struct constructor";
00217 
00218     /* query operators */
00219     operationName[oqmlSELECT] = "select operator";
00220     operationName[oqmlAND] = "and operator";
00221     operationName[oqmlOR] = "or operator";
00222 
00223     /* range operators */
00224     operationName[oqmlRANGE] = "range operator";
00225     operationName[oqmlBETWEEN] = "between operator";
00226     operationName[oqmlNOTBETWEEN] = "between operator";
00227 
00228     /* ordonnancing operators */
00229     operationName[oqmlSORT] = "sort function";
00230     operationName[oqmlISORT] = "isort function";
00231     operationName[oqmlPSORT] = "psort function";
00232 
00233     /* constructor/destructor */
00234     operationName[oqmlNEW] = "new operator";
00235     operationName[oqmlDELETE] = "delete operator";
00236 
00237     /* time functions */
00238     operationName[oqmlTIMEFORMAT] = "timeformat function";
00239 
00240     operationName[oqmlCOMPOUND_STATEMENT] = "";
00241     operationName[oqmlCAST] = "";
00242     operationName[oqmlCASTIDENT] = "";
00243 
00244     /* misc */
00245     operationName[oqmlDATABASE] = "database operator";
00246     operationName[oqmlCOMPOUND_STATEMENT] = "";
00247 
00248     /* unused */
00249     operationName[oqmlCAST] = "";
00250     operationName[oqmlCASTIDENT] = "";
00251 
00252     _init = oqml_True;
00253   }
00254 
00255   std::string
00256   oqmlNode::getOperationName() const
00257   {
00258     if (!_init) init();
00259     const char *s = operationName[type];
00260     return s ? s : "";
00261   }
00262 
00263   // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00264   //
00265   // oqmlStatus methods
00266   //
00267   // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00268 
00269   void
00270   oqmlStatus::set()
00271   {
00272     oqml_status->set(msg);
00273     returnAtom = 0;
00274   }
00275 
00276   /*
00277   void
00278   oqmlStatus::purge()
00279   {
00280     oqml_status->set("");
00281   }
00282   */
00283 
00284   oqmlStatus::oqmlStatus(const char *fmt, ...)
00285   {
00286     va_list ap, aq;
00287     va_start(ap, fmt);
00288     va_copy(aq, ap);
00289 
00290     returnAtom = 0;
00291     char *errmsg = eyedblib::getFBuffer(fmt, ap);
00292     vsprintf(errmsg, fmt, aq);
00293     va_end(ap);
00294 
00295     msg = strdup(errmsg);
00296     set();
00297   }
00298 
00299   oqmlStatus::oqmlStatus(Status s)
00300   {
00301     msg = strdup(s->getDesc());
00302     set();
00303   }
00304 
00305   static inline std::string
00306   nodePrefix(oqmlNode *node)
00307   {
00308     std::string s = node->getOperationName() + " '" + node->toString() + "' : ";
00309     return s;
00310   }
00311 
00312   oqmlStatus::oqmlStatus(oqmlNode *node, const char *fmt, ...)
00313   {
00314     va_list ap, aq;
00315     va_start(ap, fmt);
00316     va_copy(aq, ap);
00317 
00318     char *errmsg = eyedblib::getFBuffer(fmt, ap);
00319     vsprintf(errmsg, fmt, aq);
00320     va_end(ap);
00321 
00322     if (errmsg[strlen(errmsg)-1] != '.')
00323       strcat(errmsg, ".");
00324 
00325     std::string prefix = node ? nodePrefix(node) : std::string("");
00326     msg = strdup((prefix + errmsg).c_str());
00327     set();
00328   }
00329 
00330   oqmlStatus::oqmlStatus(oqmlNode *node, Status s)
00331   {
00332     std::string prefix = node ? nodePrefix(node) : std::string("");
00333     std::string str = strdup((prefix + s->getDesc()).c_str());
00334     if (str[strlen(str.c_str())-1] != '.')
00335       str += std::string(".");
00336     msg = strdup(str.c_str());
00337     set();
00338   }
00339 
00340   oqmlStatus *
00341   oqmlStatus::expected(oqmlNode *node, const char *expected, const char *got)
00342   {
00343     return new oqmlStatus(node,
00344                           (std::string(expected) + " expected, got " + got).c_str());
00345   }
00346 
00347   oqmlStatus *
00348   oqmlStatus::expected(oqmlNode *node, oqmlAtomType *expected, oqmlAtomType *got)
00349   {
00350     return oqmlStatus::expected(node, expected->getString(), got->getString());
00351   }
00352 
00353   oqmlStatus::~oqmlStatus()
00354   {
00355     //oqmlLock(returnAtom, oqml_False, oqml_True);
00356     free(msg);
00357   }
00358 }

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