Yote-SQLObjectStore

 view release on metacpan or  search on metacpan

lib/Yote/SQLObjectStore/BaseObj.pm  view on Meta::CPAN

    my ($self,$field,$value) = @_;

    # check the col
    my $cols = $self->cols;
    my $def = $cols->{$field};

    die "No field '$field' in ".ref($self) unless $def;

    my $store = $self->store;

    my ($item, $field_value) = $store->xform_in_full( $value, $def );

    my $data = $self->data;

    my $dirty = $data->{$field} ne $field_value;

    $data->{$field} = $field_value;

    if ($dirty) {
        $self->dirty;
    }

    return $item;    
}

sub get {
    my ($self,$field,$default) = @_;

    my $cols = $self->cols;
    my $def = $cols->{$field};

    die "No field '$field' in ".ref($self) unless $def;

    my $data = $self->data;

    if ((! exists $data->{$field}) and defined($default)) {
	return $self->set($field,$default);
    }

    return $self->store->xform_out( $data->{$field}, $def );
} #get


=head1 COLUMN TYPES

Object fields are declared via the package-level C<%cols> hash, mapping
column names to type definitions.

    package MyApp::User;
    use base 'Yote::SQLObjectStore::SQLite::Obj';

    our %cols = (
        name    => 'VARCHAR(100)',
        bio     => 'TEXT',
        age     => 'INTEGER',
        score   => 'DECIMAL(5,2)',
        active  => 'BOOLEAN',
        avatar  => '*MyApp::Image',
        tags    => '*ARRAY_VARCHAR(50)',
        friends => '*ARRAY_*MyApp::User',
        prefs   => '*HASH<256>_TEXT',
    );

There are two categories of column types: B<scalar types> for storing
values directly in the object's table, and B<reference types> for
storing references to other objects or to collection objects (arrays
and hashes).

=head2 Scalar Types

Scalar types use ANSI SQL standard names. Each backend automatically
translates these to native database types via C<map_type> in the
backend's TableManager, so the same C<%cols> definition works across
SQLite, MariaDB, and PostgreSQL.

=head3 Text

    TEXT            Variable-length text, no length limit
    VARCHAR(N)      Variable-length text, max N characters

C<TEXT> is the general-purpose string type. Use C<VARCHAR(N)> when you
want to express a maximum length. MariaDB and PostgreSQL enforce the
limit; SQLite preserves the declaration but treats it as TEXT affinity.

=head3 Numeric

    INTEGER         Standard integer (4 bytes on most backends)
    BIGINT          Large integer (8 bytes)
    SMALLINT        Small integer (2 bytes)
    FLOAT           Single-precision floating point
    DOUBLE          Double-precision floating point
    DECIMAL(M,D)    Exact decimal with M total digits, D after the decimal point

SQLite maps all integer types to C<INTEGER> and all floating-point types
to C<REAL>. MariaDB maps C<INTEGER> to C<INT>. PostgreSQL maps C<FLOAT>
to C<REAL> and C<DOUBLE> to C<DOUBLE PRECISION>.

=head3 Other

    BOOLEAN         True/false value
    BLOB            Binary data
    TIMESTAMP       Date and time
    DATE            Date only

SQLite maps C<BOOLEAN> to C<INTEGER> and C<TIMESTAMP>/C<DATE> to C<TEXT>.
MariaDB maps C<BOOLEAN> to C<TINYINT(1)>.
PostgreSQL maps C<BLOB> to C<BYTEA>.

=head3 Backend Type Mapping Summary

    Standard        SQLite      MariaDB         PostgreSQL
    --------        ------      -------         ----------
    TEXT            TEXT        TEXT            TEXT
    VARCHAR(N)      VARCHAR(N)  VARCHAR(N)      VARCHAR(N)
    INTEGER         INTEGER     INT             INTEGER
    BIGINT          INTEGER     BIGINT          BIGINT
    SMALLINT        INTEGER     SMALLINT        SMALLINT
    FLOAT           REAL        FLOAT           REAL
    DOUBLE          REAL        DOUBLE          DOUBLE PRECISION
    DECIMAL(M,D)    NUMERIC     DECIMAL(M,D)    DECIMAL(M,D)
    BOOLEAN         INTEGER     TINYINT(1)      BOOLEAN



( run in 1.995 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )