how can I generalize a structure using templates?

Hi,

This is something I don't want to write for each table in a database and am sure templates would be excellent for this kind of work.. but how?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
struct Password
{
	// bool valid = false;
	int id;
	QString password;
	QDateTime beginDate;
	int fkLocation;
	explicit Password(const QSqlRecord& rec)
	{
		// valid = rec != nullptr;
		id = rec.value(0).toInt();
		password = rec.value(1).toString();
		beginDate = rec.value(2).toDateTime();
		fkLocation = rec.value(3).toInt();
	}
};


class PasswordFields
{
	QSqlField f1;
	QSqlField f2;
	QSqlField f3;
	QSqlField f4;
	QSqlRecord rec;
public:
	explicit PasswordFields() : f1{ "id", QMetaType(QMetaType::Int) }, f2{ "password", QMetaType(QMetaType::QString) },
		f3{ "beginDate", QMetaType(QMetaType::QDateTime) }, f4("fkLocation", QMetaType{ QMetaType::Int })
	{}
	explicit PasswordFields(const QSqlRecord& r) : PasswordFields()
	{
		Password pwd(r);
		if(pwd.id > -1)
		{
			f1.setValue(pwd.id);
			rec.append(f1);
		}
		f2.setValue(pwd.password);
		f3.setValue(pwd.beginDate);
		f4.setValue(pwd.fkLocation);

		rec.append(f2);
		rec.append(f3);
		rec.append(f4);
	}
	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : PasswordFields()
	{
		if (id > -1)
		{
			f1.setValue(id);
			rec.append(f1);
		}
		f2.setValue(password);
		f3.setValue(beginDate);
		f4.setValue(fkLocation);
		
		rec.append(f2);
		rec.append(f3);
		rec.append(f4);
	}
	void setId(int pk)
	{
		f1.setValue(pk);
		rec.insert(0, f1);
		qDebug();
	}
	Password get() { return Password{ rec }; }
	QSqlRecord record() const { return rec; }
};


class PasswordGateway
{
	struct SQL
	{
		inline static const auto CreatePasswordTable = QLatin1String(R"(
			create table password(id integer primary key, password varchar, beginDate date, fkLocation integer)
		)");
		inline static const auto DestroyPasswordTable = QLatin1String(R"(
			drop table password
		)");
		inline static const auto InsertPassword = QLatin1String(R"(
			insert into password(password, beginDate, fkLocation)
                      values(?, ?, ?)
		)");
		inline static const auto UpdatePassword = QLatin1String(R"(
			update password SET password = ?, beginDate = ?, fkLocation = ?
			where id = ?)");
		inline static const auto DeletePassword = QLatin1String(R"(
			delete from password where id = ?)");

		inline static const auto FindPassword = QLatin1String(R"(
			select * from password where id = ?)");

		inline static const auto FindAllPassword = QLatin1String(R"(
			select * from password)");

	};
public:
	std::vector<Password> findAll()
	{
		std::vector<Password> vec;
		QSqlQuery q;
		if(! q.exec(SQL::FindAllPassword))
		{
			showError(q.lastError());
			return vec;
		}
		while(q.next())
		{
			Password rec(q.record());
			vec.push_back(rec);
		}
		return vec;
	}
	Password find(int pk)
	{
		QSqlQuery q;
		if( !q.prepare(SQL::FindPassword))
		{
			showError(q.lastError());
			return Password(QSqlRecord{});
		}
		q.addBindValue(pk);
		bool ok = q.exec();
		if(!ok)
		{
			showError(q.lastError());
		}
		q.next();
		QSqlRecord rec = q.record();
		return Password(rec);
	}
	int insert(const QString& password, QDateTime beginDate, QVariant fkLocation)
	{
		QSqlQuery q;
		if (!q.prepare(SQL::InsertPassword))
		{
			showError(q.lastError());
			return -1;
		}
		q.addBindValue(password);
		q.addBindValue(beginDate);
		q.addBindValue(fkLocation);
		bool ok = q.exec();
		return q.lastInsertId().toInt();
	}
	bool update(int pk, const QString& password, QDateTime beginDate, QVariant fkLocation)
	{
		QSqlQuery q;
		if (!q.prepare(SQL::UpdatePassword))
		{
			showError(q.lastError());
			return false;
		}
		q.addBindValue(password);
		q.addBindValue(beginDate);
		q.addBindValue(fkLocation);
		q.addBindValue(pk);
		bool ok = q.exec();
		return ok;
	}
	bool remove(int pk)
	{
		QSqlQuery q;
		if(! q.prepare(SQL::DeletePassword))
		{
			showError(q.lastError());
			return false;
		}
		q.addBindValue(pk);
		bool ok = q.exec();
		return ok;
	}
	bool createTable()
	{
		QSqlQuery q;
		bool ok = q.exec(SQL::CreatePasswordTable);
		if(!ok)
		{
			showError(q.lastError());
		}
		return ok;
	}
	bool destroyTable()
	{
		QSqlQuery q;
		bool ok = q.exec(SQL::DestroyPasswordTable);
		if(!ok)
		{
			showError(q.lastError());
		}
		return ok;
	}
};


Regarding,
Juan
Last edited on
Inheritance?
Templates are for specializing code generation over types.

Specializing function over a common class is exactly what inheritance is for.
Last edited on
used templates!
Topic archived. No new replies allowed.