-
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathDsqlCompilerScratch.h
More file actions
405 lines (348 loc) · 13.1 KB
/
Copy pathDsqlCompilerScratch.h
File metadata and controls
405 lines (348 loc) · 13.1 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
* Adriano dos Santos Fernandes
*/
#ifndef DSQL_COMPILER_SCRATCH_H
#define DSQL_COMPILER_SCRATCH_H
#include "../jrd/jrd.h"
#include "../dsql/dsql.h"
#include "../dsql/DsqlStatements.h"
#include "../dsql/BlrDebugWriter.h"
#include "../common/classes/array.h"
#include "../jrd/MetaName.h"
#include "../common/classes/stack.h"
#include "../common/classes/alloc.h"
#include <initializer_list>
#include <optional>
#include <variant>
namespace Jrd
{
class BinaryBoolNode;
class LocalDeclarationsNode;
class DeclareCursorNode;
class DeclareLocalTableNode;
class DeclareVariableNode;
class GroupingSpec;
class ParameterClause;
class RseNode;
class SelectExprNode;
class StmtNode;
class TypeClause;
class ValueExprNode;
class VariableNode;
class WithClause;
typedef Firebird::Pair<
Firebird::NonPooled<NestConst<ValueListNode>, NestConst<ValueListNode>>> ReturningClause;
// DSQL Compiler scratch block.
// Contains any kind of objects used during DsqlStatement compilation
// Is deleted with its pool as soon as DsqlStatement is fully formed in prepareStatement()
// or with the statement itself (if the statement reqested it returning true from shouldPreserveScratch())
class DsqlCompilerScratch : public BlrDebugWriter
{
public:
static const unsigned FLAG_IN_AUTO_TRANS_BLOCK = 0x0001;
static const unsigned FLAG_RETURNING_INTO = 0x0002;
static const unsigned FLAG_METADATA_SAVED = 0x0004;
static const unsigned FLAG_PROCEDURE = 0x0008;
static const unsigned FLAG_TRIGGER = 0x0010;
static const unsigned FLAG_BLOCK = 0x0020;
static const unsigned FLAG_RECURSIVE_CTE = 0x0040;
static const unsigned FLAG_UPDATE_OR_INSERT = 0x0080;
static const unsigned FLAG_FUNCTION = 0x0200;
static const unsigned FLAG_SUB_ROUTINE = 0x0400;
static const unsigned FLAG_INTERNAL_REQUEST = 0x0800;
static const unsigned FLAG_AMBIGUOUS_STMT = 0x1000;
static const unsigned FLAG_DDL = 0x2000;
static const unsigned FLAG_FETCH = 0x4000;
static const unsigned FLAG_VIEW_WITH_CHECK = 0x8000;
static const unsigned FLAG_EXEC_BLOCK = 0x010000;
static const unsigned FLAG_ALLOW_CREATED_LTT_REFERENCE = 0x020000;
static const unsigned FLAG_USING_STATEMENT = 0x040000;
static const unsigned FLAG_ACTUAL_LTT_DDL = 0x080000;
static const unsigned MAX_NESTING = 512;
public:
DsqlCompilerScratch(MemoryPool& p, dsql_dbb* aDbb, jrd_tra* aTransaction,
DsqlStatement* aDsqlStatement = nullptr, DsqlCompilerScratch* aMainScratch = nullptr)
: BlrDebugWriter(p),
dbb(aDbb),
transaction(aTransaction),
dsqlStatement(aDsqlStatement),
mainContext(p),
context(&mainContext),
unionContext(p),
derivedContext(p),
labels(p),
cursors(p),
localTables(p),
aliasRelationPrefix(p),
package(p),
currCtes(p),
hiddenVariables(p),
variables(p),
outputVariables(p),
mainScratch(aMainScratch),
outerMessagesMap(p),
outerVarsMap(p),
ddlSchema(p),
ctes(p),
cteAliases(p),
subFunctions(p),
subProcedures(p),
procedures(p),
functions(p)
{
}
protected:
// DsqlCompilerScratch should never be destroyed using delete.
// It dies together with it's pool.
~DsqlCompilerScratch()
{
}
public:
#ifdef DSQL_DEBUG
static void dumpContextStack(const DsqlContextStack* stack);
#endif
public:
virtual bool isVersion4()
{
return dsqlStatement->getBlrVersion() == 4;
}
MemoryPool& getPool()
{
return PermanentStorage::getPool();
}
dsql_dbb* getAttachment()
{
return dbb;
}
jrd_tra* getTransaction()
{
return transaction;
}
void setTransaction(jrd_tra* value)
{
transaction = value;
}
DsqlStatement* getDsqlStatement() const
{
return dsqlStatement;
}
void setDsqlStatement(DsqlStatement* aDsqlStatement)
{
dsqlStatement = aDsqlStatement;
}
void qualifyNewName(QualifiedName& name) const;
void qualifyExistingName(QualifiedName& name, std::initializer_list<ObjectType> objectTypes);
void qualifyExistingName(QualifiedName& name, ObjectType objectType)
{
qualifyExistingName(name, {objectType});
}
std::variant<std::monostate, dsql_prc*, dsql_rel*, dsql_udf*> resolveRoutineOrRelation(QualifiedName& name,
std::initializer_list<ObjectType> objectTypes);
void putBlrMarkers(ULONG marks);
void putType(const dsql_fld* field, bool useSubType);
// * Generate TypeClause blr and put it to this Scratch
// Depends on: typeOfName, typeOfTable and schema:
// blr_column_name3/blr_domain_name3 for field with schema
// blr_column_name2/blr_domain_name2 for explicit collate
// blr_column_name/blr_domain_name for regular field
void putType(const TypeClause* type, bool useSubType);
void putLocalVariableDecl(dsql_var* variable, DeclareVariableNode* hostParam, QualifiedName& collationName);
void putLocalVariableInit(dsql_var* variable, const DeclareVariableNode* hostParam);
void putLocalVariable(dsql_var* variable)
{
QualifiedName dummyCollationName;
putLocalVariableDecl(variable, nullptr, dummyCollationName);
putLocalVariableInit(variable, nullptr);
}
void putOuterMaps();
dsql_var* makeVariable(dsql_fld*, const char*, const dsql_var::Type type, USHORT,
USHORT, std::optional<USHORT> = std::nullopt);
dsql_var* resolveVariable(const MetaName& varName);
void genReturn(bool eosFlag = false);
void genParameters(Firebird::Array<NestConst<ParameterClause> >& parameters,
Firebird::Array<NestConst<ParameterClause> >& returns);
void compileAggregateFunction(Firebird::Array<NestConst<ParameterClause> >& parameters,
ParameterClause* returnParameter, NestConst<LocalDeclarationsNode>& localDeclList,
NestConst<StmtNode>& aggregateOnStartBody, NestConst<StmtNode>& aggregateOnAccumulateBody,
NestConst<StmtNode>& aggregateOnGroupBody, NestConst<StmtNode>& aggregateOnFinishBody,
bool reserveInitialReturnVarNumber);
// Get rid of any predefined contexts created for a view or trigger definition.
// Also reset hidden variables.
void resetContextStack()
{
context->clear();
contextNumber = 0;
derivedContextNumber = 0;
nextVarNumber = 0;
hiddenVariables.clear();
}
void resetTriggerContextStack()
{
context->clear();
contextNumber = 0;
}
void addCTEs(WithClause* withClause);
SelectExprNode* findCTE(const MetaName& name);
void clearCTEs();
void checkUnusedCTEs();
void addCTEAlias(const Firebird::string& alias);
const Firebird::string* getNextCTEAlias()
{
return *(--currCteAlias);
}
void resetCTEAlias(const Firebird::string& alias)
{
#ifdef DEV_BUILD
const Firebird::string* const* begin = cteAliases.begin();
#endif
currCteAlias = cteAliases.end() - 1;
fb_assert(currCteAlias >= begin);
const Firebird::string* curr = *(currCteAlias);
while (strcmp(curr->c_str(), alias.c_str()))
{
currCteAlias--;
fb_assert(currCteAlias >= begin);
curr = *(currCteAlias);
}
}
USHORT reserveVarNumber()
{
return nextVarNumber++;
}
void reserveInitialVarNumbers(USHORT count)
{
fb_assert(nextVarNumber == 0);
nextVarNumber = count;
}
bool isPsql() const { return psql; }
void setPsql(bool value) { psql = value; }
const auto& getSubFunctions() const
{
return subFunctions;
}
DeclareSubFuncNode* getSubFunction(const MetaName& name);
void putSubFunction(DeclareSubFuncNode* subFunc, bool replace = false);
const auto& getSubProcedures() const
{
return subProcedures;
}
DeclareSubProcNode* getSubProcedure(const MetaName& name);
void putSubProcedure(DeclareSubProcNode* subProc, bool replace = false);
ValueExprNode* makeGroupingValue(ValueExprNode* node);
private:
SelectExprNode* pass1RecursiveCte(SelectExprNode* input);
RseNode* pass1RseIsRecursive(RseNode* input);
bool pass1RelProcIsRecursive(RecordSourceNode* input);
BoolExprNode* pass1JoinIsRecursive(RecordSourceNode*& input);
void putType(const TypeClause& type, bool useSubType, bool useExplicitCollate);
template<bool THasTableName>
void putTypeName(const TypeClause& type, const bool useExplicitCollate);
void putDtype(const TypeClause& type, const bool useSubType);
dsql_dbb* dbb = nullptr; // DSQL attachment
jrd_tra* transaction = nullptr; // Transaction
DsqlStatement* dsqlStatement = nullptr; // DSQL statement
public:
unsigned flags = 0; // flags
unsigned nestingLevel = 0; // begin...end nesting level
dsql_rel* relation = nullptr; // relation created by this request (for DDL)
DsqlContextStack mainContext;
DsqlContextStack* context = nullptr;
DsqlContextStack unionContext; // Save contexts for views of unions
DsqlContextStack derivedContext; // Save contexts for views of derived tables
dsql_ctx* outerAggContext = nullptr; // agg context for outer ref
// CVC: I think the two contexts may need a bigger var, too.
USHORT contextNumber = 0; // Next available context number
USHORT derivedContextNumber = 0; // Next available context number for derived tables
USHORT scopeLevel = 0; // Scope level for parsing aliases in subqueries
USHORT loopLevel = 0; // Loop level
Firebird::Stack<MetaName*> labels; // Loop labels
USHORT cursorNumber = 0; // Cursor number
Firebird::Array<DeclareCursorNode*> cursors; // Cursors
USHORT localTableNumber = 0; // Local table number
Firebird::Array<DeclareLocalTableNode*> localTables; // Local tables
USHORT inSelectList = 0; // now processing "select list"
USHORT inWhereClause = 0; // processing "where clause"
USHORT inGroupByClause = 0; // processing "group by clause"
USHORT inHavingClause = 0; // processing "having clause"
USHORT inOrderByClause = 0; // processing "order by clause"
USHORT inAggregateFunction = 0; // processing aggregate function arguments
GroupingSpec* activeGroupingSpec = nullptr; // grouping set being lowered
const Firebird::SortedArray<unsigned>* activeGroupingSet = nullptr;
ValueListNode* activeGroupingSelectList = nullptr;
USHORT activeGroupingScopeLevel = 0;
USHORT errorHandlers = 0; // count of active error handlers
USHORT clientDialect = 0; // dialect passed into the API call
USHORT inOuterJoin = 0; // processing inside outer-join part
Firebird::ObjectsArray<QualifiedName> aliasRelationPrefix; // prefix for every relation-alias.
QualifiedName package; // package being defined
Firebird::Stack<SelectExprNode*> currCtes; // current processing CTE's
dsql_ctx* recursiveCtx = nullptr; // context of recursive CTE
USHORT recursiveCtxId = 0; // id of recursive union stream context
bool processingWindow = false; // processing window functions
bool checkConstraintTrigger = false; // compiling a check constraint trigger
bool aggregatePhaseReturn = false; // aggregate section return is phase-local
std::optional<AggregateFunctionPhase> aggregatePhase; // aggregate section being compiled
USHORT aggregatePhaseLabel = 0; // label used by aggregate phase-local RETURN
dsc domainValue; // VALUE in the context of domain's check constraint
Firebird::Array<dsql_var*> hiddenVariables; // hidden variables
Firebird::Array<dsql_var*> variables;
Firebird::Array<dsql_var*> outputVariables;
ReturningClause* returningClause = nullptr;
const Firebird::string* const* currCteAlias = nullptr;
DsqlCompilerScratch* mainScratch = nullptr;
Firebird::NonPooledMap<USHORT, USHORT> outerMessagesMap; // <outer, inner>
Firebird::NonPooledMap<USHORT, USHORT> outerVarsMap; // <outer, inner>
MetaName ddlSchema;
Firebird::AutoPtr<Firebird::ObjectsArray<Firebird::MetaString>> cachedDdlSchemaSearchPath;
dsql_msg* recordKeyMessage = nullptr; // Side message for positioned DML
private:
Firebird::HalfStaticArray<SelectExprNode*, 4> ctes; // common table expressions
Firebird::HalfStaticArray<const Firebird::string*, 4> cteAliases; // CTE aliases in recursive members
USHORT nextVarNumber = 0; // Next available variable number
bool psql = false;
Firebird::LeftPooledMap<MetaName, DeclareSubFuncNode*> subFunctions;
Firebird::LeftPooledMap<MetaName, DeclareSubProcNode*> subProcedures;
public:
Firebird::LeftPooledMap<QualifiedName, class dsql_prc*> procedures; // known procedures
Firebird::LeftPooledMap<QualifiedName, class dsql_udf*> functions; // known functions
bool regularCacheValid = false; // flag for relations cache
};
class PsqlChanger
{
public:
PsqlChanger(DsqlCompilerScratch* aDsqlScratch, bool value)
: dsqlScratch(aDsqlScratch),
oldValue(dsqlScratch->isPsql())
{
dsqlScratch->setPsql(value);
}
~PsqlChanger()
{
dsqlScratch->setPsql(oldValue);
}
private:
// copying is prohibited
PsqlChanger(const PsqlChanger&);
PsqlChanger& operator =(const PsqlChanger&);
DsqlCompilerScratch* dsqlScratch;
const bool oldValue;
};
} // namespace Jrd
#endif // DSQL_COMPILER_SCRATCH_H