MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdbQueryBuilder.hpp
1 /*
2  Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 
17 #ifndef NdbQueryBuilder_H
18 #define NdbQueryBuilder_H
19 
20 #include <stdlib.h>
21 #include <ndb_types.h>
22 
23 // this file is currently not located in include/ndbapi
24 // skip includes...and require them to be included first
25 // BUH!
26 
27 class NdbQueryDef;
28 class NdbQueryDefImpl;
29 class NdbQueryBuilderImpl;
30 class NdbQueryOptionsImpl;
31 class NdbQueryOperandImpl;
32 class NdbQueryOperationDefImpl;
33 
34 
56 class NdbQueryOperand // A base class specifying a single value
57 {
58 public:
59  // Column which this operand relates to
60  const NdbDictionary::Column* getColumn() const;
61  NdbQueryOperandImpl& getImpl() const;
62 
63 protected:
64  // Enforce object creation through NdbQueryBuilder factory
65  explicit NdbQueryOperand(NdbQueryOperandImpl& impl);
66  ~NdbQueryOperand();
67 
68 private:
69  // Copying disallowed:
70  NdbQueryOperand(const NdbQueryOperand& other);
71  NdbQueryOperand& operator = (const NdbQueryOperand& other);
72 
73  NdbQueryOperandImpl& m_impl;
74 };
75 
76 // A NdbQueryOperand is either of these:
78 {
79 private:
80  friend class NdbConstOperandImpl;
81  explicit NdbConstOperand(NdbQueryOperandImpl& impl);
82  ~NdbConstOperand();
83 };
84 
86 {
87 private:
88  friend class NdbLinkedOperandImpl;
89  explicit NdbLinkedOperand(NdbQueryOperandImpl& impl);
91 };
92 
94 public:
95  const char* getName() const;
96  Uint32 getEnum() const;
97 
98 private:
99  friend class NdbParamOperandImpl;
100  explicit NdbParamOperand(NdbQueryOperandImpl& impl);
101  ~NdbParamOperand();
102 };
103 
104 
113 {
114 public:
115 
129  {
130  MatchAll, // DEFAULT: Output all matches, including duplicates.
131  // Append a single NULL complemented row for non-matching childs.
132  MatchNonNull, // Output all matches, including duplicates.
133  // Parents without any matches are discarded.
134  MatchNullOnly, // Output only parent rows without any child matches.
135  // Append a single NULL complemented row for the non_matching child
136  MatchSingle, // Output a single row when >=1 child matches.
137  // One of the matching child row is included in the output.
138  Default = MatchAll
139  };
140 
143  {
148  ScanOrdering_ascending,
149  ScanOrdering_descending
150  };
151 
152  explicit NdbQueryOptions();
153  ~NdbQueryOptions();
154 
160  int setOrdering(ScanOrdering ordering);
161 
164  int setMatchType(MatchType matchType);
165 
172  int setParent(const class NdbQueryOperationDef* parent);
173 
192  int setInterpretedCode(const class NdbInterpretedCode& code);
193 
194  const NdbQueryOptionsImpl& getImpl() const;
195 
196 private:
197  // Copying disallowed:
198  NdbQueryOptions(const NdbQueryOptions& other);
199  NdbQueryOptions& operator = (const NdbQueryOptions& other);
200 
201  NdbQueryOptionsImpl* m_pimpl;
202 
203 }; // class NdbQueryOptions
204 
205 
209 class NdbQueryOperationDef // Base class for all operation definitions
210 {
211 public:
212 
216  enum Type {
221  };
222 
223  static const char* getTypeName(Type type);
224 
228  Uint32 getQueryOperationIx() const;
229 
230  Uint32 getNoOfParentOperations() const;
231  const NdbQueryOperationDef* getParentOperation(Uint32 i) const;
232 
233  Uint32 getNoOfChildOperations() const;
234  const NdbQueryOperationDef* getChildOperation(Uint32 i) const;
235 
236  Type getType() const;
237 
241  const NdbDictionary::Table* getTable() const;
242 
247  const NdbDictionary::Index* getIndex() const;
248 
249  NdbQueryOperationDefImpl& getImpl() const;
250 
251 protected:
252  // Enforce object creation through NdbQueryBuilder factory
253  explicit NdbQueryOperationDef(NdbQueryOperationDefImpl& impl);
255 
256 private:
257  // Copying disallowed:
259  NdbQueryOperationDef& operator = (const NdbQueryOperationDef& other);
260 
261  NdbQueryOperationDefImpl& m_impl;
262 }; // class NdbQueryOperationDef
263 
264 
266 {
267 public:
268 
269 private:
270  // Enforce object creation through NdbQueryBuilder factory
271  friend class NdbQueryLookupOperationDefImpl;
272  explicit NdbQueryLookupOperationDef(NdbQueryOperationDefImpl& impl);
274 }; // class NdbQueryLookupOperationDef
275 
276 class NdbQueryScanOperationDef : public NdbQueryOperationDef // Base class for scans
277 {
278 protected:
279  // Enforce object creation through NdbQueryBuilder factory
280  explicit NdbQueryScanOperationDef(NdbQueryOperationDefImpl& impl);
282 }; // class NdbQueryScanOperationDef
283 
285 {
286 private:
287  // Enforce object creation through NdbQueryBuilder factory
289  explicit NdbQueryTableScanOperationDef(NdbQueryOperationDefImpl& impl);
291 }; // class NdbQueryTableScanOperationDef
292 
294 {
295 public:
296 
297 private:
298  // Enforce object creation through NdbQueryBuilder factory
299  friend class NdbQueryIndexScanOperationDefImpl;
300  explicit NdbQueryIndexScanOperationDef(NdbQueryOperationDefImpl& impl);
302 }; // class NdbQueryIndexScanOperationDef
303 
304 
313 {
314 public:
315  // C'tor for an equal bound:
316  NdbQueryIndexBound(const NdbQueryOperand* const *eqKey)
317  : m_low(eqKey), m_lowInclusive(true), m_high(eqKey), m_highInclusive(true)
318  {};
319 
320  // C'tor for a normal range including low & high limit:
321  NdbQueryIndexBound(const NdbQueryOperand* const *low,
322  const NdbQueryOperand* const *high)
323  : m_low(low), m_lowInclusive(true), m_high(high), m_highInclusive(true)
324  {};
325 
326  // Complete C'tor where limits might be exluded:
327  NdbQueryIndexBound(const NdbQueryOperand* const *low, bool lowIncl,
328  const NdbQueryOperand* const *high, bool highIncl)
329  : m_low(low), m_lowInclusive(lowIncl), m_high(high), m_highInclusive(highIncl)
330  {}
331 
332 private:
333  friend class NdbQueryIndexScanOperationDefImpl;
334  const NdbQueryOperand* const *m_low; // 'Pointer to array of pointers', NULL terminated
335  const bool m_lowInclusive;
336  const NdbQueryOperand* const *m_high; // 'Pointer to array of pointers', NULL terminated
337  const bool m_highInclusive;
338 };
339 
340 
364 {
365  friend class NdbQueryBuilderImpl;
366 private:
367  // Constructor is private, since application should use 'create()'.
368  explicit NdbQueryBuilder(NdbQueryBuilderImpl& impl);
369  // Destructor is private, since application should use 'destroy()'
370  ~NdbQueryBuilder();
371 
372  // No copying of NdbQueryBuilder objects.
374  NdbQueryBuilder& operator=(const NdbQueryBuilder&);
375 
376 public:
381  static NdbQueryBuilder* create();
382 
386  void destroy();
387 
388  const NdbQueryDef* prepare(); // Complete building a queryTree from 'this' NdbQueryBuilder
389 
390  // NdbQueryOperand builders:
391  //
392  // ::constValue constructors variants, considder to added/removed variants
393  // Typechecking is provided and will reject constValues/() which is
394  // incompatible with the type of the column it is used against.
395  // Some very basic typeconversion is available to match destination column
396  // with different numeric presicion and spacepad character strings to defined length.
397  NdbConstOperand* constValue(Int32 value);
398  NdbConstOperand* constValue(Uint32 value);
399  NdbConstOperand* constValue(Int64 value);
400  NdbConstOperand* constValue(Uint64 value);
401  NdbConstOperand* constValue(double value);
402  NdbConstOperand* constValue(const char* value); // Null terminated char/varchar C-type string
403 
404  // Raw constValue data with specified length - No typeconversion performed to match destination format.
405  // Fixed sized datatypes requires 'len' to exactly match the destination column.
406  // Fixed sized character values should be spacepadded to required length.
407  // Variable sized datatypes requires 'len <= max(len)'.
408  NdbConstOperand* constValue(const void* value, Uint32 len);
409 
410  // ::paramValue() is a placeholder for a parameter value to be specified when
411  // a query instance is created for execution.
412  NdbParamOperand* paramValue(const char* name = 0); // Parameterized
413 
414  // ::linkedValue() defines a value available from execution of a previously defined
415  // NdbQueryOperationDef. This NdbQueryOperationDef will become the 'parent' of the
416  // NdbQueryOperationDef which uses this linkedValue() in any expression.
417  NdbLinkedOperand* linkedValue(const NdbQueryOperationDef*, const char* attr); // Linked value
418 
419 
420  // NdbQueryOperationDef builders:
421  //
422  // Common (optional) arguments:
423  //
424  // - 'ident' may be used to identify each NdbQueryOperationDef with a name.
425  // This may later be used to find the corresponding NdbQueryOperation instance when
426  // the NdbQueryDef is executed.
427  // Each NdbQueryOperationDef will also be assigned an numeric ident (starting from 0)
428  // as an alternative way of locating the NdbQueryOperation.
429 
430  const NdbQueryLookupOperationDef* readTuple(
431  const NdbDictionary::Table*, // Primary key lookup
432  const NdbQueryOperand* const keys[], // Terminated by NULL element
433  const NdbQueryOptions* options = 0,
434  const char* ident = 0);
435 
436  const NdbQueryLookupOperationDef* readTuple(
437  const NdbDictionary::Index*, // Unique key lookup w/ index
438  const NdbDictionary::Table*,
439  const NdbQueryOperand* const keys[], // Terminated by NULL element
440  const NdbQueryOptions* options = 0,
441  const char* ident = 0);
442 
443  const NdbQueryTableScanOperationDef* scanTable(
444  const NdbDictionary::Table*,
445  const NdbQueryOptions* options = 0,
446  const char* ident = 0);
447 
448  const NdbQueryIndexScanOperationDef* scanIndex(
449  const NdbDictionary::Index*,
450  const NdbDictionary::Table*,
451  const NdbQueryIndexBound* bound = 0,
452  const NdbQueryOptions* options = 0,
453  const char* ident = 0);
454 
455 
466  const NdbError& getNdbError() const;
467 
468  NdbQueryBuilderImpl& getImpl() const;
469 
470 private:
471  NdbQueryBuilderImpl& m_impl;
472 
473 }; // class NdbQueryBuilder
474 
491 {
492  friend class NdbQueryDefImpl;
493 
494 public:
495 
499  enum QueryType {
503  };
504 
505  Uint32 getNoOfOperations() const;
506 
507  // Get a specific NdbQueryOperationDef by ident specified
508  // when the NdbQueryOperationDef was created.
509  const NdbQueryOperationDef* getQueryOperation(const char* ident) const;
510  const NdbQueryOperationDef* getQueryOperation(Uint32 index) const;
511 
512  // A scan query may return multiple rows, and may be ::close'ed when
513  // the client has completed access to it.
514  bool isScanQuery() const;
515 
516  // Return the 'enum QueryType' as defined above.
517  QueryType getQueryType() const;
518 
519  // Remove this NdbQueryDef including operation and operands it contains
520  void destroy() const;
521 
522  NdbQueryDefImpl& getImpl() const;
523 
527  void print() const;
528 private:
529  NdbQueryDefImpl& m_impl;
530 
531  explicit NdbQueryDef(NdbQueryDefImpl& impl);
532  ~NdbQueryDef();
533 
535  NdbQueryDef(const NdbQueryDef& other);
537  NdbQueryDef& operator = (const NdbQueryDef& other);
538 };
539 
540 
541 #endif