Eneboo - Documentación para desarrolladores
src/sqlite/dataset.h
Ir a la documentación de este archivo.
00001 /**********************************************************************
00002  * Copyright (c) 2002, Leo Seib, Hannover
00003  *
00004  * Project:Dataset C++ Dynamic Library
00005  * Module: Dataset abstraction layer header file
00006  * Author: Leo Seib      E-Mail: leoseib@web.de
00007  * Begin: 5/04/2002
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  *
00027  **********************************************************************/
00028 
00029 
00030 #ifndef _DATASET_H
00031 #define _DATASET_H
00032 
00033 #include <cstdio>
00034 #include <string>
00035 #include <map>
00036 #include <list>
00037 #include <qry_dat.h>
00038 #include <stdarg.h>
00039 
00040 
00041 
00042 
00043 namespace dbiplus {
00044 class Dataset;          // forward declaration of class Dataset
00045 
00046 
00047 #define S_NO_CONNECTION "No active connection";
00048 
00049 #define DB_BUFF_MAX           8*1024    // Maximum buffer's capacity
00050 
00051 #define DB_CONNECTION_NONE      0
00052 #define DB_CONNECTION_OK        1
00053 #define DB_CONNECTION_BAD       2
00054 
00055 #define DB_COMMAND_OK           0       // OK - command executed
00056 #define DB_EMPTY_QUERY          1       // Query didn't return tuples
00057 #define DB_TUPLES_OK            2       // Query returned tuples
00058 #define DB_ERROR                5
00059 #define DB_BAD_RESPONSE         6
00060 #define DB_UNEXPECTED           7       // This shouldn't ever happen
00061 #define DB_UNEXPECTED_RESULT   -1       //For integer functions
00062 
00063 /******************* Class Database definition ********************
00064 
00065    represents  connection with database server;
00066 
00067 ******************************************************************/
00068 class FL_EXPORT Database  {
00069 protected:
00070   bool active;
00071   string error, // Error description
00072     host, port, db, login, passwd, //Login info
00073     sequence_table; //Sequence table for nextid
00074 
00075 public:
00076 /* constructor */
00077   Database();
00078 /* destructor */
00079   virtual ~Database();
00080   virtual Dataset *CreateDataset() const = 0;
00081 /* sets a new host name */
00082   void setHostName(const char *newHost) { host = newHost; }
00083 /* gets a host name */
00084   const char *getHostName(void) const { return host.c_str(); }
00085 /* sets a new port */
00086   void setPort(const char *newPort) { port = newPort; }
00087 /* gets a port */
00088   const char *getPort(void) const { return port.c_str(); }
00089 /* sets a new database name */
00090   void setDatabase(const char *newDb) { db = newDb; }
00091 /* gets a database name */
00092   const char *getDatabase(void) const { return db.c_str(); }
00093 /* sets a new login to database */
00094   void setLogin(const char *newLogin) { login = newLogin; }
00095 /* gets a login */
00096   const char *getLogin(void) const { return login.c_str(); }
00097 /* sets a password */
00098   void setPasswd(const char *newPasswd) { passwd = newPasswd; }
00099 /* gets a password */
00100   const char *getPasswd(void) const { return passwd.c_str(); }
00101 /* active status is OK state */
00102   virtual bool isActive(void) const { return active; }
00103 /* Set new name of sequence table */
00104   void setSequenceTable(const char *new_seq_table) { sequence_table = new_seq_table; };
00105 /* Get name of sequence table */
00106   const char *getSequenceTable(void) { return sequence_table.c_str(); }
00107 
00108 
00109 /* virtual methods that must be overloaded in derived classes */
00110 
00111   virtual int init(void) { return DB_COMMAND_OK; }
00112   virtual int status(void) { return DB_CONNECTION_NONE; }
00113   virtual int setErr(int err_code, const char *qry)=0;
00114   virtual const char *getErrorMsg(void) { return error.c_str(); }
00115         
00116   virtual int connect(void) { return DB_COMMAND_OK; }
00117   virtual int connectFull( const char *newDb, const char *newHost=NULL,
00118                       const char *newLogin=NULL, const char *newPasswd=NULL,const char *newPort=NULL);
00119   virtual void disconnect(void) { active = false; }
00120   virtual int reset(void) { return DB_COMMAND_OK; }
00121   virtual int create(void) { return DB_COMMAND_OK; }
00122   virtual int drop(void) { return DB_COMMAND_OK; }
00123   virtual long nextid(const char* seq_name)=0;
00124 
00125 /* virtual methods for transaction */
00126 
00127   virtual void start_transaction() {};
00128   virtual void commit_transaction() {};
00129   virtual void rollback_transaction() {};
00130 
00131   virtual bool in_transaction() {return false;};
00132 
00133 };
00134 
00135 
00136 
00137 
00138 /******************* Class Dataset definition *********************
00139 
00140   global abstraction for using Databases
00141 
00142 ******************************************************************/
00143 
00144 // define Dataset States type
00145 enum dsStates { dsSelect, dsInsert, dsEdit, dsUpdate, dsDelete, dsInactive };
00146 enum sqlType {sqlSelect,sqlUpdate,sqlInsert,sqlDelete,sqlExec};
00147 
00148 
00149 typedef std::list<string> StringList;
00150 typedef std::map<string,field_value> ParamList;
00151 
00152 
00153 class FL_EXPORT Dataset  {
00154 protected:
00155 /*  char *Host     = ""; //WORK_HOST;
00156   char *Database = ""; //WORK_DATABASE;
00157   char *User     = ""; //WORK_USER;
00158   char *Password = ""; //WORK_PASSWORD;
00159 */
00160 
00161   Database *db;         // info about db connection
00162   dsStates ds_state;            // current state
00163   Fields *fields_object, *edit_object;
00164       
00165 
00166   bool active;                  // Is Query Opened?
00167   bool haveError;
00168   int frecno;                   // number of current row bei bewegung
00169   string sql;
00170 
00171   ParamList plist;              // Paramlist for locate
00172   bool fbof, feof;
00173   bool autocommit;              // for transactions
00174 
00175 
00176 /* Variables to store SQL statements */
00177   string empty_sql;             // Executed when result set is empty
00178   string select_sql;            // May be only single string variable
00179 
00180   StringList update_sql;                // May be an array in complex queries
00181 /* Field values for updating must has prefix :NEW_ and :OLD_ and field name
00182    Example:
00183    update  wt_story set idobject set idobject=:NEW_idobject,body=:NEW_body
00184    where idobject=:OLD_idobject
00185    Essentually fields idobject and body must present in the
00186    result set (select_sql statement) */
00187 
00188   StringList insert_sql;                // May be an array in complex queries
00189 /* Field values for inserting must has prefix :NEW_ and field name
00190    Example:
00191    insert into wt_story (idobject, body) values (:NEW_idobject, :NEW_body)
00192    Essentually fields idobject and body must present in the
00193    result set (select_sql statement) */
00194 
00195   StringList delete_sql;                // May be an array in complex queries
00196 /* Field values for deleing must has prefix :OLD_ and field name
00197    Example:
00198    delete from wt_story where idobject=:OLD_idobject
00199    Essentually field idobject must present in the
00200    result set (select_sql statement) */
00201 
00202 
00203 
00204 
00205 /* Arrays for searching */
00206 //  StringList names, values;
00207 
00208 
00209 /* Makes direct inserts into database via mysql_query function */
00210   virtual void make_insert() = 0;
00211 /* Edit SQL */
00212   virtual void make_edit() = 0;
00213 /* Delete SQL */
00214   virtual void make_deletion() = 0;
00215 
00216 /* This function works only with MySQL database
00217    Filling the fields information from select statement */
00218   virtual void fill_fields(void)=0;
00219 
00220 /* Parse Sql - replacing fields with prefixes :OLD_ and :NEW_ with current values of OLD or NEW field. */
00221   void parse_sql(string &sql);
00222 
00223 /* Returns old field value (for :OLD) */
00224   virtual const field_value f_old(const char *f);
00225 
00226 public:
00227 
00228  virtual int str_compare(const char * s1, const char * s2);
00229 /* constructor */
00230   Dataset();
00231   Dataset(Database *newDb);
00232 
00233 /* destructor */
00234   virtual ~Dataset();
00235 
00236 /* sets a new value of connection to database */
00237   void setDatabase(Database *newDb) { db = newDb; }
00238 /* retrieves  a database which connected */
00239   Database *getDatabase(void) { return db; }
00240 
00241 /* sets a new query string to database server */
00242   void setExecSql(const char *newSql) { sql = newSql; }
00243 /* retrieves a query string */
00244   const char *getExecSql(void) { return sql.c_str(); }
00245 
00246 /* status active is OK query */
00247   virtual bool isActive(void) { return active; }
00248 
00249   virtual void setSqlParams(const char *sqlFrmt, sqlType t, ...); 
00250 
00251 
00252 /* error handling */
00253 //  virtual void halt(const char *msg);
00254 /* sequence numbers */
00255   virtual long nextid(const char *seq_name)=0;
00256 /* sequence numbers */
00257   virtual int num_rows()= 0;
00258 
00259 /* Open SQL query */
00260   virtual void open(const string &sql) = 0;
00261   virtual void open() = 0;
00262 /* func. executes a query without results to return */
00263   virtual int  exec (const string &sql) = 0;
00264   virtual int  exec() = 0;
00265   virtual const void* getExecRes()=0;
00266 /* as open, but with our query exept Sql */
00267   virtual bool query(const char *sql) = 0;
00268 /* Close SQL Query*/
00269   virtual void close();
00270 /* This function looks for field Field_name with value equal Field_value
00271    Returns true if found (position of dataset is set to founded position)
00272    and false another way (position is not changed). */
00273 //  virtual bool lookup(char *field_name, char*field_value);
00274 /* Refresh dataset (reopen it and set the same cursor position) */
00275   virtual void refresh();
00276 
00277 /* Go to record No (starting with 0) */
00278   virtual bool seek(int pos=0);
00279 /* Go to record No (starting with 1) */
00280   virtual bool goto_rec(int pos=1);
00281 /* Go to the first record in dataset */
00282   virtual void first();
00283 /* Go to next record in dataset */
00284   virtual void next();
00285 /* Go to porevious record */
00286   virtual void prev();
00287 /* Go to last record in dataset */
00288   virtual void last();
00289 
00290 /* Check for Ending dataset */
00291   virtual bool eof(void) { return feof; }
00292 /* Check for Begining dataset */
00293   virtual bool bof(void) { return fbof; }
00294 
00295 /* Start the insert mode */
00296   virtual void insert();
00297 /* Start the insert mode (alias for insert() function) */
00298   virtual void append() { insert(); }
00299 /* Start the edit mode */
00300   virtual void edit();
00301 
00302 /* Add changes, that were made during insert or edit states of dataset into the database */
00303   virtual void post();
00304 /* Delete statements from database */
00305   virtual void deletion();
00306 /* Cancel changes, made in insert or edit states of dataset */
00307   virtual void cancel() {};
00308 
00309   virtual void setParamList(const ParamList &params);
00310   virtual bool locate();
00311   virtual bool locate(const ParamList &params);
00312   virtual bool findNext();
00313 
00314 /* func. retrieves a number of fields */
00315 /* Number of fields in a record */
00316   virtual int field_count();                                            
00317   virtual int fieldCount();
00318 /* func. retrieves a field name with 'n' index */
00319   virtual const char *fieldName(int n);
00320 /* func. retrieves a field index with 'fn' field name,return -1 when field name not found */
00321   virtual int  fieldIndex(const char *fn);
00322 /* func. retrieves a field size */
00323   virtual int  fieldSize(int n);
00324 
00325 
00326 /* Set field value */
00327   virtual bool set_field_value(const char *f_name, const field_value &value);
00328 /* alias for set_field_value */
00329   virtual bool sf(const char *f, const field_value &v) { return set_field_value(f,v); }
00330 
00331 
00332 
00333 /* Return field name by it index */
00334 //  virtual char *field_name(int f_index) { return field_by_index(f_index)->get_field_name(); };
00335 
00336 /* Getting value of field for current record */
00337   virtual const field_value get_field_value(const char *f_name);
00338 /* Alias to get_field_value */
00339   const field_value fv(const char *f) { return get_field_value(f); }
00340 
00341 /* ------------ for transaction ------------------- */
00342   void set_autocommit(bool v) { autocommit = v; }
00343   bool get_autocommit() { return autocommit; }
00344 
00345 /* ----------------- for debug -------------------- */
00346   Fields *get_fields_object() {return fields_object;};
00347   Fields *get_edit_object() {return edit_object;};
00348 
00349  private:
00350   void set_ds_state(dsStates new_state) {ds_state = new_state;};        
00351  public:
00352 /* return ds_state value */
00353   dsStates get_state() {return ds_state;};
00354 
00355 /*add a new value to select_sql*/
00356   void set_select_sql(const char *sel_sql);
00357   void set_select_sql(const string &select_sql);
00358 /*add a new value to update_sql*/
00359   void add_update_sql(const char *upd_sql);
00360   void add_update_sql(const string &upd_sql);
00361 /*add a new value to insert_sql*/
00362   void add_insert_sql(const char *ins_sql);
00363   void add_insert_sql(const string &ins_sql);
00364   /*add a new value to delete_sql*/
00365   void add_delete_sql(const char *del_sql);
00366   void add_delete_sql(const string &del_sql);
00367 
00368 /*clear update_sql*/
00369   void clear_update_sql();
00370 /*clear insert_sql*/
00371   void clear_insert_sql();
00372 /*clear delete_sql*/
00373   void clear_delete_sql();
00374 
00375 /*get value of select_sql*/
00376  const char *get_select_sql();
00377 
00378 };
00379 
00380 
00381 
00382 /******************** Class DbErrors definition *********************
00383 
00384                            error handling
00385 
00386 ******************************************************************/
00387 class FL_EXPORT DbErrors  {
00388 
00389 public:
00390 
00391 /* constructor */
00392   DbErrors();
00393   DbErrors(const char *msg, ...);
00394 
00395   const char  * getMsg();
00396  private:
00397  std::string  msg_;
00398 };
00399 
00400 }
00401 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'