Advertisement
NatedogServer

import dbstr_us

Apr 24th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.79 KB | None | 0 0
  1. #! /usr/bin/perl
  2.  
  3. #use strict;
  4. #use warnings;
  5. use DBI;
  6. use Getopt::Std;
  7.  
  8. #get config info from eqemu_config.xml to connect to the db & point to dbstr_us.txt
  9. getopts('c:s:t:dh');
  10. my $conf = "eqemu_config.xml"; #default
  11. my $spellf = "dbstr_us.txt"; #default
  12. my $table = "dbstr_us"; #default
  13.  
  14. if ($opt_h) { #help (-h)
  15.     printf "\nUsage: import_dbstr_us.pl [-c path] [-s path] [-t table] [-d]\n";
  16.     printf "  -c path   path/to/eqemu_config.xml. defaults to $conf\n";
  17.     printf "  -s path   path/to/dbstr_us.txt. defaults to $spellf\n";
  18.     printf "  -t table  table to load the spells into. defaults to $table\n";
  19.     printf "  -d        erase all spells from the database first\n\n";
  20.     exit;
  21. }
  22. if ($opt_c) {$conf = $opt_c;} #use config file from -c, if defined
  23. if ($opt_s) {$spellf = $opt_s;} #use spells file from -s, if defined
  24. if ($opt_t) {$table = $opt_t;} #use db table -t, if defined
  25.  
  26. my $db = "enlightened_dark";
  27. my $user = "userhere";
  28. my $pass = "passwordhere";
  29. my $host = "localhost";
  30. open(F, "<$conf") or die "Unable to open config: $conf\n";
  31. my $indb = 0;
  32. while(<F>) {
  33.     s/\r//g;
  34.     if(/<database>/i) {
  35.         $indb = 1;
  36.     }
  37.     next unless($indb == 1);
  38.     if(/<\/database>/i) {
  39.         $indb = 0;
  40.         last;
  41.     }
  42.     if(/<host>(.*)<\/host>/i) {
  43.         $host = $1;
  44.     } elsif(/<username>(.*)<\/username>/i) {
  45.         $user = $1;
  46.     } elsif(/<password>(.*)<\/password>/i) {
  47.         $pass = $1;
  48.     } elsif(/<db>(.*)<\/db>/i) {
  49.         $db = $1;
  50.     }
  51. }
  52. if(!$db || !$user || !$pass || !$host) {
  53.     die "Invalid database info, missing one of: host, user, password, database\n";
  54. }
  55.  
  56. #connect to the db before we waste time loading the spells file
  57. my $source1 = "DBI:mysql:database=information_schema;host=$host";
  58. my $source2 = "DBI:mysql:database=$db;host=$host";
  59.  
  60. #pull the column names from the db so we don't have to maintain a list. should also help if there are more columns in the db than the spells file
  61. my $dbh = DBI->connect($source1, $user, $pass) || die "Could not create db handle\n";
  62. my $sth = $dbh->prepare("SELECT COLUMN_NAME FROM COLUMNS WHERE TABLE_SCHEMA='$db' AND TABLE_NAME='$table' ORDER BY ORDINAL_POSITION ASC");
  63. $sth->execute(); #run the query on the db
  64. #create an array of the column names. i have a feeling there might be a more direct way to do it, but oh well
  65. while (my $val = $sth->fetch()) {
  66.     push(@col_names, @$val[0]); # $column[#]
  67. }
  68.  
  69. #switch to the emu db
  70. $dbh = DBI->connect($source2, $user, $pass) || die "Could not create db handle\n";
  71.  
  72. if ($opt_d) { #drop all data in the spells table
  73.     print "Are you sure you want to erase all spells from the '$table' table?\n";
  74.     print "y/N ? ";
  75.     my $confirm = <STDIN>;
  76.     if ($confirm=~/^y/i) { #as long as it begins with a Y, that's the important part
  77.         print "Deleting spells from the '$table' table... ";
  78.         $dbh->do("DELETE from $table");
  79.         print "Done.\n\n";
  80.     } else {
  81.         print "\nSkipping erasing spells from the '$table' table\n\n";
  82.     }
  83. }
  84.  
  85. #open spell file
  86. open(SPELLS, "<$spellf") or die "Unable to open spells: $spellf\n";
  87. #parse through spells
  88. my($numspells, $loadedspells, $highid); #define some variables, cause we're cool like that
  89. while(<SPELLS>) {
  90.     ++$numspells;
  91.  
  92.     chomp(); #get rid of those pesky new lines
  93.     s/'/\\'/g; #make query safe(r) by escaping quotes (\')
  94.     @s = split(/\^/); #separate by the ^ delimiter
  95.  
  96.     my @t_col_names = @col_names; #so we can safely pop any unneeded columns, but still have all of them the next time around
  97.     my $col_diff = abs(scalar(@t_col_names) - scalar(@s)); #we need to find out if the db or spells file has more columns, so we don't blow something up. this will get the amount of extra columns
  98.     #we're going to do this each loop, just in case someone screws up a line in the spell file, or starts messing with the table in the middle of the import
  99.     if (scalar(@s) < scalar(@t_col_names)) {
  100.         for (my $z = 1; $z <= $col_diff; $z++) {
  101.             pop(@t_col_names);
  102.         }
  103.     } elsif (scalar(@s) > scalar(@t_col_names)) {
  104.         for (my $z = 1; $z <= $col_diff; $z++) {
  105.             pop(@s);
  106.         }
  107.     }
  108.  
  109.     my $insert1 = join("`,`", @t_col_names);
  110.     my $insert2 = join("','", @s); #put everything in quotes & separate w/ commas for the query, plus the beginning & ending quotes. this doesn't print out to the console correctly (for me), but hopefully the query will be fine
  111.     my $insert = sprintf("REPLACE INTO %s (`%s`) VALUES ('%s')", $table, $insert1, $insert2);
  112.  
  113.     printf("Loading \"%s\" (%d)                     \r", $s[1], $s[0]); # name (id)
  114.  
  115.     my $i = $dbh->do($insert); #put these bad boys into the db
  116.     if ($i < 1) { #if the query didn't update anything into the db, or errored
  117.         printf("Error loading \"%s\" (%s)                     \n", $s[1], $s[0]);
  118.     } else {
  119.         $loadedspells++; #to compare db inserts to total spells
  120.     }
  121.    
  122.     $highid = $s[0]; #set to current id
  123. }
  124.  
  125. print "Spells Loaded: $loadedspells of $numspells                     \n";
  126. print "Highest ID: $highid\n\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement