Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 527617f2

Von Moritz Bunkus vor fast 18 Jahren hinzugefügt

  • ID 527617f281c24f7164b44b2ba0e389463f0571c8
  • Vorgänger dddecda1
  • Nachfolger dbaa4763

Keine Form-Variablen direkt in SQL-Queries verwenden.

Unterschiede anzeigen:

SL/PE.pm
47 47
  # connect to database
48 48
  my $dbh = $form->dbconnect($myconfig);
49 49

  
50
  my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
50
  my ($where, @values);
51 51

  
52
  my $query = qq|SELECT p.id, p.projectnumber, p.description, p.active
53
                 FROM project p
54
		 WHERE 1 = 1|;
55

  
56
  if ($form->{projectnumber}) {
57
    my $projectnumber = $form->like(lc $form->{projectnumber});
58
    $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
59
  }
60
  if ($form->{projectdescription}) {
61
    my $description = $form->like(lc $form->{projectdescription});
62
    $query .= " AND lower(description) LIKE '$description'";
52
  foreach my $column (qw(projectnumber description)) {
53
    if ($form->{$column}) {
54
      $where .= qq|AND $column ILIKE ? |;
55
      push(@values, '%' . $form->{$column} . '%');
56
    }
63 57
  }
58

  
64 59
  if ($form->{status} eq 'orphaned') {
65
    $query .= " AND id NOT IN (SELECT p.id
66
                               FROM project p, acc_trans a
67
			       WHERE p.id = a.project_id)
68
                AND id NOT IN (SELECT p.id
69
		               FROM project p, invoice i
70
			       WHERE p.id = i.project_id)
71
		AND id NOT IN (SELECT p.id
72
		               FROM project p, orderitems o
73
			       WHERE p.id = o.project_id)";
60
    my %col_prefix = ("ar" => "global", "ap" => "global", "oe" => "global");
61
    my $first = 1;
62

  
63
    $where .= qq|AND id NOT IN (|;
64
    foreach my $table (qw(acc_trans invoice orderitems rmaitems ar ap oe)) {
65
      $where .= "UNION " unless ($first);
66
      $first = 0;
67
      $where .=
68
        qq|SELECT DISTINCT $col_prefix{$table}project_id FROM $table | .
69
        qq|WHERE NOT $col_prefix{$table}project_id ISNULL |;
70
    }
71
    $where .= qq|) |;
74 72
  }
73

  
75 74
  if ($form->{active} eq "active") {
76
    $query .= " AND p.active";
75
    $where .= qq|AND active |;
77 76
  } elsif ($form->{active} eq "inactive") {
78
    $query .= " AND NOT p.active";
77
    $where .= qq|AND NOT active |;
79 78
  }
80 79

  
81
  $query .= qq|
82
		 ORDER BY $sortorder|;
80
  substr($where, 0, 4) = "WHERE " if ($where);
83 81

  
84
  $sth = $dbh->prepare($query);
85
  $sth->execute || $form->dberror($query);
82
  my $sortorder = $form->{sort} ? $form->{sort} : "projectnumber";
83
  $sortorder =~ s/[^a-z_]//g;
84
  my $query =
85
    qq|SELECT id, projectnumber, description, active | .
86
    qq|FROM project | .
87
    $where .
88
    qq|ORDER BY $sortorder|;
86 89

  
87
  my $i = 0;
88
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
89
    push @{ $form->{project_list} }, $ref;
90
    $i++;
91
  }
90
  $main::lxdebug->message(1, $query);
92 91

  
93
  $sth->finish;
92
  $form->{project_list} =
93
    selectall_hashref_query($form, $dbh, $query, @values);
94 94
  $dbh->disconnect;
95 95

  
96 96
  $main::lxdebug->leave_sub();
97 97

  
98
  return $i;
98
  return scalar(@{ $form->{project_list} });
