| 1 | #!/usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # Shepherd::MythTV library |
|---|
| 4 | # |
|---|
| 5 | # This module provides some library functions for Shepherd components, |
|---|
| 6 | # relieving them of the need to duplicate functionality. |
|---|
| 7 | # |
|---|
| 8 | # To use this library, components simply need to include the line: |
|---|
| 9 | # |
|---|
| 10 | # use Shepherd::MythTV; |
|---|
| 11 | |
|---|
| 12 | package Shepherd::MythTV; |
|---|
| 13 | |
|---|
| 14 | use DBI; |
|---|
| 15 | |
|---|
| 16 | my $version = '0.1'; |
|---|
| 17 | |
|---|
| 18 | my $db; |
|---|
| 19 | |
|---|
| 20 | sub find_database_settings_file |
|---|
| 21 | { |
|---|
| 22 | my $cfgfile = shift; |
|---|
| 23 | |
|---|
| 24 | $cfgfile = &standard_mysql_locations unless ($cfgfile); |
|---|
| 25 | |
|---|
| 26 | foreach my $f (split(/:/,$cfgfile)) |
|---|
| 27 | { |
|---|
| 28 | return $f if ((-f $f) && (-r $f)); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | print "\nWARNING: Could not find MythTV mysql.txt config file!\n". |
|---|
| 32 | "Looked in: $cfgfile\n"; |
|---|
| 33 | return undef; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub standard_mysql_locations |
|---|
| 37 | { |
|---|
| 38 | return "/usr/local/share/mythtv/mysql.txt". |
|---|
| 39 | ":/usr/share/mythtv/mysql.txt". |
|---|
| 40 | ":$ENV{HOME}/.mythtv/mysql.txt". |
|---|
| 41 | ":/home/mythtv/.mythtv/mysql.txt". |
|---|
| 42 | ":/root/.mythtv/mysql.txt". |
|---|
| 43 | ":/etc/mythtv/mysql.txt"; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | # Find MythTV database settings |
|---|
| 47 | # |
|---|
| 48 | sub setup |
|---|
| 49 | { |
|---|
| 50 | my $cfgfile = shift; |
|---|
| 51 | |
|---|
| 52 | $cfgfile = &find_database_settings_file unless ($cfgfile); |
|---|
| 53 | return if (!defined $cfgfile); |
|---|
| 54 | |
|---|
| 55 | unless (open(F,"<$cfgfile")) |
|---|
| 56 | { |
|---|
| 57 | print "ERROR: Couldn't read $cfgfile: $!\n"; |
|---|
| 58 | return undef; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | print "Reading MythTV DB settings from: $cfgfile\n"; |
|---|
| 62 | |
|---|
| 63 | while (<F>) |
|---|
| 64 | { |
|---|
| 65 | chomp; |
|---|
| 66 | $db->{$1} = $2 if ($_ =~ /^(DB.*)=(.*)/); |
|---|
| 67 | } |
|---|
| 68 | close(F); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | # |
|---|
| 72 | # Send an SQL query to the MythTV DB. Will try standard locations to |
|---|
| 73 | # find the MythTV mysql.txt file; if you want to specify a non-standard |
|---|
| 74 | # location, first call Shepherd::MythTV::setup($file_location), where |
|---|
| 75 | # $file_location is a colon-separated string of (potential) filenames. |
|---|
| 76 | # |
|---|
| 77 | # Note that data is returned in array form. |
|---|
| 78 | # |
|---|
| 79 | sub query |
|---|
| 80 | { |
|---|
| 81 | my $sql = shift; |
|---|
| 82 | |
|---|
| 83 | &setup unless ($db); |
|---|
| 84 | |
|---|
| 85 | unless ($db->{DBName} and $db->{DBHostName} and $db->{DBUserName} and $db->{DBPassword}) |
|---|
| 86 | { |
|---|
| 87 | print "ERROR: Missing essential DB connection info.\n"; |
|---|
| 88 | return undef; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | my $dbh = DBI->connect( |
|---|
| 92 | "dbi:mysql:database=".$db->{DBName}.":host=".$db->{DBHostName}, |
|---|
| 93 | $db->{DBUserName}, $db->{DBPassword}); |
|---|
| 94 | unless ($dbh) |
|---|
| 95 | { |
|---|
| 96 | print "WARNING: Couldn't connect to database ".$opt->{dbname}.": $!\n"; |
|---|
| 97 | return undef; |
|---|
| 98 | } |
|---|
| 99 | return $dbh->selectrow_array($sql); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | 1; |
|---|