Advertisement
Guest User

nanomysql.h

a guest
Feb 10th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. /* Copyright 2011 ZAO "Begun".
  2.  *
  3.  * This library is free software; you can redistribute it and/or modify it under
  4.  * the terms of the GNU Lesser General Public License as published by the Free
  5.  * Software Foundation; either version 3 of the License, or (at your option)
  6.  * any later version.
  7.  * This library is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  10.  * details.
  11.  * You should have received a copy of the GNU Lesser General Public License
  12.  * along with this library.  If not, see <http://www.gnu.org/licenses/>.
  13. */
  14.  
  15. #ifndef __NANOMYSQL_H
  16. #define __NANOMYSQL_H
  17.  
  18. #include <memory>
  19. using namespace std::placeholders;
  20.  
  21. namespace nanomysql {
  22.  
  23. class Connection {
  24.  
  25.     MYSQL* m_conn;
  26.  
  27.     void throw_error(std::string msg, const std::string& m2 = "")
  28.     {
  29.  
  30.         msg += ": ";
  31.         msg += ::mysql_error(m_conn);
  32.         msg += " : ";
  33.  
  34.         char n[32];
  35.         ::snprintf(n, 31, "%d", ::mysql_errno(m_conn));
  36.         msg += n;
  37.  
  38.         if (m2.size() > 0) {
  39.             msg += " : [";
  40.             msg += m2;
  41.             msg += "]";
  42.         }
  43.  
  44.         throw std::runtime_error(msg);
  45.     }
  46.  
  47.     void init_(const std::string& host, const std::string& user, const std::string& password,
  48.                const std::string& db, int port)
  49.     {
  50.         m_conn = ::mysql_init(NULL);
  51.  
  52.         if (!m_conn)
  53.             throw std::runtime_error("Could not mysql_init()");
  54.  
  55.         if (::mysql_real_connect(m_conn, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, NULL, 0) == NULL) {
  56.             throw_error("Could not mysql_real_connect()");
  57.         }
  58.     }
  59. public:
  60.  
  61.     struct Attributes {
  62.         std::string host;
  63.         std::string user;
  64.         std::string password;
  65.         std::string db;
  66.         int port;
  67.  
  68.         Attributes() : port(0) {}
  69.  
  70.         Attributes(const std::string& _host, const std::string& _user, const std::string& _password,
  71.                const std::string& _db = "", int _port = 0) :
  72.             host(_host), user(_user), password(_password), db(_db), port(_port)
  73.         {}
  74.     };
  75.  
  76.     Connection(const Attributes &attr)
  77.     {
  78.         init_(attr.host, attr.user, attr.password, attr.db, attr.port);
  79.     }
  80.  
  81.     Connection(const std::string& host, const std::string& user, const std::string& password,
  82.                const std::string& db = "", int port = 0)
  83.     {
  84.         init_(host, user, password, db, port);
  85.     }
  86.  
  87.     ~Connection()
  88.     {
  89.         ::mysql_close(m_conn);
  90.     }
  91.  
  92.     void query(const std::string& q)
  93.     {
  94.         if (::mysql_real_query(m_conn, q.data(), q.size()) != 0)
  95.             throw_error("mysql_query() failed", q);
  96.     }
  97.  
  98.  
  99.     struct _mysql_res_wrap {
  100.         MYSQL_RES* s;
  101.         _mysql_res_wrap(MYSQL_RES* _s) : s(_s) {}
  102.         ~_mysql_res_wrap() { if (s != NULL) ::mysql_free_result(s); }
  103.     };
  104.  
  105.     struct field {
  106.         std::string name;
  107.         enum enum_field_types type;
  108.         std::string data;
  109.  
  110.         field(const std::string& n, enum enum_field_types t) : name(n), type(t) {}
  111.  
  112.         /*
  113.         template <typename T>
  114.         operator T() {
  115.             T ret;
  116.             files::scn<files::string_as_buffer>(files::string_as_buffer(data, '\0')).scan(ret, "\0", 1);
  117.             return ret;
  118.         }
  119.         */
  120.     };
  121.  
  122.  
  123.     template <typename F>
  124.     void use(F f)
  125.     {
  126.         _mysql_res_wrap re(::mysql_use_result(m_conn));
  127.  
  128.         if (re.s == NULL) {
  129.             throw_error("mysql_use_result() failed");
  130.         }
  131.  
  132.         size_t num_fields = ::mysql_num_fields(re.s);
  133.  
  134.         std::map<std::string,field> fields;
  135.         std::vector<std::map<std::string,field>::iterator> fields_n;
  136.  
  137.         while (1) {
  138.             MYSQL_FIELD* ff = ::mysql_fetch_field(re.s);
  139.  
  140.             if (!ff) break;
  141.  
  142.             fields_n.push_back(
  143.                 fields.insert(fields.end(),
  144.                               std::make_pair(ff->name,
  145.                                              field(ff->name, ff->type))));
  146.         }
  147.  
  148.         while (1) {
  149.             MYSQL_ROW row = ::mysql_fetch_row(re.s);
  150.  
  151.             if (row == NULL) {
  152.                 if (::mysql_errno(m_conn) != 0) {
  153.                     throw_error("mysql_fetch_row() failed");
  154.                 }
  155.  
  156.                 break;
  157.             }
  158.  
  159.             unsigned long* lens = ::mysql_fetch_lengths(re.s);
  160.  
  161.             for (size_t z = 0; z != num_fields; ++z) {
  162.                 fields_n[z]->second.data.assign(row[z], lens[z]);
  163.             }
  164.  
  165.             f(fields);
  166.         }
  167.     }
  168.  
  169.     typedef std::map<std::string, field> value_t;
  170.     typedef std::vector< value_t > result_t;
  171.  
  172.     void store(result_t& out)
  173.     {
  174.     auto hack = std::bind<void(result_t::*)(const value_t&)>(&result_t::push_back, &out, std::placeholders::_1);
  175.         use(hack);
  176.     }
  177. };
  178.  
  179. }
  180.  
  181. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement