Basic support for update in Crud implementation, error reporting and transfering data to modified set is still missing.

This commit is contained in:
eelke 2018-02-05 21:42:54 +01:00
parent 3fb32f1200
commit d626c19e14
15 changed files with 530 additions and 83 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ build/*
DIST/ DIST/
*.autosave *.autosave
srcdoc/

View file

@ -7,17 +7,19 @@
#include "GlobalIoService.h" #include "GlobalIoService.h"
#include "SqlFormattingUtils.h" #include "SqlFormattingUtils.h"
#include "WorkManager.h" #include "WorkManager.h"
#include "Pgsql_oids.h"
#include <QtConcurrent> #include <QtConcurrent>
#include <QFuture> #include <QFuture>
#include <QFutureWatcher> #include <QFutureWatcher>
#include "Pgsql_oids.h" #include "Pgsql_oids.h"
#include "Pgsql_Params.h"
#include <string> #include <string>
CrudModel::CrudModel(ASyncWindow *async_window) CrudModel::CrudModel(ASyncWindow *async_window)
: m_asyncWindow(async_window) : m_asyncWindow(async_window)
, m_dbConn(*getGlobalAsioIoService()) , m_dbConn(*getGlobalAsioIoService())
{ {
qDebug("CrudModel created");
connect(&m_dbConn, &ASyncDBConnection::onStateChanged, this, &CrudModel::connectionStateChanged); connect(&m_dbConn, &ASyncDBConnection::onStateChanged, this, &CrudModel::connectionStateChanged);
} }
@ -26,7 +28,6 @@ CrudModel::~CrudModel()
m_dbConn.closeConnection(); m_dbConn.closeConnection();
} }
/* /*
* Strategy * Strategy
* when ordered by primary key, offset and limit work very quickly so we can get away with not loading * when ordered by primary key, offset and limit work very quickly so we can get away with not loading
@ -61,13 +62,13 @@ QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int rol
// Basic functionality: // Basic functionality:
int CrudModel::rowCount(const QModelIndex &parent) const int CrudModel::rowCount(const QModelIndex &/*parent*/) const
{ {
return m_roData ? m_roData->rows() : 0; return m_roData ? m_roData->rows() : 0;
} }
int CrudModel::columnCount(const QModelIndex &parent) const int CrudModel::columnCount(const QModelIndex &/*parent*/) const
{ {
return m_roData ? m_roData->cols() : 0; return m_roData ? m_roData->cols() : 0;
} }
@ -77,40 +78,82 @@ Oid CrudModel::getType(int column) const
return m_roData ? m_roData->type(column) : InvalidOid; return m_roData ? m_roData->type(column) : InvalidOid;
} }
QVariant CrudModel::getData(const QModelIndex &index) const CrudModel::Value CrudModel::getData(const QModelIndex &index) const
{ {
QVariant r; Value value;
if (m_roData) { if (m_roData) {
int rij = index.row(); int rij = index.row();
int col = index.column(); int col = index.column();
if (m_roData->null(col, rij)) {
r = "null"; //Oid o = m_roData->type(col);
} // First see if we have buffered editted values that still need saving
else { boost::optional<Value> val = m_pendingRowList.getValue(col, rij);
Oid o = m_roData->type(col); if (!val) {
auto value = m_roData->get(col, rij); // No pending save have a look if we have modified saved data in the modified list
switch (o) { auto find_res = m_modifiedRowList.find(rij);
case Pgsql::bool_oid: if (find_res != m_modifiedRowList.end()) {
r = bool(value); //s = (s == "t") ? "TRUE" : "FALSE"; val = find_res->second.data()[col];
break;
default: {
QString s = value;
if (s.length() > 256) {
s.truncate(256);
}
r = s;
}
} }
} }
//Value value;
// If we did not have pending or modified data
if (!val) {
// Then we are going to read the original data.
if (!m_roData->null(col, rij)) {
value = std::string(m_roData->val(col, rij));
}
}
else {
value = *val;
}
} }
return r; return value;
} }
QVariant CrudModel::data(const QModelIndex &index, int role) const QVariant CrudModel::data(const QModelIndex &index, int role) const
{ {
QVariant v; QVariant v;
if (role == Qt::EditRole || role == Qt::DisplayRole) { if (role == Qt::EditRole) {
v = getData(index); // int row = index.row();
// if (m_pendingChanges.needsToSave(row)) {
//// if (savePendingChanges())
//// startEditingRow();
// }
auto value = getData(index);
if (value) {
// Oid o = m_roData->type(index.column());
// if (o == Pgsql::bool_oid) {
// v = *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
// }
// else {
QString s = QString::fromUtf8(value->c_str());
v = s;
// }
}
}
else if (role == Qt::DisplayRole) {
auto value = getData(index);
if (value) {
Oid o = m_roData->type(index.column());
if (o == Pgsql::bool_oid) {
if (value)
v = *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
else
v = "null";
}
else {
QString s = QString::fromUtf8(value->c_str());
if (s.length() > 256) {
s.truncate(256);
}
v = s;
}
}
else {
v = "null";
}
} }
else if (role == Qt::UserRole) { else if (role == Qt::UserRole) {
v = getType(index.column()); v = getType(index.column());
@ -176,3 +219,163 @@ Qt::ItemFlags CrudModel::flags(const QModelIndex &) const
{ {
return Qt::ItemIsSelectable + Qt::ItemIsEditable + Qt::ItemIsEnabled; return Qt::ItemIsSelectable + Qt::ItemIsEditable + Qt::ItemIsEnabled;
} }
bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
int row = index.row();
int col = index.column();
//m_pendingRowList.getRow(row);
Value val;
// Oid o = m_roData->type(index.column());
// if (o == Pgsql::bool_oid) {
// val = value.Bool ? "t" : "f";
// }
// else {
std::string s = value.toString().toUtf8().data();
if (!s.empty()) {
if (s == "''")
s.clear();
val = s;
}
// }
m_pendingRowList.setValue(col, row, val);
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
const CrudModel::ModifiedRow* CrudModel::getModifiedRow(int row) const
{
auto iter = m_modifiedRowList.find(row);
if (iter == m_modifiedRowList.end())
return nullptr;
else
return &iter->second;
}
CrudModel::PKeyValues CrudModel::getPKeyForRow(int row) const
{
PKeyValues values;
values.reserve(m_primaryKey->fkey.size());
auto mod_row = getModifiedRow(row);
if (mod_row){
for (auto attnum : m_primaryKey->key) {
int col = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
values.push_back(*(mod_row->data()[col]));
}
}
else {
for (auto attnum : m_primaryKey->key) {
int col = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
values.push_back(m_roData->get(col, row).c_str());
}
}
return values;
}
QString CrudModel::columnName(int col) const
{
return m_roData->getColName(col);
}
void CrudModel::updateRow(const PendingRow &pending_row)
{
auto pkey_values = getPKeyForRow(pending_row.row());
QString buffer;
QTextStream q(&buffer);
q << "UPDATE ";
q << genFQTableName(*m_database->catalogue(), m_table);
q << " AS d\n SET ";
auto data = pending_row.data();
Pgsql::Params params;
if (!data.empty()) {
int param = 0;
for (auto e : data) {
if (param > 0)
q << ",";
q << quoteIdent(columnName(e.first))
<< "=$" << ++param;
if (e.second)
params.add(e.second->c_str(), getType(e.first));
else
params.add(nullptr, getType(e.first));
}
q << "\nWHERE ";
int i = 0;
for (auto attnum : m_primaryKey->key) {
int col = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
params.add(pkey_values[i].c_str(), getType(col));
if (i > 0)
q << " AND ";
q << quoteIdent(columnName(col)) << "=$" << ++param;
++i;
}
q << "\nRETURNING *";
q.flush();
m_dbConn.send(buffer.toUtf8().data(), params,
[this] (Expected<std::shared_ptr<Pgsql::Result>> result, qint64) {
} );
}
}
bool CrudModel::savePendingChanges()
{
// need to start async store of changes
// modified results are only known after updates/inserts have returned the new values
// (new values can be modified by triggers and such things)
// do we leave panding changes in the list until async task complete? But then we do not know which
// ones we are storing because user can add new ones.
// We could also clear the pending list and keep a copy in the async task but then display would revert back
// to original values until save has completed.
// Moving them to modified list doesn't seem a good idea either because we would have to undo the changes
// when the save fails.
// PendingRowList copy = m_pendingRowList;
//
for (const auto& row : m_pendingRowList) {
// int row_number = row.first;
// if (row_number >= 0) {
//// update
// }
updateRow(row.second);
}
m_pendingRowList.clear();
return true;
}
bool CrudModel::submit()
{
return savePendingChanges();
}
void CrudModel::revert()
{
}

View file

@ -8,6 +8,7 @@
#include "Pgsql_Connection.h" #include "Pgsql_Connection.h"
#include <map> #include <map>
#include <memory> #include <memory>
#include <tuple>
#include <vector> #include <vector>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@ -25,13 +26,27 @@ class ASyncWindow;
* - hide columns (will not be retrieved can greatly speed up things when you disable wide columns) * - hide columns (will not be retrieved can greatly speed up things when you disable wide columns)
* - can use foreign keys to display dropdown * - can use foreign keys to display dropdown
* *
* How to load data? *
* - 2D array of QVariants won't be efficient * Need to keep track of changes from the original. Things to track
* - 2D array of strings * - new data being entered for existing row
* - We do know the type of a single column is fixed * - new data for new row
* - Keep data in Result and only load data in replacement rows * - current data of modified row
* std::string has short string optimization so this probably means * - current data of new row
* that a two dimensional array of std::string objects could actually be quite efficient! * - which rows are deleted
* We need to be able to:
* - determine row count easily
* - find specific row from table easily
* - support resorting (without requerying)
* //- support custom filtering (without requerying)
*
* Keep data as much in original result
* - use redirect list to go from index to actual row in the result
* - use an additional map index by original row number containing changed data
* - might be beneficial to have vector<bool> in sync with original list to signal changes from the result
* this could prevent doing a lot of slower searches in the changed row set by only doing this when the direty markes
* has been set vector<bool> takes 1 bit per value making it very efficient and fast. We also could put a bit in the mapping
* vector but this probably would double the size of that list taking 32 times the size of vector<bool>.
*
*/ */
class CrudModel: public QAbstractTableModel { class CrudModel: public QAbstractTableModel {
Q_OBJECT Q_OBJECT
@ -52,13 +67,15 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const override; virtual QVariant data(const QModelIndex &index, int role) const override;
virtual Qt::ItemFlags flags(const QModelIndex &) const override; virtual Qt::ItemFlags flags(const QModelIndex &) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
public slots:
virtual bool submit() override;
virtual void revert() override;
private: private:
ASyncWindow * m_asyncWindow; using PKeyValues = std::vector<std::string>;
std::shared_ptr<OpenDatabase> m_database;
PgClass m_table;
boost::optional<PgConstraint> m_primaryKey;
ASyncDBConnection m_dbConn;
bool callLoadData = false;
// using RowData = std::vector<std::string>; // using RowData = std::vector<std::string>;
@ -69,58 +86,157 @@ private:
// */ // */
// using RowList = std::vector<RowDataPtr>; // using RowList = std::vector<RowDataPtr>;
std::shared_ptr<Pgsql::Result> m_roData;
void loadData();
void loadIntoModel(std::shared_ptr<Pgsql::Result> data);
// std::shared_ptr<RowList> resultToRowList(std::shared_ptr<Pgsql::Result> result); // std::shared_ptr<RowList> resultToRowList(std::shared_ptr<Pgsql::Result> result);
using Value = boost::optional<std::string>; using Value = boost::optional<std::string>;
/** Remembers the values that have been applies to a row after the original result was retrieved. /** Used to remember the changes that have been made to rows.
*
* For simplicity it also holds the values for columns that have not been changed.
*/ */
class ModifiedRow { class ModifiedRow {
public: public:
ModifiedRow(int row, const std::vector<Value> &values)
: m_row(row), m_values(values)
{}
const auto& data() const { return m_values; }
int row() const { return m_row; }
private: private:
int m_row;
std::vector<Value> m_values; std::vector<Value> m_values;
}; };
using ModifiedRowList = std::map<int, ModifiedRow>; using ModifiedRowList = std::map<int, ModifiedRow>;
/** Manages the changes for the current row /** Similar to a modified row but it only stores values for columns that actually have been edited.
* *
*/ */
class PendingChanges { class PendingRow {
public: public:
using ValueMap = std::map<int16_t, Value>;
explicit PendingRow(int row)
: m_row(row)
{}
const auto& data() const { return m_values; }
int row() const { return m_row; }
void setValue(int col, const Value &val)
{
m_values.insert_or_assign(col, val);
}
private: private:
int m_currentRow; int m_row;
std::vector<std::string> m_currentPKeyValues; ///< The values to use for updating the row, for new rows this list is empty ValueMap m_values;
std::map<int16_t, Value> m_values; ///< values that need to be applied
}; };
using DeletedList = std::vector<int>; // These are the original indexes before there were any modifications
PendingChanges m_pendingChanges; class PendingRowList {
public:
using Map = std::map<int, PendingRow>;
PendingRow& getRow(int row)
{
auto iter = m_rows.lower_bound(row);
if (iter != m_rows.end() && iter->first == row) {
return iter->second;
}
else {
return m_rows.insert(iter, {row, PendingRow(row)})->second;
}
}
void setValue(int col, int row, const Value &value)
{
auto iter = m_rows.find(row);
if (iter == m_rows.end()) {
iter = m_rows.insert({row, PendingRow(row)}).first;
}
iter->second.setValue(col, value);
}
boost::optional<Value> getValue(int col, int row) const
{
auto iter = m_rows.find(row);
if (iter != m_rows.end()) {
auto &r = iter->second;
auto cell = r.data().find(col);
if (cell != r.data().end())
return cell->second;
}
return boost::none;
}
auto begin() { return m_rows.begin(); }
auto end() { return m_rows.end(); }
void clear() { m_rows.clear(); }
private:
Map m_rows;
};
ASyncWindow * m_asyncWindow;
std::shared_ptr<OpenDatabase> m_database;
PgClass m_table;
boost::optional<PgConstraint> m_primaryKey;
ASyncDBConnection m_dbConn;
bool callLoadData = false;
std::shared_ptr<Pgsql::Result> m_roData;
PendingRowList m_pendingRowList;
/** Maintains a list of all modified rows. /** Maintains a list of all modified rows.
* *
* The key values are the indexes of the row before any rows were deleted. * The key values are the indexes of the row before any rows were deleted.
*/ */
ModifiedRowList m_modifiedRowList; ModifiedRowList m_modifiedRowList;
using RedirectVec = std::vector<int>;
// Alternative, vector of rows /// In sync with the actual table, used to efficiently find the correct row in the result
class Row { RedirectVec m_redirectVector;
public:
int resultRow; ///< row index in original result, -1 for new rows
std::vector<Value> currentValues; ///
};
QVariant getData(const QModelIndex &index) const; void loadData();
void loadIntoModel(std::shared_ptr<Pgsql::Result> data);
Value getData(const QModelIndex &index) const;
Oid getType(int column) const; Oid getType(int column) const;
///
/// \brief columnName
/// \param col
/// \return
///
QString columnName(int col) const;
///
/// \brief getModifiedRow searches for the specified row in the modified set
/// \param row
/// \return Pointer to the modified element or nullptr if there is no modification
///
const ModifiedRow* getModifiedRow(int row) const;
///
/// \brief getPKeyForRow retrieve the primary key of the specified row
///
/// This function should not be called when there is no primarykey. (Editing should be disabled anyway in that case)
///
/// \param row Actual result row not intermeddiat model row
/// \return
///
PKeyValues getPKeyForRow(int row) const;
bool savePendingChanges();
void updateRow(const PendingRow &pending_row);
private slots: private slots:
void connectionStateChanged(ASyncDBConnection::State state); void connectionStateChanged(ASyncDBConnection::State state);

View file

@ -21,6 +21,10 @@ CrudTab::CrudTab(MainWindow *parent)
m_crudModel = new CrudModel(parent); m_crudModel = new CrudModel(parent);
ui->tableView->setModel(m_crudModel); ui->tableView->setModel(m_crudModel);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
//auto selection_model = ui->tableView->selectionModel();
// connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
// &CrudTab::tableView_currentRowChanged);
} }
CrudTab::~CrudTab() CrudTab::~CrudTab()
@ -41,3 +45,8 @@ void CrudTab::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table)
// highlighter->setTypes(*cat->types()); // highlighter->setTypes(*cat->types());
m_crudModel->setConfig(db, table); m_crudModel->setConfig(db, table);
} }
//void CrudTab::tableView_currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
//{
//}

View file

@ -32,6 +32,9 @@ private:
PgClass m_table; PgClass m_table;
CrudModel *m_crudModel = nullptr; CrudModel *m_crudModel = nullptr;
private slots:
// void tableView_currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
}; };
#endif // CRUDTAB_H #endif // CRUDTAB_H

View file

@ -3,8 +3,11 @@
#include <libpq-fe.h> #include <libpq-fe.h>
#include <vector> #include <vector>
#include <boost/container/small_vector.hpp>
using AttNumVec = std::vector<int16_t>; using AttNumVec = std::vector<int16_t>;
template<int size>
using SmallAttNumVec = boost::container::small_vector<int16_t, size>;
using OidVec = std::vector<Oid>; using OidVec = std::vector<Oid>;
#endif // PGCATALOGTYPES_H #endif // PGCATALOGTYPES_H

View file

@ -65,8 +65,8 @@ public:
bool islocal; bool islocal;
int32_t inhcount; int32_t inhcount;
bool noinherit; bool noinherit;
AttNumVec key; // list of constraint columns attnum SmallAttNumVec<5> key; // list of constraint columns attnum
AttNumVec fkey; // fkey list of referenced columns SmallAttNumVec<5> fkey; // fkey list of referenced columns
OidVec pfeqop; OidVec pfeqop;
OidVec ppeqop; OidVec ppeqop;
OidVec ffeqop; OidVec ffeqop;

View file

@ -0,0 +1,22 @@
#include "QueryGenerator.h"
#include "PgDatabaseCatalog.h"
#include "PgClass.h"
#include "PgNamespace.h"
#include "PgNamespaceContainer.h"
using namespace Querygen;
QueryGeneratorFactory::QueryGeneratorFactory(std::shared_ptr<PgDatabaseCatalog> catalog)
: m_catalog(catalog)
{}
UpdatePtr QueryGeneratorFactory::update(QString ns, QString table_name, QString alias)
{
return std::make_shared<Update>(ns, table_name, alias);
}
UpdatePtr QueryGeneratorFactory::update(const PgClass &table_class, QString alias)
{
//QString nsname = m_catalog->namespaces()->getByKey(table_class.relnamespace_name)
return update(table_class.relnamespace_name, table_class.name, alias);
}

60
pglablib/QueryGenerator.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef QUERYGENERATOR_H
#define QUERYGENERATOR_H
#include <QString>
#include <memory>
class PgDatabaseCatalog;
class PgClass;
namespace Querygen {
class QueryGeneratorFactory;
using QueryGeneratorFactoryPtr = std::shared_ptr<QueryGeneratorFactory>;
class Update;
using UpdatePtr = std::shared_ptr<Update>;
class ColumnRef;
using ColumnRefPtr = std::shared_ptr<ColumnRef>;
class FQTableName {
public:
QString nsName;
QString tableName;
};
class ColumnRef {
public:
virtual QString getFCQS() const = 0;
};
class Update {
public:
Update(QString ns, QString tbl, QString alias)
: table{ns, tbl}
{}
private:
FQTableName table;
QString tableAlias;
};
using UpdatePtr = std::shared_ptr<Update>;
class QueryGeneratorFactory {
public:
QueryGeneratorFactory(std::shared_ptr<PgDatabaseCatalog> catalog);
UpdatePtr update(QString ns, QString table_name, QString alias = QString());
UpdatePtr update(const PgClass &table_class, QString alias = QString());
private:
std::shared_ptr<PgDatabaseCatalog> m_catalog;
};
} // end namespace Querygen
#endif // QUERYGENERATOR_H

View file

@ -94,7 +94,7 @@ private:
QString m_str; QString m_str;
}; };
QString getColumnNameList(const PgDatabaseCatalog &catalog, Oid relid, const AttNumVec &attnums) QString getColumnNameList(const PgDatabaseCatalog &catalog, Oid relid, const SmallAttNumVec<5> &attnums)
{ {
IdentListString result; IdentListString result;

View file

@ -8,6 +8,7 @@ class PgConstraint;
class PgDatabaseCatalog; class PgDatabaseCatalog;
bool identNeedsQuotes(QString ident); bool identNeedsQuotes(QString ident);
QString quoteIdent(QString ident);
QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls); QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls);
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint); QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint);

View file

@ -51,7 +51,8 @@ SOURCES += \
ParamListModel.cpp \ ParamListModel.cpp \
util.cpp \ util.cpp \
SqlFormattingUtils.cpp \ SqlFormattingUtils.cpp \
PgKeywordList.cpp PgKeywordList.cpp \
QueryGenerator.cpp
HEADERS += \ HEADERS += \
Pglablib.h \ Pglablib.h \
@ -80,7 +81,8 @@ HEADERS += \
util.h \ util.h \
SqlFormattingUtils.h \ SqlFormattingUtils.h \
PgCatalogTypes.h \ PgCatalogTypes.h \
PgKeywordList.h PgKeywordList.h \
QueryGenerator.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib

View file

@ -62,9 +62,9 @@ void exportTable(const QTableView *view, QTextStream &out)
csv.setSeperator('\t'); csv.setSeperator('\t');
csv.setQuote('"'); csv.setQuote('"');
const int cols = model->columnCount(); auto cols = model->columnCount();
const int rows = model->rowCount(); auto rows = model->rowCount();
for (int row = 0; row < rows; ++row) { for (auto row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) { for (int col = 0; col < cols; ++col) {
auto idx = model->index(row, col); auto idx = model->index(row, col);
auto display_text = idx.data(Qt::DisplayRole).toString(); auto display_text = idx.data(Qt::DisplayRole).toString();

View file

@ -31,13 +31,16 @@ Params::Params(Params&& rhs)
, m_paramValues(std::move(rhs.m_paramValues)) , m_paramValues(std::move(rhs.m_paramValues))
, m_paramLengths(std::move(rhs.m_paramLengths)) , m_paramLengths(std::move(rhs.m_paramLengths))
, m_paramFormats(std::move(rhs.m_paramFormats)) , m_paramFormats(std::move(rhs.m_paramFormats))
{} {
rhs.m_paramValues.clear(); // just in case it was copied instead of moved
}
Params& Params::operator=(Params&& rhs) Params& Params::operator=(Params&& rhs)
{ {
if (&rhs != this) { if (&rhs != this) {
m_paramTypes = std::move(rhs.m_paramTypes); m_paramTypes = std::move(rhs.m_paramTypes);
m_paramValues = std::move(rhs.m_paramValues); m_paramValues = std::move(rhs.m_paramValues);
rhs.m_paramValues.clear(); // just in case it was copied instead of moved
m_paramLengths = std::move(rhs.m_paramLengths); m_paramLengths = std::move(rhs.m_paramLengths);
m_paramFormats = std::move(rhs.m_paramFormats); m_paramFormats = std::move(rhs.m_paramFormats);
} }
@ -67,14 +70,31 @@ void Params::add(const QString &s, Oid oid)
addText(p, oid); addText(p, oid);
} }
void Params::addBinary(const char *data, int length, Oid oid) void Params::add(const char *data, Oid oid)
{ {
char * p = nullptr;
int len = 0;
if (data) {
len = std::strlen(data);
p = new char[len+1];
std::memcpy(p, data, len);
p[len] = 0;
}
m_paramTypes.push_back(oid); m_paramTypes.push_back(oid);
m_paramValues.push_back(data); m_paramValues.push_back(p);
m_paramLengths.push_back(length); m_paramLengths.push_back(len);
m_paramFormats.push_back(1); m_paramFormats.push_back(0);
} }
//void Params::addBinary(const char *data, int length, Oid oid)
//{
// m_paramTypes.push_back(oid);
// m_paramValues.push_back(data);
// m_paramLengths.push_back(length);
// m_paramFormats.push_back(1);
//}
void Params::clear() void Params::clear()
{ {
m_paramTypes.clear(); m_paramTypes.clear();
@ -89,9 +109,14 @@ void Params::copyValues(const t_paramValues &r)
const int n = m_paramTypes.size(); const int n = m_paramTypes.size();
m_paramValues.reserve(n); m_paramValues.reserve(n);
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
const int len = m_paramLengths[i]; if (r[i]) {
char * p = new char[len]; const int len = m_paramLengths[i];
std::memcpy(p, r[i], len); char * p = new char[len+1];
m_paramValues.push_back(p); std::memcpy(p, r[i], len+1);
m_paramValues.push_back(p);
}
else {
m_paramValues.push_back(nullptr);
}
} }
} }

View file

@ -18,13 +18,9 @@ namespace Pgsql {
~Params(); ~Params();
/** \brief Add a parameter to the list.
*
* The class takes ownership of data and will try to delete[] it.
*/
void addText(const char *data, Oid oid=VARCHAROID);
void add(const QString &s, Oid oid=VARCHAROID); void add(const QString &s, Oid oid=VARCHAROID);
void addBinary(const char *data, int length, Oid oid); void add(const char *data, Oid oid=VARCHAROID);
//void addBinary(const char *data, int length, Oid oid);
void clear(); void clear();
bool empty() const { return m_paramTypes.empty(); } bool empty() const { return m_paramTypes.empty(); }
@ -51,6 +47,12 @@ namespace Pgsql {
t_paramValues m_paramValues; t_paramValues m_paramValues;
std::vector<int> m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying std::vector<int> m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying
std::vector<int> m_paramFormats; std::vector<int> m_paramFormats;
/** \brief Add a parameter to the list.
*
* The class takes ownership of data and will try to delete[] it.
*/
void addText(const char *data, Oid oid=VARCHAROID);
}; };
} // end namespace Pgsql } // end namespace Pgsql