Tripletail

 view release on metacpan or  search on metacpan

lib/Tripletail/DB.pm  view on Meta::CPAN

		if($this->{default_set}) {
			$set = $this->{default_set};
		} else {
			die __PACKAGE__."#_getDbSetName: do not omit the DB Set because no default DB Set has been specified." .
				" (デフォルトの DB Set が指定されていない場合は、DB Set の指定を省略できません)\n";
		}
	} else {
		if($this->{dbh}{$setname}) {
			$set = $setname;
		} else {
			die __PACKAGE__."#_getDbSetName: DB set [$setname] was not defined. Please check the INI file.".
			" (DB Set [$setname] が存在しません)\n";
		}
	}

	$set;
}

sub _connect {
    # クラスメソッド。TL#startCgi,TL#trapErrorのみが呼ぶ。
    my $class = shift;
    my $groups = shift;

    foreach my $group (@$groups) {
        if (!defined $group) {
            die "TL#startCgi: -DB has an undefined value. (DB指定にundefが含まれます)\n";
        }
        elsif (ref $group) {
            die "TL#startCgi: -DB has a reference. (DB指定にリファレンスが含まれます)\n";
        }

        $INSTANCES{$group} = __PACKAGE__->_new($group)->connect;
    }

	# initRequest, postRequest, term をフックする
	$TL->setHook(
		'initRequest',
		_INIT_REQUEST_HOOK_PRIORITY,
		\&__initRequest,
	);

	$TL->setHook(
		'postRequest',
		_POST_REQUEST_HOOK_PRIORITY,
		\&__postRequest,
	);

	$TL->setHook(
		'term',
		_TERM_HOOK_PRIORITY,
		\&__term,
	);

	undef;
}

sub _new {
	my $class = shift;
	my $group = shift;

	my $this = bless {} => $class;
	$this->{group}     = $group;
	$this->{namequery} = $TL->INI->get($group => 'namequery' => undef);
	$this->{autoreyry} = $TL->INI->get($group => 'autoretry' => undef);
	$this->{type}      = $TL->INI->get($group => 'type');

	$this->{bufsize} = undef; # 正の値でなければ無制限
	$this->{types_symtable} = \%Tripletail::DB::SQL_TYPES::;

	$this->{dbh} = {};    # {DBセット名 => Tripletail::DB::Dbh}
	$this->{dbname} = {}; # {DBコネクション名 => Tripletail::DB::Dbh}

	$this->{default_set} = $TL->INI->get($group => 'defaultset', undef); # デフォルトのセット名

	$this->{locked_dbh}   = undef; # Tripletail::DB::Dbh
	$this->{trans_dbh}    = undef; # Tripletail::DB::Dbh

	do {
		local $SIG{__DIE__} = 'DEFAULT';
		eval q{
			package Tripletail::DB::SQL_TYPES;
			use DBI qw(:sql_types);
		};
	};
	if($@) {
		die $@;
	}

	# ここでセット定義を読む
	foreach my $setname ($TL->INI->getKeys($group)) {
		$setname =~ m/^[a-z]+$/ and next; # 予約済

		my @db = split /\s*,\s*/, $TL->INI->get($group => $setname);
		if (!scalar(@db)) {
			# ゼロ個のDBから構成されるDBセットを作ってはならない。
			die __PACKAGE__."#new: DB Set [$setname] has no databases. (DB Set [$setname] にDBが1つもありません)\n";
		}

        my $dbname = $db[$$ % scalar(@db)];
        if (!$this->{dbname}{$dbname}) {
            my $type    = $this->{type};
            my $backend = exists $BACKEND_OF{$type}
                               ? $BACKEND_OF{$type}
                               : die "TL#startCgi: DB type [$type] is not supported.".
                                 " (DB type [$type] はサポートされていません)\n";

            eval qq{
                use $backend;
            };
            if ($@) {
                local $SIG{__DIE__} = 'DEFAULT';
                die $@;
            }

            my $class = $backend . '::Dbh';
            $this->{dbname}{$dbname} = $class->new($setname, $dbname);
        }
        $this->{dbh}{$setname} = $this->{dbname}{$dbname};
	}

	$this;



( run in 3.913 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )