Resolve "Improve GENERATED support"
This commit is contained in:
parent
54e39ccdb3
commit
9277731c4e
13 changed files with 749 additions and 246 deletions
|
|
@ -169,7 +169,7 @@ QVariant CrudModel::data(const QModelIndex &index, int role) const
|
|||
if (index.column() < PreColumnCount)
|
||||
{
|
||||
if (role == Qt::DisplayRole || role == CustomSortRole)
|
||||
return index.row();
|
||||
return index.row() + 1;
|
||||
else if (role == CustomDataTypeRole)
|
||||
return Pgsql::int4_oid;
|
||||
|
||||
|
|
@ -231,6 +231,7 @@ void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
|||
beginResetModel();
|
||||
m_pendingRowList.clear();
|
||||
m_roData = data;
|
||||
initializeColumnList();
|
||||
lastRowKey = data->rows() - 1;
|
||||
initRowMapping();
|
||||
appendNewRow();
|
||||
|
|
@ -276,7 +277,7 @@ Qt::ItemFlags CrudModel::flags(const QModelIndex &index) const
|
|||
if (index.column() < PreColumnCount)
|
||||
return flags;
|
||||
|
||||
if (m_primaryKey)
|
||||
if (m_primaryKey && !columnIsReadOnly(index.column()))
|
||||
flags |= Qt::ItemIsEditable;
|
||||
|
||||
return flags;
|
||||
|
|
@ -419,21 +420,16 @@ std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow
|
|||
QTextStream q(&buffer);
|
||||
q << "INSERT INTO " << table_name << "(";
|
||||
|
||||
auto columns = m_database->catalog()->attributes()->getColumnsForRelation(m_table->oid());
|
||||
bool first = true;
|
||||
for (const auto& e : data)
|
||||
{
|
||||
int num = e.first + 1;
|
||||
auto find_res = std::find_if(columns.begin(), columns.end(),
|
||||
[num] (const auto &elem) -> bool { return num == elem.num; });
|
||||
if (find_res != columns.end())
|
||||
{
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
q << ",";
|
||||
q << find_res->name;
|
||||
}
|
||||
int num = e.first;
|
||||
auto&& column = columnList[num];
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
q << ",";
|
||||
q << column.name;
|
||||
}
|
||||
q << ") VALUES ($1";
|
||||
for (size_t p = 2; p <= data.size(); ++p)
|
||||
|
|
@ -515,6 +511,30 @@ bool CrudModel::IsLastRow(RowMappingVector::iterator mapping_iter) const
|
|||
return mapping_iter == --m_rowMapping.end();
|
||||
}
|
||||
|
||||
bool CrudModel::columnIsReadOnly(int column_index) const
|
||||
{
|
||||
if (m_roData == nullptr)
|
||||
return true;
|
||||
|
||||
auto&& column = columnList[column_index - PreColumnCount];
|
||||
return column.getIdentity() == PgAttribute::Identity::Always
|
||||
|| column.getGenerated() != PgAttribute::Generated::None;
|
||||
}
|
||||
|
||||
void CrudModel::initializeColumnList()
|
||||
{
|
||||
columnList.clear();
|
||||
columnList.reserve(m_roData->cols());
|
||||
auto columns = m_database->catalog()->attributes()->getColumnsForRelation(m_table->oid());
|
||||
for (int col = 0; col < m_roData->cols(); ++col)
|
||||
{
|
||||
int attnum = m_roData->ftableCol(col);
|
||||
auto find_result = std::find_if(columns.begin(), columns.end(), [attnum](const PgAttribute &att) { return att.num == attnum; });
|
||||
assert(find_result != columns.end());
|
||||
columnList.push_back(*find_result);
|
||||
}
|
||||
}
|
||||
|
||||
bool CrudModel::submit()
|
||||
{
|
||||
return savePendingChanges();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue