summaryrefslogtreecommitdiff
path: root/src/mypasql.c
blob: 50e9d26cfe012e1aa29c04b8976924a1db906416 (plain)
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
#include <winsock.h>
#include <stdio.h>
#include <mysql.h>

MYSQL *mysql;
MYSQL_RES *result = NULL;
MYSQL_ROW row;

int __stdcall mysql_Connect(char *server, char *user, char *pass)
{
    mysql = mysql_init(NULL);
    return (int) mysql_real_connect(mysql, server, user, pass, NULL, 0,
                                    NULL, 0);
}

int __stdcall mysql_SelectDb(char *db)
{
    return (int) mysql_select_db(mysql, db);
}

const char *__stdcall mysql_Error()
{
    return mysql_error(mysql);
}

int __stdcall mysql_Query(char *query)
{
    if (result) {
        mysql_free_result(result);
        result = NULL;
    }
    return (int) mysql_real_query(mysql, query, strlen(query));
}

int __stdcall mysql_NumRows()
{
    if (!result)
        result = mysql_store_result(mysql);
    return mysql_num_rows(result);
}

char *strip(char *str)
{
    char *c;
    if ((c = strrchr(str, '\n')))
        *c = 0;
    if ((c = strrchr(str, '\r')))
        *c = 0;
    return str;
}

void add_line(char **buf, char *line)
{
    int oldlen;
    char *tmp;

    if (*buf != NULL) {
        oldlen = strlen(*buf);
        tmp = malloc(oldlen + 1);
        strcpy(tmp, *buf);
        *buf = realloc(*buf, oldlen + strlen(line) + 1);
        strcpy(*buf, tmp);
        strcat(*buf, line);
        free(tmp);
    } else
        *buf = strdup(line);
}

int __stdcall mysql_LoadFromFile(char *file)
{
    FILE *fd = fopen(file, "r");
    char line[1024];
    char *query = NULL;


    if (!fd)
        return 0;
    while (fgets(line, 1024, fd)) {
        int len;
        strip(line);
        len = strlen(line);
        if (!*line || (*line == '-' && *(line + 1) == '-'))
            continue;
        else if (line[len - 1] == ';') {        /* End of a query */
            line[len - 1] = 0;
            add_line(&query, line);
            if (mysql_real_query(mysql, query, strlen(query))) {
                free(query);
                fclose(fd);
                return 0;
            }
            free(query);
            query = NULL;
        }
        else
            add_line(&query, line);
    }
    fclose(fd);
    return 1;
}

int __stdcall mysql_NumFields()
{
    if (!result)
        result = mysql_store_result(mysql);
    return mysql_num_fields(result);
}

int __stdcall mysql_FetchRow()
{
    if (!result)
        result = mysql_store_result(mysql);
    row = mysql_fetch_row(result);
    return (int) row;
}

char *__stdcall mysql_FetchField(int i)
{
    if (i >= mysql_num_fields(result))
        return NULL;
    else
        return row[i];
}