AccountRow SQLS added

pull/1/head
moneroexamples 8 years ago
parent e80c359f84
commit eebae836f1

@ -32,6 +32,8 @@ include_directories(
${MONERO_HEADERS_DIR}/contrib/epee/include ${MONERO_HEADERS_DIR}/contrib/epee/include
${MONERO_HEADERS_DIR}/external/db_drivers/liblmdb) ${MONERO_HEADERS_DIR}/external/db_drivers/liblmdb)
include_directories(/usr/include/mysql)
# get individual monero static libraries # get individual monero static libraries
# that are needed in this project # that are needed in this project
@ -88,7 +90,8 @@ set(SOURCE_FILES
add_executable(restbed_xmr add_executable(restbed_xmr
${SOURCE_FILES}) ${SOURCE_FILES})
target_link_libraries(restbed_xmr
target_link_libraries(restbed_xmr PRIVATE
myxrm myxrm
myext myext
restbed restbed
@ -101,7 +104,8 @@ target_link_libraries(restbed_xmr
lmdb lmdb
ringct ringct
common common
mysqlcppconn mysqlpp
mysqlclient
${Boost_LIBRARIES} ${Boost_LIBRARIES}
pthread pthread
unbound unbound

@ -15,12 +15,29 @@ using namespace restbed;
int int
main() main()
{ {
xmreg::MySqlConnector xmr_accounts; xmreg::MySqlAccounts xmr_accounts;
//xmr_accounts.create_account("41vEA7Ye8Bpeda6g59v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB"); //xmr_accounts.create_account("41vEA7Ye8Bpeda6g59v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB");
//xmr_accounts.select_account("41vEA7Ye8Bpeda6g59v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB");
xmreg::Account acc;
bool r = xmr_accounts.select_account("41vEA7Ye8Bpeda6g9v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB", acc);
if (r)
{
cout << "Account foudn: " << acc.id << endl;
}
else
{
cout << "Account does not exist" << endl;
}

@ -7,6 +7,8 @@
#include <mysql++/mysql++.h> #include <mysql++/mysql++.h>
#include <mysql++/ssqls.h>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -22,8 +24,6 @@ using namespace std;
#define MYSQL_EXCEPTION_MSG(sql_excetption) cerr << "# ERR: SQLException in " << __FILE__ \ #define MYSQL_EXCEPTION_MSG(sql_excetption) cerr << "# ERR: SQLException in " << __FILE__ \
<< "(" << __FUNCTION__ << ") on line " << __LINE__ << endl \ << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl \
<< "# ERR: " << sql_excetption.what() \ << "# ERR: " << sql_excetption.what() \
<< " (MySQL error code: " <<sql_excetption.getErrorCode() \
<< ", SQLState: " << sql_excetption.getSQLState() << " )" \
<< endl; << endl;
/** /**
@ -59,7 +59,7 @@ protected:
public: public:
MySqlConnector( MySqlConnector(
string _url = "tcp://127.0.0.1:3306", string _url = "127.0.0.1",
string _username = "root", string _username = "root",
string _password = "root", string _password = "root",
string _dbname = "yourmonero" string _dbname = "yourmonero"
@ -78,52 +78,80 @@ public:
if (conn.connect(dbname.c_str(), url.c_str(), if (conn.connect(dbname.c_str(), url.c_str(),
username.c_str(), password.c_str())) username.c_str(), password.c_str()))
{ {
cout << "connection successful" << endl; cout << "Connection to Mysql successful" << endl;
} }
} }
}; };
//
//
//class MySqlAccounts: public MySqlConnector
//{ // to see what it does can run preprecoess on this file
// static constexpr const char* TABLE_NAME = "Accounts"; // g++ -I/usr/include/mysql -E ~/restbed-xmr/src/MySqlConnector.h > /tmp/out.h
// sql_create_11(AccountRow, 1, 0,
// static constexpr const char* INSERT_STMT = R"( sql_int_unsigned , id,
// INSERT INTO `Accounts` (`address`) VALUES (?) sql_varchar , address,
// )"; sql_bigint_unsigned, total_received,
// sql_bigint_unsigned, scanned_height,
// static constexpr const char* SELECT_STMT = R"( sql_bigint_unsigned, scanned_block_height,
// SELECT * FROM `Accounts` WHERE `address` = ? sql_bigint_unsigned, start_height,
// )"; sql_bigint_unsigned, transaction_height,
// sql_bigint_unsigned, blockchain_height,
//public: sql_bigint_unsigned, total_sent,
// MySqlAccounts(): MySqlConnector() {} sql_timestamp , created,
// sql_timestamp , modified);
//
// bool struct Account : public AccountRow
// select_account(const string& address) {
// { using AccountRow::AccountRow;
// mysql_unqiue_ptr<PreparedStatement> prep_stmt {con->prepareStatement(SELECT_STMT)}; };
//
// prep_stmt->setString(1, address);
//
// try class MySqlAccounts: public MySqlConnector
// { {
// unique_ptr<ResultSet> res {prep_stmt->executeQuery()}; static constexpr const char* TABLE_NAME = "Accounts";
//
// cout << res->getRow() << endl; static constexpr const char* SELECT_STMT = R"(
// SELECT * FROM `Accounts` WHERE `address` = (%0q)
// } )";
// catch (SQLException& e)
// { static constexpr const char* INSERT_STMT = R"(
// MYSQL_EXCEPTION_MSG(e); INSERT INTO `Accounts` (`address`) VALUES (%0q)
// return false; )";
// }
//
// return true; public:
// } MySqlAccounts(): MySqlConnector() {}
//
bool
select_account(const string& address, Account& account)
{
Query query = conn.query(SELECT_STMT);
query.parse();
try
{
vector<Account> res;
query.storein(res, address);
if (!res.empty())
{
account = res.at(0);
return true;
}
}
catch (mysqlpp::Exception& e)
{
MYSQL_EXCEPTION_MSG(e);
}
return false;
}
// bool // bool
// create_account(const string& address) // create_account(const string& address)
// { // {
@ -165,8 +193,8 @@ public:
// //
// return false; // return false;
// } // }
//
//}; };
} }

Loading…
Cancel
Save