<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacLochlainns Weblog &#187; Objects</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/category/objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 00:02:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MySQL Foreign Keys</title>
		<link>http://blog.mclaughlinsoftware.com/2009/09/26/mysql-foreign-keys/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/09/26/mysql-foreign-keys/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 22:48:02 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3046</guid>
		<description><![CDATA[One of my students asked how you validate the foreign keys in a MySQL database. First off, this only works if the database engine supports referential integrity (the fancy word for foreign keys as database level constraints). InnoDB and Falcon support referential integrity. The answer can be found by leveraging the data catalog in the [...]]]></description>
			<content:encoded><![CDATA[<p>One of my students asked how you validate the foreign keys in a MySQL database. First  off, this only works if the database engine supports referential integrity (the fancy word for foreign keys as database level constraints). InnoDB and Falcon support referential integrity. The answer can be found by leveraging the data catalog in the <CODE>INFORMATION_SCHEMA</code>.</p>
<p>Here's the query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>   CONCAT<span style="color: #66cc66;">&#40;</span>tc<span style="color: #66cc66;">.</span>table_schema<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span>tc<span style="color: #66cc66;">.</span>table_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span>tc<span style="color: #66cc66;">.</span>constraint_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Constraint&quot;</span>
<span style="color: #66cc66;">,</span>        CONCAT<span style="color: #66cc66;">&#40;</span>kcu<span style="color: #66cc66;">.</span>table_schema<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span>kcu<span style="color: #66cc66;">.</span>table_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span>kcu<span style="color: #66cc66;">.</span>column_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Foreign Key&quot;</span>
<span style="color: #66cc66;">,</span>        CONCAT<span style="color: #66cc66;">&#40;</span>kcu<span style="color: #66cc66;">.</span>referenced_table_schema<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span>kcu<span style="color: #66cc66;">.</span>referenced_table_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span>kcu<span style="color: #66cc66;">.</span>referenced_column_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Primary Key&quot;</span>
<span style="color: #993333; font-weight: bold;">FROM</span>     information_schema<span style="color: #66cc66;">.</span>table_constraints tc <span style="color: #993333; font-weight: bold;">JOIN</span> information_schema<span style="color: #66cc66;">.</span>key_column_usage kcu
<span style="color: #993333; font-weight: bold;">ON</span>       tc<span style="color: #66cc66;">.</span>constraint_name <span style="color: #66cc66;">=</span> kcu<span style="color: #66cc66;">.</span>constraint_name
<span style="color: #993333; font-weight: bold;">WHERE</span>    tc<span style="color: #66cc66;">.</span>constraint_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'FOREIGN KEY'</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> tc<span style="color: #66cc66;">.</span>table_name
<span style="color: #66cc66;">,</span>        kcu<span style="color: #66cc66;">.</span>column_name;</pre></div></div>

<p>It's a bit different then the <code>USER_CONSTRAINTS</code> and <code>USER_CONS_COLUMNS</code> views in Oracle, which are covered in <a href="http://blog.mclaughlinsoftware.com/2009/03/05/validating-foreign-keys/">this prior post</a>.</p>
<p>I recently ran across another interesting detail on MySQL foreign keys creation and removal that has to do with the case sensitivity of constraints. If you create the constraint in lower case and then try to drop the foreign key constraint in upper case, you'll encounter the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">ERROR 1025 (HY000): Error on rename of '.\database_name\table_name' to '.\database_name\#sql2-79c-1' (errno: 152)</pre></div></div>

<p>The reason appears to be that MySQL can't find the table with the constraint name, and therefore throws an error that appears related to failure writing the new structure. It can be misleading.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/09/26/mysql-foreign-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Watch the Event Logs</title>
		<link>http://blog.mclaughlinsoftware.com/2009/07/15/watch-the-event-logs/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/07/15/watch-the-event-logs/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:46:20 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Microsoft Vista]]></category>
		<category><![CDATA[Microsoft XP]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2928</guid>
		<description><![CDATA[It&#8217;s the end of our Spring term, and yes occasionally somebody can&#8217;t sign on to their Oracle instance because their event log is full. They get the following message on Winodws: C:\&#62;sqlplus sys AS sysdba &#160; SQL*Plus: Release 11.1.0.7.0 - Production ON Wed Jul 15 10:19:37 2009 &#160; Copyright &#40;c&#41; 1982, 2008, Oracle. ALL rights [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s the end of our Spring term, and yes occasionally somebody can&#8217;t sign on to their Oracle instance because their event log is full. They get the following message on Winodws:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">C:\<span style="color: #66cc66;">&gt;</span>sqlplus sys <span style="color: #993333; font-weight: bold;">AS</span> sysdba
&nbsp;
SQL<span style="color: #66cc66;">*</span>Plus: Release 11<span style="color: #66cc66;">.</span>1<span style="color: #66cc66;">.</span>0<span style="color: #66cc66;">.</span>7<span style="color: #66cc66;">.</span>0 <span style="color: #66cc66;">-</span> Production <span style="color: #993333; font-weight: bold;">ON</span> Wed Jul <span style="color: #cc66cc;">15</span> <span style="color: #cc66cc;">10</span>:<span style="color: #cc66cc;">19</span>:<span style="color: #cc66cc;">37</span> <span style="color: #cc66cc;">2009</span>
&nbsp;
Copyright <span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1982</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2008</span><span style="color: #66cc66;">,</span> Oracle<span style="color: #66cc66;">.</span>  <span style="color: #993333; font-weight: bold;">ALL</span> rights reserved<span style="color: #66cc66;">.</span>
&nbsp;
Enter password:
ERROR:
ORA<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">28056</span>: Writing audit records <span style="color: #993333; font-weight: bold;">TO</span> Windows Event Log failed</pre></div></div>

<p>The fix is simple, just delete your items from your Windows&#8217; event log. <img src='http://blog.mclaughlinsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/07/15/watch-the-event-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>External Table Query Fix</title>
		<link>http://blog.mclaughlinsoftware.com/2009/07/11/external-table-query-fix/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/07/11/external-table-query-fix/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 01:42:38 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2897</guid>
		<description><![CDATA[The fact that you could raise an ugly error when you query an external table always bothered me. I looked at Java stored procedures as the best solution initially. That was overkill. This afternoon, while writing about them for the new PL/SQL Workboook, it became clear. The fix is really easy. If you know little [...]]]></description>
			<content:encoded><![CDATA[<p>The fact that you could raise an ugly error when you query an external table always bothered me. I looked at Java stored procedures as the best solution initially. That was overkill. This afternoon, while writing about them for the new PL/SQL Workboook, it became clear. The fix is really easy.</p>
<p>If you know little to nothing about external tables, you can go read <a href="http://blog.mclaughlinsoftware.com/oracle-sql-programming/creating-an-external-table-that-uses-sqlloader/">this earlier post</a>. Likewise, if you don&#8217;t know about objects and object collection, you can <a href="http://blog.mclaughlinsoftware.com/2009/03/23/object-record-collections/">refer to this post</a>. Having provided you with the context, here&#8217;s an example that eliminates errors when querying an external table without an external file.</p>
<ol start="1">
<li>Create an external file, like this <code>character</code> table.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> character
<span style="color: #66cc66;">&#40;</span> character_id NUMBER
<span style="color: #66cc66;">,</span> first_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> last_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  ORGANIZATION EXTERNAL
  <span style="color: #66cc66;">&#40;</span> TYPE oracle_loader
    <span style="color: #993333; font-weight: bold;">DEFAULT</span> DIRECTORY download
    ACCESS PARAMETERS
    <span style="color: #66cc66;">&#40;</span> RECORDS DELIMITED <span style="color: #993333; font-weight: bold;">BY</span> NEWLINE CHARACTERSET US7ASCII
      BADFILE     <span style="color: #ff0000;">'DOWNLOAD'</span>:<span style="color: #ff0000;">'character.bad'</span>
      DISCARDFILE <span style="color: #ff0000;">'DOWNLOAD'</span>:<span style="color: #ff0000;">'character.dis'</span>
      LOGFILE     <span style="color: #ff0000;">'DOWNLOAD'</span>:<span style="color: #ff0000;">'character.log'</span>
      <span style="color: #993333; font-weight: bold;">FIELDS</span> TERMINATED <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">','</span>
      <span style="color: #993333; font-weight: bold;">OPTIONALLY</span> <span style="color: #993333; font-weight: bold;">ENCLOSED</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">&quot;'&quot;</span>
      MISSING <span style="color: #993333; font-weight: bold;">FIELD</span> <span style="color: #993333; font-weight: bold;">VALUES</span> ARE <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">&#41;</span>
    LOCATION <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'character.csv'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
REJECT <span style="color: #993333; font-weight: bold;">LIMIT</span> UNLIMITED;</pre></div></div>

</div>
<ol start="2">
<li>Create a user-defined object type that mirrors your external table defintion, like this:</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE character_obj <span style="color: #993333; font-weight: bold;">IS</span> OBJECT
<span style="color: #66cc66;">&#40;</span> character_id NUMBER
<span style="color: #66cc66;">,</span> first_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> last_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">/</span></pre></div></div>

</div>
<ol start="3">
<li>Create a user-defined collection of your object type, like</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE character_obj_table <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF character_obj;
<span style="color: #66cc66;">/</span></pre></div></div>

</div>
<ol start="4">
<li>Create a function that returns the user-defined collection of your object type, like</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> character_source
<span style="color: #993333; font-weight: bold;">RETURN</span> character_obj_table <span style="color: #993333; font-weight: bold;">IS</span>
  c          NUMBER;
  collection CHARACTER_OBJ_TABLE :<span style="color: #66cc66;">=</span> character_obj_table<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
BEGIN
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> character<span style="color: #66cc66;">&#41;</span> LOOP
    collection<span style="color: #66cc66;">.</span>EXTEND;
    collection<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> character_obj<span style="color: #66cc66;">&#40;</span> i<span style="color: #66cc66;">.</span>character_id
                                  <span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>first_name
                                  <span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">&#41;</span>;
    c :<span style="color: #66cc66;">=</span> c <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
  END LOOP;
  <span style="color: #993333; font-weight: bold;">RETURN</span> collection;
EXCEPTION
  WHEN OTHERS THEN
    <span style="color: #993333; font-weight: bold;">RETURN</span> collection;
END;
<span style="color: #66cc66;">/</span></pre></div></div>

</div>
<ol start="5">
<li>Query the function not the table, which returns no rows found when the file doesn&#8217;t physically exist, or the file contains no data. Lastly, the function returns the data when it is there.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#40;</span>character_source<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</div>
<p>Hope this helps those using external tables to avoid the typical error stack:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> character
<span style="color: #66cc66;">*</span>
ERROR at line <span style="color: #cc66cc;">1</span>:
ORA<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">29913</span>: error <span style="color: #993333; font-weight: bold;">IN</span> executing ODCIEXTTABLEOPEN callout
ORA<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">29400</span>: <span style="color: #993333; font-weight: bold;">DATA</span> cartridge error
KUP<span style="color: #66cc66;">-</span>04040: file character<span style="color: #66cc66;">.</span>csv <span style="color: #993333; font-weight: bold;">IN</span> CHARACTER <span style="color: #993333; font-weight: bold;">NOT</span> found</pre></div></div>

<p>I also wrote this older post about <a href="http://blog.mclaughlinsoftware.com/plsql-programming/how-to-guarantee-an-external-file-before-querying/">confirming it in the database catalog</a>. If you any follow-up suggestions, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/07/11/external-table-query-fix/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Object constructor quirk</title>
		<link>http://blog.mclaughlinsoftware.com/2009/06/25/object-constructor-quirk/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/06/25/object-constructor-quirk/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 05:57:37 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Objects]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2596</guid>
		<description><![CDATA[Never change something that works! Not really, but sometimes you feel that way. Especially, when you toast 5 to 10 minutes working through an undocumented behavior in PL/SQL. You&#8217;d think after writing it for 19 years, I&#8217;d have seen it all but not so. I was working through a tried and true example from Chapter [...]]]></description>
			<content:encoded><![CDATA[<p>Never change something that works! Not really, but sometimes you feel that way. Especially, when you toast 5 to 10 minutes working through an undocumented behavior in PL/SQL. You&#8217;d think after writing it for 19 years, I&#8217;d have seen it all but not so.</p>
<p>I was working through a tried and true example from Chapter 14 of the <a href="http://www.amazon.com/Oracle-Database-Programming-Osborne-ORACLE/dp/0071494456/ref=pd_ts_b_3?ie=UTF8&#038;s=books">Oracle Database 11g PL/SQL Programming</a> book to prepare for teaching my class tomorrow, when I found this nice quirk. It took a few minutes to figure out what was happening, but here it is so you don&#8217;t have to look for it too. <em>You can only use variable names that are attributes of the object type as formal parameters in object type constructors.</em> If you try to vary it, you&#8217;ll trigger the following exception:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">LINE<span style="color: #66cc66;">/</span>COL ERROR
<span style="color: #808080; font-style: italic;">-------- -----------------------------------------------------------------</span>
<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">11</span>     PL<span style="color: #66cc66;">/</span>SQL: Item ignored
<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">26</span>     PLS<span style="color: #66cc66;">-</span>00307: too many declarations of <span style="color: #ff0000;">'HELLO_THERE'</span> match this call
<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">5</span>      PL<span style="color: #66cc66;">/</span>SQL: Statement ignored
<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">13</span>     PLS<span style="color: #66cc66;">-</span>00320: the declaration of the type of this expression <span style="color: #993333; font-weight: bold;">IS</span> incomplete <span style="color: #993333; font-weight: bold;">OR</span> malformed</pre></div></div>

<p>All I did to trigger this exception was change the <code>who</code> variable to make it scope specific, like <code>iv_who</code> for instance variable, <code>pv_who</code> for parameter variable, and <code>lv_who</code> for local variable.</p>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Broken Code</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
<p>This shows you the broken code and explains why it&#8217;s broken.</p>
<p style="clear: both">
<div style="margin-left:20px">
<p>The broken code has an object type like the following. Interestingly enough, the object type will compile fine but the object body fails when the attribute variable name differs from a constructor function parameter value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE hello_there <span style="color: #993333; font-weight: bold;">IS</span> OBJECT
<span style="color: #66cc66;">&#40;</span> iv_who VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there
  <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT
<span style="color: #66cc66;">,</span> CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there
  <span style="color: #66cc66;">&#40;</span> pv_who VARCHAR2 <span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT
<span style="color: #66cc66;">,</span> MEMBER <span style="color: #993333; font-weight: bold;">FUNCTION</span> get_who <span style="color: #993333; font-weight: bold;">RETURN</span> VARCHAR2
<span style="color: #66cc66;">,</span> MEMBER PROCEDURE set_who <span style="color: #66cc66;">&#40;</span>pv_who VARCHAR2<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> MEMBER PROCEDURE to_string <span style="color: #66cc66;">&#41;</span>
INSTANTIABLE <span style="color: #993333; font-weight: bold;">NOT</span> FINAL;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

<p>The broken code has an object body like the following. The difference between the parameter name in the constructor and the object type attribute name causes the <code>PLS-00307</code> exception. It took some playing around to figure out what it was really complaining about.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE BODY hello_there <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT <span style="color: #993333; font-weight: bold;">IS</span>
    hello HELLO_THERE :<span style="color: #66cc66;">=</span> hello_there<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Generic Object.'</span><span style="color: #66cc66;">&#41;</span>;
  BEGIN
    self :<span style="color: #66cc66;">=</span> hello;
    <span style="color: #993333; font-weight: bold;">RETURN</span>;
  END hello_there;
&nbsp;
  CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there <span style="color: #66cc66;">&#40;</span>pv_who VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  BEGIN
    self<span style="color: #66cc66;">.</span>iv_who :<span style="color: #66cc66;">=</span> pv_who;
    <span style="color: #993333; font-weight: bold;">RETURN</span>;
  END hello_there;
&nbsp;
  MEMBER <span style="color: #993333; font-weight: bold;">FUNCTION</span> get_who <span style="color: #993333; font-weight: bold;">RETURN</span> VARCHAR2 <span style="color: #993333; font-weight: bold;">IS</span>
  BEGIN
    <span style="color: #993333; font-weight: bold;">RETURN</span> self<span style="color: #66cc66;">.</span>iv_who;
  END get_who;
&nbsp;
  MEMBER PROCEDURE set_who <span style="color: #66cc66;">&#40;</span>pv_who VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
  BEGIN
    self<span style="color: #66cc66;">.</span>iv_who :<span style="color: #66cc66;">=</span> pv_who;
  END set_who;
&nbsp;
  MEMBER PROCEDURE to_string <span style="color: #993333; font-weight: bold;">IS</span>
  BEGIN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hello '</span><span style="color: #66cc66;">||</span>self<span style="color: #66cc66;">.</span>iv_who<span style="color: #66cc66;">&#41;</span>;
  END to_string;
&nbsp;
END;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

</div>
</div>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Working Code</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
<p>This shows you the working code and explains why it works.</p>
<p style="clear: both">
<div style="margin-left:20px">
<p>The working code has an object type like the following. You should notice that the only difference renames the <code>pv_who</code> in the overriding constructor&#8217;s parameter list to <code>iv_who</code>. While this doesn&#8217;t throw an exception creating the user-defined object type, it does throw an exception when you try to create the object body or implementation of the object type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE hello_there <span style="color: #993333; font-weight: bold;">IS</span> OBJECT
<span style="color: #66cc66;">&#40;</span> iv_who VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there
  <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT
<span style="color: #66cc66;">,</span> CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there
  <span style="color: #66cc66;">&#40;</span> iv_who VARCHAR2 <span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT
<span style="color: #66cc66;">,</span> MEMBER <span style="color: #993333; font-weight: bold;">FUNCTION</span> get_who <span style="color: #993333; font-weight: bold;">RETURN</span> VARCHAR2
<span style="color: #66cc66;">,</span> MEMBER PROCEDURE set_who <span style="color: #66cc66;">&#40;</span>pv_who VARCHAR2<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> MEMBER PROCEDURE to_string <span style="color: #66cc66;">&#41;</span>
INSTANTIABLE <span style="color: #993333; font-weight: bold;">NOT</span> FINAL;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

<p>The working code has an object body like the following. Like the object type before, the <code>pv_who</code> as a formal parameter of the constructor now uses the same variable name as the attribute for the object type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE BODY hello_there <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT <span style="color: #993333; font-weight: bold;">IS</span>
    hello HELLO_THERE :<span style="color: #66cc66;">=</span> hello_there<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Generic Object.'</span><span style="color: #66cc66;">&#41;</span>;
  BEGIN
    self :<span style="color: #66cc66;">=</span> hello;
    <span style="color: #993333; font-weight: bold;">RETURN</span>;
  END hello_there;
&nbsp;
  CONSTRUCTOR <span style="color: #993333; font-weight: bold;">FUNCTION</span> hello_there <span style="color: #66cc66;">&#40;</span>iv_who VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> SELF <span style="color: #993333; font-weight: bold;">AS</span> RESULT <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  BEGIN
    self<span style="color: #66cc66;">.</span>iv_who :<span style="color: #66cc66;">=</span> iv_who;
    <span style="color: #993333; font-weight: bold;">RETURN</span>;
  END hello_there;
&nbsp;
  MEMBER <span style="color: #993333; font-weight: bold;">FUNCTION</span> get_who <span style="color: #993333; font-weight: bold;">RETURN</span> VARCHAR2 <span style="color: #993333; font-weight: bold;">IS</span>
  BEGIN
    <span style="color: #993333; font-weight: bold;">RETURN</span> self<span style="color: #66cc66;">.</span>iv_who;
  END get_who;
&nbsp;
  MEMBER PROCEDURE set_who <span style="color: #66cc66;">&#40;</span>pv_who VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
  BEGIN
    self<span style="color: #66cc66;">.</span>iv_who :<span style="color: #66cc66;">=</span> pv_who;
  END set_who;
&nbsp;
  MEMBER PROCEDURE to_string <span style="color: #993333; font-weight: bold;">IS</span>
  BEGIN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hello '</span><span style="color: #66cc66;">||</span>self<span style="color: #66cc66;">.</span>iv_who<span style="color: #66cc66;">&#41;</span>;
  END to_string;
&nbsp;
END;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

</div>
</div>
<p>This is certainly interesting. I&#8217;ve no clue why such a limitation exists. The name of a parameter list member in the constructor should be independent from the attribute of a user object.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/06/25/object-constructor-quirk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
