Files
l7/LSQResultSet.cpp

494 lines
14 KiB
C++
Raw Normal View History

2018-04-25 20:43:41 +02:00
/*
* File: LSQResultSet.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 1 juin 2014, 11:43
*/
#ifdef USE_LIBSQ
#include "LSQResultSet.h"
using namespace SQ;
LSQResultSet::LSQResultSet() : LResultSet()
{
}
LSQResultSet::LSQResultSet(LConnection* newConnection) : LResultSet(newConnection)
{
}
LSQResultSet::~LSQResultSet()
{
if (m_rs)
{
SQresult * srs = static_cast<SQresult*> (m_rs);
sqlite3_free_table(srs->m_data);
delete srs;
m_rs = NULL;
}
}
bool LSQResultSet::SetSQL(const wxString& newSql)
{
UpdateSQL(newSql);
if (RunSQL()) First();
return (m_rs != NULL);
}
bool LSQResultSet::RunSQL()
{
if (m_curSql == wxEmptyString)
{
return false;
}
wxASSERT_MSG(m_conn != NULL, _T("m_conn est NULL."));
if (m_rs != NULL)
{
SQresult * srs = static_cast<SQresult*> (m_rs);
sqlite3_free_table(srs->m_data);
wxDELETE(srs);
m_rs = NULL;
}
m_rs = m_conn->ExecuteSQL(m_curSql);
SQresult * srs = static_cast<SQresult*> (m_rs);
if (!RetrieveColNames(srs))
{ // ON EMPTY TABLES, EXECUTESQL RETURNS NO COLUMNS AS WELL
m_rs = NULL;
return false;
}
if (m_rs != NULL)
{
m_cursor = (HasData()) ? 0 : -1;
Display();
m_inserting = false;
if (!m_initialised)
{
m_initialised = true;
InformInitialised();
}
InformDataChanged();
2018-04-25 20:43:41 +02:00
return true;
}
else
{
m_cursor = -1;
Display();
m_inserting = false;
const LInformation inf(RSSQ000C, RSSQ000M);
InformLibMessage(inf);
return false;
}
}
bool LSQResultSet::HasData() const
{
if (m_rs == NULL) return false;
SQresult * srs = static_cast<SQresult*> (m_rs);
if (srs->m_nbRows == 0) return false;
return true;
}
bool LSQResultSet::Absolute(const unsigned int newRowIndex)
{
if (!m_readOnly)
{
if (m_inserting)
{
InformInserting();
return false;
}
if (IsDirty())
{
InformDirty();
return false;
}
}
if (!HasData())
{
m_cursor = -1;
Display();
return false;
}
if (newRowIndex > (GetRowCount() - 1)) return false;
InformBeforeChangeRow();
if (!m_canMove) return false;
m_cursor = newRowIndex;
Display();
m_inserting = false;
InformAfterChangeRow();
return true;
}
bool LSQResultSet::IsFirst() const
{
if (!(HasData())) return false;
return (m_cursor == 0);
}
bool LSQResultSet::IsLast() const
{
if (!(HasData())) return false;
SQresult * srs = static_cast<SQresult*> (m_rs);
return (m_cursor == (srs->m_nbRows - 1));
}
bool LSQResultSet::First()
{
if (IsFirst()) return false;
return Absolute(0);
}
bool LSQResultSet::Next()
{
if (IsLast()) return false;
return Absolute(m_cursor + 1);
}
bool LSQResultSet::Previous()
{
if (IsFirst()) return false;
return Absolute(m_cursor - 1);
}
bool LSQResultSet::Last()
{
if (IsLast()) return false;
SQresult * srs = static_cast<SQresult*> (m_rs);
return Absolute(srs->m_nbRows - 1);
}
const unsigned int LSQResultSet::GetRowCount() const
{
if (m_rs == NULL) return -1;
SQresult * srs = static_cast<SQresult*> (m_rs);
return srs->m_nbRows;
}
const unsigned int LSQResultSet::GetColumnCount() const
{
if (m_rs == NULL) return -1;
SQresult * srs = static_cast<SQresult*> (m_rs);
return srs->m_nbCols;
}
const wxString LSQResultSet::GetColumnName(const unsigned int colIndex) const
{
if (m_rs == NULL) return wxEmptyString;
if (!HasData()) return m_colNames.Item(colIndex);
SQresult * srs = static_cast<SQresult*> (m_rs);
const char* name = srs->m_data[colIndex];
if (name) return wxString(name);
return wxEmptyString;
}
const int LSQResultSet::GetColumnIndex(const wxString& colName) const
{
if (m_rs == NULL) return -1;
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 char* name = srs->m_data[c];
const wxString sName(name);
if (sName.Lower() == colName.Lower()) return c;
}
return -1;
}
const wxAny LSQResultSet::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];
if (data == NULL) return wxEmptyString;
return wxString(data, wxConvUTF8);
}
const wxAny LSQResultSet::GetData(const wxString &columnName) const
{
SQresult * srs = static_cast<SQresult*> (m_rs);
if (m_cursor < 0
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
|| IsInserting()
|| !HasData()
|| m_cursor > srs->m_nbRows - 1)
2018-04-25 20:43:41 +02:00
return wxEmptyString;
for (unsigned int c = 0; c < srs->m_nbCols; c++)
{
const char * colName = srs->m_data[c];
const wxString sColName(colName);
if (sColName.Lower() == columnName.Lower()) return GetData(m_cursor, c);
}
return wxEmptyString;
}
void LSQResultSet::Display()
{
for (unsigned int i = 0; i < m_BoundControls.GetCount(); i++)
{
LBoundControl * control = static_cast<LBoundControl*> (m_BoundControls.Item(i));
if (!control) continue;
if (control)
{
wxAny aData = GetData(control->GetColumnName());
control->SetData(aData);
}
}
}
bool LSQResultSet::Save()
{
wxASSERT_MSG(m_conn != NULL, _T("m_conn est NULL."));
if (m_readOnly || m_conn->IsReadOnly()) return false;
if (!m_inserting && !HasData()) return false;
InformBeforeSave();
if (!m_canSave) return false;
LBoundControl* pkControl = GetControl(m_pkColName);
if (!pkControl) return false;
if (pkControl->IsNull())
{
InformBeforeInsert();
if (!m_canInsert) return false;
}
else
{
InformBeforeUpdate();
if (!m_canUpdate) return false;
}
const wxString sql = MakeUpdateSQL();
if (sql.IsEmpty()) return false;
bool updating = (sql.Mid(0, 7) == _T("UPDATE "));
wxAny pkValue;
bool res;
if (updating) pkValue = GetData(m_pkColName);
if (m_conn->ExecuteUpdateSQL(sql))
{
const int oldCursor = m_cursor;
if (updating)
{
InformAfterUpdate(pkValue, sql);
res = RunSQL();
if (res)
{
m_cursor = GetRowByPKValue(pkValue);
Display();
if (oldCursor != m_cursor) InformAfterChangeRow();
}
}
else
{
wxAny newPK = m_conn->GetReturnedValue(0);
int inewPK;
newPK.GetAs<int> (&inewPK);
if (inewPK != 0)
{
InformAfterInsert(newPK, sql); // Before RunSQL so that UpdateSQL can be called by client app
res = RunSQL();
if (res)
{
m_cursor = GetRowByPKValue(newPK);
Display();
m_inserting = false;
if (oldCursor != m_cursor) InformAfterChangeRow();
}
m_conn->ClearReturnedKeys();
}
else
{
const LInformation inf(RSSQ002C, RSSQ002M);
InformLibMessage(inf);
}
}
InformAfterSave();
}
else
{
const LInformation inf(RSSQMC, wxString(m_conn->GetLastLibMessage()));
InformLibMessage(inf);
}
if (res) InformAfterAction(LResultSetEvent::ACTION_SAVE);
return res;
}
const wxString LSQResultSet::MakeUpdateSQL() const
{
if (m_readOnly || !m_canSave) return wxEmptyString;
if (!m_inserting && !HasData()) return wxEmptyString;
LBoundControl* pkControl = GetControl(m_pkColName);
if (pkControl == NULL) return wxEmptyString;
wxString sql = _T("UPDATE");
bool updating = true;
if (pkControl->IsNull())
{
sql = _T("INSERT INTO");
updating = false;
}
wxString columns = wxEmptyString;
wxString values = wxEmptyString;
wxString singleValue = wxEmptyString;
for (unsigned int i = 0; i < m_BoundControls.GetCount(); i++)
{
LBoundControl * control = static_cast<LBoundControl*> (m_BoundControls.Item(i));
if (!control) continue;
if (!control) continue;
wxString colName = control->GetColumnName();
if (updating)
{
if (colName.Lower() != m_pkColName.Lower())
{
if (control->IsDirty())
{
// Build "col = value, " string
singleValue = wxEmptyString;
control->GetData().GetAs<wxString> (&singleValue);
EscapeChar(singleValue);
const wxString quote = control->IsNull() ? wxString(wxEmptyString) : control->GetSQLQuote();
values += colName + _T(" = ") + quote
+ singleValue
+ quote + _T(", ");
}
}
}
else
{
if (colName.Lower() != m_pkColName.Lower())
{
if (control->IsDirty())
{
// Construct "col1, col2,..." string
columns += colName + _T(", ");
singleValue = wxEmptyString;
control->GetData().GetAs<wxString> (&singleValue);
EscapeChar(singleValue);
// Construct "value1, value2,..." string
const wxString quote = control->IsNull() ? wxString(wxEmptyString) : control->GetSQLQuote();
values += quote
+ singleValue
+ quote + _T(", ");
}
}
}
}
if (updating)
{
if (values.IsEmpty())
{
sql = wxEmptyString;
}
else
{
// Append "WHERE pkColName = value"
values = values.Mid(0, (values.Len() - 2));
singleValue = wxEmptyString;
pkControl->GetData().GetAs<wxString> (&singleValue);
values += _T(" WHERE ") + m_pkColName + _T(" = ")
+ pkControl->GetSQLQuote() + singleValue
+ pkControl->GetSQLQuote();
// Build final string
sql += _T(" ") + m_tblName + _T(" SET ") + values;
}
}
else
{
if (values.IsEmpty() || columns.IsEmpty())
{
sql = wxEmptyString;
}
else
{
// Manage master/child columns
wxString childColList = wxEmptyString;
wxString masterValueList = wxEmptyString;
if (IsChild())
{
childColList = MakeChildInsertColList();
masterValueList = MakeMasterValueList();
}
columns = childColList + columns;
columns = columns.Mid(0, (columns.Len() - 2));
columns = _T(" (") + columns + _T(") ");
values = masterValueList + values;
values = values.Mid(0, (values.Len() - 2));
values = _T(" (") + values + _T(") ");
// Final string
sql = sql + _T(" ") + m_tblName + columns + _T("VALUES") + values;
}
}
return sql;
}
const wxString LSQResultSet::MakeDeleteSQL() const
{
if (m_readOnly || !m_canDelete) return wxEmptyString;
if (!HasData()) return wxEmptyString;
if (m_inserting) return wxEmptyString;
LBoundControl* pkControl = GetControl(m_pkColName);
if (pkControl == NULL) return wxEmptyString;
wxString pkValue = wxEmptyString;
pkControl->GetData().GetAs<wxString>(&pkValue);
EscapeChar(pkValue); // ???
wxString sql = _T("DELETE FROM ") + m_tblName
+ _T(" WHERE ") + m_pkColName + _T(" = ")
+ pkControl->GetSQLQuote()
+ pkValue
+ pkControl->GetSQLQuote();
return sql;
}
const int LSQResultSet::GetRowByPKValue(const wxAny& pkValue) const
{
if (!HasData()) return -1;
int colIdx = GetColumnIndex(m_pkColName);
if (colIdx < 0) return -1;
for (unsigned int i = 0; i < GetRowCount(); i++)
{
wxAny beData = GetData(i, colIdx);
if (pkValue.As<wxString>() == beData.As<wxString>()) return i;
}
return -1;
}
bool LSQResultSet::RetrieveColNames(SQresult * emptyResult)
{
/*
* PRAGMA table_info(tbl1)
* gives table metadata
* but columns ordering in m_curSql is obviously app dependant
*
*/
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(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 * name = sqlite3_column_name(ppStmt, i);
m_colNames.Add(wxString(name));
}
emptyResult->m_nbCols = m_colNames.GetCount();
sqlite3_finalize(ppStmt);
return true;
}
else
{
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(" ") +
wxString(errMsg) + _T("\n") + wxString(errStr));
wxPrintf("%s\n", msg);
wxFAIL_MSG(msg);
}
}
return false;
}
#endif