]> andersk Git - moira.git/blob - db/doschema.awk
Command line printer manipulation client, and build goo.
[moira.git] / db / doschema.awk
1 # $Id$
2 #
3 # Build moira_schema.c and .h from the schema.sql file
4 # It knows which one to build cased on whether or not its input is
5 # in all caps.
6
7 BEGIN {
8         print "/* This file automatically generated */";
9         print "/* Do not edit */\n";
10
11         ntables = 0;
12         maxsize = 0;
13 }
14
15 $1 == "#" { next; }
16
17 /^create/ {
18   cfile = 1;
19   tablename[ntables] = $3;
20   count = 0;
21   next;
22 }
23
24 /^CREATE/ {
25   hfile = 1;
26   tablename[ntables] = $3;
27   count = 0;
28   next;
29 }
30
31 NF >= 2 {
32   if ($2 ~ /CHAR\([0-9]*\)/) {
33     t = split($2, temp, "(");
34     if (t != 2) printf "Can't parse %s\n", $2;
35     t = split(temp[2], temp2, ")");
36     if (t != 2) printf "Can't parse %s\n", temp[2];
37     width[count] = temp2[1];
38   } else if ($2 ~ /DATE/)
39     width[count] = 21;
40   else if ($2 ~ /INT/)
41     width[count] = 10;
42   else
43     width[count] = 0;
44
45   if (hfile && width[count]) {
46     printf "#define %s_%s_SIZE %d\n", tablename[ntables], $1, width[count] + 1;
47     if (width[count] > maxsize)
48       maxsize = width[count];
49   }
50
51   count++;
52 }
53
54 /^\);$/ {
55   if (cfile) {
56     printf "int %s_widths[] = { ", tablename[ntables];
57     for (i = 0; i < count; i++) {
58       printf "%d", width[i];
59       if (i < count-1)
60         printf ", ";
61     }
62     printf "};\n";
63   }
64   ntables++;
65 }
66
67 END {
68   if (hfile) {
69     printf "\n#define MAX_FIELD_WIDTH %d\n", maxsize + 1;
70     printf "\nenum tables { NO_TABLE,";
71     len = 22;
72   } else {
73     printf "\nint num_tables = %d;\n", ntables;
74     printf "\nchar *table_name[] = { \"none\",";
75     len = 29;
76   }
77
78   for (i = 0; i < ntables; i++) {
79     if (hfile)
80       str = sprintf(" %s_TABLE", tablename[i]);
81     else
82       str = sprintf(" \"%s\"", tablename[i]);
83     if (len + length(str) > 75) {
84       printf "\n ";
85       len = 1;
86     }
87     printf str;
88     if (i != ntables - 1)
89       printf ",";
90     len += length(str);
91   }
92   print "\n};\n";
93 }
This page took 0.050505 seconds and 5 git commands to generate.