File: SPDB.pm

package info (click to toggle)
sporum 1.8b1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,108 kB
  • ctags: 676
  • sloc: perl: 14,300; makefile: 52
file content (297 lines) | stat: -rw-r--r-- 7,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package SPDB;

use SmallPigVars qw($config);

use strict;
use DBD::mysql;
use DBI;

sub new{
  my ($class) = @_;
  my $self = {};
  $self->{'dbh'} ||= db_connect();
  $self->{'connect'}++;
  $self->{'dbcalls'} = 0;

  return bless $self, $class;
}

sub DESTROY{
    my $self = shift;
    db_disconnect($self->{'dbh'}) if $self->{'dbh'} && $self->{'connect'};
}

sub _db_handler{
  my ($self, $query) = @_;
  my $DBH = $self->{'dbh'};

  my $sth = $DBH->prepare($query) or
    die "Cant prepare: $query.";
  $sth->execute() or
    die "Cant execute: $query.";
  
  return $sth;
}

sub _db_handler_done{
    shift->finish();
}

sub _db_get_query{
  my($select, $from, $where, $other) = @_;

  my $query = "SELECT $select ";

  $from =~ s/(Cats|Boards|BoardBelongToCat|Posts|ParentAndChild|MostParents|FileAttach|AnonPosts)/$config->{'rest_tbl'}$1/g if $config->{'rest_tbl'};
  $from =~ s/(Users|BoardPrefs|Moderators|GroupMems|SubscribeBoards)/$config->{'user_tbl'}$1/g if $config->{'user_tbl'};

  $query   .= "FROM $from " if $from;
  $query   .= "WHERE $where " if $where;
  $query   .= "$other" if $other;
  return $query;
}

sub db_select_cols{
  my $self = shift;
#  $self->{'dbcalls'}++;
  my $ret = $self->db_select_cols_ref(@_);
  return $ret?@$ret:undef;
}

sub db_select_cols_ref{
  my $self = shift;
  my $sth = $self->_db_handler(_db_get_query(@_));
  return undef unless $sth->rows();
  my $ret = $sth->fetch();
  _db_handler_done($sth);
  
  return $ret;
}

sub db_select_hash{
  my $self = shift;
  my $ret = $self->db_select_hash_ref(@_);
  return $ret?@$ret:();
}

sub db_select_hash_ref{
  my $self = shift;
  my $sth = $self->_db_handler(_db_get_query(@_));
  return undef unless $sth->rows();
  my $ret = $sth->fetchrow_hashref();
  _db_handler_done($sth);
  
  return $ret;
}

sub db_select_many_new{
    my $self = shift;
    my $flag = shift;
    my $sth = $self->_db_handler(_db_get_query(@_));
    return undef unless $sth->rows();
    
    my $nums = $sth->rows;

    return ($sth, $nums);
}

sub db_handler_done{
    my ($self, $sth) = @_;
    _db_handler_done($sth) if $sth;
}

sub db_select_many{
  my $self = shift;
  my $flag = shift;
  my $sth = $self->_db_handler(_db_get_query(@_));
  return undef unless $sth->rows();

  my $nums = $sth->rows;
  my @ret;
  unless($flag){
    while(my $row = $sth->fetchrow_hashref()){
      push @ret, [%$row];
    }
  }
  else{
    while(my $row = $sth->fetch()){
      push @ret, [@$row];
    }
  }
  _db_handler_done($sth);

#  $self->{'dbcalls'}++;
  return (\@ret, $nums);
}

sub db_insert{
  my ($self, $table, $data) = @_;
  my $DBH = $self->{'dbh'};
  
  $table =~ s/(Cats|Boards|BoardBelongToCat|Posts|ParentAndChild|MostParents|FileAttach|AnonPosts)/$config->{'rest_tbl'}$1/g if $config->{'rest_tbl'};
  $table =~ s/(Users|BoardPrefs|Moderators|GroupMems|SubscribeBoards)/$config->{'user_tbl'}$1/g if $config->{'user_tbl'};

  my ($cnames, $values);
  foreach (keys(%$data)){
    $cnames .= "$_, ";
    $values .= $DBH->quote($data->{$_}).", ";
  }
  $cnames =~ s?(.*), *$?$1?g;
  $values =~ s?(.*), *$?$1?g;
  my $query = "INSERT INTO $table ($cnames) VALUES ($values)";
  $DBH->do($query) ||
    return (0, "Cant insert to sql server. Reason: $DBI::errstr");
  return (1);
}

sub db_update{
  my ($self, $table, $data, $where) = @_;
  my $DBH = $self->{'dbh'};

  $table =~ s/(Cats|Boards|BoardBelongToCat|Posts|ParentAndChild|MostParents|FileAttach|AnonPosts)/$config->{'rest_tbl'}$1/g if $config->{'rest_tbl'};
  $table =~ s/(Users|BoardPrefs|Moderators|GroupMems|SubscribeBoards)/$config->{'user_tbl'}$1/g if $config->{'user_tbl'};

  my $query   = "UPDATE $table ";
  my $pairs = "";
  foreach (keys(%$data)){
    $pairs .= " $_=$data->{$_}, ";
  }
  $pairs =~ s/(.*), $/$1/gi;
  $query  .= "SET $pairs ";
  $query  .= "WHERE $where";

  $DBH->do($query) ||
    return (0, "Cant update to sql server. $query Reason: $DBI::errstr");
  return (1);
}

