Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/scripting/kexidb/kexidbschema.cpp
Show All 17 Lines | |||||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #include "kexidbschema.h" | 20 | #include "kexidbschema.h" | ||
21 | #include "kexidbfieldlist.h" | 21 | #include "kexidbfieldlist.h" | ||
22 | 22 | | |||
23 | #include <QRegularExpression> | 23 | #include <QRegularExpression> | ||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | 25 | | |||
26 | #include <KDbExpression> | ||||
27 | | ||||
26 | using namespace Scripting; | 28 | using namespace Scripting; | ||
27 | 29 | | |||
28 | /*************************************************************************** | 30 | /*************************************************************************** | ||
29 | *KexiDBSchema | 31 | *KexiDBSchema | ||
30 | */ | 32 | */ | ||
31 | 33 | | |||
32 | KexiDBSchema::KexiDBSchema(QObject* parent, const QString& name, KDbObject* schema, KDbFieldList* fieldlist, bool owner) | 34 | KexiDBSchema::KexiDBSchema(QObject* parent, const QString& name, KDbObject* schema, KDbFieldList* fieldlist, bool owner) | ||
33 | : QObject(parent) | 35 | : QObject(parent) | ||
34 | , m_schema(schema) | 36 | , m_schema(schema) | ||
35 | , m_fieldlist(fieldlist) | 37 | , m_fieldlist(fieldlist) | ||
36 | , m_owner(owner) | 38 | , m_owner(owner) | ||
37 | { | 39 | { | ||
38 | setObjectName(name); | 40 | setObjectName(name); | ||
39 | } | 41 | } | ||
40 | 42 | | |||
41 | KexiDBSchema::~KexiDBSchema() | 43 | KexiDBSchema::~KexiDBSchema() | ||
42 | { | 44 | { | ||
43 | } | 45 | } | ||
44 | 46 | | |||
45 | const QString KexiDBSchema::name() const | 47 | QString KexiDBSchema::name() const | ||
46 | { | 48 | { | ||
47 | return m_schema->name(); | 49 | return m_schema->name(); | ||
48 | } | 50 | } | ||
49 | void KexiDBSchema::setName(const QString& name) | 51 | void KexiDBSchema::setName(const QString& name) | ||
50 | { | 52 | { | ||
51 | m_schema->setName(name); | 53 | m_schema->setName(name); | ||
52 | } | 54 | } | ||
53 | const QString KexiDBSchema::caption() const | 55 | QString KexiDBSchema::caption() const | ||
54 | { | 56 | { | ||
55 | return m_schema->caption(); | 57 | return m_schema->caption(); | ||
56 | } | 58 | } | ||
57 | void KexiDBSchema::setCaption(const QString& caption) | 59 | void KexiDBSchema::setCaption(const QString& caption) | ||
58 | { | 60 | { | ||
59 | m_schema->setCaption(caption); | 61 | m_schema->setCaption(caption); | ||
60 | } | 62 | } | ||
61 | const QString KexiDBSchema::description() const | 63 | QString KexiDBSchema::description() const | ||
62 | { | 64 | { | ||
63 | return m_schema->description(); | 65 | return m_schema->description(); | ||
64 | } | 66 | } | ||
65 | void KexiDBSchema::setDescription(const QString& description) | 67 | void KexiDBSchema::setDescription(const QString& description) | ||
66 | { | 68 | { | ||
67 | m_schema->setDescription(description); | 69 | m_schema->setDescription(description); | ||
68 | } | 70 | } | ||
69 | QObject* KexiDBSchema::fieldlist() | 71 | QObject* KexiDBSchema::fieldlist() | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | 112 | if (m_owner) | |||
111 | delete queryschema(); | 113 | delete queryschema(); | ||
112 | } | 114 | } | ||
113 | 115 | | |||
114 | KDbQuerySchema* KexiDBQuerySchema::queryschema() | 116 | KDbQuerySchema* KexiDBQuerySchema::queryschema() | ||
115 | { | 117 | { | ||
116 | return static_cast< KDbQuerySchema* >(m_schema); | 118 | return static_cast< KDbQuerySchema* >(m_schema); | ||
117 | } | 119 | } | ||
118 | 120 | | |||
119 | const QString KexiDBQuerySchema::statement() const | 121 | QString KexiDBQuerySchema::statement() const | ||
staniek: const QString -> QString | |||||
120 | { | 122 | { | ||
121 | return static_cast< KDbQuerySchema* >(m_schema)->statement(); | 123 | return static_cast< KDbQuerySchema* >(m_schema)->statement().toString(); | ||
122 | } | 124 | } | ||
123 | 125 | | |||
124 | void KexiDBQuerySchema::setStatement(const QString& statement) | 126 | void KexiDBQuerySchema::setStatement(const QString& statement) | ||
125 | { | 127 | { | ||
126 | static_cast< KDbQuerySchema* >(m_schema)->setStatement(statement); | 128 | static_cast< KDbQuerySchema* >(m_schema)->setStatement(KDbEscapedString(statement)); | ||
127 | } | 129 | } | ||
128 | 130 | | |||
129 | bool KexiDBQuerySchema::setWhereExpression(const QString& whereexpression) | 131 | bool KexiDBQuerySchema::setWhereExpression(const QString& whereexpression) | ||
130 | { | 132 | { | ||
131 | KDbExpression* oldexpr = static_cast< KDbQuerySchema* >(m_schema)->whereExpression(); | | |||
132 | Q_UNUSED(oldexpr); | | |||
133 | | ||||
134 | ///@todo use KDbParser for such kind of parser-functionality. | 133 | ///@todo use KDbParser for such kind of parser-functionality. | ||
staniek: Not needed line? | |||||
135 | QString s = whereexpression; | 134 | QString s = whereexpression; | ||
136 | QRegularExpression re("[\"',]{1,1}"); | 135 | QRegularExpression re("[\"',]{1,1}"); | ||
staniek: QRegularExpression is the current API, can we use it? | |||||
Ok, ive moved this to the new API, but I dont know if ive done it correct,y. Please check thoroughly. I suspect a test case would be good here! (infact, some test scripts to check the whole KDb wrapper would be very handy) piggz: Ok, ive moved this to the new API, but I dont know if ive done it correct,y. Please check… | |||||
137 | while (true) { | 136 | while (true) { | ||
138 | s.remove(QRegularExpression("^[\\s,]+")); | 137 | s.remove(QRegularExpression("^[\\s,]+")); | ||
139 | int pos = s.indexOf('='); | 138 | int pos = s.indexOf('='); | ||
140 | if (pos < 0) break; | 139 | if (pos < 0) break; | ||
141 | QString key = s.left(pos).trimmed(); | 140 | QString key = s.left(pos).trimmed(); | ||
142 | s = s.mid(pos + 1).trimmed(); | 141 | s = s.mid(pos + 1).trimmed(); | ||
143 | 142 | | |||
144 | QString value; | 143 | QString value; | ||
144 | QRegularExpressionMatch match = re.match(s); | ||||
145 | int sp = s.indexOf(re); | 145 | int sp = s.indexOf(re); | ||
146 | if (sp >= 0) { | 146 | | ||
147 | if (re.cap(0) == ",") { | 147 | if (match.hasMatch()) { | ||
148 | if (match.captured(1) == ",") { | ||||
148 | value = s.left(sp).trimmed(); | 149 | value = s.left(sp).trimmed(); | ||
149 | s = s.mid(sp + 1).trimmed(); | 150 | s = s.mid(sp + 1).trimmed(); | ||
150 | } else { | 151 | } else { | ||
151 | int ep = s.indexOf(re.cap(0), sp + 1); | 152 | int ep = s.indexOf(match.captured(1), sp + 1); | ||
152 | value = s.mid(sp + 1, ep - 1); | 153 | value = s.mid(sp + 1, ep - 1); | ||
153 | s = s.mid(ep + 1); | 154 | s = s.mid(ep + 1); | ||
154 | } | 155 | } | ||
155 | } else { | 156 | } else { | ||
156 | value = s; | 157 | value = s; | ||
157 | s.clear(); | 158 | s.clear(); | ||
158 | } | 159 | } | ||
159 | 160 | | |||
160 | KDbField* field = static_cast< KDbQuerySchema* >(m_schema)->field(key); | 161 | KDbField* field = static_cast< KDbQuerySchema* >(m_schema)->field(key); | ||
161 | if (! field) { | 162 | if (! field) { | ||
162 | qWarning() << QString("Invalid WHERE-expression: Field \"%1\" does not exists in tableschema \"%2\".").arg(key).arg(m_schema->name()); | 163 | qWarning() << QString("Invalid WHERE-expression: Field \"%1\" does not exists in tableschema \"%2\".").arg(key).arg(m_schema->name()); | ||
163 | return false; | 164 | return false; | ||
164 | } | 165 | } | ||
165 | 166 | | |||
166 | QVariant v(value); | 167 | QVariant v(value); | ||
167 | if (! v.convert(field->variantType())) { | 168 | if (! v.convert(field->variantType())) { | ||
168 | qWarning() << QString("Invalid WHERE-expression: The for Field \"%1\" defined value is of type \"%2\" rather then the expected type \"%3\"").arg(key).arg(v.typeName()).arg(field->variantType()); | 169 | qWarning() << QString("Invalid WHERE-expression: The for Field \"%1\" defined value is of type \"%2\" rather then the expected type \"%3\"").arg(key).arg(v.typeName()).arg(field->variantType()); | ||
169 | return false; | 170 | return false; | ||
170 | } | 171 | } | ||
171 | 172 | | |||
172 | QString errorMessage; | 173 | QString errorMessage; | ||
173 | QString errorDescription; | 174 | QString errorDescription; | ||
174 | if (!static_cast<KDbQuerySchema *>(m_schema)->addToWhereExpression( | 175 | if (!static_cast<KDbQuerySchema *>(m_schema)->addToWhereExpression(field, v, KDbToken('='), &errorMessage, &errorDescription)) | ||
175 | field, v, &errorMessage, &errorDescription)) | | |||
176 | { | 176 | { | ||
177 | qWarning() << "addToWhereExpression() failed, message=" << errorMessage | 177 | qWarning() << "addToWhereExpression() failed, message=" << errorMessage | ||
178 | << "description=" << errorDescription; | 178 | << "description=" << errorDescription; | ||
179 | return false; | 179 | return false; | ||
180 | } | 180 | } | ||
181 | } | 181 | } | ||
182 | return true; | 182 | return true; | ||
183 | } | 183 | } | ||
184 | 184 | |
const QString -> QString