/***************************************************************************
 *   Copyright (C) 2004 by Sevin Roland                                    *
 *   upsis@club-internet.fr                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
//                mysqldump CIM10 | gzip > /home/ro/CIM10.gz
//
#include <qapplication.h>
#include <qfileinfo.h>
#include <qfile.h>
#include <qdir.h>
#include <qtextstream.h>
#include <qsqlquery.h>
#include <quuid.h>
#include <qobject.h>
#include <qtextcodec.h>

#include "CApp.h"
#include "../../MedinTuxTools/CGestIni.h"

CApp* G_pCApp = 0;  // contiendra l'instance globale de l'application

//--------------------------------------------- CApp -------------------------------------------------------------------
CApp::CApp(int & argc, char ** argv)
	: CMedinTuxApp(argc, argv) , m_DataBase(0)
    {QTextCodec::setCodecForTr( QTextCodec::codecForName("utf8") );
     QString driver;        // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
     QString dataBaseName;  // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
     QString user;          // = "root"
     QString password;      // = ""
     QString hostname;      // = "localhost"
     m_Start_ID  = "";
     m_Droits    = "";
     m_CloseAfterQuery      = 0;
     if (argc>3) m_Start_ID = argv[3];
     if (argc>4) m_Droits   = argv[4];
     //.................. lire le fichier ini pour en retirer les paramêtres de connexion ...............
     QString str("");

     //QString fn = QFileInfo (qApp->argv()[0]).dirPath(true)+QDir::separator()+"Personnes.ini";
     QString fn = CGestIni::Construct_Name_File_Ini("personnes",QFileInfo (qApp->argv()[0]).dirPath (true),"");

     QFile   file( fn );
     if ( file.open( IO_ReadOnly ) ) str= QTextStream( &file ).read();
     if (CGestIni::Param_ReadParam(  str, "Connexion", "Parametres", &driver, &dataBaseName, &user, &password, &hostname) !=0
        )  // zero = pas d'erreur
        {   driver       = "QODBC3";                   // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
            dataBaseName = "DataSrcODBC-MySQL-DrTux";  // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
            user         = "root";                     // = "root"
            password     = "";                         // = ""
            hostname     = "localhost";                // = "localhost"
        }

     m_DataBase = QSqlDatabase::addDatabase( driver, "PERSONNES" );
     if (m_DataBase==0)                                                                 return;
     m_DataBase->setDatabaseName( dataBaseName );
     m_DataBase->setUserName( user );
     m_DataBase->setPassword( password );
     m_DataBase->setHostName( hostname);
     if ( ! m_DataBase->open() )
        {
            qDebug( "Failed to open medecin database: " +
                      m_DataBase->lastError().driverText() );
            qDebug( m_DataBase->lastError().databaseText() );
                                                                                        return;
        }
     //................. fichier de config des bases.......................................
     str = "";
     fn  = CGestIni::Construct_Name_File_Ini("personnes",QFileInfo (qApp->argv()[0]).dirPath (true),"DataBase.cfg");
     //fn  = QFileInfo (qApp->argv()[0]).dirPath (true)+QDir::separator()+"DataBase.cfg";
     QFile   fileCfg( fn );
     if ( fileCfg.open( IO_ReadOnly ) ) str= QTextStream( &fileCfg ).read();
     else   {qDebug ("Impossible de lire le fichier de config: %s", (const char*) fn);  return;}
     QString err("");
     SetConfBase(str,  err);
     if (err.length()) {qDebug(err);                                                    return;}
     G_pCApp = this;


}
//-----------------------------------------------------  OpenBase -------------------------------------------
int   CApp::OpenBase()
{//if (m_DataBase==0)                                            return 0;
 if (m_CloseAfterQuery==0)                                     return 1;
 if (m_DataBase->isOpen()==FALSE && m_DataBase->open()==FALSE) return 0;
 return 1;
}

//-----------------------------------------------------  CloseBase -------------------------------------------
void CApp::setOpenCloseMode(int mode)
{m_CloseAfterQuery=mode; if (m_CloseAfterQuery==0 && m_DataBase &&  m_DataBase->isOpen()==FALSE) m_DataBase->open();
}

//-----------------------------------------------------  CloseBase -------------------------------------------
void   CApp::CloseBase() //m_CloseAfterQuery
{if (m_CloseAfterQuery==0) return;
 if (m_DataBase->isOpen()) m_DataBase->close();
}

//--------------------------------------------- IsThisDroitExist ----------------------------------------------
/*! \brief Vérifie si le droit : droitToFind  (trois caractères style med adm sgn ....) existe dans la chaine de droits : listDroits 6 fois plus rapide que : listDroits.find(droitToFind) != -1
*/
bool CApp::IsThisDroitExist(const char *droitToFind)
{return IsThisDroitExist(m_Droits, droitToFind);
}
//--------------------------------------------- IsThisDroitExist ----------------------------------------------
/*! \brief Vérifie si le droit : droitToFind  (trois caractères style med adm sgn ....) existe dans la chaine de droits : listDroits 6 fois plus rapide que : listDroits.find(droitToFind) != -1
*/
bool CApp::IsThisDroitExist(const char *listDroits, const char *droitToFind)
{char *pt  = (char*)listDroits;
 while (*pt)
    {if (pt[1]==droitToFind[0] && pt[2]==droitToFind[1] &&pt[3]==droitToFind[2]) return TRUE;
     pt += 4;
    }
 return FALSE;
}

