/[papo]/papo/zot/zot2sql.pl
ViewVC logotype

Diff of /papo/zot/zot2sql.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by apronotti, Mon Apr 7 19:02:23 2003 UTC revision 1.10 by jlenton, Fri May 16 16:52:21 2003 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use Data::Dumper; $Data::Dumper::Deepcopy = 1;  use Data::Dumper; $Data::Dumper::Deepcopy = 1;
5  use strict;  use strict;
6  use Carp;  use Carp;
 use DBI;  
7  use zot;  use zot;
8    
9  our %config = ();  our %config = ();
# Line 40  $config{'^PostgreSQL 7\.2\.'} = Line 39  $config{'^PostgreSQL 7\.2\.'} =
39          return( ( sprintf("dbi:Pg:dbname=%s;host=%s", $db_name, $db_host),          return( ( sprintf("dbi:Pg:dbname=%s;host=%s", $db_name, $db_host),
40                    $user, $password, { 'RaiseError' => 1 } ) );                    $user, $password, { 'RaiseError' => 1 } ) );
41      },      },
42      'delay' => sub { system('sync'); select(undef, undef, undef, 0.5) },  #    'delay' => sub { system('sync'); select(undef, undef, undef, 0.005) },
     'delay' => sub {  },  
43      'datatypes' => {},      'datatypes' => {},
44      'relational' => sub {      'relational' => sub {
45          my $table = shift;          my $table = shift;
# Line 62  $config{'^PostgreSQL 7\.1\.'} = Line 60  $config{'^PostgreSQL 7\.1\.'} =
60      {      {
61          'bigserial' => 'serial',          'bigserial' => 'serial',
62      },      },
     'delay' => $config{'^PostgreSQL 7\.2\.'}{'delay'},  
63      'relational' => $config{'^PostgreSQL 7\.2\.'}{'relational'},      'relational' => $config{'^PostgreSQL 7\.2\.'}{'relational'},
64  };  };
65    
66  $config{'stdout'} =  $config{'raw'} =
67  {  {
68      'driver' => sub { return "dbi:stdout:" },      'driver' => sub { return "dbi:stdout:" },
69      'datatypes' => {},      'datatypes' => {},
70      'delay' => sub { },      'delay' => sub {},
71      'relational' => $config{'^PostgreSQL 7\.1\.'}{'relational'},      'relational' => $config{'^PostgreSQL 7\.1\.'}{'relational'},
72  };  };
73    
# Line 93  if (not exists $config{$db_conf}) Line 90  if (not exists $config{$db_conf})
90      exit 1;      exit 1;
91  }  }
92    
 # try to connect  
 our $db = DBI->connect( $config{$db_conf}{'driver'}->(@ARGV) );  
   
93  # check the file exists  # check the file exists
94  open F, $file or die "Unable to open $file: $!";  open F, $file or die "Unable to open $file: $!";
95  our %structure = zot::parse(<F>);  our %structure = zot::parse(<F>);
# Line 150  foreach my $table (sort keys %structure) Line 144  foreach my $table (sort keys %structure)
144          }          }
145          else          else
146          {          {
147                $r = "";
148              if ($r =~ /^_start_t\s/)              if ($r =~ /^_start_t\s/)
149              {              {
150                    $r = 'NOT NULL';
151                }
152    
153                if (substr($field, -1) eq '*')
154                {
155                    substr($field, -1) = '';
156                    $r .= ' UNIQUE';
157                }
158    
159                if (substr($field, -1) eq '+')
160                {
161                    substr($field, -1) = '';
162                  $r .= ' NOT NULL';                  $r .= ' NOT NULL';
163              }              }
164              push @r, $r;  
165                push @r, sprintf("%s %s %s", $field, $type, $r);
166          }          }
167      }      }
168    
# Line 178  foreach my $table (sort keys %structure) Line 186  foreach my $table (sort keys %structure)
186      my $sql = "CREATE TABLE $table (\n" . join(",\n", map("    $_", @r)) . "\n);\n";      my $sql = "CREATE TABLE $table (\n" . join(",\n", map("    $_", @r)) . "\n);\n";
187    
188      $v and print STDERR $sql;      $v and print STDERR $sql;
189      $db->do( $sql );      print $sql;
     $config{$db_conf}{'delay'}->();  
190  }  }
191    
192  if ($config{$db_conf}{'relational'})  if ($config{$db_conf}{'relational'})
# Line 199  if ($config{$db_conf}{'relational'}) Line 206  if ($config{$db_conf}{'relational'})
206                  $sql = $config{$db_conf}{'relational'}->($table, $rel, $structure{$table}{REL}{$rel});                  $sql = $config{$db_conf}{'relational'}->($table, $rel, $structure{$table}{REL}{$rel});
207              }              }
208              $v and print STDERR $sql;              $v and print STDERR $sql;
209              $db->do($sql);              print $sql;
             $config{$db_conf}{'delay'}->();  
210          }          }
211      }      }
212  }  }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26