2010-04-13 06:23:53 -04:00
|
|
|
#! /usr/bin/perl
|
2010-10-02 06:26:17 -04:00
|
|
|
# $OpenBSD: Column.pm,v 1.9 2010/10/02 10:26:17 espie Exp $
|
2010-04-13 06:23:53 -04:00
|
|
|
#
|
|
|
|
# Copyright (c) 2006-2010 Marc Espie <espie@openbsd.org>
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
# The Column hierarchy is mostly responsible for dealing with the database
|
|
|
|
# schema itself.
|
|
|
|
|
2010-04-13 06:23:53 -04:00
|
|
|
package Column;
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my ($class, $name) = @_;
|
2010-04-13 06:56:42 -04:00
|
|
|
$name //= $class->default_name;
|
2010-04-13 06:23:53 -04:00
|
|
|
bless {name => $name}, $class;
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub set_vartype
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
2010-04-13 06:56:42 -04:00
|
|
|
my ($self, $vartype) = @_;
|
2010-04-13 06:23:53 -04:00
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
$self->{vartype} = $vartype;
|
2010-04-13 06:23:53 -04:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub name
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{name};
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub normal_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $inserter, $class) = @_;
|
|
|
|
return $self->name." ".$self->sqltype;
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub view_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $t) = @_;
|
2010-04-25 08:35:26 -04:00
|
|
|
return $self->realname($t)." AS ".$self->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub realname
|
|
|
|
{
|
|
|
|
my ($self, $t) = @_;
|
|
|
|
return $t.".".$self->name;
|
2010-04-13 06:23:53 -04:00
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub join_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
2010-04-17 10:48:15 -04:00
|
|
|
return undef;
|
2010-04-13 06:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
package TextColumn;
|
|
|
|
our @ISA = qw(Column);
|
|
|
|
|
|
|
|
sub sqltype
|
|
|
|
{
|
|
|
|
return "TEXT NOT NULL";
|
|
|
|
}
|
|
|
|
|
|
|
|
package OptTextColumn;
|
|
|
|
our @ISA = qw(TextColumn);
|
|
|
|
|
|
|
|
sub sqltype
|
|
|
|
{
|
|
|
|
return "TEXT";
|
|
|
|
}
|
|
|
|
|
|
|
|
package IntegerColumn;
|
2010-04-17 09:06:49 -04:00
|
|
|
our @ISA = qw(Column);
|
2010-04-13 06:23:53 -04:00
|
|
|
sub sqltype
|
|
|
|
{
|
|
|
|
return "INTEGER NOT NULL";
|
|
|
|
}
|
|
|
|
|
|
|
|
package OptIntegerColumn;
|
|
|
|
our @ISA = qw(IntegerColumn);
|
|
|
|
sub sqltype
|
|
|
|
{
|
|
|
|
return "INTEGER";
|
|
|
|
}
|
|
|
|
|
|
|
|
package RefColumn;
|
|
|
|
our @ISA = qw(Column);
|
|
|
|
|
|
|
|
my $table = "T0001";
|
|
|
|
|
|
|
|
sub table
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2010-04-13 06:56:42 -04:00
|
|
|
$self->{table} //= $table++;
|
2010-04-13 06:23:53 -04:00
|
|
|
return $self->{table};
|
|
|
|
}
|
|
|
|
|
|
|
|
package PathColumn;
|
|
|
|
our @ISA = qw(RefColumn);
|
|
|
|
|
|
|
|
sub default_name
|
|
|
|
{
|
|
|
|
return "FULLPKGPATH";
|
|
|
|
}
|
|
|
|
|
2010-04-25 08:35:26 -04:00
|
|
|
sub realname
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $t) = @_;
|
2010-04-25 08:35:26 -04:00
|
|
|
return $self->table.".FULLPKGPATH";
|
2010-04-13 06:23:53 -04:00
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub normal_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $inserter, $class) = @_;
|
|
|
|
return $inserter->pathref($self->name);
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub join_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $table) = @_;
|
|
|
|
return "JOIN Paths ".$self->{table}." ON ".$self->table.".ID=$table.".$self->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
package ValueColumn;
|
|
|
|
our @ISA = qw(RefColumn);
|
|
|
|
|
|
|
|
sub default_name
|
|
|
|
{
|
|
|
|
return "VALUE";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub k
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2010-04-13 06:56:42 -04:00
|
|
|
return $self->{vartype}->keyword_table;
|
2010-04-13 06:23:53 -04:00
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub normal_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $inserter) = @_;
|
|
|
|
return $inserter->value($self->k, $self->name);
|
|
|
|
}
|
|
|
|
|
2010-04-25 08:35:26 -04:00
|
|
|
sub realname
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $t) = @_;
|
|
|
|
if (defined $self->k) {
|
2010-04-25 08:35:26 -04:00
|
|
|
return $self->table.".VALUE";
|
2010-04-13 06:23:53 -04:00
|
|
|
} else {
|
2010-04-25 08:35:26 -04:00
|
|
|
return $self->SUPER::realname($t);
|
2010-04-13 06:23:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub join_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $table) = @_;
|
|
|
|
if (defined $self->k) {
|
|
|
|
return "JOIN ".$self->k." ".$self->table." ON ".$self->table.".KEYREF=$table.".$self->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
package OptValueColumn;
|
|
|
|
our @ISA = qw(ValueColumn);
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub normal_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $inserter) = @_;
|
|
|
|
return $inserter->optvalue($self->k, $self->name);
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:56:42 -04:00
|
|
|
sub join_schema
|
2010-04-13 06:23:53 -04:00
|
|
|
{
|
|
|
|
my ($self, $table) = @_;
|
2010-04-13 06:56:42 -04:00
|
|
|
return "LEFT ".$self->SUPER::join_schema($table);
|
2010-04-13 06:23:53 -04:00
|
|
|
}
|
|
|
|
|
2010-04-26 06:19:02 -04:00
|
|
|
package OptCoalesceColumn;
|
|
|
|
our @ISA = qw(OptValueColumn);
|
|
|
|
|
|
|
|
sub realname
|
|
|
|
{
|
|
|
|
my ($self, $t) = @_;
|
|
|
|
return "group_concat(".$self->SUPER::realname($t).", ' ')";
|
|
|
|
}
|
|
|
|
|
|
|
|
package CoalesceColumn;
|
|
|
|
our @ISA = qw(ValueColumn);
|
|
|
|
|
|
|
|
sub realname
|
|
|
|
{
|
|
|
|
my ($self, $t) = @_;
|
|
|
|
return "group_concat(".$self->SUPER::realname($t).", ' ')";
|
|
|
|
}
|
|
|
|
|
2010-04-13 06:23:53 -04:00
|
|
|
1;
|