| 1 | /** |
| 2 | * \file stats/db.c |
| 3 | * \brief SQLite3 database storage functions |
| 4 | * |
| 5 | * Copyright (c) 2011 Robert Au <[email protected]> |
| 6 | * |
| 7 | * This work is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of either: |
| 9 | * |
| 10 | * a) the GNU General Public License as published by the Free Software |
| 11 | * Foundation, version 2, or |
| 12 | * |
| 13 | * b) the "Angband licence": |
| 14 | * This software may be copied and distributed for educational, |
| 15 | * research, |
| 16 | * and not for profit purposes provided that this copyright and |
| 17 | * statement |
| 18 | * are included in all such copies. Other copyrights may also apply. |
| 19 | */ |
| 20 | |
| 21 | #include <sqlite3.h> |
| 22 | #include <time.h> |
| 23 | #include <sys/stat.h> |
| 24 | |
| 25 | #include "angband.h" |
| 26 | #include "init.h" |
| 27 | #include "stats/db.h" |
| 28 | |
| 29 | /** |
| 30 | * Module state variables |
| 31 | */ |
| 32 | static sqlite3 *db; |
| 33 | static char *ANGBAND_DIR_STATS; |
| 34 | static char *db_filename; |
| 35 | |
| 36 | /** |
| 37 | * Utility functions |
| 38 | */ |
| 39 | static bool stats_make_output_dir(void) { |
| 40 | size_t size = strlen(ANGBAND_DIR_USER) + strlen(PATH_SEP) + 6; |
| 41 | ANGBAND_DIR_STATS = mem_alloc(size * sizeof(char)); |
| 42 | path_build(ANGBAND_DIR_STATS, size, ANGBAND_DIR_USER, "stats"); |
| 43 | |
| 44 | if (dir_create(ANGBAND_DIR_STATS)) { |
| 45 | return true; |
| 46 | } else { |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * ------------------------------------------------------------------------ |
| 53 | * Interface functions |
| 54 | * ------------------------------------------------------------------------ */ |
| 55 | |
| 56 | /** |
| 57 | * Call stats_db_open first to create the database file and set up a |
| 58 | * database connection. Returns true on success, false on failure. |
| 59 | */ |
| 60 | bool stats_db_open(void) { |
| 61 | size_t size; |
| 62 | char filename_buf[20]; |
| 63 | int result; |
| 64 | time_t now_time = time(NULL); |
| 65 | struct tm *now = localtime(&now_time); |
| 66 | |
| 67 | if (!stats_make_output_dir()) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | size = strlen(ANGBAND_DIR_STATS) + strlen(PATH_SEP) + 20; |
| 72 | db_filename = mem_alloc(size * sizeof(char)); |
| 73 | strnfmt(filename_buf, 20, "%4d-%02d-%02dT%02d:%02d.db", |
| 74 | now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, |
| 75 | now->tm_hour, now->tm_min); |
| 76 | path_build(db_filename, size, ANGBAND_DIR_STATS, filename_buf); |
| 77 | |
| 78 | if (file_exists(db_filename)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | result = sqlite3_open(db_filename, &db); |
| 83 | if (result) { |
| 84 | sqlite3_close(db); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Call stats_close_db to close the database connection and free |
| 93 | * module variables. |
| 94 | */ |
| 95 | bool stats_db_close(void) { |
| 96 | sqlite3_close(db); |
| 97 | mem_free(ANGBAND_DIR_STATS); |
| 98 | mem_free(db_filename); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Evaluate a sqlite3 SQL statement on the previously opened database. |
| 104 | * The argument sql_str should contain the SQL statement, encoded as UTF-8. |
| 105 | * The return value is zero on success or a sqlite3 error code on failure. |
| 106 | * This function is a wrapper around sqlite3_exec and is for statements |
| 107 | * that don't expect output. Use stats_db_stmt_prep(), sqlite3_bind_*(), and |
| 108 | * sqlite3_step() for more complex statements. |
| 109 | */ |
| 110 | |
| 111 | int stats_db_exec(const char *sql_str) { |
| 112 | assert(strlen(sql_str) > 0); |
| 113 | return sqlite3_exec(db, sql_str, NULL, NULL, NULL); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Prepare a sqlite3 SQL statement for evaluation. sql_stmt should be a |
| 118 | * non-NULL, unallocated pointer. The sqlite3 library will allocate memory |
| 119 | * appropriately. The caller should later delete this statement with |
| 120 | * sqlite3_finalize(). sql_str should contain the SQL statement, encoded |
| 121 | * as UTF-8. The function returns 0 on success or a sqlite3 error code on |
| 122 | * failure. |
| 123 | */ |
| 124 | |
| 125 | int stats_db_stmt_prep(sqlite3_stmt **sql_stmt, const char *sql_str) { |
| 126 | assert(strlen(sql_str) > 0); |
| 127 | assert(sql_stmt != NULL); |
| 128 | return sqlite3_prepare_v2(db, sql_str, strlen(sql_str), |
| 129 | sql_stmt, NULL); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Utility function for binding many ints at once. The offset argument |
| 134 | * should be the number of columns to skip before starting to bind. |
| 135 | * Arguments after num_cols should be a series of ints to be bound to |
| 136 | * parameters in sql_stmt, in order. |
| 137 | */ |
| 138 | |
| 139 | int stats_db_bind_ints(sqlite3_stmt *sql_stmt, int num_cols, int offset, ...) { |
| 140 | va_list vp; |
| 141 | int err = SQLITE_OK; |
| 142 | int col; |
| 143 | |
| 144 | va_start(vp, offset); |
| 145 | for (col = offset + 1; col <= offset + num_cols; col++) { |
| 146 | uint32_t value = va_arg(vp, uint32_t); |
| 147 | err = sqlite3_bind_int(sql_stmt, col, value); |
| 148 | if (err) { |
| 149 | va_end(vp); |
| 150 | return err; |
| 151 | } |
| 152 | } |
| 153 | va_end(vp); |
| 154 | |
| 155 | return err; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Utility function for binding a random_value to a parameter as TEXT. |
| 160 | * Note optimization for storing values without randomness. |
| 161 | */ |
| 162 | int stats_db_bind_rv(sqlite3_stmt *sql_stmt, int col, random_value rv) { |
| 163 | char sql_buf[256]; |
| 164 | |
| 165 | if (rv.dice || rv.sides || rv.m_bonus) { |
| 166 | strnfmt(sql_buf, 256, "%d+%dd%dM%d", rv.base, rv.dice, |
| 167 | rv.sides, rv.m_bonus); |
| 168 | } else { |
| 169 | strnfmt(sql_buf, 256, "%d", rv.base); |
| 170 | } |
| 171 | return sqlite3_bind_text(sql_stmt, col, sql_buf, strlen(sql_buf), |
| 172 | SQLITE_STATIC); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * I have chosen not to wrap the other sqlite3 core interfaces, since |
| 177 | * they do not require access to the database connection object db. |
| 178 | * The key functions are |
| 179 | * |
| 180 | * int sqlite3_bind_<type>(sqlite3_stmt *, int, ...) |
| 181 | * Given a previously prepared statement, bind a parameter to a value |
| 182 | * of a given type: blob, double, int, int64, null, text, text16, |
| 183 | * value, zeroblob. The second argument is the index of the parameter. |
| 184 | * |
| 185 | * int sqlite3_step(sqlite3_stmt *) |
| 186 | * Evaluate the given statement. Returns SQLITE_DONE on successful |
| 187 | * execution completion. For statements that return data, returns |
| 188 | * SQLITE_ROW if a new row of data is ready for processing. Call the |
| 189 | * function multiple times to retrieve all the rows. It may also return |
| 190 | * other sqlite3 error codes. |
| 191 | * |
| 192 | * <C type> sqlite3_column_<sqlite3 type>(sqlite3_stmt *, int) |
| 193 | * Retrieve information about a column of the current result row of a |
| 194 | * query. The second argument is the index of the column. Available |
| 195 | * types are blob, double, int, int64, text, text16, and value. |
| 196 | * Routines attempt unit conversion where appropriate. Use |
| 197 | * sqlite3_column_type() to determine the datatype code of a column, |
| 198 | * and sqlite3_column_bytes() or sqlite3_column_bytes16() to determikne |
| 199 | * the number of bytes in a blob or string. |
| 200 | * |
| 201 | * int sqlite3_reset(sqlite3_stmt *) |
| 202 | * Reset the given statement to its initial state, so that it may be |
| 203 | * re-executed. Does not alter bindings. |
| 204 | * |
| 205 | * int sqlite3_clear_bindings(sqlite3_stmt *) |
| 206 | * Reset all parameters for the given statement to NULL. |
| 207 | * |
| 208 | * int sqlite3_finalize(sqlite3_stmt *) |
| 209 | * Delete the given prepared statement. |
| 210 | */ |