OSDN Git Service

Model::LegacyDB: add 'sqlGetColumnData' and 'sqlGetCharColumnLength' method
authorhylom <hylom@users.sourceforge.jp>
Fri, 21 Oct 2016 18:08:32 +0000 (03:08 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 21 Oct 2016 18:08:32 +0000 (03:08 +0900)
src/newslash_web/lib/Newslash/Model/LegacyDB.pm

index e97d4b7..cb21230 100644 (file)
@@ -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;