| 1 | ############################################################################# | ||
| 2 | ## Name: mysql.pm | ||
| 3 | ## Purpose: HDB::MOD::mysql -> for DBD::MySQL | ||
| 4 | ## Author: Graciliano M. P. | ||
| 5 | ## Modified by: | ||
| 6 | ## Created: 15/01/2003 | ||
| 7 | ## RCS-ID: | ||
| 8 | ## Copyright: (c) 2002 Graciliano M. P. | ||
| 9 | ## Licence: This program is free software; you can redistribute it and/or | ||
| 10 | ## modify it under the same terms as Perl itself | ||
| 11 | ############################################################################# | ||
| 12 | |||
| 13 | package HDB::MOD::mysql ; | ||
| 14 | |||
| 15 | my $DRIVER = 'mysql' ; | ||
| 16 | |||
| 17 | BEGIN { | ||
| 18 | eval { require DBD::mysql } ; | ||
| 19 | |||
| 20 | if ( $@ ) { | ||
| 21 | ## Try a pure Perl version of MySQL client. | ||
| 22 | eval { require DBD::mysqlPP } ; | ||
| 23 | if ( !$@ ) { $DRIVER = 'mysqlPP' ;} | ||
| 24 | else { | ||
| 25 | die("Can't load DBD::mysql or DBD::mysqlPP") ; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | use strict qw(vars) ; | ||
| 31 | no warnings ; | ||
| 32 | |||
| 33 | our $VERSION = '1.0' ; | ||
| 34 | our @ISA = qw(HDB::MOD) ; | ||
| 35 | |||
| 36 | my %SQL = ( | ||
| 37 | REGEXP => 1 , | ||
| 38 | LOCK_TABLE => 1 , | ||
| 39 | SHOW => 1 , | ||
| 40 | LIMIT => 1 , | ||
| 41 | TYPES => ['*'] , | ||
| 42 | TYPES_MASK => { | ||
| 43 | 'BOOLEAN' => 'BOOL' , | ||
| 44 | } , | ||
| 45 | ) ; | ||
| 46 | |||
| 47 | ####### | ||
| 48 | # NEW # | ||
| 49 | ####### | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my $this = shift ; | ||
| 53 | |||
| 54 | $this->{SQL} = \%SQL ; | ||
| 55 | $this->{name} = 'HDB::MySQL' ; | ||
| 56 | |||
| 57 | bless($this , __PACKAGE__) ; | ||
| 58 | return( $this ) ; | ||
| 59 | } | ||
| 60 | |||
| 61 | ########### | ||
| 62 | # CONNECT # | ||
| 63 | ########### | ||
| 64 | |||
| 65 | sub MOD_connect { | ||
| 66 | my $this = shift ; | ||
| 67 | my ( $pass ) = @_ ; | ||
| 68 | |||
| 69 | my $db = $this->{db} ; | ||
| 70 | my $host = $this->{host} ; | ||
| 71 | |||
| 72 | $this->{dbh} = DBI->connect("DBI:$DRIVER:database=$db;host=$host", $this->{user} , $pass , { RaiseError => 0 , PrintError => 1 , AutoCommit => 1 }) ; | ||
| 73 | |||
| 74 | if (! $this->{dbh} ) { return $this->Error("Can't connect to db $db\@$host!") ;} | ||
| 75 | |||
| 76 | return( $this->{dbh} ) ; | ||
| 77 | } | ||
| 78 | |||
| 79 | ################# | ||
| 80 | # TABLE_COLUMNS # | ||
| 81 | ################# | ||
| 82 | |||
| 83 | sub table_columns { | ||
| 84 | my $this = shift ; | ||
| 85 | my ( $table ) = @_ ; | ||
| 86 | |||
| 87 | $table = HDB::CMDS::_format_table_name($table) ; | ||
| 88 | |||
| 89 | if (! $table) { $this->Error('Invalid table!') ; return ;} | ||
| 90 | |||
| 91 | my @cols = $this->cmd( "show columns from $table" , '@' ) ; | ||
| 92 | |||
| 93 | my %cols ; | ||
| 94 | |||
| 95 | foreach my $cols_i ( @cols ) { | ||
| 96 | my ( $col , $type ) = @$cols_i ; | ||
| 97 | $cols{$col} = $type ; | ||
| 98 | } | ||
| 99 | |||
| 100 | return %cols ; | ||
| 101 | } | ||
| 102 | |||
| 103 | ############## | ||
| 104 | # TYPE_FLOAT # | ||
| 105 | ############## | ||
| 106 | |||
| 107 | sub Type_FLOAT { | ||
| 108 | my $this = shift ; | ||
| 109 | my ( $type , $args ) = @_ ; | ||
| 110 | |||
| 111 | my $plus_minus ; | ||
| 112 | |||
| 113 | if ( $type =~ /^\s*([\+\-])\s*(\w+)/s) { $plus_minus = $1 ; $type = $2 ;} | ||
| 114 | |||
| 115 | $type =~ s/\W//gs ; | ||
| 116 | |||
| 117 | if ($type =~ /^f/i) { $type = 'FLOAT' ;} | ||
| 118 | elsif ($type =~ /^d/i) { $type = 'DOUBLE' ;} | ||
| 119 | |||
| 120 | my $unsigned ; | ||
| 121 | |||
| 122 | if ($plus_minus eq '+') { $unsigned = ' UNSIGNED' ;} | ||
| 123 | |||
| 124 | if ( $args !~ /\d/ ) { return($type . $unsigned) ;} | ||
| 125 | |||
| 126 | my $tp_arg ; | ||
| 127 | |||
| 128 | if ( $args =~ /(\d+)\D+(\d+)/ ) { $tp_arg = "($1,$2)" ;} | ||
| 129 | elsif ( $args =~ /(\d+)/ ) { $tp_arg = "($1)" ;} | ||
| 130 | |||
| 131 | return($type . $tp_arg . $unsigned) ; | ||
| 132 | } | ||
| 133 | |||
| 134 | ################# | ||
| 135 | # AUTOINCREMENT # | ||
| 136 | ################# | ||
| 137 | |||
| 138 | sub AUTOINCREMENT { return( "INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY" ) ;} | ||
| 139 | |||
| 140 | ############## | ||
| 141 | # LOCK_TABLE # | ||
| 142 | ############## | ||
| 143 | |||
| 144 | sub lock_table { $_[0]->dbh->do("LOCK TABLES $_[1] WRITE , $_[1] READ") ;} | ||
| 145 | |||
| 146 | ################ | ||
| 147 | # UNLOCK_TABLE # | ||
| 148 | ################ | ||
| 149 | |||
| 150 | sub unlock_table { $_[0]->dbh->do("UNLOCK TABLES") ;} | ||
| 151 | |||
| 152 | ####### | ||
| 153 | # END # | ||
| 154 | ####### | ||
| 155 | |||
| 156 | 1; | ||
| 157 |