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
|
#include "scriptingsql.h"
#include "common/unused.h"
#include "db/db.h"
#include "db/sqlquery.h"
#include "services/dbmanager.h"
#include "common/utils_sql.h"
ScriptingSql::ScriptingSql()
{
}
ScriptingSql::~ScriptingSql()
{
}
QString ScriptingSql::getLanguage() const
{
return "SQL";
}
ScriptingPlugin::Context* ScriptingSql::createContext()
{
SqlContext* ctx = new SqlContext();
contexts << ctx;
return ctx;
}
void ScriptingSql::releaseContext(ScriptingPlugin::Context* context)
{
if (!contexts.contains(context))
return;
delete context;
contexts.removeOne(context);
}
void ScriptingSql::resetContext(ScriptingPlugin::Context* context)
{
dynamic_cast<SqlContext*>(context)->errorText.clear();
}
QVariant ScriptingSql::evaluate(ScriptingPlugin::Context* context, const QString& code, const FunctionInfo& funcInfo, const QList<QVariant>& args, Db* db, bool locking)
{
SqlContext* ctx = dynamic_cast<SqlContext*>(context);
ctx->errorText.clear();
Db* theDb = nullptr;
if (db && db->isValid())
theDb = db;
else if (memDb)
theDb = memDb;
else
return QVariant();
Db::Flags execFlags;
if (!locking)
execFlags |= Db::Flag::NO_LOCK;
QString sql = code;
if (ctx->variables.size() > 0)
{
QList<QString> keys = ctx->variables.keys();
std::sort(keys.begin(), keys.end(), std::greater<QString>());
for (const QString& key : keys)
{
QString value = "'" + ctx->variables[key].toString() + "'";
sql.replace(":" + key, value).replace("@" + key, value).replace("$" + key, value);
}
}
replaceNamedArgs(sql, funcInfo, args);
SqlQueryPtr result = theDb->exec(sql, args, execFlags);
if (result->isError())
{
dynamic_cast<SqlContext*>(context)->errorText = result->getErrorText();
return QVariant();
}
return result->getSingleCell();
}
QVariant ScriptingSql::evaluate(const QString& code, const FunctionInfo& funcInfo, const QList<QVariant>& args, Db* db, bool locking, QString* errorMessage)
{
Db* theDb = nullptr;
if (db && db->isValid())
theDb = db;
else if (memDb)
theDb = memDb;
else
return QVariant();
Db::Flags execFlags;
if (!locking)
execFlags |= Db::Flag::NO_LOCK;
QString sql = code;
replaceNamedArgs(sql, funcInfo, args);
SqlQueryPtr result = theDb->exec(sql, args, execFlags);
if (result->isError())
{
*errorMessage = result->getErrorText();
return QVariant();
}
return result->getSingleCell();
}
void ScriptingSql::setVariable(ScriptingPlugin::Context* context, const QString& name, const QVariant& value)
{
dynamic_cast<SqlContext*>(context)->variables[name] = value;
}
QVariant ScriptingSql::getVariable(ScriptingPlugin::Context* context, const QString& name)
{
if (dynamic_cast<SqlContext*>(context)->variables.contains(name))
return dynamic_cast<SqlContext*>(context)->variables[name];
return QVariant();
}
bool ScriptingSql::hasError(ScriptingPlugin::Context* context) const
{
return !getErrorMessage(context).isNull();
}
QString ScriptingSql::getErrorMessage(ScriptingPlugin::Context* context) const
{
return dynamic_cast<SqlContext*>(context)->errorText;
}
QString ScriptingSql::getIconPath() const
{
return ":/images/plugins/scriptingsql.png";
}
bool ScriptingSql::init()
{
memDb = DBLIST->createInMemDb();
return memDb != nullptr;
}
void ScriptingSql::deinit()
{
for (Context* context : contexts)
delete context;
contexts.clear();
safe_delete(memDb);
}
void ScriptingSql::replaceNamedArgs(QString& sql, const ScriptingPlugin::FunctionInfo& funcInfo, const QList<QVariant>& args)
{
// First build map of argName to its value in order in which arguments were passed to the function
int i = 0;
QStringList argNames = funcInfo.getArguments();
QHash<QString, QString> argMap;
for (const QString& argName : argNames)
{
if (i >= args.size())
break;
argMap[argName] = valueToSqlLiteral(args[i++]);
}
// Then sort arguments in alphabetically descending order, to prevent replacing shorter names first
// and proceed with argument substitutions
std::sort(argNames.begin(), argNames.end(), std::greater<QString>());
for (const QString& argName : argNames)
{
QString value = argMap[argName];
sql.replace(":" + argName, value)
.replace("@" + argName, value)
.replace("$" + argName, value);
}
}
|