Menu

PLUGINS

Carlos Celso de Almeida

go to: HOME, WHERE or CONSTANTS

PLUGINS

BEWARE: Use Wisely

The plugins consists in addon to customize the process and/or to create data and/or adjusts the arguments on the Methods. They are not mandatory, however is available to use. It is can apply over:

Call Method
Delete Method
Insert Method
Open Method
Select Method
SelectCursor Method
Update Method
PreFetch Methods

NOTE: The methods are not required. If they are omitted, the module will ignore them.

Create in your plugin Methods using same name above. The module will check it before trying to run.

All modules receive as an argument the "hash_ref" address as unique data. You can run the public methods and/or make changes or adjustments in the data before the module execute the operation.

The plugin must return the following termination code:

rc < 0        Syntax error and abort the action;
rc = 0        Successful and continue the action;
rc = 1        Error and abort the action;
rc = 2        Successful but skip the action.

BEWARE: Do not make changes on currents plugins.

Plugin Constructor Method

The Plugin Constructor will receive the address of control data. This address can be changed or used to call the methods.

The constructor must be:

  # called by: SQL::SimpleOps::[interface]::[driver]->new ( sql_simple => $self );

  sub new()
  {
     my $class = shift; $class = ref($class) || $class || 'SQL::SimpleOps::[interface]::[driver]';
     my $self = {@};    # <-- sql_simple as argument
     bless($self,$class);
  }

MySQL/MariaDB

This plugins embeded and establish the "dsname" data, "argument #1" from "DBI->connect()"

  SQL::SimpleOps::DBI::MySQL

  sub new()
  {
        my $class = shift; $class = ref($class) || $class || 'SQL::SimpleOps::DBI::MySQL';
        my $self = {@_};

        $self->{sql_simple}->{init}{plugin_id} = "MySQL";
        $self->{sql_simple}->{init}{schema} = 0;
        $self->{sql_simple}->{init}{test_server} = 1;
        $self->{sql_simple}->{init}{alias_with_as} = 0;

        bless($self,$class);
  }

  sub Open()
  {
     my $self = shift;
     my $argv = shift;

     my @options;
     push(@options,"database=".$self->{sql_simple}->{argv}{db}) if (defined($self->{sql_simple}->{argv}{db}) && $self->{sql_simple}->{argv}{db}ne "");
     push(@options,"host=".$self->{sql_simple}->{argv}{server}) if (defined($self->{sql_simple}->{argv}{server}) && $self->{sql_simple}->{argv}{server} ne "");
     push(@options,$self->{sql_simple}->{argv}{port}) if (defined($self->{sql_simple}->{argv}{port}) && $self->{sql_simple}->{argv}{port} ne "");
     $self->{sql_simple}->{argv}{dsname} = "DBI:mysql:".join(';',@options);

     return 0;
  }

NOTE: No more changes need.

Postgress

This plugins embeded and establish the "dsname" data, "argument #1" from "DBI->connect()"

  SQL::SimpleOps::DBI::PG

  sub new()
  {
        my $class = shift; $class = ref($class) || $class || 'SQL::SimpleOps::DBI::PG';
        my $self = {@_};

        $self->{sql_simple}->{init}{plugin_id} = "PG";
        $self->{sql_simple}->{init}{schema} = 1;
        $self->{sql_simple}->{init}{test_server} = 1;
        $self->{sql_simple}->{init}{alias_with_as} = 1;

         bless($self,$class);
  }

  sub Open()
  {
     my $self = shift;
     my $argv = shift;

     my @options;
     push(@options,"dbname=".$self->{sql_simple}->{argv}{db}) if (defined($self->{sql_simple}->{argv}{db}) && $self->{sql_simple}->{argv}{db} ne "");
     push(@options,"host=".$self->{sql_simple}->{argv}{server}) if (defined($self->{sql_simple}->{argv}{server}) && $self->{sql_simple}->{argv}{server} ne "");
     push(@options,$self->{sql_simple}->{argv}{port}) if (defined($self->{sql_simple}->{argv}{port}) && $self->{sql_simple}->{argv}{port} ne "");

     $self->{sql_simple}->{argv}{dsname} = "DBI:Pg:".join(';',@options);
     return 0;
  }

