bool Ilwis::Postgresql::PostgresqlDatabaseUtil::exists(Ilwis::SPFeatureI feature) const
{
    PostgresqlDatabaseUtil pgUtil(_resource,_options);
    SqlStatementHelper sqlHelper(pgUtil);

    QString where;
    QList<QString> primaryKeys;
    getPrimaryKeys(primaryKeys);
    Record record = feature->record();
    foreach (QString primaryKey, primaryKeys) {
        if (where.isEmpty()) {
            where.append(" WHERE ");
        } else {
            where.append(" AND ");
        }
        ColumnDefinition coldef = feature->attributedefinition(primaryKey);
        QVariant value = record.cell(coldef.columnindex());

        where.append(primaryKey).append(" = ");
        where.append(sqlHelper.createInsertValueString(value, coldef));
    }

    QString sql = "SELECT ";
    //sql.append(" count(").append(primaryKeys.at(0)).append(") ");
    sql.append(QStringList(primaryKeys).join(","));
    sql.append(" FROM ").append(qTableFromTableResource());
    sql.append(where);

    QSqlQuery query = doQuery(sql, "exists");
    return query.next();
}
void AttributeDefinition::columndefinition(const ColumnDefinition &coldef)
{
    if ( coldef.id() >=  _columnDefinitionsByIndex.size())     {
        addColumn({coldef.name(), coldef.datadef().domain<>()});
    } else {
        auto iter1 = _columnDefinitionsByName.find(coldef.name());
        if ( iter1 != _columnDefinitionsByName.end()) {
            ColumnDefinition cdef = coldef;
            cdef.columnindex((*iter1).second);
            _columnDefinitionsByIndex[cdef.columnindex()] = cdef;
            (*iter1).second = cdef.columnindex();

        }else {
            addColumn(coldef);
        }
    }
}
bool AttributeDefinition::addColumn(const ColumnDefinition& def){

    if ( _columnDefinitionsByName.find(def.name()) != _columnDefinitionsByName.end()) { // no duplicates
        return false;
    }
    ColumnDefinition coldef = def;
    coldef.columnindex(_columnDefinitionsByIndex.size());
    _columnDefinitionsByName[coldef.name()] = _columnDefinitionsByIndex.size();
    _columnDefinitionsByIndex.push_back(coldef);

    return true;
}