Index: trunk/zedler/add_torrent/add_torrent.cc |
— | — | @@ -0,0 +1,105 @@ |
| 2 | +#include <iostream> |
| 3 | +#include <boost/format.hpp> |
| 4 | +#include <mysql.h> |
| 5 | + |
| 6 | +int |
| 7 | +main(int argc, char **argv) |
| 8 | +{ |
| 9 | + if (!argv[1]) { |
| 10 | + std::cerr << boost::format("usage: %s <hash>\n") % argv[0]; |
| 11 | + return 1; |
| 12 | + } |
| 13 | + |
| 14 | + std::string hash(argv[1]); |
| 15 | + |
| 16 | + MYSQL *conn; |
| 17 | + if ((conn = mysql_init(NULL)) == NULL) { |
| 18 | + std::cerr << "out of memory\n"; |
| 19 | + return 1; |
| 20 | + } |
| 21 | + |
| 22 | + if (mysql_options(conn, MYSQL_READ_DEFAULT_GROUP, "client")) { |
| 23 | + std::cerr << "cannot set default group\n"; |
| 24 | + return 1; |
| 25 | + } |
| 26 | + |
| 27 | + if (mysql_real_connect(conn, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL) { |
| 28 | + std::cerr << boost::format("Cannot connect to MySQL: %s\n") |
| 29 | + % mysql_error(conn); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + MYSQL_STMT *stmt(mysql_stmt_init(conn)); |
| 34 | + if (stmt == NULL) { |
| 35 | + std::cerr << "out of memory\n"; |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + static std::string stmtstr("SELECT xbt_tracker.add_torrent(?)"); |
| 40 | + if (mysql_stmt_prepare(stmt, stmtstr.c_str(), stmtstr.size())) { |
| 41 | + std::cerr << boost::format("Cannot prepare statement: %s\n") |
| 42 | + % mysql_stmt_error(stmt); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + MYSQL_BIND values[1]; |
| 47 | + std::memset(values, 0, sizeof(values)); |
| 48 | + my_bool isnull = 0; |
| 49 | + unsigned long hlen = hash.size(); |
| 50 | + values[0].buffer_type = MYSQL_TYPE_STRING; |
| 51 | + values[0].buffer = (void *) hash.data(); |
| 52 | + values[0].buffer_length = hash.size(); |
| 53 | + values[0].length = &hlen; |
| 54 | + values[0].is_null = &isnull; |
| 55 | + |
| 56 | + if (mysql_stmt_bind_param(stmt, values)) { |
| 57 | + std::cerr << boost::format("Cannot bind value: %s\n") |
| 58 | + % mysql_stmt_error(stmt); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + char errstr[256]; |
| 63 | + unsigned long errlen; |
| 64 | + my_bool trunc; |
| 65 | + MYSQL_BIND result; |
| 66 | + std::memset(&result, 0, sizeof(result)); |
| 67 | + result.buffer_type = MYSQL_TYPE_STRING; |
| 68 | + result.buffer = errstr; |
| 69 | + result.buffer_length = sizeof(errstr); |
| 70 | + result.length = &errlen; |
| 71 | + result.is_null = &isnull; |
| 72 | + result.error = &trunc; |
| 73 | + |
| 74 | + if (mysql_stmt_bind_result(stmt, &result)) { |
| 75 | + std::cerr << boost::format("Cannot bind result: %s\n") |
| 76 | + % mysql_stmt_error(stmt); |
| 77 | + return 1; |
| 78 | + } |
| 79 | + |
| 80 | + if (mysql_stmt_execute(stmt)) { |
| 81 | + std::cerr << boost::format("Cannot execute statement: %s\n") |
| 82 | + % mysql_stmt_error(stmt); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + switch (mysql_stmt_fetch(stmt)) { |
| 87 | + case 0: |
| 88 | + case MYSQL_DATA_TRUNCATED: |
| 89 | + if (strlen(errstr)) { |
| 90 | + std::cerr << boost::format("Error adding torrent: %s\n") |
| 91 | + % errstr; |
| 92 | + return 1; |
| 93 | + } |
| 94 | + return 0; |
| 95 | + case 1: |
| 96 | + std::cerr << boost::format("Cannot fetch result: %s\n") |
| 97 | + % mysql_stmt_error(stmt); |
| 98 | + return 1; |
| 99 | + case MYSQL_NO_DATA: |
| 100 | + std::cerr << "Unexpectedly got no result from adding torrent!\n"; |
| 101 | + return 1; |
| 102 | + } |
| 103 | + |
| 104 | + mysql_stmt_close(stmt); |
| 105 | + mysql_close(conn); |
| 106 | +} |
Index: trunk/zedler/add_torrent/Makefile |
— | — | @@ -0,0 +1,2 @@ |
| 2 | +add_torrent: add_torrent.cc |
| 3 | + g++ $(shell mysql_config --cflags) add_torrent.cc -o add_torrent $(shell mysql_config --libs) |