aboutsummaryrefslogtreecommitdiff
path: root/src/sql.c
blob: ff358a5855f7c00e87dd1748cbee40420f1fb6b3 (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
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
/* wydawca - FTP release synchronization daemon
   Copyright (C) 2007 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include "sql.h"

/* Singly-linked list of configured MySQL connections. */
struct sql_list
{
  struct sql_list *next;
  struct sqlconn conn;
};

static struct sql_list *sql_list;

/* Append CONN to the end of sql_list */
void
sql_register_conn (struct sqlconn *conn)
{
  struct sql_list *ent = xmalloc (sizeof *ent);
  ent->conn = *conn;
  ent->next = sql_list;
  sql_list = ent;
}

/* Find a configured connection that has the given IDENT */
struct sqlconn *
sql_find_connection (const char *ident)
{
  struct sql_list *p;
  for (p = sql_list; p; p = p->next)
    if (strcmp (p->conn.ident, ident) == 0)
      return &p->conn;
  return NULL;
}

/* Return true if there exists a connection with the given IDENT */
int
sql_connection_exists_p (const char *ident)
{
  return sql_find_connection (ident) != NULL;
}

/* Initialize MySQL access method */
int
sql_init_method (struct access_method *method)
{
  struct sqlconn *conn = sql_find_connection (method->param[0]);

  if (!conn)
    {
      logmsg (LOG_EMERG, "INTERNAL ERROR: cannot find SQL connection %s",
	      method->param[0]);
      abort ();
    }

  if (conn->initcount++ == 0)
    {
      mysql_init (&conn->mysql);
      if (!mysql_real_connect (&conn->mysql, conn->host, conn->user,
			       conn->password, conn->database, conn->port,
			       conn->socket, 0))
	{
	  logmsg (LOG_ERR, "Failed to connect to database %s: Error: %s\n",
		  method->param[0], mysql_error (&conn->mysql));
	  return 1;
	}
    }
  method->v.sqlconn = conn;
  return 0;
}

int
sql_free_result (struct access_method *method)
{
  struct sqlconn *conn = method->v.sqlconn;
  if (conn->result)
    {
      mysql_free_result (conn->result);
      conn->result = NULL;
    }
  return 0;
}
    
/* Finish the initialized MySQL access method */
int
sql_done_method (struct access_method *method)
{
  struct sqlconn *conn = method->v.sqlconn;
  if (!conn || conn->initcount == 0)
    return 0;
  if (--conn->initcount)
    return 0;
  sql_free_result (method);
  mysql_close (&conn->mysql);
  return 0;
}

/* Execute QUERY using the given access METHOD. Return 0 on success. */  
int
sql_run_method (struct access_method *method, const char *query)
{
  struct sqlconn *conn = method->v.sqlconn;
  MYSQL *mysql = &conn->mysql;
  
  if (mysql_query (mysql, query))
    {
      logmsg (LOG_ERR, "Query failed: %s", mysql_error (mysql));
      logmsg (LOG_NOTICE, "The failed query was: %s", query);
      return 1;
    }
  
  conn->result = mysql_store_result (mysql);
  if (!conn->result)
    {
      logmsg (LOG_ERR, "Cannot get result: %s", mysql_error (mysql));
      logmsg (LOG_NOTICE, "The failed query was: %s", query);
      return 1;
    }

  method->nrow = mysql_num_rows (conn->result);
  method->ncol = mysql_num_fields (conn->result);
  if (debug_level > 1)
    {
      logmsg (LOG_DEBUG, "Query returned %u columns in %u rows",
	      method->ncol, method->nrow);
      logmsg (LOG_DEBUG, "The query was: %s", query);
    }

  return 0;
}
    
int
sql_get_method (struct access_method *method, unsigned nrow, unsigned ncol)
{
  struct sqlconn *conn = method->v.sqlconn;
  MYSQL_ROW row;
  size_t len;

  if (!conn->result)
    return 1;
  mysql_data_seek (conn->result, nrow);
  row = mysql_fetch_row (conn->result);
  if (row[ncol] == NULL)
    len = 0;
  else
    len = trim_length (row[ncol]);

  method_copy_result (method, row[ncol], len);
  return 0;
}
	      

Return to:

Send suggestions and report system problems to the System administrator.