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] );
142 job->target = std::string( row[1], row[1] + l[1] );
143 job->task = std::string( row[2], row[2] + l[2] );
144 job->from = std::string( row[3], row[3] + l[3] );
145 job->to = std::string( row[4], row[4] + l[4] );
147 for( unsigned int i = 0; i < num; i++ ) {
148 printf( "[%.*s] ", ( int ) l[i], row[i] ? row[i] : "NULL" );
156 std::string MySQLJobProvider::escape_string( const std::string& target ) {
158 throw "Not connected!";
163 result.resize( target.size() * 2 );
165 long unsigned int len = mysql_real_escape_string( conn.get(), const_cast<char*>( result.data() ), target.c_str(), target.size() );
167 result.resize( len );
172 bool MySQLJobProvider::finishJob( std::shared_ptr<Job> job ) {
177 std::string q = "UPDATE jobs SET state='done' WHERE id='" + this->escape_string( job->id ) + "' LIMIT 1";
179 if( query( q ).first ) {
186 std::shared_ptr<TBSCertificate> MySQLJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
187 std::shared_ptr<TBSCertificate> cert = std::shared_ptr<TBSCertificate>( new TBSCertificate() );
188 std::string q = "SELECT CN, subject, md, profile, csr_name, csr_type FROM certs WHERE id='" + this->escape_string( job->id ) + "'";
191 std::shared_ptr<MYSQL_RES> res;
193 std::tie( err, res ) = query( q );
196 return std::shared_ptr<TBSCertificate>();
199 MYSQL_ROW row = mysql_fetch_row( res.get() );
202 return std::shared_ptr<TBSCertificate>();
205 unsigned long* l = mysql_fetch_lengths( res.get() );
208 return std::shared_ptr<TBSCertificate>();
211 cert->CN = std::string( row[0], row[0] + l[0] );
212 cert->subj = std::string( row[1], row[1] + l[1] );
213 cert->md = std::string( row[2], row[2] + l[2] );
214 cert->profile = std::string( row[3], row[3] + l[3] );
215 cert->csr = std::string( row[4], row[4] + l[4] );
216 cert->csr_type = std::string( row[5], row[5] + l[5] );