aboutsummaryrefslogtreecommitdiff
path: root/src/method.c
blob: d0fa2cb5dd7866ac74c51ed93dbd59440d0d1b2e (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
/* 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"

struct method_descr
{
  const char *name;
  int (*init) (struct access_method *);
  int (*done) (struct access_method *);
  int (*free) (struct access_method *);
  int (*get)  (struct access_method *method, unsigned nrow, unsigned ncol);
  
  int (*run) (struct access_method *, const char *);
};

static struct method_descr method_tab[] = {
  { "none", NULL, NULL, NULL, NULL, NULL },
  { "sql", sql_init_method, sql_done_method, sql_free_result,
    sql_get_method, sql_run_method },
  { "builtin", NULL, NULL, NULL, NULL, NULL },
  { "external", NULL, NULL, NULL, NULL, NULL }
};

struct access_method *
method_new (enum access_method_type type)
{
  struct access_method *mp = xmalloc (sizeof mp[0]);
  memset (mp, 0, sizeof mp[0]);
  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)
    logmsg (LOG_DEBUG, "Initializing method: %s \"%s\" \"%s\"",
	    mp->name, method->param[0],
	    method->param[1] ? method->param[1] : "");
  if (mp->init)
    rc = mp->init (method);
  if (rc == 0)
    method->init_passed = 1;
  return rc;
}

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)
    logmsg (LOG_DEBUG, "Closing method: %s \"%s\" \"%s\"",
	    mp->name, method->param[0],
	    method->param[1] ? method->param[1] : "");
  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, const char *cmd)
{
  struct method_descr *mp = method_tab + method->type;
  
  if (debug_level > 1)
    logmsg (LOG_DEBUG, "Running method: %s \"%s\"", mp->name, cmd);
  if (!method->init_passed)
    {
      logmsg (LOG_CRIT, "INTERNAL ERROR: Method %s \"%s\" not initialized",
	      mp->name, method->param[0]);
      return 1;
    }
  if (!mp->run)
    {
      logmsg (LOG_CRIT, "INTERNAL ERROR: No run function for method %s \"%s\"",
	      mp->name, method->param[0]);
      return 1;
    }
  if (mp->free)
    mp->free (method);
  return mp->run (method, 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, unsigned nrow, unsigned ncol)
{
  struct method_descr *mp = method_tab + method->type;

  if (nrow >= method->nrow || ncol >= method->ncol
      || mp->get (method, 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;
}

Return to:

Send suggestions and report system problems to the System administrator.