/********************************************************************************** * ######\ * * ## __##\ * * ## / \__| ######\ ######\####\ ######\ ######\ * * ## | \____##\ ## _## _##\ ## __##\ ## __##\ * * ## | ####### |## / ## / ## |## / ## |## / ## | * * ## | ##\ ## __## |## | ## | ## |## | ## |## | ## | * * \###### |\####### |## | ## | ## |####### |\###### | * * \______/ \_______|\__| \__| \__|## ____/ \______/ * * ## | * * ## | * * \__| * **********************************************************************************/ /******************************************************************************************************************************** * TSDB.H * * Inizio sviluppo: 13.10.2016 * * Autore: Tolla * * Descrizione: * * La seguente libreria ha due scopi: * * - Implementare una serie di funzioni che emulino interamente il funzionamento attuale di campo in Visual FoxPro su MSQL * * - Creare delle nuove API per gestire il database in MSQL con cui verranno riscritti i programmi * * Librerie esterne utilizzate: * * - SQLAPI++ (Permette la gestione dei più famosi DB attualmente esistenti) * ********************************************************************************************************************************/ #ifndef __TSDB_H #define __TSDB_H #ifndef __XVTDB_H #include #endif // Tolla tools #ifndef __TTOOLS_H #include #endif #ifndef __STRINGS_H #include #endif // !__STRINGS_H #ifndef __DATE_H #include #endif // !__DATE_H #ifndef __REAL_H #include #endif // !__REAL_H #define CHIAVE_ID_ "_ID_" /******************************************************************************** * SSimpleQuery (Sirio Simple Query) * * "Wrapper" di TXvt_recordset, implementa OGNI funzione in modo tale da * * poterlo sostituire brutalmente in qualsiasi momento così da non aver * * problemi come durante il passaggio da ISAM a SQL (spero). * * Se ti stai domandando perchè non è stata usata l'ereditarietà (magari con * * un bel ": private TXvt_recordset" così da bloccare il tutto) è per bloccare * * qualsiasi riferimento a questa classe in eventuali reimplementazioni future. * * Preferisco scrivere un po' di codice ridondante adesso che andare a fare * * salti mortali dopo * ********************************************************************************/ class SSimpleQuery { protected: TXvt_recordset _rec; /**< Costruttore, accetta in ingresso una connessione, volendo è anche possibile impostare una query ed eseguirla. Attenzione! Non risponde se la query ha avuto un esito positivo o negativo! */ //SSimpleQuery(P_CONN_VOID &c, const char * query = "", bool ex = false); public: /**< Costruttore, non inizializza nulla, da caricare successivamente */ SSimpleQuery() {} /**< Costruttore, Accetta in ingresso dei parametri per la connessione, volendo è anche possibile impostare una query ed eseguirla. Attenzione! Non risponde se la query ha avuto un esito positivo o negativo! */ SSimpleQuery(const char* db, const char* user, const char* pass, TT_driver tipoDb, const char * query = "", const bool ex = false, const bool freezed = false) : _rec(db, user, pass, tipoDb, query, ex, freezed) {} /**< Distruttore */ virtual ~SSimpleQuery() {} // Connection functions /**< Eseguo la connessione */ int sqConnect(const char* db, const char* user, const char* pass, TT_driver tipoDb) { return _rec.connect(db, user, pass, tipoDb); } /* Mi scollego */ void sqDisconnect() { _rec.disconnect(); } /**< Esegue la commit, di default in caso di errore viene chiamato il metodo rollback() */ const bool sqCommit(bool autoRoll = true) { return _rec.commit(); } /**< Esegue il rollback all'ultimo commit */ const bool sqRollback() { _rec.rollback(); } /**< Imposto il tipo di client che utilizzo */ void sqSetClient(int client) { _rec.setClient((TT_driver)client); } void sqSetClient(TT_driver client) { _rec.setClient(client); } // Imposto una opzione generica dellla connessione void sqSetConOption(const char* opt) { _rec.setConOption(opt); } /**< Abilito/Disabilito l'autocommit, (disabilitato di default) */ void sqSetAutocommit(bool ac) { _rec.setAutocommit(ac); } /** Imposta la visibilità delle transazioni (vedi Funzione) */ void sqSetVisibility(isoLvl vis = committed) { _rec.setVisibility(vis); } // Getters /** Ritorna se la conessione è connessa */ const bool sqIsConnect() const { return _rec.isConnect(); } /**< Ritorna se la connessione è attiva */ const bool sqIsAlive() const { return _rec.isAlive(); } /** Ritorna la visibilità impostata */ const int sqGetVisibility() { return _rec.getVisibility(); } /**< Ritorna se è attivo l'autocommit, true -> attivo, false -> disattivo o sconosciuto */ const bool sqGetAutocommit() { return _rec.getAutocommit(); } /**< Ritorna il valore dell'opzione specificata */ const char* sqGetOption(const char* opt) { return _rec.getOption(opt); } /**< Ritorno la versione del Client che sto utilizzando */ const long sqGetClientV() { return _rec.getClientV(); } /**< Ritorno la versione del Server che sto utilizzando */ const char* sqGetServerV() { return _rec.getServerV(); } /**< Ritorno la versione del Server che sto utilizzando in formato numerico */ const long sqGetServerVN() { return _rec.getServerVN(); } /**< Ritorno se il recordset è carico */ const bool sqIsLoaded() const { return _rec.isLoaded(); } /** Ritorno il numero di elementi nella query */ const long sqItems() { _rec.items(); } // Conguration /**< Imposta la query ricevuta come (const char *) nel recordset */ const bool sqSet(const char* query) { return _rec.set(query); } /**< Imposta la query ricevuta come (string) nel recordset */ const bool sqSet(string query) { return _rec.set(query.c_str()); } /**< Imposta la query ricevuta come (TString) nel recordset */ const bool sqSet(TString& query) { return _rec.set(static_cast(query)); } /**< Esegue la query impostata nel recordset, se viene passato autoF == true esegue anche un comando sqNext() */ const bool sqExec(bool autoF = true) { return _rec.exec(autoF); } /**< Unisce le funzioni sqSet e sqExec, riceve la query come (const char *) */ const bool sqSetExec(const char* query, bool autoF = true) { _rec.set(query); return _rec.exec(autoF); } /**< Unisce le funzioni sqSet e sqExec, riceve la query come (string) */ const bool sqSetExec(string query, bool autoF = true) { _rec.set(query.c_str()); return _rec.exec(autoF); } /**< Unisce le funzioni sqSet e sqExec, riceve la query come (TString) */ const bool sqSetExec(TString& query, bool autoF = true); /**< Si sposta avanti di un record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sqNext() { return _rec.next(); } /**< Si sposta indietro di un record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sqPrev() { return _rec.prev(); } /**< Si sposta avanti di un record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sqFirst() { return _rec.first(); } /**< Si sposta al primo record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sqLast() { return _rec.last(); } /**< Si sposta alla posizione n, in caso di esito negativo valorizza _stringError e _codeError */ const bool sqGo(int newPos) { return _rec.go(newPos); } /**< Ritorna il numero di righe affette dall'ultima query */ const int sqRowsAffected() { return _rec.rowsAffected(); } // Getters /**< Ritorna il valore nel campo (field) in formato (int) */ const int sqGetInt(const char* field) { return _rec.get_int(field); } /**< Ritorna il valore nel campo (field) in formato (short) */ const short sqGetShort(const char* field) { return _rec.get_short(field); } /**< Ritorna il valore nel campo (field) in formato (long) */ const long sqGetLong(const char* field) { return _rec.get_long(field); } /**< Ritorna il valore nel campo (field) in formato (double) */ const double sqGetDouble(const char* field) { return _rec.get_double(field); } /**< Ritorna il valore nel campo (field) in formato (bool) */ const bool sqGetBool(const char* field) { return _rec.get_bool(field); } /**< Ritorna il valore nel campo (field) in formato (TDate) */ const TDate sqGetDate(const char* field); /**< Ritorna il valore nel campo (field) in formato (real) */ const real sqGetReal(const char* field); /**< Ritorna il valore nel campo (field) in formato (SADateTime), Campo non gestisce le ore */ //SADateTime sqGetDateTime(const char* field) { get_short(field); } /**< Ritorna il valore nel campo (field) passato come (const char *) in formato (const char *) */ const char* sqGet(const char* field) { return _rec.get(field); } /**< Ritorna il valore nel campo (field) passato come (string) in formato (const char *) */ const char* sqGet(string field) { return _rec.get(field.c_str()); } /**< Ritorna il valore nel campo (field) passato come (TString) in formato (const char *) */ const char* sqGet(TString& field) { return _rec.get(static_cast(field)); } /**< Ritorna il valore nel campo (field) in formato (char) */ const char sqGetChar(const char* field) { return _rec.get_char(field); } /**< Ritorna la posizione attuale */ const long sqPos() const { return _rec.pos(); } // Error Getters /**< Ritorno l'ultimo codice errore segnalato in formato /int) */ const long sqGetCodeError(bool erase = true) { return _rec.get_code_error(erase); } /**< Ritorno l'ultima stringa di errore segnalato in formato (const char *) */ const char* sqGetStringError(bool erase = true) { return _rec.get_string_error(erase); } //char * getCharPointer(const char * field) { return const_cast(static_cast(recset.Field(field).asString())); } }; #endif