99 99
}
100 100

  
101 101
sub get_project {
......
106 106
  # connect to database
107 107
  my $dbh = $form->dbconnect($myconfig);
108 108

  
109
  my $query = qq|SELECT p.*
110
                 FROM project p
111
	         WHERE p.id = $form->{id}|;
109
  my $query =
110
    qq|SELECT * FROM project | .
111
    qq|WHERE id = ?|;
112
	my @values = ($form->{id});
112 113
  my $sth = $dbh->prepare($query);
113
  $sth->execute || $form->dberror($query);
114
  $sth->execute(@values) || $form->dberror($query);
114 115

  
115 116
  my $ref = $sth->fetchrow_hashref(NAME_lc);
116 117

  
......
119 120
  $sth->finish;
120 121

  
121 122
  # check if it is orphaned
122
  $query = qq|SELECT count(*)
123
              FROM acc_trans a
124
	      WHERE a.project_id = $form->{id}|;
125
  $sth = $dbh->prepare($query);
126
  $sth->execute || $form->dberror($query);
123
  my %col_prefix = ("ar" => "global", "ap" => "global", "oe" => "global");
124
  @values = ();
125
  $query = qq|SELECT |;
126
  my $first = 1;
127
  foreach my $table (qw(acc_trans invoice orderitems rmaitems ar ap oe)) {
128
    $query .= " + " unless ($first);
129
    $first = 0;
130
    $query .=
131
      qq|(SELECT COUNT(*) FROM $table | .
132
      qq| WHERE $col_prefix{$table}project_id = ?) |;
133
    push(@values, $form->{id});
134
  }
127 135

  
128
  ($form->{orphaned}) = $sth->fetchrow_array;
136
  ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
129 137
  $form->{orphaned} = !$form->{orphaned};
130 138

  
131
  $sth->finish;
132

  
133 139
  $dbh->disconnect;
134 140

  
135 141
  $main::lxdebug->leave_sub();
......
149 155
    $query =
150 156
      qq|UPDATE project SET projectnumber = ?, description = ?, active = ? | .
151 157
      qq|WHERE id = ?|;
152
    push(@values, $form->{active} ? 't' : 'f', $form->{id});
158
    push(@values, ($form->{active} ? 't' : 'f'), $form->{id});
153 159
  } else {
154 160
    $query =
155 161
      qq|INSERT INTO project (projectnumber, description, active) | .
......
167 173

  
168 174
  my ($self, $myconfig, $form) = @_;
169 175

  
170
  my $var;
171

  
172 176
  # connect to database
173 177
  my $dbh = $form->dbconnect($myconfig);
174 178

  
175
  my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
176

  
177
  my $query = qq|SELECT g.*
178
                 FROM partsgroup g|;
179

  
180
  my $where = "1 = 1";
179
  my ($where, @values);
181 180

  
182 181
  if ($form->{partsgroup}) {
183
    $var = $form->like(lc $form->{partsgroup});
184
    $where .= " AND lower(g.partsgroup) LIKE '$var'";
182
    $where .= qq| AND partsgroup ILIKE ?|;
183
    push(@values, '%' . $form->{partsgroup} . '%');
185 184
  }
186
  $query .= qq|
187
               WHERE $where
188
	       ORDER BY $sortorder|;
189 185

  
190 186
  if ($form->{status} eq 'orphaned') {
191
    $query = qq|SELECT g.*
192
                FROM partsgroup g
193
                LEFT JOIN parts p ON (p.partsgroup_id = g.id)
194
		WHERE $where
195
                EXCEPT
196
                SELECT g.*
197
	        FROM partsgroup g
198
	        JOIN parts p ON (p.partsgroup_id = g.id)
199
	        WHERE $where
200
		ORDER BY $sortorder|;
187
    $where .=
188
      qq| AND id NOT IN | .
189
      qq|  (SELECT DISTINCT partsgroup_id FROM parts | .
190
      qq|   WHERE NOT partsgroup_id ISNULL) |;
201 191
  }
202 192

  
203
  $sth = $dbh->prepare($query);
204
  $sth->execute || $form->dberror($query);
193
  substr($where, 0, 4) = "WHERE " if ($where);
205 194

  
206
  my $i = 0;
207
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
208
    push @{ $form->{item_list} }, $ref;
209
    $i++;
210
  }
195
  my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
196
  $sortorder =~ s/[^a-z_]//g;
197

  
198
  my $query =
199
    qq|SELECT id, partsgroup FROM partsgroup | .
200
    $where .
201
    qq|ORDER BY $sortorder|;
202

  
203
  $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
211 204

  
212
  $sth->finish;
213 205
  $dbh->disconnect;
214 206

  
215 207
  $main::lxdebug->leave_sub();
216 208

  
217
  return $i;
209
  return scalar(@{ $form->{item_list} });
