Flesh out Blerg::Database OO layer and convenience functions
[blerg.git] / lib / perl / Blerg-Database / Database.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "ppport.h"
6
7 #include "config.h"
8 #include "database/database.h"
9 #include "database/blergref.h"
10 #include "database/tags.h"
11 #include "database/subscription.h"
12 #include "database/util.h"
13
14 #include "const-c.inc"
15
16 HV * blergref_to_perl_hash(struct blergref *list) {
17         HV *tmp;
18         char buf[21];
19         int n;
20
21         tmp = newHV();
22         hv_store(tmp, "author", 6, newSVpv(list->author, 0), 0);
23         n = snprintf(buf, 21, "%llu", list->record);
24         hv_store(tmp, "record", 6, newSVpv(buf, n), 0);
25
26         return tmp;
27 }
28
29 MODULE = Blerg::Database                PACKAGE = Blerg::Database               
30
31 INCLUDE: const-xs.inc
32 PROTOTYPES: ENABLE
33
34 int exists(const char *name)
35     CODE:
36         RETVAL = blerg_exists(name);
37     OUTPUT:
38         RETVAL
39
40 struct blerg * _open(const char *name)
41     CODE:
42         RETVAL = blerg_open(name);
43     OUTPUT:
44         RETVAL
45
46 int _close(struct blerg *ptr)
47     CODE:
48         RETVAL = blerg_close(ptr);
49     OUTPUT:
50         RETVAL
51
52 int _store(struct blerg *ptr, const char *data, int length(data))
53     PROTOTYPE: $$
54     CODE:
55         RETVAL = blerg_store(ptr, data, XSauto_length_of_data);
56     OUTPUT:
57         RETVAL
58
59 const char * _fetch(struct blerg *ptr, int record)
60     INIT:
61         char *buf;
62         int buflen;
63         int n;
64     PPCODE:
65         n = blerg_fetch(ptr, record, &buf, &buflen);
66         if (!n) {
67             XSRETURN_UNDEF;
68         }
69         XPUSHs(sv_2mortal(newSVpvn(buf, buflen)));
70         free(buf);
71
72 const char * _get_record_count(struct blerg *ptr)
73     INIT:
74         uint64_t count;
75         char buf[21];
76         int n;
77     PPCODE:
78         count = blerg_get_record_count(ptr);
79         n = snprintf(buf, 21, "%llu", count);
80         XPUSHs(sv_2mortal(newSVpv(buf, n)));
81
82 time_t _get_timestamp(struct blerg *ptr, int record)
83     CODE:
84         RETVAL = blerg_get_timestamp(ptr, record);
85     OUTPUT:
86         RETVAL
87
88 int _set_subscription_mark(struct blerg *ptr)
89     CODE:
90         RETVAL = blerg_set_subscription_mark(ptr);
91     OUTPUT:
92         RETVAL
93
94 const char * _get_subscription_mark(struct blerg *ptr)
95     INIT:
96         uint64_t mark;
97         char buf[21];
98         int n;
99     PPCODE:
100         mark = blerg_get_subscription_mark(ptr);
101         n = snprintf(buf, 21, "%llu", mark);
102         XPUSHs(sv_2mortal(newSVpv(buf, n)));
103
104 int _set_mute(struct blerg *ptr, int v)
105     CODE:
106         RETVAL = blerg_set_mute(ptr, v);
107     OUTPUT:
108         RETVAL
109
110 int _get_mute(struct blerg *ptr)
111     CODE:
112         RETVAL = blerg_get_mute(ptr);
113     OUTPUT:
114         RETVAL
115
116 void tag_list(const char *tag, const char *str_offset, int direction)
117     INIT:
118         HV * tmp;
119         struct blergref *list;
120         uint64_t offset;
121         int count, i;
122     PPCODE:
123         offset = strtoull(str_offset, NULL, 0);
124         list = tag_list(tag, offset, &count, direction);
125         if (list == NULL) {
126             XSRETURN_UNDEF;
127         }
128
129         i = count - 1;
130         while (i >= 0) {
131             tmp = blergref_to_perl_hash(&list[i]);
132             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
133             i--;
134         }
135         free(list);
136
137 int subscription_add(const char *from, const char *to)
138     CODE:
139         RETVAL = subscription_add(from, to);
140     OUTPUT:
141         RETVAL
142
143 int subscription_remove(const char *from, const char *to)
144     CODE:
145         RETVAL = subscription_remove(from, to);
146     OUTPUT:
147         RETVAL
148
149 void subscription_list(const char *author, const char *str_offset, int direction)
150     INIT:
151         HV *tmp;
152         struct blergref *list;
153         uint64_t offset;
154         int count, i;
155     PPCODE:
156         offset = strtoull(str_offset, NULL, 0);
157         list = subscription_list(author, offset, &count, direction);
158         if (list == NULL) {
159             XSRETURN_UNDEF;
160         }
161
162         i = count - 1;
163         while (i >= 0) {
164             tmp = blergref_to_perl_hash(&list[i]);
165             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
166             i--;
167         }
168         free(list);
169
170 int valid_tag_name(const char *name)
171
172 int valid_name(const char *name)