aboutsummaryrefslogtreecommitdiff
path: root/src/mysql.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mysql.c')
-rw-r--r--src/mysql.c48
1 files changed, 20 insertions, 28 deletions
diff --git a/src/mysql.c b/src/mysql.c
index d788a92..ac7e0f7 100644
--- a/src/mysql.c
+++ b/src/mysql.c
@@ -1,54 +1,54 @@
1/* This file is part of guile-sql. 1/* This file is part of guile-sql.
2 Copyright (C) 2002,2004,2005 Sergey Poznyakoff 2 Copyright (C) 2002, 2004, 2005 Sergey Poznyakoff
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify it
5 it under the terms of the GNU General Public License as published by 5 under the terms of the GNU General Public License as published by the
6 the Free Software Foundation; either version 2 of the License, or 6 Free Software Foundation; either version 3 of the License, or (at your
7 (at your option) any later version. 7 option) any later version.
8 8
9 This program is distributed in the hope that it will be useful, 9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details. 12 GNU General Public License for more details.
13 13
14 You should have received a copy of the GNU General Public License 14 You should have received a copy of the GNU General Public License along
15 along with this program; if not, write to the Free Software Foundation, Inc., 15 with this program. If not, see <http://www.gnu.org/licenses/>. */
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
17 16
18#ifdef HAVE_CONFIG_H 17#ifdef HAVE_CONFIG_H
19# include <config.h> 18# include <config.h>
20#endif 19#endif
21#include <string.h> 20#include <string.h>
22#include <guile-sql.h> 21#include <guile-sql.h>
23#include <mysql/mysql.h> 22#include <mysql/mysql.h>
24 23
25SCM 24static SCM
26s_mysql_mark(struct sql_connect *conn) 25s_mysql_mark(struct sql_connect *conn)
27{ 26{
28 return SCM_BOOL_F; 27 return SCM_BOOL_F;
29} 28}
30 29
31scm_sizet 30static scm_sizet
32s_mysql_free(struct sql_connect *conn) 31s_mysql_free(struct sql_connect *conn)
33{ 32{
34 MYSQL *mysql = (MYSQL*) conn->data; 33 MYSQL *mysql = (MYSQL*) conn->data;
35 if (!mysql) 34 if (!mysql)
36 return 0; 35 return 0;
37 mysql_close(mysql); 36 mysql_close(mysql);
38 return sizeof(MYSQL); 37 return sizeof(MYSQL);
39} 38}
40 39
41SCM 40static SCM
42s_mysql_connect (char *hostname, int port, 41s_mysql_connect (const char *hostname, int port,
43 char *dbname, char *user, char *pass, const char *why) 42 const char *dbname, const char *user, const char *pass,
43 const char *why)
44{ 44{
45 MYSQL *mysql; 45 MYSQL *mysql;
46 SCM smob; 46 SCM smob;
47 struct sql_connect *conn; 47 struct sql_connect *conn;
48 char *socket_path = NULL; 48 const char *socket_path = NULL;
49 49
50 mysql = mysql_init(NULL); 50 mysql = mysql_init(NULL);
51 if (!mysql) 51 if (!mysql)
52 scm_throw(gsql_error, 52 scm_throw(gsql_error,
53 scm_list_2(scm_makfrom0str("mysql_init() failed"), 53 scm_list_2(scm_makfrom0str("mysql_init() failed"),
54 scm_makfrom0str(""))); 54 scm_makfrom0str("")));
@@ -69,14 +69,14 @@ s_mysql_connect (char *hostname, int port,
69 smob = sql_connect_create("mysql"); 69 smob = sql_connect_create("mysql");
70 conn = (struct sql_connect *)SCM_CDR(smob); 70 conn = (struct sql_connect *)SCM_CDR(smob);
71 conn->data = mysql; 71 conn->data = mysql;
72 return smob; 72 return smob;
73} 73}
74 74
75SCM 75static SCM
76s_mysql_query(struct sql_connect *conn, char *query) 76s_mysql_query(struct sql_connect *conn, const char *query)
77{ 77{
78 MYSQL *mysql = conn->data; 78 MYSQL *mysql = conn->data;
79 MYSQL_RES *result; 79 MYSQL_RES *result;
80 SCM cell; 80 SCM cell;
81 81
82 if (mysql_query(mysql, query)) 82 if (mysql_query(mysql, query))
@@ -96,36 +96,28 @@ s_mysql_query(struct sql_connect *conn, char *query)
96 SCM head = SCM_EOL, tail; 96 SCM head = SCM_EOL, tail;
97 MYSQL_ROW row = mysql_fetch_row(result); 97 MYSQL_ROW row = mysql_fetch_row(result);
98 98
99 if (!row) 99 if (!row)
100 break; 100 break;
101 for (j = 0; j < nfields; j++) { 101 for (j = 0; j < nfields; j++) {
102 SCM new_elt; 102 SCM new_elt = scm_cons(scm_makfrom0str(row[j]),
103 SCM_NEWCELL(new_elt); 103 SCM_EOL);
104 SCM_SETCAR(new_elt, scm_makfrom0str(row[j]));
105 if (head == SCM_EOL) 104 if (head == SCM_EOL)
106 head = new_elt; 105 head = new_elt;
107 else 106 else
108 SCM_SETCDR(tail, new_elt); 107 SCM_SETCDR(tail, new_elt);
109 tail = new_elt; 108 tail = new_elt;
110 } 109 }
111 110
112 if (head != SCM_EOL) 111 new_row = scm_cons(head, SCM_EOL);
113 SCM_SETCDR(tail, SCM_EOL);
114
115 SCM_NEWCELL(new_row);
116 SCM_SETCAR(new_row, head);
117
118 if (row_head == SCM_EOL) 112 if (row_head == SCM_EOL)
119 row_head = new_row; 113 row_head = new_row;
120 else 114 else
121 SCM_SETCDR(row_tail, new_row); 115 SCM_SETCDR(row_tail, new_row);
122 row_tail = new_row; 116 row_tail = new_row;
123 } 117 }
124 if (row_head != SCM_EOL)
125 SCM_SETCDR(row_tail, SCM_EOL);
126 cell = row_head; 118 cell = row_head;
127 mysql_free_result(result); 119 mysql_free_result(result);
128 } else { /* should it have returned something? */ 120 } else { /* should it have returned something? */
129 if (mysql_field_count(mysql) == 0) { 121 if (mysql_field_count(mysql) == 0) {
130 cell = scm_makenum(mysql_affected_rows(mysql)); 122 cell = scm_makenum(mysql_affected_rows(mysql));
131 } else { /* mysql_store_result() should have returned data */ 123 } else { /* mysql_store_result() should have returned data */
@@ -134,13 +126,13 @@ s_mysql_query(struct sql_connect *conn, char *query)
134 scm_makfrom0str(mysql_error(mysql)))); 126 scm_makfrom0str(mysql_error(mysql))));
135 } 127 }
136 } 128 }
137 return cell; 129 return cell;
138} 130}
139 131
140void 132static void
141s_mysql_close(struct sql_connect *conn) 133s_mysql_close(struct sql_connect *conn)
142{ 134{
143 if (conn->data) 135 if (conn->data)
144 mysql_close(conn->data); 136 mysql_close(conn->data);
145 conn->data = NULL; 137 conn->data = NULL;
146} 138}

Return to:

Send suggestions and report system problems to the System administrator.