C:/GCC/docs/DocSqlite/wxSqlite/wx_SQLite.cpp

00001 #ifndef WX_SQLITE_CCP_INCLUDED
00002 #define WX_SQLITE_CCP_INCLUDED
00003 
00004 #include <string.h>
00005 #include <cstring>
00006 #include <cstdlib>
00007 #include <ctime>
00008 #include <vector>
00009 #include <algorithm>
00010 #include <iostream>
00011 
00012 #include <wx/wxSqlite/wx_SQLite.h>
00013 #include <wx/wxSqlite/wxsqlite3.cpp>
00014 //------------------------
00015 // procedure acces DDS
00016 //------------------------
00017 
00018 wx_SQL::wx_SQL() {}; //initalisation
00019 wx_SQL::~wx_SQL(){};
00020 
00021 
00022 // Initialisation Field
00023 void wx_SQL::F_Field(int nField, wxString nameField,wxString valeurField,
00024 int typeField, int lenField,   int entierField, int precisionField ,
00025 bool keyField , int ordrefield){
00026 
00027 // si nombre    donné la longeur entierField voir la precision aussi
00028 // si keyField  donné N° ordrefield
00029 DDS_FILE.ds.push_back(DDS(nField, nameField,valeurField,typeField,lenField,entierField,precisionField,keyField,ordrefield));
00030 };
00031 
00032 // Initialisation Index
00033 void wx_SQL::F_Index(int nField, wxString nameField,wxString valeurField,
00034 bool keyField , int ordrefield){
00035 // si keyField  donné N° ordrefield
00036 DDS_FILE.ki.push_back(KID(nField,nameField,valeurField,keyField,ordrefield));
00037 };
00038 
00039 
00040 // Initialisation Index pour lecture Logique
00041 void wx_SQL::L_Index(int nField, wxString nameField,wxString valeurField,
00042 bool keyField , int ordrefield){
00043 DDS_FILE.lg.push_back(KLF(nField,nameField,valeurField,keyField,ordrefield));
00044 };
00045 
00046 // initialisation des Logiques
00047 
00048 void  wx_SQL::L_ValLGF( int i ,const wxString V_field ) {
00049        DDS_FILE.lg[i].wxVal = V_field;
00050 };
00051 
00052 // lecture des fields pour wxString
00053 const wxString  wx_SQL::F_GetDDS( int i ) {
00054 return DDS_FILE.ds[i].wxVal;
00055 };
00056 
00057 
00058 // initialisation des fields pour wxString
00059 void wx_SQL::F_SetDDS( int i ,const wxString V_field ) {
00060         DDS_FILE.ds[i].wxVal = V_field ;
00061 
00062   if (DDS_FILE.ds[i].Key == true) DDS_FILE.ki[DDS_FILE.ds[i].Ord].wxVal = V_field;
00063 };
00064 
00065 void wx_SQL::F_OpnDDS(wxString Fpath)
00066 {
00067 
00068  if  (DDS_FILE.OpnBD == false ) {
00069     DDS_FILE.BD = Fpath ;
00070 //    remove(DDS_FILE.BD);
00071     DDS_FILE.db.Open(DDS_FILE.BD);
00072     DDS_FILE.OpnBD = true;
00073  }
00074 };
00075 
00076 
00077 // fonction de creation Sqlite File  Formatage
00078 wxString wx_SQL::F_CrtDDS(wxString Fname)
00079 {
00080 
00081    wxString Csql ;
00082 
00083    Csql.Clear();
00084    Xsql.Clear();
00085 
00086    Xsql =  _T("CREATE TABLE  ") + Fname  +  _T("(");
00087 
00088    Csql += Xsql;
00089 
00090 
00091     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ds.size() ; i++, f++)
00092   {
00093   Xsql.Clear();
00094   Xsql += DDS_FILE.ds[f].Name ;
00095 
00096       switch (DDS_FILE.ds[f].Type)
00097      {
00098       case xText : Xsql +=_T(" CHAR(") ;
00099                    Xsql += wxString::Format("%d", DDS_FILE.ds[f].Lng ) ;
00100                    break;
00101 
00102       case xInt  : Xsql +=  _T(" INTG(");
00103                    Xsql += wxString::Format("%d", DDS_FILE.ds[f].Lng );
00104                    break;
00105       case xDbl  : Xsql +=  _T(" REAL(") ;
00106       Xsql += wxString::Format("%d.%d", DDS_FILE.ds[f].Ent,DDS_FILE.ds[f].Prs ) ;
00107                    break;
00108      case xDate : Xsql +=  _T("  DATE(") ;
00109                   Xsql += wxString::Format("%d", DDS_FILE.ds[f].Lng ) ;
00110                   break;
00111       case xBlod : Xsql +=  _T(" BLOD ") ; break;
00112      }
00113 
00114       if ( i < DDS_FILE.ds.size()) {
00115       if (DDS_FILE.ds[f].Type != xBlod)  Xsql += ")  ,";
00116      else Xsql +=  "  ,";
00117                                    }
00118 
00119       if ( i == DDS_FILE.ds.size()) {
00120       if (DDS_FILE.ds[f].Type != xBlod)  Xsql += ")  ";
00121 
00122                                   }
00123 
00124 
00125  Csql += Xsql;
00126   }
00127   Xsql.Clear();
00128       if (DDS_FILE.ds[DDS_FILE.ds.size()].Type != xBlod) Xsql = "  );";
00129      else Xsql =  "  ;";
00130 
00131  Csql += Xsql;
00132 
00133  Xsql =Csql.Trim();
00134   return  Xsql ;
00135 };
00136 
00137 
00138 // fonction de creation Sqlite File  Index
00139 
00140 const wxString  wx_SQL::F_IdxDDS(wxString Fname)
00141 {
00142   Xsql.Clear();
00143 // create unique index Key ON bd(Nom ASC , Prenom ASC)
00144  Xsql = "CREATE UNIQUE INDEX Key ON  " + Fname + " (";
00145     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ki.size() ; i++, f++)
00146   {
00147       Xsql += DDS_FILE.ki[f].Name  ;
00148 
00149       if ( i < DDS_FILE.ki.size())   Xsql += " ASC , ";
00150       else Xsql += " ASC  );";
00151 
00152   }
00153   return Xsql;
00154 };
00155 
00156 
00157 // initialisation  de la base de données
00158 bool  wx_SQL::F_CrtTable(wxString Fname, bool index )
00159 {
00160    Xsql.Clear();
00161   try
00162   {
00163     if(false == DDS_FILE.db.TableExists(Fname)){
00164     Xsql = F_CrtDDS (Fname);
00165     DDS_FILE.db.ExecuteQuery(Xsql.c_str());
00166 
00167      if ( index == true ) {
00168                          Xsql = F_IdxDDS (Fname);
00169                          DDS_FILE.db.ExecuteQuery(Xsql);
00170                          }
00171      }
00172     Xsql.Clear();
00173     return Berr = false ;
00174 
00175     }
00176   catch (wxSQLite3Exception& e)
00177   {
00178   Merr =   _T(e.GetMessage());
00179   return Berr = true ;
00180   }
00181 
00182 };
00183 
00184 
00185 //  INSERT BD
00186 bool wx_SQL::F_WrtDDS(wxString Fname) {
00187  Xsql.Clear();
00188    Xsql = "insert into " + Fname + " values (";
00189     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ds.size() ; i++, f++)
00190   {
00191       if ( i < DDS_FILE.ds.size())  Xsql += "?,";
00192      else Xsql +=  "?);";
00193 
00194   }
00195 
00196   try
00197   {
00198 
00199     DDS_FILE.stm = DDS_FILE.db.PrepareStatement(Xsql);
00200     DDS_FILE.db.Begin();
00201     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ds.size() ; i++, f++)
00202     {
00203       DDS_FILE.stm.Bind(i,DDS_FILE.ds[f].wxVal);
00204 
00205     }
00206      DDS_FILE.stm.ExecuteUpdate();
00207      DDS_FILE.db.Commit();
00208      DDS_FILE.stm.Finalize();
00209      return Berr = false ;
00210 
00211     }
00212   catch (wxSQLite3Exception& e)
00213   {  DDS_FILE.db.Commit();
00214      DDS_FILE.stm.Finalize();
00215   Merr =   _T(e.GetMessage());Merr +=   _T("AJout Enregistrement Berr");
00216   return Berr = true ;
00217   }
00218 
00219 };
00220 //  UPDATE BD
00221 bool wx_SQL::F_UpdDDS(wxString Fname) {
00222 int nerr ;
00223  Xsql.Clear();
00224      Xsql = "UPDATE " + Fname + "  SET ";
00225 
00226     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ds.size() ; i++, f++)
00227   {
00228       if ( i < DDS_FILE.ds.size())  Xsql += DDS_FILE.ds[f].Name + "=?,";
00229      else Xsql += DDS_FILE.ds[f].Name+ "=?  ";
00230 
00231   }
00232 
00233 
00234      Xsql += " WHERE " ;
00235     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ki.size() ; i++, f++)
00236   {
00237       if ( i < DDS_FILE.ki.size()) Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal.Trim()+ "'  and " ;
00238      else Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal.Trim() + "'  ";
00239 
00240  }
00241 
00242        Xsql +=  " ;" ;
00243 
00244 
00245 // debug
00246 //   Merr = Xsql;  return Berr = true ;
00247   try
00248   {
00249 
00250    DDS_FILE.stm = DDS_FILE.db.PrepareStatement(Xsql);
00251    DDS_FILE.db.Begin();
00252     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ds.size() ; i++, f++)
00253     {
00254       DDS_FILE.stm.Bind(i,DDS_FILE.ds[f].wxVal);
00255 
00256     }
00257   nerr = DDS_FILE.stm.ExecuteUpdate();
00258    DDS_FILE.db.Commit();
00259    DDS_FILE.stm.Finalize();
00260  if (nerr > 0 )  return Berr = false ;
00261  else { Merr =   _T("Aucune Mise a jour");  return Berr = true ;}
00262     }
00263   catch (wxSQLite3Exception& e)
00264   {  DDS_FILE.db.Commit();
00265      DDS_FILE.stm.Finalize();
00266   Merr =   _T(e.GetMessage());Merr += _T("Aucune Mise a jour");
00267   return Berr = true ;
00268   }
00269 
00270 };
00271 
00272 
00273 //  UPDATE BD ( 1 Field)
00274 bool wx_SQL::F_UpdDDSx(wxString Fname,unsigned int x) {
00275  int nerr;
00276 
00277  Xsql.Clear();
00278     Xsql = "UPDATE " + Fname + "  SET ";
00279     Xsql += DDS_FILE.ds[x].Name  +  " =? ";
00280      Xsql += " WHERE " ;
00281     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ki.size() ; i++, f++)
00282   {
00283       if ( i < DDS_FILE.ki.size()) Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal.Trim()+ "'  and " ;
00284      else Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal.Trim() + "'  ";
00285 
00286  }
00287       Xsql +=  " ;" ;
00288 //if( DDS_FILE.db.CheckSyntax(Xsql)== false) {Merr = _T(Xsql); return Berr = true ;  }
00289 
00290 
00291   try
00292   {
00293      DDS_FILE.stm = DDS_FILE.db.PrepareStatement(Xsql);
00294     DDS_FILE.db.Begin();
00295           DDS_FILE.stm.Bind(1,DDS_FILE.ds[x].wxVal);
00296   nerr = DDS_FILE.stm.ExecuteUpdate();
00297    DDS_FILE.db.Commit();
00298    DDS_FILE.stm.Finalize();
00299 
00300  if (nerr > 0 )  return Berr = false ;
00301  else { Merr =   _T("Aucune Mise a jour"); Merr = Xsql; return Berr = true ;}
00302 
00303     }
00304   catch (wxSQLite3Exception& e)
00305   {  DDS_FILE.db.Commit();
00306        DDS_FILE.stm.Finalize();
00307   Merr =   _T(e.GetMessage()); Merr += _T("Aucune Mise a jour");
00308   return Berr = true ;
00309   }
00310 
00311 };
00312 
00313 //DELETE DB
00314 bool wx_SQL::F_DltDDS(wxString Fname) {
00315 int nerr;
00316  Xsql.Clear();
00317    Xsql = "DELETE FROM  " + Fname + " WHERE " ;
00318 
00319     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ki.size() ; i++, f++)
00320   {
00321       if ( i < DDS_FILE.ki.size()) Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal + "'  and " ;
00322      else Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal + "' ";
00323 
00324   }
00325       Xsql +=  " ;" ;
00326 // Merr = Xsql;  return valide = true ;
00327   try
00328   {
00329      DDS_FILE.stm = DDS_FILE.db.PrepareStatement(Xsql);
00330     DDS_FILE.db.Begin();
00331   nerr = DDS_FILE.stm.ExecuteUpdate();
00332    DDS_FILE.db.Commit();
00333    DDS_FILE.stm.Finalize();
00334 
00335  if (nerr > 0 )  return Berr = false ;
00336  else { Merr =   _T("Aucune Suppression");  return Berr = true ;}
00337 
00338     }
00339   catch (wxSQLite3Exception& e)
00340   {  DDS_FILE.db.Commit();
00341   Merr =   _T(e.GetMessage());  Merr = Xsql;
00342   return Berr = true ;
00343   }
00344 
00345 };
00346 
00347 
00348 // Stell Index Unique
00349 bool wx_SQL::F_ChainDDS(wxString Fname) {
00350  Xsql.Clear();
00351    Xsql = "SELECT * from  " + Fname + " WHERE " ;
00352 
00353     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ki.size() ; i++, f++)
00354   {
00355       if ( i < DDS_FILE.ki.size()) Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal + "'  and " ;
00356      else Xsql += DDS_FILE.ki[f].Name  +  " ='" +DDS_FILE.ki[f].wxVal + "'  ORDER BY  ";
00357 
00358   }
00359      for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ki.size() ; i++, f++)
00360   {
00361       if ( i < DDS_FILE.ki.size()) Xsql += DDS_FILE.ki[f].Name   + "  , " ;
00362      else Xsql += DDS_FILE.ki[f].Name   + " ";
00363 
00364   }
00365        Xsql +=  " ;" ;
00366 //debug
00367 // Merr = Xsql;
00368   try
00369   {
00370 
00371      DDS_FILE.tbl= DDS_FILE.db.GetTable(Xsql);
00372 
00373     for ( unsigned int i = 1,f =0 ; i <= DDS_FILE.ds.size() ; i++, f++)
00374     {
00375      F_SetDDS( f ,DDS_FILE.tbl.GetAsString(f) )  ;
00376     }
00377 
00378 
00379     return Berr = false ;
00380     }
00381   catch (wxSQLite3Exception& e)
00382   {
00383   Merr =   _T(e.GetMessage());  Merr = Xsql;
00384   return Berr= true ;
00385   }
00386 
00387 };
00388 
00389 
00390 
00391 
00392 
00393 
00394 //*********************************************************
00395 //*********************************************************
00396 // Gestion Lecture sur logique
00397 //*********************************************************
00398 //*********************************************************
00399 
00400 
00401 
00402 // Setll read *all
00403 bool wx_SQL::L_RallDDS(wxString Fname) {
00404  Xsql.Clear();
00405 unsigned int knbr;
00406 unsigned int kx;
00407    Xsql = "SELECT * from  " + Fname + " ORDER BY " ;
00408 
00409 for( kx=0 , knbr = 0; DDS_FILE.klg[kx]  <99; kx++) knbr ++;
00410 
00411     for ( unsigned int i = 1,f =0 ; i <= knbr ; i++, f++)
00412   {
00413       if ( i < knbr) {
00414 if (DDS_FILE.lg[DDS_FILE.klg[f]].Desc == 1 )  Xsql +=  DDS_FILE.lg[DDS_FILE.klg[f]].Name   + " DESC, ";
00415 else Xsql += DDS_FILE.lg[DDS_FILE.klg[f]].Name   + " , " ;
00416                      }
00417      else { Xsql += DDS_FILE.lg[DDS_FILE.klg[f]].Name   + "  ";
00418             if (DDS_FILE.lg[DDS_FILE.klg[f]].Desc == 1 )  Xsql += " DESC ";
00419           }
00420   }
00421       Xsql +=  " ;" ;
00422 // Merr = Xsql;  return valide = true ;
00423   try
00424   {
00425 
00426      DDS_FILE.qry= DDS_FILE.db.ExecuteQuery(Xsql);
00427     return Berr = false ;
00428     }
00429   catch (wxSQLite3Exception& e)
00430   {
00431   Merr =   _T(e.GetMessage());  Merr = Xsql;
00432   return Berr = true ;
00433   }
00434 
00435 };
00436 
00437 
00438 // Setll  read *GE
00439 bool wx_SQL::L_ReadGeDDS(wxString Fname ) {
00440  Xsql.Clear();
00441 unsigned int knbr;
00442 unsigned int kx;
00443 
00444 for( kx=0 , knbr = 0; DDS_FILE.slg[kx]  <99; kx++) knbr ++;
00445 
00446    Xsql = "SELECT * from  " + Fname ;
00447 //==========================================================================================
00448 // debut WHERE
00449 //==========================================================================================
00450 
00451        if (  knbr >= 1) Xsql += " WHERE " ;
00452       if (  knbr == 1) {
00453            Xsql += DDS_FILE.lg[DDS_FILE.slg[0]].Name ;
00454            if ( DDS_FILE.lg[DDS_FILE.slg[0]].Sel == 1) Xsql += " ='" ; else Xsql += " >='";
00455            Xsql += DDS_FILE.lg[DDS_FILE.slg[0]].wxVal + "'    " ;
00456       }
00457 
00458 
00459 
00460       if (  knbr > 1){
00461     for ( unsigned int i = 1,f =0 ; i <=  knbr    ; i++, f++)
00462   {  if ( i < knbr) {
00463             Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].Name ;
00464             if ( DDS_FILE.lg[DDS_FILE.slg[f]].Sel == 1) Xsql += " ='" ; else Xsql += " >='";
00465             Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].wxVal + "'  and " ;
00466      }
00467      else { Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].Name ;
00468             if ( DDS_FILE.lg[DDS_FILE.slg[f]].Sel == 1) Xsql += " ='" ; else Xsql += " >='";
00469             Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].wxVal + "'     ";
00470      }
00471   }
00472                      }
00473 //==========================================================================================
00474 // debut ORDER BY
00475 //==========================================================================================
00476 
00477 for( kx=0 , knbr = 0; DDS_FILE.klg[kx]  <99; kx++) knbr ++;
00478 
00479       Xsql +=  "  ORDER BY  ";
00480 
00481 
00482     for ( unsigned int i = 1,f =0 ; i <= knbr ; i++, f++)
00483   {
00484       if ( i < knbr) {
00485 if (DDS_FILE.lg[DDS_FILE.klg[f]].Desc == 1 )  Xsql +=  DDS_FILE.lg[DDS_FILE.klg[f]].Name   + " DESC , ";
00486 else Xsql += DDS_FILE.lg[DDS_FILE.klg[f]].Name   + " , " ;
00487                      }
00488      else { Xsql += DDS_FILE.lg[DDS_FILE.klg[f]].Name   + "  ";
00489             if (DDS_FILE.lg[DDS_FILE.klg[f]].Desc == 1 )  Xsql += " DESC ";
00490           }
00491   }
00492 
00493     Xsql +=  " ;" ;
00494 // Merr = Xsql;   // return valide = true ;
00495   try
00496   {
00497 
00498      DDS_FILE.qry= DDS_FILE.db.ExecuteQuery(Xsql);
00499 
00500     return Berr = false ;
00501     }
00502   catch (wxSQLite3Exception& e)
00503   {
00504   Merr =   _T(e.GetMessage());  Merr = Xsql;
00505   return Berr = true ;
00506   }
00507 
00508 };
00509 
00510 
00511 
00512 
00513 
00514 // Setll  readp *LE
00515 bool wx_SQL::L_ReadpLeDDS(wxString Fname) {
00516  Xsql.Clear();
00517    Xsql = "SELECT * from  " + Fname ;
00518 unsigned int knbr;
00519 unsigned int kx;
00520 
00521 for( kx=0 , knbr = 0; DDS_FILE.slg[kx]  <99; kx++) knbr ++;
00522 
00523 //==========================================================================================
00524 // debut WHERE
00525 //==========================================================================================
00526 
00527        if (  knbr >= 1) Xsql += " WHERE " ;
00528       if (  knbr == 1) {
00529            Xsql += DDS_FILE.lg[DDS_FILE.slg[0]].Name ;
00530            if ( DDS_FILE.lg[DDS_FILE.slg[0]].Sel == 1) Xsql += " ='" ; else Xsql += " <='";
00531            Xsql += DDS_FILE.lg[DDS_FILE.slg[0]].wxVal + "'    " ;
00532       }
00533 
00534 
00535 
00536       if (  knbr > 1){
00537     for ( unsigned int i = 1,f =0 ; i <=  knbr    ; i++, f++)
00538   {  if ( i < knbr) {
00539             Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].Name ;
00540             if ( DDS_FILE.lg[DDS_FILE.slg[f]].Sel == 1) Xsql += " ='" ; else Xsql += " <='";
00541             Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].wxVal + "'  and " ;
00542      }
00543      else { Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].Name ;
00544             if ( DDS_FILE.lg[DDS_FILE.slg[f]].Sel == 1) Xsql += " ='" ; else Xsql += " <='";
00545             Xsql += DDS_FILE.lg[DDS_FILE.slg[f]].wxVal + "'     ";
00546      }
00547   }
00548                      }
00549 //==========================================================================================
00550 // debut ORDER BY
00551 //==========================================================================================
00552 
00553 for( kx=0 , knbr = 0; DDS_FILE.klg[kx]  <99; kx++) knbr ++;
00554 
00555     Xsql += "  ORDER BY  ";
00556 
00557 
00558     for ( unsigned int i = 1,f =0 ; i <= knbr ; i++, f++)
00559   {
00560       if ( i < knbr ) Xsql += DDS_FILE.lg[DDS_FILE.klg[f]].Name   + "  DESC, " ;
00561      else Xsql += DDS_FILE.lg[DDS_FILE.klg[f]].Name   + "  DESC";
00562 
00563   }
00564       Xsql +=  " ;" ;
00565 Merr = _T(Xsql);  //return Berr = true ;
00566   try
00567   {
00568 
00569      DDS_FILE.qry= DDS_FILE.db.ExecuteQuery(Xsql);
00570 
00571     return Berr = false ;
00572     }
00573   catch (wxSQLite3Exception& e)
00574   {
00575   Merr =   _T(e.GetMessage());  Merr = Xsql;
00576   return Berr = true ;
00577   }
00578 
00579 };
00580 
00581 #endif

Generated on Fri Aug 4 18:17:15 2006 for wx_Sqlite by  doxygen 1.4.6-NO