void Config::_loadFromDB() { Mysql sql; if (!connectDb(sql)) { printf("Cannot connect to db, check db parameters\n"); throw std::exception(); } enum EServerFlds { FLD_IP, FLD_PORT, FLD_STATUS }; auto res = sql.query(sql.createQuery() << "SELECT ip, port, status FROM storage WHERE id=" << ESC << _serverID); if (!res || !res->next()) { printf("Cannot get information about %u storage\n", _serverID); throw std::exception(); } _listenIp = res->get<decltype(_listenIp)>(FLD_IP); _port = res->get<decltype(_port)>(FLD_PORT); _storageStatus = res->get<decltype(_storageStatus)>(FLD_STATUS); if (!(_storageStatus & ST_STORAGE_ACTIVE)) { printf("Cannot load an inactive storage %u\n", _serverID); throw std::exception(); } }
void *Mysql_create(char *host, char *user, char *pass, char *db) { MYSQL *con = mysql_init(NULL); if (con == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); exit(1); } Mysql proto = { .host = host, .user = user, .pass = pass, .db = db, .con = con, .init = Mysql_init, .query = Mysql_query, .printResults = Mysql_results, .close = Mysql_close }; Mysql *m = calloc(1, sizeof(Mysql)); *m = proto; if (mysql_real_connect(m->con, host, user, pass, db, 0, NULL, 0) == NULL) { finish_with_error(m); } if (!m->init(m)) { // looks like it didn't initialize properly m->close(m); return NULL; } else { // all done, we made an object of any type return m; } }
std::vector<Song> Song::getAllSongs_noBell() { Mysql db; MYSQL_RES* res = db.query("SELECT id FROM song WHERE id > 16"); MYSQL_ROW row; std::vector<Song> rtr; while (row = db.next_res(res)) { Song s(db.stringInt(row[0])); rtr.push_back(s); } db.free_res(res); return rtr; }