| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::ObjectDriver::Driver::DBD::mysql; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use warnings; |
|---|
| 11 | |
|---|
| 12 | use base qw( |
|---|
| 13 | MT::ObjectDriver::Driver::DBD::Legacy |
|---|
| 14 | Data::ObjectDriver::Driver::DBD::mysql |
|---|
| 15 | MT::ErrorHandler |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | sub dsn_from_config { |
|---|
| 19 | my $dbd = shift; |
|---|
| 20 | my $dsn = $dbd->SUPER::dsn_from_config(@_); |
|---|
| 21 | my ($cfg) = @_; |
|---|
| 22 | $dsn .= ':database=' . $cfg->Database; |
|---|
| 23 | $dsn .= ';hostname=' . $cfg->DBHost if $cfg->DBHost; |
|---|
| 24 | $dsn .= ';mysql_socket=' . $cfg->DBSocket if $cfg->DBSocket; |
|---|
| 25 | $dsn .= ';port=' . $cfg->DBPort if $cfg->DBPort; |
|---|
| 26 | return $dsn; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub sql_class { |
|---|
| 30 | require MT::ObjectDriver::SQL::mysql; |
|---|
| 31 | return 'MT::ObjectDriver::SQL::mysql'; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | sub ddl_class { |
|---|
| 35 | require MT::ObjectDriver::DDL::mysql; |
|---|
| 36 | return 'MT::ObjectDriver::DDL::mysql'; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub init_dbh { |
|---|
| 40 | my $dbd = shift; |
|---|
| 41 | my ($dbh) = @_; |
|---|
| 42 | $dbd->SUPER::init_dbh(@_); |
|---|
| 43 | $dbd->_set_names($dbh); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | sub _set_names { |
|---|
| 47 | my $dbd = shift; |
|---|
| 48 | my ($dbh) = @_; |
|---|
| 49 | return 1 if exists $dbh->{private_set_names}; |
|---|
| 50 | |
|---|
| 51 | warn "got here"; |
|---|
| 52 | my $cfg = MT->config; |
|---|
| 53 | my $set_names = $cfg->SQLSetNames; |
|---|
| 54 | $dbh->{private_set_names} = 1; |
|---|
| 55 | return 1 if (defined $set_names) && !$set_names; |
|---|
| 56 | |
|---|
| 57 | eval { |
|---|
| 58 | local $@; |
|---|
| 59 | my $sth = $dbh->prepare('show variables like "character_set_database"') |
|---|
| 60 | or die "error collecting variables from mysql: " . $dbh->errstr; |
|---|
| 61 | $sth->execute or die "error collecting variables from mysql: " . $sth->errstr; |
|---|
| 62 | my $result = $sth->fetchall_hashref('Variable_name'); |
|---|
| 63 | my $charset_db = $result->{character_set_database}{Value}; |
|---|
| 64 | if (defined($charset_db) && ($charset_db ne 'latin1')) { |
|---|
| 65 | # MySQL 4.1+ and non-latin1(database) == needs SET NAMES call. |
|---|
| 66 | my $c = lc $cfg->PublishCharset; |
|---|
| 67 | my %Charset = ( |
|---|
| 68 | 'utf-8' => 'utf8', |
|---|
| 69 | 'shift_jis' => 'sjis', |
|---|
| 70 | 'shift-jis' => 'sjis', |
|---|
| 71 | 'euc-jp' => 'ujis', |
|---|
| 72 | #'iso-8859-1' => 'latin1' |
|---|
| 73 | ); |
|---|
| 74 | $c = $Charset{$c} ? $Charset{$c} : $c; |
|---|
| 75 | $dbh->do("SET NAMES " . $c) or |
|---|
| 76 | return ($dbh->errstr); |
|---|
| 77 | if (!defined $set_names) { |
|---|
| 78 | # SQLSetNames has never been assigned; we had a successful |
|---|
| 79 | # 'SET NAMES' command, so it's safe to SET NAMES in the future. |
|---|
| 80 | $cfg->SQLSetNames(1, 1); |
|---|
| 81 | } |
|---|
| 82 | } else { |
|---|
| 83 | # 'set names' command isn't working for this verison of mysql, |
|---|
| 84 | # assign SQLSetNames to 0 to prevent further errors. |
|---|
| 85 | $cfg->SQLSetNames(0, 1); |
|---|
| 86 | return 0; |
|---|
| 87 | } |
|---|
| 88 | }; |
|---|
| 89 | 1; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | 1; |
|---|
| 93 | __END__ |
|---|
| 94 | |
|---|
| 95 | =head1 NAME |
|---|
| 96 | |
|---|
| 97 | MT::ObjectDriver::Driver::DBD::mysql |
|---|
| 98 | |
|---|
| 99 | =head1 METHODS |
|---|
| 100 | |
|---|
| 101 | TODO |
|---|
| 102 | |
|---|
| 103 | =head1 AUTHOR & COPYRIGHT |
|---|
| 104 | |
|---|
| 105 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 106 | |
|---|
| 107 | =cut |
|---|