Skip to content

Commit

Permalink
Issue #1908: Updating sql drivers to use NOT NULL and DEFAULT correct…
Browse files Browse the repository at this point in the history
…ly, cannot use bind because it will quote variables with inverted commas in sql query.
  • Loading branch information
dennis-dko committed Dec 22, 2022
1 parent 9df8b6c commit eac8faa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
18 changes: 5 additions & 13 deletions Kernel/System/MigrateFromOTRS/CloneDB/Driver/mysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ sub TranslateColumnInfos {
}

# Alter table add column
# Note: add also custom columns which not belongs to standard
sub AlterTableAddColumn {
my ( $Self, %Param ) = @_;

Expand All @@ -285,32 +286,23 @@ sub AlterTableAddColumn {
}

my %ColumnInfos = %{ $Param{ColumnInfos} };
my $SQL = qq{ALTER TABLE ? ADD ? ?};

my @Bind = (
\$Param{Table},
\$Param{Column},
\$ColumnInfos{DATA_TYPE},
);
my $SQL = qq{ALTER TABLE $Param{Table} ADD $Param{Column} $ColumnInfos{DATA_TYPE}};

if ( $ColumnInfos{LENGTH} ) {
$SQL .= " (?)";
push(@Bind, \$ColumnInfos{LENGTH});
$SQL .= " ($ColumnInfos{LENGTH})";
}

if ( $ColumnInfos{COLUMN_DEFAULT} ) {
$SQL .= qq{ DEFAULT "?"};
push(@Bind, \$ColumnInfos{COLUMN_DEFAULT});
$SQL .= " DEFAULT '$ColumnInfos{COLUMN_DEFAULT}'";
}

# IS_NULLABLE is either YES or NO
if ( $ColumnInfos{IS_NULLABLE} eq "NO" ) {
$SQL .= ' NOT NULL';
$SQL .= " NOT NULL";
}

my $Success = $Param{DBObject}->Do(
SQL => $SQL,
Bind => \@Bind,
);

if ( !$Success ) {
Expand Down
18 changes: 5 additions & 13 deletions Kernel/System/MigrateFromOTRS/CloneDB/Driver/oracle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ sub TranslateColumnInfos {
}

# Alter table add column
# Note: add also custom columns which not belongs to standard
sub AlterTableAddColumn {
my ( $Self, %Param ) = @_;

Expand All @@ -370,32 +371,23 @@ sub AlterTableAddColumn {
}

my %ColumnInfos = %{ $Param{ColumnInfos} };
my $SQL = qq{ALTER TABLE ? ADD ? ?};

my @Bind = (
\$Param{Table},
\$Param{Column},
\$ColumnInfos{DATA_TYPE},
);
my $SQL = qq{ALTER TABLE $Param{Table} ADD $Param{Column} $ColumnInfos{DATA_TYPE}};

if ( $ColumnInfos{LENGTH} ) {
$SQL .= " (?)";
push(@Bind, \$ColumnInfos{LENGTH});
$SQL .= " ($ColumnInfos{LENGTH})";
}

if ( $ColumnInfos{COLUMN_DEFAULT} ) {
$SQL .= qq{ DEFAULT "?"};
push(@Bind, \$ColumnInfos{COLUMN_DEFAULT});
$SQL .= " DEFAULT '$ColumnInfos{COLUMN_DEFAULT}'";
}

# IS_NULLABLE is either YES or NO
if ( $ColumnInfos{IS_NULLABLE} eq "NO" ) {
$SQL .= ' NOT NULL';
$SQL .= " NOT NULL";
}

my $Success = $Param{DBObject}->Do(
SQL => $SQL,
Bind => \@Bind,
);

if ( !$Success ) {
Expand Down
18 changes: 5 additions & 13 deletions Kernel/System/MigrateFromOTRS/CloneDB/Driver/postgresql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ sub TranslateColumnInfos {
}

# Alter table add column
# Note: add also custom columns which not belongs to standard
sub AlterTableAddColumn {
my ( $Self, %Param ) = @_;

Expand All @@ -340,32 +341,23 @@ sub AlterTableAddColumn {
}

my %ColumnInfos = %{ $Param{ColumnInfos} };
my $SQL = qq{ALTER TABLE ? ADD ? ?};

my @Bind = (
\$Param{Table},
\$Param{Column},
\$ColumnInfos{DATA_TYPE},
);
my $SQL = qq{ALTER TABLE $Param{Table} ADD $Param{Column} $ColumnInfos{DATA_TYPE}};

if ( $ColumnInfos{LENGTH} ) {
$SQL .= " (?)";
push(@Bind, \$ColumnInfos{LENGTH});
$SQL .= " ($ColumnInfos{LENGTH})";
}

if ( $ColumnInfos{COLUMN_DEFAULT} ) {
$SQL .= qq{ DEFAULT "?"};
push(@Bind, \$ColumnInfos{COLUMN_DEFAULT});
$SQL .= " DEFAULT '$ColumnInfos{COLUMN_DEFAULT}'";
}

# IS_NULLABLE is either YES or NO
if ( $ColumnInfos{IS_NULLABLE} eq "NO" ) {
$SQL .= ' NOT NULL';
$SQL .= " NOT NULL";
}

my $Success = $Param{DBObject}->Do(
SQL => $SQL,
Bind => \@Bind,
);

if ( !$Success ) {
Expand Down

0 comments on commit eac8faa

Please sign in to comment.