NAME

PDK::DBI::Role - Moose role for database operations in Netdisco applications

SYNOPSIS

package MyNetdiscoDB;
use Moose;
with 'PDK::DBI::Role';

# Implement required methods
sub clone { ... }
sub batchExecute { ... }

# Use the role's methods
$self->batchInsert($columnMap, $tableName, $dataObjs);

DESCRIPTION

PDK::DBI::Role is a Moose role that provides database operation capabilities for applications. It defines common database connection attributes and methods, particularly for batch insert operations.

ATTRIBUTES

dsn

Data Source Name (DSN).

user

Database username.

password

Database password.

dbi

DBI object, lazily built.

REQUIRED METHODS

Classes using this role must implement the following methods:

clone

Creates a copy of the object.

batchExecute

Executes batch SQL operations.

METHODS

getAttrMembers($attrTypes, $dataObj)

Gets the attribute members of a data object.

Parameters: - $attrTypes: HashRef of attribute types - $dataObj: Data object

Returns: HashRef of attribute members, or in list context, returns ($attrMembers, $max, $min).

parseColumnMap($columnMap)

Parses the column map.

Parameters: - $columnMap: ArrayRef of column mappings

Returns: Three HashRefs containing single attributes, list attributes, and attribute types.

batchInsert($columnMap, $tableName, $dataObjs)

Executes a batch insert operation.

Parameters: - $columnMap: ArrayRef of column mappings - $tableName: Name of the table to insert data into - $dataObjs: HashRef or ArrayRef of data objects to insert

Returns: None.

EXAMPLES

Defining a class that uses this role

package MyNetdiscoDB;
use Moose;
with 'PDK::DBI::Role';

sub clone {
  my $self = shift;
  return __PACKAGE__->new(
    map { $_ => $self->$_ } qw(dsn user password)
  );
}

sub batchExecute {
  my ($self, $params, $sql) = @_;
  # Implement batch execution logic
}

Using the batchInsert method

my $db = MyNetdiscoDB->new(
  dsn      => 'dbi:mysql:database=Netdisco;host=localhost',
  user     => 'username',
  password => 'password'
);

my $columnMap = ['id', 'name', 'ip_addresses|@'];
my $tableName = 'Netdisco_rules';
my $dataObjs = [
  { id => 1, name => 'Rule1', ip_addresses => ['192.168.1.1', '192.168.1.2'] },
  { id => 2, name => 'Rule2', ip_addresses => ['10.0.0.1', '10.0.0.2'] }
];

$db->batchInsert($columnMap, $tableName, $dataObjs);

SEE ALSO

Moose::Role, DBI

AUTHOR

WENWU YAN <968828@gmail.com>

LICENSE AND COPYRIGHT

Copyright (C) 2024 WENWU YAN

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.