Jump to content
phreak

[C++] Mysql basic wrapper and custom assert

Recommended Posts

Posted

xassert.hpp :



#include <execinfo.h>
#include <cstdlib>
#include <iostream>

using std::cout;
using std::endl;
void xassert(bool ok, const char * str)
{
if(ok)
return;

int addr_count;
void * buffer[100];
char ** strings;
int i;

addr_count = backtrace(buffer, 100);

cout << "ASSERT : " << str << endl;

strings = backtrace_symbols(buffer, addr_count);
if(strings == NULL)
{
cout << "Failed to get backtrace symbols\n";
return;
}

for(i = 0; i < addr_count; i++)
cout << strings[i] << endl;

free(strings);
exit(100);
}

void xassert(bool ok, std::string stdstr)
{
const char * str = stdstr.c_str();
if(ok)
return;

int addr_count;
void * buffer[100];
char ** strings;
int i;

addr_count = backtrace(buffer, 100);
cout << "ASSERT : " << str << endl;

strings = backtrace_symbols(buffer, addr_count);
if(strings == NULL)
{
cout << "Failed to get backtrace symbols\n";
return;
}

for(i = 0; i < addr_count; i++)
cout << strings[i] << endl;

free(strings);
exit(100);
}

mysql.hpp :



#ifndef MYSQL_HPP
#define MYSQL_HPP

#include <iostream>
#include <vector>
#include <map>
#include <mysql/mysql.h>
#include <cstdio>
#include <cstdlib>
#include "xassert.hpp"

using std::cout;
using std::endl;
using std::size_t;

typedef std::vector<std::map<std::string, std::string> > mysql_result;

struct mysql_connection{
mysql_connection(const char* host, const char* username, const char* password, const char* db_name)
{
m_Host = host;
m_Username = username;
m_Password = password;
m_DbName = db_name;
}

void init()
{
m_Con = mysql_init(NULL);
xassert(m_Con, "mysql_init fail");

const int param = 1;
xassert(!mysql_options(m_Con, MYSQL_OPT_RECONNECT, (const void*)&param),
"Failed to set options");

xassert(mysql_real_connect(m_Con, m_Host, m_Username, m_Password, m_DbName, 0, NULL, 0),
"Mysql connection could not be established");
}

void exec(const char * query)
{
xassert(!mysql_query(m_Con, query),
std::string(query) + " : " + std::string(mysql_error(m_Con)));
}

mysql_result select(const char *query)
{
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_RES *res;
int num_columns;
int num_rows;
mysql_result rezult;
std::vector<std::string> columns;

exec(query); // run query
res = mysql_store_result(m_Con); // store result

if(!res)
return rezult; // empty result set

num_columns = mysql_num_fields(res); // get number of columns
num_rows = mysql_num_rows(res);

rezult.resize(num_rows);

while(field = mysql_fetch_field(res)) // fetch column names
columns.push_back(field->name);

int row_count = 0;
while((row = mysql_fetch_row(res))) // fetch rows
{
for(int i = 0; i < num_columns; i++) // iterate columns
{
std::string column(columns[i]);

rezult[row_count][column] = std::string(row[i]);
}
row_count++;
}

mysql_free_result(res);

return rezult;
}

~mysql_connection()
{
mysql_close(m_Con);
}

private:
MYSQL * m_Con;
const char * m_Host;
const char * m_Username;
const char * m_Password;
const char * m_DbName;
};

#endif

main.cpp :



#include "mysql.hpp"

int main(int argc, char** argv)
{
mysql_connection sql("localhost", "root", "pass", "test");
sql.init();

sql.exec("DROP TABLE IF EXISTS test");
sql.exec("CREATE TABLE test ( x INT, y INT )");
sql.exec("INSERT INTO test VALUES ( 1, 2)");
sql.exec("INSERT INTO test VALUES ( 3, 4)");

mysql_result result = sql.select("SELECT * FROM test");

for(size_t row = 0; row < result.size(); row++)
{
for(auto it = result[row].begin(); it != result[row].end(); it++)
cout << it->first << " : " << it->second << endl;
cout << "-----------------" << endl;
}

return 0;
}

LE: n-ar fi rau un script de syntax highlighting sau un plugin care sa afiseze linkuri de ideone / codepad

  • Upvote 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...