Subject: | Add Select Alias support |
I needed column alias support in the select function so I hacked this.
I do not know how portable across DBs the column alias capability is.
100% backwards compatible and all tests pass.
sabs->select("table", ["col1", [col2=>"alias2"], {col3=>alias3}]);
$ diff -r SQL-Abstract-1.22 SQL-Abstract
diff -r SQL-Abstract-1.22/lib/SQL/Abstract.pm SQL-
Abstract/lib/SQL/Abstract.pm
147c147
< our $VERSION = '1.22';
---
Show quoted text
> our $VERSION = '1.22.01';
537c537,554
< my $f = (ref $fields eq 'ARRAY') ? join ', ', map { $self->_quote
($_) } @$fields : $fields;
---
Show quoted text> my $f=$fields;
> if (ref($fields) eq 'ARRAY') {
> $f=join ', ',
> map {
> if (ref($_) eq "ARRAY") {
> die("Error: Array reference must have two values")
> unless scalar(@$_) == 2;
> sprintf(qq{%s AS "%s"}, $self->_quote($_->[0]), $_-
>[1]);
> } elsif (ref($_) eq "HASH") {
> die("Error: Hash reference must have one key")
> unless scalar(keys %$_) == 1;
> sprintf(qq{%s AS "%s"}, $self->_quote(keys(%$_)),
values(%$_));
Show quoted text> } else {
> $self->_quote($_);
> }
> } @$fields;
> }
>
diff -r SQL-Abstract-1.22/t/01generate.t SQL-Abstract/t/01generate.t
9c9
< BEGIN { plan tests => 60 }
---
Show quoted text> BEGIN { plan tests => 62 }
16a17,25
Show quoted text> args => ['test', ["scalar",
> [array=>"Aalias"],
> {hash=>"Halias"}]],
> stmt => 'SELECT scalar, array AS "Aalias", hash
AS "Halias" FROM test',
Show quoted text> stmt_q => 'SELECT `scalar`, `array` AS "Aalias", `hash`
AS "Halias" FROM `test`',
Show quoted text> bind => []
> },
> {
> func => 'select',
I, Michael R. Davis (mrdvt92), hereby release this code to the public
domain.