//-----------------------------------------------------  SetConfBase -------------------------------------------
int CApp::SetConfBase(const char* confFile, QString err)
{ char    *deb = (char*) confFile;
  char     *pt = (char*) confFile;
  int     line = 0;

  //............................. nom de la table ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TBL_NAME", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TBL_NAME,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du GUID ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_GUID", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_GUID,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du LOGIN_SIGEMS ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_LOGIN_SIGEMS", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_LOGIN_SIGEMS,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du LOGIN ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_LOGIN", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_LOGIN,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du PASSWORD_SIGEMS ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_PASSWORD_SIGEMS", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_PASSWORD_SIGEMS,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du PASSWORD ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_PASSWORD", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_PASSWORD,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ des droits  ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_DROITS", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_DROITS,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du CODE_SPEC ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_CODE_SPEC", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_CODE_SPEC,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du NUM_ORDRE ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_NUM_ORDRE", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_NUM_ORDRE,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du NOM ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_NOM", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_NOM,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du PRENOM ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_PRENOM", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_PRENOM,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du ADRESSE ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_ADRESSE", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_ADRESSE,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du CONVENTION ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_CONVENTION", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_CONVENTION,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du CODE_POST ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_CODE_POST", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_CODE_POST,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du VILLE ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_VILLE", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_VILLE,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_1 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_1", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_1,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_2 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_2", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_2,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_3 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_3", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_3,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du EMAIL ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_EMAIL", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_EMAIL,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du PK ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_PK", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_PK,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du NOTE ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_NOTE", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_NOTE,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du QUALITE ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_QUALITE", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_QUALITE,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du CODE_REGIME ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_CODE_REGIME", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_CODE_REGIME,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du CODE_REGIME ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_SEXE", &line, err);            if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_SEXE,  &line, err);            if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TITRE ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TITRE", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TITRE,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du CHER ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_CHER", &line, err);     if (err.length())            goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_CHER,  &line, err);     if (err.length())            goto SetConfBase_Error;
  //............................. nom du champ du TEL_TYPE_1 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_TYPE_1", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_TYPE_1,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_TYPE_2 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_TYPE_2", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_TYPE_2,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_TYPE_3 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_TYPE_3", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_TYPE_3,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_ABR_1 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_ABR_1", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_ABR_1,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_ABR_2 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_ABR_2", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_ABR_2,  &line, err);     if (err.length())      goto SetConfBase_Error;
  //............................. nom du champ du TEL_ABR_3 ................................................
  pt      = SetConfBaseGetToken(deb,   "INTERVENANTS_TEL_ABR_3", &line, err);     if (err.length())      goto SetConfBase_Error;
  else pt = SetConfBaseGetValue(pt,   m_INTERVENANTS_TEL_ABR_3,  &line, err);     if (err.length())      goto SetConfBase_Error;
  return 1;

SetConfBase_Error:
  return 0;
}


//--------------------------------------------- SetConfBaseGetToken -----------------------------------------------------
char *CApp::SetConfBaseGetToken(char *pt, const char *token, int *line , QString &err)
{err  = "";
 long len_token = strlen (token);
 while(*pt)
    {SKIP_BLANK_CAR(pt);
     if (strncmp(pt, token, len_token)==0)
        {return pt+len_token;
        }
     NEXT_LINE(pt);
     if (line) *line++;
    }
 err  = "Syntax Error: SetConfBase()\r\n";
 err += token;
 err +=QObject::tr(" non trouvé à la ligne N°: ");
 err += QString::number(*line);
 return 0;
}


//--------------------------------------------- SetConfBaseGetValue -----------------------------------------------------
char *CApp::SetConfBaseGetValue(char *pt, QString &value, int *line , QString &errMess )
{errMess = "";
 value   = "";
  //............. chercher premier delimiteur ...............................
  while(*pt && *pt!='\r' && *pt !='\n' && *pt !='\'' ) pt++;

  if (*pt=='\'')         // si delimiteur pas trouve erreur
    {pt++;               // passer le delimiteur
     char *end = pt;
     //............. chercher deuxiéme delimiteur ...............................
     while(*end && *end!='\r' && *end !='\n' && *end !='\'')end++;
     if (*end=='\'')
        {char dst[256];
	 long len  = end-pt;
	 //end[0]    = 0;          // acces à variable static plante !!!!
	 memcpy(dst, pt, len);
	 dst[len]  = 0;
	 value     = dst;
	 //end[0]    = '\'';
	 return end +1;
        }
     else
       {errMess  = tr("Syntax Error : SetConfBaseGetValue()\r\n Delimiteur \"'\" de fin non trouvé à la ligne N° : ");
        errMess += QString::number(*line);
	return 0;
       }
    }
  else
    {errMess  = tr("Syntax Error : SetConfBaseGetValue()\r\n Delimiteur \"'\" de début non trouvé à la ligne N° : ");
     errMess += QString::number(*line);
     return 0;
    }
}

