AmberDB

 view release on metacpan or  search on metacpan

lib/AmberDB.pm  view on Meta::CPAN

                $self->{_auth}->{$tableid}->{$rid} = [ $self->db_decode($val) ];
            }
        }
    }

    return 1;
}

# Writes user/action audit to .aut file. Active when log_owner is enabled.
# my $ok = $adb->auth_write($tableid, $table_path, "add|edit|del", $rid);
# ------------------------------------------------
sub auth_write {

    my ( $self, $tableid, $table_path, $action, $rid ) = @_;

    return unless $tableid;
    my $table_info = $self->table_info($tableid);
    return
      unless ( $rid
        && $action
        && $table_path
        && $table_info->{record_index}
        && $table_info->{log_owner}
        && $action =~ /^(add|edit|del)$/ );
    return unless -e "$table_path.$self->{db_ext}";

    my $file_path = "$table_path.aut";

    $self->table_write($file_path)
      or do { cluck "[DB_TIE] $tableid -> $action, Autority write error. Can't open.\n"; return; };

    my $value = $self->recs_get( $file_path, $rid );

    my @record = $self->db_decode( $value->{$rid} );
    my $user   = $self->config('user') || 'user_system';
    if ( !scalar @record ) {
        if ( $action ne "add" ) {
            @record = (
                "root", [ "root", "add", $self->{date}->{year} . "01010000" ]
            );
        }
        else {
            @record = ( $user );
        }
    }
    push @record,
      [ $user, $action, $self->{date}->{minute_id} ];
    $self->recs_put( [ $file_path, $tableid ], [ $rid, @record ] );
    $self->table_close($file_path);

    return 1;
}

# Cache and buffer methods have been moved to AmberDB::Cache.
# Transaction methods have been moved to AmberDB::Transact.

1;

__END__

=encoding utf8

=head1 NAME

AmberDB - High-performance embedded NoSQL database engine for Perl

=head1 SYNOPSIS

  use AmberDB;
  my $adb = AmberDB->new(
      cfg  => { language => "gb" },
      path => { dbase_dir => "./dbstore" }
  );

  my @record = ( 0, "John Doe", "New York", 1980, 'john@example.com' );

  # Insert record
  $adb->insert_id("table_id", @record);

  # Update record (update_id / modify_id)
  $adb->update_id("table_id", @record_updated);

  # Delete record
  $adb->delete_id("table_id", $record_id);

  # Read record by ID
  my @record = $adb->read_id("table_id", $record_id);

  # Read all records
  my @records = $adb->read_all("table_id");

  # Read list of records by IDs
  my @records = $adb->read_list("table_id", \@id_list);

  # Search for the string "New York" in field 2 using the match function.
  my @records = $adb->field_fetch("table_id", 2, "New York");
  # or if the New York ID is 142
  my @records = $adb->field_fetch("table_id", 2, 142);


  # Full-text string search
  my @records = $adb->search_table("table_id", "search string");

  # If `read_all`, `field_fetch`, and `search_table` take the `$limit` parameter, they will not read all records; they will read a specific range based on `offset` and `limit`, and return the number of records at the beginning.
  my ($count, @records) = $adb->read_all("table_id", $offset, $limit);
  my ($count, @records) = $adb->field_fetch("table_id", $field_no, $match_value, $offset, $limit);
  my ($count, @records) = $adb->search_table("table_id", "search string", $offset, $limit);

  # Direct low-level table access
  $adb->table_write($file_path);
  my $records = $adb->recs_get($file_path, @rec_ids);
  my $ok      = $adb->recs_put($file_path, @records);
  $adb->table_close($file_path);

  # Transaction example (Checkout / Stock operation)
  my $res = $adb->transact_start();
  my $order_id = $adb->insert_id("order", 0, $user_id, $item_id, $qty);
  if ( !$order_id ) {
      $adb->transact_error();
  }
  my $stock_id = $adb->modify_id("stock", $item_id, $user_id, $item_id, $new_qty);



( run in 0.643 second using v1.01-cache-2.11-cpan-364913b4093 )