Skip to content

Commit e1ad548

Browse files
committed
Standardization CWF. Code by Sylll
1 parent 8b5f56f commit e1ad548

File tree

4 files changed

+118
-166
lines changed

4 files changed

+118
-166
lines changed

CPPWebFramework/cwf/model.cpp

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
CWF_BEGIN_NAMESPACE
44

5-
Model::Model(const QString &name) : QObject(nullptr)
6-
{
7-
m_name = name;
8-
}
9-
10-
Model::~Model()
11-
{
12-
13-
}
14-
155
void Model::updateDB()
166
{
177
// ****************************************************************
@@ -45,7 +35,7 @@ void Model::build(const QMap<QString, QVariant> &selectCondition)
4535

4636
QStringList properties = listAllProperties();
4737

48-
QMap<QString, QVariant> propValueMap = m_basicOperation.build(m_name, selectCondition, properties);
38+
QMap<QString, QVariant> propValueMap = basicOperation.build(name, selectCondition, properties);
4939

5040
// Loop on all the prop value to update the current object
5141
for(const auto& it : propValueMap.toStdMap() )
@@ -56,9 +46,9 @@ void Model::build(const QMap<QString, QVariant> &selectCondition)
5646
// Depending on the content of propValueMap, we set the built flag to true or false.
5747
// If the db query failed then the list is empty here.
5848
if(propValueMap.empty() )
59-
m_built = false;
49+
built = false;
6050
else
61-
m_built = true;
51+
built = true;
6252
}
6353

6454
void Model::buildFromJson(const QJsonObject &json, bool withTableNamePrefix)
@@ -79,7 +69,7 @@ void Model::buildFromJson(const QJsonObject &json, bool withTableNamePrefix)
7969

8070
if(jsonValue.isUndefined() )
8171
{
82-
m_built = false;
72+
built = false;
8373
break;
8474
}
8575

@@ -89,7 +79,7 @@ void Model::buildFromJson(const QJsonObject &json, bool withTableNamePrefix)
8979

9080
if(!success)
9181
{
92-
m_built = false;
82+
built = false;
9383
break;
9484
}
9585
}
@@ -113,14 +103,14 @@ bool Model::save()
113103
QDateTime currentDt = QDateTime::currentDateTime();
114104

115105
// Creation of a new object
116-
if(m_id == -1)
106+
if(id == -1)
117107
{
118-
this->setProperty("createdDateTime", currentDt.toString(m_dtFormat) );
108+
this->setProperty("createdDateTime", currentDt.toString(dtFormat) );
119109
setCreatedDt(currentDt);
120110
}
121111

122112
// Modification
123-
this->setProperty("lastModifiedDateTime", currentDt.toString(m_dtFormat) );
113+
this->setProperty("lastModifiedDateTime", currentDt.toString(dtFormat) );
124114
setLastModifiedDt(currentDt);
125115

126116
// --------------------------------------------------
@@ -133,7 +123,7 @@ bool Model::save()
133123

134124
QMap<QString, QVariant> propsMap = computePropsMap(*this);
135125

136-
qint64 id = m_basicOperation.save(m_name, propsMap);
126+
qint64 id = basicOperation.save(name, propsMap);
137127

138128
setProperty("id", id);
139129

@@ -147,27 +137,27 @@ bool Model::remove()
147137
{
148138
const qint64& id = getId();
149139

150-
return m_basicOperation.remove(getTableName(), id);
140+
return basicOperation.remove(getTableName(), id);
151141
}
152142

153143
QString Model::getCreatedDtStr() const
154144
{
155-
return m_createdDateTime;
145+
return createdDateTime;
156146
}
157147

158148
QDateTime Model::getCreatedDt() const
159149
{
160-
return QDateTime::fromString(m_createdDateTime, m_dtFormat);
150+
return QDateTime::fromString(createdDateTime, dtFormat);
161151
}
162152

163153
QDateTime Model::getLastModifiedDt() const
164154
{
165-
return QDateTime::fromString(m_lastModifiedDateTime, m_dtFormat);
155+
return QDateTime::fromString(lastModifiedDateTime, dtFormat);
166156
}
167157