sub db_delete{
  my ($self, $table, $where) = @_;
  my $DBH = $self->{'dbh'};

  $table =~ s/(Cats|Boards|BoardBelongToCat|Posts|ParentAndChild|MostParents|FileAttach|AnonPosts)/$config->{'rest_tbl'}$1/g if $config->{'rest_tbl'};
  $table =~ s/(Users|BoardPrefs|Moderators|GroupMems|SubscribeBoards)/$config->{'user_tbl'}$1/g if $config->{'user_tbl'};

  $where = "WHERE $where" if $where;

  my $query = "DELETE FROM $table $where";
  $DBH->do($query) ||
    die "Cant delete rows. query: $query Reason: $DBI::errstr";
}

sub db_do_query{
  my ($self, $query) = @_;
  my $DBH = $self->{'dbh'};
  
  $DBH->do($query) || print "Cant execute. Reason: $DBI::errstr\n";
}

sub db_drop_table{
  my ($self, $table) = @_;
  my $DBH = $self->{'dbh'};
  
  my $query = "DROP TABLE $table";
  $DBH->do($query) ||
    die "Cant drop table. Reason: $DBI::errstr";
}

sub db_connect{
  return DBI->connect("dbi:$config->{'dbdriver'}:$config->{'dbname'}:$config->{'dbhost'}:$config->{'dbport'}", 
		      $config->{'dbuser'}, $config->{'dbpass'}, {PrintError => 0});
}

sub db_disconnect{
  my($DBH) = @_;
  
  if($DBH){  
      $DBH->disconnect();
      #or  die "Cant disconnect from database. Reason: $DBI::errstr"; 
  }
}

sub db_lock{
  my ($self, @wtable, @rtable) = @_;
  my $DBH = $self->{'dbh'};
  
  my $locks;
  foreach (@wtable){ 

      s/(Cats|Boards|BoardBelongToCat|Posts|ParentAndChild|MostParents|FileAttach|AnonPosts)/$config->{'rest_tbl'}$1/g if $config->{'rest_tbl'};
      s/(Users|BoardPrefs|Moderators|GroupMems|SubscribeBoards)/$config->{'user_tbl'}$1/g if $config->{'user_tbl'};

      $locks .= "$_ WRITE, "; 

  }
  foreach (@rtable){ 
      
      s/(Cats|Boards|BoardBelongToCat|Posts|ParentAndChild|MostParents|FileAttach|AnonPosts)/$config->{'rest_tbl'}$1/g if $config->{'rest_tbl'};
      s/(Users|BoardPrefs|Moderators|GroupMems|SubscribeBoards)/$config->{'user_tbl'}$1/g if $config->{'user_tbl'};
      
      $locks .= "$_ READ, "; 

  }
  $locks =~ s/, $//;

  $DBH->do("LOCK TABLES $locks") ||
      return (0, "Cant lock tables @wtable @rtable. Reason: $DBI::errstr");
  return (1);
}

sub db_unlock{
  my ($self) = @_;
  my $DBH = $self->{'dbh'};

  $DBH->do("UNLOCK TABLES") ||
      return (0, "Cant unlock tables. Reason: $DBI::errstr");
  return (1);
}

sub db_date{
  my ($self, $format) = @_;
  my $select = "NOW()";
  $select = "date_format($select, '$format')" if $format;
  my ($time) = $self->db_select_cols($select);
  return $time;
}

sub db_count{
  my ($self, $from, $where) = @_;
  my @query = ("COUNT(*)", $from, $where);
  my $sth = $self->_db_handler(_db_get_query(@query));
  my ($ret) =  $sth->fetchrow();
  _db_handler_done($sth);
  
  return $ret;
}

sub db_sum{
  my ($self, $select, $from, $where) = @_;
  my @query = ("SUM($select)", $from, $where);
  my $sth = $self->_db_handler(_db_get_query(@query));
  my ($ret) =  $sth->fetchrow();
  _db_handler_done($sth);
  
  return $ret;
}

sub db_max{
  my ($self, $select, $from, $where) = @_;
  my @query = ("MAX($select)", $from, $where);
  my $sth = $self->_db_handler(_db_get_query(@query));
  my ($ret) =  $sth->fetchrow();
  _db_handler_done($sth);
  
  return $ret;
}

sub db_min{
  my ($self, $select, $from, $where) = @_;
  my @query = ("MIN($select)", $from, $where);
  my $sth = $self->_db_handler(_db_get_query(@query));
  my ($ret) =  $sth->fetchrow();
  _db_handler_done($sth);
  
  return $ret;
}

return 1;