From fc4dc2f5837eaa149c5250dee8edb4422d05aa58 Mon Sep 17 00:00:00 2001 From: hylom Date: Sat, 22 Oct 2016 03:08:32 +0900 Subject: [PATCH] Model::LegacyDB: add 'sqlGetColumnData' and 'sqlGetCharColumnLength' method --- src/newslash_web/lib/Newslash/Model/LegacyDB.pm | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/newslash_web/lib/Newslash/Model/LegacyDB.pm b/src/newslash_web/lib/Newslash/Model/LegacyDB.pm index e97d4b73..cb212304 100644 --- a/src/newslash_web/lib/Newslash/Model/LegacyDB.pm +++ b/src/newslash_web/lib/Newslash/Model/LegacyDB.pm @@ -156,5 +156,34 @@ sub sqlSelectColArrayref { return $array; } +sub sqlGetColumnData { + my($self, $table, $col) = @_; + return unless $table; + + $self->sqlConnect() or return undef; + my $hr = $self->{_dbh}->selectall_hashref("SHOW COLUMNS FROM $table", 'Field'); + if ($col) { + # Return only one column's data. + return exists($hr->{$col}) ? $hr->{$col} : undef; + } + # Return all columns' data in a big hashref. + return $hr; +} + +{ # closure +my $_textcollen_hr = { }; +sub sqlGetCharColumnLength { + my($self, $table, $col) = @_; + return undef unless $table && $col; + return $_textcollen_hr->{$table}{$col} if exists $_textcollen_hr->{$table}{$col}; + $self->sqlConnect() or return undef; + my $hr = $self->sqlGetColumnData($table, $col); + return $_textcollen_hr->{$table}{$col} = undef if !$hr; + my $type = $hr->{Type}; + return $_textcollen_hr->{$table}{$col} = undef unless $type && $type =~ /^(?:var)?char\((\d+)\)$/i; + return $_textcollen_hr->{$table}{$col} = $1; +} +} # end closure + 1; -- 2.11.0