Line # Revision Author
1 3 ahitrov@rambler.ru #!/usr/bin/perl -w
2
3 use strict;
4 use warnings "all";
5
6 use FindBin;
7 BEGIN { require "$FindBin::RealBin/../lib/Modules.pm" }
8
9 use Contenido::Globals;
10 use Contenido::Init;
11
12 my $config_file = shift or die "Usage: ".$FindBin::RealScript." /path/to/project.info\n";
13
14 die "File '$config_file' doesnt exists or not readable" unless -e $config_file and -r $config_file;
15
16 Contenido::Init->init();
17
18 $keeper = Contenido::Keeper->new($state);
19
20 my $rows = $keeper->{SQL}->selectrow_array("SELECT count(*) FROM options");
21
22 unless ($rows) {
23 $keeper->{SQL}->do(qq(CREATE TABLE options (id INTEGER NOT NULL PRIMARY KEY DEFAULT NEXTVAL('public.documents_id_seq'::text), pid INTEGER, name TEXT, VALUE text, "type" TEXT NOT NULL)));
24
25 $keeper->t_connect() || do { $keeper->error(); return undef; };
26 $keeper->{TSQL}->do("DELETE FROM options");
27
28 my $insert = $keeper->{TSQL}->prepare("INSERT INTO options (pid, name, value, type) VALUES (?, ?, ?, ?)");
29
30 my $config = do $config_file;
31 die "$config_file has invalid format" unless ref $config eq "HASH";
32
33 store(undef, undef, $config, $insert, $keeper);
34
35 $keeper->t_finish();
36 } else {
37 die "Table 'options' already exists and not empty";
38 }
39
40 sub store {
41 my $pid = shift;
42 my $key = shift;
43 my $data = shift;
44 my $insert = shift;
45 my $keeper = shift;
46
47 my $new_pid;
48
49 if (ref $data eq "HASH") {
50 if (defined $key) {
51 $insert->execute($pid, $key, undef, "HASH");
52 $new_pid = $keeper->{TSQL}->selectrow_array("SELECT currval('documents_id_seq')");
53 }
54
55 foreach my $new_key (keys %$data) {
56 store($new_pid, $new_key, $data->{$new_key}, $insert, $keeper);
57 }
58 } elsif (ref $data eq "ARRAY") {
59 if (defined $key) {
60 $insert->execute($pid, $key, undef, "ARRAY");
61 $new_pid = $keeper->{TSQL}->selectrow_array("SELECT currval('documents_id_seq')");
62 }
63
64 foreach my $new_key (0 .. $#$data) {
65 store($new_pid, $new_key, $data->[$new_key], $insert, $keeper);
66 }
67 } else {
68 $insert->execute($pid, $key, $data, "SCALAR") || return $keeper->t_abort();
69 }
70 }