<?xml version="1.0" encoding="windows-1252"?>
<node id="279149" title="Re: Class::DBI Intro" created="2003-07-30 08:16:48" updated="2005-08-11 02:48:56">
<type id="11">
note</type>
<author id="275605">
dbwiz</author>
<data>
<field name="doctext">
&lt;p&gt;A few comments.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
In your connection to DBI, you are testing "$!". Be aware that this won't work as you expect. See the relevant info from DBI docs.&lt;br&gt;
&lt;blockquote&gt;&lt;i&gt;
If the connect fails (see below), it returns "undef" and sets both "$DBI::err" and "$DBI::errstr". (&lt;u&gt;It does not set "$!"&lt;/u&gt;, etc.) You should generally test the return status of "connect" and "print $DBI::errstr" if it has failed. &lt;/i&gt;&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;i&gt;delete&lt;/i&gt; method is rather inefficient. When called, Class::DBI will issue one DELETE statement for each row in your table. Instead of producing&lt;br&gt;
&lt;code&gt;
DELETE FROM page WHERE user_id = 1
DELETE FROM user WHERE user_id = 1
&lt;/code&gt;

It does &lt;br&gt;
&lt;code&gt;
DELETE FROM   page WHERE  page_id = '1'
DELETE FROM   page WHERE  page_id = '2'
DELETE FROM   page WHERE  page_id = '3'
DELETE FROM   page WHERE  page_id = '4'
DELETE FROM   page WHERE  page_id = '5'
DELETE FROM   user WHERE  user_id = '1'
&lt;/code&gt;
which can be quite long for large data sets.&lt;br&gt;
The sad thing is that, even if I use a database that enforces referential integrity, Class::DBI will still override the database features and issue the same DELETE statements. In your example, if you replace the table definition to use the InnoDB table type, adding &lt;br&gt;
&lt;code&gt;
FOREIGN KEY (user_id) references user (user_id) ON DELETE CASCADE,
KEY user_id (user_id)
# and replace "MyISAM" with "InnoDB" for both tables
&lt;/code&gt;
&lt;p&gt;You can then get the same behavior with &lt;/p&gt;
&lt;code&gt;%dbh-&gt;do("DELETE FROM user WHERE user_id=1")&lt;/code&gt;
&lt;p&gt;and the database engine will take care of cascade deletiing the appropriate records in "page".&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;You said nothing about how DBI::Class handles queries with one or more JOIN, which are quite common in a production environment. This article would be much better if you explained how it works. Without it, it seems no more than an interesting toy. &lt;/li&gt;
&lt;/ul&gt;
</field>
<field name="root_node">
279077</field>
<field name="parent_node">
279077</field>
</data>
</node>
