完成属性数据更新逻辑
This commit is contained in:
parent
730ad02320
commit
25249b97b6
|
|
@ -37,6 +37,7 @@ public:
|
|||
bool getAtrributeInfo(const QString&, const QString&, Attribute&);
|
||||
bool batchInsertAttributes(const QString&, int, int, QList<Attribute>);
|
||||
bool batchDeleteAttributes(const QString&, int, int, QList<int>);
|
||||
bool batchUpdateAttributes(const QString&, int, int, QList<Attribute>);
|
||||
|
||||
signals:
|
||||
void errorOccurred(const QString& error);
|
||||
|
|
|
|||
|
|
@ -113,6 +113,7 @@ bool AttributeTableModel::setData(const QModelIndex &index, const QVariant &valu
|
|||
m_currentPageData[row] = modifiedRow;
|
||||
|
||||
emit dataChanged(index, index, {role, Qt::UserRole + 100});
|
||||
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -432,8 +433,10 @@ void AttributeTableModel::submitChanges()
|
|||
bool insertCompleted = false;
|
||||
QList<RowData> deleteRows = filterRowsByEditState(Deleted);
|
||||
bool deleteCompleted = false;
|
||||
// QList<RowData> updateRows = filterRowsByEditState(Modified);
|
||||
QList<RowData> updateRows = filterRowsByEditState(Modified);
|
||||
bool updateCompleted = false;
|
||||
|
||||
//插入操作
|
||||
if(!insertRows.isEmpty())
|
||||
{
|
||||
QList<Attribute> insertAttributes;
|
||||
|
|
@ -451,11 +454,12 @@ void AttributeTableModel::submitChanges()
|
|||
QString defaultValue = "null";
|
||||
if(rowData.values.value(4).isValid())
|
||||
defaultValue = rowData.values.value(4).toString();
|
||||
|
||||
insertAttributes.emplace_back(-1, name, type, dataTypeID, dataLength, defaultValue);
|
||||
}
|
||||
insertCompleted = SqlQueryExecutor::instance().batchInsertAttributes(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID, insertAttributes);
|
||||
}
|
||||
|
||||
//删除操作
|
||||
if(!deleteRows.isEmpty())
|
||||
{
|
||||
QList<int> deleteAttributes;
|
||||
|
|
@ -468,8 +472,34 @@ void AttributeTableModel::submitChanges()
|
|||
}
|
||||
deleteCompleted = SqlQueryExecutor::instance().batchDeleteAttributes(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID, deleteAttributes);
|
||||
}
|
||||
//更新操作
|
||||
if(!updateRows.isEmpty())
|
||||
{
|
||||
QList<Attribute> updateAttributes;
|
||||
for(const RowData& rowData : updateRows)
|
||||
{
|
||||
if(rowData.values.count() != m_displayField.count() + 1) //loadPageData时,会把属性id放到valuse的最后,并且不做展示
|
||||
continue;
|
||||
|
||||
if( (!insertRows.isEmpty() && !insertCompleted) && (!deleteRows.isEmpty() && !deleteCompleted) )
|
||||
int id = rowData.values.last().toInt();
|
||||
QString type = rowData.values.value(0).toString();
|
||||
QString name = rowData.values.value(1).toString();
|
||||
int dataTypeID = rowData.values.value(2).toInt();
|
||||
int dataLength = -1;
|
||||
if(rowData.values.value(3).isValid())
|
||||
dataLength = rowData.values.value(3).toInt();
|
||||
QString defaultValue = "null";
|
||||
if(rowData.values.value(4).isValid())
|
||||
defaultValue = rowData.values.value(4).toString();
|
||||
|
||||
updateAttributes.emplace_back(id, name, type, dataTypeID, dataLength, defaultValue);
|
||||
}
|
||||
updateCompleted = SqlQueryExecutor::instance().batchUpdateAttributes(m_connection, m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID, updateAttributes);
|
||||
}
|
||||
|
||||
if( (!insertRows.isEmpty() && !insertCompleted)
|
||||
&& (!deleteRows.isEmpty() && !deleteCompleted)
|
||||
&& (!updateRows.isEmpty() && !updateCompleted) )
|
||||
{
|
||||
emit showMessage(type_warning, QString::fromWCharArray(L"错误"), QString::fromWCharArray(L"数据提交失败,详情可查看日志文件"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -466,18 +466,18 @@ bool SqlQueryExecutor::batchInsertAttributes(const QString& connectionName, int
|
|||
|
||||
//先插入进属性表attribute,因为要获取插入后的自增id,所以采用逐条插入的方法
|
||||
QList<qint64> attributeIDList;
|
||||
for(const Attribute& atrribute : attributes)
|
||||
for(const Attribute& attribute : attributes)
|
||||
{
|
||||
qint64 attributeID = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
//先向model_type中插入一条记录
|
||||
QString strSQL = "INSERT INTO basic.attribute (attribute, attribute_name, data_type_id, length_precision, default_value) VALUES "
|
||||
"(:type, :name, :dataType, :dataLength, :defaultValue)";
|
||||
QVariantHash params;
|
||||
params.insert(":type", atrribute.type);
|
||||
params.insert(":name", atrribute.name);
|
||||
params.insert(":dataType", atrribute.dataTypeID);
|
||||
params.insert(":dataLength", atrribute.dataLength);
|
||||
params.insert(":defaultValue", atrribute.defaultValue);
|
||||
params.insert(":type", attribute.type);
|
||||
params.insert(":name", attribute.name);
|
||||
params.insert(":dataType", attribute.dataTypeID);
|
||||
params.insert(":dataLength", attribute.dataLength);
|
||||
params.insert(":defaultValue", attribute.defaultValue);
|
||||
try
|
||||
{
|
||||
QSqlQuery query = executeSQL(connectionName, strSQL, params);
|
||||
|
|
@ -592,3 +592,66 @@ bool SqlQueryExecutor::batchDeleteAttributes(const QString& connectionName, int
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SqlQueryExecutor::batchUpdateAttributes(const QString& connectionName, int modelID, int attributeGroupID, QList<Attribute> attributes)
|
||||
{
|
||||
//属于批量操作,需要开启事务
|
||||
QSqlDatabase db = QSqlDatabase::database(connectionName);
|
||||
if(!db.isOpen())
|
||||
{
|
||||
LOG_ERROR("DB", QString("Database not open. connectionName: %1").arg(connectionName));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!db.transaction())
|
||||
{
|
||||
LOG_ERROR("DB", QString("Start transaction failed. connectionName: %1. error: %2").arg(connectionName, db.lastError().databaseText()));
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
QString strSQL = "UPDATE basic.attribute SET attribute = ?, attribute_name = ?, data_type_id = ?, length_precision = ?, default_value = ? WHERE id = ?";
|
||||
if(!query.prepare(strSQL))
|
||||
{
|
||||
LOG_ERROR("SQL", QString("SQL '%1' prepare fialed. error: %2").arg(strSQL, query.lastError().databaseText()));
|
||||
if(!db.rollback()) // 回滚
|
||||
{
|
||||
LOG_ERROR("DB", QString("Rollback failed. connectionName: %1. error: %2").arg(connectionName, db.lastError().databaseText()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
QVariantList types, names, dataTypes, lengths, defaults, ids;
|
||||
for(const Attribute& attribute : attributes)
|
||||
{
|
||||
types << attribute.type;
|
||||
names << attribute.name;
|
||||
dataTypes << attribute.dataTypeID;
|
||||
lengths << attribute.dataLength;
|
||||
defaults << attribute.defaultValue;
|
||||
ids << attribute.id;
|
||||
}
|
||||
query.addBindValue(types);
|
||||
query.addBindValue(names);
|
||||
query.addBindValue(dataTypes);
|
||||
query.addBindValue(lengths);
|
||||
query.addBindValue(defaults);
|
||||
query.addBindValue(ids);
|
||||
if( !query.execBatch() )
|
||||
{
|
||||
LOG_ERROR("SQL", QString("SQL '%1' execBatch error: %2").arg(strSQL, query.lastError().databaseText()));
|
||||
LOG_INFO("DB", QString("DB Rollback"));
|
||||
if(!db.rollback()) // 回滚
|
||||
{
|
||||
LOG_ERROR("DB", QString("Rollback failed. connectionName: %1. error: %2").arg(connectionName, db.lastError().databaseText()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!db.commit()) // 提交
|
||||
{
|
||||
LOG_ERROR("DB", QString("Commit transaction failed. connectionName: %1. error: %2").arg(connectionName, db.lastError().databaseText()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue