Files
qt5-legatus-legacy/Lts/Common/LtsUtils/LtsUtilsCore/pgarray.h
T
2026-08-13 18:33:10 +02:00

51 lines
1.2 KiB
C++
Executable File

/**
* Helper class for a vector of integer (stored as integer[] in the postgres database
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2016-05-03
*/
#pragma once
#include <QString>
#include <map>
#include <vector>
#include <cstdint>
namespace LtsUtils
{
template<typename T, typename C>
class PgArray
{
public:
using Type = T;
using ContainerType = C;
PgArray() = default;
Q_DECL_CONSTEXPR explicit PgArray(const C &data)
: m_data(data)
{}
Q_DECL_CONSTEXPR C operator()()const
{
return m_data;
}
QString toPgString() const;
static PgArray fromPgString(const QString &str);
private:
C m_data;
};
// helper to convert a postgres array (one-dimensional) into a std::vector<T> and vice versa
// { 1, 5, 5, 2, 9 }
using IntArray = PgArray<int, std::vector<int>>; // integer[]
using BigintArray = PgArray<int64_t, std::vector<int64_t>>; // bigint[]
// helper to convert a two-dimensional postgres array into a std::map<T, T> and vice versa
// { { 1, 2 }, { 4, 3 }, { 10, 1 } }
using IntArray2D = PgArray<int, std::map<int, int>>; // integer[][]
using BigintArray2D = PgArray<int64_t, std::map<int64_t, int64_t>>; // bigint[][]
} // namespace LtsUtils