source: tags/svn.1.5.4/ARBDB/adtables.cxx

Last change on this file was 8319, checked in by westram, 14 years ago
  • ignored PERL2ARB interface as referrer (to detect functions that are only used from perl)
    • moved several functions to static scope or removed them (partly reverted by [13155])
    • for some functions it's ok to be only used from perl (e.g. macro support functions). Added comments there!
  • there is still some dead code in there, e.g.
    • read-security is implemented, but unused (and unwanted)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : adtables.cxx                                      //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include <arbdbt.h>
12
13#include "gb_main.h"
14#include "gb_data.h"
15
16
17/* *************** tables, for ARB BIO storage *******************
18
19 *  hierarchical organization:
20
21
22 *  main:
23 *      table_data:
24 *          table:
25 *              name    (indexed by default)
26 *              entries
27 *                  entry
28 *                      name (which is an id)
29 *                      ....
30 *                  entry
31 *                  ...
32 *              fields
33 *                  field
34 *                      name
35 *                      type
36 *                      description
37 *                  field
38 *                  ....
39 *             }
40 *      }
41 */
42
43static GBDATA *GBT_find_table_entry(GBDATA *gb_table, const char *id) {
44    GBDATA *gb_entries = GB_entry(gb_table, "entries");
45    GBDATA *gb_entry_name = GB_find_string(gb_entries, "name", id, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
46    if (!gb_entry_name) return NULL;
47    return GB_get_father(gb_entry_name);
48}
49
50static GBDATA *gbt_table_link_follower(GBDATA *gb_main, GBDATA */*gb_link*/, const char *link) {
51    GBDATA *gb_table;
52    char save;
53    char *sep;
54    // GBUSE(gb_main);
55    // GBUSE(gb_link);
56    sep = const_cast<char*>(strchr(link, ':'));
57    if (!sep) {
58        GB_export_errorf("Link '%s' is missing second ':' tag", link);
59        return NULL;
60    }
61    save = *sep;
62    *sep = 0;
63    gb_table = GBT_open_table(gb_main, link, 1);
64    *sep = save;
65
66    if (!gb_table) {
67        GB_export_errorf("Table '%s' does not exist", link);
68        return NULL;
69    }
70    return GBT_find_table_entry(gb_table, sep+1);
71}
72
73GB_ERROR GBT_install_table_link_follower(GBDATA *gb_main) {
74    GB_install_link_follower(gb_main, "T", gbt_table_link_follower);
75    return 0;
76}
77
78static void g_bt_table_deleted(GBDATA *gb_table, int *clientdata, GB_CB_TYPE gbtype) {
79    GB_MAIN_TYPE *Main = gb_get_main_during_cb();
80    GBS_free_hash(Main->table_hash);
81    Main->table_hash = GBS_create_hash(256, GB_MIND_CASE);
82}
83
84GBDATA *GBT_open_table(GBDATA *gb_table_root, const char *table_name, bool read_only) {
85    // open a table. This routines is optimized to look for existing tables
86    GBDATA *gb_table;
87    GBDATA *gb_table_name;
88    GBDATA *gb_table_description;
89    GBDATA *gb_table_data;
90    GBDATA *gb_table_entries;
91    GBDATA *gb_table_fields;
92    GBDATA *gb_table_name_field;
93
94    GB_MAIN_TYPE *Main = GB_MAIN(gb_table_root);
95    gb_table           = (GBDATA *)GBS_read_hash(Main->table_hash, table_name);
96    if (gb_table) return gb_table;
97
98    gb_table_data = GB_search(gb_table_root, "table_data", GB_CREATE_CONTAINER);
99    ASSERT_NO_ERROR(GB_create_index(gb_table_data, "name", GB_IGNORE_CASE, 256));
100
101    gb_table_name = GB_find_string(gb_table_data, "name", table_name, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
102    if (gb_table_name) return GB_get_father(gb_table_name);
103    if (read_only) return NULL;
104
105    // now lets create the table
106    gb_table = GB_create_container(gb_table_data, "table");
107    GB_add_callback(gb_table, GB_CB_DELETE, g_bt_table_deleted, 0);
108
109    gb_table_name = GB_create(gb_table, "name", GB_STRING);
110    GB_write_string(gb_table_name, table_name);
111    GB_write_security_levels(gb_table_name, 0, 7, 7); // neither delete nor change the name
112
113    gb_table_description = GB_create(gb_table, "description", GB_STRING);
114    GB_write_string(gb_table_description, "No description");
115
116    gb_table_entries = GB_create_container(gb_table, "entries");
117    GB_write_security_levels(gb_table_entries, 0, 0, 7); // never delete this
118
119    gb_table_fields = GB_create_container(gb_table, "fields");
120    GB_write_security_levels(gb_table_fields, 0, 0, 7); // not intended to be deleted
121
122    gb_table_name_field = GBT_open_table_field(gb_table, "name", GB_STRING); // for the id
123    GB_write_security_levels(gb_table_name_field, 0, 0, 7); // never delete name field
124
125    return gb_table;
126}
127
128GBDATA *GBT_first_table(GBDATA *gb_main) {
129    GBDATA *gb_table_data;
130    GBDATA *gb_table;
131    gb_table_data = GB_search(gb_main, "table_data", GB_CREATE_CONTAINER);
132    ASSERT_NO_ERROR(GB_create_index(gb_table_data, "name", GB_IGNORE_CASE, 256));
133    gb_table = GB_entry(gb_table_data, "table");
134    return gb_table;
135}
136
137GBDATA *GBT_next_table(GBDATA *gb_table) {
138    gb_assert(GB_has_key(gb_table, "table"));
139    return GB_nextEntry(gb_table);
140}
141
142
143
144GBDATA *GBT_first_table_entry(GBDATA *gb_table) {
145    GBDATA *gb_entries = GB_entry(gb_table, "entries");
146    return GB_entry(gb_entries, "entry");
147}
148
149GBDATA *GBT_next_table_entry(GBDATA *gb_table_entry) {
150    gb_assert(GB_has_key(gb_table_entry, "entry"));
151    return GB_nextEntry(gb_table_entry);
152}
153
154GBDATA *GBT_first_table_field(GBDATA *gb_table) {
155    GBDATA *gb_fields = GB_entry(gb_table, "fields");
156    return GB_entry(gb_fields, "field");
157}
158
159GBDATA *GBT_next_table_field(GBDATA *gb_table_field) {
160    gb_assert(GB_has_key(gb_table_field, "field"));
161    return GB_nextEntry(gb_table_field);
162}
163
164GBDATA *GBT_find_table_field(GBDATA *gb_table, const char *id) {
165    GBDATA *gb_fields = GB_entry(gb_table, "fields");
166    GBDATA *gb_field_name = GB_find_string(gb_fields, "name", id, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
167    if (!gb_field_name) return NULL;
168    return GB_get_father(gb_field_name);
169}
170
171GBDATA *GBT_open_table_field(GBDATA *gb_table, const char *fieldname, GB_TYPES type_of_field) {
172    GBDATA *gb_table_field = GBT_find_table_field(gb_table, fieldname);
173    GBDATA *gb_table_field_name;
174    GBDATA *gb_table_field_type;
175    GBDATA *gb_table_field_description;
176    GBDATA *gb_fields;
177
178    if (gb_table_field) return gb_table_field;
179
180    gb_fields           = GB_entry(gb_table, "fields");
181    gb_table_field      = GB_create_container(gb_fields, "field");
182    gb_table_field_name = GB_create(gb_table_field, "name", GB_STRING);
183
184    GB_write_string(gb_table_field_name, fieldname);
185    GB_write_security_levels(gb_table_field_name, 0, 7, 7); // never change this
186
187    gb_table_field_type = GB_create(gb_table_field, "type", GB_INT);
188
189    GB_write_int(gb_table_field_type, type_of_field);
190    GB_write_security_levels(gb_table_field_type, 0, 7, 7);
191
192    gb_table_field_description = GB_create(gb_table_field, "description", GB_STRING);
193    GB_write_string(gb_table_field_description, "No description yet");
194
195    return gb_table_field;
196}
197
Note: See TracBrowser for help on using the repository browser.