SNode.C
Loading...
Searching...
No Matches
database::mariadb::MariaDBConnection Class Reference

#include <MariaDBConnection.h>

Inheritance diagram for database::mariadb::MariaDBConnection:
Collaboration diagram for database::mariadb::MariaDBConnection:

Public Member Functions

 MariaDBConnection (MariaDBClient *mariaDBClient, const MariaDBConnectionDetails &connectionDetails)
 
 MariaDBConnection (const MariaDBConnection &)=delete
 
 ~MariaDBConnection () override
 
MariaDBConnectionoperator= (const MariaDBConnection &)=delete
 
MariaDBCommandSequenceexecute_async (MariaDBCommandSequence &&commandSequence)
 
void execute_sync (MariaDBCommand *mariaDBCommand)
 
void commandStart (const utils::Timeval &currentTime)
 
void commandContinue (int status)
 
void commandCompleted ()
 
void unmanaged ()
 

Protected Member Functions

void checkStatus (int status)
 
void readEvent () override
 
void writeEvent () override
 
void outOfBandEvent () override
 
void readTimeout () override
 
void writeTimeout () override
 
void outOfBandTimeout () override
 
void unobservedEvent () override
 

Private Attributes

MariaDBClientmariaDBClient
 
MYSQL * mysql
 
std::deque< MariaDBCommandSequencecommandSequenceQueue
 
MariaDBCommandcurrentCommand = nullptr
 
bool connected = false
 
MariaDBCommandStartEvent commandStartEvent
 

Additional Inherited Members

- Private Member Functions inherited from core::eventreceiver::ReadEventReceiver
 ReadEventReceiver (const std::string &name, const utils::Timeval &timeout)
 
- Private Member Functions inherited from core::DescriptorEventReceiver
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
 
int getRegisteredFd () const
 
bool isEnabled () const
 
bool isSuspended () const
 
void setTimeout (const utils::Timeval &timeout)
 
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
 
void checkTimeout (const utils::Timeval &currentTime)
 
bool enable (int fd)
 
void disable ()
 
void suspend ()
 
void resume ()
 
- Private Member Functions inherited from core::EventReceiver
 EventReceiver (const std::string &name)
 
 EventReceiver (EventReceiver &)=delete
 
 EventReceiver (EventReceiver &&)=delete
 
EventReceiveroperator= (EventReceiver &)=delete
 
EventReceiveroperator= (EventReceiver &&)=delete
 
virtual void destruct ()
 
void span ()
 
void relax ()
 
const std::string & getName () const
 
virtual ~EventReceiver ()=default
 
- Private Member Functions inherited from core::eventreceiver::WriteEventReceiver
 WriteEventReceiver (const std::string &name, const utils::Timeval &timeout)
 
- Private Member Functions inherited from core::eventreceiver::ExceptionalConditionEventReceiver
 ExceptionalConditionEventReceiver (const std::string &name, const utils::Timeval &timeout=60)
 
- Static Private Member Functions inherited from core::EventReceiver
static void atNextTick (const std::function< void(void)> &callBack)
 

Detailed Description

Definition at line 64 of file MariaDBConnection.h.

Constructor & Destructor Documentation

◆ MariaDBConnection() [1/2]

database::mariadb::MariaDBConnection::MariaDBConnection ( MariaDBClient * mariaDBClient,
const MariaDBConnectionDetails & connectionDetails )
explicit

Definition at line 40 of file MariaDBConnection.cpp.

45 , mysql(mysql_init(nullptr))
46 , commandStartEvent("MariaDBCommandStartEvent", this) {
47 mysql_options(mysql, MYSQL_OPT_NONBLOCK, nullptr);
48
50 connectionDetails,
51 [this]() {
52 if (mysql_errno(mysql) == 0) {
53 const int fd = mysql_get_socket(mysql);
54
55 LOG(DEBUG) << "MariaDB: Got valid descriptor: " << fd;
56
57 if (ReadEventReceiver::enable(fd) && WriteEventReceiver::enable(fd) && ExceptionalConditionEventReceiver::enable(fd)) {
58 ReadEventReceiver::suspend();
59 WriteEventReceiver::suspend();
60 ExceptionalConditionEventReceiver::suspend();
61
62 connected = true;
63 } else {
64 if (ReadEventReceiver::isEnabled()) {
65 ReadEventReceiver::disable();
66 }
67 if (WriteEventReceiver::isEnabled()) {
68 WriteEventReceiver::disable();
69 }
70 if (ExceptionalConditionEventReceiver::isEnabled()) {
71 ExceptionalConditionEventReceiver::disable();
72 }
73 }
74 } else {
75 LOG(WARNING) << "MariaDB: Got no valid descriptor: " << mysql_error(mysql) << ", " << mysql_errno(mysql);
76 }
77 },
78 []() {
79 LOG(DEBUG) << "MariaDB: Connect success";
80 },
81 [](const std::string& errorString, unsigned int errorNumber) {
82 LOG(WARNING) << "MariaDB: Connect error: " << errorString << " : " << errorNumber;
83 }))));
84 }
ExceptionalConditionEventReceiver(const std::string &name, const utils::Timeval &timeout=60)
ReadEventReceiver(const std::string &name, const utils::Timeval &timeout)
WriteEventReceiver(const std::string &name, const utils::Timeval &timeout)
MariaDBCommandStartEvent commandStartEvent
MariaDBCommandSequence & execute_async(MariaDBCommandSequence &&commandSequence)

