/********************************************************************************** * ######\ * * ## __##\ * * ## / \__| ######\ ######\####\ ######\ ######\ * * ## | \____##\ ## _## _##\ ## __##\ ## __##\ * * ## | ####### |## / ## / ## |## / ## |## / ## | * * ## | ##\ ## __## |## | ## | ## |## | ## |## | ## | * * \###### |\####### |## | ## | ## |####### |\###### | * * \______/ \_______|\__| \__| \__|## ____/ \______/ * * ## | * * ## | * * \__| * **********************************************************************************/ /******************************************************************************************************************************** * 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 MSSQL 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_" #include "recset.h" /******************************************************************************** * 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 SSimple_query { 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: // Connection functions /**< Eseguo la connessione */ int sq_connect(const char* db, const char* user, const char* pass, const TT_driver tipo_db) { return _rec.connect(db, user, pass, tipo_db); } /* Mi scollego */ void sq_disconnect() { _rec.disconnect(); } /**< Esegue la commit, di default in caso di errore viene chiamato il metodo rollback() */ const bool sq_commit(const bool auto_roll = true) { return _rec.commit(auto_roll); } /**< Esegue il rollback all'ultimo commit */ const bool sq_rollback() { return _rec.rollback(); } /**< Imposto il tipo di client che utilizzo */ void sq_set_client(int client) { _rec.set_client(static_cast(client)); } void sq_set_client(const TT_driver client) { _rec.set_client(client); } // Imposto una opzione generica della connessione void sq_set_con_option(const char* opt, const char* val) { _rec.set_con_option(opt, val); } /**< Abilito/Disabilito l'autocommit, (disabilitato di default) */ void sq_set_autocommit(const bool ac) { _rec.set_autocommit(ac); } /** Imposta la visibilità delle transazioni (vedi Funzione) */ void sq_set_visibility(const iso_lvl vis = committed) { _rec.set_visibility(vis); } // Getters /** Ritorna se la conessione è connessa */ const bool sq_is_connect() const { return _rec.is_connect(); } /**< Ritorna se la connessione è attiva */ const bool sq_is_alive() const { return _rec.is_alive(); } /** Ritorna la visibilità impostata */ const int sq_get_visibility() { return _rec.get_visibility(); } /**< Ritorna se è attivo l'autocommit, true -> attivo, false -> disattivo o sconosciuto */ const bool sq_get_autocommit() { return _rec.get_autocommit(); } /**< Ritorna il valore dell'opzione specificata */ const char* sq_get_option(const char* opt) { return _rec.get_option(opt); } /**< Ritorno la versione del Client che sto utilizzando */ const long sq_get_client_v() { return _rec.get_client_v(); } /**< Ritorno la versione del Server che sto utilizzando */ const char* sq_get_server_v() { return _rec.get_server_v(); } /**< Ritorno la versione del Server che sto utilizzando in formato numerico */ const long sq_get_server_vn() { return _rec.get_server_vn(); } /**< Ritorno se il recordset è carico */ const bool sq_is_loaded() const { return _rec.is_loaded(); } /** Ritorno il numero di elementi nella query */ const long sq_items() { return _rec.items(); } // Configuration /**< Imposta la query ricevuta come (const char *) nel recordset */ const bool sq_set(const char* query) { return _rec.set(query); } /**< Imposta la query ricevuta come (string) nel recordset */ const bool sq_set(const string& query) { return _rec.set(query.c_str()); } /**< Imposta la query ricevuta come (TString) nel recordset */ const bool sq_set(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 sq_exec(bool autoF = true) { return _rec.exec(autoF); } /**< Unisce le funzioni sqSet e sqExec, riceve la query come (const char *) */ const bool sq_set_exec(const char* query, const bool auto_f = true) { _rec.set(query); return _rec.exec(auto_f); } /**< Unisce le funzioni sqSet e sqExec, riceve la query come (string) */ const bool sq_set_exec(const string& query, const bool auto_f = true) { _rec.set(query.c_str()); return _rec.exec(auto_f); } /**< Unisce le funzioni sqSet e sqExec, riceve la query come (TString) */ const bool sq_set_exec(TString& query, const bool auto_f = true) { return sq_set_exec(static_cast(query), auto_f); } /**< Si sposta avanti di un record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sq_next() { return _rec.next(); } /**< Si sposta indietro di un record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sq_prev() { return _rec.prev(); } /**< Si sposta al primo record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sq_first() { return _rec.first(); } /**< Si sposta all'ultimo record, in caso di esito negativo valorizza _stringError e _codeError */ const bool sq_last() { return _rec.last(); } /**< Si sposta alla posizione n, in caso di esito negativo valorizza _stringError e _codeError */ const bool sq_go(const int new_pos) { return _rec.go(new_pos); } /**< Ritorna il numero di righe affette dall'ultima query */ const int sq_rows_affected() const { return _rec.rows_affected(); } // Getters /**< Ritorna il valore nel campo (field) in formato (int) */ const int sq_get_int(const char* field) { return _rec.get_int(field); } /**< Ritorna il valore nel campo (field) in formato (short) */ const short sq_get_short(const char* field) { return _rec.get_short(field); } /**< Ritorna il valore nel campo (field) in formato (long) */ const long sq_get_long(const char* field) { return _rec.get_long(field); } /**< Ritorna il valore nel campo (field) in formato (double) */ const double sq_get_double(const char* field) { return _rec.get_double(field); } /**< Ritorna il valore nel campo (field) in formato (bool) */ const bool sq_get_bool(const char* field) { return _rec.get_bool(field); } /**< Ritorna il valore nel campo (field) in formato (TDate) */ const TDate sq_get_date(const char* field); /**< Ritorna il valore nel campo (field) in formato (real) */ const real sq_get_real(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 *) */ TString sq_get(const char* field, bool rtrim = true); /**< Ritorna il valore nel campo (field) passato come (string) in formato (const char *) */ TString sq_get(const string& field, bool rtrim = true); /**< Ritorna il valore nel campo (field) passato come (TString) in formato (const char *) */ TString sq_get(TString& field, bool rtrim = true); /**< Ritorna il valore della colonna numero (column) passato come (unsigned int) */ TString sq_get(unsigned int column, bool rtrim = true); /**< Ritorna il valore nel campo (field) in formato (char) */ const char sq_get_char(const char* field) { return _rec.get_char(field); } /**< Ritorna il numero di campi dopo l'ultimo comando di esecuzione effettuato; se il risultato esiste */ unsigned int sq_get_num_fields() const; /**< Ritorna il nome del campo numero (column) in formato (TString= */ TString sq_get_name_field(unsigned column) const; /**< Ritorna la grandezza del campo numero (column) */ int sq_get_width_field(unsigned column) const; /**< Ritorna il tipo del campo numero (column) in formato (TFieldtypes) */ TFieldtypes sq_get_type_field(unsigned column) const; /**< Ritorna la posizione attuale */ const long sq_pos() const { return _rec.pos(); } // Error Getters /**< Ritorno l'ultimo codice errore segnalato in formato /int) */ const long sq_get_code_error(const bool erase = true) { return _rec.get_code_error(erase); } /**< Ritorno l'ultima stringa di errore segnalato in formato (const char *) */ const char* sq_get_string_error(const bool erase = true) { return _rec.get_string_error(erase); } /**< Ritorno l'ultima stringa di errore segnalato in formato (const char *) */ const char* sq_get_text_error(const bool erase = true) { return _rec.get_text_error(erase); } const char* sq_get_token_text_error(const int token, const bool erase = true); // Gestione freeze /**< Controlla se il cursore è bloccato */ const bool is_freezed() const { return _rec.is_freezed(); } /**< Congela il cursore */ void freeze() { _rec.freeze(); } /**< Scongela il cursore */ void defrost() { _rec.defrost(); } /**< Costruttore, non inizializza nulla, da caricare successivamente */ SSimple_query() = default; /**< 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! */ SSimple_query(const char* db, const char* user, const char* pass, const TT_driver tipo_db, const char * query = "", const bool ex = false, const bool freezed = false) : _rec(db, user, pass, tipo_db, query, ex, freezed) {} //SSimpleQuery(const TString& db, const TString& user, const TString& 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 ~SSimple_query() = default; }; #endif