AmberDB

 view release on metacpan or  search on metacpan

bin/amberdb_cli.pl  view on Meta::CPAN

}

my $active_token;
my $session;
unless ($is_connect_cmd) {
    $active_token = resolve_active_token( $opt_token, $is_session_cmd || !@raw_tokens );
    $session      = $active_token ? load_session($active_token) : undef;

    if ( defined $opt_token && !$session ) {
        die "[AMBERDB_ERROR] Invalid or expired session token '$opt_token'.\n";
    }

    if ($session) {
        eval { $adb->connect( token => $active_token ) };
        if ( $session->{path}->{dbase_dir} && !$explicit_db ) {
            $adb->set_datadir( $session->{path}->{dbase_dir} );
        }
        $adb->path( $session->{path} ) if $session->{path};
        $adb->config( $session->{cfg} ) if $session->{cfg};
        if ( $session->{table_attrs} ) {
            for my $tbl ( keys %{ $session->{table_attrs} } ) {
                $adb->table_attr( $tbl, $session->{table_attrs}->{$tbl} );
            }
        }
    }
}

# Step 5: Process remaining tokens
for my $raw (@raw_tokens) {
    my $arg = $raw;
    $arg =~ s/^--?//;

    if ( $arg =~ /^([^=]+)=(.*)$/s ) {
        my ( $k, $v ) = ( $1, $2 );
        my $val = parse_value($v);

        if ( $k eq 'action' ) {
            $opt_action = $v;
        }
        elsif ( $k eq 'token' ) {
            $opt_token = $v;
        }
        elsif ( $k eq 'format' ) {
            $opt_format = lc($v);
        }
        elsif ( $k eq 'time' ) {
            $opt_time = $val ? 1 : 0;
        }
        elsif ( $k =~ /^(?:dry-run|dry_run)$/ ) {
            $opt_dry_run = $val ? 1 : 0;
        }
        elsif ( $k =~ /^(?:force|f)$/ ) {
            $opt_force = $val ? 1 : 0;
        }
        elsif ( $k =~ /^(?:check|c)$/i ) {
            $method_args{check} = $val ? 1 : 0;
        }
        elsif ( $k =~ /^(?:all|a)$/i ) {
            $method_args{all} = $val ? 1 : 0;
        }
        elsif ( $k =~ /^(?:no-backup|no_backup)$/i ) {
            $method_args{no_backup} = $val ? 1 : 0;
        }
        elsif ( $k eq 'cpanm' ) {
            $method_args{cpanm} = $v;
        }
        elsif ( $k eq 'manifest' ) {
            $method_args{manifest} = $v;
        }
        elsif ( $k =~ /^(?:help|h|usage)$/i ) {
            $opt_help = 1;
            if ( defined $v && $v =~ /^(?:tr|en)$/i ) {
                $opt_help_lang = lc($v);
            }
        }
        elsif ( $k =~ /^(?:help|usage)\.(tr|en)$/i ) {
            $opt_help = 1;
            $opt_help_lang = lc($1);
        }
        elsif ( $k =~ /^cfg-(.+)$/ ) {
            $adb->config( $1 => $val );
            $has_cfg_updates = 1;
        }
        elsif ( $k =~ /^path-(.+)$/ ) {
            my $pkey = $1;
            if ( $pkey eq 'dbase_dir' || $pkey eq 'db' || $pkey eq 'dbase' ) {
                my $abs = eval { abs_path($val) } // $val;
                $adb->set_datadir($abs);
                $explicit_db = 1;
            }
            else {
                $adb->path( $pkey => $val );
            }
            $has_path_updates = 1;
        }
        elsif ( $k eq 'no_write' ) {
            $adb->config( no_write => $val );
            $has_cfg_updates = 1;
        }
        elsif ( $k eq 'db' || $k eq 'dbase_dir' || $k eq 'dbase' ) {
            my $abs = eval { abs_path($val) } // $val;
            $adb->set_datadir($abs);
            $explicit_db = 1;
            $has_path_updates = 1;
        }
        elsif ( $k eq 'data' ) {
            $method_args{data} = $val;
        }
        elsif ( $k eq 'user' || $k eq 'username' ) {
            $opt_user = $val;
        }
        elsif ( $k eq 'pass' || $k eq 'password' || $k eq 'passwd' ) {
            $opt_pass = $val;
        }
        elsif ( $k eq 'database' || $k eq 'dbname' ) {
            $opt_database = $val;
            $adb->{_connect}->{database} = $val;
            $adb->clear_cache('ramdisk');
            $adb->ramdisk_setup();
        }
        else {
            if ( $k =~ /[-.]/ ) {
                my @parts = split /[-.]/, $k;
                set_nested_key( \%method_args, \@parts, $val );
            }
            else {
                $method_args{$k} = $val;
            }
        }
    }
    else {
        if ( $arg =~ /^(?:help|h|usage)$/i ) {
            $opt_help = 1;
            $opt_action //= 'help';
        }
        elsif ( $arg =~ /^(?:help|usage)\.(tr|en)$/i ) {
            $opt_help = 1;
            $opt_help_lang = lc($1);
            $opt_action //= 'help';
        }
        elsif ( $arg =~ /^(?:dry-run|dry_run)$/i ) {
            $opt_dry_run = 1;
        }
        elsif ( $arg =~ /^(?:force|f)$/i ) {
            $opt_force = 1;
        }
        elsif ( $raw =~ /^--?(?:check|c)$/i ) {
            $method_args{check} = 1;
        }
        elsif ( $raw =~ /^--?(?:all|a)$/i ) {
            $method_args{all} = 1;
        }
        elsif ( $raw =~ /^--?(?:no-backup|no_backup)$/i ) {
            $method_args{no_backup} = 1;
        }

        elsif ( $raw =~ /^--?time$/i ) {
            $opt_time = 1;
        }
        elsif ( !defined $opt_action ) {
            $opt_action = $arg;
        }
        else {
            push @pos_args, $arg;
        }
    }
}

# Step 6: Action alias normalization
if ( defined $opt_action ) {
    my $act = lc($opt_action);
    if ( $act =~ /^(?:help|usage)(?:\.(tr|en))?$/i ) {
        $opt_help = 1;
        $opt_help_lang = lc($1) if defined $1;
        $opt_action = 'help';
    }
    $opt_action = 'setup'          if $act eq 'setup' || $act eq 'install';
    $opt_action = 'status'         if $act eq 'tables' || $act eq 'list';
    $opt_action = 'user'           if $act eq 'users';
    $opt_action = 'config'         if $act eq 'cfg';
    $opt_action = 'attr'           if $act eq 'table_attr';
    $opt_action = 'info'           if $act eq 'table_info';
    $opt_action = 'search'         if $act eq 'search_table';
    $opt_action = 'fetch'          if $act eq 'field_fetch';
    $opt_action = 'count'          if $act eq 'table_count';
    $opt_action = 'insert'         if $act eq 'insert_id';
    $opt_action = 'update'         if $act eq 'update_id';
    $opt_action = 'delete'         if $act eq 'delete_id';
    $opt_action = 'migrate'        if $act eq 'update_table';
    $opt_action = 'update_storage' if $act eq 'update-storage' || $act eq 'updatedb' || $act eq 'update_storage';
    $opt_action = 'update_version' if $act eq 'update-amberdb' || $act eq 'update-version' || $act eq 'update_version';
    $opt_action = 'export'         if $act eq 'tie2csv';
    $opt_action = 'import'         if $act eq 'csv2tie';
    $opt_action = 'reindex'        if $act eq 'set_index';
    $opt_action = 'vacuum'         if $act eq 'vacuum';
}

# Step 6b: Subcommand resolution for 'update' (e.g. amberdb update storage / amberdb update version)
if ( defined $opt_action && $opt_action eq 'update' && @pos_args ) {
    if ( lc($pos_args[0]) eq 'storage' ) {
        $opt_action = 'update_storage';
        shift @pos_args;
    }
    elsif ( lc($pos_args[0]) eq 'version' || lc($pos_args[0]) eq 'engine' || lc($pos_args[0]) eq 'amberdb' ) {
        $opt_action = 'update_version';
        shift @pos_args;
    }
}

