summaryrefslogtreecommitdiff
path: root/scripts/gddb.pm
blob: 88f5bea65222296404a724f9b779467ac3ab82d2 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
##################################################
#
# GiantDisc mp3 Jukebok
# 
# © 2000, Rolf Brugger
#
##################################################


###
### GiantDisc: Common database routines (mainly db modification)
###

package gddb;

use strict;

### Init constants


#BEGIN{
#}

############################################################
###                    TOOL ROUTINES                     ###
############################################################

############################################################

sub sql_escape_squotes{
  ### escapes single quotes of the string passed as argument
  #
  # Parameter: string to be quoted 
  # Returns:   quoted string

  # Usually you would use $dbh->quote instead, except if you don't want
  # to add single quotes around the string
  
  my $sqlstring = $_[0];
  $sqlstring =~ s/'/\\'/g;
  return $sqlstring;
}


############################################################
###
sub field_where_clause{ # returns a string with a SQL-where clause
			# for a text field like artist or title. If
			# keyword is empty, an empty string is returned
			#
			# - Multiple keywords are space separated
			# - Field begins are matched using the * wildcard
			
  # Examples: 
  #		field="artist" keyword="abc"
  #		-> "AND artist LIKE 
  #
  #		field="artist" keyword="abc xyz"
  #		-> "AND artist LIKE "%abc%" AND artist LIKE "%xyz%" "
  #
  #		field="artist" keyword="abc*"   (everything after * is ignored)
  #		-> "AND artist LIKE "abc%" "
  #			

  my ($fieldname,$keywords) = @_;
  $keywords = sql_escape_squotes($keywords);
  my $cmd = "";
  
  if ($keywords ne ""){
    if($keywords =~ m/\*/ ){
    	  ### wildcard expression
      my @words = split (/\*/, $keywords );
      $cmd.=" AND $fieldname LIKE '$words[0]%' ";
    }
    else{ ### non wildcard expression
      my @words = split (/ /, $keywords );
      my $current;
      while($current = shift(@words)){
        $cmd.=" AND $fieldname LIKE '%$current%' ";
      }
    }
  }
  return $cmd;
}

############################################################
###
my $get_all=0;
my $get_tracks=1;
my $get_streams=2;

sub attrib_where_clause{ # returns a string with a SQL-where clause
			# for genre, year, language, type and rating
  # field names are prefixed by 'tracks.' that the clauses can also be 
  # used in JOIN queries.
  
  my ($get_what, $genre1,$genre2,$yearfrom,$yearto,$lang,$type,$rating) = @_;

  my $tmpcmd;
  my $cmd=" ";

  ### Genre
  $tmpcmd="0 ";		# false OR ...
  if ($genre1 ne ""){ 
    $tmpcmd.="OR tracks.genre1 LIKE '$genre1%' OR tracks.genre2 LIKE '$genre1%' ";};
  if ($genre2 ne ""){ 
    $tmpcmd.="OR tracks.genre1 LIKE '$genre2%' OR tracks.genre2 LIKE '$genre2%' ";};
  if (length($tmpcmd)>3){ # genre query not empty?
    $cmd .= " AND ($tmpcmd) ";
  }

  ### Year
  if(length($yearfrom)==4){ $cmd.=" AND tracks.year >= ".$yearfrom;}
  if(length($yearto)==4){   $cmd.=" AND tracks.year <= ".$yearto;}

  ### Language
  if ($lang ne ""){ $cmd.=" AND tracks.lang = '$lang' ";};

  ### type
  if ($type ne ""){ $cmd.=" AND tracks.type = '$type' ";};

  ### rating
  if ($rating ne ""){ 
    if ($rating == 0) {
      $cmd.=" AND tracks.rating = $rating ";
    }
    else{
      $cmd.=" AND tracks.rating >= $rating ";
    }
  }
  
  ### track/stream/all
  if($get_what==$get_tracks){
    $cmd.=" AND mp3file NOT LIKE 'http://%' ";
  }
  if($get_what==$get_streams){
    $cmd.=" AND mp3file LIKE 'http://%' ";
  }
  
  return $cmd;
}

############################################################
###
sub track_where_clause{ # returns the where clause without keyword "WHERE"
  my ($get_what, $artist,$title,$genre1,$genre2,$yearfrom,$yearto,
      $lang,$type,$rating,
      $ordercmd) = @_;

  my $where=" 1 ";	# true AND ...

  ### Artist
  $where .= field_where_clause("artist",$artist);
  ### Title
  $where .= field_where_clause("title",$title);
  ### genre, etc ...
  $where.= attrib_where_clause($get_what, $genre1,$genre2,$yearfrom,$yearto,$lang,$type,$rating);

  return $where;
}

############################################################

