This repository was archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_replicator_template.pl
More file actions
141 lines (137 loc) · 4.83 KB
/
read_replicator_template.pl
File metadata and controls
141 lines (137 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/local/bin/perl
use strict;
use warnings;
use List::MoreUtils qw(first_index any);
use Data::Dumper;
my $template_file = shift @ARGV;
open my $in_fh, '<', $template_file or die "Cannot read input file: $!";
my @lines = <$in_fh>;
close $in_fh or die "Cannot close input file: $!";
chomp(@lines);
@lines = grep {$_ !~ /^$/} @lines;
my @tables;
while (scalar (@lines) > 0) {
die "Input file not in correct format." if ($lines[0] !~ /^CREATE\s+(\w+)\s+(\S+)\s*$/i);
my $table_name = $1;
my $class_name = $2;
my $end = first_index {$_ =~ /^ENDTABLE$/i} @lines;
my @a = splice(@lines, 0,$end);
$a[0] = $table_name.' '.$class_name;
shift @lines;
push @tables, \@a;
}
for (my $arr = 0; $arr < scalar(@tables); $arr++) {
my @a = @{$tables[$arr]};
my %table;
my @names = split(/\s+/, $a[0]);
shift @a;
$table{name} = $names[0];
$table{class_name} = $names[1];
$table{fields} = [];
$table{uniques} = [];
$table{primary_key} = [];
$table{constraints} = [];
$table{relationships} = [];
if (any {$_ =~ /^ID$/i} @a) {
my $i = first_index {$_ =~ /^ID$/i} @a;
splice(@a, $i, 1);
my $id = {
name => 'id', type => 'SERIAL', not_null => 1, sequence => $table{name}.'_id_seq', primary_key => 1,
};
push @{$table{fields}}, $id;
push @{$table{primary_key}}, 'id';
}
if (any {$_ =~ /^FF$/i} @a) {
my $i = first_index {$_ =~ /^FF$/i} @a;
splice(@a, $i, 1);
push @{$table{fields}}, {name => 'date_created', type => 'TIMESTAMP', not_null => 1, default => 'now'};
push @{$table{fields}}, {name => 'created_by', type => 'VARCHAR', not_null => 1, default => '', };
push @{$table{fields}}, {name => 'last_modified', type => 'TIMESTAMP', not_null => 1 };
push @{$table{fields}}, {name => 'modified_by', type => 'VARCHAR', not_null => 1, default => '', };
push @{$table{triggers}}, {name => .$table{name}'_last_modified', table => $table{name}};
}
my @r_lines = grep(/^RELATIONSHIP /, @a);
for my $r_line (@r_lines) {
my @rel = split(/\s+/, $r_line);
shift @rel;
if ($rel[1] eq '11') { $rel[1] = 'one to one';
}elsif ($rel[1] eq '1*') { $rel[1] = 'one to many';
}elsif ($rel[1] eq '**') { $rel[1] = 'many to many';
}else { die "Improper formatting in RELATIONSHIP line." }
my $r_obj = {
name => $rel[0],
type => $rel[1],
class => $rel[2],
column => $rel[3],
foreign_column => $rel[4],
};
push @{$table{relationships}}, $r_obj;
}
@a = grep(!/^RELATIONSHIP /, @a);
if (any {$_ =~ /^TEST$/i} @a) {
my $i = first_index {$_ =~ /^TEST$/i} @a;
splice(@a, $i, 1);
$table{create_test_file} = 1;
}
if (any {$_ =~ /^MODEL$/i} @a) {
my $i = first_index {$_ =~ /^MODEL$/i} @a;
splice(@a, $i, 1);
$table{create_model_file} = 1;
}
if (any {$_ =~ /^MANAGE$/i} @a) {
my $i = first_index {$_ =~ /^MANAGE$/i} @a;
splice(@a, $i, 1);
$table{create_manage_file} = 1;
}
my @single_uniques;
for my $line (@a) {
my @b = split(/\s+/, $line);
my $field_name = shift @b;
my $field_type = uc shift @b;
my $not_null = 0;
my $default = undef;;
while (scalar (@b) > 0) {
my $reading = shift @b;
if ($reading =~ /^NN$/i) {
$not_null = 1; next;
}
elsif ($reading =~ /^D=(.+)$/i) {
$default = $1;
}
elsif ($reading =~ /^P$/i) {
push @{$table{primary_key}}, $field_name;
}
elsif ($reading =~ /^FK/i) {
my @fk = split(/-/, $reading);
shift @fk;
my $z = {
field => $field_name,
sql_table => $fk[0],
sql_column => $fk[1],
model_class => $fk[2],
model_name => $fk[3],
};
push @{$table{constraints}}, $z;
}
elsif ($reading =~ /^U\d*$/i) {
$reading =~ /^U(\d*)$/i;
if ($1 !~ //) {
my $index = $1;
$index--;
push @{$table{uniques}->[$index]}, $field_name;
} else {
push @single_uniques, [$field_name];
}
}
else { die "Cannot recognize $reading\n"; }
}
push @{$table{uniques}}, @single_uniques;
my %s = ('name', $field_name, 'type', $field_type);
$s{not_null} = 1 if ($not_null);
$s{default} = $default if (defined($default));
push @{$table{fields}}, \%s;
}
$" = "\n";
$tables[$arr] = \%table;
}
print Dumper(@tables);