dbgrid.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 735 行 · 第 1/2 页

CPP
735
字号
            (sqltype == SQL_C_DOUBLE))
        {
            return true;
        }
        return false;
    }
    return false;
}

bool wxDbGridTableBase::CanSetValueAs(int WXUNUSED(row), int col, const wxString& typeName)
{
    if (typeName == wxGRID_VALUE_STRING)
    {
        //FIXME ummm What about blob field etc.
        return true;
    }

    if (!(m_data->GetColDefs()[(m_ColInfo[col].DbCol)].Updateable))
    {
        return false;
    }

    if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol)
    {
        //If a virtual column then we can't find it's type. we have to faulse to
        //get using wxVairent.
        return false;
    }

    int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype;
    if (typeName == wxGRID_VALUE_DATETIME)
    {
        if ((sqltype == SQL_C_DATE) ||
            (sqltype == SQL_C_TIME) ||
            (sqltype == SQL_C_TIMESTAMP))
        {
            return true;
        }
        return false;
    }
    if (typeName == wxGRID_VALUE_NUMBER)
    {
        if ((sqltype == SQL_C_SSHORT) ||
            (sqltype == SQL_C_USHORT) ||
            (sqltype == SQL_C_SLONG)  ||
            (sqltype == SQL_C_ULONG))
        {
            return true;
        }
        return false;
    }
    if (typeName == wxGRID_VALUE_FLOAT)
    {
        if ((sqltype == SQL_C_SSHORT) ||
            (sqltype == SQL_C_USHORT) ||
            (sqltype == SQL_C_SLONG)  ||
            (sqltype == SQL_C_ULONG)  ||
            (sqltype == SQL_C_FLOAT)  ||
            (sqltype == SQL_C_DOUBLE))
        {
            return true;
        }
        return false;
    }
    return false;
}

long wxDbGridTableBase::GetValueAsLong(int row, int col)
{
    ValidateRow(row);

    if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol)
    {
        wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns"));
        return 0;
    }
    int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype;
    if ((sqltype == SQL_C_SSHORT) ||
        (sqltype == SQL_C_USHORT) ||
        (sqltype == SQL_C_SLONG) ||
        (sqltype == SQL_C_ULONG))
    {
        wxVariant val = m_data->GetColumn(m_ColInfo[col].DbCol);
        return val.GetLong();
    }
    wxFAIL_MSG (_T("unknown column, "));
    return 0;
}

double wxDbGridTableBase::GetValueAsDouble(int row, int col)
{
    wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row,col);
    ValidateRow(row);

    if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol)
    {
        wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns"));
        return 0.0;
    }
    int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype;
    if ((sqltype == SQL_C_SSHORT) ||
        (sqltype == SQL_C_USHORT) ||
        (sqltype == SQL_C_SLONG) ||
        (sqltype == SQL_C_ULONG) ||
        (sqltype == SQL_C_FLOAT) ||
        (sqltype == SQL_C_DOUBLE))
    {
        wxVariant val = m_data->GetColumn(m_ColInfo[col].DbCol);
        return val.GetDouble();
    }
    wxFAIL_MSG (_T("unknown column"));
    return 0.0;
}

bool wxDbGridTableBase::GetValueAsBool(int row, int col)
{
    wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row,col);
    ValidateRow(row);

    if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol)
    {
        wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns"));
        return 0;
    }
    int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype;
    if ((sqltype == SQL_C_SSHORT) ||
        (sqltype == SQL_C_USHORT) ||
        (sqltype == SQL_C_SLONG) ||
        (sqltype == SQL_C_ULONG))
    {
        wxVariant val = m_data->GetColumn(m_ColInfo[col].DbCol);
        return val.GetBool();
    }
    wxFAIL_MSG (_T("unknown column, "));
    return 0;
}

void* wxDbGridTableBase::GetValueAsCustom(int row, int col, const wxString& typeName)
{
    wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row,col);
    ValidateRow(row);

    if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol)
    {
        wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns"));
        return NULL;
    }
    if (m_data->IsColNull((UWORD)m_ColInfo[col].DbCol))
        return NULL;

    if (typeName == wxGRID_VALUE_DATETIME)
    {
        wxDbColDef *pColDefs = m_data->GetColDefs();
        int sqltype = pColDefs[(m_ColInfo[col].DbCol)].SqlCtype;

        if ((sqltype == SQL_C_DATE) ||
            (sqltype == SQL_C_TIME) ||
            (sqltype == SQL_C_TIMESTAMP))
        {
            wxVariant val = m_data->GetColumn(m_ColInfo[col].DbCol);
            return new wxDateTime(val.GetDateTime());
        }
    }
    wxFAIL_MSG (_T("unknown column data type "));
    return NULL;
}


