MySQL Connector/C++ 9.3.0
MySQL connector library for C and C++ applications
All Classes Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
result.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2024, Oracle and/or its affiliates.
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, version 2.0, as
6 * published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms, as
10 * designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * https://oss.oracle.com/licenses/universal-foss-exception.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License, version 2.0, for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31#ifndef MYSQLX_RESULT_H
32#define MYSQLX_RESULT_H
33
39#include "common.h"
40#include "document.h"
41#include "row.h"
42#include "collations.h"
43#include "detail/result.h"
44
45
46namespace mysqlx {
47MYSQLX_ABI_BEGIN(2,0)
48
49using std::ostream;
50
51class Session;
52class Schema;
53class Collection;
54class Result;
55class Row;
56class RowResult;
57class SqlResult;
58class DbDoc;
59class DocResult;
60
61template <class Res, class Op> class Executable;
62
63
64namespace internal {
65
66/*
67 A wrapper which adds methods common for all result classes.
68*/
69
70template <class Base>
71class Result_common
72 : protected Base
73{
74 using WarningList = internal::Result_detail::WarningList;
75
76public:
77
79
80 unsigned getWarningsCount() const
81 {
82 try {
83 return Base::get_warning_count();
84 }
85 CATCH_AND_WRAP
86 }
87
89
90 WarningList getWarnings()
91 {
92 try {
93 return Base::get_warnings();
94 }
95 CATCH_AND_WRAP
96 }
97
99 // TODO: Change arg type to size_t?
100
101 Warning getWarning(unsigned pos)
102 {
103 try {
104 return Base::get_warning(pos);
105 }
106 CATCH_AND_WRAP
107 }
108
109 // TODO: expose this in the API?
110 //using WarningsIterator = Result_detail::iterator;
111
116 uint64_t getAffectedItemsCount() const
117 {
118 try {
119 return Result_detail::get_affected_rows();
120 } CATCH_AND_WRAP
121 }
122
123protected:
124
125 // Wrap base ctors/assginment with catch handlers
126
127 Result_common()
128 try
129 : Base()
130 {}
131 CATCH_AND_WRAP
132
133 Result_common(Result_common &&other)
134 try
135 : Base(std::move(other))
136 {}
137 CATCH_AND_WRAP
138
139 Result_common& operator=(Result_common &&other)
140 try
141 {
142 Base::operator=(std::move(other));
143 return *this;
144 }
145 CATCH_AND_WRAP
146
147 Result_common(common::Result_init &init)
148 try
149 : Base(init)
150 {}
151 CATCH_AND_WRAP
152
153};
154
155} // internal namespace
156
157
183 : public internal::Result_common<internal::Result_detail>
184{
185
186public:
187
188 Result() = default;
189
195 uint64_t getAutoIncrementValue() const
196 {
197 try {
198 return Result_detail::get_auto_increment();
199 } CATCH_AND_WRAP
200 }
201
202
208 DocIdList getGeneratedIds() const
209 {
210 try {
211 return Result_detail::get_generated_ids();
212 } CATCH_AND_WRAP
213 }
214
215private:
216
217
218 Result(common::Result_init &init)
219 : Result_common(init)
220 {}
221
222 template <class Res, class Op>
223 friend class Executable;
224 friend Collection;
225};
226
227
228// Row based results
229// -----------------
230
240enum class Type : unsigned short
241{
242#undef TYPE_ENUM
243#define TYPE_ENUM(X,N) X=N,
244
245 RESULT_TYPE_LIST(TYPE_ENUM)
246};
247
248/*
249 Note: Normally we would put these docs in the RESULT_TYPE_LIST macro
250 but it would pollute documentation of methods like typeName() below
251 that also use this macro.
252*/
253
292
293
300inline
301const char* typeName(Type t)
302{
303#define TYPE_NAME(T,X) case Type::T: return #T;
304
305 switch (t)
306 {
307 RESULT_TYPE_LIST(TYPE_NAME)
308 default:
309 THROW("Unknown type");
310 }
311}
312
313inline
314std::ostream& operator<<(std::ostream &out, Type t)
315{
316 return out << typeName(t);
317}
318
319
326class Column
327 : public virtual common::Printable
328 , private internal::Column_detail
329{
330public:
331
332 string getSchemaName() const
333 {
334 try {
335 return Column_detail::get_schema_name();
336 }
337 CATCH_AND_WRAP
339
340 string getTableName() const
341 {
342 try {
343 return Column_detail::get_table_name();
344 }
345 CATCH_AND_WRAP
347
348 string getTableLabel() const
349 {
350 try {
351 return Column_detail::get_table_label();
352 }
353 CATCH_AND_WRAP
355
356 string getColumnName() const
357 {
358 try {
359 return Column_detail::get_name();
360 }
361 CATCH_AND_WRAP
363
364 string getColumnLabel() const
365 {
366 try {
367 return Column_detail::get_label();
368 }
369 CATCH_AND_WRAP
371
372 Type getType() const
373 {
374 try {
375 return Type(Column_detail::get_type());
376 }
377 CATCH_AND_WRAP
378 }
379
392 unsigned long getLength() const
393 {
394 try {
395 return Column_detail::get_length();
396 }
397 CATCH_AND_WRAP
399
400 unsigned short getFractionalDigits() const
401 {
402 try {
403 return Column_detail::get_decimals();
404 }
405 CATCH_AND_WRAP
407
408 bool isNumberSigned() const
409 {
410 try {
411 return Column_detail::is_signed();
412 }
413 CATCH_AND_WRAP
415
416 CharacterSet getCharacterSet() const
417 {
418 try {
419 return Column_detail::get_charset();
420 }
421 CATCH_AND_WRAP
422 }
425 std::string getCharacterSetName() const
426 {
427 try {
429 }
430 CATCH_AND_WRAP
432
433 const CollationInfo& getCollation() const
434 {
435 try {
436 return Column_detail::get_collation();
437 }
438 CATCH_AND_WRAP
439 }
442 std::string getCollationName() const
443 {
444 try {
445 return getCollation().getName();
446 }
447 CATCH_AND_WRAP
448 }
451 bool isPadded() const
452 {
453 try {
454 return Column_detail::is_padded();
455 }
456 CATCH_AND_WRAP
457 }
458
459protected:
460
461
462 using Column_detail::Impl;
463
464 Column(const Impl *impl)
465 try
466 : Column_detail(impl)
467 {}
468 CATCH_AND_WRAP
469
470 Column() = default;
471 Column(const Column&) = default;
472 Column(Column&&) = default;
473
474 Column& operator=(const Column&) = default;
475
476 void print(std::ostream &out) const
477 {
478 // TODO: not sure if this code will be called by operator<<.
479 try {
480 Column_detail::print(out);
481 }
482 CATCH_AND_WRAP
483 }
484
485public:
486
487 virtual ~Column()
488 {}
489
490 friend RowResult;
491 struct INTERNAL Access;
492 friend Access;
493};
494
495
496/*
497 Extern declarations for Columns_detail<Column> template specialization
498 elements that are defined in result.cc.
499
500 Note: "extern template" works with MSVC but not with GCC.
501*/
502
503namespace internal {
504
505template<> PUBLIC_API
506void Columns_detail<Column>::init(const Result_detail::Impl&);
507
508} // internal
509
510
511extern template PUBLIC_API
512void internal::Columns_detail<Column>::init(
513 const internal::Result_detail::Impl &impl
514);
515
516
517class Columns
518 : private internal::Columns_detail<Column>
519{
520public:
521
522 using Columns_detail::operator[];
523
524 using Columns_detail::iterator;
525
526 using Columns_detail::begin;
527 using Columns_detail::end;
528
529private:
530
531 using Columns_detail::init;
532
533 // note: Required by Row_result_detail
534
535 Columns() = default;
536 Columns(Columns&&) = default;
537 Columns& operator=(Columns&&) = default;
538
540 friend internal::Row_result_detail<Columns>;
542};
543
544
545/*
546 Extern declarations for Row_result_detail<Columns> template specialization
547 elements that are defined in result.cc.
548*/
549
550namespace internal {
551
552template<> PUBLIC_API
553bool Row_result_detail<Columns>::iterator_next();
554
555template<> PUBLIC_API
556col_count_t Row_result_detail<Columns>::col_count() const;
557
558template<> PUBLIC_API
559Row_result_detail<Columns>::Row_result_detail(
560 common::Result_init &init
561);
562
563template<> PUBLIC_API
564auto Row_result_detail<Columns>::get_column(col_count_t pos) const
565-> const Column&;
566
567template<> PUBLIC_API
568auto internal::Row_result_detail<Columns>::get_columns() const
569-> const Columns&;
570
571template<> PUBLIC_API
572row_count_t internal::Row_result_detail<Columns>::row_count();
573
574} // internal
575
576
588class RowResult
589 : public internal::Result_common<internal::Row_result_detail<Columns>>
590{
591public:
592
593 using Columns = mysqlx::Columns;
594
595 RowResult() = default;
596
597
599
600 col_count_t getColumnCount() const
601 {
602 try {
603 return Row_result_detail::col_count();
604 }
605 CATCH_AND_WRAP
606 }
607
609
610 const Column& getColumn(col_count_t pos) const
611 {
612 try {
613 return Row_result_detail::get_column(pos);
614 }
615 CATCH_AND_WRAP
616 }
617
624 const Columns& getColumns() const
625 {
626 try {
627 return Row_result_detail::get_columns();
628 }
629 CATCH_AND_WRAP
630 }
631
638 Row fetchOne()
639 {
640 try {
641 return Row_result_detail::get_row();
642 }
643 CATCH_AND_WRAP
644 }
645
654 RowList fetchAll()
655 {
656 try {
657 return Row_result_detail::get_rows();
658 }
659 CATCH_AND_WRAP
660 }
661
669 row_count_t count()
670 {
671 try {
672 return Row_result_detail::row_count();
673 }
674 CATCH_AND_WRAP
675 }
676
677 /*
678 Iterate over rows (range-for support).
679
680 Rows that have been fetched using iterator are not available when
681 calling fetchOne() or fetchAll()
682 */
683
684 iterator begin()
685 {
686 try {
687 return Row_result_detail::begin();
688 }
689 CATCH_AND_WRAP
690 }
691
692 iterator end() const
693 {
694 try {
695 return Row_result_detail::end();
696 }
697 CATCH_AND_WRAP
698 }
699
700
701private:
702
703 RowResult(common::Result_init &init)
704 : Result_common(init)
705 {}
706
707public:
708
709 template <class Res, class Op> friend class Executable;
710 friend SqlResult;
711 friend DocResult;
712};
713
714
733class SqlResult
734 : public RowResult
735{
736public:
737
738 SqlResult() = default;
739
747 bool hasData() const
748 {
749 try {
750 return Result_detail::has_data();
751 }
752 CATCH_AND_WRAP
753 }
754
755
765 bool nextResult()
766 {
767 try {
768 return Row_result_detail::next_result();
769 }
770 CATCH_AND_WRAP
771 }
772
773
779 uint64_t getAutoIncrementValue()
780 {
781 try {
782 return Result_detail::get_auto_increment();
783 }
784 CATCH_AND_WRAP
785 }
786
787private:
788
789 SqlResult(common::Result_init &init)
790 : RowResult(init)
791 {}
792
793 template <class Res, class Op>
794 friend class Executable;
795};
796
797
798// Document based results
799// ----------------------
800
801
818class DocResult
819 : public internal::Result_common<internal::Doc_result_detail>
820{
821
822public:
823
824 DocResult() = default;
825
832 DbDoc fetchOne()
833 {
834 try {
835 return Doc_result_detail::get_doc();
836 }
837 CATCH_AND_WRAP
838 }
839
848 DocList fetchAll()
849 {
850 try {
851 return Doc_result_detail::get_docs();
852 }
853 CATCH_AND_WRAP
854 }
855
863 uint64_t count()
864 {
865 try {
866 return Doc_result_detail::count();
867 }
868 CATCH_AND_WRAP
869 }
870
871 /*
872 Iterate over documents (range-for support).
873
874 Documents that have been fetched using iterator are not available when
875 calling fetchOne() or fetchAll()
876 */
877
878 using iterator = Doc_result_detail::iterator;
879
880 iterator begin()
881 {
882 try {
883 return Doc_result_detail::begin();
884 }
885 CATCH_AND_WRAP
886 }
887
888 iterator end() const
889 {
890 try {
891 return Doc_result_detail::end();
892 }
893 CATCH_AND_WRAP
894 }
895
896
897private:
898
899 DocResult(common::Result_init &init)
900 : Result_common(init)
901 {}
902
903 friend DbDoc;
904 template <class Res, class Op>
905 friend class Executable;
906};
907
908MYSQLX_ABI_END(2,0)
909} // mysqlx
910
911#endif
Represents a collection of documents in a schema.
Definition: xdevapi.h:912
Provides meta-data for a single result column.
Definition: result.h:327
const CollationInfo & getCollation() const
< TODO
Definition: result.h:431
std::string getCharacterSetName() const
TODO.
Definition: result.h:423
string getSchemaName() const
< TODO
Definition: result.h:330
bool isPadded() const
TODO.
Definition: result.h:449
std::string getCollationName() const
TODO.
Definition: result.h:440
CharacterSet getCharacterSet() const
< TODO
Definition: result.h:414
Represents an operation that can be executed.
Definition: executable.h:68
Represents a result of an operation that does not return data.
Definition: result.h:184
DocIdList getGeneratedIds() const
Return a list of identifiers of multiple documents added to a collection, generated by the server.
Definition: result.h:208
uint64_t getAutoIncrementValue() const
Get the auto-increment value if one was generated by a table insert statement.
Definition: result.h:195
Details for public API result classes.
Declaration of DbDoc and related classes.
const char * characterSetName(CharacterSet id)
Returns name of a character set given by its id.
Definition: collations.h:71
const char * typeName(Type t)
Return name of a given type.
Definition: result.h:300
Type
Types that can be reported in result meta-data.
Definition: result.h:241
TODO.
Structure that provides information about character set collation.
Definition: collations.h:94
const char * getName() const
String name of a collation, such as "latin1_generic_ci".
Definition: collations.h:101