summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-09-17 11:45:42 +0200
committerYorhel <git@yorhel.nl>2019-09-17 11:48:38 +0200
commit158825be34b6987c18fcdf80da4d05aee5900be6 (patch)
treeb30a7ef502280af87b0bb3418a3a433f7ba6c441
parent3999549a305581966a353e07cae5ea7a9c191d80 (diff)
Rename VNDBSchema to VNDB::Schema and let it figure out the root path itself
I always avoided using the VNDB::* schema for non-web related utility modules, but thats pretty silly (especially as I'm slowly trying to move away from the old VNDB::* web code).
-rw-r--r--lib/VNDB/Schema.pm (renamed from lib/VNDBSchema.pm)16
-rwxr-xr-xutil/dbdump.pl8
-rw-r--r--util/sql/schema.sql2
-rwxr-xr-xutil/sqleditfunc.pl4
4 files changed, 15 insertions, 15 deletions
diff --git a/lib/VNDBSchema.pm b/lib/VNDB/Schema.pm
index 94f495b2..1094356c 100644
--- a/lib/VNDBSchema.pm
+++ b/lib/VNDB/Schema.pm
@@ -4,11 +4,14 @@
# This is not a full-blown SQL parser. The code makes all kinds of assumptions
# about the formatting of the .sql files.
-package VNDBSchema;
+package VNDB::Schema;
-use strict;
+use v5.12;
use warnings;
+my $ROOT = $INC{'VNDB/Schema.pm'} =~ s{/lib/VNDB/Schema\.pm}{}r;
+
+
# Reads schema.sql and returns a hashref with the following structure:
# {
# vn => {
@@ -25,10 +28,9 @@ use warnings;
# }
# }
sub schema {
- my $fn = shift;
my %schema;
my $table;
- open my $F, '<', $fn or die "$fn: $!";
+ open my $F, '<', "$ROOT/util/sql/schema.sql" or die "schema.sql: $!";
while(<$F>) {
chomp;
next if /^\s*--/ || /^\s*$/;
@@ -81,9 +83,8 @@ sub schema {
# }, ..
# }
sub types {
- my $fn = shift;
my %types;
- open my $F, '<', $fn or die "$fn: $!";
+ open my $F, '<', "$ROOT/util/sql/schema.sql" or die "schema.sql: $!";
while(<$F>) {
chomp;
if(/^CREATE TYPE ([^ ]+)/) {
@@ -106,9 +107,8 @@ sub types {
# }, ..
# ]
sub references {
- my $fn = shift;
my @ref;
- open my $F, '<', $fn or die "$fn: $!";
+ open my $F, '<', "$ROOT/util/sql/tableattrs.sql" or die "tableattrs.sql: $!";
while(<$F>) {
chomp;
next if !/^\s*ALTER\s+TABLE\s+([^ ]+)\s+ADD\s+CONSTRAINT\s+([^ ]+)\s+FOREIGN\s+KEY\s+\(([^\)]+)\)\s*REFERENCES\s+([^ ]+)\s*\(([^\)]+)\)/;
diff --git a/util/dbdump.pl b/util/dbdump.pl
index d513c989..f90cce8b 100755
--- a/util/dbdump.pl
+++ b/util/dbdump.pl
@@ -34,7 +34,7 @@ our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/dbdump\.pl$}{}; }
use lib "$ROOT/lib";
-use VNDBSchema;
+use VNDB::Schema;
# Tables and columns to export.
@@ -99,9 +99,9 @@ my %tables = (
);
my @tables = map +{ name => $_, %{$tables{$_}} }, sort keys %tables;
-my $schema = VNDBSchema::schema("$ROOT/util/sql/schema.sql");
-my $types = VNDBSchema::types("$ROOT/util/sql/schema.sql");
-my $references = VNDBSchema::references("$ROOT/util/sql/tableattrs.sql");
+my $schema = VNDB::Schema::schema;
+my $types = VNDB::Schema::types;
+my $references = VNDB::Schema::references;
my $db = DBI->connect('dbi:Pg:dbname=vndb', 'vndb', undef, { RaiseError => 1 });
$db->do('SET TIME ZONE +0');
diff --git a/util/sql/schema.sql b/util/sql/schema.sql
index 57f33efc..1450196a 100644
--- a/util/sql/schema.sql
+++ b/util/sql/schema.sql
@@ -40,7 +40,7 @@
-- defined in util/dbdump.pl.
--
-- Note: Every CREATE TABLE clause and each column should be on a separate
--- line. This file is parsed by lib/VNDBSchema.pm and it doesn't implement a
+-- line. This file is parsed by lib/VNDB/Schema.pm and it doesn't implement a
-- full SQL query parser.
diff --git a/util/sqleditfunc.pl b/util/sqleditfunc.pl
index 0d1749a2..3375136a 100755
--- a/util/sqleditfunc.pl
+++ b/util/sqleditfunc.pl
@@ -9,9 +9,9 @@ our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/sqleditfunc\.pl$}{}; }
use lib "$ROOT/lib";
-use VNDBSchema;
+use VNDB::Schema;
-my $schema = VNDBSchema::schema("$ROOT/util/sql/schema.sql");
+my $schema = VNDB::Schema::schema;
my $template = join '', <DATA>;
sub gensql {