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 86 of file MariaDBConnection.h.

Constructor & Destructor Documentation

◆ MariaDBConnection() [1/2]

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

Definition at line 62 of file MariaDBConnection.cpp.

67 , mysql(mysql_init(nullptr))
68 , commandStartEvent("MariaDBCommandStartEvent", this) {
69 mysql_options(mysql, MYSQL_OPT_NONBLOCK, nullptr);
70
72 connectionDetails,
73 [this]() {
74 if (mysql_errno(mysql) == 0) {
75 const int fd = mysql_get_socket(mysql);
76
77 LOG(DEBUG) << "MariaDB: Got valid descriptor: " << fd;
78
79 if (ReadEventReceiver::enable(fd) && WriteEventReceiver::enable(fd) && ExceptionalConditionEventReceiver::enable(fd)) {
80 ReadEventReceiver::suspend();
81 WriteEventReceiver::suspend();
82 ExceptionalConditionEventReceiver::suspend();
83
84 connected = true;
85 } else {
86 if (ReadEventReceiver::isEnabled()) {
87 ReadEventReceiver::disable();
88 }
89 if (WriteEventReceiver::isEnabled()) {
90 WriteEventReceiver::disable();
91 }
92 if (ExceptionalConditionEventReceiver::isEnabled()) {
93 ExceptionalConditionEventReceiver::disable();
94 }
95 }
96 } else {
97 LOG(WARNING) << "MariaDB: Got no valid descriptor: " << mysql_error(mysql) << ", " << mysql_errno(mysql);
98 }
99 },
100 []() {
101 LOG(DEBUG) << "MariaDB: Connect success";
102 },
103 [](const std::string& errorString, unsigned int errorNumber) {
104 LOG(WARNING) << "MariaDB: Connect error: " << errorString << " : " << errorNumber;
105 }))));
106 }
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 108 of file MariaDBConnection.cpp.

108 {
109 for (MariaDBCommandSequence& mariaDBCommandSequence : commandSequenceQueue) {
110 for (MariaDBCommand* mariaDBCommand : mariaDBCommandSequence.sequence()) {
112 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
113 }
114
115 delete mariaDBCommand;
116 }
117 }
118
119 if (mariaDBClient != nullptr) {
121 }
122
123 mysql_close(mysql);
124 mysql_library_end();
125 }
static State state()
Definition SNodeC.cpp:76
std::deque< MariaDBCommandSequence > commandSequenceQueue

Member Function Documentation

◆ checkStatus()

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

Definition at line 195 of file MariaDBConnection.cpp.

195 {
196 if (connected) {
197 if ((status & MYSQL_WAIT_READ) != 0) {
198 if (ReadEventReceiver::isSuspended() && ReadEventReceiver::isEnabled()) {
199 ReadEventReceiver::resume();
200 }
201 } else if (!ReadEventReceiver::isSuspended()) {
202 ReadEventReceiver::suspend();
203 }
204
205 if ((status & MYSQL_WAIT_WRITE) != 0) {
206 if (WriteEventReceiver::isSuspended() && WriteEventReceiver::isEnabled()) {
207 WriteEventReceiver::resume();
208 }
209 } else if (!WriteEventReceiver::isSuspended()) {
210 WriteEventReceiver::suspend();
211 }
212
213 if ((status & MYSQL_WAIT_EXCEPT) != 0) {
214 if (ExceptionalConditionEventReceiver::isSuspended() && ExceptionalConditionEventReceiver::isEnabled()) {
215 ExceptionalConditionEventReceiver::resume();
216 }
217 } else if (!ExceptionalConditionEventReceiver::isSuspended()) {
218 ExceptionalConditionEventReceiver::suspend();
219 }
220
221 if ((status & MYSQL_WAIT_TIMEOUT) != 0) {
222 ReadEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
223 // WriteEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
224 // ExceptionalConditionEventReceiver::setTimeout(mysql_get_timeout_value(mysql));
225 } else {
226 ReadEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
227 // WriteEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
228 // ExceptionalConditionEventReceiver::setTimeout(core::DescriptorEventReceiver::TIMEOUT::DEFAULT);
229 }
230
231 if (status == 0) {
232 if (mysql_errno(mysql) == 0) {
235 }
236 } else {
237 currentCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
239 }
241 }
242 } else {
243 currentCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
245 delete this;
246 }
247 }
virtual void commandError(const std::string &errorString, unsigned int errorNumber)=0

◆ commandCompleted()

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

Definition at line 179 of file MariaDBConnection.cpp.

179 {
180 LOG(DEBUG) << "MariaDB: Completed: " << currentCommand->commandInfo();
181 commandSequenceQueue.front().commandCompleted();
182
183 if (commandSequenceQueue.front().empty()) {
184 commandSequenceQueue.pop_front();
185 }
186
187 delete currentCommand;
188 currentCommand = nullptr;
189 }

◆ commandContinue()

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

Definition at line 169 of file MariaDBConnection.cpp.

169 {
170 if (currentCommand != nullptr) {
172 } else if ((status & MYSQL_WAIT_READ) != 0 && commandSequenceQueue.empty()) {
173 ReadEventReceiver::disable();
174 WriteEventReceiver::disable();
175 ExceptionalConditionEventReceiver::disable();
176 }
177 }
virtual int commandContinue(int status)=0

◆ commandStart()

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

Definition at line 150 of file MariaDBConnection.cpp.

150 {
151 if (!commandSequenceQueue.empty()) {
152 currentCommand = commandSequenceQueue.front().nextCommand();
153
154 LOG(DEBUG) << "MariaDB: Start: " << currentCommand->commandInfo();
155
158 } else if (mariaDBClient != nullptr) {
159 if (ReadEventReceiver::isSuspended() && ReadEventReceiver::isEnabled()) {
160 ReadEventReceiver::resume();
161 }
162 } else {
163 ReadEventReceiver::disable();
164 WriteEventReceiver::disable();
165 ExceptionalConditionEventReceiver::disable();
166 }
167 }
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 127 of file MariaDBConnection.cpp.

127 {
128 if (currentCommand == nullptr && commandSequenceQueue.empty()) {
130 }
131
132 commandSequenceQueue.emplace_back(std::move(commandSequence));
133
134 return commandSequenceQueue.back();
135 }

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 137 of file MariaDBConnection.cpp.

137 {
138 mariaDBCommand->commandStart(mysql, utils::Timeval::currentTime());
139
140 if (mysql_errno(mysql) == 0) {
141 if (mariaDBCommand->commandCompleted()) {
142 }
143 } else {
144 mariaDBCommand->commandError(mysql_error(mysql), mysql_errno(mysql));
145 }
146
147 delete mariaDBCommand;
148 }
static Timeval currentTime()
Definition Timeval.cpp:76

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 257 of file MariaDBConnection.cpp.

257 {
258 commandContinue(MYSQL_WAIT_EXCEPT);
259 }

◆ outOfBandTimeout()

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

Reimplemented from core::eventreceiver::ExceptionalConditionEventReceiver.

Definition at line 269 of file MariaDBConnection.cpp.

269 {
270 commandContinue(MYSQL_WAIT_TIMEOUT);
271 }

◆ readEvent()

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

Implements core::eventreceiver::ReadEventReceiver.

Definition at line 249 of file MariaDBConnection.cpp.

249 {
250 commandContinue(MYSQL_WAIT_READ);
251 }

◆ readTimeout()

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

Reimplemented from core::eventreceiver::ReadEventReceiver.

Definition at line 261 of file MariaDBConnection.cpp.

261 {
262 commandContinue(MYSQL_WAIT_TIMEOUT);
263 }

◆ unmanaged()

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

Definition at line 191 of file MariaDBConnection.cpp.

191 {
192 mariaDBClient = nullptr;
193 }

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 273 of file MariaDBConnection.cpp.

273 {
274 delete this;
275 }

◆ writeEvent()

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

Implements core::eventreceiver::WriteEventReceiver.

Definition at line 253 of file MariaDBConnection.cpp.

253 {
254 commandContinue(MYSQL_WAIT_WRITE);
255 }

◆ writeTimeout()

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

Reimplemented from core::eventreceiver::WriteEventReceiver.

Definition at line 265 of file MariaDBConnection.cpp.

265 {
266 commandContinue(MYSQL_WAIT_TIMEOUT);
267 }

Member Data Documentation

◆ commandSequenceQueue

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

Definition at line 124 of file MariaDBConnection.h.

◆ commandStartEvent

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

Definition at line 129 of file MariaDBConnection.h.

◆ connected

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

Definition at line 127 of file MariaDBConnection.h.

◆ currentCommand

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

Definition at line 126 of file MariaDBConnection.h.

◆ mariaDBClient

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

Definition at line 121 of file MariaDBConnection.h.

◆ mysql

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

Definition at line 122 of file MariaDBConnection.h.


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