Main Page | Class Hierarchy | Class List | File List | Class Members

subsql.h

00001 //-< SUBSQL.H >------------------------------------------------------*--------*
00002 // GigaBASE                  Version 1.0         (c) 1999  GARRET    *     ?  *
00003 // (Post Relational Database Management System)                      *   /\|  *
00004 //                                                                   *  /  \  *
00005 //                          Created:     20-Nov-98    K.A. Knizhnik  * / [] \ *
00006 //                          Last update: 10-Dec-98    K.A. Knizhnik  * GARRET *
00007 //-------------------------------------------------------------------*--------*
00008 // Interactive data manipulation language (subset of SQL)
00009 //-------------------------------------------------------------------*--------*
00010 
00011 #ifndef __SUBSQL_H__
00012 #define __SUBSQL_H__
00013 
00014 BEGIN_GIGABASE_NAMESPACE
00015 
00016 enum SubSqlTokens {
00017     tkn_alter = tkn_last_token,
00018     tkn_array,
00019     tkn_autoincrement,
00020     tkn_autocommit,
00021     tkn_backup,
00022     tkn_bool,
00023     tkn_commit,
00024     tkn_compactify,
00025     tkn_count,
00026     tkn_create,
00027     tkn_delete,
00028     tkn_describe,
00029     tkn_drop,
00030     tkn_exit,
00031     tkn_export,
00032     tkn_hash,
00033     tkn_help,
00034     tkn_http, 
00035     tkn_import,
00036     tkn_index,
00037     tkn_int1,
00038     tkn_int2,
00039     tkn_int4,
00040     tkn_int8,
00041     tkn_inverse,
00042     tkn_of,
00043     tkn_off,
00044     tkn_on,
00045     tkn_open,
00046     tkn_real4,
00047     tkn_real8,
00048     tkn_rectangle,
00049     tkn_reference,
00050     tkn_restore,
00051     tkn_rollback,
00052     tkn_server,
00053     tkn_set,
00054     tkn_stop,
00055     tkn_semi,
00056     tkn_show,
00057     tkn_to,
00058     tkn_update,
00059     tkn_values,
00060     tkn_version
00061 };
00062 
00063 struct tableField {
00064     char_t* name;
00065     char_t* refTableName;
00066     char_t* inverseRefName;
00067     int     type;
00068 
00069     tableField() { name = refTableName = inverseRefName = NULL; }
00070     ~tableField() { delete[] name; delete[] refTableName; delete[] inverseRefName; }
00071 };
00072 
00073 
00074 class dbList {
00075   public:
00076     enum NodeType {
00077         nInteger,
00078         nBool,
00079         nReal,
00080         nString,
00081         nTuple,
00082         nAutoinc
00083     };
00084 
00085     dbList* next;
00086     int type;
00087     union {
00088         bool    bval;
00089         db_int8 ival;
00090         real8   fval;
00091         char_t* sval;
00092         struct {
00093             int     nComponents;
00094             dbList* components;
00095         } aggregate;
00096     };
00097 
00098     ~dbList() {
00099         if (type == nTuple) {
00100             delete aggregate.components;
00101         } else if (type == nString) {
00102             delete[] sval;
00103         }
00104     }
00105 
00106     dbList(int type) {
00107         this->type = type;
00108         next = NULL;
00109     }
00110 };
00111 
00112 class dbUpdateElement { 
00113   public:
00114     dbUpdateElement*   next;
00115     dbFieldDescriptor* field;
00116     dbExprNode*        value;
00117     char_t*            strValue;
00118 
00119     dbUpdateElement() { 
00120         next = NULL;
00121         strValue = NULL;
00122     }
00123     ~dbUpdateElement() { 
00124         delete[] strValue;
00125     }
00126 };
00127 
00128 #define MAX_HISTORY_SIZE 16
00129 
00130 class dbXmlScanner { 
00131   public:
00132     enum { 
00133         MaxIdentSize = 256
00134     };
00135     enum token { 
00136         xml_ident, 
00137         xml_sconst, 
00138         xml_iconst, 
00139         xml_fconst, 
00140         xml_lt, 
00141         xml_gt, 
00142         xml_lts, 
00143         xml_gts,
00144         xml_eq, 
00145         xml_eof,
00146         xml_error
00147     };    
00148     dbXmlScanner(FILE* f) { 
00149         in = f;
00150         sconst = new char_t[size = 1024];
00151         line = 1;
00152         pos = 0;
00153     }
00154     token scan();
00155 
00156     char_t* getString() { 
00157         return sconst;
00158     }
00159 
00160     char_t* getIdentifier() { 
00161         return ident;
00162     }
00163 
00164     size_t  getStringLength() { 
00165         return slen;
00166     }
00167 
00168     db_int8 getInt() { 
00169         return iconst;
00170     }
00171 
00172     double getReal() { 
00173         return fconst;
00174     }
00175 
00176     bool expect(int sourcePos, token expected) { 
00177         return assure(scan(), sourcePos, expected);
00178     }
00179 
00180     bool assure(token tkn, int sourcePos, token expected) { 
00181         if (tkn != expected) { 
00182             fprintf(stderr, "subsql.cpp:%d: line %d, column %d: Get token %d instead of expected token %d\n", 
00183                     sourcePos, line, pos, tkn, expected);
00184             return false;
00185         }
00186         return true;
00187     }
00188 
00189     bool expect(int sourcePos, char_t* expected) { 
00190         token tkn = scan();
00191         if (tkn != xml_ident) { 
00192             fprintf(stderr, "subsql.cpp:%d: line %d, column %d: Get token %d instead of expected identifier\n", 
00193                     sourcePos, line, pos, tkn);
00194             return false;
00195         }
00196         if (STRCMP(ident, expected) != 0) { 
00197             FPRINTF(stderr, STRLITERAL("subsql.cpp:%d: line %d, column %d: Get tag '%s' instead of expected '%s'\n"), 
00198                     sourcePos, line, pos, ident, expected);
00199             return false;
00200         }
00201         return true;
00202     }
00203 
00204   private:
00205     int   get();
00206     void  unget(int ch);
00207 
00208     int       line;
00209     int       pos;
00210     FILE*     in;
00211     char_t*   sconst;
00212     size_t    size;
00213     size_t    slen;
00214     db_int8   iconst;
00215     double    fconst;
00216     char_t    ident[MaxIdentSize];
00217 };
00218 
00219 class dbTmpAllocator { 
00220     enum { 
00221         CHUNK_SIZE = 4096
00222     };
00223     struct Chunk { 
00224         Chunk* next;
00225         Chunk* prev; // is not used, added for alignment
00226     };
00227     Chunk* curr;
00228     size_t used;
00229 
00230   public:
00231     dbTmpAllocator() { 
00232         curr = NULL;
00233         used = CHUNK_SIZE;
00234     }
00235 
00236     ~dbTmpAllocator() { 
00237         reset();
00238     }
00239 
00240     void reset() { 
00241         Chunk *c, *next; 
00242         for (c = curr; c != NULL; c = next) { 
00243             next = c->next;
00244             dbFree(c);
00245         }
00246         curr = NULL;
00247         used = CHUNK_SIZE;
00248     }
00249 
00250 
00251     void* alloc(size_t size) { 
00252         size = DOALIGN(size, 8);
00253         if (size > CHUNK_SIZE/2) { 
00254             Chunk* newChunk = (Chunk*)dbMalloc(size + sizeof(Chunk));
00255             if (curr != NULL) { 
00256                 newChunk->next = curr->next;
00257                 curr->next = newChunk;
00258             } else { 
00259                 curr = newChunk;
00260                 newChunk->next = NULL;
00261                 used = CHUNK_SIZE;
00262             }
00263             return newChunk+1;
00264         } else if (size <= CHUNK_SIZE - used) { 
00265             used += size;
00266             return (char*)(curr+1) + used - size;
00267         } else { 
00268             Chunk* newChunk = (Chunk*)dbMalloc(CHUNK_SIZE);
00269             used = sizeof(Chunk) + size;
00270             newChunk->next = curr;
00271             curr = newChunk;
00272             return newChunk+1;
00273         }
00274     }
00275 };
00276 
00277     
00278 class dbSubSql : public dbDatabase {
00279   private:
00280     int     pos;
00281     int     line;
00282     int     tknPos;
00283     char_t* buf;
00284     int     buflen;
00285     FILE*   in;
00286     bool    opened;
00287     db_int8 ival;
00288     real8   fval;
00289     char_t* name;
00290 
00291     oid_t*  oidMap;
00292     oid_t   oidMapSize;
00293 
00294     dbTmpAllocator tmpAlloc;
00295     
00296     dbTableDescriptor* metatable;
00297     static char const* prompt;
00298 
00299     dbTableDescriptor* droppedTables;
00300     dbTableDescriptor* existedTables;
00301 
00302     dbQuery query;
00303     dbCompiler compiler;
00304 
00305     int      ungetToken;
00306     bool     autocommit;
00307 
00308     dbEvent daemonTerminationEvent;
00309     dbThread httpServerThread;
00310     HTTPapi* httpServer;
00311     bool     httpServerRunning;
00312     char_t*  databasePath;
00313     char*    queryHistory[MAX_HISTORY_SIZE];
00314     unsigned historyUsed;
00315     unsigned historyCurr;
00316 
00317     static void thread_proc httpServerThreadProc(void* arg);
00318     
00319     void httpServerLoop();
00320 
00321     void startHttpServer(char_t const* address);
00322     void stopHttpServer(char_t const* address);
00323 
00324     void handleError(dbErrorClass error, char const* msg = NULL,  int arg = 0);
00325 
00326     void warning(char const* msg);
00327     void error(char const* msg);
00328 
00329     int  get();
00330     void unget(int ch);
00331     int  scan();
00332     bool parse();
00333 
00334     bool expect(char const* expected, int token);
00335 
00336     void recovery();
00337 
00338     void exportDatabase(FILE* out);
00339     bool importDatabase(FILE* in);
00340    
00341     oid_t mapId(long id);
00342     bool importField(char_t* terminator, dbFieldDescriptor* fd, byte* rec, dbXmlScanner& scanner);
00343     bool importRecord(char_t* terminator, dbFieldDescriptor* fieldList, byte* rec, dbXmlScanner& scanner);
00344     void insertRecord(dbTableDescriptor* desc, oid_t oid, void const* record);
00345 
00346     bool isValidOid(oid_t oid);
00347 
00348     static void dumpRecord(byte* record, dbFieldDescriptor* first);
00349     static int calculateRecordSize(dbList* list, int offs,
00350                                    dbFieldDescriptor* first);
00351     int  initializeRecordFields(dbList* node, byte* dst, int offs,
00352                                       dbFieldDescriptor* first);
00353     bool insertRecord(dbList* list, dbTableDescriptor* desc);
00354     bool readCondition();
00355     int  readExpression();
00356     int  readValues(dbList** chain);
00357     bool updateFields(dbAnyCursor* cursor, dbUpdateElement* elems);
00358     bool updateTable(bool create);
00359     int  parseType(char_t*& reftableName, char_t*& inverseRefName);
00360     int  updateRecords(dbTableDescriptor* desc, dbList *fields, dbList *values, dbAnyCursor &cursor, byte *buf);
00361 
00362     dbFieldDescriptor* readFieldName();
00363   public:
00364     void run(int argc, char* argv[]);
00365     void selectionPage(WWWconnection& con);
00366     void queryPage(WWWconnection& con);
00367     void defaultPage(WWWconnection& con);
00368 
00369     dbSubSql(dbAccessType type = dbAllAccess, size_t poolSize = 0);
00370     virtual~dbSubSql();
00371 };
00372 
00373 END_GIGABASE_NAMESPACE
00374 
00375 
00376 #endif

Generated on Thu Feb 12 18:46:27 2004 for GigaBASE by doxygen 1.3.5