item_db.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/perl
  2. $db = "item_db";
  3. $nb_columns = 22;
  4. @str_col = (1,2,19,20,21);
  5. $create_table = "#
  6. # Table structure for table `item_db`
  7. #
  8. DROP TABLE IF EXISTS `item_db`;
  9. CREATE TABLE `item_db` (
  10. `id` smallint(5) unsigned NOT NULL default '0',
  11. `name_english` varchar(50) NOT NULL default '',
  12. `name_japanese` varchar(50) NOT NULL default '',
  13. `type` tinyint(2) unsigned NOT NULL default '0',
  14. `price_buy` mediumint(10) unsigned default NULL,
  15. `price_sell` mediumint(10) unsigned default NULL,
  16. `weight` smallint(5) unsigned NOT NULL default '0',
  17. `attack` smallint(3) unsigned default NULL,
  18. `defence` tinyint(3) unsigned default NULL,
  19. `range` tinyint(2) unsigned default NULL,
  20. `slots` tinyint(2) unsigned default NULL,
  21. `equip_jobs` int(12) unsigned default NULL,
  22. `equip_upper` tinyint(8) unsigned default NULL,
  23. `equip_genders` tinyint(2) unsigned default NULL,
  24. `equip_locations` smallint(4) unsigned default NULL,
  25. `weapon_level` tinyint(2) unsigned default NULL,
  26. `equip_level` tinyint(3) unsigned default NULL,
  27. `refineable` tinyint(1) unsigned default NULL,
  28. `view` tinyint(3) unsigned default NULL,
  29. `script` text,
  30. `equip_script` text,
  31. `unequip_script` text,
  32. PRIMARY KEY (`id`)
  33. ) TYPE=MyISAM;
  34. ";
  35. printf("%s\n",$create_table);
  36. while ($ligne=<STDIN>)
  37. {
  38. if ($ligne =~ /[^\r\n]+/)
  39. {
  40. $ligne = $&;
  41. if ($ligne =~ /^\/\//)
  42. {
  43. printf("# %s\n", $ligne);
  44. } else {
  45. @champ = split (",(?![^{]*[})])",$ligne);
  46. if ($#champ != $nb_columns - 1)
  47. {
  48. printf ("# WARNING BAD LINE : %d (%s)\n", $#champ, $ligne);
  49. printf STDERR ("Warning : can't parse %s\n", $ligne);
  50. } else {
  51. printf("REPLACE INTO `%s` VALUES (", $db);
  52. for ($i=0; $i<$#champ; $i++)
  53. {
  54. printField($champ[$i],",",$i);
  55. }
  56. printField($champ[$#champ],");\n",$#champ);
  57. }
  58. }
  59. }
  60. }
  61. sub printField {
  62. my ($str, $suffix, $idCol) = @_;
  63. # Remove first { and last }
  64. if ($str =~ /{.*}/)
  65. {
  66. $str = substr($&,1,-1);
  67. }
  68. # If nothing, put NULL
  69. if ($str eq "") {
  70. printf("NULL%s", $suffix);
  71. } else {
  72. my $flag = 0;
  73. # Search if it's a string column ?
  74. foreach $col (@str_col)
  75. {
  76. if ($col == $idCol)
  77. {
  78. $flag = 1;
  79. break;
  80. }
  81. }
  82. if ($flag == 1)
  83. {
  84. # String column, so escape and add ''
  85. printf("'%s'%s", escape($str), $suffix);
  86. } else {
  87. # Not a string column
  88. printf("%s%s", $str,$suffix);
  89. }
  90. }
  91. }
  92. sub escape {
  93. my ($str) = @_;
  94. my @str_splitted = split("'", $str);
  95. my $result = "";
  96. for (my $i=0; $i<=$#str_splitted; $i++)
  97. {
  98. if ($i == 0) {
  99. $result = @str_splitted[0];
  100. } else {
  101. $result = $result."\\'".@str_splitted[$i];
  102. }
  103. }
  104. return $result
  105. }