Patch level : 12.0 no-patch

Files correlati     : xvtext, include
Commento            :
- Allineato codice xvtdb a standard C++ > 2011 sostituendo i char* a const char*
- Pulito codice generale con refactor di nomi, aggiunta di const
- Aggiunta funzione sq_get_token_text_error che trasforma sq_get_text_error in una TToken_string (separatore \n) e poi torna il token scelto
This commit is contained in:
Mattia Tollari 2019-03-19 16:45:34 +01:00
parent 6e7d8ab89a
commit 724fced6a1
5 changed files with 81 additions and 116 deletions

View File

@ -95,6 +95,12 @@ TFieldtypes SSimple_query::sq_get_type_field(const unsigned column) const
return _nullfld;
}
const char* SSimple_query::sq_get_token_text_error(const int token, const bool erase)
{
TToken_string errors(sq_get_text_error(erase), '\n');
return errors.get(token);
}
void TDB_recordset::reset()
{
_current_row = -1;

View File

@ -105,7 +105,7 @@ public:
/**< 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.getServerV(); }
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 */
@ -187,6 +187,7 @@ public:
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 */

View File

@ -6,7 +6,7 @@
#define _RCS(a) ((SACommand *)a)
#define _ERR(a) ((SAException *)a)
#define _CPY_STR(from,to) (to = strdup(from));
#define _CPY_STR(from,to) (to = _strdup(from));
#define _GET_ERROR(from,to) _CPY_STR(from,to)
#define CHECK_FREEZED if (is_freezed()) { return set_error_freezed(); }
@ -33,17 +33,16 @@ TXvt_recordset::TXvt_recordset() : _freezed(false)
}
TXvt_recordset::TXvt_recordset(const char* db, const char* user, const char* pass, TT_driver tipoDb, const char * query, const bool ex, const bool freezed)
: _db(db), _usr(user), _psw(pass), _drv(tipoDb), _freezed(false)
TXvt_recordset::TXvt_recordset(const char* db, const char* user, const char* pass, const TT_driver tipo_db, const char * query, const bool ex, const bool freezed)
: _db(db), _usr(user), _psw(pass), _drv(tipo_db), _query(""), _recno(0), _freezed(false), _loaded(false)
{
_con = new SAConnection;
if(connect(db, user, pass, tipoDb) == NOERR)
if(connect(db, user, pass, tipo_db) == NOERR)
{
try
{
_recset = new SACommand;
_RCS(_recset)->setConnection(_CON(_con));
// if (query[0] != '\0')
if (query && *query)
{
set(query);
@ -110,33 +109,8 @@ TT_driver TXvt_recordset::str_to_driver(const char* tipo_db)
return tipoDb_driver;
}
int strcmp_ins(const char* str1, const char* str2)
{
int len1 = strlen(str1) + 1;
int len2 = strlen(str2) + 1;
char * str1_up = new char[len1];
char * str2_up = new char[len2];
strcpy_s(str1_up, len1, str1);
strcpy_s(str2_up, len2, str2);
char *p = str1_up;
char *q = str2_up;
while (*p != '\0')
{
*p = toupper(*p);
p++;
}
while (*q != '\0')
{
*q = toupper(*q);
q++;
}
return strcmp(str1_up, str2_up);
}
TXvt_recordset::TXvt_recordset(const char* db, const char* user, const char* pass, const char* tipoDb,
const char* query, const bool ex, const bool freezed) : TXvt_recordset(db, user, pass, str_to_driver(tipoDb), query, ex, freezed)
TXvt_recordset::TXvt_recordset(const char* db, const char* user, const char* pass, const char* tipo_db,
const char* query, const bool ex, const bool freezed) : TXvt_recordset(db, user, pass, str_to_driver(tipo_db), query, ex, freezed)
{
}
@ -156,9 +130,9 @@ TXvt_recordset::~TXvt_recordset()
/* PRIVATE FUNCTIONS **************************************************************************************************/
const bool TXvt_recordset::checkPermission()
const bool TXvt_recordset::check_permission()
{
bool err = is_freezed();
const bool err = is_freezed();
if (err)
set_error_freezed();
return !err;
@ -172,17 +146,17 @@ const bool TXvt_recordset::checkPermission()
int TXvt_recordset::connect(const char* db, const char* user, const char* pass, TT_driver tipo_db)
{
if (_con != NULL)
if (_con != nullptr)
{
// Se è già connesso lo scollego
if (_CON(_con)->isConnected())
_CON(_con)->Disconnect();
SAString dbAddress = db;
SAString db_address = db;
SAString usr = user;
SAString psw = pass;
SAClient_t dbDriver = (SAClient_t)tipo_db;
if (dbAddress.IsEmpty() || usr.IsEmpty())
const SAString psw = pass;
const SAClient_t db_driver = static_cast<SAClient_t>(tipo_db);
if (db_address.IsEmpty() || usr.IsEmpty())
{
_code_error = NOT_INITIALIZED;
_string_error = NOT_INITIALIZEDS;
@ -192,7 +166,7 @@ int TXvt_recordset::connect(const char* db, const char* user, const char* pass,
try
{
// Mi collego
_CON(_con)->Connect(dbAddress, usr, psw, dbDriver);
_CON(_con)->Connect(db_address, usr, psw, db_driver);
// Imposto che non si possono vedere i record non committati
_CON(_con)->setIsolationLevel(SA_ReadCommitted);
_CPY_STR(db, _db);
@ -231,7 +205,7 @@ void TXvt_recordset::disconnect()
}
}
bool TXvt_recordset::commit(bool autoRoll)
bool TXvt_recordset::commit(const bool auto_roll)
{
CHECK_FREEZED
try
@ -240,7 +214,7 @@ bool TXvt_recordset::commit(bool autoRoll)
}
catch (SAException &x)
{
if (autoRoll)
if (auto_roll)
{
rollback();
}
@ -269,17 +243,17 @@ bool TXvt_recordset::rollback()
return true;
}
void TXvt_recordset::set_client(TT_driver client)
void TXvt_recordset::set_client(TT_driver client) const
{
_CON(_con)->setClient((SAClient_t) client);
_CON(_con)->setClient(static_cast<SAClient_t>(client));
}
void TXvt_recordset::set_con_option(const char* opt)
void TXvt_recordset::set_con_option(const char* opt) const
{
_CON(_con)->setOption(opt);
}
void TXvt_recordset::set_autocommit(bool ac)
void TXvt_recordset::set_autocommit(const bool ac) const
{
_CON(_con)->setAutoCommit(ac ? SA_AutoCommitOn : SA_AutoCommitOff);
}
@ -294,7 +268,7 @@ void TXvt_recordset::set_visibility(iso_lvl vis)
* 2 -> Repeatable read.
* 3 -> Serializable.
*/
_CON(_con)->setIsolationLevel((SAIsolationLevel_t)vis);
_CON(_con)->setIsolationLevel(static_cast<SAIsolationLevel_t>(vis));
}
const bool TXvt_recordset::is_connect() const
@ -305,27 +279,27 @@ const bool TXvt_recordset::is_alive() const
{
return _CON(_con)->isAlive();
}
int TXvt_recordset::get_visibility()
int TXvt_recordset::get_visibility() const
{
return (iso_lvl)_CON(_con)->IsolationLevel();
return static_cast<iso_lvl>(_CON(_con)->IsolationLevel());
}
bool TXvt_recordset::get_autocommit()
bool TXvt_recordset::get_autocommit() const
{
return _CON(_con)->AutoCommit() == SA_AutoCommitOn;
}
const char* TXvt_recordset::get_option(const char* opt)
const char* TXvt_recordset::get_option(const char* opt) const
{
return _CON(_con)->Option(opt);
}
long TXvt_recordset::get_client_v()
long TXvt_recordset::get_client_v() const
{
return _CON(_con)->ClientVersion();
}
const char* TXvt_recordset::getServerV()
const char* TXvt_recordset::get_server_v() const
{
return _CON(_con)->ServerVersionString();
}
long TXvt_recordset::get_server_vn()
long TXvt_recordset::get_server_vn() const
{
return _CON(_con)->ServerVersion();
}
@ -340,12 +314,12 @@ long TXvt_recordset::items()
// Per non creare casini utilizzo una funzione apposita che mi ritorna il numero
P_CONN_VOID conn = new SAConnection;
SAString q;
long items = -1;
long items = DEFAULT_ERR_NUMBER;
// Connetto la nuova istanza
try
{
_CON(conn)->Connect(_db, _usr, _psw, (SAClient_t)_drv);
_CON(conn)->Connect(_db, _usr, _psw, static_cast<SAClient_t>(_drv));
items = xvt_rcs_get_items(conn, _query);
if (conn != _con)
delete conn;
@ -395,7 +369,7 @@ bool TXvt_recordset::set(const char* query)
return true;
}
bool TXvt_recordset::exec(bool autoF)
bool TXvt_recordset::exec(const bool auto_f)
{
CHECK_FREEZED
bool ok = false;
@ -403,10 +377,10 @@ bool TXvt_recordset::exec(bool autoF)
{
_RCS(_recset)->Execute();
_loaded = true;
_recno = -1;
_recno = DEFAULT_ERR_NUMBER;
// Se trovo almeno un "select" faccio l'autofetch
SAString s = _RCS(_recset)->CommandText(); s.MakeUpper();
ok = s.Find("SELECT") != SIZE_MAX && autoF ? next() : true;
ok = s.Find("SELECT") != SIZE_MAX && auto_f ? next() : true;
}
catch (SAException &x)
{
@ -418,7 +392,7 @@ bool TXvt_recordset::exec(bool autoF)
return ok;
}
bool TXvt_recordset::set_exec(const char* query, bool auto_f)
bool TXvt_recordset::set_exec(const char* query, const bool auto_f)
{
set(query);
return exec(auto_f);
@ -487,14 +461,6 @@ bool TXvt_recordset::first()
bool TXvt_recordset::last()
{
/* La vita sarebbe molto bella se potessi chiamare la funzione FetchLast(),
// siccome non so la posizione del record eseguo ciclicamente Next
// _RCS(_recset)->FetchLast();
while (Next())
{
_recno++;
}
*/
bool fetched = false;
try
{
@ -513,10 +479,10 @@ bool TXvt_recordset::last()
return fetched;
}
bool TXvt_recordset::go(int new_pos)
bool TXvt_recordset::go(const int new_pos)
{
// Controllo che la nuova posizione non sia fuori dal limite inferiore (Non so quanto è grande il recordset)
bool result = new_pos >= 0 ? true : false;
bool result = new_pos >= 0;
// Se la posizione è minore mi sposto indietro
while (new_pos < _recno && result)
@ -528,13 +494,7 @@ bool TXvt_recordset::go(int new_pos)
{
result = next();
}
/*
// Controllo finale per prevenire errori
if (newPos == _recno)
return true;
else
return false;
*/
return result;
}
@ -554,7 +514,7 @@ int TXvt_recordset::get_int(const char* field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return -1;
return DEFAULT_ERR_NUMBER;
}
}
@ -569,7 +529,7 @@ short TXvt_recordset::get_short(const char* field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return -1;
return DEFAULT_ERR_NUMBER;
}
}
@ -584,7 +544,7 @@ long TXvt_recordset::get_long(const char* field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return -1;
return DEFAULT_ERR_NUMBER;
}
}
@ -599,7 +559,7 @@ double TXvt_recordset::get_double(const char* field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return -1;
return DEFAULT_ERR_NUMBER;
}
}
@ -617,12 +577,7 @@ bool TXvt_recordset::get_bool(const char* field)
return false;
}
}
/*
real TXvt_recordset::GetReal(const char * field)
{
return Get(field);
}
*/
const char* TXvt_recordset::get_date(const char * field)
{
try
@ -637,7 +592,7 @@ const char* TXvt_recordset::get_date(const char * field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return NULL;
return DEFAULT_ERR_STRING;
}
}
@ -656,7 +611,7 @@ const char* TXvt_recordset::get(const char* field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return "";
return DEFAULT_ERR_STRING;
}
}
@ -673,7 +628,7 @@ const char* TXvt_recordset::get(unsigned int field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return '\0';
return DEFAULT_ERR_STRING;
}
}
@ -688,7 +643,7 @@ char TXvt_recordset::get_char(const char* field)
_code_error = x.ErrNativeCode();
_GET_ERROR(x.ErrMessage(), _string_error);
_GET_ERROR(x.ErrText(), _string_error_full_text);
return '\0';
return DEFAULT_ERR_CHAR;
}
}
@ -753,9 +708,9 @@ const char* TXvt_recordset::get_type_field(const unsigned column) const
}
}
long TXvt_recordset::get_code_error(bool erase)
long TXvt_recordset::get_code_error(const bool erase)
{
long app = _code_error;
const long app = _code_error;
if (erase)
_code_error = NOERR;
return app;

View File

@ -62,19 +62,19 @@ protected:
/**< Oggetto della connessione */
P_CONN_VOID _con;
/**< Database */
const char* _db;
const char* _db;
/**< Utente */
const char* _usr;
const char* _usr;
/**< Password */
const char* _psw;
const char* _psw;
/**< Driver */
TT_driver _drv;
TT_driver _drv;
/**< Query */
const char* _query;
const char* _query{};
/**< Ultima stringa con codice di errore ricevuto */
char* _string_error;
const char* _string_error;
/**< Ultima stringa con codice di errore ricevuto full text */
char* _string_error_full_text;
const char* _string_error_full_text;
/**< Ultimo codice di errore ricevuto */
long _code_error;
/**< Numero record corrente */
@ -84,7 +84,7 @@ protected:
/**< Indica se il cursore è caricato */
bool _loaded;
const bool checkPermission();
const bool check_permission();
private:
// Disabilito il copy constructor
@ -94,10 +94,10 @@ public:
/**< Costruttore, crea un oggetto vuoto, PERICOLOSO!! Ricordarsi di inizializzarlo opportunamente con il metodo connect() e set() */
TXvt_recordset();
/**< Costruttore, crea un oggetto dai parametri di connessione passati */
TXvt_recordset(const char* db, const char* user, const char* pass, TT_driver tipoDb, const char * query = "", const bool ex = false, const bool freezed = false);
TXvt_recordset(const char* db, const char* user, const char* pass, TT_driver tipo_db, const char * query = "", const bool ex = false, const bool freezed = false);
static TT_driver str_to_driver(const char* tipo_db);
/**< Costruttore, crea un oggetto dai parametri di connessione passati, chiama costruttore con TT_driver */
TXvt_recordset(const char* db, const char* user, const char* pass, const char* tipoDb, const char * query = "", const bool ex = false, const bool freezed = false);
TXvt_recordset(const char* db, const char* user, const char* pass, const char* tipo_db, const char * query = "", const bool ex = false, const bool freezed = false);
/**< Distruttore */
~TXvt_recordset();
@ -118,18 +118,18 @@ public:
/* Mi scollego */
void disconnect();
/**< Esegue la commit, di default in caso di errore viene chiamato il metodo rollback() */
bool commit(bool autoRoll = true);
bool commit(bool auto_roll = true);
/**< Esegue il rollback all'ultimo commit */
bool rollback();
// Setters
/**< Imposto il tipo di client che utilizzo */
void set_client(TT_driver client);
void set_client(TT_driver client) const;
// Imposto una opzione generica dellla connessione
void set_con_option(const char* opt);
void set_con_option(const char* opt) const;
/**< Abilito/Disabilito l'autocommit, (disabilitato di default) */
void set_autocommit(bool ac);
void set_autocommit(bool ac) const;
/**< Imposta la visibilità delle transazioni (vedi Funzione) */
void set_visibility(iso_lvl vis = committed);
@ -140,17 +140,17 @@ public:
/**< Ritorna se la connessione è attiva */
const bool is_alive() const;
/**< Ritorna la visibilità impostata */
int get_visibility();
int get_visibility() const;
/**< Ritorna se è attivo l'autocommit, true -> attivo, false -> disattivo o sconosciuto */
bool get_autocommit();
bool get_autocommit() const;
/**< Ritorna il valore dell'opzione specificata */
const char* get_option(const char* opt);
const char* get_option(const char* opt) const;
/**< Ritorno la versione del Client che sto utilizzando */
long get_client_v();
long get_client_v() const;
/**< Ritorno la versione del Server che sto utilizzando */
const char* getServerV();
const char* get_server_v() const;
/**< Ritorno la versione del Server che sto utilizzando in formato numerico */
long get_server_vn();
long get_server_vn() const;
/**< Ritorno se il recordset è carico */
bool is_loaded() const;
/**< Ritorno il numero di elementi nella query */
@ -164,7 +164,7 @@ public:
/**< Imposta la query ricevuta nel recordset */
bool set(const char* query);
/**< Esegue la query impostata nel recordset, se viene passato autoF == true esegue anche un comando Next() */
bool exec(bool autoF = true);
bool exec(bool auto_f = true);
/**< Unisce le funzioni Set e Exec, riceve la query */
bool set_exec(const char* query, bool auto_f = true);
/**< Si sposta avanti di un record, in caso di esito negativo valorizza _stringError e _codeError */

View File

@ -4,4 +4,7 @@
#define NOT_INITIALIZED -86552
#define NOT_INITIALIZEDS "ERROR: Recordset not initialized"
#define ERROR_FREEZED -883
#define ERROR_FREEZEDS "ERROR: Recordset on ice"
#define ERROR_FREEZEDS "ERROR: Recordset on ice"
#define DEFAULT_ERR_NUMBER -19
#define DEFAULT_ERR_STRING ""
#define DEFAULT_ERR_CHAR '\0'