Referenced by database::mariadb::MariaDBClient::execute_async(), and database::mariadb::MariaDBClient::execute_sync().

Here is the caller graph for this function:

◆ MariaDBConnection() [2/2]

database::mariadb::MariaDBConnection::MariaDBConnection ( const MariaDBConnection & )
delete

◆ ~MariaDBConnection()

database::mariadb::MariaDBConnection::~MariaDBConnection ( )
override

Definition at line 86 of file MariaDBConnection.cpp.

86 {
87 for (MariaDBCommandSequence& mariaDBCommandSequence : commandSequenceQueue) {
88 for (MariaDBCommand* mariaDBCommand : mariaDBCommandSequence.sequence()) {
90 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
91 }
92
93 delete mariaDBCommand;
94 }
95 }
96
97 if (mariaDBClient != nullptr) {
99 }
100
101 mysql_close(mysql);
102 mysql_library_end();
103 }
static State state()
Definition SNodeC.cpp:54
std::deque< MariaDBCommandSequence > commandSequenceQueue

Member Function Documentation

◆ checkStatus()

void database::mariadb::MariaDBConnection::checkStatus ( int status)
protected

Definition at line 173 of file MariaDBConnection.cpp.

173 {
174 if (connected) {
175 if ((status & MYSQL_WAIT_READ) != 0) {
176 if (ReadEventReceiver::isSuspended() && ReadEventReceiver::isEnabled()) {
177 ReadEventReceiver::resume();
178 }
179 } else if (!ReadEventReceiver::isSuspended()) {
180 ReadEventReceiver::suspend();
181 }
182
183 if ((status & MYSQL_WAIT_WRITE) != 0) {
184 if (WriteEventReceiver::isSuspended() && WriteEventReceiver::isEnabled()) {
185 WriteEventReceiver::resume();
186 }
187 } else if (!WriteEventReceiver::isSuspended()) {
188 WriteEventReceiver::suspend();
189 }
190
191 if ((status & MYSQL_WAIT_EXCEPT) != 0) {
192 if (ExceptionalConditionEventReceiver::isSuspended() && ExceptionalConditionEventReceiver::isEnabled()) {
193 ExceptionalConditionEventReceiver::resume();
194 }
195 } else if (!ExceptionalConditionEventReceiver::isSuspended()) {
196 ExceptionalConditionEventReceiver::suspend();
197 }
198
199 if ((status & MYSQL_WAIT_TIMEOUT) != 0) {
200 ReadEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
201 // WriteEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
202 // ExceptionalConditionEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
203 } else {
204 ReadEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
205 // WriteEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
206 // ExceptionalConditionEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
207 }
208
209 if (status == 0) {
210 if (mysql_errno(mysql) == 0) {
213 }
214 } else {
215 currentCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
217 }
219 }
220 } else {
221 currentCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
223 delete this;
224 }
225 }
virtual void commandError(const std::string &errorString, unsigned int errorNumber)=0

◆ commandCompleted()

void database::mariadb::MariaDBConnection::commandCompleted ( )

Definition at line 157 of file MariaDBConnection.cpp.

157 {
158 LOG(DEBUG) << "MariaDB: Completed: " << currentCommand->commandInfo();
159 commandSequenceQueue.front().commandCompleted();
160
161 if (commandSequenceQueue.front().empty()) {
162 commandSequenceQueue.pop_front();
163 }
164
165 delete currentCommand;
166 currentCommand = nullptr;
167 }

◆ commandContinue()

void database::mariadb::MariaDBConnection::commandContinue ( int status)

Definition at line 147 of file MariaDBConnection.cpp.

147 {
148 if (currentCommand != nullptr) {
150 } else if ((status & MYSQL_WAIT_READ) != 0 && commandSequenceQueue.empty()) {
151 ReadEventReceiver::disable();
152 WriteEventReceiver::disable();
153 ExceptionalConditionEventReceiver::disable();
154 }
155 }
virtual int commandContinue(int status)=0

◆ commandStart()

void database::mariadb::MariaDBConnection::commandStart ( const utils::Timeval & currentTime)

Definition at line 128 of file MariaDBConnection.cpp.

128 {
129 if (!commandSequenceQueue.empty()) {
130 currentCommand = commandSequenceQueue.front().nextCommand();
131
132 LOG(DEBUG) << "MariaDB: Start: " << currentCommand->commandInfo();
133
136 } else if (mariaDBClient != nullptr) {
137 if (ReadEventReceiver::isSuspended() && ReadEventReceiver::isEnabled()) {
138 ReadEventReceiver::resume();
139 }
140 } else {
141 ReadEventReceiver::disable();
142 WriteEventReceiver::disable();
143 ExceptionalConditionEventReceiver::disable();
144 }
145 }
int commandStart(MYSQL *mysql, const utils::Timeval &currentTime)
void setMariaDBConnection(MariaDBConnection *mariaDBConnection)

◆ execute_async()

MariaDBCommandSequence & database::mariadb::MariaDBConnection::execute_async ( MariaDBCommandSequence && commandSequence)

Definition at line 105 of file MariaDBConnection.cpp.

105 {
106 if (currentCommand == nullptr && commandSequenceQueue.empty()) {
108 }
109
110 commandSequenceQueue.emplace_back(std::move(commandSequence));
111
112 return commandSequenceQueue.back();
113 }

Referenced by database::mariadb::MariaDBClient::execute_async().

Here is the caller graph for this function:

◆ execute_sync()

void database::mariadb::MariaDBConnection::execute_sync ( MariaDBCommand * mariaDBCommand)

Definition at line 115 of file MariaDBConnection.cpp.

115 {
116 mariaDBCommand->commandStart(mysql, utils::Timeval::currentTime());
117
118 if (mysql_errno(mysql) == 0) {
119 if (mariaDBCommand->commandCompleted()) {
120 }
121 } else {
122 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
123 }
124
125 delete mariaDBCommand;
126 }
static Timeval currentTime()
Definition Timeval.cpp:54

Referenced by database::mariadb::MariaDBClient::execute_sync().

Here is the caller graph for this function:

◆ operator=()

MariaDBConnection & database::mariadb::MariaDBConnection::operator= ( const MariaDBConnection & )
delete

◆ outOfBandEvent()

void database::mariadb::MariaDBConnection::outOfBandEvent ( )
overrideprotectedvirtual

Implements core::eventreceiver::ExceptionalConditionEventReceiver.

Definition at line 235 of file MariaDBConnection.cpp.

235 {
236 commandContinue(MYSQL_WAIT_EXCEPT);
237 }

◆ outOfBandTimeout()

void database::mariadb::MariaDBConnection::outOfBandTimeout ( )
overrideprotectedvirtual

Reimplemented from core::eventreceiver::ExceptionalConditionEventReceiver.

Definition at line 247 of file MariaDBConnection.cpp.

247 {
248 commandContinue(MYSQL_WAIT_TIMEOUT);
249 }

◆ readEvent()

void database::mariadb::MariaDBConnection::readEvent ( )
overrideprotectedvirtual

Implements core::eventreceiver::ReadEventReceiver.

Definition at line 227 of file MariaDBConnection.cpp.

227 {
228 commandContinue(MYSQL_WAIT_READ);
229 }

◆ readTimeout()

void database::mariadb::MariaDBConnection::readTimeout ( )
overrideprotectedvirtual

Reimplemented from core::eventreceiver::ReadEventReceiver.

Definition at line 239 of file MariaDBConnection.cpp.

239 {
240 commandContinue(MYSQL_WAIT_TIMEOUT);
241 }

◆ unmanaged()

void database::mariadb::MariaDBConnection::unmanaged ( )

Definition at line 169 of file MariaDBConnection.cpp.

169 {
170 mariaDBClient = nullptr;
171 }

Referenced by database::mariadb::MariaDBClient::~MariaDBClient().

Here is the caller graph for this function:

◆ unobservedEvent()

void database::mariadb::MariaDBConnection::unobservedEvent ( )
overrideprotectedvirtual

Implements core::Observer.

Definition at line 251 of file MariaDBConnection.cpp.

251 {
252 delete this;
253 }

◆ writeEvent()

void database::mariadb::MariaDBConnection::writeEvent ( )
overrideprotectedvirtual

Implements core::eventreceiver::WriteEventReceiver.

Definition at line 231 of file MariaDBConnection.cpp.

231 {
232 commandContinue(MYSQL_WAIT_WRITE);
233 }

◆ writeTimeout()

void database::mariadb::MariaDBConnection::writeTimeout ( )
overrideprotectedvirtual

Reimplemented from core::eventreceiver::WriteEventReceiver.

Definition at line 243 of file MariaDBConnection.cpp.

243 {
244 commandContinue(MYSQL_WAIT_TIMEOUT);
245 }

Member Data Documentation

◆ commandSequenceQueue

std::deque<MariaDBCommandSequence> database::mariadb::MariaDBConnection::commandSequenceQueue
private

Definition at line 102 of file MariaDBConnection.h.

◆ commandStartEvent

MariaDBCommandStartEvent database::mariadb::MariaDBConnection::commandStartEvent
private

Definition at line 107 of file MariaDBConnection.h.

◆ connected

bool database::mariadb::MariaDBConnection::connected = false
private

Definition at line 105 of file MariaDBConnection.h.

◆ currentCommand

MariaDBCommand* database::mariadb::MariaDBConnection::currentCommand = nullptr
private

Definition at line 104 of file MariaDBConnection.h.

◆ mariaDBClient

MariaDBClient* database::mariadb::MariaDBConnection::mariaDBClient
private

Definition at line 99 of file MariaDBConnection.h.

◆ mysql

MYSQL* database::mariadb::MariaDBConnection::mysql
private

Definition at line 100 of file MariaDBConnection.h.


The documentation for this class was generated from the following files: