fix spacing, braces, quotes
This commit is contained in:
parent
a9ebcd6e8a
commit
c55814647c
|
@ -55,6 +55,7 @@ a sql file, which can then be easily imported to another zoneminder system.
|
||||||
--help - Print usage information.
|
--help - Print usage information.
|
||||||
--user=<dbuser> - Alternate dB user with privileges to alter dB.
|
--user=<dbuser> - Alternate dB user with privileges to alter dB.
|
||||||
--pass=<dbpass> - Password of alternate dB user with privileges to alter dB.
|
--pass=<dbpass> - Password of alternate dB user with privileges to alter dB.
|
||||||
|
--version - Print version.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
use strict;
|
use strict;
|
||||||
|
@ -149,8 +150,7 @@ if ($topreset) {
|
||||||
###############
|
###############
|
||||||
|
|
||||||
# Execute a pre-built sql select query
|
# Execute a pre-built sql select query
|
||||||
sub selectQuery
|
sub selectQuery {
|
||||||
{
|
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $sql = shift;
|
my $sql = shift;
|
||||||
my $monitorid = shift;
|
my $monitorid = shift;
|
||||||
|
@ -167,8 +167,7 @@ sub selectQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
# Exectute a pre-built sql query
|
# Exectute a pre-built sql query
|
||||||
sub runQuery
|
sub runQuery {
|
||||||
{
|
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $sql = shift;
|
my $sql = shift;
|
||||||
my $sth = $dbh->prepare_cached( $sql )
|
my $sth = $dbh->prepare_cached( $sql )
|
||||||
|
@ -181,14 +180,13 @@ sub runQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
# Build and execute a sql insert query
|
# Build and execute a sql insert query
|
||||||
sub insertQuery
|
sub insertQuery {
|
||||||
{
|
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $tablename = shift;
|
my $tablename = shift;
|
||||||
my @data = @_;
|
my @data = @_;
|
||||||
|
|
||||||
my $sql = "INSERT INTO $tablename VALUES (NULL,"
|
my $sql = "INSERT INTO $tablename VALUES (NULL,"
|
||||||
.(join ", ", ("?") x @data).")"; # Add "?" for each array element
|
.(join ', ', ('?') x @data).')'; # Add "?" for each array element
|
||||||
|
|
||||||
my $sth = $dbh->prepare_cached( $sql )
|
my $sth = $dbh->prepare_cached( $sql )
|
||||||
or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
||||||
|
@ -200,8 +198,7 @@ sub insertQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
# Build and execute a sql delete query
|
# Build and execute a sql delete query
|
||||||
sub deleteQuery
|
sub deleteQuery {
|
||||||
{
|
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $sqltable = shift;
|
my $sqltable = shift;
|
||||||
my $sqlname = shift;
|
my $sqlname = shift;
|
||||||
|
@ -217,8 +214,7 @@ sub deleteQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
# Build and execute a sql select count query
|
# Build and execute a sql select count query
|
||||||
sub checkExists
|
sub checkExists {
|
||||||
{
|
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $sqltable = shift;
|
my $sqltable = shift;
|
||||||
my $sqlname = shift;
|
my $sqlname = shift;
|
||||||
|
@ -241,8 +237,7 @@ sub checkExists
|
||||||
}
|
}
|
||||||
|
|
||||||
# Import camera control & presets into the zoneminder dB
|
# Import camera control & presets into the zoneminder dB
|
||||||
sub importsql
|
sub importsql {
|
||||||
{
|
|
||||||
my @newcontrols;
|
my @newcontrols;
|
||||||
my @overwritecontrols;
|
my @overwritecontrols;
|
||||||
my @skippedcontrols;
|
my @skippedcontrols;
|
||||||
|
@ -258,7 +253,7 @@ sub importsql
|
||||||
$sqlfile = $Config{ZM_PATH_DATA}.'/db/zm_create.sql';
|
$sqlfile = $Config{ZM_PATH_DATA}.'/db/zm_create.sql';
|
||||||
}
|
}
|
||||||
|
|
||||||
open(my $SQLFILE,"<",$sqlfile)
|
open(my $SQLFILE,'<',$sqlfile)
|
||||||
or die( "Can't Open file: $!\n" );
|
or die( "Can't Open file: $!\n" );
|
||||||
|
|
||||||
# Find and extract ptz control and monitor preset records
|
# Find and extract ptz control and monitor preset records
|
||||||
|
@ -281,14 +276,14 @@ sub importsql
|
||||||
|
|
||||||
my $dbh = zmDbConnect();
|
my $dbh = zmDbConnect();
|
||||||
foreach (keys %controls) {
|
foreach (keys %controls) {
|
||||||
if (!checkExists($dbh,"Controls",$_)) {
|
if (!checkExists($dbh,'Controls',$_)) {
|
||||||
# No existing Control was found. Add new control to dB.
|
# No existing Control was found. Add new control to dB.
|
||||||
runQuery($dbh,$controls{$_});
|
runQuery($dbh,$controls{$_});
|
||||||
push @newcontrols, $_;
|
push @newcontrols, $_;
|
||||||
} elsif ($overwrite) {
|
} elsif ($overwrite) {
|
||||||
# An existing Control was found and the overwrite flag is set.
|
# An existing Control was found and the overwrite flag is set.
|
||||||
# Overwrite the control.
|
# Overwrite the control.
|
||||||
deleteQuery($dbh,"Controls",$_);
|
deleteQuery($dbh,'Controls',$_);
|
||||||
runQuery($dbh,$controls{$_});
|
runQuery($dbh,$controls{$_});
|
||||||
push @overwritecontrols, $_;
|
push @overwritecontrols, $_;
|
||||||
} else {
|
} else {
|
||||||
|
@ -299,14 +294,14 @@ sub importsql
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (keys %monitorpresets) {
|
foreach (keys %monitorpresets) {
|
||||||
if (!checkExists($dbh,"MonitorPresets",$_)) {
|
if (!checkExists($dbh,'MonitorPresets',$_)) {
|
||||||
# No existing MonitorPreset was found. Add new MonitorPreset to dB.
|
# No existing MonitorPreset was found. Add new MonitorPreset to dB.
|
||||||
runQuery($dbh,$monitorpresets{$_});
|
runQuery($dbh,$monitorpresets{$_});
|
||||||
push @newpresets, $_;
|
push @newpresets, $_;
|
||||||
} elsif ($overwrite) {
|
} elsif ($overwrite) {
|
||||||
# An existing MonitorPreset was found and the overwrite flag is set.
|
# An existing MonitorPreset was found and the overwrite flag is set.
|
||||||
# Overwrite the MonitorPreset.
|
# Overwrite the MonitorPreset.
|
||||||
deleteQuery($dbh,"MonitorPresets",$_);
|
deleteQuery($dbh,'MonitorPresets',$_);
|
||||||
runQuery($dbh,$monitorpresets{$_});
|
runQuery($dbh,$monitorpresets{$_});
|
||||||
push @overwritepresets, $_;
|
push @overwritepresets, $_;
|
||||||
} else {
|
} else {
|
||||||
|
@ -317,43 +312,42 @@ sub importsql
|
||||||
}
|
}
|
||||||
|
|
||||||
if (@newcontrols) {
|
if (@newcontrols) {
|
||||||
print "Number of ptz camera controls added: "
|
print 'Number of ptz camera controls added: '
|
||||||
.scalar(@newcontrols)."\n";
|
.scalar(@newcontrols)."\n";
|
||||||
}
|
}
|
||||||
if (@overwritecontrols) {
|
if (@overwritecontrols) {
|
||||||
print "Number of existing ptz camera controls overwritten: "
|
print 'Number of existing ptz camera controls overwritten: '
|
||||||
.scalar(@overwritecontrols)."\n";
|
.scalar(@overwritecontrols)."\n";
|
||||||
}
|
}
|
||||||
if (@skippedcontrols) {
|
if (@skippedcontrols) {
|
||||||
print "Number of existing ptz camera controls skipped: "
|
print 'Number of existing ptz camera controls skipped: '
|
||||||
.scalar(@skippedcontrols)."\n";
|
.scalar(@skippedcontrols)."\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (@newpresets) {
|
if (@newpresets) {
|
||||||
print "Number of monitor presets added: "
|
print 'Number of monitor presets added: '
|
||||||
.scalar(@newpresets)."\n";
|
.scalar(@newpresets)."\n";
|
||||||
}
|
}
|
||||||
if (@overwritepresets) {
|
if (@overwritepresets) {
|
||||||
print "Number of existing monitor presets overwritten: "
|
print 'Number of existing monitor presets overwritten: '
|
||||||
.scalar(@overwritepresets)."\n";
|
.scalar(@overwritepresets)."\n";
|
||||||
}
|
}
|
||||||
if (@skippedpresets) {
|
if (@skippedpresets) {
|
||||||
print "Number of existing presets skipped: "
|
print 'Number of existing presets skipped: '
|
||||||
.scalar(@skippedpresets)."\n";
|
.scalar(@skippedpresets)."\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Export camera controls & presets from the zoneminder dB to STDOUT
|
# Export camera controls & presets from the zoneminder dB to STDOUT
|
||||||
sub exportsql
|
sub exportsql {
|
||||||
{
|
|
||||||
|
|
||||||
my ( $host, $port ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ );
|
my ( $host, $port ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ );
|
||||||
my $command = "mysqldump -t --skip-opt --compact -h".$host;
|
my $command = 'mysqldump -t --skip-opt --compact -h'.$host;
|
||||||
$command .= " -P".$port if defined($port);
|
$command .= ' -P'.$port if defined($port);
|
||||||
if ( $dbUser ) {
|
if ( $dbUser ) {
|
||||||
$command .= " -u".$dbUser;
|
$command .= ' -u'.$dbUser;
|
||||||
if ( $dbPass ) {
|
if ( $dbPass ) {
|
||||||
$command .= " -p".$dbPass;
|
$command .= ' -p'.$dbPass;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,13 +372,12 @@ if ( $status ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub toPreset
|
sub toPreset {
|
||||||
{
|
|
||||||
my $dbh = zmDbConnect();
|
my $dbh = zmDbConnect();
|
||||||
my $monitorid = $ARGV[0];
|
my $monitorid = $ARGV[0];
|
||||||
|
|
||||||
# Grap the following fields from the Monitors table
|
# Grap the following fields from the Monitors table
|
||||||
my $sql = "SELECT
|
my $sql = 'SELECT
|
||||||
Name,
|
Name,
|
||||||
Type,
|
Type,
|
||||||
Device,
|
Device,
|
||||||
|
@ -406,7 +399,7 @@ sub toPreset
|
||||||
ControlAddress,
|
ControlAddress,
|
||||||
DefaultRate,
|
DefaultRate,
|
||||||
DefaultScale
|
DefaultScale
|
||||||
FROM Monitors WHERE Id = ?";
|
FROM Monitors WHERE Id = ?';
|
||||||
my @data = selectQuery($dbh,$sql,$monitorid);
|
my @data = selectQuery($dbh,$sql,$monitorid);
|
||||||
|
|
||||||
if (!@data) {
|
if (!@data) {
|
||||||
|
@ -431,13 +424,13 @@ sub toPreset
|
||||||
if (!checkExists($dbh,"MonitorPresets",$data[0])) {
|
if (!checkExists($dbh,"MonitorPresets",$data[0])) {
|
||||||
# No existing Preset was found. Add new Preset to dB.
|
# No existing Preset was found. Add new Preset to dB.
|
||||||
print "Adding new preset: $data[0]\n";
|
print "Adding new preset: $data[0]\n";
|
||||||
insertQuery($dbh,"MonitorPresets",@data);
|
insertQuery($dbh,'MonitorPresets',@data);
|
||||||
} elsif ($overwrite) {
|
} elsif ($overwrite) {
|
||||||
# An existing Control was found and the overwrite flag is set.
|
# An existing Control was found and the overwrite flag is set.
|
||||||
# Overwrite the control.
|
# Overwrite the control.
|
||||||
print "Existing preset $data[0] detected.\nOverwriting...\n";
|
print "Existing preset $data[0] detected.\nOverwriting...\n";
|
||||||
deleteQuery($dbh,"MonitorPresets",$data[0]);
|
deleteQuery($dbh,'MonitorPresets',$data[0]);
|
||||||
insertQuery($dbh,"MonitorPresets",@data);
|
insertQuery($dbh,'MonitorPresets',@data);
|
||||||
} else {
|
} else {
|
||||||
# An existing Control was found and the overwrite flag was not set.
|
# An existing Control was found and the overwrite flag was not set.
|
||||||
# Do nothing.
|
# Do nothing.
|
||||||
|
@ -445,3 +438,5 @@ sub toPreset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1;
|
||||||
|
__END__
|
||||||
|
|
Loading…
Reference in New Issue