-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.c
334 lines (280 loc) · 8.98 KB
/
oop.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* $Id: oop.c 27410 2014-09-01 07:39:28Z piotras $
Copyright (C) 1999 Jukka Zitting <jukka.zitting@iki.fi>
Copyright (C) 2000 The Midgard Project ry
Copyright (C) 2000 Emile Heyns, Aurora SA <emile@iris-advies.com>
Copyright (C) 2003 David Schmitter, Dataflow Solutions GmbH <schmitt@dataflow.ch>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "mgd_internal.h"
#include "mgd_oop.h"
#include "php_midgard.h"
#include "php_midgard_gobject.h"
typedef char *charp;
void mgd_object_init(zval *obj, ...)
{
va_list args;
char *propname;
TSRMLS_FETCH();
va_start(args, obj);
while ((propname = va_arg(args, charp)) != NULL) {
add_property_unset(obj, propname);
}
va_end(args);
}
void php_midgard_bless(zval *object, MidgardClass *species)
{
TSRMLS_FETCH();
if (species->entry_ptr == NULL) {
fprintf(stderr, "SEVERE: No PHP class entry pointer for Midgard class '%s'\n", species->name);
return;
}
object_init_ex(object, species->entry_ptr);
php_midgard_ctor(object, species);
}
void php_midgard_ctor(zval *object, MidgardClass *species)
{
MidgardProperty *midgard_props;
TSRMLS_FETCH();
/* add default properties */
add_property_string(object, "__table__", (char*)species->table, 1);
add_property_long(object, "id", 0);
add_property_long(object, "sitegroup", 0);
add_property_string(object, "guid", "", 1);
/* add other properties */
for (midgard_props = species->properties; midgard_props &&
midgard_props->name; midgard_props++) {
switch(midgard_props->type) {
case IS_LONG:
add_property_long(object, midgard_props->name, 0);
break;
case IS_STRING:
add_property_string(object, midgard_props->name, "", 1);
break;
default: /* should not happen, but just in case */
add_property_unset(object, midgard_props->name);
break;
}
}
}
void php_midgard_bless_oop(zval *object, MidgardClass *species)
{
TSRMLS_FETCH();
if (species->entry_ptr == NULL) {
fprintf(stderr, "SEVERE: No PHP class entry pointer for Midgard class '%s'\n", species->name);
return;
}
object_init_ex(object, species->entry_ptr);
add_property_string(object, "__table__", (char*)species->table, 1);
add_property_string(object, "guid", "", 1);
add_property_long(object, "id", 0);
add_property_long(object, "sitegroup", 0);
}
void php_midgard_delete(zval * return_value, const char *table, int id)
{
TSRMLS_FETCH();
CHECK_MGD;
if (mgd_delete(mgd_handle(), table, id)) {
RETURN_TRUE;
}
RETVAL_FALSE_BECAUSE(MGD_ERR_INTERNAL);
}
void php_midgard_delete_repligard(const char *table, int id)
{
if (!mgd_rcfg())
return;
(void) mgd_delete_repligard(mgd_handle(), table, id);
}
void php_midgard_update(zval * return_value, const char *table,
const char *fields, int id, ...)
{
int ret;
va_list args;
TSRMLS_FETCH();
CHECK_MGD;
va_start(args, id);
if ((ret = mgd_vupdate(mgd_handle(), table, id, fields, args)) > 0) {
RETVAL_TRUE;
}
else {
#if HAVE_MIDGARD_QUOTA
if (ret == -64) RETVAL_FALSE_BECAUSE(MGD_ERR_QUOTA)
else RETVAL_FALSE_BECAUSE(MGD_ERR_INTERNAL);
#else
RETVAL_FALSE_BECAUSE(MGD_ERR_INTERNAL);
#endif
}
va_end(args);
}
void php_midgard_create(zval * return_value, zval * self, const char *table,
const char *fields, const char *values, ...)
{
va_list args;
int id;
TSRMLS_FETCH();
CHECK_MGD;
va_start(args, values);
id = mgd_vcreate(mgd_handle(), table, fields, values, args);
va_end(args);
if (!id) {
RETURN_FALSE_BECAUSE(MGD_ERR_INTERNAL);
/* EEH: TODO: log_error(
* * "Midgard: create of %s failed",
* table ? table : "<null class>"); */
}
#if HAVE_MIDGARD_QUOTA
else if (id == -64) {
RETURN_FALSE_BECAUSE(MGD_ERR_QUOTA);
}
#endif
if (self) { add_property_long(self, "id", id); }
RETVAL_LONG(id);
}
void php_midgard_select(MidgardClass *species, zval * return_value,
const char *fields, const char *tables,
const char *where, const char *order, ...)
{
va_list args;
midgard_res *res;
int res_id;
TSRMLS_FETCH();
va_start(args, order);
res =
mgd_sitegroup_vselect(mgd_handle(), fields, tables, where, order,
args);
va_end(args);
if (res) {
/*
* ZEND_REGISTER_RESOURCE(return_value,res,le_midgard_list_fetch);
* PP: I do not know if return_value may be used as resource property.
* Anyway It is good to register resource before inserting it to Zend resources list */
res_id = zend_list_insert(res, le_midgard_list_fetch);
php_midgard_bless_oop(return_value, species);
add_property_long(return_value, "N", mgd_rows(res));
add_property_resource(return_value, "__res__", res_id);
}
}
void php_midgard_sitegroup_get(MidgardClass *species, zval * return_value,
int grouped, const char *fields, const char *table, int id);
void php_midgard_get(MidgardClass *species, zval * return_value,
const char *fields, const char *table, int id)
{
php_midgard_sitegroup_get(species, return_value, 1, fields, table, id);
}
#if HAVE_MIDGARD_MULTILANG
void php_midgard_sitegroup_get_lang(MidgardClass *species, zval * return_value,
int grouped, const char *fields, const char *table, int id, int lang);
void php_midgard_get_lang(MidgardClass *species, zval * return_value,
const char *fields, const char *table, int id, int lang)
{
php_midgard_sitegroup_get_lang(species, return_value, 1, fields, table, id, lang);
}
#endif /* HAVE_MIDGARD_MULTILANG */
void php_midgard_sitegroup_get(MidgardClass *species, zval * return_value,
int grouped, const char *fields, const char *table, int id)
{
#if ! HAVE_MIDGARD_MULTILANG
midgard_res *res;
#else /* HAVE_MIDGARD_MULTILANG */
php_midgard_sitegroup_get_lang(species, return_value, grouped, fields, table, id, 0);
}
void php_midgard_sitegroup_get_lang(MidgardClass *species, zval * return_value,
int grouped, const char *fields, const char *table, int id, int lang) {
#endif /* HAVE_MIDGARD_MULTILANG */
int i = 0;
#if HAVE_MIDGARD_MULTILANG
midgard_res *res = NULL;
#endif /* HAVE_MIDGARD_MULTILANG */
midgard_res *params = NULL;
midgard_pool *pool = NULL;
char *propname, *value=NULL;
TSRMLS_FETCH();
if (grouped) {
if (lang) {
res = mgd_sitegroup_record_lang(mgd_handle(), fields, table, id);
} else {
res = mgd_sitegroup_record(mgd_handle(), fields, table, id);
}
} else {
res = mgd_ungrouped_record(mgd_handle(), fields, table, id);
}
if (!res || !mgd_fetch(res)) {
if(res) mgd_release(res);
RETVAL_FALSE_BECAUSE(MGD_ERR_NOT_EXISTS);
} else {
php_midgard_bless_oop(return_value, species);
//mgd_fetch(res);
for (i = 0; i < mgd_cols(res); i++) {
value = (char *)mgd_colvalue(res, i);
if (value == NULL ) {
value = "";
}
char *value_end;
long long_value = strtol(value, &value_end, 10);
if (value_end != value && *value_end == '\0' &&
(!g_str_equal((char*)mgd_colname(res, i), "name"))) {
add_property_long(return_value,
(char*)mgd_colname(res, i), long_value);
} else {
add_property_string(return_value,
(char*)mgd_colname(res, i), (char *)value, 1);
}
}
if (res) mgd_release(res);
int paramlang = mgd_lang(mgd_handle());
gchar *order = NULL;
if(paramlang > 0) order = "lang ASC";
params =
mgd_ungrouped_select(mgd_handle(), "domain,name,value",
"record_extension",
"tablename=$q AND oid=$d and lang in ($d, $d)", order, table,
id,
mgd_get_default_lang(mgd_handle()),
paramlang);
if (params) {
pool = mgd_alloc_pool();
while (mgd_fetch(params)) {
propname =
mgd_format(mgd_handle(), pool, "$s_$s",
mgd_colvalue(params, 0),
mgd_colvalue(params, 1));
add_property_string(return_value, propname,
(char*)mgd_colvalue (params, 2), 1);
}
mgd_release(params);
}
}
if (pool) mgd_free_pool(pool);
}
void php_midgard_get_object(zval *return_value, int table, int id)
{
#define MGD_GET_OBJECT_SITEGROUP_FIELD ",guid,sitegroup"
switch (table) {
case MIDGARD_OBJECT_BLOBS:
php_midgard_get(&MidgardAttachment, return_value,
"id,name,title,mimetype,score,author,created,ptable,pid,location"
MGD_GET_OBJECT_SITEGROUP_FIELD,
"blobs", id);
break;
case MIDGARD_OBJECT_SITEGROUP:
php_midgard_sitegroup_get(&midgardsitegroup, return_value, 0, "*",
"sitegroup", id);
break;
default:
RETURN_FALSE_BECAUSE(MGD_ERR_INTERNAL);
}
}
MidgardClassPtr MidgardClasses [] = {
&MidgardAttachment,
&midgardsitegroup,
NULL
};