统一数据展示和数据存储顺序一致,完善测试发现bug

This commit is contained in:
duanshengchao 2025-05-07 17:34:27 +08:00
parent 95ae695919
commit 122ac57e3d
8 changed files with 83 additions and 22 deletions

View File

@ -35,6 +35,14 @@ public:
//创建 QFlags 包装类型
Q_DECLARE_FLAGS(EditState, EditStateFlag)
struct RowData
{
//QSqlRecord record;
QVector<QVariant> values;
QHash<int, bool> cellModified; //记录单元格是否被修改
EditState state = EditState(Clean);
};
struct DataType //数据类型
{
int id;
@ -78,10 +86,12 @@ public:
void refresh();
void forceRefresh(); //强制刷新(不会出现询问提示,数据同步时会用到)
void insertRecord(int);
void addRecords(QList<RowData>);
void removeRecord();
bool removeRecord(int);
void submitChanges(); //提交更改(增、删、改)
void cancleChanges(); //取消修改
QList<RowData> getSelectedRowData();
//展示列控制
//void setVisibleColumns(const QStringList& columns);
@ -104,14 +114,6 @@ private:
QString dataType = "unknown type"; //数据类型
};
struct RowData
{
//QSqlRecord record;
QVector<QVariant> values;
QHash<int, bool> cellModified; //记录单元格是否被修改
EditState state = EditState(Clean);
};
void iniDisplayField();
void getDataTypesFromDB();
void loadPageData(); // 加载当前页数据

View File

@ -65,6 +65,7 @@ private slots:
void onSIG_connectionStatusChanged(const QString& strConnectionName, bool bConnected);
void onSIG_addModel(Model&);
void onSIG_addGroups(int, QVector<int>);
//void onSIG_addAttributes();
void onSIG_openAttributeInfo(const QString&, ModelAttributeGroup&);
void onSIG_closeAttributeInfo(ModelAttributeGroup&);
void onSIG_closeAllAttributeInfo();

View File

@ -42,7 +42,7 @@ public:
int getAttributeCount(const QString&, int, int);
int getAllAttributeCount(const QString&);
bool getAttributeInfo(const QString&, const QString&, Attribute&);
int attributeTypeExistsInDB(const QString&, const QString&);
int getAttributeID(const QString&, const QString&);
bool attributeTypeUseByModelGroup(const QString&, int, int, int);
bool batchInsertAttributes(const QString&, int, int, QList<Attribute>);
bool batchDeleteAttributes(const QString&, int, int, QList<int>);

View File

