diff --git a/include/attributeTableModel.h b/include/attributeTableModel.h index 672ff7c..7103598 100644 --- a/include/attributeTableModel.h +++ b/include/attributeTableModel.h @@ -58,7 +58,7 @@ public: void lastPage(); //数据操作 - void setTable(const QString&); + //void setTable(const QString&); void refresh(); void insertRecord(int); void removeRecord(int); diff --git a/include/global.h b/include/global.h index 956b79a..ca15509 100644 --- a/include/global.h +++ b/include/global.h @@ -74,13 +74,23 @@ struct Model struct Attribute { - int id = -1; + int id; QString name; //中文展示名称(filed:attribute_name) QString type; //英文表示名称(filed:attribute),不可重名 int dataTypeID; - int dataLength = -1; //filed:length_precision + int dataLength; //filed:length_precision QString defaultValue; + Attribute() + { + id = -1; + name = ""; + type = ""; + dataTypeID = -1; + dataLength = -1; + defaultValue = ""; + } + Attribute(int id, QString name, QString type, int dataTypeID, int dataLength, QString defaultVaule) :id(id), name(std::move(name)), diff --git a/include/sqlQueryExecutor.h b/include/sqlQueryExecutor.h index 373562f..d8488e9 100644 --- a/include/sqlQueryExecutor.h +++ b/include/sqlQueryExecutor.h @@ -33,7 +33,8 @@ public: bool modelTypeExistsInDB(const QString&, const QString&); bool removeModel(const QString&, int); //属性相关 - int getAttributeCount(const QString&, const QString&); + int getAttributeCount(const QString&, int, int); + bool getAtrributeInfo(const QString&, const QString&, Attribute&); bool batchInserAttributis(const QString&, int, int, QList); signals: diff --git a/source/attributeTableModel.cpp b/source/attributeTableModel.cpp index dcddfec..2b5644a 100644 --- a/source/attributeTableModel.cpp +++ b/source/attributeTableModel.cpp @@ -14,6 +14,7 @@ AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttribu getDataTypes(); iniDisplayField(); + refresh(); } AttributeTableModel::~AttributeTableModel() @@ -219,34 +220,40 @@ void AttributeTableModel::loadPageData() { m_currentPageData.clear(); - if (m_tableName.isEmpty()) + /*if (m_tableName.isEmpty()) { LOG_ERROR("DB", QString("Attribute table name is empty, load data failed")); return; - } + }*/ beginResetModel(); - QStringList columns; - for(int i = 0; i < m_displayField.count(); i++) - columns << m_displayField.at(i).originalName; - QString strSQL = QString("SELECT %1 FROM %2 LIMIT %3 OFFSET %4") - .arg(columns.join(", ")) - .arg(m_tableName) + QString strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND attribute_group_id = %2 LIMIT %3 OFFSET %4") + .arg(m_modelAttributeGroup.modelID) + .arg(m_modelAttributeGroup.groupID) .arg(m_paginationInfo.entriesPerPage) .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); try { + QStringList columns; + for(int i = 0; i < m_displayField.count(); i++) + columns << m_displayField.at(i).originalName; + QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL); while(query.next()) { - RowData data; - //data.record = query.record(); - for(int i = 0; i < m_displayField.count(); i++) + Attribute attribute; + attribute.id = query.value(0).toInt(); + if(SqlQueryExecutor::instance().getAtrributeInfo(m_connection, columns.join(", "), attribute)) { - data.values.append(query.value(i)); + RowData data; + data.values.append(attribute.type); + data.values.append(attribute.name); + data.values.append(attribute.dataTypeID); + data.values.append(attribute.dataLength); + data.values.append(attribute.defaultValue); + m_currentPageData.append(data); } - m_currentPageData.append(data); } } catch (const DatabaseException& e) @@ -319,13 +326,13 @@ void AttributeTableModel::lastPage() void AttributeTableModel::updateTotalCount() { - if (m_tableName.isEmpty()) + /*if (m_tableName.isEmpty()) { LOG_ERROR("SQL", QString::fromWCharArray(L"属性表名称为空,获取属性数量失败")); return; - } + }*/ - m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_tableName); + m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID); } QList AttributeTableModel::filterRowsByEditState(EditState state) @@ -339,7 +346,7 @@ QList AttributeTableModel::filterRowsByEditState(E return result; } -void AttributeTableModel::setTable(const QString& tableName) +/*void AttributeTableModel::setTable(const QString& tableName) { if(m_tableName == tableName) return; @@ -347,7 +354,7 @@ void AttributeTableModel::setTable(const QString& tableName) m_tableName = tableName; m_modifiedRows.clear(); refresh(); -} +}*/ void AttributeTableModel::refresh() { diff --git a/source/sqlQueryExecutor.cpp b/source/sqlQueryExecutor.cpp index b57ffed..d7e599d 100644 --- a/source/sqlQueryExecutor.cpp +++ b/source/sqlQueryExecutor.cpp @@ -65,10 +65,10 @@ QSqlQuery SqlQueryExecutor::executeSQL(const QString& connectionName, const QStr } catch (const DatabaseException& e) { - LOG_INFO("DB", QString("DB Rollback")); // 错误处理:回滚事务(如果已开启) if(transactionStarted) { + LOG_INFO("DB", QString("DB Rollback")); if(!db.rollback()) // 回滚失败时记录警告 { LOG_ERROR("DB", QString("Rollback failed. connectionName: %1. error: %2").arg(connectionName, db.lastError().databaseText())); @@ -403,10 +403,10 @@ const QString SqlQueryExecutor::getArributeGropuName(const QString& connectionNa return name; } -int SqlQueryExecutor::getAttributeCount(const QString& connectionName, const QString& tableName) +int SqlQueryExecutor::getAttributeCount(const QString& connectionName, int modelID, int groupID) { int count = 0; - QString strSQL = QString("SELECT COUNT(*) FROM %1").arg(tableName); + QString strSQL = QString("SELECT COUNT(*) FROM basic.model_attribute WHERE model_type_id = %1 AND attribute_group_id = %2").arg(modelID).arg(groupID); try { QSqlQuery query = executeSQL(connectionName, strSQL); @@ -416,11 +416,38 @@ int SqlQueryExecutor::getAttributeCount(const QString& connectionName, const QSt catch (const DatabaseException& e) { LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性数量失败")); + return 0; } return count; } +bool SqlQueryExecutor::getAtrributeInfo(const QString& connectionName, const QString& columns, Attribute& attribute) +{ + QString strSQL = QString("SELECT %1 FROM basic.attribute WHERE id = %2").arg(columns).arg(attribute.id); + try + { + QSqlQuery query = executeSQL(connectionName, strSQL); + if(query.next()) + { + attribute.type = query.value(0).toString(); + attribute.name = query.value(1).toString(); + attribute.dataTypeID = query.value(2).toInt(); + attribute.dataLength = query.value(3).toInt(); + attribute.defaultValue = query.value(4).toString(); + } + else + return false; + } + catch (const DatabaseException& e) + { + LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性数量失败")); + return false; + } + + return true; +} + bool SqlQueryExecutor::batchInserAttributis(const QString& connectionName, int modelID, int attributeGroupID, QList attributes) { //属于批量操作,需要开启事务