Showing 1 changed files with 189 additions and 0 deletions
+189
lib/DBI/Custom.pm
... ...
@@ -409,6 +409,154 @@ sub run_tranzaction {
409 409
     }
410 410
 }
411 411
 
412
+# Get last insert id
413
+sub last_insert_id {
414
+    my $self = shift;
415
+    
416
+    # Not connected
417
+    croak("Not yet connect to database")
418
+      unless $self->connected;
419
+    
420
+    return $self->dbh->last_insert_id(@_);
421
+}
422
+
423
+# Insert
424
+sub insert {
425
+    my ($self, $table, $insert_params, $edit_query_cb) = @_;
426
+    $insert_params ||= {};
427
+    
428
+    # Insert keys
429
+    my @insert_keys = keys %$insert_params;
430
+    
431
+    # Not exists insert keys
432
+    croak("Insert key must be specified")
433
+      unless @insert_keys;
434
+    
435
+    # Templte for insert
436
+    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
437
+    
438
+    # Create query
439
+    my $query = $self->create_query($template);
440
+    
441
+    # Edit query callback must be code reference
442
+    croak("Edit query callback must be code reference")
443
+      if $edit_query_cb && !ref $edit_query_cb eq 'CODE';
444
+    
445
+    # Edit query if need
446
+    $edit_query_cb->($query) if ref $edit_query_cb eq 'CODE';
447
+    
448
+    # Execute query
449
+    my $ret_val = $self->execute($query, $insert_params);
450
+    
451
+    return $ret_val;
452
+}
453
+
454
+sub update {
455
+    my ($self, $table, $update_params,
456
+        $where_params, $edit_query_cb, $options) = @_;
457
+    
458
+    $update_params ||= {};
459
+    $where_params  ||= {};
460
+    
461
+    # Update keys
462
+    my @update_keys = keys %$where_params;
463
+    
464
+    # Not exists update kyes
465
+    croak("Update key must be specified")
466
+      unless @update_keys;
467
+    
468
+    # Where keys
469
+    my @where_keys = keys %$where_params;
470
+    
471
+    # Not exists where keys
472
+    croak("Where key must be specified")
473
+      if !@where_keys && !$options->{allow_update_all};
474
+    
475
+    # Update clause
476
+    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
477
+    
478
+    # Where clause
479
+    my $where_clause = 'where ';
480
+    foreach my $where_key (@where_keys) {
481
+        $where_clause .= "{= $where_key} && ";
482
+    }
483
+    $where_clause =~ s/ && $//;
484
+    
485
+    # Template for update
486
+    my $template = "update $table $update_clause $where_clause";
487
+    
488
+    # Create query
489
+    my $query = $self->create_query($template);
490
+    
491
+    # Edit query callback must be code reference
492
+    croak("Edit query callback must be code reference")
493
+      if $edit_query_cb && !ref $edit_query_cb eq 'CODE';
494
+    
495
+    # Edit query if need
496
+    $edit_query_cb->($query) if $edit_query_cb;
497
+    
498
+    # Rearrange parammeters
499
+    my $params = {'#update' => $update_params, %$where_params};
500
+    
501
+    # Execute query
502
+    my $ret_val = $self->execute($query, $params);
503
+    
504
+    return $ret_val;
505
+}
506
+
507
+# Update all rows
508
+sub update_all {
509
+    my ($self, $table, $update_params, $edit_query_cb) = @_;
510
+    
511
+    return $self->update($table, $update_params, {}, $edit_query_cb,
512
+                         {allow_update_all => 1});
513
+}
514
+
515
+# Delete
516
+sub delete {
517
+    my ($self, $table, $where_params, $edit_query_cb, $options) = @_;
518
+    $where_params ||= {};
519
+    
520
+    # Where keys
521
+    my @where_keys = keys %$where_params;
522
+    
523
+    # Not exists where keys
524
+    croak("Where key must be specified")
525
+      if !@where_keys && !$options->{allow_update_all};
526
+    
527
+    # Where clause
528
+    my $where_clause = 'where ';
529
+    foreach my $where_key (@where_keys) {
530
+        $where_clause .= "{= $where_key} && ";
531
+    }
532
+    $where_clause =~ s/ && $//;
533
+    
534
+    # Template for delete
535
+    my $template = "delete from $table $where_clause";
536
+    
537
+    # Create query
538
+    my $query = $self->create_query($template);
539
+    
540
+    # Edit query callback must be code reference
541
+    croak("Edit query callback must be code reference")
542
+      if $edit_query_cb && !ref $edit_query_cb eq 'CODE';
543
+    
544
+    # Edit query if need
545
+    $edit_query_cb->($query) if $edit_query_cb;
546
+    
547
+    # Execute query
548
+    my $ret_val = $self->execute($query, $where_params);
549
+    
550
+    return $ret_val;
551
+}
552
+
553
+# Delete all rows
554
+sub delete_all {
555
+    my ($self, $table, $edit_query_cb) = @_;
556
+    return $self->delete($table, {}, $edit_query_cb, {allow_delete_all => 1});
557
+}
558
+
559
+
412 560
 Object::Simple->build_class;
413 561
 
414 562
 =head1 NAME
... ...
@@ -668,6 +816,47 @@ See also L<DBI::Custom::SQL::Template>
668 816
 If tranzaction is success, commit is execute. 
669 817
 If tranzation is died, rollback is execute.
670 818
 
819
+=head2 insert
820
+
821
+    # Insert
822
+    $dbi->insert($table, $insert_values);
823
+    
824
+    # Sample
825
+    $dbi->insert('books', {title => 'Perl', author => 'Taro'});
826
+
827
+=head2 update
828
+
829
+    # Update
830
+    $dbi->update($table, $update_values, $where);
831
+    
832
+    # Sample
833
+    $dbi->update('books', {title => 'Perl', author => 'Taro'}, {id => 5});
834
+
835
+=head2 update_all
836
+
837
+    # Update all rows
838
+    $dbi->update($table, $updat_values);
839
+
840
+=head2 delete
841
+
842
+    # Delete
843
+    $dbi->delete($table, $where);
844
+    
845
+    # Sample
846
+    $dbi->delete('Books', {id => 5});
847
+
848
+=head2 delete_all
849
+
850
+    # Delete all rows
851
+    $dbi->delete_all($table);
852
+
853
+=head2 last_insert_id
854
+
855
+    # Get last insert id
856
+    $last_insert_id = $dbi->last_insert_id;
857
+    
858
+This method is same as DBI last_insert_id;
859
+
671 860
 =head1 CAUTION
672 861
 
673 862
 DBI::Custom have DIB object internal.