@ -187,7 +187,7 @@ QWidget* AttributeTableDelegate::createEditor(QWidget *parent, const QStyleOptio
textEditor->setRegularExpression("[A-Za-z0-9_]");
connect(textEditor, &TextEditWidget::confirm, this, [=]{
/*QString strText = textEditor->editText();
int id = SqlQueryExecutor::instance().attributeTypeExistsInDB(m_connection, strText);
int id = SqlQueryExecutor::instance().getAttributeID(m_connection, strText);
if(id != -1)
textEditor->setErrorInfo(QString::fromWCharArray(L"该类型已存在"));
else
@ -313,7 +313,7 @@ void AttributeTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *m
if(attributeTableModel && textEditor)
{
QString strText = textEditor->editText();
int id = SqlQueryExecutor::instance().attributeTypeExistsInDB(m_connection, strText);
int id = SqlQueryExecutor::instance().getAttributeID(m_connection, strText);
//1.是否以已在同组中(要同时从内存和数据库中都进行查找,避免遗漏已创建但未提交的数据)
bool existsInCurrentGropu = attributeTableModel->attributeTypeExistsInCurrentGroup(id, strText);
if(existsInCurrentGropu)

View File

@ -355,7 +355,7 @@ void AttributeTableModel::loadPageData()
if(m_modelAttributeGroup.modelID == -1) //表示当前加载数据对象是所有属性
{
columns.append("id"); //将id放到最后和按照具体模型、具体属性组读取数据方式时逻辑统一具体见下面分支逻辑
QString strSQL = QString("SELECT %1 FROM basic.attribute LIMIT %2 OFFSET %3")
QString strSQL = QString("SELECT %1 FROM basic.attribute ORDER BY id ASC LIMIT %2 OFFSET %3")
.arg(columns.join(", "))
.arg(m_paginationInfo.entriesPerPage)
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
@ -384,12 +384,12 @@ void AttributeTableModel::loadPageData()
QString strSQL = "";
bool isPublicGroup = SqlQueryExecutor::instance().isPublicAttributeGroup(m_connection, m_modelAttributeGroup.groupID);
if(isPublicGroup)
strSQL = QString("SELECT attribute_id FROM basic.model_attribute_public WHERE attribute_group_id = %1 LIMIT %2 OFFSET %3")
strSQL = QString("SELECT attribute_id FROM basic.model_attribute_public WHERE attribute_group_id = %1 ORDER BY id ASC LIMIT %2 OFFSET %3")
.arg(m_modelAttributeGroup.groupID)
.arg(m_paginationInfo.entriesPerPage)
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
//利用短路机制当attribute_group_id!=-1时括号整体为false需要进行内部条件判断=-1时整体为true忽略整体也就忽略了attribute_group_id的条件判断
else strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1) LIMIT %3 OFFSET %4")
else strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1) ORDER BY id ASC LIMIT %3 OFFSET %4")
.arg(m_modelAttributeGroup.modelID)
.arg(m_modelAttributeGroup.groupID)
.arg(m_paginationInfo.entriesPerPage)
@ -568,6 +568,40 @@ void AttributeTableModel::insertRecord(int row)
endInsertRows();
}
void AttributeTableModel::addRecords(QList<RowData> records)
{
QList<RowData> validRecords;
//首先过滤掉已经存在记录
for(const RowData& record : records)
{
int id = record.values.last().toInt(); //目前该函数的数据源都来‘属性选择器’(都是已创建好的属性)
QString type = record.values.value(0).toString();
bool existed = attributeTypeExistsInCurrentGroup(id, type);
if(!existed)
validRecords.append(record);
}
if(validRecords.count() == 0)
return;
beginInsertRows(QModelIndex(), rowCount(), rowCount() + records.count() - 1);
int row = m_currentPageData.count();
for(const RowData& record : records)
{
RowData newRow;
newRow.state = New;
newRow.values = record.values;
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
m_modifiedRows[globalRow] = newRow;
m_currentPageData.insert(row, newRow);
row++;
}
endInsertRows();
}
void AttributeTableModel::removeRecord()
{
//以第一列也就是编号列是否选中为判断依据,找出所有被选中的行
@ -712,6 +746,18 @@ void AttributeTableModel::cancleChanges()
emit syncDataStatus(dataHasbeenModified(), m_paginationInfo);
}
QList<AttributeTableModel::RowData> AttributeTableModel::getSelectedRowData()
{
QList<RowData> result;
for(int row = 0; row < rowCount(); row++)
{
QModelIndex numberIndex = createIndex(row, 0);
if(m_selectionModel && m_selectionModel->isSelected(numberIndex))
result.append(m_currentPageData.at(row));
}
return result;
}
void AttributeTableModel::triggerSyncSignal()
{
emit syncDataStatus(dataHasbeenModified(), m_paginationInfo);

View File

@ -244,7 +244,7 @@ void DBStructureModel::addDataGroup(const QString& connection, int modelID, QVec
QModelIndex connIndex = index(connNode->row(), 0, QModelIndex());
QModelIndex modelIndex = index(modelNode->row(), 0, connIndex);
beginInsertRows(modelIndex, modelNode->childCount(), groups.count());
beginInsertRows(modelIndex, modelNode->childCount(), modelNode->childCount() + groups.count() - 1);
for(int groupID : groups)
{

View File

@ -337,7 +337,7 @@ void MainWindow::onSIG_openAttributeSelector()
m_pAttributeSelector = new AttributeSelector(getCurConnection(), this);
m_pAttributeSelector->setMainWindow(this);
m_pAttributeSelector->installEventFilter(this);
connect(m_pAttributeSelector, &AttributeSelector::finished, this, [=]{ MaskManager::instance()->hideMask(m_pAttributeSelector);});
connect(m_pAttributeSelector, &AttributeSelector::finished, this, [=]{ MaskManager::instance()->hideMask(m_pAttributeSelector); });
}
int nX = (this->width() - m_pAttributeSelector->width()) * 0.5;

View File

@ -233,7 +233,7 @@ int SqlQueryExecutor::getModelCount(const QString& connectionName)
QVector<int> SqlQueryExecutor::getModelGroups(const QString& connectionName, int modelID)
{
QVector<int> groups;
QString strSQL = "SELECT attribute_group_id FROM basic.model_group WHERE model_type_id = " + QString::number(modelID);
QString strSQL = "SELECT attribute_group_id FROM basic.model_group WHERE model_type_id = " + QString::number(modelID) + " ORDER BY id ASC";
try
{
QSqlQuery query = executeSQL(connectionName, strSQL);
@ -642,7 +642,7 @@ bool SqlQueryExecutor::getAttributeInfo(const QString& connectionName, const QSt
return true;
}
int SqlQueryExecutor::attributeTypeExistsInDB(const QString& connectionName, const QString& type)
int SqlQueryExecutor::getAttributeID(const QString& connectionName, const QString& type)
{
int id = -1;
QString strSQL = "SELECT id FROM basic.attribute WHERE attribute = \'" + type + "\'";
@ -661,11 +661,23 @@ int SqlQueryExecutor::attributeTypeExistsInDB(const QString& connectionName, con
bool SqlQueryExecutor::attributeTypeUseByModelGroup(const QString& connectionName, int attributeID, int modelID, int groupID)
{
QString strSQL = "SELECT id FROM basic.model_attribute WHERE model_type_id = :modelID AND attribute_group_id = :groupID AND attribute_id = :attributeID";
QString strSQL;
QVariantHash params;
params.insert(":modelID", modelID);
params.insert(":groupID", groupID);
params.insert(":attributeID", attributeID);
bool isPublicGroup = isPublicAttributeGroup(connectionName, groupID);
if(isPublicGroup)
{
strSQL = "SELECT id FROM basic.model_attribute_public WHERE attribute_group_id = :groupID AND attribute_id = :attributeID";
params.insert(":groupID", groupID);
params.insert(":attributeID", attributeID);
}
else
{
strSQL = "SELECT id FROM basic.model_attribute WHERE model_type_id = :modelID AND attribute_group_id = :groupID AND attribute_id = :attributeID";
params.insert(":modelID", modelID);
params.insert(":groupID", groupID);
params.insert(":attributeID", attributeID);
}
try
{
QSqlQuery query = executeSQL(connectionName, strSQL, params);