package Daje::Database::Model::Super::Common::Base;
use Mojo::Base -base, -signatures;
use v5.40;

# Autogenerated class Thu Jun 26 17:06:45 2025
# Any manual changes to this class will be overwritten next time its generated

has 'db';

sub load_from_index($self, $table, $select, $condition ) {
    my $result->{result} = 1;
    try {
        my $load = $self->db->select(
            $table,
                $select,
                $condition
        );
        $result->{data} = {};
        $result->{data} = $load->hash if $load and $load->rows > 0;
    } catch ($e) {
        $result->{result} = 0;
        $result->{data} = "";
        $result->{error} = $e;
    };

    return $result;

}

sub load_pk($self, $table, $select, $primary_key_name, $primary_key) {
    my $result->{result} = 1;
    try {
        my $load = $self->db->select(
            $table,
                $select,
                {$primary_key_name => $primary_key}
        );
        $result->{data} = {};
        $result->{data} = $load->hash if $load and $load->rows > 0;
    } catch ($e) {
        $result->{result} = 0;
        $result->{data} = "";
        $result->{error} = $e;
    };

    return $result;
};

sub load_fkey($self, $table, $select, $foreign_key_name, $foreign_key) {
    my $result->{result} = 1;
    try {
        my $load = $self->db->select(
            $table,
                $select,
            {$foreign_key_name => $foreign_key}
        );
        $result->{data} = {};
        $result->{data} = $load->hashes if $load and $load->rows > 0;
    } catch ($e) {
        $result->{result} = 0;
        $result->{data} = "";
        $result->{error} = $e;
    };
    return $result;
}

sub load_a_list($self, $table, $select, $key_value) {
    my $result->{result} = 1;
     try {
        my $load = $self->db->select(
            $table,
                $select,
            {$key_value}
        );
        $result->{data} = {};
        $result->{data} = $load->hashes if $load and $load->rows > 0;
    } catch ($e) {
        $result->{result} = 0;
        $result->{data} = "";
        $result->{error} = $e;
    };

    return $result;
}

sub load_a_full_list($self, $table, $select) {
    my $result->{result} = 1;
    try {
        my $load = $self->db->select(
            $table,
            $select
        );
        $result->{data} = {};
        $result->{data} = $load->hashes if $load and $load->rows > 0;
    } catch ($e) {
        $result->{result} = 0;
        $result->{data} = "";
        $result->{error} = $e;
    };

    return $result;
}
sub insert($self, $table, $data, $primary_key_name) {
    my $result->{result} = 1;
    try {
        my $primary_key = $self->db->insert(
            $table, $data,
                { returning => $primary_key_name }
        );
        $result->{data} = {};
        $result->{data} = $primary_key->hash
            if $primary_key and $primary_key->rows > 0;
    } catch ($e) {
        $result->{error} = $e;
        $result->{result} = 0;
        $result->{data} = {};
    };

    return $result;
}

sub update($self, $table, $data, $keys) {
    my $result->{result} = 1;
    try {
        $self->db->update($table, $data, $keys);
    } catch($e) {
        $result->{error} = $e;
        $result->{result} = 0;
    };
    return $result;
}

1;

