Class-orMapper

 view release on metacpan or  search on metacpan

lib/Class/orMapper.pm  view on Meta::CPAN

	$sth->execute(@{$v});
	my @o;
	while(my $r = $sth->fetchrow_arrayref){
		my @tmp = map{$_?$_:''} @{$r};
		push(@o, \@tmp);
	}
	$sth->finish;
	return \@o;
}

sub select_n_hashref{
	my ($self,$s,$v) = @_;
	my $sth = $self->{dbh_r}->prepare($s);
	$sth->execute(@{$v});
	my @o;
	while(my $r = $sth->fetchrow_hashref){
		push(@o, $r);
	}
	$sth->finish;
	return \@o;
}

sub select_arrayref{
	my ($self,$p) = @_;
	my ($s,@v) = $self->select_base($p);
	my $sth = $self->{dbh_r}->prepare($s);
	$sth->execute(@v);
	my @o;
	while(my $r = $sth->fetchrow_arrayref){
		my @tmp = map{$_?$_:''} @{$r};
		push(@o, \@tmp);
	}
	$sth->finish;
	return \@o;
}

sub select_hashref{
	my ($self,$p) = @_;
	my ($s,@v) = $self->select_base($p);
	my $sth = $self->{dbh_r}->prepare($s);
	$sth->execute(@v);
	my @o;
	while(my $r = $sth->fetchrow_hashref){
		push(@o, $r);
	}
	$sth->finish;
	return \@o;
}

# insert
sub insert{
	my ($self,$p) = @_;
	my ($s,@v);
	$s = "insert into " . $p->{table} . "(" . join(",",map{push(@v,$p->{columns}->{$_});$_} keys %{$p->{columns}}) . ") values(" . join(',',map{$_ = '?';$_} values %{$p->{columns}}) . ")";
	my $sth = $self->{dbh_w}->prepare($s);
	$sth->execute(@v);
	$sth->finish;
}

# update
sub update{
	my ($self,$p) = @_;
	my ($s,@v);
	$s = "update " . $p->{table} . " set " . join(',', map{push(@v,$p->{columns}->{$_});$_ = $_ . '=?'} keys %{$p->{columns}});
	my ($w,@vv) = where($p);
	if($w){
		$w =~ s/ and //;
		$s .= ' where ' . $w;
	}
	push(@v,$_) for (@vv);
	my $sth = $self->{dbh_w}->prepare($s);
	$sth->execute(@v);
	$sth->finish;
}

# delete
sub delete{
	my ($self,$p) = @_;
	my $s = "delete from " . $p->{table};
	my ($w,@v) = where($p);
	if($w){
	$w =~ s/ and //;
	$s .= ' where ' . $w;
	}
	my $sth = $self->{dbh_w}->prepare($s);
	$sth->execute(@v);
	$sth->finish;
}

# truncate
sub truncate{
	my ($self,$p) = @_;
	my $s = "truncate table " . $p->{table};
	$self->{dbh_w}->do($s);
}

# internal use function
sub select_base{
	my ($self,$p) = @_;
	my $s = "select " . join(',',@{$p->{columns}}) . " from " . $p->{table};
	my ($w,@v) = where($p);
	if($w){
		$w =~ s/ and //;
		$s .= ' where ' . $w;
	}
	my $o;
	if($p->{order}){
		$o .= ',' . $_ . ' ' . $p->{order}->{$_} for (keys %{$p->{order}});
	}
	if($o){
		$o =~ s/^,//;
		$s .= ' order by ' . $o;
	}
	return ($s,@v);
}

sub where{
	my $p = shift;
	my ($w,@v);
	for my $ww (@{$p->{where}}){
		for my $www (keys %{$ww}){



( run in 0.597 second using v1.01-cache-2.11-cpan-13bb782fe5a )