# Step 7: Natural 'read' command resolution
if ( defined $opt_action && $opt_action eq 'read' ) {
    my $tbl = delete $method_args{table} // shift @pos_args;
    $method_args{table} = $tbl;

bin/amberdb_cli.pl  view on Meta::CPAN

  amberdb vacuum products                     # BDB disk boşluklarını temizle
  amberdb migrate products                    # Şema güncellemesi
  amberdb export products file=yedek.csv      # CSV dışa aktarımı
  amberdb import products file=yeni.csv       # CSV içe aktarımı
  amberdb dump products file=yedek.tar.gz     # Veritabanı yedeği al
  amberdb restore file=yedek.tar.gz           # Yedekten geri yükle
  amberdb rename from=eski to=yeni            # Tablo adı değiştir
  amberdb drop temp_tbl force=1               # Tabloyu kalıcı olarak sil

Genel Seçenekler:
  --format=table|json|pretty|tsv|dumper       Çıktı biçimi (Varsayılan: table. Veya sonda: json, tsv, dumper)
  --db=/path/to/dbstore                       Oturumsuz doğrudan veritabanı yolu
  --token=TOKEN                               Aktif oturum anahtarı
  --time, time, time=1                        İşlem süresini en altta satır olarak yazar
  --dry-run                                   İşlemi uygulamadan simüle eder
  --force                                     Silme eylemleri için zorunlu onay
  --version, -v, version                      Sürüm bilgisini yazar

İngilizce yardım için: amberdb usage
USAGE_TR
        exit 0;
    }

    # Default: English
    print <<"USAGE_EN";
AmberDB CLI v$AmberDB::VERSION - Embedded Database Console & Management Tool

Usage:
  amberdb [action|token] [table] [parameters...]

Usage Modes:

1. Direct Local Execution (Default - Local ./dbstore):
  amberdb tables                              # List tables in current directory ./dbstore
  amberdb read products 10                    # Read single record by ID
  amberdb read products 10 inflate=1 json     # Read record with schema inflation as JSON
  amberdb search products "headphone" limit=10# Full-text and phonetic search
  amberdb insert users 0 data='{"name":"Ali"}'# Insert record (creates ./dbstore if missing)
  amberdb update users 10 data='{"role":"admin"}'
  amberdb delete users 10

2. Named Session (Session Management):
  amberdb connect <database>                  # Connect to ~/.amberdb/<database> pool
  amberdb connect <database>\@<path>           # Connect to custom folder (e.g. mydb\@./dbstore)
  amberdb 1245 tables                         # List tables for session 1245
  amberdb 1245 read products 10               # Read record in session 1245
  amberdb 1245 disconnect                     # Terminate active session

3. Infrastructure & Setup:
  amberdb setup                               # Provision ~/.amberdb global workspace
  amberdb setup [dir]                         # Initialize database skeleton in custom directory
  amberdb setup ramdisk [start|stop|status]   # RAM-disk drive management

Maintenance & Administrative Actions:
  amberdb update storage [--check] [--force]  # Directory & ABR v5 format migration
  amberdb update version [--check]            # MetaCPAN core version check / update
  amberdb reindex products                    # Rebuild all derived indexes
  amberdb check products                      # Physical file integrity check
  amberdb vacuum products                     # Compact and reclaim BDB storage space
  amberdb migrate products                    # Schema migration
  amberdb export products file=backup.csv     # Export table to CSV
  amberdb import products file=new.csv        # Import table from CSV
  amberdb dump products file=backup.tar.gz    # Create database backup archive
  amberdb restore file=backup.tar.gz          # Restore database from backup
  amberdb rename from=old to=new              # Rename table
  amberdb drop temp_tbl force=1               # Permanently drop table and indexes

General Options:
  --format=table|json|pretty|tsv|dumper       Output format (Default: table. Or trailing: json, tsv, dumper)
  --db=/path/to/dbstore                       Direct database directory without session
  --token=TOKEN                               Active session token
  --time, time, time=1                        Prints execution time at the bottom
  --dry-run                                   Simulates execution without writes
  --force                                     Required confirmation for destructive actions
  --version, -v, version                      Display version information

For Turkish help: amberdb usage.tr
USAGE_EN
    exit 0;
}

if ( $opt_help || ( defined $opt_action && $opt_action eq 'help' ) ) {
    my $lang = $opt_help_lang;
    if ( @pos_args && $pos_args[0] =~ /^(?:tr|en)$/i ) {
        $lang //= lc(shift @pos_args);
    }
    $lang //= 'en';
    $opt_help = 1;
    show_usage($lang);
}

if ( defined $opt_action && $opt_action =~ /^(?:version|--version|-v)$/i ) {
    if ( defined $opt_format && $opt_format eq 'json' ) {
        output_result( { version => $AmberDB::VERSION, engine => 'AmberDB', platform => $^O }, $opt_format );
    }
    else {
        print "AmberDB v$AmberDB::VERSION (CLI)\n";
    }
    exit 0;
}

# ============================================================================
# LIFECYCLE: CONNECT & DISCONNECT
# ============================================================================

if ( defined $opt_action && $opt_action eq 'connect' ) {
    my $conn_arg = $opt_database // shift @pos_args;
    my ( $conn_db, $custom_path );

    if ( defined $conn_arg ) {
        # Custom directory format: <dbname>@<path> (e.g. mydb@./dbstore or mydb@C:/data)
        if ( $conn_arg =~ /^([a-zA-Z0-9_\-]+)@(.+)$/ ) {
            $conn_db     = $1;
            $custom_path = $2;
        }
        else {
            $conn_db = $conn_arg;
        }
    }

    if ( defined $conn_db && ( $conn_db eq 'dbstore' || $conn_db =~ m{[/\\\\]} || $conn_db =~ /^[a-zA-Z]:/ ) ) {
        die "[AMBERDB_ERROR] '$conn_db' is a directory path/name, not a valid database name. Database name is expected after 'connect'. For custom directory use format: <dbname>\@<path> (e.g. amberdb connect mydb\@./dbstore) or specify --db=<dir>.\n"...
    }

    $conn_db //= $detected_dbname;
    unless ( defined $conn_db && length $conn_db ) {
        die "[AMBERDB_ERROR] Database name required. Usage: amberdb connect <database_name> (or <database_name>\@<path>)\n";
    }

    my $target_data_dir;
    if ( defined $custom_path && length $custom_path ) {
        $target_data_dir = resolve_abs_path($custom_path);
    }
    elsif ( $explicit_db ) {
        $target_data_dir = $adb->path('dbase_dir');
    }
    else {
        # Central store under ~/.amberdb/<dbname>
        $target_data_dir = "$AMBERDB_HOME/$conn_db";
    }
    $target_data_dir = resolve_abs_path($target_data_dir);

    # Ensure target data directory and skeleton exist
    make_path(
        "$target_data_dir/table", "$target_data_dir/schema", "$target_data_dir/journal",
        "$target_data_dir/lock",  "$target_data_dir/session", "$target_data_dir/config",
        "$target_data_dir/backup", "$target_data_dir/ramdisk"
    );
    my $conf_file = "$target_data_dir/config/core.conf";
    if ( !-f $conf_file && open my $cfh, '>', $conf_file ) {
        print $cfh "site_id        $conn_db\nlanguage       tr\n";
        close $cfh;
    }

    $adb->set_datadir($target_data_dir);
    $adb->{_connect}->{database} = $conn_db;
    $adb->clear_cache('ramdisk');
    $adb->ramdisk_setup();

    # Authenticate and generate token
    my $conn_user = 'cli';
    my $entered_user = shift @pos_args if @pos_args;
    my $conn_pass = $opt_pass // shift @pos_args // '';

    my $token = eval {
        $adb->connect(
            database => $conn_db,
            username => $conn_user,
            ( defined $conn_pass && length $conn_pass ? ( password => $conn_pass ) : () ),
        );
    };
    if ( $@ || !$token ) {
        my $err = $@ || $adb->last_error() || "Authentication failed";
        $err =~ s/ at .* line \d+.*//s;
        die "[AMBERDB_ERROR] $err\n";
    }

    # Save session with canonical absolute path in ~/.amberdb/session/sess_$token
    my $sess_data = {
        token    => $token,
        database => $conn_db,
        username => $conn_user,
        path     => { dbase_dir => $target_data_dir },
        cfg      => $adb->config(),
    };
    set_session_path( $token, $target_data_dir, $sess_data );

    my $actual_file = cli_session_file($token);

    if ( defined $opt_format && $opt_format eq 'json' ) {
        print encode_json( {
            status       => 'connected',
            token        => $token,
            session_file => $actual_file,
            database     => $conn_db,
            username     => $conn_user,
            path         => { dbase_dir => $target_data_dir },
            cfg          => $adb->config(),
        } ), "\n";
    }
    else {
        print "[AMBERDB] Connected successfully.\n";
        print "Session Token : $token\n";
        print "Database      : $conn_db\n";
        print "Data Dir      : $target_data_dir\n";
        print "Session File  : $actual_file\n";
        print "\nUsage with Token:\n";

bin/amberdb_cli.pl  view on Meta::CPAN

                }
                exit $? >> 8;
            }
        }
        else {
            if ( $subcmd eq 'status' ) {
                my $shm = -d "/dev/shm" ? "/dev/shm (Linux tmpfs available)" : "Not mounted";
                print "RAM-Disk Status: $shm\n";
                exit 0;
            }
            elsif ( $subcmd eq 'start' ) {
                eval { $adb->ramdisk_setup() };
                print "[RAM-DISK] Linux /dev/shm initialized.\n";
                exit 0;
            }
            elsif ( $subcmd eq 'stop' ) {
                print "[RAM-DISK] Stopped.\n";
                exit 0;
            }
        }
    }

    # If no argument is passed: setup global environment (~/.amberdb)
    if ( !defined $first_arg || !length $first_arg ) {
        setup_global_workspace();

        if ( defined $opt_format && $opt_format eq 'json' ) {
            output_result( {
                status       => 'ok',
                action       => 'setup',
                mode         => 'global',
                amberdb_home => $AMBERDB_HOME,
                session_dir  => $CLI_SESS_DIR,
            }, $opt_format );
            exit 0;
        }

        print "=================================================================\n";
        print " AmberDB Global Environment Setup & Initialization               \n";
        print "=================================================================\n";
        print "AmberDB Home     : $AMBERDB_HOME (~/.amberdb)\n";
        print "Session Registry : $CLI_SESS_DIR/\n";
        print "Platform         : $^O\n";
        print "-----------------------------------------------------------------\n";
        print "[SUCCESS] AmberDB global workspace initialized successfully!\n\n";
        print "Kullanım Biçimleri:\n";
        print "  1. Yerel Proje   : Proje klasörünüzde doğrudan çalıştırın (./dbstore kullanılır)\n";
        print "     amberdb insert users 0 data='{\"name\":\"Ahmet\"}'\n";
        print "     amberdb tables\n\n";
        print "  2. Merkezi Havuz : Belirtilen ada oturum açarak ~/.amberdb/<ad> kullanılır\n";
        print "     amberdb connect eticaretim\n";
        print "     amberdb 1245 tables\n\n";
        print "  3. Özel Klasör   : 'ad\@yol' formatı ile oturum açılır\n";
        print "     amberdb connect eticaretim\@./dbstore\n";
        print "=================================================================\n";
        exit 0;
    }

    # If custom directory passed (e.g. amberdb setup /var/data or amberdb setup my_dir)
    my $target_dir = resolve_abs_path($first_arg);
    my @subdirs = qw(table schema journal lock session config backup ramdisk);
    my @created;
    my @existing;
    make_path($target_dir) unless -d $target_dir;
    for my $sub (@subdirs) {
        my $dir_path = "$target_dir/$sub";
        if ( -d $dir_path ) {
            push @existing, $sub;
        }
        else {
            make_path($dir_path);
            push @created, $sub;
        }
    }
    my $core_conf = "$target_dir/config/core.conf";
    my $conf_created = 0;
    if ( !-f $core_conf && open my $fh, '>', $core_conf ) {
        print $fh "# AmberDB Core Configuration\n";
        print $fh "site_id        amberdb\n";
        print $fh "language       tr\n";
        close $fh;
        $conf_created = 1;
    }
    if ( defined $opt_format && $opt_format eq 'json' ) {
        output_result( {
            status        => 'ok',
            action        => 'setup',
            dbase_dir     => $target_dir,
            dirs_created  => \@created,
            dirs_existing => \@existing,
            config_init   => $conf_created ? 1 : 0,
        }, $opt_format );
        exit 0;
    }

    print "=================================================================\n";
    print " AmberDB Infrastructure Setup: $target_dir                       \n";
    print "=================================================================\n";
    for my $c (@created)  { print "  [+] Created  $c/\n"; }
    for my $e (@existing) { print "  [.] Exists   $e/\n"; }
    print "-----------------------------------------------------------------\n";
    print "[SUCCESS] Initialized successfully in $target_dir\n";
    print "=================================================================\n";
    exit 0;
}

# ============================================================================
# DEFAULT ACTION: DASHBOARD OVERVIEW (ALL TABLES)
# ============================================================================

if ( !defined $opt_action || $opt_action eq '' || $opt_action eq 'list' || $opt_action eq 'status' || $opt_action eq 'tables' ) {
    my $db_dir = $adb->path('dbase_dir') || ".";

    if ( !-d $db_dir || ( !-d "$db_dir/table" && !-d "$db_dir/schema" ) ) {
        if ( defined $opt_format && ( $opt_format eq 'json' || $opt_format eq 'pretty' ) ) {
            output_result( {
                database      => $adb->connect('database') || $detected_dbname || '-',
                user          => $adb->connect('username') || 'cli',
                data_dir      => $db_dir,
                total_tables  => 0,
                total_records => 0,

bin/amberdb_cli.pl  view on Meta::CPAN

        for my $i ( 0 .. $#$blocks ) {
            my $b = $blocks->[$i];
            my $bname = ref $b eq 'HASH' ? ( $b->{name} // "block_$i" ) : "block_$i";
            my $btype = ref $b eq 'HASH' ? ( $b->{type} // "string" ) : "string";
            my $bidx  = ref $b eq 'HASH' ? ( $b->{index} // "-" ) : "-";
            my $srch  = $is_search{$i} ? "yes" : "-";
            my $rdbm = "-";
            if ( ref $b eq 'HASH' && $b->{rdbm} ) {
                if ( ref $b->{rdbm} eq 'HASH' ) {
                    my $rt = $b->{rdbm}->{table} // '';
                    my $rb = $b->{rdbm}->{block} // '';
                    $rdbm = ( $rb ne '' ) ? "$rt,$rb" : $rt;
                }
                else {
                    $rdbm = $b->{rdbm};
                }
            }
            printf( "%-6s %-20s %-12s %-12s %-8s %-15s\n",
                $i,
                substr( $bname, 0, 19 ),
                substr( $btype, 0, 11 ),
                substr( $bidx,  0, 11 ),
                $srch,
                substr( $rdbm,  0, 14 )
            );
        }
        print "=" x 80, "\n";
        if ( ref $info eq 'HASH' ) {
            my @meta_attrs;
            for my $k ( sort keys %$info ) {
                next if $k eq 'blocks' || $k eq 'search_block';
                my $v = $info->{$k};
                push @meta_attrs, "$k=" . ( ref $v ? encode_json($v) : $v );
            }
            print "Attributes: " . join( ", ", @meta_attrs ) . "\n" if @meta_attrs;
        }
    }
    exit 0;
}

# 9. INSERT_ID / INSERT
if ( $action eq 'insert_id' || $action eq 'insert' ) {
    if ( $adb->config('no_write') ) {
        die "[AMBERDB_ERROR] no_write aktif: Bu oturumda yazma yetkisi kısıtlanmıştır!\n";
    }

    my $table = delete $method_args{table} // shift @pos_args;
    my $id    = delete $method_args{id};
    if ( !defined $id && @pos_args && $pos_args[0] =~ /^\d+$/ ) {
        $id = shift @pos_args;
    }
    $id //= 0;
    my $data  = delete $method_args{data};

    # Ensure database directory and skeleton exist on write
    my $db_dir = $adb->path('dbase_dir') || "dbstore";
    if ( !-d $db_dir || !-d "$db_dir/table" ) {
        make_path(
            "$db_dir/table", "$db_dir/schema", "$db_dir/journal",
            "$db_dir/lock",  "$db_dir/session", "$db_dir/config",
            "$db_dir/backup", "$db_dir/ramdisk"
        );
        my $conf_file = "$db_dir/config/core.conf";
        if ( !-f $conf_file && open my $cfh, '>', $conf_file ) {
            print $cfh "site_id        amberdb\nlanguage       tr\n";
            close $cfh;
        }
    }

    if ( !defined $data ) {
        $data = scalar keys %method_args ? \%method_args : [ map { parse_value($_) } @pos_args ];
    }
    elsif ( !ref $data ) {
        my $parsed = parse_value($data);
        $data = $parsed if ref $parsed;
    }

    die "[AMBERDB_ERROR] 'table' parameter required for insert\n" unless defined $table && length $table;

    if ($opt_dry_run) {
        output_result( { dry_run => 1, action => 'insert_id', table => $table, id => $id, data => $data }, $opt_format );
        exit 0;
    }

    my $res;
    if ( ref $data eq 'ARRAY' ) {
        $res = $adb->insert_id( $table, $id, @$data );
    }
    else {
        $res = $adb->insert_id( $table, $id, $data );
    }
    output_result( { status => 'ok', action => 'insert_id', table => $table, id => $res }, $opt_format );
    exit 0;
}

# 9b. UPDATE_STORAGE
if ( $action eq 'update_storage' ) {
    my %opts;
    $opts{target_dir} = $adb->path('dbase_dir');
    $opts{force}      = $opt_force if $opt_force;
    $opts{check}      = $method_args{check} if exists $method_args{check};
    $opts{no_backup}  = $method_args{no_backup} if exists $method_args{no_backup};
    $opts{tables}     = $method_args{tables} // $method_args{table} // ( @pos_args ? join(',', @pos_args) : undef );
    $opts{manifest}   = $method_args{manifest} if exists $method_args{manifest};

    my $res = $tools->update_storage(%opts);
    output_result( $res, $opt_format ) if defined $opt_format;
    exit 0;
}

# 9c. UPDATE_VERSION
if ( $action eq 'update_version' ) {
    my %opts;
    $opts{check} = $method_args{check} if exists $method_args{check};
    $opts{cpanm} = $method_args{cpanm} if exists $method_args{cpanm};

    my $res = $tools->update_version(%opts);
    output_result( $res, $opt_format ) if defined $opt_format;
    exit 0;
}

# 10. UPDATE_ID / UPDATE
if ( $action eq 'update_id' || $action eq 'update' ) {
    if ( $adb->config('no_write') ) {
        die "[AMBERDB_ERROR] no_write aktif: Bu oturumda yazma yetkisi kısıtlanmıştır!\n";
    }

    my $table = delete $method_args{table} // shift @pos_args;
    my $id    = delete $method_args{id}    // shift @pos_args;
    my $data  = delete $method_args{data};

    if ( !defined $data ) {
        $data = scalar keys %method_args ? \%method_args : [ map { parse_value($_) } @pos_args ];
    }
    elsif ( !ref $data ) {
        my $parsed = parse_value($data);
        $data = $parsed if ref $parsed;
    }

    die "[AMBERDB_ERROR] 'table' and 'id' parameters required for update\n"
      unless defined $table && defined $id && length $id;

    if ( !is_db_ready() || !table_file_exists($table) ) {
        die "[AMBERDB_ERROR] Table '$table' does not exist.\n";
    }

    if ($opt_dry_run) {
        output_result( { dry_run => 1, action => 'update_id', table => $table, id => $id, data => $data }, $opt_format );
        exit 0;
    }

    my $res;
    if ( ref $data eq 'ARRAY' ) {
        $res = $adb->update_id( $table, $id, @$data );
    }
    else {
        $res = $adb->update_id( $table, $id, $data );
    }
    output_result( { status => ( $res ? 'ok' : 'error' ), action => 'update_id', table => $table, id => $id, result => ( $res ? 1 : 0 ) }, $opt_format );
    exit 0;
}



( run in 1.144 second using v1.01-cache-2.11-cpan-99fa8243a42 )