Anope IRC Services  Version 1.8
mypasql.c
Go to the documentation of this file.
1 #include <winsock.h>
2 #include <stdio.h>
3 #include <mysql.h>
4 
5 MYSQL *mysql;
6 MYSQL_RES *result = NULL;
7 MYSQL_ROW row;
8 
9 int __stdcall mysql_Connect(char *server, char *user, char *pass)
10 {
11  mysql = mysql_init(NULL);
12  return (int) mysql_real_connect(mysql, server, user, pass, NULL, 0,
13  NULL, 0);
14 }
15 
16 int __stdcall mysql_SelectDb(char *db)
17 {
18  return (int) mysql_select_db(mysql, db);
19 }
20 
21 const char *__stdcall mysql_Error()
22 {
23  return mysql_error(mysql);
24 }
25 
26 int __stdcall mysql_Query(char *query)
27 {
28  if (result) {
29  mysql_free_result(result);
30  result = NULL;
31  }
32  return (int) mysql_real_query(mysql, query, strlen(query));
33 }
34 
35 int __stdcall mysql_NumRows()
36 {
37  if (!result)
38  result = mysql_store_result(mysql);
39  return mysql_num_rows(result);
40 }
41 
42 char *strip(char *str)
43 {
44  char *c;
45  if ((c = strrchr(str, '\n')))
46  *c = 0;
47  if ((c = strrchr(str, '\r')))
48  *c = 0;
49  return str;
50 }
51 
52 void add_line(char **buf, char *line)
53 {
54  int oldlen;
55  char *tmp;
56 
57  if (*buf != NULL) {
58  oldlen = strlen(*buf);
59  tmp = malloc(oldlen + 1);
60  strcpy(tmp, *buf);
61  *buf = realloc(*buf, oldlen + strlen(line) + 1);
62  strcpy(*buf, tmp);
63  strcat(*buf, line);
64  free(tmp);
65  } else
66  *buf = strdup(line);
67 }
68 
69 int __stdcall mysql_LoadFromFile(char *file)
70 {
71  FILE *fd = fopen(file, "r");
72  char line[1024];
73  char *query = NULL;
74 
75 
76  if (!fd)
77  return 0;
78  while (fgets(line, 1024, fd)) {
79  int len;
80  strip(line);
81  len = strlen(line);
82  if (!*line || (*line == '-' && *(line + 1) == '-'))
83  continue;
84  else if (line[len - 1] == ';') { /* End of a query */
85  line[len - 1] = 0;
86  add_line(&query, line);
87  if (mysql_real_query(mysql, query, strlen(query))) {
88  free(query);
89  fclose(fd);
90  return 0;
91  }
92  free(query);
93  query = NULL;
94  }
95  else
96  add_line(&query, line);
97  }
98  fclose(fd);
99  return 1;
100 }
101 
102 int __stdcall mysql_NumFields()
103 {
104  if (!result)
105  result = mysql_store_result(mysql);
106  return mysql_num_fields(result);
107 }
108 
109 int __stdcall mysql_FetchRow()
110 {
111  if (!result)
112  result = mysql_store_result(mysql);
113  row = mysql_fetch_row(result);
114  return (int) row;
115 }
116 
117 char *__stdcall mysql_FetchField(int i)
118 {
119  if (i >= mysql_num_fields(result))
120  return NULL;
121  else
122  return row[i];
123 }
E char * strdup(const char *s)
Definition: compat.c:94
int __stdcall mysql_LoadFromFile(char *file)
Definition: mypasql.c:69
char *__stdcall mysql_FetchField(int i)
Definition: mypasql.c:117
char * strip(char *str)
Definition: mypasql.c:42
int __stdcall mysql_FetchRow()
Definition: mypasql.c:109
int __stdcall mysql_NumFields()
Definition: mypasql.c:102
Command * c
Definition: ns_recover.c:17
void add_line(char **buf, char *line)
Definition: mypasql.c:52
int __stdcall mysql_Query(char *query)
Definition: mypasql.c:26
MYSQL_RES * result
Definition: mypasql.c:6
int __stdcall mysql_NumRows()
Definition: mypasql.c:35
MYSQL_ROW row
Definition: mypasql.c:7
int __stdcall mysql_SelectDb(char *db)
Definition: mypasql.c:16
int __stdcall mysql_Connect(char *server, char *user, char *pass)
Definition: mypasql.c:9
MYSQL * mysql
Definition: mypasql.c:5
const char *__stdcall mysql_Error()
Definition: mypasql.c:21