public
Description: Friendly neighborhood wrapper around the MySQL C API.
Homepage:
Clone URL: git://github.com/mleung/old_school.git
old_school / old_school.c
100644 200 lines (161 sloc) 5.7 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* old_school.c
* old_school - Wraps the MySQL C API in an easy to use library.
*
* Created by Michael Leung on 6/6/08.
* Copyright 2008. All rights reserved.
*
* Compile this bad boy on OS X: gcc -I/usr/local/mysql/include old_school.c -L/usr/local/mysql/lib -lmysqlclient -lm -o test
*
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "old_school.h"
#include "utilities.c"
 
// Being used to test the lib.
int main(int argc, char** argv) {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    
    // TODO: Extract this out to a config file and read it.
    if (conn = do_connect("localhost", "root", NULL, "urbis_development")) {
        if (res = find("items", "id, title", "id = 1", conn, NULL, NULL)) {
            while ((row = mysql_fetch_row(res)) != NULL) {
             printf("%s \n", row[1]);
            }
        }
        mysql_free_result(res);
        
        long rowsAffected = insert("items", "id, title", "99999, 'test'", conn);
        printf("%d rows affected by insert\n", rowsAffected);
        
        rowsAffected = update("items", "title = 'testy!'", "title = 'test'", conn);
        printf("%d rows affected by update\n", rowsAffected);
 
        rowsAffected = delete("items", "title = 'testy!'", conn);
        printf("%d rows affected by delete\n", rowsAffected);
        
        mysql_close(conn);
    }
}
 
 MYSQL* do_connect(char *server, char *user, char *pwd, char *database) {
 
  MYSQL *conn;
conn = mysql_init(NULL);
 
if (conn == NULL) {
        printf("An error occurred while trying to connect to MySQL. So sorry.");
        return NULL;
}
 
  if (!mysql_real_connect(conn, server, user, pwd, database, 0, NULL, 0)) {
  fprintf(stderr, "%s\n", mysql_error(conn));
        return NULL;
  }
 
    return conn;
   
 }
 
 // Think about passing in a struct here soon. The param list is getting a bit out of control.
 MYSQL_RES* find(char *table, char *fields, char *conditions, MYSQL *conn, char *limit, char *joins) {
 
     if (!valid_string(table)) {
         printf("You must supply a table name");
         return NULL;
     }
     
     // TODO: Check connection before using.
 
     char query[MAX_QUERY_SIZE] = "";
     MYSQL_RES *res;
     char *fieldList;
     char *selectStatement;
     char *fromStatement;
     
     fieldList = fields != NULL ? fields : "*";
    
     // This builds up the query string so the user doesn't have to.
     strcat(query, "SELECT ");
     strcat(query, fieldList);
     strcat(query, " FROM ");
     strcat(query, table);
     if (valid_string(joins)) {
         // Concat a space so the user can just type like "INNER JOIN blah ON blah.id = blahblah.id"
         strcat(query, " ");
         strcat(query, joins);
     }
     if (valid_string(conditions)) {
         strcat(query, " WHERE ");
         strcat(query, conditions);
     }
     if (valid_string(limit)) {
         strcat(query, " LIMIT ");
         strcat(query, limit);
     }
     
     // Call and return the find query.
     if (run_query(conn, query)) {
         return mysql_use_result(conn);
     }
     
     return NULL;
 }
 
long insert(char *table, char *fields, char *values, MYSQL *conn) {
    /* TODO: Extract this to a separate function, as it's used elsewhere.
* In fact, maybe we should have a generic function for checking a value
* and printing a message.
*/
    if (!valid_string(table)) {
        printf("You must supply a table to insert into.");
        return 0;
    }
    if (!valid_string(fields)) {
        printf("You must supply a list of fields to insert.");
        return 0;
    }
    if (!valid_string(values)) {
        printf("You must supply a list of values to insert.");
        return 0;
    }
 
    char query[MAX_QUERY_SIZE] = "";
   
    // This builds up the query string so the user doesn't have to.
    strcat(query, "INSERT INTO ");
    strcat(query, table);
    strcat(query, "(");
    strcat(query, fields);
    strcat(query, ")");
    strcat(query, " VALUES(");
    strcat(query, values);
    strcat(query, ")");
    
    return run_query(conn, query);
}
 
long update(char *table, char *statement, char *conditions, MYSQL *conn) {
    if (!valid_string(table)) {
        printf("You must supply a table to update.");
        return 0;
    }
    if (!valid_string(statement)) {
        printf("You must supply an update statment.");
        return 0;
    }
    
    char query[MAX_QUERY_SIZE] = "";
   
    // This builds up the query string so the user doesn't have to.
    strcat(query, "UPDATE ");
    strcat(query, table);
    strcat(query, " SET ");
    strcat(query, statement);
    // FIXME: This is being used in other places verbatim. Not too DRY, my man. Extract method.
    if (valid_string(conditions)) {
        strcat(query, " WHERE ");
        strcat(query, conditions);
    }
 
    return run_query(conn, query);
}
 
long delete(char *table, char *conditions, MYSQL *conn) {
    if (!valid_string(table)) {
        printf("You must supply a table to delete from.");
        return 0;
    }
 
    char query[MAX_QUERY_SIZE] = "";
 
    // This builds up the query string so the user doesn't have to.
    strcat(query, "DELETE FROM ");
    strcat(query, table);
    // FIXME: This is being used in other places verbatim. Not too DRY, my man. Extract method.
    if (valid_string(conditions)) {
        strcat(query, " WHERE ");
        strcat(query, conditions);
    }
 
    return run_query(conn, query);
    
}
 
long run_query(MYSQL *conn, char *query) {
    if (mysql_query(conn, query)) {
     fprintf(stderr, "%s\n", mysql_error(conn));
     return 0;
    }
    
    return (long) mysql_affected_rows(conn);
}