Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
awit-frameworks
awit-perl-toolkit
Commits
a3e3e727
Commit
a3e3e727
authored
Sep 15, 2016
by
Nigel Kukard
Browse files
Improved the TSQL conversion to use objects and implemented CI tests
parent
8ce53c02
Changes
11
Hide whitespace changes
Inline
Side-by-side
Makefile.PL
View file @
a3e3e727
...
...
@@ -26,11 +26,11 @@ use ExtUtils::MakeMaker;
WriteMakefile(
NAME
=>
'AWITPT'
,
VERSION_FROM
=>
"lib/AWITPT/Version.pm"
,
'
NAME
'
=>
'AWITPT'
,
'
VERSION_FROM
'
=>
"lib/AWITPT/Version.pm"
,
EXE_FILES
=>
[
qw
(
bin/convert-tsql
)]
,
MAN3PODS
=>
{
'
EXE_FILES
'
=>
[qw(
bin/convert-tsql
)],
'
MAN3PODS
'
=>
{
'lib/AWITPT/DataObj.pm'
=> '$(INST_MAN3DIR)/AWITPT
::
DataObj.3'
,
'lib/AWITPT/DB/DataObj.pm'
=>
'
$(INST_MAN3DIR)
/AWITPT::DB::DataObj.3'
}
,
...
...
bin/convert-tsql
View file @
a3e3e727
#!/
bin/bash
#!/
usr/bin/perl
# Database translation/creation script
# Copyright (C) 2009-2016, AllWorldIT
# Copyright (C) 2008, LinuxRulz
...
...
@@ -17,98 +17,24 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
database
=
"
$1
"
file
=
"
$2
"
prefix
=
"
$3
"
# Display usage info
display_usage
()
{
echo
"Usage:
$0
<database type> <file> [prefix]"
echo
echo
"Valid database types:"
echo
" mysql - For MySQL v5 (v5.5)"
echo
" mysql55 - For MySQL v5.5"
echo
" mysql4 - For MySQL v4"
echo
" pgsql - For PostgreSQL"
echo
" sqlite - For SQLite v3"
echo
exit
}
use
strict
;
use
warnings
;
# Check we have our params
if
[
-z
"
$database
"
-o
-z
"
$file
"
]
then
display_usage
fi
use
Config
;
use
FindBin
;
use
lib
("
$FindBin
::Bin/../share/perl5
",
"
$FindBin
::Bin/../share/perl/
$Config
{'version'}
");
use
AWITPT::Util::ConvertTSQL::
client
;
# Check file exists
if
[
!
-f
"
$file
"
]
then
echo
"ERROR: Cannot open file '
$file
'"
exit
1
fi
# Check what we converting for
case
"
$database
"
in
"mysql"
|
"mysql55"
)
sed
\
-e
"s/@PREFIX@/
$prefix
/g"
\
-e
's/@PRELOAD@/SET FOREIGN_KEY_CHECKS=0;/'
\
-e
's/@POSTLOAD@/SET FOREIGN_KEY_CHECKS=1;/'
\
-e
's/@CREATE_TABLE_SUFFIX@/ENGINE=InnoDB CHARACTER SET latin1 COLLATE latin1_bin/'
\
-e
's/@SERIAL_TYPE@/SERIAL/'
\
-e
's/@BIG_INTEGER_UNSIGNED@/BIGINT UNSIGNED/'
\
-e
's/@INT_UNSIGNED@/INT UNSIGNED/'
\
-e
's/@TRACK_KEY_LEN@/512/'
\
-e
's/@SERIAL_REF_TYPE@/BIGINT UNSIGNED/'
<
"
$file
"
;;
"mysql4"
)
sed
\
-e
"s/@PREFIX@/
$prefix
/g"
\
-e
's/@PRELOAD@/SET FOREIGN_KEY_CHECKS=0;/'
\
-e
's/@POSTLOAD@/SET FOREIGN_KEY_CHECKS=1;/'
\
-e
's/@CREATE_TABLE_SUFFIX@/TYPE=InnoDB CHARACTER SET latin1 COLLATE latin1_bin/'
\
-e
's/@SERIAL_TYPE@/SERIAL/'
\
-e
's/@BIG_INTEGER_UNSIGNED@/BIGINT UNSIGNED/'
\
-e
's/@TRACK_KEY_LEN@/255/'
\
-e
's/@SERIAL_REF_TYPE@/BIGINT UNSIGNED/'
<
"
$file
"
;;
"pgsql"
)
sed
\
-e
"s/@PREFIX@/
$prefix
/g"
\
-e
's/@PRELOAD@/SET CONSTRAINTS ALL DEFERRED;/'
\
-e
's/@POSTLOAD@//'
\
-e
's/@CREATE_TABLE_SUFFIX@//'
\
-e
's/@SERIAL_TYPE@/SERIAL PRIMARY KEY/'
\
-e
's/@BIG_INTEGER_UNSIGNED@/INT8/'
\
-e
's/@TRACK_KEY_LEN@/512/'
\
-e
's/@SERIAL_REF_TYPE@/INT8/'
<
"
$file
"
;;
"sqlite"
)
sed
\
-e
"s/@PREFIX@/
$prefix
/g"
\
-e
's/@PRELOAD@//'
\
-e
's/@POSTLOAD@//'
\
-e
's/@CREATE_TABLE_SUFFIX@//'
\
-e
's/@SERIAL_TYPE@/INTEGER PRIMARY KEY AUTOINCREMENT/'
\
-e
's/@BIG_INTEGER_UNSIGNED@/UNSIGNED BIG INT/'
\
-e
's/@TRACK_KEY_LEN@/512/'
\
-e
's/@SERIAL_REF_TYPE@/INT8/'
<
"
$file
"
;;
*
)
echo
"ERROR: Invalid database type '
$database
'"
exit
1
;;
esac
# Grab and exit with result received
my
$res
=
AWITPT::Util::ConvertTSQL::
client
->
run
();
exit
(
$res
);
lib/AWITPT/Util/ConvertTSQL.pm
0 → 100755
View file @
a3e3e727
# TSQL conversion
# Copyright (C) 2016, AllWorldIT
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
=encoding utf8
=head1 NAME
AWITPT::Util::ConvertTSQL - TSQL conversion
=head1 SYNOPSIS
#
# This class is only meant to be inherited
#
=head1 DESCRIPTION
The AWITPT::Object class provides a basic abstraction layer to Perl objects.
=cut
package
AWITPT::Util::
ConvertTSQL
;
use
strict
;
use
warnings
;
use
base
'
AWITPT::Object
';
our
$VERSION
=
"
1.000
";
=head1 METHODS
C<AWITPT::Util::ConvertTSQL> provides the below methods.
=cut
=head2 convert
$tsql->convert("CREATE @PREFIX@table ( ....... );";
The C<convert> method is called to convert a piece of TSQL into the format we're working with.
=over
=back
=cut
# Convert a TSQL line into the current format
sub
convert
{
my
(
$self
,
$line
)
=
@_
;
# Loop with the expressions
while
((
my
$macro
,
my
$value
)
=
each
%
{
$self
->
{'
expressions
'}})
{
$line
=~
s/$macro/$value/g
;
}
return
$line
;
}
=head2 substitute
# Example using ->substitute in config()
sub config
{
my ($self,@params) = @_;
$self->substitute('@SERIAL_TYPE@','SERIAL');
return $self;
}
The C<substitute> method is called to create a substitution of a macro into a piece of text, used when converting TSQL into various
other DB formats.
=over
=back
=cut
# Substitute macro into something
sub
substitute
{
my
(
$self
,
$macro
,
$value
)
=
@_
;
$self
->
{'
expressions
'}
->
{
$macro
}
=
$value
;
return
$self
;
}
#
# INTERNAL METHODS BELOW
#
=head2 _config
sub _config
{
my ($self,@params) = @_;
$self->substitute('@TEST@','ABCD');
return $self;
}
The C<_config> method is called during initialization to setup regular expressions. The @params comes from the C<new>->() call.
=over
=back
=cut
# Configure the conversion expressions
sub
_config
{
my
(
$self
,
@params
)
=
@_
;
$self
->
substitute
('
@PREFIX@
',
$self
->
{'
prefix
'});
return
$self
;
}
=head2 _init
The C<_init> method is called during class instantiation before its returned from the new() method. In this case we initialize
the regular expression property.
=over
=back
=cut
# Initialize internals of the object
sub
_init
{
my
(
$self
,
@params
)
=
@_
;
# If we have uneven number of params, the first will be our sub-class
if
(
@params
%
2
)
{
my
$subClass
=
shift
(
@params
);
my
$newClassPM
=
"
AWITPT/Util/ConvertTSQL/
$subClass
.pm
";
my
$newClass
=
"
AWITPT::Util::ConvertTSQL::
$subClass
";
# Try load the new class
# uncoverable branch true
eval
{
require
$newClassPM
;
};
# Check if the eval succeeded
if
(
$@
)
{
warn
"
AWITPT::Util::ConvertTSQL::
$subClass
not found
";
return
;
};
# Import methods
$newClass
->
import
();
# Instantiate the new class and replace $self
return
$newClass
->
new
(
@params
);
}
# Grab parameter list
my
%params
=
(
@params
);
# Populate internal properties
$self
->
{'
prefix
'}
=
$params
{'
prefix
'}
//
"";
$self
->
{'
expressions
'}
=
{
};
return
$self
->
_config
();
}
1
;
__END__
=head1 AUTHORS
Nigel Kukard E<lt>nkukard@lbsd.netE<gt>
=head1 BUGS
All bugs should be reported via the project issue tracker
L<http://gitlab.devlabs.linuxassist.net/awit-frameworks/awit-perl-toolkit/issues/>.
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2016, AllWorldIT
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, either version 3 of the License, or
(at your option) any later version.
=head1 SEE ALSO
L<AWITPT::DataObj>.
=cut
lib/AWITPT/Util/ConvertTSQL/MySQL.pm
0 → 100644
View file @
a3e3e727
# TSQL conversion to MySQL
# Copyright (C) 2016, AllWorldIT
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
=encoding utf8
=head1 NAME
AWITPT::Util::ConvertTSQL::MySQL - TSQL conversion to MySQL
=head1 SYNOPSIS
my $tsql = AWITPT::Util::ConvertTSQL->new('MySQL','prefix' => "myapp_");
=head1 DESCRIPTION
The AWITPT::Util::ConvertTSQL::MySQL class provides TSQL conversion to MySQL.
=cut
package
AWITPT::Util::ConvertTSQL::
MySQL
;
use
strict
;
use
warnings
;
use
base
'
AWITPT::Util::ConvertTSQL
';
our
$VERSION
=
"
1.000
";
our
(
@EXPORT
,
@EXPORT_OK
);
@EXPORT
=
qw(
)
;
@EXPORT_OK
=
qw(
)
;
=head1 METHODS
C<AWITPT::Util::ConvertTSQL::MySQL> provides the below methods.
=cut
=head2 new
This class is instantiated by the parent class.
The C<new> method is used to instantiate the object, it supports some options including 'prefix' which can be used to set the
table prefix for the resulting SQL.
=head3 'prefix'
Allow the specification of the resulting table prefixes.
=over
=cut
#
# INTERNAL METHODS BELOW
#
# Initialize internals of the object
sub
_config
{
my
(
$self
,
@params
)
=
@_
;
# Call parent config
$self
->
SUPER::
_config
(
@params
);
$self
->
substitute
('
@PRELOAD@
','
SET FOREIGN_KEY_CHECKS=0;
');
$self
->
substitute
('
@POSTLOAD@
','
SET FOREIGN_KEY_CHECKS=1;
');
$self
->
substitute
('
@CREATE_TABLE_SUFFIX@
','
ENGINE=InnoDB CHARACTER SET latin1 COLLATE latin1_bin
');
$self
->
substitute
('
@SERIAL_TYPE@
','
SERIAL
');
$self
->
substitute
('
@SERIAL_REF_TYPE@
','
BIGINT UNSIGNED
');
$self
->
substitute
('
@BIG_INTEGER_UNSIGNED@
','
BIGINT UNSIGNED
');
$self
->
substitute
('
@INT_UNSIGNED@
','
INT UNSIGNED
');
$self
->
substitute
('
@TRACK_KEY_LEN@
','
512
');
return
$self
;
}
1
;
__END__
=head1 AUTHORS
Nigel Kukard E<lt>nkukard@lbsd.netE<gt>
=head1 BUGS
All bugs should be reported via the project issue tracker
L<http://gitlab.devlabs.linuxassist.net/awit-frameworks/awit-perl-toolkit/issues/>.
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2016, AllWorldIT
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, either version 3 of the License, or
(at your option) any later version.
=head1 SEE ALSO
L<AWITPT::Util::ConvertTSQL>, L<AWITPT::DataObj>.
=cut
lib/AWITPT/Util/ConvertTSQL/PgSQL.pm
0 → 100644
View file @
a3e3e727
# TSQL conversion to PgSQL
# Copyright (C) 2016, AllWorldIT
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
=encoding utf8
=head1 NAME
AWITPT::Util::ConvertTSQL::PgSQL - TSQL conversion to PgSQL
=head1 SYNOPSIS
my $tsql = AWITPT::Util::ConvertTSQL->new('PgSQL','prefix' => "myapp_");
=head1 DESCRIPTION
The AWITPT::Util::ConvertTSQL::PgSQL class provides TSQL conversion to PgSQL.
=cut
package
AWITPT::Util::ConvertTSQL::
PgSQL
;
use
strict
;
use
warnings
;
use
base
'
AWITPT::Util::ConvertTSQL
';
our
$VERSION
=
"
1.000
";
our
(
@EXPORT
,
@EXPORT_OK
);
@EXPORT
=
qw(
)
;
@EXPORT_OK
=
qw(
)
;
=head1 METHODS
C<AWITPT::Util::ConvertTSQL::PgSQL> provides the below methods.
=cut
=head2 new
This class is instantiated by the parent class.
The C<new> method is used to instantiate the object, it supports some options including 'prefix' which can be used to set the
table prefix for the resulting SQL.
=head3 'prefix'
Allow the specification of the resulting table prefixes.
=over
=cut
#
# INTERNAL METHODS BELOW
#
# Initialize internals of the object
sub
_config
{
my
(
$self
,
@params
)
=
@_
;
# Call parent config
$self
->
SUPER::
_config
(
@params
);
$self
->
substitute
('
@PRELOAD@
','
SET CONSTRAINTS ALL DEFERRED;
');
$self
->
substitute
('
@POSTLOAD@
','
SET CONSTRAINTS NOT DEFERRED;
');
$self
->
substitute
('
@CREATE_TABLE_SUFFIX@
','');
$self
->
substitute
('
@SERIAL_TYPE@
','
SERIAL
');
$self
->
substitute
('
@SERIAL_REF_TYPE@
','
BIGINT UNSIGNED
');
$self
->
substitute
('
@BIG_INTEGER_UNSIGNED@
','
BIGINT UNSIGNED
');
$self
->
substitute
('
@INT_UNSIGNED@
','
INT UNSIGNED
');
$self
->
substitute
('
@TRACK_KEY_LEN@
','
512
');
return
$self
;
}
1
;
__END__
=head1 AUTHORS
Nigel Kukard E<lt>nkukard@lbsd.netE<gt>
=head1 BUGS
All bugs should be reported via the project issue tracker
L<http://gitlab.devlabs.linuxassist.net/awit-frameworks/awit-perl-toolkit/issues/>.
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2016, AllWorldIT
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, either version 3 of the License, or
(at your option) any later version.
=head1 SEE ALSO
L<AWITPT::Util::ConvertTSQL>, L<AWITPT::DataObj>.
=cut
lib/AWITPT/Util/ConvertTSQL/SQLite.pm
0 → 100644
View file @
a3e3e727
# TSQL conversion to SQLite
# Copyright (C) 2016, AllWorldIT
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General P