Re: create and insert
[prev]
[thread]
[next]
[Date index for 2005/01/21]
Hartmaier Alexander wrote:
> Thanks for the answers!
>
> It seems I haven't specified everything good enough.
>
> This is my object:
>
> package NAC::Interface;
> use strict;
> use warnings;
> use nactools;
> use base 'NAC::DBI';
> __PACKAGE__->table($dbtools::schemaname . '.interface');
> __PACKAGE__->sequence('seq_interface');
> __PACKAGE__->columns(Primary => qw/id_interface/);
> __PACKAGE__->columns(Essential => qw/fk_device fk_line fk_monitor fk_division fk_servicetime port name snmpid snmpid_rtt type speed mtu ipaddr ipnetmask description/);
> __PACKAGE__->columns(Others => qw/startofservice user_speed bandwidth_override bandwidth_computed offerbw_nonob offerbw_ob bandwidth_comment xfervol_computed exclude_time compu
> ted_date computed_from computed_to max_downtime/);
> # needed for down/notreachable interfaces
> __PACKAGE__->columns(TEMP => qw/datetime_last/);
>
> __PACKAGE__->has_a(fk_device => 'NAC::Device');
> __PACKAGE__->has_a(fk_line => 'NAC::Line');
> __PACKAGE__->has_a(fk_division => 'NAC::Division');
> __PACKAGE__->has_a(fk_servicetime => 'NAC::Servicetime');
>
> __PACKAGE__->set_sql('down', 'SELECT * FROM nacadm.view_down_interfaces');
> __PACKAGE__->set_sql('notreach', 'SELECT * FROM nacadm.view_notreach_interfaces');
> __PACKAGE__->set_sql('mod', 'SELECT * FROM nacadm.view_mod_interfaces');
>
> __PACKAGE__->set_sql('from_scan', 'SELECT fk_device, port, name, snmpid, snmpid_rtt, type, speed, mtu, ipaddr, ipnetmask, description FROM nacadm.scaninterface WHERE port = ?');
>
> As you can see interfaces are saved in the table 'interface'.
> I have an app which scans devices for new interfaces and inserts them into the table 'scaninterface'. This table hast almost the same field as table 'interface' but uses the 'port' field as primary/unique key.
>
> I have a webpage which shows the difference between the two tables (new interfaces and a second which shows obsolete interfaces, which aren't found any more).
>
> What I do is the following:
>
> # read interface data from scaninterface (this works and NO primary key is generated, so it seems this is a bug...)
> ($obj_new_interface) = NAC::Interface->search_from_scan($newport);
I'm not exactly sure how to explain this, but I do NOT think you are
using the set_sql in the proper way (someone will correct me if I'm
wrong). You are retreiving an NAC::Interface object, but short
circuiting it by actually getting data from the "scaninterface" table.
So there is no "id_interface" because the data didn't come from a table
with "id_interface". If you commit this data, my guess is your sequence
will generate a "id_interface" value for you.
What I really think you want to do is either create a NAC::Scaninterface
class, and access it via class::dbi. Or just create your own $sth
within this code that reads the data out of scaninterface, and then
->create a NAC::Interface populating it with the data from scaninterface.
> # set the monitor value (if the interface should be monitored or not)
> $obj_new_interface->monitor($monitor);
> # lookup matching NAC::Line object and save it's primary key in the 'line' object variable
> $obj_new_interface->trig_line;
I don't know where this "trig_line" comes from, I don't see it as a
column or a method in the above code. My guess you are somehow really
trying to access the ->fk_line.
> Should I create a new object class for the scaninterface table? Maybe even without the use of Class::DBI?
> I can't immediately create the NAC::Interface object out of the scaninterface data because I have to check for a matching NAC::Line object first if it doesn't exist (by displaying a message and a link which calls a create line webpage with pre-filled values).
>
> -Alex
Yes I think you should create a NAC::ScanInterface class. And in that
class you'd set up the relationship to the NAC::Line object. That way
you could:
@scan = NAC::ScanInterface->search(%some_search_criteria);
foreach my $scan (@scan) {
# code to locate the line
# maybe you can setup a foreign key from scaninterface to line
# or maybe it's programmatic
my $interface = NAC::Interface->find_or_create({fk_line=>$scan->fkline,
...});
$interface->monitor($scan->monitor);
}
|
(message missing)
|