forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdbQueryBuilder.cpp
More file actions
2852 lines (2465 loc) · 84.6 KB
/
NdbQueryBuilder.cpp
File metadata and controls
2852 lines (2465 loc) · 84.6 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <NdbDictionary.hpp>
#include "NdbIndexScanOperation.hpp"
#include "NdbQueryBuilder.hpp"
#include "NdbQueryBuilderImpl.hpp"
#include <ndb_global.h>
#include <Vector.hpp>
#include "signaldata/QueryTree.hpp"
#include "NdbDictionaryImpl.hpp"
#include <NdbRecord.hpp>
#include "AttributeHeader.hpp"
#include "NdbOut.hpp"
#include <NdbInterpretedCode.hpp>
/**
* Implementation of all QueryBuilder objects are hidden from
* both the API interface and other internals in the NDBAPI using the
* pimpl idiom.
*
* The object hierarch visible through the interface has its 'Impl'
* counterparts inside this module. Some classes are
* even subclassed further as part of the implementation.
* (Particular the ConstOperant in order to implement multiple datatypes)
*
* In order to avoid allocating both an interface object and its particular
* Impl object, all 'final' Impl objects 'has a' interface object
* which is accessible through the virtual method ::getInterface.
*
* ::getImpl() methods has been defined for convenient access
* to all available Impl classes.
*
*/
// For debugging purposes. Enable to print query tree graph to ndbout.
static const bool doPrintQueryTree = false;
/* Various error codes that are not specific to NdbQuery. */
static const int Err_MemoryAlloc = 4000;
static const int Err_FinaliseNotCalled = 4519;
static void
setErrorCode(NdbQueryBuilderImpl* qb, int aErrorCode)
{ qb->setErrorCode(aErrorCode);
}
static void
setErrorCode(NdbQueryBuilder* qb, int aErrorCode)
{ qb->getImpl().setErrorCode(aErrorCode);
}
//////////////////////////////////////////////////////////////////
// Convenient macro for returning specific errorcodes if
// 'cond' does not evaluate to true.
//////////////////////////////////////////////////////////////////
#define returnErrIf(cond,err) \
if (unlikely((cond))) \
{ ::setErrorCode(this,err); \
return NULL; \
}
//////////////////////////////////////////////
// Implementation of NdbQueryOperand interface
//
// The common baseclass 'class NdbQueryOperandImpl',
// and its 'const', 'linked' and 'param' subclasses are
// defined in "NdbQueryBuilderImpl.hpp"
// The 'const' operand subclass is a pure virtual baseclass
// which has different type specific subclasses defined below:
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Implements different const datatypes by further
// subclassing of the baseclass NdbConstOperandImpl.
//////////////////////////////////////////////////
class NdbInt64ConstOperandImpl : public NdbConstOperandImpl
{
public:
explicit NdbInt64ConstOperandImpl (Int64 value) :
NdbConstOperandImpl(),
m_value(value) {}
private:
int convertInt8();
int convertUint8();
int convertInt16();
int convertUint16();
int convertInt24();
int convertUint24();
int convertInt32();
int convertUint32();
int convertInt64();
int convertUint64();
const Int64 m_value;
};
class NdbDoubleConstOperandImpl : public NdbConstOperandImpl
{
public:
explicit NdbDoubleConstOperandImpl (double value) :
NdbConstOperandImpl(),
m_value(value) {}
private:
int convertDouble();
int convertFloat();
private:
const double m_value;
};
class NdbCharConstOperandImpl : public NdbConstOperandImpl
{
public:
explicit NdbCharConstOperandImpl (const char* value) :
NdbConstOperandImpl(), m_value(value) {}
private:
int convertChar();
int convertVChar();
//int convertLVChar();
private:
const char* const m_value;
};
class NdbGenericConstOperandImpl : public NdbConstOperandImpl
{
public:
explicit NdbGenericConstOperandImpl (const void* value,
Uint32 len)
: NdbConstOperandImpl(),
m_value(value),
m_len(len)
{}
private:
int convert2ColumnType();
const void* const m_value;
const Uint32 m_len;
};
////////////////////////////////////////////////
// Implementation of NdbQueryOperation interface
////////////////////////////////////////////////
// Common Baseclass 'class NdbQueryOperationDefImpl' is
// defined in "NdbQueryBuilderImpl.hpp"
class NdbQueryLookupOperationDefImpl : public NdbQueryOperationDefImpl
{
friend class NdbQueryBuilder; // Allow privat access from builder interface
public:
virtual const NdbQueryOperandImpl* const* getKeyOperands() const
{ return m_keys; }
protected:
explicit NdbQueryLookupOperationDefImpl (
const NdbTableImpl& table,
const NdbQueryOperand* const keys[],
const NdbQueryOptionsImpl& options,
const char* ident,
Uint32 ix,
int& error);
virtual const NdbQueryLookupOperationDef& getInterface() const
{ return m_interface; }
// Append pattern for creating lookup key to serialized code
Uint32 appendKeyPattern(Uint32Buffer& serializedDef) const;
virtual bool isScanOperation() const
{ return false; }
protected:
NdbQueryLookupOperationDef m_interface;
NdbQueryOperandImpl* m_keys[MAX_ATTRIBUTES_IN_INDEX+1];
}; // class NdbQueryLookupOperationDefImpl
class NdbQueryPKLookupOperationDefImpl : public NdbQueryLookupOperationDefImpl
{
friend class NdbQueryBuilder; // Allow privat access from builder interface
public:
virtual int serializeOperation(Uint32Buffer& serializedDef);
private:
explicit NdbQueryPKLookupOperationDefImpl (
const NdbTableImpl& table,
const NdbQueryOperand* const keys[],
const NdbQueryOptionsImpl& options,
const char* ident,
Uint32 ix,
int& error)
: NdbQueryLookupOperationDefImpl(table,keys,options,ident,ix,error)
{}
virtual NdbQueryOperationDef::Type getType() const
{ return NdbQueryOperationDef::PrimaryKeyAccess; }
}; // class NdbQueryPKLookupOperationDefImpl
class NdbQueryIndexOperationDefImpl : public NdbQueryLookupOperationDefImpl
{
friend class NdbQueryBuilder; // Allow privat access from builder interface
public:
virtual const NdbIndexImpl* getIndex() const
{ return &m_index; }
virtual int serializeOperation(Uint32Buffer& serializedDef);
private:
explicit NdbQueryIndexOperationDefImpl (
const NdbIndexImpl& index,
const NdbTableImpl& table,
const NdbQueryOperand* const keys[],
const NdbQueryOptionsImpl& options,
const char* ident,
Uint32 ix,
int& error)
: NdbQueryLookupOperationDefImpl(table,keys,options,ident,ix,error),
m_index(index)
{}
virtual NdbQueryOperationDef::Type getType() const
{ return NdbQueryOperationDef::UniqueIndexAccess; }
private:
const NdbIndexImpl& m_index;
}; // class NdbQueryIndexOperationDefImpl
class NdbQueryTableScanOperationDefImpl : public NdbQueryScanOperationDefImpl
{
friend class NdbQueryBuilder; // Allow privat access from builder interface
public:
virtual int serializeOperation(Uint32Buffer& serializedDef);
virtual const NdbQueryTableScanOperationDef& getInterface() const
{ return m_interface; }
virtual NdbQueryOperationDef::Type getType() const
{ return NdbQueryOperationDef::TableScan; }
private:
explicit NdbQueryTableScanOperationDefImpl (
const NdbTableImpl& table,
const NdbQueryOptionsImpl& options,
const char* ident,
Uint32 ix,
int& error)
: NdbQueryScanOperationDefImpl(table,options,ident,ix,error),
m_interface(*this)
{}
NdbQueryTableScanOperationDef m_interface;
}; // class NdbQueryTableScanOperationDefImpl
///////////////////////////////////////////////////
/////// End 'Impl' class declarations /////////////
///////////////////////////////////////////////////
NdbQueryDef::NdbQueryDef(NdbQueryDefImpl& impl) : m_impl(impl)
{}
NdbQueryDef::~NdbQueryDef()
{}
Uint32
NdbQueryDef::getNoOfOperations() const
{ return m_impl.getNoOfOperations();
}
const NdbQueryOperationDef*
NdbQueryDef::getQueryOperation(Uint32 index) const
{ return &m_impl.getQueryOperation(index).getInterface();
}
const NdbQueryOperationDef*
NdbQueryDef::getQueryOperation(const char* ident) const
{ const NdbQueryOperationDefImpl *opDef = m_impl.getQueryOperation(ident);
return (opDef!=NULL) ? &opDef->getInterface() : NULL;
}
bool
NdbQueryDef::isScanQuery() const
{ return m_impl.isScanQuery();
}
NdbQueryDef::QueryType
NdbQueryDef::getQueryType() const
{ return m_impl.getQueryType();
}
NdbQueryDefImpl&
NdbQueryDef::getImpl() const{
return m_impl;
}
void
NdbQueryDef::destroy() const
{
delete &getImpl();
}
void
NdbQueryDef::print() const
{
m_impl.getQueryOperation(0U).printTree(0, Bitmask<(NDB_SPJ_MAX_TREE_NODES+31)/32>());
}
/*************************************************************************
* Glue layer between NdbQueryOperand interface and its Impl'ementation.
************************************************************************/
NdbQueryOperand::NdbQueryOperand(NdbQueryOperandImpl& impl) : m_impl(impl)
{}
NdbConstOperand::NdbConstOperand(NdbQueryOperandImpl& impl) : NdbQueryOperand(impl)
{}
NdbParamOperand::NdbParamOperand(NdbQueryOperandImpl& impl) : NdbQueryOperand(impl)
{}
NdbLinkedOperand::NdbLinkedOperand(NdbQueryOperandImpl& impl) : NdbQueryOperand(impl)
{}
// D'tors
NdbQueryOperand::~NdbQueryOperand()
{}
NdbConstOperand::~NdbConstOperand()
{}
NdbParamOperand::~NdbParamOperand()
{}
NdbLinkedOperand::~NdbLinkedOperand()
{}
/**
* Get'ers for NdbQueryOperand...Impl object.
* Functions overridden to supply 'impl' casted to the correct '...OperandImpl' type
* for each available interface class.
*/
inline NdbQueryOperandImpl&
NdbQueryOperand::getImpl() const
{ return m_impl;
}
inline static
NdbQueryOperandImpl& getImpl(const NdbQueryOperand& op)
{ return op.getImpl();
}
inline static
NdbConstOperandImpl& getImpl(const NdbConstOperand& op)
{ return static_cast<NdbConstOperandImpl&>(op.getImpl());
}
inline static
NdbParamOperandImpl& getImpl(const NdbParamOperand& op)
{ return static_cast<NdbParamOperandImpl&>(op.getImpl());
}
inline static
NdbLinkedOperandImpl& getImpl(const NdbLinkedOperand& op)
{ return static_cast<NdbLinkedOperandImpl&>(op.getImpl());
}
/**
* BEWARE: Return 'NULL' Until the operand has been bound to an operation.
*/
const NdbDictionary::Column*
NdbQueryOperand::getColumn() const
{
return ::getImpl(*this).getColumn();
}
const char*
NdbParamOperand::getName() const
{
return ::getImpl(*this).getName();
}
Uint32
NdbParamOperand::getEnum() const
{
return ::getImpl(*this).getParamIx();
}
/****************************************************************************
* Implementation of NdbQueryOptions.
* Because of the simplicity of this class, the implementation has *not*
* been split into methods in the Impl class with a glue layer to the
* outside interface.
****************************************************************************/
static NdbQueryOptionsImpl defaultOptions;
NdbQueryOptions::NdbQueryOptions()
: m_pimpl(&defaultOptions)
{}
NdbQueryOptions::~NdbQueryOptions()
{
if (m_pimpl!=&defaultOptions)
delete m_pimpl;
}
const NdbQueryOptionsImpl&
NdbQueryOptions::getImpl() const
{
return *m_pimpl;
}
int
NdbQueryOptions::setOrdering(ScanOrdering ordering)
{
if (m_pimpl==&defaultOptions)
{
m_pimpl = new NdbQueryOptionsImpl;
if (unlikely(m_pimpl==0))
{
return Err_MemoryAlloc;
}
}
m_pimpl->m_scanOrder = ordering;
return 0;
}
int
NdbQueryOptions::setMatchType(MatchType matchType)
{
if (m_pimpl==&defaultOptions)
{
m_pimpl = new NdbQueryOptionsImpl;
if (unlikely(m_pimpl==0))
{
return Err_MemoryAlloc;
}
}
m_pimpl->m_matchType = matchType;
return 0;
}
int
NdbQueryOptions::setParent(const NdbQueryOperationDef* parent)
{
if (m_pimpl==&defaultOptions)
{
m_pimpl = new NdbQueryOptionsImpl;
if (unlikely(m_pimpl==0))
{
return Err_MemoryAlloc;
}
}
m_pimpl->m_parent = &parent->getImpl();
return 0;
}
int
NdbQueryOptions::setInterpretedCode(const NdbInterpretedCode& code)
{
if (m_pimpl==&defaultOptions)
{
m_pimpl = new NdbQueryOptionsImpl;
if (unlikely(m_pimpl==0))
{
return Err_MemoryAlloc;
}
}
return m_pimpl->copyInterpretedCode(code);
}
NdbQueryOptionsImpl::~NdbQueryOptionsImpl()
{
delete m_interpretedCode;
}
NdbQueryOptionsImpl::NdbQueryOptionsImpl(const NdbQueryOptionsImpl& src)
: m_matchType(src.m_matchType),
m_scanOrder(src.m_scanOrder),
m_parent(src.m_parent),
m_interpretedCode(NULL)
{
if (src.m_interpretedCode)
{
copyInterpretedCode(*src.m_interpretedCode);
}
}
/*
* Make a deep copy, such that 'src' can be destroyed when this method
* returns.
*/
int
NdbQueryOptionsImpl::copyInterpretedCode(const NdbInterpretedCode& src)
{
/* Check the program's finalised */
if (unlikely(!(src.m_flags & NdbInterpretedCode::Finalised)))
{
return Err_FinaliseNotCalled; // NdbInterpretedCode::finalise() not called.
}
if (src.m_instructions_length == 0)
{
return 0;
}
NdbInterpretedCode* interpretedCode = new NdbInterpretedCode();
if (unlikely(interpretedCode==NULL))
{
return Err_MemoryAlloc;
}
const int error = interpretedCode->copy(src);
if (unlikely(error))
{
delete interpretedCode;
return error;
}
/* Replace existing NdbInterpretedCode */
if (m_interpretedCode)
delete m_interpretedCode;
m_interpretedCode = interpretedCode;
return 0;
}
/****************************************************************************
* Glue layer between NdbQueryOperationDef interface and its Impl'ementation.
****************************************************************************/
NdbQueryOperationDef::NdbQueryOperationDef(NdbQueryOperationDefImpl& impl) : m_impl(impl)
{}
NdbQueryLookupOperationDef::NdbQueryLookupOperationDef(NdbQueryOperationDefImpl& impl) : NdbQueryOperationDef(impl)
{}
NdbQueryScanOperationDef::NdbQueryScanOperationDef(NdbQueryOperationDefImpl& impl) : NdbQueryOperationDef(impl)
{}
NdbQueryTableScanOperationDef::NdbQueryTableScanOperationDef(NdbQueryOperationDefImpl& impl) : NdbQueryScanOperationDef(impl)
{}
NdbQueryIndexScanOperationDef::NdbQueryIndexScanOperationDef(NdbQueryOperationDefImpl& impl) : NdbQueryScanOperationDef(impl)
{}
// D'tors
NdbQueryOperationDef::~NdbQueryOperationDef()
{}
NdbQueryLookupOperationDef::~NdbQueryLookupOperationDef()
{}
NdbQueryScanOperationDef::~NdbQueryScanOperationDef()
{}
NdbQueryTableScanOperationDef::~NdbQueryTableScanOperationDef()
{}
NdbQueryIndexScanOperationDef::~NdbQueryIndexScanOperationDef()
{}
NdbQueryOperationDefImpl::~NdbQueryOperationDefImpl()
{
// Unlink any parent and child refering this object
if (m_parent != NULL)
{
m_parent->removeChild(this);
}
for (Uint32 i = 0; i<m_children.size(); i++)
{
assert(m_children[i]->m_parent == this);
m_children[i]->m_parent = NULL;
}
}
/**
* Get'ers for QueryOperation...DefImpl object.
* Functions overridden to supply 'impl' casted to the correct '...DefImpl' type
* for each available interface class.
*/
NdbQueryOperationDefImpl&
NdbQueryOperationDef::getImpl() const
{ return m_impl;
}
inline static
NdbQueryOperationDefImpl& getImpl(const NdbQueryOperationDef& op)
{ return op.getImpl();
}
inline static
NdbQueryLookupOperationDefImpl& getImpl(const NdbQueryLookupOperationDef& op)
{ return static_cast<NdbQueryLookupOperationDefImpl&>(op.getImpl());
}
inline static
NdbQueryTableScanOperationDefImpl& getImpl(const NdbQueryTableScanOperationDef& op)
{ return static_cast<NdbQueryTableScanOperationDefImpl&>(op.getImpl());
}
inline static
NdbQueryIndexScanOperationDefImpl& getImpl(const NdbQueryIndexScanOperationDef& op)
{ return static_cast<NdbQueryIndexScanOperationDefImpl&>(op.getImpl());
}
Uint32
NdbQueryOperationDef::getQueryOperationIx() const
{
return ::getImpl(*this).getQueryOperationIx();
}
Uint32
NdbQueryOperationDef::getNoOfParentOperations() const
{
return ::getImpl(*this).getNoOfParentOperations();
}
const NdbQueryOperationDef*
NdbQueryOperationDef::getParentOperation(Uint32 i) const
{
return &::getImpl(*this).getParentOperation(i).getInterface();
}
Uint32
NdbQueryOperationDef::getNoOfChildOperations() const
{
return ::getImpl(*this).getNoOfChildOperations();
}
const NdbQueryOperationDef*
NdbQueryOperationDef::getChildOperation(Uint32 i) const
{
return &::getImpl(*this).getChildOperation(i).getInterface();
}
const char*
NdbQueryOperationDef::getTypeName(Type type)
{
switch(type)
{
case PrimaryKeyAccess:
return "PrimaryKeyAccess";
case UniqueIndexAccess:
return "UniqueIndexAccess";
case TableScan:
return "TableScan";
case OrderedIndexScan:
return "OrderedIndexScan";
default:
return "<Invalid NdbQueryOperationDef::Type value>";
}
}
NdbQueryOperationDef::Type
NdbQueryOperationDef::getType() const
{
return ::getImpl(*this).getType();
}
const NdbDictionary::Table*
NdbQueryOperationDef::getTable() const
{
return &::getImpl(*this).getTable();
}
const NdbDictionary::Index*
NdbQueryOperationDef::getIndex() const
{
return ::getImpl(*this).getIndex();
}
/*******************************************
* Implementation of NdbQueryBuilder factory
******************************************/
// Static method.
NdbQueryBuilder* NdbQueryBuilder::create()
{
NdbQueryBuilderImpl* const impl = new NdbQueryBuilderImpl();
if (likely (impl != NULL))
{
if (likely(impl->getNdbError().code == 0))
{
return &impl->m_interface;
}
else
{
// Probably failed to create Vector instances.
assert(impl->getNdbError().code == Err_MemoryAlloc);
delete impl;
return NULL;
}
}
else
{
return NULL;
}
}
void NdbQueryBuilder::destroy()
{
delete &m_impl;
}
NdbQueryBuilder::NdbQueryBuilder(NdbQueryBuilderImpl& impl)
: m_impl(impl)
{}
NdbQueryBuilder::~NdbQueryBuilder()
{}
inline NdbQueryBuilderImpl&
NdbQueryBuilder::getImpl() const
{ return m_impl;
}
const NdbError&
NdbQueryBuilder::getNdbError() const
{
return m_impl.getNdbError();
}
//////////////////////////////////////////////////
// Implements different const datatypes by further
// subclassing of NdbConstOperand.
/////////////////////////////////////////////////
NdbConstOperand*
NdbQueryBuilder::constValue(const char* value)
{
returnErrIf(value==0,QRY_REQ_ARG_IS_NULL);
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbCharConstOperandImpl(value)));
}
NdbConstOperand*
NdbQueryBuilder::constValue(const void* value, Uint32 len)
{
returnErrIf(value == 0, QRY_REQ_ARG_IS_NULL);
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbGenericConstOperandImpl(value,len)));
}
NdbConstOperand*
NdbQueryBuilder::constValue(Int32 value)
{
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbInt64ConstOperandImpl(value)));
}
NdbConstOperand*
NdbQueryBuilder::constValue(Uint32 value)
{
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbInt64ConstOperandImpl(value)));
}
NdbConstOperand*
NdbQueryBuilder::constValue(Int64 value)
{
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbInt64ConstOperandImpl(value)));
}
NdbConstOperand*
NdbQueryBuilder::constValue(Uint64 value)
{
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbInt64ConstOperandImpl(value)));
}
NdbConstOperand*
NdbQueryBuilder::constValue(double value)
{
return static_cast<NdbConstOperand*>
(m_impl.addOperand(new NdbDoubleConstOperandImpl(value)));
}
NdbParamOperand*
NdbQueryBuilder::paramValue(const char* name)
{
return static_cast<NdbParamOperand*>
(m_impl.addOperand(new NdbParamOperandImpl(name,getImpl().m_paramCnt++)));
}
NdbLinkedOperand*
NdbQueryBuilder::linkedValue(const NdbQueryOperationDef* parent, const char* attr)
{
// Required non-NULL arguments
returnErrIf(parent==0 || attr==0, QRY_REQ_ARG_IS_NULL);
NdbQueryOperationDefImpl& parentImpl = parent->getImpl();
// Parent should be a OperationDef contained in this query builder context
returnErrIf(!m_impl.contains(&parentImpl), QRY_UNKONWN_PARENT);
// 'attr' should refer a column from the underlying table in parent:
const NdbColumnImpl* column = parentImpl.getTable().getColumn(attr);
returnErrIf(column==0, QRY_UNKNOWN_COLUMN); // Unknown column
// Locate refered parrent column in parent operations SPJ projection list;
// Add if not already present
int error = 0;
Uint32 colIx = parentImpl.addColumnRef(column, error);
if (unlikely(error != 0))
{
m_impl.setErrorCode(error);
return NULL;
}
return static_cast<NdbLinkedOperand*>
(m_impl.addOperand(new NdbLinkedOperandImpl(parentImpl,colIx)));
}
const NdbQueryLookupOperationDef*
NdbQueryBuilder::readTuple(const NdbDictionary::Table* table, // Primary key lookup
const NdbQueryOperand* const keys[], // Terminated by NULL element
const NdbQueryOptions* options,
const char* ident)
{
int i;
if (m_impl.hasError())
return NULL;
returnErrIf(table==0 || keys==0, QRY_REQ_ARG_IS_NULL);
const NdbTableImpl& tableImpl = NdbTableImpl::getImpl(*table);
// All column values being part of primary key should be specified:
int keyfields = table->getNoOfPrimaryKeys();
int colcount = table->getNoOfColumns();
// Check: keys[] are specified for all fields in PK
for (i=0; i<keyfields; ++i)
{
// A 'Key' value is undefined
returnErrIf(keys[i]==NULL, QRY_TOO_FEW_KEY_VALUES);
}
// Check for propper NULL termination of keys[] spec
returnErrIf(keys[keyfields]!=NULL, QRY_TOO_MANY_KEY_VALUES);
int error = 0;
NdbQueryPKLookupOperationDefImpl* op =
new NdbQueryPKLookupOperationDefImpl(tableImpl,
keys,
options ? options->getImpl() : defaultOptions,
ident,
m_impl.m_operations.size(),
error);
returnErrIf(m_impl.takeOwnership(op)!=0, Err_MemoryAlloc);
returnErrIf(error!=0, error); // C'tor returned error, bailout
Uint32 keyindex = 0;
for (i=0; i<colcount; ++i)
{
const NdbColumnImpl *col = tableImpl.getColumn(i);
if (col->getPrimaryKey())
{
assert (keyindex==col->m_keyInfoPos);
int error = op->m_keys[col->m_keyInfoPos]->bindOperand(*col,*op);
returnErrIf(error!=0, error);
keyindex++;
if (keyindex >= static_cast<Uint32>(keyfields))
break; // Seen all PK fields
}
}
return &op->m_interface;
}
const NdbQueryLookupOperationDef*
NdbQueryBuilder::readTuple(const NdbDictionary::Index* index, // Unique key lookup w/ index
const NdbDictionary::Table* table, // Primary key lookup
const NdbQueryOperand* const keys[], // Terminated by NULL element
const NdbQueryOptions* options,
const char* ident)
{
int i;
if (m_impl.hasError())
return NULL;
returnErrIf(table==0 || index==0 || keys==0, QRY_REQ_ARG_IS_NULL);
const NdbIndexImpl& indexImpl = NdbIndexImpl::getImpl(*index);
const NdbTableImpl& tableImpl = NdbTableImpl::getImpl(*table);
// TODO: Restrict to only table_version_major() mismatch?
returnErrIf(indexImpl.m_table_id
!= static_cast<Uint32>(table->getObjectId()) ||
indexImpl.m_table_version
!= static_cast<Uint32>(table->getObjectVersion()),
QRY_UNRELATED_INDEX);
// Only 'UNIQUE' indexes may be used for lookup operations:
returnErrIf(index->getType()!=NdbDictionary::Index::UniqueHashIndex,
QRY_WRONG_INDEX_TYPE);
// Check: keys[] are specified for all fields in 'index'
int inxfields = index->getNoOfColumns();
for (i=0; i<inxfields; ++i)
{
// A 'Key' value is undefined
returnErrIf(keys[i]==NULL, QRY_TOO_FEW_KEY_VALUES);
}
// Check for propper NULL termination of keys[] spec
returnErrIf(keys[inxfields]!=NULL, QRY_TOO_MANY_KEY_VALUES);
int error = 0;
NdbQueryIndexOperationDefImpl* op =
new NdbQueryIndexOperationDefImpl(indexImpl, tableImpl,
keys,
options ? options->getImpl() : defaultOptions,
ident,
m_impl.m_operations.size(),
error);
returnErrIf(m_impl.takeOwnership(op)!=0, Err_MemoryAlloc);
returnErrIf(error!=0, error); // C'tor returned error, bailout
// Bind to Column and check type compatibility
for (i=0; i<inxfields; ++i)
{
const NdbColumnImpl& col = NdbColumnImpl::getImpl(*indexImpl.getColumn(i));
assert (col.getColumnNo() == i);
error = keys[i]->getImpl().bindOperand(col,*op);
returnErrIf(error!=0, error);
}
return &op->m_interface;
}
const NdbQueryTableScanOperationDef*
NdbQueryBuilder::scanTable(const NdbDictionary::Table* table,
const NdbQueryOptions* options,
const char* ident)
{
if (m_impl.hasError())
return NULL;
returnErrIf(table==0, QRY_REQ_ARG_IS_NULL); // Required non-NULL arguments
int error = 0;
NdbQueryTableScanOperationDefImpl* op =
new NdbQueryTableScanOperationDefImpl(NdbTableImpl::getImpl(*table),
options ? options->getImpl() : defaultOptions,
ident,
m_impl.m_operations.size(),
error);
returnErrIf(m_impl.takeOwnership(op)!=0, Err_MemoryAlloc);
returnErrIf(error!=0, error); // C'tor returned error, bailout
return &op->m_interface;
}
const NdbQueryIndexScanOperationDef*
NdbQueryBuilder::scanIndex(const NdbDictionary::Index* index,
const NdbDictionary::Table* table,
const NdbQueryIndexBound* bound,
const NdbQueryOptions* options,
const char* ident)
{
if (m_impl.hasError())
return NULL;
// Required non-NULL arguments
returnErrIf(table==0 || index==0, QRY_REQ_ARG_IS_NULL);
const NdbIndexImpl& indexImpl = NdbIndexImpl::getImpl(*index);
const NdbTableImpl& tableImpl = NdbTableImpl::getImpl(*table);
// TODO: Restrict to only table_version_major() mismatch?
returnErrIf(indexImpl.m_table_id
!= static_cast<Uint32>(table->getObjectId()) ||
indexImpl.m_table_version
!= static_cast<Uint32>(table->getObjectVersion()),
QRY_UNRELATED_INDEX);
// Only ordered indexes may be used in scan operations:
returnErrIf(index->getType()!=NdbDictionary::Index::OrderedIndex,
QRY_WRONG_INDEX_TYPE);
int error = 0;