Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information
melo committed Mar 22, 2012
1 parent f3a4a5d commit d7bc969
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/Percy/Schema/Type.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ for my $w (qw(before after)) {
has $attr => (is => 'ro');

no strict 'refs';
*{__PACKAGE__ . '::' . $meth} = sub {
*{ __PACKAGE__ . '::' . $meth } = sub {
return ($_[0]{$attr} || sub { })->(@_);
};
}
Expand Down
13 changes: 5 additions & 8 deletions lib/Percy/Storage/DBI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ sub create {
$spec->before_change($self, $r, 'create');
$spec->before_create($self, $r);

my $oid = $r->{oid} =
_dbi_create_obj($dbh, $r, $spec->encode_to_db($self, $r));
my $oid = $r->{oid} = _dbi_create_obj($dbh, $r, $spec->encode_to_db($self, $r));

$spec->after_create($self, $r);
$spec->after_change($self, $r, 'create');
Expand Down Expand Up @@ -310,8 +309,7 @@ sub _dbi_obj_where {
return ("$sql WHERE oid=?", $r->{oid}) if defined $r->{oid};
return ("$sql WHERE type=? AND pk=?", $r->{type}, $r->{pk})
if defined $r->{type} && defined $r->{pk};
Carp::confess(
"FATAL: insufficient conditions to identify a specific object,");
Carp::confess("FATAL: insufficient conditions to identify a specific object,");
}

sub _type_fetch {
Expand Down Expand Up @@ -405,8 +403,7 @@ sub deploy {
sub _generate_table_stmts {
my $class = ref($_[0]) || $_[0];

Carp::confess(
"FATAL: redefine the _generate_table_stmts() method in '$class',");
Carp::confess("FATAL: redefine the _generate_table_stmts() method in '$class',");
}

sub _collect_obj_storage_table_stmts {
Expand All @@ -430,7 +427,7 @@ sub _collect_obj_storage_table_stmts {
},
],
pk => ['pk'],
unique => {pk_type => [qw(pk type)]},
unique => { pk_type => [qw(pk type)] },
};

return $self->_generate_table_stmts($table);
Expand Down Expand Up @@ -472,7 +469,7 @@ sub _collect_set_table_stmts {
$type = 'VARCHAR(100)' if $type eq 'String';
$type = uc($type);

push @{$table->{fields}}, {name => $field, type => "$type NOT NULL"};
push @{ $table->{fields} }, { name => $field, type => "$type NOT NULL" };
$table->{indexes}{$sn} = ['m_oid', $field];
}

Expand Down
17 changes: 6 additions & 11 deletions lib/Percy/Storage/DBI/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sub _generate_table_stmts {
my $skip_pk;
my $tn = $table->{name};
my $tbl = "CREATE TABLE IF NOT EXISTS $tn (\n";
for my $f (@{$table->{fields}}) {
for my $f (@{ $table->{fields} }) {
$tbl .= ",\n" if $skip_pk;
$tbl .= " $f->{name} $f->{type}";
if ($f->{is_auto_increment}) {
Expand All @@ -29,26 +29,21 @@ sub _generate_table_stmts {
}

if (!$skip_pk) {
$tbl .= "\n CONSTRAINT ${tn}_pk PRIMARY KEY ("
. join(', ', @{$table->{pk}}) . ")\n";
$tbl .= "\n CONSTRAINT ${tn}_pk PRIMARY KEY (" . join(', ', @{ $table->{pk} }) . ")\n";
}
$tbl .= ')';
push @sql_stmts, $tbl;

## Indexes
while (my ($in, $if) = each %{$table->{indexes} || {}}) {
while (my ($in, $if) = each %{ $table->{indexes} || {} }) {
push @sql_stmts,
"CREATE INDEX IF NOT EXISTS ${tn}_${in}_idx"
. " ON $tn ("
. join(', ', @$if) . ")";
"CREATE INDEX IF NOT EXISTS ${tn}_${in}_idx" . " ON $tn (" . join(', ', @$if) . ")";
}

## Unique keys
while (my ($un, $uf) = each %{$table->{unique} || {}}) {
while (my ($un, $uf) = each %{ $table->{unique} || {} }) {
push @sql_stmts,
"CREATE UNIQUE INDEX IF NOT EXISTS ${tn}_${un}_un"
. " ON $tn ("
. join(', ', @$uf) . ")";
"CREATE UNIQUE INDEX IF NOT EXISTS ${tn}_${un}_un" . " ON $tn (" . join(', ', @$uf) . ")";
}

return \@sql_stmts;
Expand Down
12 changes: 5 additions & 7 deletions lib/Percy/Storage/DBI/mysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,24 @@ sub _generate_table_stmts {
my $tbl = "CREATE TABLE IF NOT EXISTS $tn (\n";

my @fields;
for my $f (@{$table->{fields}}) {
for my $f (@{ $table->{fields} }) {
push @fields, " $f->{name} $f->{type}";
$fields[-1] .= " PRIMARY KEY AUTO_INCREMENT" if $f->{is_auto_increment};
}
$tbl .= join(",\n", @fields);

if (@$pks > 1) {
$tbl .= ",\n\n CONSTRAINT ${tn}_pk PRIMARY KEY ("
. join(', ', @{$table->{pk}}) . ")";
$tbl .= ",\n\n CONSTRAINT ${tn}_pk PRIMARY KEY (" . join(', ', @{ $table->{pk} }) . ")";
}

## Indexes
while (my ($in, $if) = each %{$table->{indexes} || {}}) {
while (my ($in, $if) = each %{ $table->{indexes} || {} }) {
$tbl .= ",\n INDEX ${tn}_${in}_idx (" . join(', ', @$if) . ")";
}

## Unique keys
while (my ($un, $uf) = each %{$table->{unique} || {}}) {
$tbl
.= ",\n CONSTRAINT ${tn}_${un}_un UNIQUE (" . join(', ', @$uf) . ")";
while (my ($un, $uf) = each %{ $table->{unique} || {} }) {
$tbl .= ",\n CONSTRAINT ${tn}_${un}_un UNIQUE (" . join(', ', @$uf) . ")";
}
$tbl .= "\n) ENGINE = InnoDB\n";

Expand Down

0 comments on commit d7bc969

Please sign in to comment.