X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer.pm;h=94a3d43529d11258110f6299697ac65a5399d02e;hb=1868ddbee17731eb23de17472c429c6bbf13a037;hp=b2ba10e44b1b4f738e4d6f62b085e8f05fa2b9e8;hpb=11ad2df91bcc0674faa8fb5b6bab52c9e4a73762;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer.pm b/lib/SQL/Translator/Producer.pm index b2ba10e..94a3d43 100644 --- a/lib/SQL/Translator/Producer.pm +++ b/lib/SQL/Translator/Producer.pm @@ -1,26 +1,9 @@ package SQL::Translator::Producer; -# ------------------------------------------------------------------- -# Copyright (C) 2002-4 SQLFairy Authors -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - use strict; -use vars qw($VERSION); -$VERSION = '1.59'; +use warnings; +use Scalar::Util (); +our $VERSION = '1.59'; sub produce { "" } @@ -32,7 +15,9 @@ sub produce { "" } ## They are special per Producer, and provide support for the old 'now()' ## default value exceptions sub _apply_default_value { - my (undef, $field_ref, $default, $exceptions) = @_; + my ($self, $field, $field_ref, $exceptions) = @_; + my $default = $field->default_value; + return if !defined $default; if ($exceptions and ! ref $default) { for (my $i = 0; $i < @$exceptions; $i += 2) { @@ -47,14 +32,27 @@ sub _apply_default_value { } } + my $type = lc $field->data_type; + my $is_numeric_datatype = ($type =~ /^(?:(?:big|medium|small|tiny)?int(?:eger)?|decimal|double|float|num(?:ber|eric)?|real)$/); + if (ref $default) { $$field_ref .= " DEFAULT $$default"; + } elsif ($is_numeric_datatype && Scalar::Util::looks_like_number ($default) ) { + # we need to check the data itself in addition to the datatype, for basic safety + $$field_ref .= " DEFAULT $default"; } else { - $$field_ref .= " DEFAULT '$default'"; + $default = $self->_quote_string($default); + $$field_ref .= " DEFAULT $default"; } } +sub _quote_string { + my ($self, $string) = @_; + $string =~ s/'/''/g; + return qq{'$string'}; +} + 1; # ------------------------------------------------------------------- @@ -72,8 +70,8 @@ SQL::Translator::Producer - describes how to write a producer Producer modules designed to be used with SQL::Translator need to implement a single function, called B. B will be -called with the SQL::Translator object from which it is expected to -retrieve the SQL::Translator::Schema object which has been populated +called with the SQL::Translator object from which it is expected to +retrieve the SQL::Translator::Schema object which has been populated by the parser. It is expected to return a string. =head1 METHODS