AW: create and insert

[prev] [thread] [next] [Date index for 2005/01/21]

From: Hartmaier Alexander
Subject: AW: create and insert
Date: 15:57 on 21 Jan 2005
Thanks Jay!

Looks good...

I didn't paste all of the NAC::Interface methods i defined...here is the =
trig_line (I wanted to use a trigger first but found out later that the s=
earch_from_scan() doesn't trigger any trigger....)

sub trig_line {
  my ($self) =3D @_;
  my $dbh =3D nactools::connectdbnacweb();

  if (not defined $self->line)
  {
    my @ary_description =3D split(/\s/, $self->description, 4);
    # workaround for LAN interfaces which have no provider
    if ($ary_description[0] =3D~ /^LAN/ )
    {
      # LAN interfaces have no provider
      unshift (@ary_description, '');
      # and no speed
      splice (@ary_description, 3, 0, '');
    }
    # all others have no location=20
    else
    {
      splice (@ary_description, 2, 0, '');
    }
    #$obj_line->name         ($ary_description[1]);
    #$obj_line->location     ($ary_description[2]);
    #$obj_line->speed        ($ary_description[3]);
    #$obj_line->description  ($ary_description[4]);
    #$obj_line->fk_provider  (get_provider ($dbh, $ary_description[0]));

    $self->line(NAC::Line->sql_find_line->select_val($ary_description[1],=
 $ary_description[2]));
  }
}

This method looks for a matching NAC::Line object for the new NAC::Interf=
ace object from the 'scaninterface' table and assigns the foreign key.

I think the 'cleanest' way to do it is to define a new NAC::ScanInterface=
 object which then hast the trig_line method (cause the NAC::Interface cl=
ass doesn't need it then) and write that class strictly for use of genera=
ting new NAC::Interface objects (with a copy or something like that).
Now the questing is: use Class::DBI or not? Can a string be used as prima=
ry key? I'd define the 'port' column in the 'scaninterface' table as pri.=
 key if possible (then a ->retrieve would do what it should do ;-).

A last question ;-): NAC::ScanInterface or NAC::Scaninterface (uppercase =
or lowercase 'i')??? Is there an oo 'standard' when to use upper or lower=
case?
Sorry for my newbee questions but I AM a newbie in OO ;-| (not in perl!!!=
!)

Thanks guys!
-Alex

-----Urspr=FCngliche Nachricht-----
Von: Jay Strauss [mailto:me@xxxxxx.xxx]=20
Gesendet: Freitag, 21. Januar 2005 16:41
An: Hartmaier Alexander
Cc: cdbi-talk@xxxxxx.xxxxx.xxx
Betreff: Re: create and insert

Hartmaier Alexander wrote:
> Thanks for the answers!
>=20
> It seems I haven't specified everything good enough.
>=20
> This is my object:
>=20
> 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   =3D> qw/id_interface/);
> __PACKAGE__->columns(Essential =3D> 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    =3D> qw/startofservice user_speed bandwi=
dth_override bandwidth_computed offerbw_nonob offerbw_ob bandwidth_commen=
t xfervol_computed exclude_time compu
> ted_date computed_from computed_to max_downtime/);
> # needed for down/notreachable interfaces
> __PACKAGE__->columns(TEMP      =3D> qw/datetime_last/);
>=20
> __PACKAGE__->has_a(fk_device      =3D> 'NAC::Device');
> __PACKAGE__->has_a(fk_line        =3D> 'NAC::Line');
> __PACKAGE__->has_a(fk_division    =3D> 'NAC::Division');
> __PACKAGE__->has_a(fk_servicetime =3D> 'NAC::Servicetime');
>=20
> __PACKAGE__->set_sql('down', 'SELECT * FROM nacadm.view_down_interfaces=
');
> __PACKAGE__->set_sql('notreach', 'SELECT * FROM nacadm.view_notreach_in=
terfaces');
> __PACKAGE__->set_sql('mod', 'SELECT * FROM nacadm.view_mod_interfaces')=
;
>=20
> __PACKAGE__->set_sql('from_scan', 'SELECT fk_device, port, name, snmpid=
, snmpid_rtt, type, speed, mtu, ipaddr, ipnetmask, description FROM nacad=
m.scaninterface WHERE port =3D ?');
>=20
> As you can see interfaces are saved in the table 'interface'.
> I have an app which scans devices for new interfaces and inserts them i=
nto the table 'scaninterface'. This table hast almost the same field as t=
able 'interface' but uses the 'port' field as primary/unique key.
>=20
> I have a webpage which shows the difference between the two tables (new=
 interfaces and a second which shows obsolete interfaces, which aren't fo=
und any more).
>=20
> What I do is the following:
>=20
> # read interface data from scaninterface (this works and NO primary key=
 is generated, so it seems this is a bug...)
> ($obj_new_interface) =3D NAC::Interface->search_from_scan($newport);

I'm not exactly sure how to explain this, but I do NOT think you are=20
using the set_sql in the proper way (someone will correct me if I'm=20
wrong).  You are retreiving an NAC::Interface object, but short=20
circuiting it by actually getting data from the "scaninterface" table.=20=

So there is no "id_interface" because the data didn't come from a table=20=

with "id_interface".  If you commit this data, my guess is your sequence=20=

will generate a "id_interface" value for you.

What I really think you want to do is either create a NAC::Scaninterface=20=

  class, and access it via class::dbi.  Or just create your own $sth=20
within this code that reads the data out of scaninterface, and then=20
->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 'li=
ne' object variable
> $obj_new_interface->trig_line;

I don't know where this "trig_line" comes from, I don't see it as a=20
column or a method in the above code.  My guess you are somehow really=20=

trying to access the ->fk_line.

> Should I create a new object class for the scaninterface table? Maybe e=
ven without the use of Class::DBI?
> I can't immediately create the NAC::Interface object out of the scanint=
erface 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 cr=
eate line webpage with pre-filled values).
>=20
> -Alex

Yes I think you should create a NAC::ScanInterface class.  And in that=20=

class you'd set up the relationship to the NAC::Line object.  That way=20=

you could:

@scan =3D 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 =3D NAC::Interface->find_or_create({fk_line=3D>$scan->fkli=
ne,=20
=2E..});

	$interface->monitor($scan->monitor);
}


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*"*
Hinweis: Dieses E-mail kann vertrauliche und gesch=FCtzte Informationen e=
nthalten.
Sollten Sie nicht der beabsichtigte Empf=E4nger sein, verst=E4ndigen Sie =
bitte den Absender und l=F6schen Sie dieses E-mail dann sofort.

Notice: This e-mail contains information that is confidential and may be =
privileged.
If you are not the intended recipient, please notify the sender and then =
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*"*

(message missing)

AW: create and insert
Hartmaier Alexander 16:45 on 20 Jan 2005

AW: create and insert
Hartmaier Alexander 15:57 on 21 Jan 2005

Re: AW: create and insert
Jay Strauss 16:45 on 21 Jan 2005

Generated at 17:42 on 27 Jan 2005 by mariachi v0.52