1 /*
2  * Copyright (c) 2015, 2016, 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, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as 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 included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 #ifndef _XPL_EXPECT_H_
27 #define _XPL_EXPECT_H_
28 
29 #include "ngs/protocol_encoder.h"
30 
31 #include <list>
32 
33 #include "ngs_common/protocol_protobuf.h"
34 
35 namespace xpl
36 {
37   class Expect_condition
38   {
39   public:
~Expect_condition()40     virtual ~Expect_condition() {}
41     virtual Expect_condition *copy() = 0;
42     virtual ngs::Error_code check() = 0;
43 
key()44     uint32_t key() const { return m_key; }
set_key(uint32_t k)45     void set_key(uint32_t k) { m_key = k; }
46   private:
47     uint32_t m_key;
48   };
49 
50   class Expectation
51   {
52   public:
Expectation()53     Expectation() : m_fail_on_error(false), m_gtid_wait_less_than(0) {}
54     Expectation(const Expectation &other);
55 
56     ~Expectation();
57 
58     Expectation &operator =(const Expectation &other);
59 
60     // whether an error occurred previously in a no_error block
set_failed(const std::string & reason)61     void set_failed(const std::string &reason) { m_failed = reason; }
failed_condition()62     std::string failed_condition() const { return m_failed; }
failed()63     bool failed() const { return !m_failed.empty(); }
fail_on_error()64     bool fail_on_error() const { return m_fail_on_error; }
65 
66     ngs::Error_code check();
67 
68     void unset(uint32_t key);
69     ngs::Error_code set(uint32_t key, const std::string &value);
70 
71     void swap(Expectation &one, Expectation &other);
72 
73     void add_condition(Expect_condition *cond);
74   private:
75     std::list<Expect_condition*> m_conditions;
76     std::string m_failed;
77     bool m_fail_on_error;
78 
79     int m_gtid_wait_less_than;
80   };
81 
82   class Expectation_stack
83   {
84   public:
85     Expectation_stack();
86 
87     ngs::Error_code open(const Mysqlx::Expect::Open &open);
88     ngs::Error_code close();
89 
90     // called before executing client statements. should signal error if one is returned
91     ngs::Error_code pre_client_stmt(int8_t msgid);
92 
93     // called when an error occurs executing client statements
94     void post_client_stmt(int8_t msgid, const ngs::Error_code &error);
95 
96   private:
97     std::vector<Expectation> m_expect_stack;
98   };
99 }
100 
101 
102 #endif
103