149 lines
4.4 KiB
C
149 lines
4.4 KiB
C
|
#ifndef _DATA_COLUMN_
|
|||
|
#define _DATA_COLUMN_
|
|||
|
|
|||
|
#define INSENSETIVE
|
|||
|
#include <iostream>
|
|||
|
#include <QString>
|
|||
|
#include <QDebug>
|
|||
|
#include "UtilityGlobal.h"
|
|||
|
#include <QDateTime>
|
|||
|
using namespace std;
|
|||
|
|
|||
|
class DataTable;
|
|||
|
|
|||
|
/* 定义:表示 DataTable 或DataRow中的列的架构
|
|||
|
DataColumn是用于创建 架构DataTable的基本构建基块。 通过将一个或多个 DataColumn 对象添加到 来 DataColumnCollection生成架构。
|
|||
|
可通过Value()获取值Value(param)设置值
|
|||
|
也可通过=设置值 */
|
|||
|
template <typename T>
|
|||
|
class UTILITY_API TDataColumn
|
|||
|
{
|
|||
|
public:
|
|||
|
TDataColumn() : _column_name(QString()), _caption(QString()){};
|
|||
|
TDataColumn(const QString& column_name) : _column_name(column_name), _caption(QString()){};
|
|||
|
TDataColumn(const QString& column_name, QVariant::Type column_type) : _column_name(column_name), _type(column_type){};
|
|||
|
virtual ~TDataColumn(){};
|
|||
|
|
|||
|
public:
|
|||
|
void Caption(const QString& caption) { _caption = caption; } //设置列的标题
|
|||
|
QString Caption() const { return _caption.size() ? _caption : _column_name; } //获取列的标题
|
|||
|
void ColumnName(const QString& column_name) { _column_name = column_name; } //设置 DataColumnCollection 中的列的名称
|
|||
|
QString ColumnName() const { return _column_name; } //获取 DataColumnCollection 中的列的名称
|
|||
|
void Value(const T& value) { _value = value; } //获取值
|
|||
|
T Value() const { return _value; } //设置值
|
|||
|
void Table(DataTable* p_table){ _table = p_table; } //设置列所属的 DataTable
|
|||
|
DataTable* Table() const { return _table; } //获取列所属的 DataTable
|
|||
|
QVariant::Type DataType() const { return _type;} //获取存储在列中的数据的类型
|
|||
|
void DataType(QVariant::Type column_type) { _type = column_type;} //设置存储在列中的数据的类型
|
|||
|
|
|||
|
protected:
|
|||
|
QString _column_name; //列名
|
|||
|
QString _caption; //列的标题
|
|||
|
T _value = T(); //值
|
|||
|
QVariant::Type _type; //列的数据类型
|
|||
|
DataTable* _table = nullptr; //列所属DataTable
|
|||
|
};
|
|||
|
|
|||
|
//template class UTILITY_API TDataColumn<QString>;
|
|||
|
//template class UTILITY_API TDataColumn<std::string>;
|
|||
|
template class UTILITY_API TDataColumn<QVariant>; // 经测试,QVariant没有精度问题
|
|||
|
|
|||
|
class UTILITY_API DataColumn : public TDataColumn<QVariant>
|
|||
|
{
|
|||
|
friend class DataColumnCollection;
|
|||
|
friend class DataTable;
|
|||
|
|
|||
|
public:
|
|||
|
DataColumn() : TDataColumn<QVariant>(){};
|
|||
|
explicit DataColumn(const QString& column_name) : TDataColumn<QVariant>(column_name) {}
|
|||
|
DataColumn(const QString& column_name, QVariant::Type column_type) : TDataColumn<QVariant>(column_name, column_type) {}
|
|||
|
~DataColumn() { };
|
|||
|
|
|||
|
QString toString()
|
|||
|
{
|
|||
|
if(this->Value().isNull())
|
|||
|
{
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
if (this->DataType() == QVariant::DateTime)
|
|||
|
{
|
|||
|
return this->Value().toDateTime().toString("yyyy-MM-dd");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return this->Value().toString();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
int toInt()
|
|||
|
{
|
|||
|
if(this->Value().isNull())
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
return this->Value().toInt();
|
|||
|
}
|
|||
|
double toDouble()
|
|||
|
{
|
|||
|
if(this->Value().isNull())
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
return this->Value().toDouble();
|
|||
|
}
|
|||
|
|
|||
|
bool toBool()
|
|||
|
{
|
|||
|
if(this->Value().isNull())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return this->Value().toBool();
|
|||
|
}
|
|||
|
DataColumn(const DataColumn& data_column) { *this = data_column; }
|
|||
|
DataColumn &operator=(const DataColumn& data_column)
|
|||
|
{
|
|||
|
_column_name = data_column._column_name;
|
|||
|
_caption = data_column._caption;
|
|||
|
_value = data_column._value;
|
|||
|
_table = data_column._table;
|
|||
|
_type = data_column._type;
|
|||
|
return *this;
|
|||
|
}
|
|||
|
DataColumn &operator=(const QVariant& value) { //可使用=给value赋值
|
|||
|
_value = value;
|
|||
|
return *this;
|
|||
|
}
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
//备用代码: QString 版本
|
|||
|
/* class UTILITY_API DataColumn : public TDataColumn<QString>
|
|||
|
{
|
|||
|
public:
|
|||
|
friend class DataColumnCollection;
|
|||
|
friend class DataTable;
|
|||
|
DataColumn() : TDataColumn<QString>(){};
|
|||
|
~DataColumn(){ qDebug() << "delete!"; };
|
|||
|
DataColumn(const DataColumn& data_column){ *this = data_column; }
|
|||
|
DataColumn &operator=(const DataColumn& data_column)
|
|||
|
{
|
|||
|
_column_name = data_column._column_name;
|
|||
|
_caption = data_column._caption;
|
|||
|
_value = data_column._value;
|
|||
|
_table = data_column._table;
|
|||
|
return *this;
|
|||
|
}
|
|||
|
DataColumn(const QString& column_name) : TDataColumn<QString>(column_name){}
|
|||
|
void Show()
|
|||
|
{
|
|||
|
qDebug() << "columnName:" << _column_name << ",caption:" << Caption() << ",value:" << _value ;
|
|||
|
}
|
|||
|
|
|||
|
};
|
|||
|
*/
|
|||
|
|
|||
|
#endif
|