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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/*
* This file is part of the xTuple ERP: PostBooks Edition, a free and
* open source Enterprise Resource Planning software suite,
* Copyright (c) 1999-2010 by OpenMFG LLC, d/b/a xTuple.
* It is licensed to you under the Common Public Attribution License
* version 1.0, the full text of which (including xTuple-specific Exhibits)
* is available at www.xtuple.com/CPAL. By using this software, you agree
* to be bound by its terms.
*/
#include "loadable.h"
#include <QDomDocument>
#include <QRegExp>
#include <QSqlError>
#include <QVariant> // used by XSqlQuery::value()
#include <limits.h>
#include "xsqlquery.h"
QRegExp Loadable::trueRegExp("^t(rue)?$", Qt::CaseInsensitive);
QRegExp Loadable::falseRegExp("^f(alse)?$", Qt::CaseInsensitive);
QString Loadable::_sqlerrtxt = TR("The following error was "
"encountered while trying to import %1 into "
"the database:<br><pre>%2<br>%3</pre>");
Loadable::Loadable(const QString &nodename, const QString &name,
const int grade, const bool system, const QString &schema,
const QString &comment,
const QString &filename)
{
_nodename = nodename;
_name = name;
_grade = grade;
_system = system;
_schema = schema;
_comment = comment;
_filename = (filename.isEmpty() ? name : filename);
_minMql = 0;
_maxMql = 0;
_selectMql = 0;
_insertMql = 0;
_updateMql = 0;
}
Loadable::Loadable(const QDomElement & elem, const bool system,
QStringList &/*msg*/, QList<bool> &/*fatal*/)
{
_system = system;
_nodename = elem.nodeName();
_grade = 0;
if (elem.hasAttribute("name"))
_name = elem.attribute("name");
if (elem.hasAttribute("grade"))
{
if (elem.attribute("grade").contains("highest", Qt::CaseInsensitive))
_grade = INT_MAX;
else if (elem.attribute("grade").contains("lowest", Qt::CaseInsensitive))
_grade = INT_MIN;
else
_grade = elem.attribute("grade").toInt();
}
else if (elem.hasAttribute("order"))
{
if (elem.attribute("order").contains("highest", Qt::CaseInsensitive))
_grade = INT_MAX;
else if (elem.attribute("order").contains("lowest", Qt::CaseInsensitive))
_grade = INT_MIN;
else
_grade = elem.attribute("order").toInt();
}
if (elem.hasAttribute("file"))
_filename = elem.attribute("file");
else
_filename = _name;
if (elem.hasAttribute("schema"))
_schema = elem.attribute("schema");
if (elem.hasAttribute("onerror"))
_onError = Script::nameToOnError(elem.attribute("onerror"));
else
_onError = Script::nameToOnError("Stop");
_comment = elem.text().trimmed();
_minMql = 0;
_maxMql = 0;
_selectMql = 0;
_insertMql = 0;
_updateMql = 0;
}
Loadable::~Loadable()
{
if (_minMql) delete _minMql;
if (_maxMql) delete _maxMql;
if (_selectMql) delete _selectMql;
if (_insertMql) delete _insertMql;
if (_updateMql) delete _updateMql;
}
QString Loadable::schema() const
{
return _schema;
}
QDomElement Loadable::createElement(QDomDocument & doc)
{
QDomElement elem = doc.createElement(_nodename);
elem.setAttribute("name", _name);
elem.setAttribute("grade", _grade);
elem.setAttribute("file", _filename);
if (! _schema.isEmpty())
elem.setAttribute("schema", _schema);
if(!_comment.isEmpty())
elem.appendChild(doc.createTextNode(_comment));
return elem;
}
int Loadable::writeToDB(const QByteArray &pdata, const QString pkgname,
QString &errMsg, ParameterList ¶ms)
{
params.append("name", _name);
params.append("type", _pkgitemtype);
params.append("source", QString(pdata));
params.append("notes", _comment);
// alter the name of the loadable's table if necessary
QString destschema = "public";
QString prefix;
if (_schema.isEmpty() && pkgname.isEmpty())
; // leave it alone
else if (_schema.isEmpty() && ! pkgname.isEmpty())
{
prefix = "pkg";
destschema = pkgname;
}
else if ("public" == _schema && pkgname.isEmpty())
; // leave it alone
else if ("public" == _schema && ! pkgname.isEmpty())
prefix = "public.";
else if (! _schema.isEmpty())
{
prefix = _schema + ".pkg";
destschema = _schema;
}
if (! prefix.isEmpty())
{
params.append("pkgname", destschema);
// yuck - no Parameter::operator==(Parameter&) and no replace()
QString tablename = params.value("tablename").toString();
for (int i = 0; i < params.size(); i++)
{
if (params.at(i).name() == "tablename")
{
params.takeAt(i);
params.append("tablename", prefix + tablename);
break;
}
}
}
if (_minMql && _minMql->isValid() && _grade == INT_MIN)
{
XSqlQuery minOrder = _minMql->toQuery(params);
if (minOrder.first())
_grade = minOrder.value(0).toInt();
else if (minOrder.lastError().type() != QSqlError::NoError)
{
QSqlError err = minOrder.lastError();
errMsg = _sqlerrtxt.arg(_filename).arg(err.driverText()).arg(err.databaseText());
return -3;
}
else
_grade = 0;
}
else if (_maxMql && _maxMql->isValid() && _grade == INT_MAX)
{
XSqlQuery maxOrder = _maxMql->toQuery(params);
if (maxOrder.first())
_grade = maxOrder.value(0).toInt();
else if (maxOrder.lastError().type() != QSqlError::NoError)
{
QSqlError err = maxOrder.lastError();
errMsg = _sqlerrtxt.arg(_filename).arg(err.driverText()).arg(err.databaseText());
return -4;
}
else
_grade = 0;
}
params.append("grade", _grade);
XSqlQuery select;
int itemid = -1;
select = _selectMql->toQuery(params);
if (select.first())
itemid = select.value(0).toInt();
else if (select.lastError().type() != QSqlError::NoError)
{
QSqlError err = select.lastError();
errMsg = _sqlerrtxt.arg(_filename).arg(err.driverText()).arg(err.databaseText());
return -5;
}
params.append("id", itemid);
XSqlQuery upsert;
if (itemid >= 0)
upsert = _updateMql->toQuery(params);
else
upsert = _insertMql->toQuery(params);
if (upsert.first())
itemid = upsert.value("id").toInt();
else if (upsert.lastError().type() != QSqlError::NoError)
{
QSqlError err = upsert.lastError();
errMsg = _sqlerrtxt.arg(_filename)
.arg(err.driverText())
.arg(err.databaseText());
return -7;
}
return itemid;
}
|