NOTE: No more changes need.

SQLite

This plugins embeded and establish the "dsname" data, "argument #1" from "DBI->connect()"

  SQL::SimpleOps::DBI::SQLite

  sub new()
  {
        my $class = shift; $class = ref($class) || $class || 'SQL::SimpleOps::DBI::SQLite';
        my $self = {@_};

        if ($self->{sql_simple}->{argv}{db} eq "" && $self->{sql_simple}->{argv}{dbfile} eq "")
        {
                $self->{sql_simple}->setMessage($self,"new",-1,"001");
                return undef;
        }
        $self->{sql_simple}->{init}{plugin_id} = "SQLite";
        $self->{sql_simple}->{init}{schema} = 0;
        $self->{sql_simple}->{init}{test_server} = 0;
        $self->{sql_simple}->{init}{alias_with_as} = 0;

        bless($self,$class);
  }

  sub Open()
  {
     my $self = shift;
     my $argv = shift;

     $self->{sql_simple}->{argv}{dbfile} = $self->{sql_simple}->{argv}{db}.".db" if (!defined($self->{sql_simple}->{argv}{dbfile}) || $self->{sql_simple}->{argv}{dbfile} eq "");
     $self->{sql_simple}->{argv}{dsname} = "DBI:SQLite:dbname=$self->{sql_simple}->{argv}{dbfile}";

     return 0;
  }

NOTE: No more changes need.

Custom

This sample show a simple struct to be create to support your addon.

The function "PreFetch" will be call befere the "prepare" operations where specias adjusts can be done.

  ## The "Open" method must initialize the "dsname" environment.

  ## The "dsname" will be used as the first argument on the "DBI->connect"
  #
  ## rc < 0 - syntax error and abort
  ## rc = 0 - successfull and continue
  ## rc = 1 - errors and abort
  ## rc = 2 - successfull but do not make nothing

  package SQL::SimpleOps::[interface]::[driver];

  use 5.006001;
  use strict;
  use Exporter;

  our @ISA = qw ( Exporter );
  our @EXPORT = qw( Open $VERSION );
  our @EXPORT_OK = @EXPORT;
  our %EXPORT_TAGS = ( all => [@EXPORT_OK] );
  1;

  sub new()
  {
        my $class = shift; $class = ref($class) || $class || 'SQL::SimpleOps::DBI::MyPlugin';
        my $self = {@_};

        # give my plugin name
        $self->{sql_simple}->{init}{plugin_id} = "MyPlugin";

        # use '1' if your db have schema option format
        $self->{sql_simple}->{init}{schema} = 1;

        # use '1' if your args must have server/tcport values
        $self->{sql_simple}->{init}{test_server} = 1;

        # use '1' if between your field/alias infomation have 'as' value
        $self->{sql_simple}->{init}{alias_with_as} = 0;

        bless($self,$class);
  }

  sub Open()
  {
        my $self = shift;
        my $argv = shift;

        ## sets the dsnam here
        ## $self->{sql_simple}->{argv}{dsname} = ...

        return 0;
  }

  sub Select()
  {
        my $self = shift;
        my $argv = shift;
        return 0;
  }

  sub SelectCursor()
  {
        my $self = shift;
        my $argv = shift;
        return 0;
  }

  sub Delete()
  {
        my $self = shift;
        my $argv = shift;
        return 0;
  }

  sub Update()
  {
        my $self = shift;
        my $argv = shift;
        return 0;
  }

  sub Call()
  {
        my $self = shift;
        my $argv = shift;
        return 0;
  }

  sub PreFetch()
  {
        my $self = shift;
        my $argv = shift;
        return 0;
  }

PreFetch Method

This method is called before the 'prepare operations'. It's can be used to provide the last changes or checks before the SQL call.

ENDED


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.