Call checkName for certain reptitive tasks

This commit is contained in:
Andy Bauer 2014-01-22 09:20:56 -06:00
parent f00e72be1e
commit 91b0676343
1 changed files with 4 additions and 29 deletions

33
scripts/zmcamtool.pl.in Executable file → Normal file
View File

@ -127,14 +127,13 @@ PARAMETERS:
} }
# Subroutine that checks if a name already exists in a sql table # Subroutine that checks if a name already exists in a sql table
# Not currently in-use b/c it requires a call to zmdbconnect each time (expensive)
sub checkName sub checkName
{ {
my $dbh = shift;
my $sqltable = shift; my $sqltable = shift;
my $sqlname = shift; my $sqlname = shift;
my $result = 0; my $result = 0;
my $dbh = zmDbConnect();
my $sql = "select count(*) from $sqltable where Name = ?"; my $sql = "select count(*) from $sqltable where Name = ?";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute($sqlname) or die( "Can't execute: ".$sth->errstr() ); my $res = $sth->execute($sqlname) or die( "Can't execute: ".$sth->errstr() );
@ -188,14 +187,7 @@ sub importsql
my $dbh = zmDbConnect(); my $dbh = zmDbConnect();
foreach (keys %controls) { foreach (keys %controls) {
my $sql = "select count(*) from Controls where Name = ?"; if (!checkName($dbh,"Controls",$_)) {
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute($_) or die( "Can't execute: ".$sth->errstr() );
my $rows = $sth->fetchrow_arrayref();
$sth->finish();
if ($rows->[0] == 0) {
# No existing Control was found. Add new control to dB. # No existing Control was found. Add new control to dB.
$sql = $controls{$_}; $sql = $controls{$_};
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
@ -219,14 +211,7 @@ sub importsql
} }
foreach (keys %monitorpresets) { foreach (keys %monitorpresets) {
my $sql = "select count(*) from MonitorPresets where Name = ?"; if (!checkName($dbh,"MonitorPresets",$_)) {
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute($_) or die( "Can't execute: ".$sth->errstr() );
my $rows = $sth->fetchrow_arrayref();
$sth->finish();
if ($rows->[0] == 0) {
# No existing MonitorPreset was found. Add new MonitorPreset to dB. # No existing MonitorPreset was found. Add new MonitorPreset to dB.
$sql = $monitorpresets{$_}; $sql = $monitorpresets{$_};
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
@ -324,17 +309,7 @@ sub toPreset
# TO-DO - Add code that tries to intelligently replace certain values with generic ones e.g. <ip>, <user>, <pwd>, etc. # TO-DO - Add code that tries to intelligently replace certain values with generic ones e.g. <ip>, <user>, <pwd>, etc.
# #
# Check that we don't already have a record with the same name if (!checkName($dbh,"MonitorPresets",$data[0])) {
# my $nameexists = checkName("MonitorPresets",$data[0]);
$sql = "select count(*) from MonitorPresets where Name = ?";
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute($data[0]) or die( "Can't execute: ".$sth->errstr() );
my $rows = $sth->fetchrow_arrayref();
$sth->finish();
if ($rows->[0] == 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";
$sql = "insert into MonitorPresets values (NULL,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; $sql = "insert into MonitorPresets values (NULL,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";