further camelcase fixing
[rrq/rrqmisc.git] / tests / example-relation.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <Relation.h>
4 #include <stringitem.h>
5
6 char *val(void* t) {
7     static char buf[2][100];
8     static int i = 0;
9     if ( ++i >= 2 ) {
10         i = 0;
11     }
12     if ( t ) {
13         sprintf( buf[i], "\"%s\"", (char*)t );
14         return buf[i];
15     }
16     return "(null)";
17 }
18
19 void prtuple(char *pre, tuple *t) {
20     if ( t ) {
21         fprintf( stderr, "%s<%s, %s>\n", pre, val((*t)[0]), val((*t)[1]) );
22     } else {
23         // fprintf( stderr, "%s< >\n", pre );
24     }
25 }
26
27 void prdropout(const VectorIndex index,const void *t) {
28     prtuple( ".. ", (tuple*)t );
29 }
30
31 void add_Relation(Relation *r,char *p,char *c) {
32     tuple *t = tuple_create( 2, p, c);
33     prtuple( "Adding: ", t );
34     Vector *dropped = Relation_add( r, t );
35     if ( dropped ) {
36         fprintf( stderr, "Knockout %ld tuples\n", dropped->size );
37         Vector_dump( dropped, prdropout );
38         Vector_resize( dropped, 0, 0, 0 );
39         free( dropped );
40     }
41 }
42
43 typedef union {
44     struct {
45         void *c1;
46         void *c2;
47     } cols;
48     void *tup[2];
49 } tuple2;
50
51 int main(int argc,char **argv) {
52     Relation *rltn2 = Relation_create(
53         TupleSchema_create(
54             2, tuple_create( 2, (void*) &stringitem, (void*) &stringitem ) ) );
55     Relation_add_constraint( rltn2, 0, 1 );
56     add_Relation( rltn2, "benton", "holly" );
57     add_Relation( rltn2, "benton", "molly");
58     add_Relation( rltn2, "gully", "holly");
59     add_Relation( rltn2, "gully", "vanitha");
60     VectorIndex index = 0;
61     tuple2 q1 = {{ "benton", 0 }};
62     tuple *t;
63     prtuple( "Simple query: ", &q1.tup );
64     for ( ; index < rltn2->content.table.size; index++ ) {
65         t = Relation_next( rltn2, &index, &q1.tup );
66         prtuple( ".. ", t );
67     }
68     // Test null query
69     fprintf( stderr, "Null query: (null)\n" );
70     for ( index = 0 ; index < rltn2->content.table.size; index++ ) {
71         t = Relation_next( rltn2, &index, 0 );
72         prtuple( ".. ", t );
73     }
74     // Test Generic query
75     q1 = (tuple2) {{ 0, 0 }};
76     prtuple( "Generic query: ", &q1.tup );
77     for ( index = 0 ; index < rltn2->content.table.size; index++ ) {
78         t = Relation_next( rltn2, &index, &q1.tup );
79         prtuple( ".. ", t );
80     }
81     // Test deletion
82     q1 = (tuple2) {{ "gully", 0 }};
83     prtuple( "Deletion query: ", &q1.tup );
84     Vector *deleted = Relation_delete( rltn2, &q1.tup );
85     for ( index = 0 ; index < rltn2->content.table.size; index++ ) {
86         t = Relation_next( rltn2, &index, 0 );
87         prtuple( ".. ", t );
88     }
89     for ( index = 0 ; index < deleted->size; index++ ) {
90         tuple **p = (tuple**) Vector_next_used( deleted, &index );
91         prtuple( "** ", p? *p : 0 );
92     }
93     return 0;
94 }