MToken

 view release on metacpan or  search on metacpan

lib/MToken/Store.pm  view on Meta::CPAN

package MToken::Store; # $Id: Store.pm 116 2021-10-12 15:17:49Z minus $
use strict;
use utf8;

=encoding utf-8

=head1 NAME

MToken::Store - MToken store class

=head1 VERSION

Version 1.00

=head1 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;

=head1 DESCRIPTION

This module provides store methods.

=head2 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
    );

=head2 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;

=head2 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
    );

=head1 METHODS

=head2 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

=head2 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

=head2 count

    print $store->count();

Returns count of records

=head2 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

=head2 dsn

    my $dsn = $store->dsn;

Returns DSN string of current database connection

=head2 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.

=head2 file

    my $file = $store->file;

Returns the file of SQLite database

=head2 get



( run in 1.563 second using v1.01-cache-2.11-cpan-39a47a84364 )