From fe57f6df3e3c1672bc9524e0021dffa1cb2e63ca Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Sun, 13 Feb 2011 21:25:29 -0700 Subject: [PATCH] Added support for MySQL. This has the side effect of making database:format a required config value. --- bin/auto | 50 ++++++++++++++++++++++++++++++++++++++++++++------ doc/CHANGELOG | 2 ++ install | 20 ++++++++++++-------- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/bin/auto b/bin/auto index 1fcae51..3addcd7 100755 --- a/bin/auto +++ b/bin/auto @@ -15,7 +15,6 @@ use Sys::Hostname; use IO::Socket; use IO::Select; use DBI; -use DBD::SQLite; use Class::Unload; use FindBin qw($Bin); our $Bin = $Bin; ## no critic qw(NamingConventions::Capitalization Variables::ProhibitPackageVars) @@ -144,7 +143,7 @@ if (conf_get('die')) { } # Check for required configuration values. -my @REQCVALS = qw(locale expire_logs server fantasy_pf ratelimit); +my @REQCVALS = qw(locale expire_logs server fantasy_pf ratelimit database:format); foreach my $REQCVAL (@REQCVALS) { if (!conf_get($REQCVAL)) { my $err = 2; @@ -168,11 +167,50 @@ println ' Success'; API::Log::expire_logs(); # Connect to database. -if (!-e "$Bin/../etc/auto.db") { - system "touch $Bin/../etc/auto.db"; - system "chmod a+x $Bin/../etc/auto.db"; +our $DB; +given (lc((conf_get('database:format'))[0][0])) { + when ('sqlite') { + # SQLite. + if ($ENFEAT !~ /sqlite/) { err(2, 'Auto not built with SQLite support. Aborting.', 1); } + if (!conf_get('database:filename')) { err(2, 'Missing required configuration value database:filename. Aborting.', 1); } + + # Import DBD::SQLite. + require DBD::SQLite; + + if (!-e "$Bin/../etc/".(conf_get('database:filename'))[0][0]) { + # Create if it's missing. + system "touch $Bin/../etc/".(conf_get('database:filename'))[0][0]; + system "chmod a+x $Bin/../etc/".(conf_get('database:filename'))[0][0]; + } + # Connect to database. + $DB = DBI->connect("dbi:SQLite:dbname=$Bin/../etc/".(conf_get('database:filename'))[0][0], q{}, q{}); + } + when ('mysql') { + # MySQL. + if ($ENFEAT !~ /mysql/) { err(2, 'Auto not built with SQLite support. Aborting.', 1); } + my @reqcval = qw(database:host database:name database:username database:password); + foreach (@reqcval) { if (!conf_get($_)) { err(2, "Missing required configuration value $_. Aborting.", 1); } } + undef @reqcval; + + # Import DBD::mysql. + require DBD::mysql; + + # Connect to database. + if (conf_get('database:port')) { + # If we were given a port, connect with it. + $DB = DBI->connect('DBI:mysql:database='.(conf_get('database:name'))[0][0].';host='.(conf_get('database:host'))[0][0].';port='.(conf_get('database:port'))[0][0], + (conf_get('database:username'))[0][0], (conf_get('database:password'))[0][0]); + } + else { + # If not, connect without specifying a port. + $DB = DBI->connect('DBI:mysql:database='.(conf_get('database:name'))[0][0].';host='.(conf_get('database:host'))[0][0], + (conf_get('database:username'))[0][0], (conf_get('database:password'))[0][0]); + } + } + # Unknown database format. + default { err(2, 'Unknown database format \''.lc((conf_get('database:format'))[0][0]).'\'. Aborting.', 1); } } -our $DB = DBI->connect("dbi:SQLite:dbname=$Bin/../etc/auto.db", q{}, q{}); + # Ratelimit timer. Core::IRC::clear_usercmd_timer(); diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 27b8ce0..8ab7c59 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,8 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev =============================================================================== + * database:format now a required configuration value. + * Added support for MySQL. ./install --with-mysql * Added a database upgrade script for upgrading the `qdb` table in 3.0.0a3 to the new format used by 3.0.0a4. diff --git a/install b/install index 41eb383..29e49aa 100755 --- a/install +++ b/install @@ -17,7 +17,7 @@ our $VERSION = 1.00; our $ERROR = 0; # Iterate through the arguments passed to us. -my $features = 'base ssl'; +my $features = 'base ssl sqlite'; if (defined $ARGV[0]) { foreach (@ARGV) { if ($_ eq '-h' or $_ eq '--help') { @@ -28,17 +28,20 @@ if (defined $ARGV[0]) { println '*** End of Help ***'; exit 1; } - elsif ($_ eq "--enable-sasl") { - $features .= " sasl"; + elsif ($_ eq '--enable-sasl') { + $features .= ' sasl'; } - elsif ($_ eq "--disable-ssl") { + elsif ($_ eq '--disable-ssl') { $features =~ s/ ssl//g; } - elsif ($_ eq "--enable-ipv6") { - $features .= " ipv6"; + elsif ($_ eq '--enable-ipv6') { + $features .= ' ipv6'; + } + elsif ($_ eq '--with-mysql') { + $features =~ s/sqlite/mysql/; } else { - println "Warning: Unknown option '".$_."'"; + println "Warning: Unknown option '$_'"; } } } @@ -97,7 +100,8 @@ println 'Checking for required CPAN modules......'; println "\0"; #modfind("Mouse"); modfind('DBI'); -modfind('DBD::SQLite'); +modfind('DBD::SQLite') if $features =~ /sqlite/; +modfind('DBD::mysql') if $features =~ /mysql/; modfind('Class::Unload'); modfind('IO::Socket::INET6') if $features =~ /ipv6/; modfind('IO::Socket::SSL') if $features =~ /ssl/;