73b0dc0b66b94fd1c21aac82b6dcf9d2f28ab990
[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,
22                  val(t->elements[0]), val(t->elements[1]) );
23     } else {
24         // fprintf( stderr, "%s< >\n", pre );
25     }
26 }
27
28 void prdropout(const VectorIndex index,const void *t) {
29     prtuple( ".. ", (Tuple*)t );
30 }
31
32 void add_Relation(Relation *r,char *p,char *c) {
33     Tuple *t = Tuple_create( 2, p, c);
34     prtuple( "Adding: ", t );
35     Vector *dropped = Relation_add( r, t );
36     if ( dropped ) {
37         fprintf( stderr, "Knockout %ld tuples\n", dropped->size );
38         Vector_dump( dropped, prdropout );
39         Vector_resize( dropped, 0, 0, 0 );
40         free( dropped );
41     }
42 }
43
44 typedef union {
45     Tuple tup;
46     struct {
47         void *type;
48         void *c1;
49         void *c2;
50     } cols;
51 } Tuple2;
52
53 int main(int argc,char **argv) {
54     Relation *rltn2 = Relation_create(
55         TupleSchema_create(
56             2, Tuple_create( 2, (void*) &stringitem, (void*) &stringitem ) ) );
57     Relation_add_constraint( rltn2, 0, 1 );
58     add_Relation( rltn2, "benton", "holly" );
59     add_Relation( rltn2, "benton", "molly");
60     add_Relation( rltn2, "gully", "holly");
61     add_Relation( rltn2, "gully", "vanitha");
62     VectorIndex index = 0;
63     Tuple2 q1 = (Tuple2) { .cols = { 0, "benton", 0 }};
64     Tuple *t;
65     prtuple( "Simple query: ", &q1.tup );
66     for ( ; index < rltn2->content.table.size; index++ ) {
67         t = Relation_next( rltn2, &index, &q1.tup );
68         prtuple( ".. ", t );
69     }
70     // Test null query
71     fprintf( stderr, "Null query: (null)\n" );
72     for ( index = 0 ; index < rltn2->content.table.size; index++ ) {
73         t = Relation_next( rltn2, &index, 0 );
74         prtuple( ".. ", t );
75     }
76     // Test Generic query
77     q1 = (Tuple2) { .cols = { 0, 0, 0 }};
78     prtuple( "Generic query: ", &q1.tup );
79     for ( index = 0 ; index < rltn2->content.table.size; index++ ) {
80         t = Relation_next( rltn2, &index, &q1.tup );
81         prtuple( ".. ", t );
82     }
83     // Test deletion
84     q1 = (Tuple2) { .cols = { 0, "gully", 0 }};
85     prtuple( "Deletion query: ", &q1.tup );
86     Vector *deleted = Relation_delete( rltn2, &q1.tup );
87     for ( index = 0 ; index < rltn2->content.table.size; index++ ) {
88         t = Relation_next( rltn2, &index, 0 );
89         prtuple( ".. ", t );
90     }
91     for ( index = 0 ; index < deleted->size; index++ ) {
92         Tuple **p = (Tuple**) Vector_next_used( deleted, &index );
93         prtuple( "** ", p? *p : 0 );
94     }
95     return 0;
96 }