]> mj.ucw.cz Git - pciids.git/blob - scripts/initdb.pl
Remove useless directories
[pciids.git] / scripts / initdb.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 BEGIN {
6         unshift @INC, ".";
7 }
8 use PciIds::Config;
9 use PciIds::Db;
10 use DBI;
11
12 my @lines;
13 my $tablename;
14
15 defConf( { "dbcharset" => "UTF8", "dbtype" => "InnoDB" } );
16
17 my %replaces = (
18         "CHARSET" => "CHARSET ".$config{"dbcharset"}
19 );
20
21 sub createTable( $ ) {
22         die "Invalid table definition\n" unless( defined( $tablename ) && @lines );
23         my $nt = $_[ 0 ]->prepare( "CREATE TABLE ".$tablename." (".( join "\n", @lines ).") TYPE = $config{dbtype};" );
24         $nt->execute();
25         @lines = ();
26         print "Created table $tablename\n";
27         undef $tablename;
28 }
29
30 my $dbh = connectDb();
31 open TABLES, "tables" or die "Could not open table definitions\n";
32 foreach( <TABLES> ) {
33         chomp;
34         if( /^\s*$/ ) {
35                 createTable( $dbh );
36         } elsif( s/^@// ) {
37                 $tablename = $_;
38         } else {
39                 s/#.*//;
40                 s/<<([^<>]+)>>/$replaces{$1}/g;
41                 push @lines, $_;
42         }
43 }
44 close TABLES;
45 createTable( $dbh );
46 $dbh->commit();
47 $dbh->disconnect;