Added QDate/Time variant is valid checks for SQL use

With MySQL v5.7+ you have to specifically set either a valid date or NULL
otherwise you get SQL errors.  In previous versions setting a date to "" implied
NULL, this is no longer the case which causes nice SQL errors all over the
place.

This new RDCheckDateTime is an overloaded function that calls the respective
isValid method of QDate/Time/DateTime and either returns the requested formatted
string or returns NULL as a string.

This can then be used as part of an INSERT/UPDATE without breaking the stricter
NULL interpretation of MySQL v5.7+.
This commit is contained in:
Wayne Merricks 2016-06-03 23:45:31 +01:00
parent 04b4db9157
commit d222ed9ca0
2 changed files with 57 additions and 0 deletions

View File

@ -21,6 +21,58 @@
#include <vector>
#include <rdescape_string.h>
#include <qdatetime.h>
/**
* RDCheckDateTime - Checks for QTime.isValid
* @param time - QTime object
* @param format - QString representing time format e.g. HH:MM
* @return QString, "NULL" if not Valid else formatted Time String
*/
QString RDCheckDateTime(QTime const &time, QString const &format)
{
QString checkedValue = "NULL";
if(time.isValid())
checkedValue = time.toString(format);
return checkedValue;
}
/**
* RDCheckDateTime - Checks for QDateTime.isValid
* @param datetime - QDateTime object
* @param format - QString representing date time format e.g. yyyy-mm-dd HH:MM
* @return QString, "NULL" if not Valid else formatted DateTime String
*/
QString RDCheckDateTime(QDateTime const &datetime, QString const &format)
{
QString checkedValue = "NULL";
if(datetime.isValid())
checkedValue = datetime.toString(format);
return checkedValue;
}
/**
* RDCheckDateTime - Checks for QDate.isValid
* @param date - QDate object
* @param format - QString representing date format e.g. yyyy-mm-dd
* @return QString, "NULL" if not Valid else formatted Date String
*/
QString RDCheckDateTime(QDate const &date, QString const &format)
{
QString checkedValue = "NULL";
if(date.isValid())
checkedValue = date.toString(format);
return checkedValue;
}
QString RDEscapeString(QString const &str)
{

View File

@ -19,11 +19,16 @@
//
#include <qstring.h>
#include <qdatetime.h>
#ifndef RDESCAPE_STRING_H
#define RDESCAPE_STRING_H
QString RDCheckDateTime(const QTime &time, const QString &format);
QString RDCheckDateTime(const QDateTime &datetime, const QString &format);
QString RDCheckDateTime(const QDate &date, const QString &format);
QString RDEscapeString(const QString &str);
#endif // RDESCAPE_STRING_H