Files
l7/LLightSQResultSet.cpp

220 lines
5.7 KiB
C++
Raw Permalink Normal View History

2018-04-25 20:43:41 +02:00
/*
* File: LLightSQResultSet.cpp
2022-11-19 16:05:44 +01:00
* Author: Saleem Edah-Tally - nmset@yandex.com
2018-04-25 20:43:41 +02:00
* License : LGPL version 2.1
2022-11-19 16:05:44 +01:00
* Copyright Saleem Edah-Tally, M. D. - © 2014
2018-04-25 20:43:41 +02:00
*
* Created on 31 mai 2014, 18:34
*/
#ifdef USE_LIBSQ
#include "LLightSQResultSet.h"
#include "LSQConnection.h"
using namespace SQ;
LLightSQResultSet::LLightSQResultSet() : LLightResultSet()
{
}
LLightSQResultSet::LLightSQResultSet(LConnection * newConnection) : LLightResultSet(newConnection)
{
}
LLightSQResultSet::~LLightSQResultSet()
{
if (m_rs)
{
SQresult * srs = static_cast<SQresult*> (m_rs);
sqlite3_free_table(srs->m_data);
Added classes to manage simple tabular data of known structure using a popup containing a wxGrid object. BasePicker : Abstract class adding a popup to wxPickerBase styled with a wxTextCtrl. BaseGridPicker : Abstract class adding a wxGrid in the popup. JsonGridPickerCtrl : UI control storing the tabular data in a JSON array. LBoundJsonGridPicker : connect JsonGridPicker to database. LGridJsonCellEditor : use LBoundJsonGridPicker in other wxGrid objects. LGridJsonCellRenderer : render cell JSON data. XmlGridPickerCtrl : UI control storing the tabular data in as XML document. LBoundXmlGridPicker : connect XmlGridPicker to database. LGridXmlCellEditor : grid editor for LBoundXmlGridPicker. LGridXmlCellRenderer : grid renderer for LBoundXmlGridPicker. JsonHelper and XmlHelper : for applications to quickly get intent value from database data.. The structure of managed tabular data : Column 1 : Intent - this is what we want to store/edit. This can be telephone numbers, email addresses, instant messaging addresses... any single line piece of information that can exist many times for one entity (person, company...). Column 2 : Type - A short description of the intent : 'Home, Work, Mobile, Fax, Other...'. It is displayed in a non editable wxComboBox. Column 3 : Preferred - One line of data can be selected as the preferred one. It is not mandatory, but it must be a single choice. Column 4 : Notes - single line notes. Adjust sql scripts and L7.dox. Applied ANSI formatting style to all files. Other changes : Work around a nasty misbehavior. Grid columns edited by a translated combobox expect full string data as cell values. LResultSet::BEData() will report these mapped strings, instead of database real data. LBoundComboBox::IsDirty() will always be true even if the editor is unchanged once created. Simplest workaround : disconnect m_BoundComboBox if unchanged. LGridTextEditor::ProvideFormEditor() : set the form editor's value explicitely.; wxTextCtrl does not interpret data it receives LBoundControl::SetNull must be void. LBoundGrid : Unbind:: instructions should limit to the widget's id, like Bind:: LConnection::GetReturnedKeys should return NULL. LConnection::SetData should return void. Notes : wxJSON must be configured with the same prefix as wxWidgets, here /usr/local/{wxWidgets,wxWidgets-Release}.
2019-12-22 16:35:16 +01:00
delete srs;
m_rs = NULL;
2018-04-25 20:43:41 +02:00
}
}
bool LLightSQResultSet::SetSQL(const wxString& newSql)
{
UpdateSQL(newSql);
if (RunSQL()) First();
return (m_rs != NULL);
}
bool LLightSQResultSet::RunSQL()
{
if (m_curSql.IsEmpty()) return false;
wxASSERT_MSG(m_conn != NULL, _T("m_conn est NULL."));
if (m_rs)
{
SQresult * srs = static_cast<SQresult*> (m_rs);
sqlite3_free_table(srs->m_data);
Added classes to manage simple tabular data of known structure using a popup containing a wxGrid object. BasePicker : Abstract class adding a popup to wxPickerBase styled with a wxTextCtrl. BaseGridPicker : Abstract class adding a wxGrid in the popup. JsonGridPickerCtrl : UI control storing the tabular data in a JSON array. LBoundJsonGridPicker : connect JsonGridPicker to database. LGridJsonCellEditor : use LBoundJsonGridPicker in other wxGrid objects. LGridJsonCellRenderer : render cell JSON data. XmlGridPickerCtrl : UI control storing the tabular data in as XML document. LBoundXmlGridPicker : connect XmlGridPicker to database. LGridXmlCellEditor : grid editor for LBoundXmlGridPicker. LGridXmlCellRenderer : grid renderer for LBoundXmlGridPicker. JsonHelper and XmlHelper : for applications to quickly get intent value from database data.. The structure of managed tabular data : Column 1 : Intent - this is what we want to store/edit. This can be telephone numbers, email addresses, instant messaging addresses... any single line piece of information that can exist many times for one entity (person, company...). Column 2 : Type - A short description of the intent : 'Home, Work, Mobile, Fax, Other...'. It is displayed in a non editable wxComboBox. Column 3 : Preferred - One line of data can be selected as the preferred one. It is not mandatory, but it must be a single choice. Column 4 : Notes - single line notes. Adjust sql scripts and L7.dox. Applied ANSI formatting style to all files. Other changes : Work around a nasty misbehavior. Grid columns edited by a translated combobox expect full string data as cell values. LResultSet::BEData() will report these mapped strings, instead of database real data. LBoundComboBox::IsDirty() will always be true even if the editor is unchanged once created. Simplest workaround : disconnect m_BoundComboBox if unchanged. LGridTextEditor::ProvideFormEditor() : set the form editor's value explicitely.; wxTextCtrl does not interpret data it receives LBoundControl::SetNull must be void. LBoundGrid : Unbind:: instructions should limit to the widget's id, like Bind:: LConnection::GetReturnedKeys should return NULL. LConnection::SetData should return void. Notes : wxJSON must be configured with the same prefix as wxWidgets, here /usr/local/{wxWidgets,wxWidgets-Release}.
2019-12-22 16:35:16 +01:00
wxDELETE(srs);
;
2018-04-25 20:43:41 +02:00
m_rs = NULL;
}
m_rs = m_conn->ExecuteSQL(m_curSql);
SQresult * srs = static_cast<SQresult*> (m_rs);
Added classes to manage simple tabular data of known structure using a popup containing a wxGrid object. BasePicker : Abstract class adding a popup to wxPickerBase styled with a wxTextCtrl. BaseGridPicker : Abstract class adding a wxGrid in the popup. JsonGridPickerCtrl : UI control storing the tabular data in a JSON array. LBoundJsonGridPicker : connect JsonGridPicker to database. LGridJsonCellEditor : use LBoundJsonGridPicker in other wxGrid objects. LGridJsonCellRenderer : render cell JSON data. XmlGridPickerCtrl : UI control storing the tabular data in as XML document. LBoundXmlGridPicker : connect XmlGridPicker to database. LGridXmlCellEditor : grid editor for LBoundXmlGridPicker. LGridXmlCellRenderer : grid renderer for LBoundXmlGridPicker. JsonHelper and XmlHelper : for applications to quickly get intent value from database data.. The structure of managed tabular data : Column 1 : Intent - this is what we want to store/edit. This can be telephone numbers, email addresses, instant messaging addresses... any single line piece of information that can exist many times for one entity (person, company...). Column 2 : Type - A short description of the intent : 'Home, Work, Mobile, Fax, Other...'. It is displayed in a non editable wxComboBox. Column 3 : Preferred - One line of data can be selected as the preferred one. It is not mandatory, but it must be a single choice. Column 4 : Notes - single line notes. Adjust sql scripts and L7.dox. Applied ANSI formatting style to all files. Other changes : Work around a nasty misbehavior. Grid columns edited by a translated combobox expect full string data as cell values. LResultSet::BEData() will report these mapped strings, instead of database real data. LBoundComboBox::IsDirty() will always be true even if the editor is unchanged once created. Simplest workaround : disconnect m_BoundComboBox if unchanged. LGridTextEditor::ProvideFormEditor() : set the form editor's value explicitely.; wxTextCtrl does not interpret data it receives LBoundControl::SetNull must be void. LBoundGrid : Unbind:: instructions should limit to the widget's id, like Bind:: LConnection::GetReturnedKeys should return NULL. LConnection::SetData should return void. Notes : wxJSON must be configured with the same prefix as wxWidgets, here /usr/local/{wxWidgets,wxWidgets-Release}.
2019-12-22 16:35:16 +01:00
if (!RetrieveColNames(srs))
{ // No columns returned if no data !
2018-04-25 20:43:41 +02:00
m_rs = NULL;
return false;
}
if (m_rs)
{
m_cursor = (HasData()) ? 0 : -1;
if (!m_initialised) m_initialised = true;
return true;
}
else
{
m_cursor = -1;
return false;
}
}
bool LLightSQResultSet::HasData() const
{
wxASSERT_MSG(m_rs != NULL, _T("m_rs est NULL."));
SQresult * srs = static_cast<SQresult*> (m_rs);
return (srs->m_nbRows > 0);
}
bool LLightSQResultSet::Absolute(const unsigned int newRowIndex)
{
if (!HasData())
{
m_cursor = -1;
return false;
}
wxASSERT_MSG(newRowIndex < GetRowCount(), _T("Paramètre newRowIndex invalide."));
m_cursor = newRowIndex;
return true;
}
bool LLightSQResultSet::IsFirst() const
{
if (!(HasData())) return false;
return (m_cursor == 0);
}
bool LLightSQResultSet::IsLast() const
{
if (!(HasData())) return false;
SQresult * srs = static_cast<SQresult*> (m_rs);
return (m_cursor == (srs->m_nbRows - 1));
}
bool LLightSQResultSet::First()
{
if (IsFirst()) return false;
return Absolute(0);
}
bool LLightSQResultSet::Next()
{
if (IsLast()) return false;
return Absolute(m_cursor + 1);
}
bool LLightSQResultSet::Previous()
{
if (IsFirst()) return false;
return Absolute(m_cursor - 1);
}
bool LLightSQResultSet::Last()
{
if (IsLast()) return false;
SQresult * srs = static_cast<SQresult*> (m_rs);
return Absolute(srs->m_nbRows - 1);
}
const unsigned int LLightSQResultSet::GetRowCount() const
{
wxASSERT_MSG(m_rs != NULL, _T("m_rs est NULL."));
SQresult * srs = static_cast<SQresult*> (m_rs);
return srs->m_nbRows;
}
const unsigned int LLightSQResultSet::GetColumnCount() const
{
wxASSERT_MSG(m_rs != NULL, _T("m_rs est NULL."));
SQresult * srs = static_cast<SQresult*> (m_rs);
return srs->m_nbCols;
}
const wxString LLightSQResultSet::GetColumnName(const unsigned int colIndex) const
{
wxASSERT_MSG(m_rs != NULL, _T("m_rs est NULL."));
if (!HasData()) return m_colNames.Item(colIndex);
SQresult * srs = static_cast<SQresult*> (m_rs);
return wxString(srs->m_data[colIndex]);
}
const int LLightSQResultSet::GetColumnIndex(const wxString& colName) const
{
wxASSERT_MSG(m_rs != NULL, _T("m_rs est NULL."));
if (!HasData()) return m_colNames.Index(colName, false);
SQresult * srs = static_cast<SQresult*> (m_rs);
for (unsigned int c = 0; c < srs->m_nbCols; c++)
{
const wxString sName(srs->m_data[c]);
if (sName.Lower() == colName.Lower()) return c;
}
return -1;
}
const wxAny LLightSQResultSet::GetData(const unsigned int rowIdx, const unsigned int colIdx) const
{
if (!HasData()) return wxEmptyString;
SQresult * srs = static_cast<SQresult*> (m_rs);
const char * data = srs->m_data[((rowIdx + 1) * (srs->m_nbCols)) + colIdx];
return wxString(data, wxConvUTF8);
}
const wxAny LLightSQResultSet::GetData(const wxString &columnName) const
{
if (m_cursor < 0) return wxEmptyString;
if (!HasData()) return wxEmptyString;
SQresult * srs = static_cast<SQresult*> (m_rs);
for (int c = 0; c < srs->m_nbCols; c++)
{
const wxString sColName(srs->m_data[c]);
if (sColName.Lower() == columnName.Lower()) return GetData(m_cursor, c);
}
return wxEmptyString;
}
bool LLightSQResultSet::RetrieveColNames(SQresult * emptyResult)
{
wxASSERT_MSG(m_conn != NULL, _T("m_conn est NULL."));
sqlite3 * db = static_cast<sqlite3*> (m_conn->Get());
if (db)
{
sqlite3_stmt * ppStmt;
int res = sqlite3_prepare_v2(db, m_curSql, -1, &ppStmt, NULL);
if (res == SQLITE_OK)
{
m_colNames.Clear();
int colCount = sqlite3_column_count(ppStmt);
for (unsigned int i = 0; i < colCount; i++)
{
const char * constName = sqlite3_column_name(ppStmt, i);
m_colNames.Add(wxString(constName));
}
emptyResult->m_nbCols = m_colNames.GetCount();
sqlite3_finalize(ppStmt);
return true;
Added classes to manage simple tabular data of known structure using a popup containing a wxGrid object. BasePicker : Abstract class adding a popup to wxPickerBase styled with a wxTextCtrl. BaseGridPicker : Abstract class adding a wxGrid in the popup. JsonGridPickerCtrl : UI control storing the tabular data in a JSON array. LBoundJsonGridPicker : connect JsonGridPicker to database. LGridJsonCellEditor : use LBoundJsonGridPicker in other wxGrid objects. LGridJsonCellRenderer : render cell JSON data. XmlGridPickerCtrl : UI control storing the tabular data in as XML document. LBoundXmlGridPicker : connect XmlGridPicker to database. LGridXmlCellEditor : grid editor for LBoundXmlGridPicker. LGridXmlCellRenderer : grid renderer for LBoundXmlGridPicker. JsonHelper and XmlHelper : for applications to quickly get intent value from database data.. The structure of managed tabular data : Column 1 : Intent - this is what we want to store/edit. This can be telephone numbers, email addresses, instant messaging addresses... any single line piece of information that can exist many times for one entity (person, company...). Column 2 : Type - A short description of the intent : 'Home, Work, Mobile, Fax, Other...'. It is displayed in a non editable wxComboBox. Column 3 : Preferred - One line of data can be selected as the preferred one. It is not mandatory, but it must be a single choice. Column 4 : Notes - single line notes. Adjust sql scripts and L7.dox. Applied ANSI formatting style to all files. Other changes : Work around a nasty misbehavior. Grid columns edited by a translated combobox expect full string data as cell values. LResultSet::BEData() will report these mapped strings, instead of database real data. LBoundComboBox::IsDirty() will always be true even if the editor is unchanged once created. Simplest workaround : disconnect m_BoundComboBox if unchanged. LGridTextEditor::ProvideFormEditor() : set the form editor's value explicitely.; wxTextCtrl does not interpret data it receives LBoundControl::SetNull must be void. LBoundGrid : Unbind:: instructions should limit to the widget's id, like Bind:: LConnection::GetReturnedKeys should return NULL. LConnection::SetData should return void. Notes : wxJSON must be configured with the same prefix as wxWidgets, here /usr/local/{wxWidgets,wxWidgets-Release}.
2019-12-22 16:35:16 +01:00
}
else
{
2018-04-25 20:43:41 +02:00
int errCode = sqlite3_errcode(db);
const char * errMsg = sqlite3_errmsg(db);
const char * errStr = sqlite3_errstr(errCode);
const wxString msg(wxAny(errCode).As<wxString>() + _T(" ") +
Added classes to manage simple tabular data of known structure using a popup containing a wxGrid object. BasePicker : Abstract class adding a popup to wxPickerBase styled with a wxTextCtrl. BaseGridPicker : Abstract class adding a wxGrid in the popup. JsonGridPickerCtrl : UI control storing the tabular data in a JSON array. LBoundJsonGridPicker : connect JsonGridPicker to database. LGridJsonCellEditor : use LBoundJsonGridPicker in other wxGrid objects. LGridJsonCellRenderer : render cell JSON data. XmlGridPickerCtrl : UI control storing the tabular data in as XML document. LBoundXmlGridPicker : connect XmlGridPicker to database. LGridXmlCellEditor : grid editor for LBoundXmlGridPicker. LGridXmlCellRenderer : grid renderer for LBoundXmlGridPicker. JsonHelper and XmlHelper : for applications to quickly get intent value from database data.. The structure of managed tabular data : Column 1 : Intent - this is what we want to store/edit. This can be telephone numbers, email addresses, instant messaging addresses... any single line piece of information that can exist many times for one entity (person, company...). Column 2 : Type - A short description of the intent : 'Home, Work, Mobile, Fax, Other...'. It is displayed in a non editable wxComboBox. Column 3 : Preferred - One line of data can be selected as the preferred one. It is not mandatory, but it must be a single choice. Column 4 : Notes - single line notes. Adjust sql scripts and L7.dox. Applied ANSI formatting style to all files. Other changes : Work around a nasty misbehavior. Grid columns edited by a translated combobox expect full string data as cell values. LResultSet::BEData() will report these mapped strings, instead of database real data. LBoundComboBox::IsDirty() will always be true even if the editor is unchanged once created. Simplest workaround : disconnect m_BoundComboBox if unchanged. LGridTextEditor::ProvideFormEditor() : set the form editor's value explicitely.; wxTextCtrl does not interpret data it receives LBoundControl::SetNull must be void. LBoundGrid : Unbind:: instructions should limit to the widget's id, like Bind:: LConnection::GetReturnedKeys should return NULL. LConnection::SetData should return void. Notes : wxJSON must be configured with the same prefix as wxWidgets, here /usr/local/{wxWidgets,wxWidgets-Release}.
2019-12-22 16:35:16 +01:00
wxString(errMsg) + _T("\n") + wxString(errStr));
2018-04-25 20:43:41 +02:00
wxPrintf("%s\n", msg);
wxFAIL_MSG(msg);
}
}
return false;
}
#endif