void wxDbGridTableBase::SetValueAsCustom(int row, int col, const wxString& typeName, void* value)
{
    wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row,col);
    ValidateRow(row);

    if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol)
    {
        wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns"));
        return;
    }

    if (typeName == wxGRID_VALUE_DATETIME)
    {
        int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype;
        if ((sqltype == SQL_C_DATE) ||
            (sqltype == SQL_C_TIME) ||
            (sqltype == SQL_C_TIMESTAMP))
        {
            //FIXME: you can't dynamic_cast from (void *)
            //wxDateTime *date = wxDynamicCast(value, wxDateTime);
            wxDateTime *date = (wxDateTime *)value;
            if (!date)
            {
                wxFAIL_MSG (_T("Failed to convert data"));
                return;
            }
            wxVariant val(date);
            m_rowmodified = true;
            m_data->SetColumn(m_ColInfo[col].DbCol,val);
        }
    }
    wxFAIL_MSG (_T("unknown column data type"));
    return ;
}


wxString wxDbGridTableBase::GetColLabelValue(int col)
{
    if (GetNumberCols() > col)
    {
        return m_ColInfo[col].Title;
    }
    wxFAIL_MSG (_T("unknown column"));
    return wxString();
}

bool wxDbGridTableBase::IsEmptyCell(int row, int col)
{
    wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row,col);

    ValidateRow(row);
    return m_data->IsColNull((UWORD)m_ColInfo[col].DbCol);
}


wxString wxDbGridTableBase::GetValue(int row, int col)
{
    wxLogDebug(wxT("GetValue() on %i,%i"),row,col);

    ValidateRow(row);
    wxVariant val = m_data->GetColumn(m_ColInfo[col].DbCol);
    wxLogDebug(wxT("\tReturning \"%s\"\n"),val.GetString().c_str());

    return val.GetString();
}


void wxDbGridTableBase::SetValue(int row, int col,const wxString& value)
{
    wxLogDebug(wxT("SetValue() on %i,%i"),row,col);

    ValidateRow(row);
    wxVariant val(value);

    m_rowmodified = true;
    m_data->SetColumn(m_ColInfo[col].DbCol,val);
}


void wxDbGridTableBase::SetValueAsLong(int row, int col, long value)
{
    wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row,col);

    ValidateRow(row);
    wxVariant val(value);

    m_rowmodified = true;
    m_data->SetColumn(m_ColInfo[col].DbCol,val);
}


void wxDbGridTableBase::SetValueAsDouble(int row, int col, double value)
{
    wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row,col);

    ValidateRow(row);
    wxVariant val(value);

    m_rowmodified = true;
    m_data->SetColumn(m_ColInfo[col].DbCol,val);

}


void wxDbGridTableBase::SetValueAsBool(int row, int col, bool value)
{
    wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row,col);

    ValidateRow(row);
    wxVariant val(value);

    m_rowmodified = true;
    m_data->SetColumn(m_ColInfo[col].DbCol,val);
}


void wxDbGridTableBase::ValidateRow(int row)
{
    wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row,m_row,m_keys.GetCount());

    if (row == m_row)
         return;
    Writeback();

    //We add to row as Count is unsigned!
    if ((unsigned)(row+1) > m_keys.GetCount())
    {
        wxLogDebug(wxT("\trow key unknown"));
        // Extend Array, iterate through data filling with keys
        m_data->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY);
        int trow;
        for (trow = m_keys.GetCount(); trow <= row; trow++)
        {
            wxLogDebug(wxT("Fetching row %i.."), trow);
            bool ret = m_data->GetNext();

            wxLogDebug(wxT(" ...success=(%i)"),ret);
            GenericKey k = m_data->GetKey();
            m_keys.Add(k);
        }
        m_row = row;
    }
    else
    {
        wxLogDebug(wxT("\trow key known centering data"));
        GenericKey k = m_keys.Item(row);
        m_data->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL);
        m_data->ClearMemberVars();
        m_data->SetKey(k);
        if (!m_data->QueryOnKeyFields())
        {
            wxDbLogExtendedErrorMsg(_T("ODBC error during Query()\n\n"), m_data->GetDb(),__TFILE__,__LINE__);
        }

        m_data->GetNext();

        m_row = row;
    }
    m_rowmodified = false;
}

bool wxDbGridTableBase::Writeback() const
{
    if (!m_rowmodified)
    {
        return true;
    }

    bool result=true;
    wxLogDebug(wxT("\trow key unknown"));

// FIXME: this code requires dbtable support for record status
#if 0
    switch (m_data->get_ModifiedStatus())
    {
        case wxDbTable::UpdatePending:
            result = m_data->Update();
           break;
        case wxDbTable::InsertPending:
            result = (m_data->Insert() == SQL_SUCCESS);
        break;
        default:
            //Nothing
        break;
    }
#else
    wxLogDebug(wxT("WARNING : Row writeback not implemented "));
#endif
    return result;
}

#include "wx/arrimpl.cpp"

WX_DEFINE_EXPORTED_OBJARRAY(keyarray);

#endif  // #if wxUSE_GRID
#endif  // #if wxUSE_ODBC

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?