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
|
#include "stdafx.h"
#include "QueryStr.h"
#include "StrUtils.h"
namespace sql {
static const Nat tagValue = 0x80000000;
static const Nat textValue = tagValue - 1;
static const Nat nameValue = textValue - 1;
static const Nat placeholderValue = nameValue - 1;
static const Nat autoIncrementValue = placeholderValue - 1;
static const Nat lastRowIdValue = autoIncrementValue - 1;
static Bool hasTag(Nat val) {
return (val & tagValue) != 0;
}
QueryStr::QueryStr(GcArray<Nat> *ops, GcArray<Str *> *text)
: opData(ops), textData(text) {}
QueryStr::Visitor::Visitor() {}
void QueryStr::Visitor::put(StrBuf *to, Str *text) {
*to << text;
}
void QueryStr::Visitor::name(StrBuf *to, Str *name) {
*to << S("\"") << name << S("\"");
}
void QueryStr::Visitor::placeholder(StrBuf *to) {
*to << S("?");
}
void QueryStr::Visitor::autoIncrement(StrBuf *to) {
*to << S("AUTOINCREMENT");
}
void QueryStr::Visitor::lastRowId(StrBuf *to) {
*to << S("LAST_INSERT_ID()");
}
void QueryStr::Visitor::type(StrBuf *to, QueryType type) {
*to << type;
}
Str *QueryStr::generate(Visitor *v) const {
StrBuf *out = new (this) StrBuf();
size_t textPos = 0;
for (size_t i = 0; i < opData->count; i++) {
if (opData->v[i] == textValue) {
v->put(out, textData->v[textPos++]);
} else if (opData->v[i] == nameValue) {
v->name(out, textData->v[textPos++]);
} else if (opData->v[i] == placeholderValue) {
v->placeholder(out);
} else if (opData->v[i] == autoIncrementValue) {
v->autoIncrement(out);
} else if (opData->v[i] == lastRowIdValue) {
v->lastRowId(out);
} else if (hasTag(opData->v[i])) {
Nat type = opData->v[i++] & ~tagValue;
v->type(out, QueryType(type, opData->v[i]));
} else {
v->type(out, QueryType(opData->v[i]));
}
}
return out->toS();
}
void QueryStr::toS(StrBuf *to) const {
*to << generate(new (this) Visitor());
}
/**
* The builder.
*/
QueryStrBuilder::QueryStrBuilder() {
currentStr = new (this) StrBuf();
ops = new (this) Array<Nat>();
strings = new (this) Array<Str *>();
}
void QueryStrBuilder::deepCopy(CloneEnv *env) {
cloned(currentStr, env);
cloned(ops, env);
cloned(strings, env);
}
void QueryStrBuilder::put(Str *str) {
*currentStr << str;
}
void QueryStrBuilder::quoted(Str *str) {
put(toSQLStrLiteral(str));
}
void QueryStrBuilder::flush() {
*strings << currentStr->toS();
*ops << textValue;
currentStr->clear();
}
void QueryStrBuilder::name(Str *str) {
flush();
*strings << str;
*ops << nameValue;
}
void QueryStrBuilder::placeholder() {
flush();
*ops << placeholderValue;
}
void QueryStrBuilder::autoIncrement() {
flush();
*ops << autoIncrementValue;
}
void QueryStrBuilder::lastRowId() {
flush();
*ops << lastRowIdValue;
}
void QueryStrBuilder::type(QueryType type) {
flush();
if (type.typeSize == 0) {
*ops << type.type;
} else {
*ops << (type.type | tagValue);
*ops << type.typeSize;
}
}
QueryStr *QueryStrBuilder::build() {
flush();
GcArray<Nat> *o = runtime::allocArray<Nat>(engine(), &natArrayType, ops->count());
for (Nat i = 0; i < ops->count(); i++)
o->v[i] = ops->at(i);
GcArray<Str *> *s = runtime::allocArray<Str *>(engine(), &pointerArrayType, strings->count());
for (Nat i = 0; i < strings->count(); i++)
s->v[i] = strings->at(i);
return new (this) QueryStr(o, s);
}
void QueryStrBuilder::clear() {
currentStr->clear();
ops->clear();
strings->clear();
}
}
|