5 #include <mysql/errmsg.h>
7 //This static variable exists to handle initializing and finalizing the MySQL driver library
8 std::shared_ptr<int> MySQLJobProvider::lib_ref(
9 //Initializer: Store the return code as a pointer to an integer
10 new int( mysql_library_init( 0, NULL, NULL ) ),
11 //Finalizer: Check the pointer and free resources
14 //The library is not initialized
19 //The library did return an error when initializing
29 MySQLJobProvider::MySQLJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
30 if( !lib_ref || *lib_ref ) {
31 throw "MySQL library not initialized!";
34 connect( server, user, password, database );
37 MySQLJobProvider::~MySQLJobProvider() {
41 bool MySQLJobProvider::connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
50 conn = _connect( server, user, password, database );
55 std::shared_ptr<MYSQL> MySQLJobProvider::_connect( const std::string& server, const std::string& user, const std::string& password, const std::string& database ) {
56 MYSQL* tmp( mysql_init( NULL ) );
59 return std::shared_ptr<MYSQL>();
62 tmp = mysql_real_connect( tmp, server.c_str(), user.c_str(), password.c_str(), database.c_str(), 3306, NULL, CLIENT_COMPRESS );
65 return std::shared_ptr<MYSQL>();
69 return std::shared_ptr<MYSQL>(
78 bool MySQLJobProvider::disconnect() {
88 std::pair< int, std::shared_ptr<MYSQL_RES> > MySQLJobProvider::query( const std::string& query ) {
90 return std::make_pair( CR_SERVER_LOST, std::shared_ptr<MYSQL_RES>() );
93 int err = mysql_real_query( this->conn.get(), query.c_str(), query.size() );
96 return std::make_pair( err, std::shared_ptr<MYSQL_RES>() );
100 std::shared_ptr<MYSQL_RES> res(
101 mysql_store_result( conn.get() ),
102 [c]( MYSQL_RES * r ) {
107 mysql_free_result( r );
110 return std::make_pair( err, res );
113 std::shared_ptr<Job> MySQLJobProvider::fetchJob() {
114 std::string q = "SELECT id, targetId, task, executeFrom, executeTo FROM jobs WHERE state='open'";
117 std::shared_ptr<MYSQL_RES> res;
119 std::tie( err, res ) = query( q );
122 return std::shared_ptr<Job>();
125 unsigned int num = mysql_num_fields( res.get() );
127 MYSQL_ROW row = mysql_fetch_row( res.get() );
130 return std::shared_ptr<Job>();
133 std::shared_ptr<Job> job( new Job() );
135 unsigned long* l = mysql_fetch_lengths( res.get() );
138 return std::shared_ptr<Job>();
141 job->id = std::string( row[0], row[0] + l[0] );
143 for( unsigned int i = 0; i < num; i++ ) {
144 printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
152 std::string MySQLJobProvider::escape_string( const std::string& target ) {
154 throw "Not connected!";
159 result.resize( target.size() * 2 );
161 long unsigned int len = mysql_real_escape_string( conn.get(), const_cast<char*>( result.data() ), target.c_str(), target.size() );
163 result.resize( len );
168 bool MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
173 std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
175 if( query( q ).first ) {