Rivendellaudio/lib/rdescape_string.cpp
Wayne Merricks f1175cd2f2 Fixes for invalid NULL sql on date/times
Most files are simple swaps to get rid of extra " as a NULL return will give
invalid SQL if used as follows UPDATE x SET y="NULL";

See github issue 121 for more info.

Extra changes have been made to the following files:
* lib/rdcartslot.cpp
- Added a QDateTime variable to prevent EVENT_DATETIME becoming
  "2016-06-09" "NULL" as it was checked separately for date and time
* lib/rdcut.cpp
- Reworked START_DATETIME and END_DATETIME to save the SQL ifs.  I believe this
  makes it easier to read and understand.
- The >QDate(1900,1,1) and < 8000 is probably not needed but I left it there
  just in case.
* lib/rdescape_string.cpp
- Reworked to add extra " if the date is not NULL see issue 121
* lib/rdfeed.cpp && lib/rdpodcast.cpp
- Had to add a SetRow for QDateTime as with the string conversion the existing
  SetRow would add an extra set of "
* lib/rdsound_panel.cpp && rdairplay/log_traffic.cpp
        && utils/rddgimport/rddgimport.cpp
- Added a QString to contain EVENT_DATETIME to prevent double checks of date
  and time separately (similar to lib/rdcartslot.cpp)
* rdcatchd/rdcatchd.cpp
- Reverted the changes.  The RML here is not touching the DB so is fine, plus
  with the RDCheckDateTime changes you'd have extra " unless it was NULL
* tests/sas_switch_torture.cpp && sas_torture.cpp
- Included missing rdescape_string.h
2016-06-09 00:01:38 +01:00

227 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// rdescape_string.cpp
//
// Escape non-valid characters in a string.
//
// (C) Copyright 2002-2005,2016 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#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)
{
QString res;
for(unsigned i=0;i<str.length();i++) {
switch(((const char *)str)[i]) {
case '(':
res+=QString("\\\(");
break;
case ')':
res+=QString("\\)");
break;
case '{':
res+=QString("\\\{");
break;
case '"':
res+=QString("\\\"");
break;
case '`':
res+=QString("\\`");
break;
case '[':
res+=QString("\\\[");
break;
case '\'':
res+=QString("\\\'");
break;
case '\\':
res+=QString("\\");
res+=QString("\\");
break;
case '?':
res+=QString("\\\?");
break;
case ' ':
res+=QString("\\ ");
break;
case '&':
res+=QString("\\&");
break;
case ';':
res+=QString("\\;");
break;
case '<':
res+=QString("\\<");
break;
case '>':
res+=QString("\\>");
break;
case '|':
res+=QString("\\|");
break;
default:
res+=((const char *)str)[i];
break;
}
}
/*
for(unsigned i=0;i<str.length();i++) {
switch(((const char *)str)[i]) {
case '(':
res+=QString("\\\(");
break;
case ')':
res+=QString("\\)");
break;
case '{':
res+=QString("\\\{");
break;
case '"':
res+=QString("\\\"");
break;
case '´':
res+=QString("\\´");
break;
case '`':
res+=QString("\\`");
break;
case '[':
res+=QString("\\\[");
break;
case '\'':
res+=QString("\\\'");
break;
case '\\':
res+=QString("\\");
res+=QString("\\");
break;
case '?':
res+=QString("\\\?");
break;
case ' ':
res+=QString("\\ ");
break;
case '&':
res+=QString("\\&");
break;
case ';':
res+=QString("\\;");
break;
case '<':
res+=QString("\\<");
break;
case '>':
res+=QString("\\>");
break;
case '|':
res+=QString("\\|");
break;
default:
res+=((const char *)str)[i];
break;
}
}
*/
return res;
}