diff options
Diffstat (limited to 'src/sql.c')
| -rw-r--r-- | src/sql.c | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/src/sql.c b/src/sql.c new file mode 100644 index 0000000..3353b23 --- /dev/null +++ b/src/sql.c | |||
| @@ -0,0 +1,234 @@ | |||
| 1 | #include "sql.h" | ||
| 2 | #include "config.h" | ||
| 3 | #include "utils.h" | ||
| 4 | #include "da.h" | ||
| 5 | #include <libpq-fe.h> | ||
| 6 | #include <stdio.h> | ||
| 7 | |||
| 8 | static void notice_processor(void *arg, const char *message) | ||
| 9 | { | ||
| 10 | #ifdef VERBOSE | ||
| 11 | fprintf(stderr, "%s", message); | ||
| 12 | #endif | ||
| 13 | } | ||
| 14 | |||
| 15 | const char *sql_field_type_to_label(sql_oid oid) | ||
| 16 | { | ||
| 17 | const char *label = "unknown"; | ||
| 18 | |||
| 19 | switch (oid) { | ||
| 20 | case SQL_FIELD_TYPE_INT: label = "integer"; break; | ||
| 21 | case SQL_FIELD_TYPE_BIGINT: label = "bigint"; break; | ||
| 22 | case SQL_FIELD_TYPE_TEXT: label = "text"; break; | ||
| 23 | case SQL_FIELD_TYPE_VARCHAR: label = "character varrying"; break; | ||
| 24 | case SQL_FIELD_TYPE_DATE: label = "date"; break; | ||
| 25 | default: ERROR("received unknown oid: %d", oid); | ||
| 26 | } | ||
| 27 | |||
| 28 | return label; | ||
| 29 | } | ||
| 30 | |||
| 31 | struct open_connections { | ||
| 32 | SQL_CONNECTION *items; | ||
| 33 | size_t count; | ||
| 34 | size_t capacity; | ||
| 35 | }; | ||
| 36 | |||
| 37 | static struct open_connections open_connections; | ||
| 38 | |||
| 39 | static void finish_all_open_connections() | ||
| 40 | { | ||
| 41 | DA_FOREACH(connection, open_connections) | ||
| 42 | sql_disconnect(*connection); | ||
| 43 | } | ||
| 44 | |||
| 45 | void sql_init(void) { atexit(finish_all_open_connections); } | ||
| 46 | |||
| 47 | SQL_CONNECTION sql_connect(const char *conninfo) | ||
| 48 | { | ||
| 49 | LOG("connecting to database: %s", conninfo); | ||
| 50 | SQL_CONNECTION connection = PQconnectdb(conninfo); | ||
| 51 | if (PQstatus(connection) != CONNECTION_OK) { | ||
| 52 | ERROR("%s", PQerrorMessage(connection)); | ||
| 53 | return NULL; | ||
| 54 | } | ||
| 55 | |||
| 56 | DA_APPEND(open_connections, connection); | ||
| 57 | PQsetNoticeProcessor(connection, notice_processor, NULL); | ||
| 58 | |||
| 59 | return connection; | ||
| 60 | } | ||
| 61 | |||
| 62 | void sql_disconnect(SQL_CONNECTION connection) | ||
| 63 | { | ||
| 64 | LOG("disconnecting from database"); | ||
| 65 | DA_FOREACH(open_connection, open_connections) { | ||
| 66 | if (*open_connection != connection) continue; | ||
| 67 | size_t i = open_connection - open_connections.items; | ||
| 68 | memmove (open_connections.items + i, open_connections.items + i + 1, | ||
| 69 | (open_connections.count - i - 1)*(sizeof *open_connection)); | ||
| 70 | |||
| 71 | break; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | void sql_exec_command(SQL_CONNECTION connection, const char *query) | ||
| 76 | { | ||
| 77 | LOG("executing sql query: %s", query); | ||
| 78 | PGresult *res = PQexec(connection, query); | ||
| 79 | if (PQresultStatus(res) != PGRES_COMMAND_OK) | ||
| 80 | ERROR("sql command: %s: %s", query, PQerrorMessage(connection)); | ||
| 81 | PQclear(res); | ||
| 82 | } | ||
| 83 | |||
| 84 | SQL_RESULT sql_exec_tuples (SQL_CONNECTION connection, const char *query) | ||
| 85 | { | ||
| 86 | LOG("executing sql query: %s", query); | ||
| 87 | PGresult *res = PQexec(connection, query); | ||
| 88 | if (PQresultStatus(res) != PGRES_TUPLES_OK) { | ||
| 89 | ERROR("sql tuples: %s: %s", query, PQerrorMessage(connection)); | ||
| 90 | PQclear(res); | ||
| 91 | return NULL; | ||
| 92 | } return res; | ||
| 93 | } | ||
| 94 | |||
| 95 | sql_oid sql_result_field_type (SQL_RESULT res, size_t field ) { return PQftype (res, field ); } | ||
| 96 | void sql_result_free (SQL_RESULT res ) { return PQclear (res ); } | ||
| 97 | size_t sql_result_tuple_count (SQL_RESULT res ) { return PQntuples (res ); } | ||
| 98 | size_t sql_result_field_count (SQL_RESULT res ) { return PQnfields (res ); } | ||
| 99 | const char *sql_result_get_value (SQL_RESULT res, size_t row, size_t col) { return PQgetvalue (res, row, col); } | ||
| 100 | |||
| 101 | /////////////////////////////////////////////////////////////////////////////// | ||
| 102 | |||
| 103 | size_t sql_table_count_tuples (SQL_CONNECTION connection, | ||
| 104 | const char *table_name) | ||
| 105 | { | ||
| 106 | size_t count; | ||
| 107 | char *query = tprintf ("SELECT COUNT(*) FROM %s", table_name); | ||
| 108 | SQL_RESULT res = sql_exec_tuples (connection, query); | ||
| 109 | sscanf (sql_result_get_value (res, 0, 0), "%lu", &count); | ||
| 110 | sql_result_free (res); | ||
| 111 | return count; | ||
| 112 | } | ||
| 113 | |||
| 114 | void sql_print_result (SQL_RESULT res, bool print_types) | ||
| 115 | { | ||
| 116 | int field_count = sql_result_field_count (res); | ||
| 117 | int tuple_count = sql_result_tuple_count (res); | ||
| 118 | int field_widths[field_count]; | ||
| 119 | |||
| 120 | for (int field_idx = 0; field_idx < field_count; ++field_idx) { | ||
| 121 | int field_width = strlen (PQfname (res, field_idx)); | ||
| 122 | |||
| 123 | if (print_types) { | ||
| 124 | const char *field_type_label = sql_field_type_to_label (sql_result_field_type (res, field_idx)); | ||
| 125 | int fmod = PQfmod (res, field_idx); | ||
| 126 | int type_width = fmod == -1 | ||
| 127 | ? strlen (field_type_label) | ||
| 128 | : snprintf (NULL, 0, "%s(%d)", field_type_label, fmod); | ||
| 129 | if (type_width > field_width) field_width = type_width; | ||
| 130 | } | ||
| 131 | |||
| 132 | for (int tuple_idx = 0; tuple_idx < tuple_count; tuple_idx++) { | ||
| 133 | const char *value = PQgetvalue (res, tuple_idx, field_idx); | ||
| 134 | int tuple_field_width = display_width (value); | ||
| 135 | switch (PQftype (res, field_idx)) { | ||
| 136 | case SQL_FIELD_TYPE_TEXT: | ||
| 137 | case SQL_FIELD_TYPE_VARCHAR: | ||
| 138 | case SQL_FIELD_TYPE_DATE: | ||
| 139 | tuple_field_width += 2; | ||
| 140 | break; | ||
| 141 | default: break; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (tuple_field_width > field_width) | ||
| 145 | field_width = tuple_field_width; | ||
| 146 | } field_widths[field_idx] = field_width; | ||
| 147 | } | ||
| 148 | |||
| 149 | printf ("╭"); | ||
| 150 | for (int i = 0; i < field_widths[0] + 2; ++i) | ||
| 151 | printf ("─"); | ||
| 152 | for (int field_idx = 1; field_idx < field_count; ++field_idx) { | ||
| 153 | printf ("┬"); | ||
| 154 | for (int i = 0; i < field_widths[field_idx] + 2; ++i) | ||
| 155 | printf ("─"); | ||
| 156 | } printf ("╮\n"); | ||
| 157 | |||
| 158 | for (int field_idx = 0; field_idx < field_count; ++field_idx) { | ||
| 159 | printf ("│ %-*s ", field_widths[field_idx], PQfname (res, field_idx)); | ||
| 160 | } printf ("│\n"); | ||
| 161 | |||
| 162 | if (print_types) { | ||
| 163 | for (int field_idx = 0; field_idx < field_count; ++field_idx) { | ||
| 164 | int fmod = PQfmod (res, field_idx) - 4; | ||
| 165 | sql_oid field_type_oid = sql_result_field_type (res, field_idx); | ||
| 166 | const char *field_type = sql_field_type_to_label (field_type_oid); | ||
| 167 | int field_width = field_widths[field_idx]; | ||
| 168 | if (fmod > 0) { | ||
| 169 | int nchar = printf ("│ %s(%d)", field_type, fmod) - 2; | ||
| 170 | printf ("%*c", 1 + field_width - nchar, ' '); | ||
| 171 | } else | ||
| 172 | printf ("│ %-*s ", field_width, field_type); | ||
| 173 | } printf ("│\n"); | ||
| 174 | } | ||
| 175 | |||
| 176 | printf ("├"); | ||
| 177 | for (int i = 0; i < field_widths[0] + 2; ++i) | ||
| 178 | printf ("─"); | ||
| 179 | for (int field_idx = 1; field_idx < field_count; ++field_idx) { | ||
| 180 | printf ("┼"); | ||
| 181 | for (int i = 0; i < field_widths[field_idx] + 2; ++i) | ||
| 182 | printf ("─"); | ||
| 183 | } printf ("┤\n"); | ||
| 184 | |||
| 185 | for (int tuple_idx = 0; tuple_idx < tuple_count; tuple_idx++) { | ||
| 186 | for (int field_idx = 0; field_idx < field_count; field_idx++) { | ||
| 187 | int field_width = field_widths[field_idx]; | ||
| 188 | if (PQgetisnull (res, tuple_idx, field_idx)) { | ||
| 189 | printf ("│ %*c ", field_width, ' '); | ||
| 190 | continue; | ||
| 191 | } | ||
| 192 | |||
| 193 | const char *value = PQgetvalue (res, tuple_idx, field_idx); | ||
| 194 | switch (PQftype (res, field_idx)) { | ||
| 195 | case SQL_FIELD_TYPE_BIGINT: | ||
| 196 | case SQL_FIELD_TYPE_INT: | ||
| 197 | printf ("│ %*s ", field_width, value); | ||
| 198 | break; | ||
| 199 | case SQL_FIELD_TYPE_TEXT: | ||
| 200 | case SQL_FIELD_TYPE_VARCHAR: | ||
| 201 | case SQL_FIELD_TYPE_DATE: | ||
| 202 | printf ("│ '%s'%*c", value, | ||
| 203 | 1 + field_width - ((int) display_width (value) + 2), ' '); | ||
| 204 | break; | ||
| 205 | default: break; | ||
| 206 | } | ||
| 207 | } printf ("│\n"); | ||
| 208 | } | ||
| 209 | |||
| 210 | printf ("╰"); | ||
| 211 | for (int i = 0; i < field_widths[0] + 2; ++i) | ||
| 212 | printf ("─"); | ||
| 213 | for (int field_idx = 1; field_idx < field_count; ++field_idx) { | ||
| 214 | printf ("┴"); | ||
| 215 | for (int i = 0; i < field_widths[field_idx] + 2; ++i) | ||
| 216 | printf ("─"); | ||
| 217 | } printf ("╯\n"); | ||
| 218 | } | ||
| 219 | |||
| 220 | void sql_print_table_sample (SQL_CONNECTION connection, const char *table_name) | ||
| 221 | { | ||
| 222 | char *q = tprintf ("SELECT * FROM %s ORDER BY RANDOM() LIMIT %d", | ||
| 223 | table_name, SAMPLE_TABLE_LIMIT); | ||
| 224 | SQL_RESULT res = sql_exec_tuples (connection, q); | ||
| 225 | sql_print_result (res, true); | ||
| 226 | sql_result_free (res); | ||
| 227 | } | ||
| 228 | |||
| 229 | void sql_run_and_print_query (SQL_CONNECTION connection, const char *query) | ||
| 230 | { | ||
| 231 | SQL_RESULT res = sql_exec_tuples (connection, query); | ||
| 232 | sql_print_result (res, false); | ||
| 233 | sql_result_free (res); | ||
| 234 | } | ||