218 210
}
219 211

  
220 212
sub save_partsgroup {
......
225 217
  # connect to database
226 218
  my $dbh = $form->dbconnect($myconfig);
227 219

  
228
  map { $form->{$_} =~ s/\'/\'\'/g } qw(partsgroup);
229 220
  $form->{discount} /= 100;
230 221

  
222
  my @values = ($form->{partsgroup});
223

  
231 224
  if ($form->{id}) {
232
    $query = qq|UPDATE partsgroup SET
233
                partsgroup = '$form->{partsgroup}'
234
		WHERE id = $form->{id}|;
225
    $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
226
		push(@values, $form->{id});
235 227
  } else {
236
    $query = qq|INSERT INTO partsgroup
237
                (partsgroup)
238
                VALUES ('$form->{partsgroup}')|;
228
    $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
239 229
  }
240
  $dbh->do($query) || $form->dberror($query);
230
  do_query($form, $dbh, $query, @values);
241 231

  
242 232
  $dbh->disconnect;
243 233

  
......
252 242
  # connect to database
253 243
  my $dbh = $form->dbconnect($myconfig);
254 244

  
255
  my $query = qq|SELECT p.*
256
                 FROM partsgroup p
257
	         WHERE p.id = $form->{id}|;
258
  my $sth = $dbh->prepare($query);
259
  $sth->execute || $form->dberror($query);
260

  
245
  my $query =
246
    qq|SELECT pg.*, | .
247
    qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
248
    qq|FROM partsgroup pg | .
249
    qq|WHERE pg.id = ?|;
250
  my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
251
                                  $form->{id});
261 252
  my $ref = $sth->fetchrow_hashref(NAME_lc);
262 253

  
263
  map { $form->{$_} = $ref->{$_} } keys %$ref;
264

  
265
  $sth->finish;
266

  
267
  # check if it is orphaned
268
  $query = qq|SELECT count(*)
269
              FROM parts p
270
	      WHERE p.partsgroup_id = $form->{id}|;
271
  $sth = $dbh->prepare($query);
272
  $sth->execute || $form->dberror($query);
273

  
274
  ($form->{orphaned}) = $sth->fetchrow_array;
275
  $form->{orphaned} = !$form->{orphaned};
276

  
254
  map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
277 255
  $sth->finish;
278 256

  
279 257
  $dbh->disconnect;
......
289 267
  # connect to database
290 268
  my $dbh = $form->dbconnect($myconfig);
291 269

  
292
  $query = qq|DELETE FROM $form->{type}
293
	      WHERE id = $form->{id}|;
294
  $dbh->do($query) || $form->dberror($query);
270
  my $table =
271
    $form->{type} eq "project" ? "project" :
272
    $form->{type} eq "pricegroup" ? "pricegroup" :
273
    "partsgroup";
274

  
275
  $query = qq|DELETE FROM $table WHERE id = ?|;
276
  do_query($form, $dbh, $query, $form->{id});
295 277

  
296 278
  $dbh->disconnect;
297 279

  
......
306 288

  
307 289
  my ($self, $myconfig, $form) = @_;
308 290

  
309
  my $var;
310

  
311 291
  # connect to database
312 292
  my $dbh = $form->dbconnect($myconfig);
313 293

  
314
  my $sortorder = ($form->{sort}) ? $form->{sort} : "pricegroup";
315

  
316
  my $query = qq|SELECT g.id, g.pricegroup
317
                 FROM pricegroup g|;
318

  
319
  my $where = "1 = 1";
294
  my ($where, @values);
320 295

  
321 296
  if ($form->{pricegroup}) {
322
    $var = $form->like(lc $form->{pricegroup});
323
    $where .= " AND lower(g.pricegroup) LIKE '$var'";
297
    $where .= qq| AND pricegroup ILIKE ?|;
298
    push(@values, '%' . $form->{pricegroup} . '%');
324 299
  }
325
  $query .= qq|
326
               WHERE $where
327
	       ORDER BY $sortorder|;
328 300

  
329 301
  if ($form->{status} eq 'orphaned') {
330
    $query = qq|SELECT pg.*
331
                FROM pricegroup pg
332
                LEFT JOIN prices p ON (p.pricegroup_id = pg.id)
333
		WHERE $where
334
                EXCEPT
335
                SELECT pg.*
336
	        FROM pricegroup pg
337
	        JOIN prices p ON (p.pricegroup_id = pg.id)
338
	        WHERE $where
339
		ORDER BY $sortorder|;
302
    my $first = 1;
303

  
304
    $where .= qq| AND id NOT IN (|;
305
    foreach my $table (qw(invoice orderitems prices rmaitems)) {
306
      $where .= "UNION " unless ($first);
307
      $first = 0;
308
      $where .=
309
        qq|SELECT DISTINCT pricegroup_id FROM $table | .
310
        qq|WHERE NOT pricegroup_id ISNULL |;
311
    }
312
    $where .= qq|) |;
340 313
  }
341 314

  
342
  $sth = $dbh->prepare($query);
343
  $sth->execute || $form->dberror($query);
315
  substr($where, 0, 4) = "WHERE " if ($where);
344 316

  
345
  my $i = 0;
346
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
347
    push @{ $form->{item_list} }, $ref;
348
    $i++;
349
  }
