AmberDB

 view release on metacpan or  search on metacpan

bin/amberdb_cli.pl  view on Meta::CPAN

        # Fallback if inner quotes were removed by shell: {name:Ahmet,role:admin}
        if ( $v =~ /^\{(.*)\}$/s ) {
            my $inner = $1;
            my %hash;
            while ( $inner =~ /([^,:]+)\s*:\s*([^,]+)/g ) {
                my ( $k, $item ) = ( $1, $2 );
                $k    =~ s/^\s+|\s+$//g;
                $k    =~ s/^['"]//;
                $k    =~ s/['"]$//;
                $item =~ s/^\s+|\s+$//g;
                $item =~ s/^['"]//;
                $item =~ s/['"]$//;
                $item = 0 + $item if $item =~ /^-?\d+$/;
                $hash{$k} = $item;
            }
            return \%hash if keys %hash;
        }
    }
    return 1 if $v =~ /^(?:true|yes)$/i;
    return 0 if $v =~ /^(?:false|no)$/i;
    return 0 + $v if $v =~ /^-?\d+$/;
    return $v;
}

sub set_nested_key {
    my ( $target, $path, $val ) = @_;
    my $curr = $target;
    for my $i ( 0 .. $#$path - 1 ) {
        my $p = $path->[$i];
        $curr->{$p} = {} unless ref $curr->{$p} eq 'HASH';
        $curr = $curr->{$p};
    }
    $curr->{ $path->[-1] } = $val;
}

sub format_bytes {
    my ($bytes) = @_;
    return "0 B" unless defined $bytes && $bytes > 0;
    if ( $bytes < 1024 ) {
        return "$bytes B";
    }
    elsif ( $bytes < 1024 * 1024 ) {
        return sprintf( "%.1f KB", $bytes / 1024 );
    }
    elsif ( $bytes < 1024 * 1024 * 1024 ) {
        return sprintf( "%.1f MB", $bytes / ( 1024 * 1024 ) );
    }
    else {
        return sprintf( "%.2f GB", $bytes / ( 1024 * 1024 * 1024 ) );
    }
}

# ============================================================================
# OUTPUT FORMATTING
# ============================================================================

sub format_cell {
    my ($val) = @_;
    return '' unless defined $val;
    if ( ref $val ) {
        my $json = eval { JSON::PP->new->utf8(0)->canonical(1)->encode($val) };
        if ( defined $json ) {
            $json =~ s/\r?\n/ /g;
            return $json;
        }
    }
    elsif ( $val =~ /^\{([^{}]*)\}$/s && $val !~ /"/ ) {
        # Unquoted hash string from shell: {name:Ahmet,role:admin}
        my $inner = $1;
        my %hash;
        while ( $inner =~ /([^,:]+)\s*:\s*([^,]+)/g ) {
            my ( $k, $item ) = ( $1, $2 );
            $k    =~ s/^\s+|\s+$//g;
            $k    =~ s/^['"]//;
            $k    =~ s/['"]$//;
            $item =~ s/^\s+|\s+$//g;
            $item =~ s/^['"]//;
            $item =~ s/['"]$//;
            $hash{$k} = ( $item =~ /^-?\d+$/ ? 0 + $item : $item );
        }
        if ( keys %hash ) {
            my $json = eval { JSON::PP->new->utf8(0)->canonical(1)->encode(\%hash) };
            if ( defined $json ) {
                $json =~ s/\r?\n/ /g;
                return $json;
            }
        }
    }
    return "$val";
}

sub format_tsv_cell {
    my ($val) = @_;
    my $cell = format_cell($val);
    $cell =~ s/\t/ /g;
    $cell =~ s/\r?\n/ /g;
    return $cell;
}

sub render_table_box {
    my ( $headers, $rows ) = @_;
    return unless @$headers;

    # 1. Calculate max widths
    my @widths = map { length($_) } @$headers;
    for my $row (@$rows) {
        for my $i ( 0 .. $#$headers ) {
            my $cell = format_cell( $row->[$i] );
            $widths[$i] = length($cell) if length($cell) > $widths[$i];
        }
    }

    # 2. Border lines
    my $sep_line = "+" . join( "+", map { "-" x ( $_ + 2 ) } @widths ) . "+";

    # 3. Print header
    print $sep_line, "\n";
    print "|";
    for my $i ( 0 .. $#$headers ) {
        printf( " %-${widths[$i]}s |", $headers->[$i] );
    }
    print "\n";
    print $sep_line, "\n";

    # 4. Print rows
    for my $row (@$rows) {
        print "|";
        for my $i ( 0 .. $#$headers ) {
            my $cell = format_cell( $row->[$i] );
            printf( " %-${widths[$i]}s |", $cell );
        }
        print "\n";
    }
    print $sep_line, "\n";
}

our $current_table;

sub render_records_table {
    my ( $recs, $table ) = @_;
    return unless $recs && ref $recs eq 'ARRAY' && @$recs;

bin/amberdb_cli.pl  view on Meta::CPAN

    }
    my $table = delete $method_args{table} // shift @pos_args;
    die "[AMBERDB_ERROR] 'table' parameter required for drop\n" unless defined $table && length $table;

    unless ($opt_force) {
        if ( -t STDIN ) {
            print "\n[UYARI] '$table' tablosu ve ilişkili tüm indeksler kalıcı olarak silinecek!\n";
            print "Onaylamak için tablo adını ('$table') yazın: ";
            my $confirm = <STDIN>;
            chomp($confirm) if defined $confirm;
            if ( !defined $confirm || $confirm ne $table ) {
                print "[AMBERDB] Tablo silme iÅŸlemi iptal edildi.\n";
                exit 0;
            }
        }
        else {
            die "[AMBERDB_ERROR] Table drop requires 'force=1' or '--force' flag for safety.\n";
        }
    }

    my $ok = $tools->del_table($table);
    output_result( { status => ( $ok ? 'ok' : 'error' ), action => 'drop', table => $table }, $opt_format );
    exit 0;
}

# ============================================================================
# GENERIC DYNAMIC DISPATCH FALLBACK ($adb->$action or $tools->$action)
# ============================================================================

if ( $adb->can($action) ) {
    my $res = eval { $adb->$action( @pos_args, %method_args ) };
    if ($@) {
        die "[AMBERDB_ERROR] Execution of \$adb->$action failed: $@\n";
    }
    output_result( $res, $opt_format );
    exit 0;
}

if ( $tools->can($action) ) {
    my $res = eval { $tools->$action( @pos_args, %method_args ) };
    if ($@) {
        die "[AMBERDB_ERROR] Execution of \$tools->$action failed: $@\n";
    }
    output_result( $res, $opt_format );
    exit 0;
}

die "[AMBERDB_ERROR] Unknown action or method '$action'. Use 'help' to view available commands.\n";

END {
    return unless $opt_time;
    return if $opt_help;
    return if defined $? && $? != 0;
    my $elapsed = eval { tv_interval($t0) } // 0;
    my $time_str = sprintf( "%.4fs (%.2f ms)", $elapsed, $elapsed * 1000 );
    print "[Time: $time_str]\n";
}

__END__

=encoding utf8

=head1 NAME

amberdb_cli.pl - High-performance Command-Line Console & Embedded Management Utility for AmberDB

=head1 SYNOPSIS

  # 1. Overview Dashboard (lists all tables, records, size, engine status)
  perl bin/amberdb_cli.pl
  perl bin/amberdb_cli.pl format=json

  # 2. Token-Based Session Lifecycle (Connect, Configure & Disconnect)
  perl bin/amberdb_cli.pl connect mydatabase cfg-language=tr
  perl bin/amberdb_cli.pl token=K2A78T02 cfg-no_write=1
  perl bin/amberdb_cli.pl token=K2A78T02 disconnect

  # 3. Dynamic In-Memory Schema Customization (table_attr)
  perl bin/amberdb_cli.pl token=K2A78T02 action=table_attr table=products keep_deleted=1 search_block=[2,3,6]

  # 4. CRUD & Query Operations
  perl bin/amberdb_cli.pl token=K2A78T02 action=read_id table=users id=10
  perl bin/amberdb_cli.pl token=K2A78T02 action=read_all table=products offset=0 limit=20 dir=asc
  perl bin/amberdb_cli.pl token=K2A78T02 action=read_list table=users ids=1,2,5,10
  perl bin/amberdb_cli.pl token=K2A78T02 action=search_table table=products query="laptop" limit=10
  perl bin/amberdb_cli.pl token=K2A78T02 action=insert_id table=users id=0 data='{"name":"Ahmet","status":1}'
  perl bin/amberdb_cli.pl token=K2A78T02 action=update_id table=users id=10 data='{"status":2}'
  perl bin/amberdb_cli.pl token=K2A78T02 action=delete_id table=users id=10

  # 5. Maintenance, Indexing & Disaster Recovery
  perl bin/amberdb_cli.pl token=K2A78T02 action=reindex table=products
  perl bin/amberdb_cli.pl token=K2A78T02 action=check table=products
  perl bin/amberdb_cli.pl token=K2A78T02 action=vacuum table=products
  perl bin/amberdb_cli.pl token=K2A78T02 action=export table=products file=products.csv
  perl bin/amberdb_cli.pl token=K2A78T02 action=import table=products file=products.csv
  perl bin/amberdb_cli.pl token=K2A78T02 action=drop table=temp_products force=1

=head1 DESCRIPTION

C<amberdb_cli.pl> is a direct file-based command-line console and administrative utility for C<AmberDB>. It bypasses network daemon overhead, operating directly upon native database files (C<.db>, C<.inx>, C<.fld>, C<.src>, C<.fac>) at microsecond em...

Key capabilities include:

=over 4

=item * B<Interactive & Non-Interactive Dashboard:> Running with no arguments renders an overview of all database tables, record counts, file sizes, and storage directories.

=item * B<Token-Based Session Persistence:> State, paths, language codes, safety flags, and runtime schema mutations (C<table_attr>) persist across terminal invocations via short session tokens (stored under C<.amberdb_sessions/>).

=item * B<Full CRUD Suite:> Supports positional or key-value record reading, pagination (C<offset>/C<limit>), full-text phonetic search, and bulk operations.

=item * B<Maintenance & Utilities:> Native disaster recovery archiving (C<dump>/C<restore>), CSV export/import, binary index rebuilding (C<reindex>), database compacting (C<vacuum>), and schema inspection.

=item * B<Dynamic Method Reflection:> Any method supported by C<AmberDB> or C<AmberDB::Tools> can be dispatched dynamically via C<action=E<lt>methodE<gt>>.

=back

=head1 SESSIONS & ARGUMENT CONVENTIONS

=head2 Argument Formats



( run in 1.222 second using v1.01-cache-2.11-cpan-234fa220139 )