| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use FindBin; |
| 4 |
use lib "$FindBin::Bin/../../"; |
| 5 |
use t::scan::Util; |
| 6 |
|
| 7 |
test(<<'TEST'); # JEPRICE/Meta-Widget-Gtk-Sprite-0.01/Sprite.pm |
| 8 |
sub new |
| 9 |
{ |
| 10 |
_debug "New sprite manager created"; |
| 11 |
my $self = bless {}, ref($_[0]) || $_[0] || __PACKAGE__; |
| 12 |
$self->{sprite} = {}; |
| 13 |
$self->{croot} = $_[1]; |
| 14 |
$self->{cgroup} = {}; |
| 15 |
return $self; |
| 16 |
} |
| 17 |
|
| 18 |
=item $sprite_number = $sprites->create("/path/to/filename", 10, 20); |
| 19 |
|
| 20 |
Create will load an image file (right now, only xpm format) from disk and make a sprite out of it. The two numbers are the x and y positi |
| 21 |
on on the canvas. |
| 22 |
|
| 23 |
=cut |
| 24 |
|
| 25 |
sub create |
| 26 |
{ |
| 27 |
my ($self, $filename, $x, $y) = @_; |
| 28 |
my $img = Gtk::Gdk::ImlibImage->load_image($filename) || die "Could not load requested tile, $filename. $!"; |
| 29 |
my ( $cg, $cg_index ) = $self->_get_new_cgroup(); |
| 30 |
$cg->hide; |
| 31 |
my $imgitem = $cg->new($cg, "Gnome::CanvasImage", |
| 32 |
'image' => $img, |
| 33 |
'x' => $x, |
| 34 |
'y' => $y, |
| 35 |
width => $img->rgb_width, |
| 36 |
height => $img->rgb_height, |
| 37 |
); |
| 38 |
$cg->{x} = $x; |
| 39 |
$cg->{y} = $y; |
| 40 |
$cg->{width} = $img->rgb_width; |
| 41 |
$cg->{height} = $img->rgb_height; |
| 42 |
#$cg->{radius} = sqrt($cg->{width}**2 + $cg->{height}**2)/2; |
| 43 |
$cg->{radius} = ($cg->{width} + $cg->{height})/4; |
| 44 |
$cg->{cx} = $cg->{x} + $cg->{width}/2; |
| 45 |
$cg->{cy} = $cg->{y} + $cg->{height}/2; |
| 46 |
my $index = $self->_add_sprite($cg); |
| 47 |
$cg->{index} = $index; |
| 48 |
return $index; |
| 49 |
} |
| 50 |
|
| 51 |
=item $sprites->show( $sprite_number ); |
| 52 |
|
| 53 |
Makes the sprite appear on the canvas |
| 54 |
|
| 55 |
=cut |
| 56 |
|
| 57 |
sub show |
| 58 |
{ |
| 59 |
my ($self, $item) = @_; |
| 60 |
$self->{sprite}->{$item}->show; |
| 61 |
} |
| 62 |
|
| 63 |
=item $sprites->hide( $sprite_number ); |
| 64 |
|
| 65 |
Make the sprite picture disappear from the canvas. Note that it can still collide with other sprites. If you don't want it to hit anythi |
| 66 |
ng, move it out of the way or ignore it in your own collision handler. |
| 67 |
|
| 68 |
|
| 69 |
=cut |
| 70 |
|
| 71 |
sub hide |
| 72 |
{ |
| 73 |
my ($self, $item) = @_; |
| 74 |
$self->{sprite}->{$item}->hide; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
TEST |
| 79 |
|
| 80 |
test(<<'TEST'); |
| 81 |
b#!/usr/bin/perl |
| 82 |
#$Id: pssql.pm 4624 2011-05-26 18:10:55Z pro $ $URL: svn://svn.setun.net/search/trunk/lib/pssql.pm $ |
| 83 |
|
| 84 |
=copyright |
| 85 |
PRO-search sql library |
| 86 |
Copyright (C) 2003-2011 Oleg Alexeenkov http://pro.setun.net/search/ [email protected] |
| 87 |
|
| 88 |
This program is free software: you can redistribute it and/or modify |
| 89 |
it under the terms of the GNU General Public License as published by |
| 90 |
the Free Software Foundation, either version 3 of the License, or |
| 91 |
(at your option) any later version. |
| 92 |
|
| 93 |
This program is distributed in the hope that it will be useful, |
| 94 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 95 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 96 |
GNU General Public License for more details. |
| 97 |
|
| 98 |
You should have received a copy of the GNU General Public License |
| 99 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 100 |
=cut |
| 101 |
|
| 102 |
=c |
| 103 |
todo: |
| 104 |
|
| 105 |
pg |
| 106 |
2009/10/06-13:53:11 dev HandleError DBD::Pg::db do failed: no connection to the server |
| 107 |
DBI::db=HASH(0x1229568) 7 no connection to the server |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
2009/06/02-19:37:35 dev HandleError DBD::Pg::st execute failed: FATAL: terminating connection due to administrator command |
| 112 |
server closed the connection unexpectedly |
| 113 |
This probably means the server terminated abnormally |
| 114 |
before or while processing the request. |
| 115 |
DBI::st=HASH(0x271b688) 7 FATAL: terminating connection due to administrator command |
| 116 |
server closed the connection unexpectedly |
| 117 |
This probably means the server terminated abnormally |
| 118 |
before or while processing the request. |
| 119 |
|
| 120 |
2009/06/02-19:37:35 dev err_parse st0 ret1 wdi= di= fa= 1 er= 300 1000 fatal 57P01 |
| 121 |
2009/06/02-19:37:36 dev HandleError DBD::Pg::st execute failed: no connection to the server |
| 122 |
DBI::st=HASH(0x271b718) 7 no connection to the server |
| 123 |
|
| 124 |
2009/06/02-19:37:39 dev HandleError DBD::Pg::db do failed: no connection to the server |
| 125 |
DBI::db=HASH(0x1209d38) 7 no connection to the server |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
$work |
| 130 |
|
| 131 |
=cut |
| 132 |
|
| 133 |
#our ( %config); |
| 134 |
package #no cpan |
| 135 |
pssql; |
| 136 |
use strict; |
| 137 |
use utf8; |
| 138 |
no warnings qw(uninitialized); |
| 139 |
our $VERSION = ( split( ' ', '$Revision: 4624 $' ) )[1]; |
| 140 |
#use locale; |
| 141 |
use DBI; |
| 142 |
use Data::Dumper; #dev only |
| 143 |
$Data::Dumper::Sortkeys = $Data::Dumper::Useqq = $Data::Dumper::Indent = $Data::Dumper::Terse = 1; |
| 144 |
our ( %work, ); #%stat %static, $param, |
| 145 |
our (%config); |
| 146 |
#local *config = *main::config; |
| 147 |
#*pssql::config = *main::config; |
| 148 |
#*pssql::work = *main::work; |
| 149 |
#*pssql::stat = *main::stat; |
| 150 |
*config = *main::config; |
| 151 |
*work = *main::work; |
| 152 |
*stat = *main::stat; |
| 153 |
use lib::abs './'; |
| 154 |
use psmisc; |
| 155 |
#use psconn; |
| 156 |
#our ( %config, %work, %stat, %static, $param, ); |
| 157 |
use base 'psconn'; |
| 158 |
our $AUTOLOAD; |
| 159 |
#our $VERSION = ( split( ' ', '$Revision: 4624 $' ) )[1]; |
| 160 |
my ( $tq, $rq, $vq ); |
| 161 |
my ( $roworder, $tableorder, ); |
| 162 |
our ( %row, %default ); |
| 163 |
$config{ 'log_' . $_ } = 0 for grep { !exists $config{ 'log_' . $_ } } qw(trace dmpbef); |
| 164 |
#warn "SQL UESEEDDD" ; |
| 165 |
sub row { |
| 166 |
my $row = shift @_; |
| 167 |
return { |
| 168 |
%{ ( defined $config{'row'} ? $config{'row'}{$row} : undef ) || $row{$row} || {} }, %{ $config{'row_all'} || {} }, |
| 169 |
'order' => --$roworder, |
| 170 |
@_ |
| 171 |
}; |
| 172 |
} |
| 173 |
|
| 174 |
sub table { |
| 175 |
my $table = shift @_; |
| 176 |
return @_; |
| 177 |
#{ |
| 178 |
#%{ ( defined $config{'row'} ? $config{'row'}{$row} : undef ) || $row{$row} || {} }, %{ $config{'row_all'} || {} }, |
| 179 |
#'order' => --$tableorder, |
| 180 |
#@_ |
| 181 |
#}; |
| 182 |
} |
| 183 |
#} |
| 184 |
BEGIN { |
| 185 |
%row = ( |
| 186 |
'time' => { |
| 187 |
'type' => 'INT', |
| 188 |
'unsigned' => 1, |
| 189 |
'default' => 0, |
| 190 |
'date_time' => 1, #todo |
| 191 |
}, |
| 192 |
'uint' => { 'type' => 'INTEGER', 'unsigned' => 1, 'default' => 0, }, |
| 193 |
'uint16' => { 'type' => 'SMALLINT', 'unsigned' => 1, 'default' => 0, }, |
| 194 |
'uint64' => { 'type' => 'BIGINT', 'unsigned' => 1, 'default' => 0, }, |
| 195 |
'text' => { 'type' => 'VARCHAR', 'index' => 10, 'default' => '', }, |
| 196 |
'stem' => { |
| 197 |
'type' => 'VARCHAR', |
| 198 |
#! 'length' => 128, |
| 199 |
'fulltext' => 'stemi', |
| 200 |
'default' => '', |
| 201 |
'not null' => 1, |
| 202 |
'stem_index' => 1, |
| 203 |
}, |
| 204 |
); |
| 205 |
$row{'id'} ||= row( 'uint', 'auto_increment' => 1, 'primary' => 1 ), |
| 206 |
$row{'added'} ||= row( 'time', 'default_insert' => int( time() ), 'no_insert_update' => 1, ); |
| 207 |
$row{'year'} ||= row('uint16'); |
| 208 |
$row{'size'} ||= row('uint64'); |
| 209 |
%default = ( |
| 210 |
'sqlite' => { |
| 211 |
#'dbi' => 'SQLite2', |
| 212 |
'dbi' => 'SQLite', |
| 213 |
'params' => [qw(dbname)], |
| 214 |
'dbname' => $config{'root_path'} . 'sqlite.db', |
| 215 |
'table quote' => '"', |
| 216 |
'row quote' => '"', |
| 217 |
'value quote' => "'", |
| 218 |
'IF NOT EXISTS' => 'IF NOT EXISTS', |
| 219 |
'index_IF NOT EXISTS' => 'IF NOT EXISTS', |
| 220 |
'IF EXISTS' => 'IF EXISTS', |
| 221 |
'REPLACE' => 'REPLACE', |
| 222 |
'AUTO_INCREMENT' => 'AUTOINCREMENT', |
| 223 |
'ANALYZE' => 'ANALYZE', |
| 224 |
'err_ignore' => [qw( 1 )], |
| 225 |
'error_type' => sub { #TODO!!! |
| 226 |
my $self = shift; |
| 227 |
my ( $err, $errstr ) = @_; |
| 228 |
#$self->log('dev',"ERRDETECT($err, $errstr)"); |
| 229 |
return 'install' if $errstr =~ /no such table:|unable to open database file/i; |
| 230 |
return 'syntax' if $errstr =~ /syntax|unrecognized token/i or $errstr =~ /misuse of aggregate/; |
| 231 |
return 'retry' if $errstr =~ /database is locked/i; |
| 232 |
#return 'connection' if $errstr =~ /connect/i; |
| 233 |
return undef; |
| 234 |
}, |
| 235 |
'on_connect' => sub { |
| 236 |
my $self = shift; |
| 237 |
$self->do("PRAGMA synchronous = OFF;"); |
| 238 |
#$self->log( 'sql', 'on_connect!' ); |
| 239 |
}, |
| 240 |
'no_dbirows' => 1, |
| 241 |
}, |
| 242 |
'pgpp' => { |
| 243 |
'dbi' => 'PgPP', |
| 244 |
'user' => ( $^O =~ /^(?:(ms)?(dos|win(32|nt)?))/i ? 'postgres' : 'pgsql' ), |
| 245 |
#'port' => 5432, |
| 246 |
'IF EXISTS' => 'IF EXISTS', 'CREATE TABLE' => 'CREATE TABLE', 'OFFSET' => 'OFFSET', |
| 247 |
#'unsigned' => 0, |
| 248 |
'UNSIGNED' => '', |
| 249 |
'table quote' => '"', |
| 250 |
'row quote' => '"', |
| 251 |
'value quote' => "'", |
| 252 |
'index_name_table' => 1, |
| 253 |
'REPLACE' => 'INSERT', |
| 254 |
'EXPLAIN' => 'EXPLAIN ANALYZE', |
| 255 |
'CASCADE' => 'CASCADE', |
| 256 |
'SET NAMES' => 'SET client_encoding = ', |
| 257 |
'fulltext_config' => 'pg_catalog.simple', |
| 258 |
'params' => [qw(dbname host port path debug)], |
| 259 |
'err_ignore' => [qw( 1 7)], |
| 260 |
'error_type' => sub { |
| 261 |
my $self = shift, my ( $err, $errstr ) = @_; |
| 262 |
#$self->log('dev',"ERRDETECT($err, [$errstr])"); |
| 263 |
return 'install_db' if $errstr =~ /FATAL:\s*database ".*?" does not exist/i; |
| 264 |
return 'fatal' if $errstr =~ /fatal/i; |
| 265 |
return 'syntax' if $errstr =~ /syntax/i; |
| 266 |
return 'connection' if $errstr =~ /connect|Unknown message type: ''/i; |
| 267 |
return 'install' if $errstr =~ /ERROR:\s*(?:relation \S+ does not exist)/i; |
| 268 |
#return 'retry' if $errstr =~ /ERROR:\s*cannot drop the currently open database/i; |
| 269 |
return 'retry' if $errstr =~ /ERROR: database ".*?" is being accessed by other users/i; |
| 270 |
return 'ignore' |
| 271 |
if $errstr =~ |
| 272 |
/(?:duplicate key violates unique constraint)|(?:duplicate key value violates unique constraint)|(?:ERROR:\s*(?:database ".*?" already exists)|(?:relation ".*?" already exists)|(?:invalid byte sequence for encoding)|(?:function .*? does not exist)|(?:null value in column .*? violates not-null constraint))/i; |
| 273 |
return undef; |
| 274 |
}, |
| 275 |
'on_connect' => sub { |
| 276 |
my $self = shift; |
| 277 |
$self->set_names(); |
| 278 |
$self->do("select set_curcfg('default');") if $self->{'use_fulltext'} and $self->{'old_fulltext'}; |
| 279 |
}, |
| 280 |
'no_dbirows' => 1, |
| 281 |
'cp1251' => 'win1251', |
| 282 |
'fulltext_word_glue' => '&', |
| 283 |
}, |
| 284 |
'sphinx' => { |
| 285 |
'dbi' => 'mysql', |
| 286 |
'user' => 'root', |
| 287 |
'port' => 9306, |
| 288 |
'params' => [qw(host port )], # perldoc DBD::mysql |
| 289 |
'sphinx' => 1, |
| 290 |
'value quote' => "'", |
| 291 |
'no_dbirows' => 1, |
| 292 |
'no_column_prepend_table' => 1, |
| 293 |
'no_join' => 1, |
| 294 |
'OPTION' => 'OPTION', |
| 295 |
'option' => { 'max_query_time' => 20000, 'cutoff' => 1000 }, |
| 296 |
}, |
| 297 |
'mysql5' => { |
| 298 |
'dbi' => 'mysql', |
| 299 |
'user' => 'root', |
| 300 |
'use_drh' => 1, |
| 301 |
'mysql_enable_utf8' => 1, |
| 302 |
'varchar_max' => 65530, |
| 303 |
'unique_max' => 1000, |
| 304 |
'primary_max' => 999, |
| 305 |
'fulltext_max' => 1000, |
| 306 |
'err_connection' => [qw( 1 1040 1053 1129 1213 1226 2002 2003 2006 2013 )], |
| 307 |
'err_fatal' => [qw( 1016 1046 1251 )], # 1045, |
| 308 |
'err_syntax' => [qw( 1054 1060 1064 1065 1067 1071 1096 1103 1118 1148 1191 1364 1366 1406 1439)], #maybe all 1045..1075 |
| 309 |
'err_repair' => [qw( 126 130 144 145 1034 1062 1194 1582 )], |
| 310 |
'err_retry' => [qw( 1317 )], |
| 311 |
'err_install' => [qw( 1146 )], |
| 312 |
'err_install_db' => [qw( 1049 )], |
| 313 |
'err_ignore ' => [qw( 2 1264 )], |
| 314 |
'error_type' => sub { |
| 315 |
my $self = shift, my ( $err, $errstr ) = @_; |
| 316 |
#$self->log('dev',"MYERRDETECT($err, $errstr)"); |
| 317 |
for my $errtype (qw(connection retry syntax fatal repair install install_db)) { |
| 318 |
#$self->log('dev',"ERRDETECTED($err, $errstr) = $errtype"), |
| 319 |
return $errtype if grep { $err eq $_ } @{ $self->{ 'err_' . $errtype } }; |
| 320 |
} |
| 321 |
return undef; |
| 322 |
}, |
| 323 |
'table quote' => "`", |
| 324 |
'row quote' => "`", |
| 325 |
'value quote' => "'", |
| 326 |
#'index quote' => "`", |
| 327 |
#'unsigned' => 1, |
| 328 |
'quote_slash' => 1, |
| 329 |
'index in create table' => 1, |
| 330 |
'utf-8' => 'utf8', |
| 331 |
'koi8-r' => 'koi8r', |
| 332 |
'table options' => 'ENGINE = MYISAM DELAY_KEY_WRITE=1', |
| 333 |
'IF NOT EXISTS' => 'IF NOT EXISTS', |
| 334 |
'IF EXISTS' => 'IF EXISTS', |
| 335 |
'IGNORE' => 'IGNORE', |
| 336 |
'REPLACE' => 'REPLACE', |
| 337 |
'INSERT' => 'INSERT', |
| 338 |
'HIGH_PRIORITY' => 'HIGH_PRIORITY', |
| 339 |
'SET NAMES' => 'SET NAMES', |
| 340 |
'DEFAULT CHARACTER SET' => 'DEFAULT CHARACTER SET', |
| 341 |
'USE_FRM' => 'USE_FRM', |
| 342 |
'EXTENDED' => 'EXTENDED', |
| 343 |
'QUICK' => 'QUICK', |
| 344 |
'ON DUPLICATE KEY UPDATE' => 'ON DUPLICATE KEY UPDATE', |
| 345 |
'UNSIGNED' => 'UNSIGNED', |
| 346 |
'UNLOCK TABLES' => 'UNLOCK TABLES', |
| 347 |
'LOCK TABLES' => 'LOCK TABLES', |
| 348 |
'OPTIMIZE' => 'OPTIMIZE TABLE', |
| 349 |
'ANALYZE' => 'ANALYZE TABLE', |
| 350 |
'CHECK' => 'CHECK TABLE', |
| 351 |
'FLUSH' => 'FLUSH TABLE', |
| 352 |
'LOW_PRIORITY' => 'LOW_PRIORITY', |
| 353 |
'on_connect' => sub { |
| 354 |
my $self = shift; |
| 355 |
$self->{'db_id'} = $self->{'dbh'}->{'mysql_thread_id'}; |
| 356 |
$self->set_names() if !( $ENV{'MOD_PERL'} || $ENV{'FCGI_ROLE'} ); |
| 357 |
}, |
| 358 |
'on_user' => sub { |
| 359 |
my $self = shift; |
| 360 |
$self->set_names() if $ENV{'MOD_PERL'} || $ENV{'FCGI_ROLE'}; |
| 361 |
}, |
| 362 |
'params' => [ |
| 363 |
qw(host port database mysql_client_found_rows mysql_compression mysql_connect_timeout mysql_read_default_file mysql_read_default_group mysql_socket |
| 364 |
mysql_ssl mysql_ssl_client_key mysql_ssl_client_cert mysql_ssl_ca_file mysql_ssl_ca_path mysql_ssl_cipher |
| 365 |
mysql_local_infile mysql_embedded_options mysql_embedded_groups mysql_enable_utf8) |
| 366 |
], # perldoc DBD::mysql |
| 367 |
'insert_by' => 1000, ( !$ENV{'SERVER_PORT'} ? ( 'auto_check' => 1 ) : () ), 'unique name' => 1, # test it |
| 368 |
'match' => sub { |
| 369 |
my $self = shift; |
| 370 |
my ( $param, $param_num, $table, $search_str, $search_str_stem ) = @_; |
| 371 |
my ( $ask, $glue ); |
| 372 |
local %_; |
| 373 |
map { $_{ $self->{'table'}{$table}{$_}{'fulltext'} } = 1 } |
| 374 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} or ( $self->{'sphinx'} and $self->{'table'}{$table}{$_}{'sphinx'} ) } |
| 375 |
keys %{ $self->{'table'}{$table} }; |
| 376 |
for my $index ( keys %_ ) { |
| 377 |
if ( |
| 378 |
$_ = join( ' , ', |
| 379 |
map { "$rq$_$rq" } |
| 380 |
sort { $self->{'table'}{$table}{$b}{'order'} <=> $self->{'table'}{$table}{$a}{'order'} } |
| 381 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} eq $index } keys %{ $self->{'table'}{$table} } ) |
| 382 |
) |
| 383 |
{ |
| 384 |
my $stem = |
| 385 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} eq $index and $self->{'table'}{$table}{$_}{'stem_index'} } |
| 386 |
keys %{ $self->{'table'}{$table} }; |
| 387 |
#TODO: maybe some message for user ? |
| 388 |
$self->{'accurate'} = 1, next, |
| 389 |
if ($stem |
| 390 |
and length $search_str_stem |
| 391 |
and $self->{'auto_accurate_on_slow'} |
| 392 |
and $search_str_stem =~ /\b\w{$self->{'auto_accurate_on_slow'}}\b/ ); |
| 393 |
my $double = |
| 394 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} and $self->{'table'}{$table}{$_}{'stem'} } |
| 395 |
keys %{ $self->{'table'}{$table} }; |
| 396 |
next if $double and ( $self->{'accurate'} xor !$stem ); |
| 397 |
my $match; |
| 398 |
if ( $self->{'sphinx'} ) { $match = ' MATCH (' . $self->squotes( $stem ? $search_str_stem : $search_str ) . ')' } |
| 399 |
else { |
| 400 |
$match = ' MATCH (' . $_ . ')' . ' AGAINST (' . $self->squotes( $stem ? $search_str_stem : $search_str ) . ( |
| 401 |
( !$self->{'no_boolean'} and $param->{ 'adv_query' . $param_num } eq 'on' ) |
| 402 |
? 'IN BOOLEAN MODE' |
| 403 |
#: ( $self->{'allow_query_expansion'} ? 'WITH QUERY EXPANSION' : '' ) |
| 404 |
: $self->{'fulltext_extra'} |
| 405 |
) . ') '; |
| 406 |
} |
| 407 |
$ask .= " $glue " . $match; |
| 408 |
$work{'what_relevance'}{$table} ||= $match . " AS $rq" . "relev$rq" |
| 409 |
if $self->{'select_relevance'} |
| 410 |
or $self->{'table_param'}{$table}{'select_relevance'}; |
| 411 |
} |
| 412 |
$glue = $self->{'fulltext_glue'}; |
| 413 |
} |
| 414 |
return $ask; |
| 415 |
}, |
| 416 |
}, |
| 417 |
); |
| 418 |
} |
| 419 |
|
| 420 |
sub new { |
| 421 |
my $self = bless( {}, shift ); |
| 422 |
$self->init(@_); |
| 423 |
$self->psconn::init(@_); |
| 424 |
return $self; |
| 425 |
} |
| 426 |
|
| 427 |
sub cmd { |
| 428 |
my $self = shift; |
| 429 |
my $cmd = shift; |
| 430 |
$self->log( 'trace', "pssql::$cmd [$self->{'dbh'}]", @_ ) if $cmd ne 'log'; |
| 431 |
$self->{'handler_bef'}{$cmd}->( $self, \@_ ) if $self->{'handler_bef'}{$cmd}; |
| 432 |
my @ret = |
| 433 |
ref( $self->{$cmd} ) eq 'CODE' |
| 434 |
? ( wantarray ? ( $self->{$cmd}->( $self, @_ ) ) : scalar $self->{$cmd}->( $self, @_ ) ) |
| 435 |
: ( exists $self->{$cmd} ? ( ( defined( $_[0] ) ? ( $self->{$cmd} = $_[0] ) : ( $self->{$cmd} ) ) ) : |
| 436 |
(!$self->{'dbh'} ? () : $self->{'dbh'}->can($cmd) ? $self->{'dbh'}->$cmd(@_) : exists $self->{'dbh'}{$cmd} ? |
| 437 |
( ( defined( $_[0] ) ? ( $self->{'dbh'}->{$cmd} = $_[0] ) : ( $self->{'dbh'}->{$cmd} ) ) ) : undef) ); |
| 438 |
$self->{'handler'}{$cmd}->( $self, \@_, \@ret ) if $self->{'handler'}{$cmd}; |
| 439 |
return wantarray ? @ret : $ret[0]; |
| 440 |
} |
| 441 |
|
| 442 |
sub AUTOLOAD { |
| 443 |
my $self = shift; |
| 444 |
my $type = ref($self) or return; |
| 445 |
my $name = $AUTOLOAD; |
| 446 |
$name =~ s/.*://; # strip fully-qualified portion |
| 447 |
#$self->log('dev', 'autoload', $name, $AUTOLOAD, @_); |
| 448 |
return $self->cmd( $name, @_ ); |
| 449 |
} |
| 450 |
|
| 451 |
sub _disconnect { |
| 452 |
my $self = shift; |
| 453 |
$self->log( 'trace', 'pssql::_diconnect', "dbh=$self->{'dbh'}" ); |
| 454 |
$self->flush_insert() unless $self->{'in_disconnect'}; |
| 455 |
$self->{'in_disconnect'} = 1; |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
sub _dropconnect { |
| 460 |
my $self = shift; |
| 461 |
$self->log( 'trace', 'pssql::_dropconnect' ); |
| 462 |
$self->{'in_disconnect'} = 1; |
| 463 |
$self->{'sth'}->finish() if $self->{'sth'}; |
| 464 |
$self->{'dbh'}->disconnect(), $self->{'dbh'} = undef if $self->{'dbh'} and keys %{ $self->{'dbh'} }; |
| 465 |
delete $self->{'in_disconnect'}; |
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
sub _check { |
| 470 |
my $self = shift; |
| 471 |
return 1 if !$self->{'dbh'} or !$self->{'connected'}; #or !keys %{$self->{'dbh'}}; |
| 472 |
return !$self->{'dbh'}->ping(); |
| 473 |
} |
| 474 |
|
| 475 |
sub init { |
| 476 |
my $self = shift; |
| 477 |
#warn Dumper $self, \@_; |
| 478 |
local %_ = ( |
| 479 |
'log' => sub (@) { |
| 480 |
shift; |
| 481 |
psmisc::printlog(@_); |
| 482 |
}, |
| 483 |
'driver' => 'mysql5', |
| 484 |
'host' => ( $^O eq 'cygwin' ? '127.0.0.1' : 'localhost' ), |
| 485 |
'database' => 'pssqldef', |
| 486 |
#'connect_tries' => 100, |
| 487 |
'error_sleep' => ( $ENV{'SERVER_PORT'} ? 1 : 3600 ), |
| 488 |
'error_tries' => ( $ENV{'SERVER_PORT'} ? 1 : 1000 ), |
| 489 |
'error_chain_tries' => ( $ENV{'SERVER_PORT'} ? 1 : 100 ), |
| 490 |
#($ENV{'SERVER_PORT'} ? ('connect_tries'=>1) : ()), |
| 491 |
#'reconnect_tries' => 10, #look old |
| 492 |
'connect_tries' => ( $ENV{'SERVER_PORT'} ? 1 : 0 ), |
| 493 |
'connect_chain_tries' => 0, |
| 494 |
'connect_auto' => 0, |
| 495 |
'connect_params' => { |
| 496 |
'RaiseError' => 0, |
| 497 |
'AutoCommit' => 1, |
| 498 |
'PrintError' => 0, |
| 499 |
'PrintWarn' => 0, |
| 500 |
'HandleError' => sub { |
| 501 |
$self->log( 'dev', 'HandleError', @_, $DBI::err, $DBI::errstr ); |
| 502 |
#$self->{'err'} = "$DBI::err, $DBI::errstr"; |
| 503 |
#psmisc::caller_trace(15) |
| 504 |
}, |
| 505 |
}, |
| 506 |
#'connect_check' => 1, #check connection on every keep() |
| 507 |
( $ENV{'SERVER_PORT'} ? () : ( 'auto_repair' => 10 ) ), # or number 10-30 |
| 508 |
'auto_repair_selected' => 0, # repair all tables |
| 509 |
'auto_install' => 1, 'auto_install_db' => 1, 'err_retry_unknown' => 0, |
| 510 |
#'reconnect_sleep' => 3600, #maximum sleep on connect error |
| 511 |
'codepage' => 'utf-8', |
| 512 |
#'cp_in' => 'utf-8', |
| 513 |
'index_postfix' => '_i', 'limit_max' => 1000, 'limit_default' => 100, |
| 514 |
#'limit' => 100, |
| 515 |
'page_min' => 1, 'page_default' => 1, |
| 516 |
#'varchar_max' => 255, |
| 517 |
'varchar_max' => 65535, |
| 518 |
'row_max' => 65535, |
| 519 |
'primary_max' => 65535, |
| 520 |
'fulltext_max' => 65535, |
| 521 |
'AUTO_INCREMENT' => 'AUTO_INCREMENT', |
| 522 |
'EXPLAIN' => 'EXPLAIN', |
| 523 |
'statable' => { 'queries' => 1, 'connect_tried' => 1, 'connects' => 1, 'inserts' => 1 }, |
| 524 |
'statable_time' => { 'queries_time' => 1, 'queries_avg' => 1, }, |
| 525 |
'param_trans_int' => { 'on_page' => 'limit', 'show_from' => 'limit_offset', 'page' => 'page', 'accurate' => 'accurate' }, |
| 526 |
#'param_trans' => { 'codepage'=>'cp_out' ,}, |
| 527 |
'connect_cached' => 1, |
| 528 |
'char_type' => 'VARCHAR', |
| 529 |
'true' => 1, |
| 530 |
'fulltext_glue' => 'OR', |
| 531 |
'retry_vars' => [qw(auto_repair connect_tries connect_chain_tries error_sleep error_tries auto_check)], |
| 532 |
'err' => 0, |
| 533 |
'insert_cached_time' => 60, |
| 534 |
'auto_repairs_max' => 2, |
| 535 |
@_, |
| 536 |
); |
| 537 |
@{$self}{ keys %_ } = values %_; |
| 538 |
#$self->{$_} //= $_{$_} for keys %_; |
| 539 |
#%_ = @_; |
| 540 |
#$self->{$_} = $_{$_} for keys %_; |
| 541 |
#$self->log( 'dev', 'initdb', "$self->{'database'},$self->{'dbname'};"); |
| 542 |
$self->{'database'} = $self->{'dbname'} if $self->{'dbname'}; |
| 543 |
$self->{'dbname'} ||= $self->{'database'}; |
| 544 |
$self->calc(); |
| 545 |
$self->functions(); |
| 546 |
( $tq, $rq, $vq ) = $self->quotes(); |
| 547 |
DBI->trace( $self->{'trace_level'}, $self->{'trace'} ) if $self->{'trace_level'} and $self->{'trace'}; |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
sub calc { |
| 552 |
my $self = shift; |
| 553 |
$self->{'default'} ||= \%default; |
| 554 |
$self->{'default'}{'pgpp'}{'match'} = sub { |
| 555 |
my $self = shift; |
| 556 |
return undef unless $self->{'use_fulltext'}; |
| 557 |
my ( $param, $param_num, $table, $search_str, $search_str_stem ) = @_; |
| 558 |
my ( $ask, $glue ); |
| 559 |
s/(?:^\s+)|(?:\s+$)//, s/\s+/$self->{'fulltext_word_glue'}/g for ( $search_str, $search_str_stem ); |
| 560 |
local %_; |
| 561 |
map { $_{ $self->{'table'}{$table}{$_}{'fulltext'} } = 1 } |
| 562 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} } keys %{ $self->{'table'}{$table} }; |
| 563 |
for my $index ( keys %_ ) { |
| 564 |
my $stem = |
| 565 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} eq $index and $self->{'table'}{$table}{$_}{'stem_index'} } |
| 566 |
keys %{ $self->{'table'}{$table} }; |
| 567 |
my $double = |
| 568 |
grep { $self->{'table'}{$table}{$_}{'fulltext'} and $self->{'table'}{$table}{$_}{'stem'} } |
| 569 |
keys %{ $self->{'table'}{$table} }; |
| 570 |
next if $double and ( $self->{'accurate'} xor !$stem ); |
| 571 |
$ask .= " $glue $index @@ to_tsquery( ${vq}$self->{'fulltext_config'}${vq}, " |
| 572 |
. $self->squotes( $stem ? $search_str_stem : $search_str ) . ")"; |
| 573 |
$glue ||= $self->{'fulltext_glue'}; |
| 574 |
} |
| 575 |
return $ask; |
| 576 |
} |
| 577 |
if $self->{'use_fulltext'}; |
| 578 |
%{ $self->{'default'}{'mysql6'} } = %{ $self->{'default'}{'mysql5'} }; |
| 579 |
%{ $self->{'default'}{'mysql4'} } = %{ $self->{'default'}{'mysql5'} }; |
| 580 |
$self->{'default'}{'mysql4'}{'SET NAMES'} = $self->{'default'}{'mysql4'}{'DEFAULT CHARACTER SET'} = |
| 581 |
$self->{'default'}{'mysql4'}{'ON DUPLICATE KEY UPDATE'} = ''; |
| 582 |
$self->{'default'}{'mysql4'}{'varchar_max'} = 255; |
| 583 |
%{ $self->{'default'}{'mysql3'} } = %{ $self->{'default'}{'mysql4'} }; |
| 584 |
$self->{'default'}{'mysql3'}{'table options'} = ''; |
| 585 |
$self->{'default'}{'mysql3'}{'USE_FRM'} = ''; |
| 586 |
$self->{'default'}{'mysql3'}{'no_boolean'} = 1; |
| 587 |
#%{ $self->{'default'}{'sqlite2'} } = %{ $self->{'default'}{'sqlite'} }; |
| 588 |
#$self->{'default'}{'sqlite2'}{'IF NOT EXISTS'} = $self->{'default'}{'sqlite2'}{'IF EXISTS'} = ''; |
| 589 |
$self->{'default'}{'pgpp'}{'fulltext_config'} = 'default' if $self->{'old_fulltext'}; |
| 590 |
%{ $self->{'default'}{'pg'} } = %{ $self->{'default'}{'pgpp'} }; |
| 591 |
$self->{'default'}{'pg'}{'dbi'} = 'Pg'; |
| 592 |
$self->{'default'}{'pg'}{'params'} = [qw(host port options tty dbname user password)]; |
| 593 |
%{ $self->{'default'}{'mysqlpp'} } = %{ $self->{'default'}{'mysql5'} }; |
| 594 |
$self->{'default'}{'mysqlpp'}{'dbi'} = 'mysqlPP'; |
| 595 |
$self->{'default'}{'sphinx'}{'match'} = $self->{'default'}{'mysql5'}{'match'}; |
| 596 |
$self->{'driver'} ||= 'mysql5'; |
| 597 |
$self->{'driver'} = 'mysql5' if $self->{'driver'} eq 'mysql'; |
| 598 |
#print "U0:", $self->{user}; |
| 599 |
#print "D0:", $self->{dbi}; |
| 600 |
$self->{$_} //= $self->{'default'}{ $self->{'driver'} }{$_} for keys %{ $self->{'default'}{ $self->{'driver'} } }; |
| 601 |
#print "U1:", $self->{user}; |
| 602 |
#print "D1:", $self->{dbi}; |
| 603 |
#$self->log( 'dev', "calc dbi[$self->{'dbi'} ||= $self->{'driver'}]"); |
| 604 |
$self->{'dbi'} ||= $self->{'driver'}, $self->{'dbi'} =~ s/\d+$//i unless $self->{'dbi'}; |
| 605 |
$self->{'codepage'} = psmisc::cp_normalize( $self->{'codepage'} ); |
| 606 |
local $_ = $self->{ $self->{'codepage'} } || $self->{'codepage'}; |
| 607 |
$self->{'cp'} = $_; |
| 608 |
$self->{'cp_set_names'} ||= $_; |
| 609 |
#$self->{'cp_int'} ||= 'cp1251'; # internal |
| 610 |
$self->{'cp_int'} ||= 'utf-8'; # internal |
| 611 |
$self->cp_client( $self->{'codepage'} ); |
| 612 |
} |
| 613 |
|
| 614 |
sub _connect { |
| 615 |
my $self = shift; |
| 616 |
|
| 617 |
=c |
| 618 |
$self->log( |
| 619 |
'dev', 'conn', |
| 620 |
"dbi:$self->{'dbi'}:" |
| 621 |
#"dbi:$self->{'default'}{ $self->{'driver'} }{'dbi'}:database=$self->{'base'};" |
| 622 |
#map {"$_:$self->{$_}"} qw(dbi database) |
| 623 |
. join( |
| 624 |
';', |
| 625 |
map( { $_ . '=' . $self->{$_} } |
| 626 |
grep { defined( $self->{$_} ) } @{ $self->{'params'} } ) |
| 627 |
), |
| 628 |
$self->{'user'}, |
| 629 |
$self->{'pass'}, |
| 630 |
#\%{ $self->{'connect_params'} } |
| 631 |
$self->{'connect_params'} |
| 632 |
); |
| 633 |
=cut |
| 634 |
|
| 635 |
local @_ = ( |
| 636 |
"dbi:$self->{'dbi'}:" |
| 637 |
. join( ';', map( { $_ . '=' . $self->{$_} } grep { defined( $self->{$_} ) } @{ $self->{'params'} } ) ), |
| 638 |
$self->{'user'}, $self->{'pass'}, $self->{'connect_params'} |
| 639 |
); |
| 640 |
#$self->log('dmp', "connect_cached = ",$self->{'connect_cached'}, Dumper(\@_)); |
| 641 |
$self->{'dbh'} = ( $self->{'connect_cached'} ? DBI->connect_cached(@_) : DBI->connect(@_) ); |
| 642 |
local $_ = $self->err_parse( \'Connection', $DBI::err, $DBI::errstr ); |
| 643 |
return $_; |
| 644 |
} |
| 645 |
TEST |
| 646 |
|
| 647 |
done_testing; |