317
  my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
318
  $sortorder =~ s/[^a-z_]//g;
319

  
320
  my $query =
321
    qq|SELECT id, pricegroup FROM pricegroup | .
322
    $where .
323
    qq|ORDER BY $sortorder|;
324

  
325
  $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
350 326

  
351
  $sth->finish;
352 327
  $dbh->disconnect;
353 328

  
354 329
  $main::lxdebug->leave_sub();
355 330

  
356
  return $i;
331
  return scalar(@{ $form->{item_list} });
357 332
}
333

  
358 334
########################
359 335
# save pricegruop to database
360 336
#
......
365 341

  
366 342
  # connect to database
367 343
  my $dbh = $form->dbconnect($myconfig);
368

  
369
  map { $form->{$_} =~ s/\'/\'\'/g } qw(pricegroup);
344
  my $query;
370 345

  
371 346
  $form->{discount} /= 100;
372 347

  
348
  my @values = ($form->{pricegroup});
349

  
373 350
  if ($form->{id}) {
374
    $query = qq|UPDATE pricegroup SET
375
                pricegroup = '$form->{pricegroup}'
376
		WHERE id = $form->{id}|;
351
    $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
352
		push(@values, $form->{id});
377 353
  } else {
378
    $query = qq|INSERT INTO pricegroup
379
                (pricegroup)
380
                VALUES ('$form->{pricegroup}')|;
354
    $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
381 355
  }
382
  $dbh->do($query) || $form->dberror($query);
356
  do_query($form, $dbh, $query, @values);
383 357

  
384 358
  $dbh->disconnect;
385 359

  
386 360
  $main::lxdebug->leave_sub();
387 361
}
362

  
388 363
############################
389 364
# get one pricegroup from database
390 365
#
......
396 371
  # connect to database
397 372
  my $dbh = $form->dbconnect($myconfig);
398 373

  
399
  my $query = qq|SELECT p.id, p.pricegroup
400
                 FROM pricegroup p
401
	         WHERE p.id = $form->{id}|;
402
  my $sth = $dbh->prepare($query);
403
  $sth->execute || $form->dberror($query);
404

  
374
  my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
375
  my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
405 376
  my $ref = $sth->fetchrow_hashref(NAME_lc);
406 377

  
407
  map { $form->{$_} = $ref->{$_} } keys %$ref;
378
  map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
408 379

  
409 380
  $sth->finish;
410 381

  
411
  # check if it is orphaned
412
  $query = qq|SELECT count(*)
413
              FROM prices p
414
	      WHERE p.pricegroup_id = $form->{id}|;
415
  $sth = $dbh->prepare($query);
416
  $sth->execute || $form->dberror($query);
382
  my $first = 1;
417 383

  
418
  ($form->{orphaned}) = $sth->fetchrow_array;
419
  $form->{orphaned} = !$form->{orphaned};
384
  my @values = ();
385
  $query = qq|SELECT |;
386
  foreach my $table (qw(invoice orderitems prices rmaitems)) {
387
    $query .= " + " unless ($first);
388
    $first = 0;
389
    $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
390
    push(@values, $form->{id});
391
  }
420 392

  
421
  $sth->finish;
393
  ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
394
  $form->{orphaned} = !$form->{orphaned};
422 395

  
423 396
  $dbh->disconnect;
424 397

  

Auch abrufbar als: Unified diff