完成属性信息读取逻辑

This commit is contained in:
duanshengchao 2025-04-01 16:45:30 +08:00
parent f5aa607369
commit e0f33eb2de
5 changed files with 70 additions and 25 deletions

View File

@ -58,7 +58,7 @@ public:
void lastPage(); void lastPage();
//数据操作 //数据操作
void setTable(const QString&); //void setTable(const QString&);
void refresh(); void refresh();
void insertRecord(int); void insertRecord(int);
void removeRecord(int); void removeRecord(int);

View File

@ -74,13 +74,23 @@ struct Model
struct Attribute struct Attribute
{ {
int id = -1; int id;
QString name; //中文展示名称(filed:attribute_name) QString name; //中文展示名称(filed:attribute_name)
QString type; //英文表示名称(filed:attribute),不可重名 QString type; //英文表示名称(filed:attribute),不可重名
int dataTypeID; int dataTypeID;
int dataLength = -1; //filed:length_precision int dataLength; //filed:length_precision
QString defaultValue; 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) Attribute(int id, QString name, QString type, int dataTypeID, int dataLength, QString defaultVaule)
:id(id), :id(id),
name(std::move(name)), name(std::move(name)),

View File

@ -33,7 +33,8 @@ public:
bool modelTypeExistsInDB(const QString&, const QString&); bool modelTypeExistsInDB(const QString&, const QString&);
bool removeModel(const QString&, int); 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<Attribute>); bool batchInserAttributis(const QString&, int, int, QList<Attribute>);
signals: signals:

View File

@ -14,6 +14,7 @@ AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttribu
getDataTypes(); getDataTypes();
iniDisplayField(); iniDisplayField();
refresh();
} }
AttributeTableModel::~AttributeTableModel() AttributeTableModel::~AttributeTableModel()
@ -219,34 +220,40 @@ void AttributeTableModel::loadPageData()
{ {
m_currentPageData.clear(); m_currentPageData.clear();
if (m_tableName.isEmpty()) /*if (m_tableName.isEmpty())
{ {
LOG_ERROR("DB", QString("Attribute table name is empty, load data failed")); LOG_ERROR("DB", QString("Attribute table name is empty, load data failed"));
return; return;
} }*/
beginResetModel(); beginResetModel();
QStringList columns; QString strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND attribute_group_id = %2 LIMIT %3 OFFSET %4")
for(int i = 0; i < m_displayField.count(); i++) .arg(m_modelAttributeGroup.modelID)
columns << m_displayField.at(i).originalName; .arg(m_modelAttributeGroup.groupID)
QString strSQL = QString("SELECT %1 FROM %2 LIMIT %3 OFFSET %4")
.arg(columns.join(", "))
.arg(m_tableName)
.arg(m_paginationInfo.entriesPerPage) .arg(m_paginationInfo.entriesPerPage)
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
try 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); QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
while(query.next()) while(query.next())
{ {
RowData data; Attribute attribute;
//data.record = query.record(); attribute.id = query.value(0).toInt();
for(int i = 0; i < m_displayField.count(); i++) 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) catch (const DatabaseException& e)
@ -319,13 +326,13 @@ void AttributeTableModel::lastPage()
void AttributeTableModel::updateTotalCount() void AttributeTableModel::updateTotalCount()
{ {
if (m_tableName.isEmpty()) /*if (m_tableName.isEmpty())
{ {
LOG_ERROR("SQL", QString::fromWCharArray(L"属性表名称为空,获取属性数量失败")); LOG_ERROR("SQL", QString::fromWCharArray(L"属性表名称为空,获取属性数量失败"));
return; 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::RowData> AttributeTableModel::filterRowsByEditState(EditState state) QList<AttributeTableModel::RowData> AttributeTableModel::filterRowsByEditState(EditState state)
@ -339,7 +346,7 @@ QList<AttributeTableModel::RowData> AttributeTableModel::filterRowsByEditState(E
return result; return result;
} }
void AttributeTableModel::setTable(const QString& tableName) /*void AttributeTableModel::setTable(const QString& tableName)
{ {
if(m_tableName == tableName) if(m_tableName == tableName)
return; return;
@ -347,7 +354,7 @@ void AttributeTableModel::setTable(const QString& tableName)
m_tableName = tableName; m_tableName = tableName;
m_modifiedRows.clear(); m_modifiedRows.clear();
refresh(); refresh();
} }*/
void AttributeTableModel::refresh() void AttributeTableModel::refresh()
{ {

View File

@ -65,10 +65,10 @@ QSqlQuery SqlQueryExecutor::executeSQL(const QString& connectionName, const QStr
} }
catch (const DatabaseException& e) catch (const DatabaseException& e)
{ {
LOG_INFO("DB", QString("DB Rollback"));
// 错误处理:回滚事务(如果已开启) // 错误处理:回滚事务(如果已开启)
if(transactionStarted) if(transactionStarted)
{ {
LOG_INFO("DB", QString("DB Rollback"));
if(!db.rollback()) // 回滚失败时记录警告 if(!db.rollback()) // 回滚失败时记录警告
{ {
LOG_ERROR("DB", QString("Rollback failed. connectionName: %1. error: %2").arg(connectionName, db.lastError().databaseText())); 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; return name;
} }
int SqlQueryExecutor::getAttributeCount(const QString& connectionName, const QString& tableName) int SqlQueryExecutor::getAttributeCount(const QString& connectionName, int modelID, int groupID)
{ {
int count = 0; 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 try
{ {
QSqlQuery query = executeSQL(connectionName, strSQL); QSqlQuery query = executeSQL(connectionName, strSQL);
@ -416,11 +416,38 @@ int SqlQueryExecutor::getAttributeCount(const QString& connectionName, const QSt
catch (const DatabaseException& e) catch (const DatabaseException& e)
{ {
LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性数量失败")); LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性数量失败"));
return 0;
} }
return count; 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<Attribute> attributes) bool SqlQueryExecutor::batchInserAttributis(const QString& connectionName, int modelID, int attributeGroupID, QList<Attribute> attributes)
{ {
//属于批量操作,需要开启事务 //属于批量操作,需要开启事务