Gen code for query #78
4 changed files with 50 additions and 28 deletions
29
pglablib/FormatToStream.cpp
Normal file
29
pglablib/FormatToStream.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "FormatToStream.h"
|
||||
#include <QRegularExpression>
|
||||
#include <QTextStream>
|
||||
|
||||
void FormatToStream(QTextStream &stream, QString format, std::function<void(QTextStream &, QString)> field_callback)
|
||||
{
|
||||
// Use static to optimize only once
|
||||
static QRegularExpression cached_find_var_re("(?:[^\\\\]|^)(\\/%([a-zA-Z0-9_-]+)%\\/)", QRegularExpression::OptimizeOnFirstUsageOption);
|
||||
|
||||
int from = 0;
|
||||
QRegularExpressionMatch match;
|
||||
while (format.indexOf(cached_find_var_re, from, &match) >= 0) {
|
||||
if (from > 0) {
|
||||
// Because the regex has to check for backslash in front we have the from position
|
||||
// one position before where we actually should continue for the second match and later ie when from > 0
|
||||
// Therefor increase from by 1 to make the substring (midRef) calculation work
|
||||
++from;
|
||||
}
|
||||
// copy code before the var to the stream
|
||||
stream << format.midRef(from, match.capturedStart(1) - from);
|
||||
field_callback(stream, match.captured(2));
|
||||
from = match.capturedEnd()-1; // -1 because it wants to match one character before or start of line to make sure there is no backslash
|
||||
}
|
||||
if (from > 0) {
|
||||
// same reason as at the start of the loop
|
||||
++from;
|
||||
}
|
||||
stream << format.midRef(from);
|
||||
}
|
||||
17
pglablib/FormatToStream.h
Normal file
17
pglablib/FormatToStream.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef FORMATTOSTREAM_H
|
||||
#define FORMATTOSTREAM_H
|
||||
|
||||
#include <QString>
|
||||
#include <functional>
|
||||
|
||||
class QTextStream;
|
||||
|
||||
/**
|
||||
* @brief FormatToStream replaces /%var%/ variables in a string with the values returned by the callback
|
||||
* @param stream The stream to which to write the result of the formatting operation
|
||||
* @param format The format string containing the vars.
|
||||
* @param field_callback A callable which returns the values for var.
|
||||
*/
|
||||
void FormatToStream(QTextStream &stream, QString format, std::function<void(QTextStream &, QString)> field_callback);
|
||||
|
||||
#endif // FORMATTOSTREAM_H
|
||||
|
|
@ -6,32 +6,6 @@
|
|||
#include "util.h"
|
||||
#include <QTextStream>
|
||||
|
||||
void FormatToStream(QTextStream &stream, QString format, std::function<void(QTextStream &, QString)> field_callback)
|
||||
{
|
||||
// Use static to optimize only once
|
||||
static QRegularExpression cached_find_var_re("(?:[^\\\\]|^)(\\/%([a-zA-Z0-9_-]+)%\\/)", QRegularExpression::OptimizeOnFirstUsageOption);
|
||||
|
||||
int from = 0;
|
||||
QRegularExpressionMatch match;
|
||||
while (format.indexOf(cached_find_var_re, from, &match) >= 0) {
|
||||
if (from > 0) {
|
||||
// Because the regex has to check for backslash in front we have the from position
|
||||
// one position before where we actually should continue for the second match and later ie when from > 0
|
||||
// Therefor increase from by 1 to make the substring (midRef) calculation work
|
||||
++from;
|
||||
}
|
||||
// copy code before the var to the stream
|
||||
stream << format.midRef(from, match.capturedStart(1) - from);
|
||||
field_callback(stream, match.captured(2));
|
||||
from = match.capturedEnd()-1; // -1 because it wants to match one character before or start of line to make sure there is no backslash
|
||||
}
|
||||
if (from > 0) {
|
||||
// same reason as at the start of the loop
|
||||
++from;
|
||||
}
|
||||
stream << format.midRef(from);
|
||||
}
|
||||
|
||||
void CodeBuilder::GenCodeForExecutingQuery(QTextStream &q, const QString &query, const Pgsql::Result &result)
|
||||
{
|
||||
// %1 query string
|
||||
|
|
|
|||
|
|
@ -64,7 +64,8 @@ codebuilder/NameManglingRules.cpp \
|
|||
codebuilder/DefaultConfigs.cpp \
|
||||
codebuilder/TypeMappings.cpp \
|
||||
codebuilder/IndentationConfig.cpp \
|
||||
codebuilder/StructureTemplate.cpp
|
||||
codebuilder/StructureTemplate.cpp \
|
||||
FormatToStream.cpp
|
||||
|
||||
HEADERS += \
|
||||
Pglablib.h \
|
||||
|
|
@ -106,7 +107,8 @@ codebuilder/NameManglingRules.h \
|
|||
codebuilder/DefaultConfigs.h \
|
||||
codebuilder/TypeMappings.h \
|
||||
codebuilder/IndentationConfig.h \
|
||||
codebuilder/StructureTemplate.h
|
||||
codebuilder/StructureTemplate.h \
|
||||
FormatToStream.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue