Advertisement
Guest User

dbhandler.pm

a guest
Feb 28th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # DBhandler.pm
  4.  
  5.  
  6. package DBhandler;
  7. use DBD::Pg;
  8. use Loghandler;
  9. use strict;
  10. use Unicode::Normalize;
  11. use Encode;
  12. use utf8;
  13. use Data::Dumper;
  14.  
  15.  
  16. use String::Multibyte;
  17.  
  18.  
  19. sub new #dbname,host,login,password,port
  20. {
  21. my $class = shift;
  22. my $self =
  23. {
  24. dbname => shift,
  25. host => shift,
  26. login => shift,
  27. password => shift,
  28. port => shift,
  29. conn => ""
  30. };
  31. setupConnection($self);
  32. bless $self, $class;
  33. return $self;
  34. }
  35.  
  36. sub setupConnection
  37. {
  38. my ($self) = @_[0];
  39. my $conn = $self->{conn};
  40. my $dbname = $self->{dbname};
  41. my $host = $self->{host};
  42. my $login = $self->{login};
  43. my $pass = $self->{password};
  44. my $port = $self->{port};
  45. $conn = DBI->connect("DBI:Pg:dbname=$dbname;host=$host;port=$port", $login, $pass, {pg_utf8_strings => 1,AutoCommit => 1}); #'RaiseError' => 1,post_connect_sql => "SET CLIENT_ENCODING TO 'UTF8'"
  46.  
  47. $self->{conn} = $conn;
  48. }
  49.  
  50. sub update
  51. {
  52. my ($self) = @_[0];
  53. my $conn = $self->{conn};
  54. my $querystring = @_[1];
  55.  
  56. my $ret = $conn->do($querystring);
  57. return $ret;
  58. }
  59.  
  60. sub query
  61. {
  62.  
  63. #
  64. #All of this messed up code commented out were different efforts to work out some strange
  65. #and unusual characters coming out of the database. Some of them still throw warnings to the
  66. #console but don't seem to halt execution. Example:
  67. #"\x{2113}" does not map to iso-8859-1 at /usr/lib64/perl5/Encode.pm line 158.
  68. #Right now the output to the marc records are correct but output to the console looks wrong.
  69. #This is probably due to multibyte unicode characters not being shown for the locale of my session.
  70. #
  71. my ($self) = @_[0];
  72. my $conn = $self->{conn};
  73. my $querystring = @_[1];
  74. my @ret;
  75. # print "$querystring\n";
  76.  
  77. my $query = $conn->prepare($querystring);
  78. $query->execute();
  79. my %ar;
  80. #mb_internal_encoding("UTF-8");
  81. while (my $row = $query->fetchrow_arrayref())
  82. {
  83. my @pusher;
  84. foreach(@$row)
  85. {
  86. my $utf8 = String::Multibyte->new('UTF8');
  87. #print "Raw = $_\n";
  88. #my $teststring = "ṭṭār";
  89. #print "testing $teststring\n";
  90. #Encode::_set_utf8_on($_);
  91. #my $conv = decode_utf8($_);# Encode::decode("utf8",$_);#Encode::_set_utf8_on($_);# $utf8->substr($_,0,$utf8->length($_));#$_;#Encode::encode_utf8($_);#$utf8->substr($_,0,$utf8->length($_));#Encode::encode_utf8($_);
  92. #my $conv = $_;
  93. my $conv = $utf8->substr($_,0,$utf8->length($_));
  94. #$conv = Encode::encode_utf8($decode);
  95. #print "Enc = $conv\n";
  96. #print "conv = $conv\n";
  97.  
  98. # ------------ This if statement doesn't execute
  99. if(0)
  100. {
  101.  
  102. if(Encode::is_utf8($conv))
  103. {
  104.  
  105. }
  106. else
  107. {
  108. #print "$_\nIS NOT UTF8\n";
  109. }
  110.  
  111. my @mchars = $utf8->strsplit('', $conv);
  112. foreach(@mchars)
  113. {
  114.  
  115. my $ord = $_; #ord $_;
  116. #print "$_ = $ord\n";
  117.  
  118. if(exists($ar{$ord}))
  119. {
  120. $ar{$ord}++;
  121. }
  122. else
  123. {
  124. $ar{$ord}=1;
  125. }
  126. }
  127.  
  128. my $str = $conv;#Encode::encode_utf8($_);
  129. if(0)
  130. {
  131. # this code is borrowed from the evergreen git repository
  132. # (I added a few more unicode characters to the regex)
  133. #$str = uc $str;
  134. $str =~ s/\x{0098}.*?\x{009C}//g;
  135. $str = NFKD($str);
  136. $str =~ s/\x{00C6}/AE/g;
  137. $str =~ s/\x{00DE}/TH/g;
  138. $str =~ s/\x{0152}/OE/g;
  139. $str =~ tr/\xC3\x81\x84\xAD\xA1\xBB\x8A\x{0302}\x{0303}\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}\x{0117}][/DDOLl/d;
  140. $conv = $str;
  141. }
  142. }
  143. # ------------ END OF DISABLED CODE
  144. #print "Enc = $str\n";
  145.  
  146. push(@pusher, $conv);
  147. #print "done testing $teststring\n";
  148. }
  149. #my @pusher;
  150. #foreach(@$row)
  151. #{
  152. #my $pushChars=Encode::encode("UTF-8","");
  153. #my @chars = split("",$_);
  154. #if(!Test::utf8->is_sane_utf8(Encode::encode("UTF-8",$_)))
  155. #{
  156. #print "not an utf8 character\n";
  157. #}
  158. #foreach(@chars)
  159. #{
  160. #my $test = Test::utf8->isnt_within_ascii();
  161. #print compose(reorder($_));
  162. #(my $str = $_) =~ s/(.|\n)/sprintf("%02lx", ord $1)/eg;
  163. #
  164. #my $encoded = $_;#Encode::encode("UTF-8",$_);#decode("UTF-8",$_);#
  165. #if(Encode::is_utf8($_))
  166. #{
  167. # $encoded = Encode::encode("UTF-8",$_);
  168. #print "it's UTF-8";
  169. #}
  170. #my $ord = ord($encoded);
  171. #$pushChars.=$encoded;
  172. #my $temp = utf8::upgrade($_);
  173. #print $encoded." $ord ";
  174. #}
  175. #print "$pushChars\n";
  176. #}
  177. push(@ret,[@pusher]); #@$row @pusher
  178. @pusher=undef;
  179. }
  180. #print $querystring."\n";
  181. #while ((my $internal, my $value ) = each(%ar))
  182. #{
  183. #if($value<20)
  184. #{
  185. #my $in = ord $internal;
  186. #print "$internal = $in occured $value time(s)\n";
  187. #}
  188. #}
  189.  
  190. undef($querystring);
  191. return \@ret;
  192.  
  193. }
  194.  
  195. sub getConnectionInfo
  196. {
  197. my ($self) = @_[0];
  198. my %info = (
  199. dbname => $self->{dbname},
  200. host => $self->{host},
  201. login => $self->{login},
  202. password => $self->{password},
  203. port => $self->{port}
  204. );
  205. return \%info;
  206. }
  207. sub DESTROY
  208. {
  209. my ($self) = @_[0];
  210. my $conn = $self->{conn};
  211. $conn->disconnect();
  212. $conn = undef;
  213. }
  214. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement