The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MToken::Store - MToken store class

VERSION

Version 1.00

SYNOPSIS

    use MToken::Store;

    my $store = MToken::Store->new(
        file => "/tmp/test.db",
        attributes => "RaiseError=0; PrintError=0; sqlite_unicode=1",
        do_init => 1, # Need to try initialize the db
    );

    my $store = MToken::Store->new(
        dsn => "DBI:mysql:database=MToken;host=mysql.example.com",
        user => "username",
        password => "password",
        set => [
            "RaiseError        0",
            "PrintError        0",
            "mysql_enable_utf8 1",
        ],
    );

    die($store->error) unless $store->status;

DESCRIPTION

This module provides store methods.

SQLITE DDL

    CREATE TABLE "mtoken" (
      "id"          INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
      "file"        CHAR(256) NOT NULL UNIQUE,
      "size"        INTEGER NOT NULL,
      "mtime"       INTEGER NOT NULL,
      "checksum"    CHAR(64) DEFAULT NULL,
      "tags"        CHAR(256) DEFAULT NULL,
      "subject"     CHAR(1024) DEFAULT NULL,
      "content"     TEXT DEFAULT NULL
    );

MYSQL DDL

    CREATE DATABASE `mtoken` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;
    CREATE TABLE IF NOT EXISTS `mtoken` (
      `id`          INT(11) NOT NULL AUTO_INCREMENT,
      `file`        VARCHAR(256) COLLATE utf8_bin NOT NULL, -- File name
      `size`        INT(11) NOT NULL, -- File size
      `mtime`       INT(11) NOT NULL, -- Unixtime value of modified time (mtime)
      `checksum`    VARCHAR(64) NOT NULL, -- Checksum (MD5/SHA1/SHA256)
      `tags`        VARCHAR(256) DEFAULT NULL, -- Tags
      `subject`     VARCHAR(1024) DEFAULT NULL, -- Subject
      `content`     TEXT COLLATE utf8_bin DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `file` (`file`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

POSTGRESQL DDL

    CREATE TABLE IF NOT EXISTS `mtoken` (
      `id`          INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
      `file`        CHAR(256) COLLATE utf8_bin NOT NULL UNIQUE,
      `size`        INTEGER NOT NULL,
      `mtime`       INTEGER NOT NULL,
      `checksum`    CHAR(64) DEFAULT NULL,
      `tags`        CHAR(256) DEFAULT NULL,
      `subject`     CHAR(1024) DEFAULT NULL,
      `content`     TEXT COLLATE utf8_bin DEFAULT NULL
    );

METHODS

new

    my $store = MToken::Store->new(
        file => "/tmp/test.db",
        attributes => "RaiseError=0; PrintError=0; sqlite_unicode=1",
        do_init => 1, # Need to try initialize the db
    );

    # ... or MySQL:

    my $store = MToken::Store->new(
        dsn => "DBI:mysql:database=mtoken;host=mysql.example.com",
        user => "username",
        password => "password",
        set => [
            "RaiseError        0",
            "PrintError        0",
            "mysql_enable_utf8 1",
        ],
    );

Creates DBI object

add

    $store->add(
        file        => "test.txt",
        size        => 1024,
        mtime       => 1590000000,
        checksum    => "1a6f4a41ae8eec2da84dbfa48636e02e33575dbd",
        tags        => "test, example",
        subject     => "Test file for example",
        content     => "...Content of the file...",
    ) or die($store->error);

Add new recored

count

    print $store->count();

Returns count of records

del

    $store->del("test.txt") or die($store->error);

Delete record by filename

    $store->del(1) or die($store->error);

Delete record by record id

dsn

    my $dsn = $store->dsn;

Returns DSN string of current database connection

error

    my $error = $store->error;

Returns error message

    my $status = $store->error( "Error message" );

Sets error message if argument is provided. This method in "set" context returns status of the operation as status() method.

file

    my $file = $store->file;

Returns the file of SQLite database

get

    my %data = $store->get("test.txt");

Returns data from database by filename

    my %data = $store->get(1);

Returns data from database by record id

getall

    my @table = $store->getall();
    my @table_100 = $store->getall(100); # row_count
    my @table_100 = $store->getall(100, 100); # offset, row_count

Returns data from database with limit supporting

is_sqlite

    print $store->is_sqlite ? "Is SQLite" : "Is NOT SQLite"

Returns true if type of current database is SQLite

ping

    $store->ping ? 'OK' : 'Database session is expired';

Checks the connection to database

set

    $store->set(
        file        => "test.txt",
        size        => 1024,
        mtime       => 1590000000,
        checksum    => "1a6f4a41ae8eec2da84dbfa48636e02e33575dbd",
        tags        => "test, example",
        subject     => "Test file for example",
        content     => "...New content of the file...",
    ) or die($store->error);

Update recored by document number

    $store->set(
        id          => 1,
        file        => "test.txt",
        size        => 1024,
        mtime       => 1590000000,
        checksum    => "1a6f4a41ae8eec2da84dbfa48636e02e33575dbd",
        tags        => "test, example",
        subject     => "Test file for example",
        content     => "...New content of the file...",
    ) or die($store->error);

Update recored by record id

status

    my $status = $store->status;
    my $status = $store->status( 1 ); # Sets the status value and returns it

Get/set BOOL status of the operation

truncate

    $store->truncate or die($store->error);

Delete all records

HISTORY

See Changes file

TO DO

See TODO file

BUGS

* none noted

SEE ALSO

CTK::DBI, MToken

AUTHOR

Serż Minus (Sergey Lepenkov) http://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See LICENSE file and https://dev.perl.org/licenses/