Simone Palacino abe7dd1461 Patch level : 12.0 no-patch
Files correlati     : tsdb
Commento            : Prima implementazione TD_recordset per SQLAPI
2019-03-12 18:04:44 +01:00

192 lines
4.7 KiB
C++

#include <tsdb.h>
#include <set>
/******************************************************************************
* SSimpleQuery *
* Classe per esecuzioni di query temporanee (wrapper semplice per SACommand) *
******************************************************************************/
const TDate SSimple_query::sq_get_date(const char * field)
{
const TDate app(_rec.get_date(field));
return app;
}
const real SSimple_query::sq_get_real(const char * field)
{
const real app(_rec.get(field));
return app;
}
TString SSimple_query::sq_get(const char* field, bool rtrim)
{
TString fld = _rec.get(field);
if (rtrim)
fld.rtrim();
return fld;
}
TString SSimple_query::sq_get(const string& field, const bool rtrim)
{
return sq_get(field.c_str(), rtrim);
}
TString SSimple_query::sq_get(TString& field, bool rtrim)
{
return sq_get(static_cast<const char*>(field), rtrim);
}
void TDB_recordset::reset()
{
_first_row = 0;
_items = 0;
_current_row = -1;
_pagesize = 512;
_page.destroy();
_column.destroy();
_columns_loaded = false;
}
/* La query può iniziare con la stringa di connessione così fatta:
* CONNECT(server, user, psw, driver = "MSSQL") il driver può essere omesso
* Se c'è la estraggo, setto la connessione e prendo la vera query
* Se no setto direttamente la query
*/
void TDB_recordset::set(const char* sql)
{
TString real_query = "";
// Guardo se la query inizia con la stringa di connessione
if(TString(sql).starts_with("CONNECT(", true))
{
TString query(sql);
int pos_EOCon = query.find(')'); // End Of Conn
TString conn_str = query.sub(0, ++pos_EOCon);
set_connection(conn_str);
real_query << query.sub(pos_EOCon);
}
else
real_query << sql;
if (!_freezed || _sql != sql)
reset();
_sql.cut(0) << real_query;
if (_sql.find("SELECT") >= 0 || _sql.find("select") >= 0)
find_and_reset_vars();
_rec->sq_set(_sql);
}
void TDB_recordset::set_connection(const char* conn_str)
{
TString pn(conn_str);
TString srv = "", usr = "", pwd = "", drv = "";
int first_pos = pn.find("(", 0);
int last_pos = pn.find(",", first_pos);
int i;
for (i = 0; last_pos != -1; last_pos = pn.find(",", first_pos + 1), i++) {
switch (i)
{
case 0:
srv = pn.sub(first_pos + 1, last_pos);
break;
case 1:
usr = pn.sub(first_pos + 1, last_pos);
break;
case 2:
pwd = pn.sub(first_pos + 1, last_pos);
break;
case 3:
drv = pn.sub(first_pos + 1, last_pos);
default:
break;
}
first_pos = last_pos;
}
// Guardo se ho valorizzato almeno i primi 3 elementi della connect
// Se non valorizzo l'ultimo metto come default MSSQL Server
if (i == 2)
connect(srv.ltrim(), usr.ltrim(), pwd.ltrim(), TSDB_MSSQL);
else if(i == 3)
connect(srv.ltrim(), usr.ltrim(), pwd.ltrim(), str_to_driver(drv.ltrim()));
}
int TDB_recordset::connect(const char * db, const char * user, const char * pass, const TT_driver tipo_db)
{
return _rec->sq_connect(db, user, pass, tipo_db);
}
TT_driver TDB_recordset::str_to_driver(const char* tipo_db)
{
if (_stricmp(tipo_db, "") != 0)
return TSDB_undefined;
//! ODBC
if (_stricmp(tipo_db, "ODBC") == 0)
return TSDB_ODBC;
//! Oracle
if (_stricmp(tipo_db, "Oracle") == 0)
return TSDB_Oracle;
//! Microsoft SQL Server
if (_stricmp(tipo_db, "MSSQL") == 0)
return TSDB_MSSQL;
//! InterBase or Firebird
if (_stricmp(tipo_db, "InterBase") == 0)
return TSDB_InterBase;
//! SQLBase
if (_stricmp(tipo_db, "SQLBase") == 0)
return TSDB_SQLBase;
//! IBM DB2
if (_stricmp(tipo_db, "DB2") == 0)
return TSDB_DB2;
//! Informix
if (_stricmp(tipo_db, "Informix") == 0)
return TSDB_Informix;
//! Sybase ASE
if (_stricmp(tipo_db, "Sybase") == 0)
return TSDB_Sybase;
//! MySQL
if (_stricmp(tipo_db, "MySQL") == 0)
return TSDB_MySQL;
//! PostgreSQL
if (_stricmp(tipo_db, "PostgreSQL") == 0)
return TSDB_PostgreSQL;
//! SQLite
if (_stricmp(tipo_db, "SQLite") == 0)
return TSDB_SQLite;
//! SQL Anywere
if (_stricmp(tipo_db, "SQLAnywhere") == 0)
return TSDB_SQLAnywhere;
return TSDB_undefined;
}
int TDB_recordset::connect(const char* db, const char* user, const char* pass, const char* tipo_db) const
{
return _rec->sq_connect(db, user, pass, str_to_driver(tipo_db));
}
TRecnotype TDB_recordset::items() const
{
if(!_rec->sq_is_loaded() && _items == 0)
_rec->sq_exec();
return _rec->sq_items();
}
bool TDB_recordset::move_to(TRecnotype pos)
{
return _rec->sq_go(pos);
}
TDB_recordset::TDB_recordset(const char* sql, const bool freezed) : _freezed(freezed), _loaded(false)
{
set(sql);
_rec = new SSimple_query();
_items = 0;
set(sql);
}