168158
QString Model::getLastModifiedDtStr() const
169159
{
170-
return m_lastModifiedDateTime;
160+
return lastModifiedDateTime;
171161
}
172162

173163
QStringList Model::listAllProperties() const
@@ -207,15 +197,15 @@ bool Model::preSaveCheck() const
207197

208198
void Model::verifyDbTableExist()
209199
{
210-
bool tableExistInDb = m_basicOperation.isTableInDb(m_name);
200+
bool tableExistInDb = basicOperation.isTableInDb(name);
211201

212202
if(!tableExistInDb)
213203
{
214-
bool success = m_basicOperation.createTable(m_name);
204+
bool success = basicOperation.createTable(name);
215205

216206
if(success)
217207
{
218-
qInfo() << "Model::verifyDbTableExist:" << "Table " << m_name << " was successfuly created";
208+
qInfo() << "Model::verifyDbTableExist:" << "Table " << name << " was successfuly created";
219209
}
220210
}
221211
}
@@ -229,7 +219,7 @@ void Model::verifyDbFields()
229219
QStringList fieldsFromObject = listAllProperties();
230220

231221
// Then get all the fields of the object that are stored in the db
232-
QStringList fieldsFromDb = m_basicOperation.fields(m_name);
222+
QStringList fieldsFromDb = basicOperation.fields(name);
233223

234224
// Loop on all the fields of the object and check if they are in the database
235225
for(const QString& fieldName : fieldsFromObject)
@@ -240,7 +230,7 @@ void Model::verifyDbFields()
240230
{
241231
// The field is not present in the db and we need to add it
242232

243-
qInfo() << "Model::verifyDbFields:"<< "In" << m_name
233+
qInfo() << "Model::verifyDbFields:"<< "In" << name
244234
<< "the field" << fieldName << "is present in the object but was not found in database."
245235
<< "We update the database."
246236
;
@@ -250,9 +240,9 @@ void Model::verifyDbFields()
250240

251241
QVariant::Type propertyType = this->propertyType(fieldName);
252242

253-
m_basicOperation.addFieldToTable(fieldName, propertyType, m_name);
243+
basicOperation.addFieldToTable(fieldName, propertyType, name);
254244

255-
customizeField(fieldName, propertyType, m_name);
245+
customizeField(fieldName, propertyType, name);
256246
}
257247
}
258248

@@ -272,13 +262,13 @@ void Model::verifyDbFields()
272262
// If we performed a change in the db
273263
if(dbChangedPerformed)
274264
{
275-
qInfo() << "The database was modified to update the table" << m_name << "."
265+
qInfo() << "The database was modified to update the table" << name << "."
276266
<< "We will update the version for this table."
277267
;
278268

279-
qint32 tableVersion = m_basicOperation.tableVersion(m_name);
269+
qint32 tableVersion = basicOperation.tableVersion(name);
280270
tableVersion += 1;
281-
m_basicOperation.changeTableVersion(m_name, tableVersion);
271+
basicOperation.changeTableVersion(name, tableVersion);
282272
}
283273
}
284274

@@ -310,7 +300,7 @@ QMetaProperty Model::findProperty(const QString &propertyName) const
310300

311301
if(!found)
312302
{
313-
QString msg = QString("") + "Model::findProperty:" + " The property " + propertyName + " was not found in " + m_name;
303+
QString msg = QString("") + "Model::findProperty:" + " The property " + propertyName + " was not found in " + name;
314304
qDebug() << msg;
315305
qFatal("Property should always be found here");
316306
}
@@ -362,7 +352,7 @@ void Model::setCreatedDt(const QDateTime &dt)
362352
{
363353
if(dt != getCreatedDt() )
364354
{
365-
m_createdDateTime = dt.toString(m_dtFormat);
355+
createdDateTime = dt.toString(dtFormat);
366356

367357
createdDateTimeChanged();
368358
}
@@ -372,7 +362,7 @@ void Model::setLastModifiedDt(const QDateTime &dt)
372362
{
373363
if(dt != getLastModifiedDt() )
374364
{
375-
m_lastModifiedDateTime = dt.toString(m_dtFormat);
365+
lastModifiedDateTime = dt.toString(dtFormat);
376366

377367
lastModifiedDateTimeChanged();
378368
}

CPPWebFramework/cwf/model.h

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CWF_BEGIN_NAMESPACE
3232
* {
3333
* Q_OBJECT
3434
*
35-
* Q_PROPERTY(QString email MEMBER m_email NOTIFY emailChanged)
35+
* Q_PROPERTY(QString email MEMBER email NOTIFY emailChanged)
3636
*
3737
* public:
3838
* UserModel() : Model("user") // Name of the model and database table
@@ -45,7 +45,7 @@ CWF_BEGIN_NAMESPACE
4545
* void emailChanged();
4646
*
4747
* private:
48-
* QString m_email;
48+
* QString email;
4949
* }
5050
* ```
5151
*
@@ -83,86 +83,82 @@ class CPPWEBFRAMEWORKSHARED_EXPORT Model : public QObject
8383
{
8484
Q_OBJECT
8585

86-
Q_PROPERTY(qint64 id MEMBER m_id NOTIFY idChanged)
87-
Q_PROPERTY(QString createdDateTime MEMBER m_createdDateTime NOTIFY createdDateTimeChanged)
88-
Q_PROPERTY(QString lastModifiedDateTime MEMBER m_lastModifiedDateTime NOTIFY lastModifiedDateTimeChanged)
86+
Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged)
87+
Q_PROPERTY(QString createdDateTime MEMBER createdDateTime NOTIFY createdDateTimeChanged)
88+
Q_PROPERTY(QString lastModifiedDateTime MEMBER lastModifiedDateTime NOTIFY lastModifiedDateTimeChanged)
8989

9090
public:
91-
Model(const QString& name); ///< @brief Constructor
92-
virtual ~Model(); ///< @brief Destructor
93-
9491
/**
95-
* @brief updateDB Will create or update the database schema to match the model class.
96-
*
92+
* @brief Constructor
93+
*/
94+
Model(const QString &name) : name(name) {}
95+
/**
96+
* @brief Destructor
97+
*/
98+
virtual ~Model() = default;
99+
/**
100+
* @brief updateDB Will create or update the database schema to match the model class.
97101
* This allows to create a working table from scratch or to update one. Note that a table update can add new
98102
* fields (Qt properties in the model code) but cannot remove any. Therefore, a developer should only add new
99103
* properties to a model and never remove one.
100104
*/
101105
void updateDB();
102-
103106
/**
104107
* @brief build Populate the model with data from the database.
105108
* @param id The id of the data to be used to populate the model.
106109
*/
107110
void build(const qint64& id);
108-
109111
/**
110112
* @brief build Populate the model with data from the database.
111113
* @param selectCondition A map ([propertyName] = propertyValue) of property values
112114
* used to choose which data to insert in the model.
113115
*
114116
* The first entry of database table that matchs all the property values will be used to populate the model.
115117
*/
116-
void build(const QMap<QString, QVariant>& selectCondition);
117-
118+
void build(const QMap<QString, QVariant> &selectCondition);
118119
/**
119120
* @brief buildFromJson Populate a model from json data
120121
* @param json The data used to populate the model instance
121122
* @param withTableNamePrefix Are the date properties prefixed with the name of this model db table ? This is false by default.
122123
*/
123-
void buildFromJson(const QJsonObject& json, bool withTableNamePrefix=false);
124-
124+
void buildFromJson(const QJsonObject &json, bool withTableNamePrefix = false);
125125
/**
126126
* @brief save Persist the model in the database
127127
* @return Bool: Was the model correctly persisted ?
128128
*/
129129
bool save();
130-
131130
/**
132131
* @brief remove Delete the model from the database
133132
* @return Bool: Was the data removed without error ?
134133
*/
135134
bool remove();
136-
137135
/**
138136
* @brief setCreatedDt Manually set the creation date
139137
* @param dt Date of the model first save into the database.
140138
*/
141139
void setCreatedDt(const QDateTime& dt);
142-
143140
/**
144141
* @brief setLastModifiedDt Set the last date at which the model was persisted in the database.
145142
* @param dt The date to be used.
146143
*/
147144
void setLastModifiedDt(const QDateTime& dt);
148-
149145
/**
150146
* @brief setWasBuild Set the "was build" flag that indicate if a model instance was correctly populated (build) by data.
151147
* @param b The flag.
152148
*/
153-
void setWasBuild(bool b) {m_built = b;}
149+
void setWasBuild(bool b) { built = b;}
154150

155151
/**
156152
* @brief getWasBuild Get the value of the "was build" flag.
157153
* @return Bool: Was the model instance populated by data from the database ?
158154
*/
159-
bool getWasBuild() const {return m_built;}
155+
bool getWasBuild() const { return built; }
160156

161157
/**
162158
* @brief getId Get the id of the model
163159
* @return qint64: The id of the model
164160
*/
165-
const qint64& getId() const {return m_id;}
161+
const qint64& getId() const { return id; }
166162

167163
/**
168164
* @brief getCreatedDt Get the creation date of the model
@@ -192,7 +188,7 @@ class CPPWEBFRAMEWORKSHARED_EXPORT Model : public QObject
192188
* @brief getTableName The name of database table corresponding to the model
193189
* @return QString: Name of database table and of the model
194190
*/
195-
const QString& getTableName() const {return m_name;}
191+
const QString& getTableName() const { return name; }
196192

197193
/**
198194
* @brief findProperty Find a property with its name
@@ -233,40 +229,37 @@ class CPPWEBFRAMEWORKSHARED_EXPORT Model : public QObject
233229
void lastModifiedDateTimeChanged();
234230

235231
protected:
236-
ModelBasicOperation m_basicOperation;
232+
ModelBasicOperation basicOperation;
237233

238234
/**
239235
* @brief preSaveCheck Perform some checks before persisting (saving) the model instance.
240236
* The persisting is done only if the checks where successful.
241237
* @return Bool: Was the check successful ?
242238
*/
243239
virtual bool preSaveCheck() const;
244-
245240
/**
246241
* @brief customizeField Allow to give special instructions when a field is created in the database.
247242
*/
248-
virtual void customizeField(const QString &/*fieldName*/,
249-
const QVariant::Type /*type*/,
250-
const QString &/*tableName*/
251-
) const {;}
243+
virtual void customizeField(const QString &fieldName,
244+
const QVariant::Type &type,
245+
const QString &tableName
246+
) const = 0;
252247

253248
private:
254-
QString m_name; ///< @brief m_name The name of the model
255-
249+
QString name; ///< @brief name The name of the model
256250
/**
257-
* @brief m_id The database id of the model
258-
*
251+
* @brief id The database id of the model
259252
* The id is set to -1 by default. It may change if the model was populated with data from the database (the id will become the one
260253
* of the entity stored in the database) or if a user manually set the variable before.
261254
*/
262-
qint64 m_id = -1;
255+
qint64 id = -1;
263256

264-
qint64 m_version; ///< @brief The version of the table (database side)
265-
bool m_built = false; ///< @brief Was the model instance populated with data ?
257+
qint64 version; ///< @brief The version of the table (database side)
258+
bool built = false; ///< @brief Was the model instance populated with data ?
266259

267-
QString m_dtFormat = "dd/MM/yyyy hh:mm:ss.zzz"; ///< @brief Format of the date used in this class
268-
QString m_createdDateTime; ///< @brief First save in the database date
269-
QString m_lastModifiedDateTime; ///< @brief Last modified and persisted date
260+
QString dtFormat = "dd/MM/yyyy hh:mm:ss.zzz"; ///< @brief Format of the date used in this class
261+
QString createdDateTime; ///< @brief First save in the database date
262+
QString lastModifiedDateTime; ///< @brief Last modified and persisted date
270263

271264
void verifyDbTableExist(); ///< @brief Check the database contains table corresponding to this model. Also create or update the table if needed.
272265
void verifyDbFields(); ///< @brief Check the database table contains all required fields (columns). If not, create them.

0 commit comments

Comments
 (0)