Aggiunto supporto per SQLite3 in dll dinamica
git-svn-id: svn://10.65.10.50/branches/R_10_00@22966 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
574734d8a8
commit
de7f099855
@ -495,6 +495,15 @@ XVTDLL BOOLEAN xvt_odbc_free_connection(XVT_ODBC handle);
|
||||
XVTDLL ULONG xvt_odbc_execute(XVT_ODBC handle, const char* sql, ODBC_CALLBACK cb, void* jolly);
|
||||
XVTDLL BOOLEAN xvt_odbc_driver(XVT_ODBC handle, char* str, int max_size);
|
||||
|
||||
XVTDLL BOOLEAN xvt_sql_close(XVT_SQLDB handle);
|
||||
XVTDLL BOOLEAN xvt_sql_driver(XVT_SQLDB handle, char* str, int max_size);
|
||||
XVTDLL ULONG xvt_sql_execute(XVT_SQLDB handle, const char* sql, ODBC_CALLBACK cb, void* jolly);
|
||||
XVTDLL SLIST xvt_sql_list_fields(XVT_SQLDB handle, const char* table);
|
||||
XVTDLL SLIST xvt_sql_list_tables(XVT_SQLDB handle);
|
||||
XVTDLL XVT_SQLDB xvt_sql_open(const char* dsn, const char* usr, const char* pwd, const char* dir);
|
||||
XVTDLL BOOLEAN xvt_sql_table_exists(XVT_SQLDB handle, const char* name);
|
||||
|
||||
|
||||
typedef BOOLEAN PROP_CALLBACK(WINDOW win, XVT_TREEVIEW_NODE node, void* app_data);
|
||||
XVTDLL XVT_TREEVIEW_NODE xvt_prop_add(WINDOW win, const char* type, const char* name, const char* value, const char* label);
|
||||
XVTDLL XVT_TREEVIEW_NODE xvt_prop_current(WINDOW win);
|
||||
|
277
xvaga/xvt_sql.cpp
Normal file
277
xvaga/xvt_sql.cpp
Normal file
@ -0,0 +1,277 @@
|
||||
#include "wxinc.h"
|
||||
#include "xvt.h"
|
||||
|
||||
#include "../wxSqlite3/wxSqlite3.h"
|
||||
|
||||
class XVT_SQLDataBase
|
||||
{
|
||||
public:
|
||||
virtual bool Open(const char* dsn, const char* usr, const char* pwd, const char* dir) = 0;
|
||||
virtual bool Close() = 0 { return false; }
|
||||
virtual bool IsOk() const = 0 { return false; }
|
||||
virtual ULONG Execute(const char* sql, ODBC_CALLBACK cb, void* jolly) = 0;
|
||||
virtual SLIST ListFields(const char* table) const = 0;
|
||||
virtual SLIST ListTables() const = 0;
|
||||
virtual bool TableExists(const char* name) const;
|
||||
|
||||
virtual ~XVT_SQLDataBase() { Close(); }
|
||||
};
|
||||
|
||||
bool XVT_SQLDataBase::TableExists(const char* name) const
|
||||
{
|
||||
bool yes = false;
|
||||
SLIST list = ListTables();
|
||||
for (SLIST_ELT e = xvt_slist_get_first(list); e != NULL && !yes; e = xvt_slist_get_next(list, e))
|
||||
{
|
||||
const char* table = xvt_slist_get(list, e, NULL);
|
||||
yes = xvt_str_compare_ignoring_case(name, table) == 0;
|
||||
}
|
||||
xvt_slist_destroy(list);
|
||||
|
||||
return yes;
|
||||
}
|
||||
|
||||
class XVT_SQLDB_SQLite3 : public XVT_SQLDataBase
|
||||
{
|
||||
wxSQLite3Database* m_pDB;
|
||||
|
||||
protected:
|
||||
virtual bool Open(const char* dsn, const char* usr, const char* pwd, const char* dir);
|
||||
virtual bool Close();
|
||||
virtual bool IsOk() const { return m_pDB != NULL && m_pDB->IsOpen(); }
|
||||
virtual ULONG Execute(const char* sql, ODBC_CALLBACK cb, void* jolly);
|
||||
virtual SLIST ListFields(const char* table) const;
|
||||
virtual SLIST ListTables() const;
|
||||
virtual bool TableExists(const char* name) const;
|
||||
|
||||
public:
|
||||
XVT_SQLDB_SQLite3() : m_pDB(NULL) {}
|
||||
};
|
||||
|
||||
bool XVT_SQLDB_SQLite3::Open(const char* dsn, const char*, const char*, const char*)
|
||||
{
|
||||
Close();
|
||||
m_pDB = new wxSQLite3Database;
|
||||
if (dsn == NULL || *dsn <= ' ')
|
||||
dsn = ":memory:";
|
||||
try
|
||||
{
|
||||
m_pDB->Open(dsn);
|
||||
}
|
||||
catch(wxSQLite3Exception& e)
|
||||
{
|
||||
xvt_dm_post_error(e.GetMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool XVT_SQLDB_SQLite3::Close()
|
||||
{
|
||||
if (IsOk())
|
||||
{
|
||||
delete m_pDB;
|
||||
m_pDB = NULL;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ULONG XVT_SQLDB_SQLite3::Execute(const char* sql, ODBC_CALLBACK cb, void* jolly)
|
||||
{
|
||||
ULONG nRows = 0;
|
||||
if (!IsOk())
|
||||
return nRows;
|
||||
|
||||
if (cb != NULL) // Ho una vera callback?
|
||||
{
|
||||
try
|
||||
{
|
||||
wxSQLite3ResultSet rs = m_pDB->ExecuteQuery(sql);
|
||||
const short numcols = rs.GetColumnCount();
|
||||
|
||||
if (numcols > 0)
|
||||
{
|
||||
char** values = new char*[numcols]; // Lista dei valori del record corrente
|
||||
memset(values, 0, numcols*sizeof(char*));
|
||||
|
||||
char** names = new char*[numcols*2]; // Lista dei nomi dei campi e dei tipi
|
||||
memset(names, 0, numcols*2*sizeof(char*));
|
||||
|
||||
short c;
|
||||
for (c = 0; c < numcols; c++)
|
||||
{
|
||||
names[c] = (char*)(const char*)rs.GetColumnName(c);
|
||||
switch (rs.GetColumnType(c))
|
||||
{
|
||||
case WXSQLITE_INTEGER:
|
||||
case WXSQLITE_FLOAT:
|
||||
names[c+numcols] = "NUMERIC"; break;
|
||||
default:
|
||||
names[c+numcols] = "VARCHAR"; break;
|
||||
}
|
||||
}
|
||||
|
||||
while (rs.NextRow())
|
||||
{
|
||||
for (c = 0; c < numcols; c++)
|
||||
values[c] = (char*)(const char*)rs.GetAsString(c);
|
||||
if (cb(jolly, numcols, values, names) != 0)
|
||||
break;
|
||||
nRows++;
|
||||
}
|
||||
|
||||
delete values; // butta la lista dei valori
|
||||
delete names; // butta la lista dei nomi
|
||||
}
|
||||
}
|
||||
catch(wxSQLite3Exception& e)
|
||||
{
|
||||
xvt_dm_post_error(e.GetMessage() + "\n" + sql);
|
||||
}
|
||||
}
|
||||
else
|
||||
nRows = m_pDB->ExecuteUpdate(sql);
|
||||
|
||||
return nRows;
|
||||
}
|
||||
|
||||
SLIST XVT_SQLDB_SQLite3::ListTables() const
|
||||
{
|
||||
wxASSERT(m_pDB != NULL);
|
||||
SLIST list = xvt_slist_create();
|
||||
const wxString strQuery = wxT("SELECT name FROM sqlite_master WHERE type = 'table'");
|
||||
try
|
||||
{
|
||||
wxSQLite3ResultSet rs = m_pDB->ExecuteQuery(strQuery);
|
||||
while (rs.NextRow())
|
||||
xvt_slist_add_at_elt(list, NULL, rs.GetAsString(0), 0L);
|
||||
}
|
||||
catch(wxSQLite3Exception& e)
|
||||
{
|
||||
xvt_dm_post_error(e.GetMessage() + "\n" + strQuery);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
SLIST XVT_SQLDB_SQLite3::ListFields(const char* strTable) const
|
||||
{
|
||||
SLIST list = NULL;
|
||||
if (TableExists(strTable))
|
||||
{
|
||||
list = xvt_slist_create();
|
||||
wxString strQuery; strQuery << "PRAGMA table_info(" << (const char*)strTable << ");";
|
||||
try
|
||||
{
|
||||
wxSQLite3ResultSet rs = m_pDB->ExecuteQuery(strQuery);
|
||||
while (rs.NextRow())
|
||||
{
|
||||
const wxString strField = rs.GetAsString(1);
|
||||
const wxString strType = rs.GetAsString(2);
|
||||
if (strType == "INTEGER")
|
||||
xvt_slist_add_at_elt(list, NULL, strField, 2L); // intfld
|
||||
if (strType == "NUMERIC")
|
||||
xvt_slist_add_at_elt(list, NULL, strField, 5L); // realfld
|
||||
else
|
||||
xvt_slist_add_at_elt(list, NULL, strField, 1L); // alfafld
|
||||
}
|
||||
}
|
||||
catch(wxSQLite3Exception& e)
|
||||
{
|
||||
xvt_dm_post_error(e.GetMessage() + "\n" + strQuery);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
bool XVT_SQLDB_SQLite3::TableExists(const char* name) const
|
||||
{
|
||||
return m_pDB != NULL && name && *name && m_pDB->TableExists(name);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
// xvt_sql_...
|
||||
///////////////////////////////////////////////////////////
|
||||
|
||||
XVT_SQLDB xvt_sql_open(const char* dsn, const char* usr, const char* pwd, const char* dir)
|
||||
{
|
||||
XVT_SQLDataBase* db = new XVT_SQLDB_SQLite3;
|
||||
db->Open(dsn, usr, pwd, dir);
|
||||
return (XVT_SQLDB)db;
|
||||
}
|
||||
|
||||
BOOLEAN xvt_sql_close(XVT_SQLDB handle)
|
||||
{
|
||||
BOOLEAN ok = handle != NULL;
|
||||
if (ok)
|
||||
{
|
||||
XVT_SQLDataBase* db = (XVT_SQLDataBase*)handle;
|
||||
if (db != NULL)
|
||||
delete db;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
ULONG xvt_sql_execute(XVT_SQLDB handle, const char* sql, ODBC_CALLBACK cb, void* jolly)
|
||||
{
|
||||
ULONG n = 0;
|
||||
if (handle && sql && *sql)
|
||||
{
|
||||
XVT_SQLDataBase* db = (XVT_SQLDataBase*)handle;
|
||||
if (db->IsOk())
|
||||
{
|
||||
try
|
||||
{
|
||||
n = db->Execute(sql, cb, jolly);
|
||||
}
|
||||
catch(wxSQLite3Exception& e)
|
||||
{
|
||||
xvt_dm_post_error(e.GetMessage() + "\n" + sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
SLIST xvt_sql_list_fields(XVT_SQLDB handle, const char* table)
|
||||
{
|
||||
SLIST list = NULL;
|
||||
XVT_SQLDataBase* db = (XVT_SQLDataBase*)handle;
|
||||
if (table && *table && db != NULL && db->IsOk())
|
||||
list = db->ListFields(table);
|
||||
return list;
|
||||
}
|
||||
|
||||
SLIST xvt_sql_list_tables(XVT_SQLDB handle)
|
||||
{
|
||||
SLIST list = NULL;
|
||||
XVT_SQLDataBase* db = (XVT_SQLDataBase*)handle;
|
||||
if (db != NULL && db->IsOk())
|
||||
list = db->ListTables();
|
||||
return list;
|
||||
}
|
||||
|
||||
BOOLEAN xvt_sql_table_exists(XVT_SQLDB handle, const char* name)
|
||||
{
|
||||
BOOLEAN yes = FALSE;
|
||||
if (handle && name && *name)
|
||||
{
|
||||
const XVT_SQLDataBase* db = (XVT_SQLDataBase*)handle;
|
||||
if (db->IsOk())
|
||||
yes = db->TableExists(name);
|
||||
}
|
||||
return yes;
|
||||
}
|
||||
|
||||
|
||||
BOOLEAN xvt_sql_driver(XVT_SQLDB handle, char* str, int max_size)
|
||||
{
|
||||
if (str != NULL && max_size > 8)
|
||||
{
|
||||
if (handle != NULL)
|
||||
wxStrncpy(str, "SQLite 3", max_size);
|
||||
else
|
||||
wxStrncpy(str, "ODBC 2.0", max_size);
|
||||
}
|
||||
return handle != NULL;
|
||||
}
|
@ -19,6 +19,7 @@ typedef unsigned int UNIT_TYPE;
|
||||
typedef unsigned long ULONG;
|
||||
typedef unsigned long XVT_ERRMSG;
|
||||
typedef unsigned long XVT_ODBC;
|
||||
typedef unsigned long XVT_SQLDB;
|
||||
|
||||
typedef wchar_t XVT_WCHAR;
|
||||
typedef short MENU_TAG;
|
||||
|
Loading…
x
Reference in New Issue
Block a user