aboutsummaryrefslogtreecommitdiff
path: root/src/method.c
blob: 8fe81fb9f08d650edd609e89919e55b12640448d (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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* wydawca - automatic release submission daemon
   Copyright (C) 2007 Sergey Poznyakoff

   Wydawca 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.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

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

struct method_descr
{
  const char *name;

  int (*init) (struct access_method *);
  int (*done) (struct access_method *);
  int (*free) (struct access_method *, void *);

  void *(*open) (struct access_method *);
  int (*close) (struct access_method *, void *);

  int (*get)  (struct access_method *, void *, unsigned, unsigned);
  int (*run) (struct access_method *, void *, const char *);
  int (*quote) (struct access_method *, void *, const char *, char **, size_t *);
};

static struct method_descr method_tab[] = {
  { "none", NULL, NULL, NULL, NULL, NULL, NULL, NULL },
  { "sql", sql_init_method, sql_done_method, sql_free_result,
    sql_open, NULL, sql_get_method, sql_run_method, sql_quote },
  { "builtin", builtin_init, builtin_done, builtin_free_result,
    builtin_open, NULL,
    builtin_get,
    builtin_run },
  { "external", NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};

struct access_method *
method_new (enum access_method_id id, enum access_method_type type)
{
  struct access_method *mp = xmalloc (sizeof mp[0]);
  memset (mp, 0, sizeof mp[0]);
  mp->id = id;
  mp->type = type;
  return mp;
}

int
method_init (struct access_method *method)
{
  struct method_descr *mp = method_tab + method->type;
  int rc = 0;

  if (method->init_passed++)
    return 0;
  if (debug_level > 1)
    {
      int i;
      logmsg (LOG_DEBUG, "Initializing method: %s \"%s\"",
	      mp->name, SP (method->query));
      for (i = 0; i < method->parmc; i++)
	logmsg (LOG_DEBUG, " parmv[%d]=%s", i, method->parmv[i]);
    }
  if (mp->init)
    rc = mp->init (method);
  if (rc == 0)
    method->init_passed = 1;
  return rc;
}

void *
method_open (struct access_method *method)
{
  struct method_descr *mp = method_tab + method->type;

  if (!mp->open)
    return NULL;
  return mp->open (method);
}

int
method_close (struct access_method *method, void *handle)
{
  struct method_descr *mp = method_tab + method->type;
  if (!mp->close)
    return 0;
  return mp->close (method, handle);
}

int
method_done (struct access_method *method)
{
  struct method_descr *mp = method_tab + method->type;
  int rc = 0;

  if (method->init_passed == 0)
    return 0;
  if (--method->init_passed)
    return 0;
  if (debug_level > 1)
    {
      int i;
      logmsg (LOG_DEBUG, "Closing method: %s \"%s\"",
	      mp->name, SP (method->query));
      for (i = 0; i < method->parmc; i++)
	logmsg (LOG_DEBUG, " parmv[%d]=%s", i, method->parmv[i]);
    }
  if (mp->done)
    rc = mp->done (method);
  free (method->result);
  method->result = NULL;
  method->result_size = 0;
  return rc;
}

int
method_run (struct access_method *method, void *handle, const char *cmd)
{
  struct method_descr *mp = method_tab + method->type;

  if (debug_level > 1)
    {
      if (cmd)
	logmsg (LOG_DEBUG, "Running method: %s \"%s\"", mp->name, cmd);
      else
	logmsg (LOG_DEBUG, "Running method: %s", mp->name);
    }

  if (!method->init_passed)
    {
      logmsg (LOG_CRIT, "INTERNAL ERROR: Method %s \"%s\" not initialized",
	      mp->name, SP (method->query));
      return 1;
    }
  if (!mp->run)
    {
      logmsg (LOG_CRIT, "INTERNAL ERROR: No run function for method %s \"%s\"",
	      mp->name, SP (method->query));
      return 1;
    }
  if (mp->free)
    mp->free (method, handle);
  return mp->run (method, handle, cmd);
}

unsigned
method_num_rows (struct access_method *method)
{
  return method->nrow;
}

unsigned
method_num_cols (struct access_method *method)
{
  return method->ncol;
}

const char *
method_result (struct access_method *method, void *handle,
	       unsigned nrow, unsigned ncol)
{
  struct method_descr *mp = method_tab + method->type;

  if (nrow >= method->nrow || ncol >= method->ncol
      || mp->get (method, handle, nrow, ncol))
    return NULL;
  return method->result;
}

void
method_copy_result (struct access_method *method, const char *res, size_t size)
{
  if (method->result_size < size + 1)
    {
      method->result_size = size + 1;
      method->result = x2realloc (method->result, &method->result_size);
    }
  memcpy (method->result, res, size);
  method->result[size] = 0;
}

/* Quote non-printable characters in INPUT. Point *OUTPUT to the malloc'ed
   quoted string. Return its length. */
int
method_quote_string (struct access_method *method, void *handle,
		     const char *input,
		     char **poutput, size_t *psize)
{
  struct method_descr *mp = method_tab + method->type;
  size_t size;
  int quote;
  char *output;

  if (!input)
    {
      *poutput = xmalloc (1);
      (*poutput)[0] = 0;
      *psize = 1;
      return 0;
    }

  if (mp->quote)
    return mp->quote (method, handle, input, poutput, psize);

  size = argcv_quoted_length (input, &quote);
  output = xmalloc (size + 1);
  argcv_quote_copy (output, input);
  output[size] = 0;

  *poutput = output;
  if (psize)
    *psize = size;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.