//-----------------------------------------------------  GUID_Create --------------------------------------------------
QString CApp::GUID_Create()
{
    QString guid ( QUuid::createUuid ().toString());
    return  guid.mid(1, 36);

}


//-------------------------------------------------- OutSQL_error -----------------------------------------------
void CApp::OutSQL_error( const QSqlQuery &cur, const char *messFunc /*=0*/, const char *requete /*=0*/)
{    QSqlError error = cur.lastError();
     QString   qserr("");

     if (error.type() != QSqlError::None)
        {switch (error.type())
           { case    QSqlError::None:           qserr =tr(" - no error occurred: ")          ; break;
             case    QSqlError::Connection:     qserr =tr(" - connection error: ")           ; break;
             case    QSqlError::Statement:      qserr =tr(" - SQL statement syntax error: ") ; break;
             case    QSqlError::Transaction:    qserr =tr(" - transaction failed error: ")   ; break;
	     case    QSqlError::Unknown:        qserr =tr(" - unknown error: ")              ; break;
	     default:                           qserr =tr(" - unknown error: ")              ; break;
           }
	 if (messFunc) qDebug(messFunc);
         qDebug(qserr + error.databaseText ());
	 if (requete) qDebug(requete);
     qDebug(error.text());
        }
//     qDebug(qserr + error.databaseText ());
}

//************************************************************************************************************************
//=================================== GESTION CODE POSTAL ================================================================
//************************************************************************************************************************

//-----------------------------------------------------  GetVilleFromCodePostal -------------------------------------------
QString CApp::GetVilleFromCodePostal(const QString &codePostal)
{ QString result  ("");
  QString requete ("SELECT ville FROM codes_postaux WHERE code_postal = ");
          requete +=  codePostal ;
  if (OpenBase()==0)     return result;
  QSqlQuery* pSqlQuery = new QSqlQuery(requete , m_DataBase );
  if (pSqlQuery && pSqlQuery->isActive() && pSqlQuery->next())
     {result = pSqlQuery->value(0).toString();
     }
  if (pSqlQuery) delete  pSqlQuery;
  CloseBase();
  return result;
}

//-----------------------------------------------------  GetCodePostalFromVille -------------------------------------------
QString CApp::GetCodePostalFromVille(const QString &ville)
{ QString result  ("");
  QString requete ("SELECT code_postal FROM codes_postaux WHERE ville = '");
          requete +=  ville + "'";
  if (OpenBase()==0)   return result;
  QSqlQuery* pSqlQuery = new QSqlQuery(requete , m_DataBase );
  if (pSqlQuery && pSqlQuery->isActive() && pSqlQuery->next())
     {result = pSqlQuery->value(0).toString();
     }
  if (pSqlQuery) delete  pSqlQuery;
  CloseBase();
  return result;
}

//-----------------------------------------------------  GetVilleFromCodePostal -------------------------------------------
void CApp::GetVilleFromCodePostal(const QString &codePostal, QStringList &villeList)
{ QString cdPost (codePostal);
  if (cdPost[0]=='0') cdPost.remove(0,1);
  villeList.clear();
  QString requete ("SELECT ville FROM codes_postaux WHERE code_postal = ");
          requete +=  cdPost ;
  if (OpenBase()==0)   return ;
  QSqlQuery* pSqlQuery = new QSqlQuery(requete , m_DataBase );
  if (pSqlQuery && pSqlQuery->isActive())
     {while(pSqlQuery->next()) villeList.append( pSqlQuery->value(0).toString());
     }
  if (pSqlQuery) delete  pSqlQuery;
  CloseBase();
  return;
}

//-----------------------------------------------------  GetCodePostalFromVille -------------------------------------------
void CApp::GetCodePostalFromVille(const QString &ville, QStringList &codePostalList)
{ codePostalList.clear();
  QString requete ("SELECT code_postal , ville FROM codes_postaux WHERE ville  LIKE '");
          requete +=  ville + "%'";
  if (OpenBase()==0)   return ;
  QSqlQuery* pSqlQuery = new QSqlQuery(requete , m_DataBase );
  if (pSqlQuery && pSqlQuery->isActive())
     {while(pSqlQuery->next())
	 {QString codePostal (pSqlQuery->value(0).toString());
	  if (codePostal.length()<5) codePostal.prepend("0");
	  codePostalList.append( codePostal +" "+pSqlQuery->value(1).toString());
	 }
     }
  if (pSqlQuery) delete  pSqlQuery;
  CloseBase();
  return;
}