sub track_order_clause{ # returns the order clause with keyword "ORDER BY"
  my ($get_what, $artist,$title,$genre1,$genre2,$yearfrom,$yearto,$lang,$type,$rating,
      $ordercmd) = @_;
  
  my $order = "";
  
  if(length($ordercmd)>1){
    $order = "ORDER BY ";
    if   ($ordercmd =~ m/random/ ){
      $order .= "RAND() ";
    }
    elsif($ordercmd =~ m/year/ ){
      $order .= "year ";
    }
    elsif($ordercmd =~ m/recd/ ){
      $order .= "created ";
    }
    elsif($ordercmd =~ m/modd/ ){
      $order .= "modified ";
    }
    elsif($ordercmd =~ m/recmod/ ){
      $order .= "GREATEST(created,modified) ";
    }
    
    if($ordercmd =~ m/-inv/ ){
      $order .= " DESC ";
    }
  }
  
  return $order;
}

############################################################
###
sub album_where_clause{
  my ($artist,$title,$genre1,$genre2,$yearfrom,$yearto,
      $lang,$type,$rating,
      $ordercmd) = @_;

  my $where=" 1 ";	# true AND ...
  ### Album: Artist
  $where .= gddb::field_where_clause("album.artist",$artist);
  ### Album: Title
  $where .= gddb::field_where_clause("album.title",$title);
  ### Track: genre, etc ...
  $where.= gddb::attrib_where_clause(1, $genre1,$genre2,$yearfrom,$yearto,$lang,$type,$rating);

#print "ALBUM WHERE: $where\n";
  return $where;
}

############################################################

sub album_order_clause{ # returns the order clause with keyword "ORDER BY"
  my ($artist,$title,$genre1,$genre2,$yearfrom,$yearto,$lang,$type,$rating,
      $ordercmd) = @_;
  
  my $order = "";

  if(length($ordercmd)>1){
    $order = "ORDER BY ";
    if   ($ordercmd =~ m/random/ ){
      $order .= "RAND() ";
    }
    elsif($ordercmd =~ m/year/ ){
      $order .= "tracks.year ";
    }
    elsif($ordercmd =~ m/recd/ ){
      $order .= "album.modified ";
    }
    elsif($ordercmd =~ m/modd/ ){
      $order .= "album.modified ";
    }
    elsif($ordercmd =~ m/recmod/ ){
      $order .= "album.modified ";
    }
    
    if($ordercmd =~ m/-inv/ ){
      $order .= " DESC ";
    }
  }
  
  return $order;
}

############################################################
###                 DB CREATION & UPDATE                 ###
############################################################

############################################################
### Creates/updates a new track record 
  # If $id is empty, a new record is created. Otherwise, the record
  # is updated.
  # Returns the (new) id

sub insert_track_record{ 
  my ($dbh,$artist,$title,$genre1,$genre2,$year,
      $lang,$type,$rating,$length,$source,$sourceid,
      $tracknb,$mp3file,$condition,$voladjust,
      $lengthfrm,$startfrm,$bpm,$bitrate,
      $created,$id,    ### these two fields are only defined on update!
      ) = @_;

  if(length($artist)==0){$artist="-";};
  if(length($title)==0) {$title="-";};
  if(length($year)<4)   {$year="0";};
  if(length($type)==0)  {$type="NULL";};
  if(length($rating)==0){$rating="NULL";};
  if(length($length)==0){$length="0";};
  if(length($source)==0){$source="0";};
  if(length($tracknb)==0){$tracknb="0";};
  if(length($condition)==0){$condition="0";};
  if(length($voladjust)==0){$voladjust="0";};
  if(length($lengthfrm)==0){$lengthfrm="0";};
  if(length($startfrm)==0) {$startfrm="0";};
  if(length($bpm)==0)      {$bpm="0";};
  if(length($bitrate)==0)  {$bitrate="128";};
  if(length($created)==0)  {$created="CURDATE()";};


  my $sqlcmd;
  $sqlcmd = 
    	   "artist=".$dbh->quote($artist).", "	# quote adds single quotes around the string!
    	  ."title=".$dbh->quote($title).", "
    	  ."genre1='$genre1', "
    	  ."genre2='$genre2', "
    	  ."year  = $year, "
    	  ."lang  ='$lang', "
    	  ."type  = $type, "
    	  ."rating=$rating, "
    	  ."length=$length, "
    	  ."source=$source, "
    	  ."sourceid='$sourceid', "
    	  ."tracknb=$tracknb, "
    	  ."mp3file='$mp3file', "
    	  ."condition=$condition, "
    	  ."voladjust=$voladjust, "
    	  ."lengthfrm=$lengthfrm, "
    	  ."startfrm=$startfrm, "
    	  ."bpm=$bpm, "
    	  ."bitrate='$bitrate', ";

  if(length($id)==0){
    ### INSERT a new record
    $sqlcmd = "INSERT tracks SET ".$sqlcmd
    	  ."created=CURDATE() ";
  }
  else{
    ### REPLACE an existing record
    $sqlcmd = "UPDATE tracks SET ".$sqlcmd
    	  ."created='$created', "
    	  ."modified=CURDATE() "
    	  ."WHERE id=$id ";
  }

  #print("SQL: $sqlcmd \n"); 
  my $sth = $dbh->prepare($sqlcmd);
  $sth->execute;

  if(length($id)==0){ ### if new record created
    $id = $sth->{mysql_insertid};
  }  
  $sth->finish;
  return $id;
}





############################################################
###                       QUERIES                        ###
############################################################
#





#### end
1;