| 1 | #!/bin/perl | ||
| 2 | |||
| 3 | # ----------------------------------------------------------------------------- | ||
| 4 | # | ||
| 5 | # Nes by Skriptke | ||
| 6 | # Copyright 2009 - 2010 Enrique Castañón | ||
| 7 | # Licensed under the GNU GPL. | ||
| 8 | # | ||
| 9 | # CPAN: | ||
| 10 | # http://search.cpan.org/dist/Nes/ | ||
| 11 | # | ||
| 12 | # Sample: | ||
| 13 | # http://nes.sourceforge.net/ | ||
| 14 | # | ||
| 15 | # Repository: | ||
| 16 | # http://github.com/Skriptke/nes | ||
| 17 | # | ||
| 18 | # Version 1.03 | ||
| 19 | # | ||
| 20 | # DB.pm | ||
| 21 | # | ||
| 22 | # ----------------------------------------------------------------------------- | ||
| 23 | |||
| 24 | { | ||
| 25 | package Nes::DB; | ||
| 26 | |||
| 27 | use DBI; | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $class = shift; | ||
| 31 | my ($name, $user, $pass, $drv, $host, $port) = @_; | ||
| 32 | $self = bless {}, $class; | ||
| 33 | |||
| 34 | $self->{'name'} = $name; | ||
| 35 | $self->{'user'} = $user; | ||
| 36 | $self->{'pass'} = $pass; | ||
| 37 | $self->{'drv'} = $drv; | ||
| 38 | $self->{'host'} = $host; | ||
| 39 | $self->{'port'} = $port; | ||
| 40 | $self->{'data_source'} = "DBI:$self->{'drv'}:database=$self->{'name'};host=$self->{'host'};port=$self->{'port'}"; | ||
| 41 | |||
| 42 | $self->{'dbh'} = 0; | ||
| 43 | $self->{'rows'} = 0; | ||
| 44 | $self->{'errstr'} = ''; | ||
| 45 | $self->{'error'} = ''; | ||
| 46 | $self->{'filas'} = 0; | ||
| 47 | |||
| 48 | $self->open(); | ||
| 49 | |||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub open { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | if($self->{'dbh'}) { # la base ya esta abierta, salir | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | eval { | ||
| 61 | $self->{'dbh'} = DBI->connect($self->{'data_source'},$self->{'user'},$self->{'pass'}); | ||
| 62 | $self->{'errstr'} = $DBI::errstr; | ||
| 63 | $self->{'error'} = $DBI::errstr; | ||
| 64 | }; | ||
| 65 | |||
| 66 | return $self->{'dbh'}; | ||
| 67 | |||
| 68 | } | ||
| 69 | |||
| 70 | sub close { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | if(!$self->{'dbh'}) { # la base ya esta cerrada, salir. | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | eval { | ||
| 78 | $self->{'dbh'}->disconnect; | ||
| 79 | # $self->{'errstr'} = $DBI::errstr; | ||
| 80 | $self->{'error'} .= $DBI::errstr; | ||
| 81 | }; | ||
| 82 | |||
| 83 | $self->{'dbh'} = 0; | ||
| 84 | |||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub isopen { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | return $self->{'dbh'}; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub create { | ||
| 95 | my $self = shift; | ||
| 96 | local ($tabla,$campos) = @_; | ||
| 97 | my $resultado; | ||
| 98 | |||
| 99 | if($self->{existe}) { # la tabla existe. | ||
| 100 | return $self->{'error'} = "No se puede crear la tabla, ya existe"; | ||
| 101 | } | ||
| 102 | |||
| 103 | $resultado = $self->{'dbh'}->do("create table $tabla ($campos);") || return $self->{'error'} = $DBI::errstr; | ||
| 104 | $self->{'rows'} = $self->{'dbh'}->rows; | ||
| 105 | $self->{'errstr'} = $self->{'dbh'}->errstr; | ||
| 106 | |||
| 107 | return $resultado; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub sen_select { | ||
| 111 | my $self = shift; | ||
| 112 | my ($SQL_sentencia) = @_; | ||
| 113 | my $resultado = ''; | ||
| 114 | my @encontrados = (); | ||
| 115 | |||
| 116 | $self->open(); | ||
| 117 | |||
| 118 | my $sth = ''; | ||
| 119 | eval { | ||
| 120 | $sth = $self->{'dbh'}->prepare( $SQL_sentencia ); | ||
| 121 | $self->{'filas'} = $sth->execute(); | ||
| 122 | $self->{'rows'} = $self->{'dbh'}->rows; | ||
| 123 | $self->{'errstr'} = $self->{'dbh'}->errstr; | ||
| 124 | }; | ||
| 125 | |||
| 126 | return if $DBI::errstr; | ||
| 127 | while( $resultado = $sth->fetchrow_hashref ) { | ||
| 128 | push(@encontrados, $resultado); | ||
| 129 | } | ||
| 130 | $sth->finish(); | ||
| 131 | $self->close(); | ||
| 132 | |||
| 133 | return @encontrados; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub sen_select_no_close { | ||
| 137 | my $self = shift; | ||
| 138 | my ($SQL_sentencia) = @_; | ||
| 139 | my $resultado = ''; | ||
| 140 | my @encontrados = (); | ||
| 141 | |||
| 142 | $self->open(); | ||
| 143 | |||
| 144 | my $sth = ''; | ||
| 145 | eval { | ||
| 146 | $sth = $self->{'dbh'}->prepare( $SQL_sentencia ); | ||
| 147 | $self->{'filas'} = $sth->execute(); | ||
| 148 | $self->{'rows'} = $self->{'dbh'}->rows; | ||
| 149 | $self->{'errstr'} = $self->{'dbh'}->errstr; | ||
| 150 | }; | ||
| 151 | |||
| 152 | return if $DBI::errstr; | ||
| 153 | |||
| 154 | while( $resultado = $sth->fetchrow_hashref ) { | ||
| 155 | push(@encontrados, $resultado); | ||
| 156 | } | ||
| 157 | $sth->finish(); | ||
| 158 | |||
| 159 | return @encontrados; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub sen_no_select { | ||
| 163 | my $self = shift; | ||
| 164 | my ($SQL_sentencia) = @_; | ||
| 165 | my $resultado; | ||
| 166 | |||
| 167 | $self->open(); | ||
| 168 | |||
| 169 | eval { | ||
| 170 | $resultado = $self->{'dbh'}->do( $SQL_sentencia ); | ||
| 171 | $self->{'rows'} = $self->{'dbh'}->rows; | ||
| 172 | $self->{'errstr'} = $self->{'dbh'}->errstr; | ||
| 173 | $self->{'error'} = $DBI::errstr; | ||
| 174 | }; | ||
| 175 | |||
| 176 | $self->close(); | ||
| 177 | |||
| 178 | return $resultado; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub sen_no_select_no_close { | ||
| 182 | my $self = shift; | ||
| 183 | my ($SQL_sentencia) = @_; | ||
| 184 | my $resultado; | ||
| 185 | |||
| 186 | $self->open(); | ||
| 187 | |||
| 188 | eval { | ||
| 189 | $resultado = $self->{'dbh'}->do( $SQL_sentencia ); | ||
| 190 | $self->{'rows'} = $self->{'dbh'}->rows; | ||
| 191 | $self->{'errstr'} = $self->{'dbh'}->errstr; | ||
| 192 | $self->{'error'} = $DBI::errstr; | ||
| 193 | }; | ||
| 194 | |||
| 195 | return $resultado; | ||
| 196 | } | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | 1; |