<?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>IONCANNON &#187; Oracle</title>
	<atom:link href="http://www.ioncannon.net/tag/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ioncannon.net</link>
	<description>Thoughts on Software Development and Engineering</description>
	<lastBuildDate>Tue, 03 Jan 2012 13:59:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<atom:link rel='hub' href='http://www.ioncannon.net/?pushpress=hub'/>
		<item>
		<title>FreeRADIUS with Oracle</title>
		<link>http://www.ioncannon.net/system-administration/136/freeradius-with-oracle/</link>
		<comments>http://www.ioncannon.net/system-administration/136/freeradius-with-oracle/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 12:41:51 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[system administration]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[radius]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/?p=136</guid>
		<description><![CDATA[I recently needed to find a RADIUS server for use in a project where I could stick profile data into Oracle. I remembered seeing FreeRADIUS a while back so I checked to see if it was active and supported Oracle. Sure enough it did. It was a little tricky to set up because some of [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to find a RADIUS server for use in a project where I could stick profile data into Oracle. I remembered seeing <a href="http://freeradius.org/">FreeRADIUS</a> a while back so I checked to see if it was active and supported Oracle. Sure enough it did. It was a little tricky to set up because some of the documentation is out of sync with the latest version so here is what you need to know to get it working.</p>
<p><span id="more-136"></span></p>
<p>I&#039;m using FreeRADIUS version 2.0.3 so some of these issues may be fixed down the road. The first thing you should do is compile FreeRADIUS and get it working using the normal users file. After you have done that and successfully tested queries to the server you can recompile to build Oracle in. </p>
<p>I used the <a href="http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html">Oracle Instant client</a> again. I&#039;ve used the Oracle instant client a number of times now and I can&#039;t believe it took them so long to release their SDK in this type of paired down package.</p>
<p>This should be all you need to add to the configure command to enable the Oracle driver:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">./configure &#8211;with-oracle-home-dir=&lt;path to oracle instant client&gt;</div>
</div>
<p>However that didn&#039;t work for me. Instead I had to go into the RLM Oracle driver directory and run the configure command from there:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">cd freeradius-server-2.0.3/src/modules/rlm_sql/drivers/rlm_sql_oracle<br />
ORACLE_HOME=&lt;path to oracle instant client&gt; CFLAGS=-I&lt;path to oracle instant client&gt;/sdk/include/ LDFLAGS=-L&lt;path to oracle instant client&gt; ./configure</div>
</div>
<p>This created the Makefile but then that still wasn&#039;t correct. I had to modify the includes and libraries so they matched the correct location:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">RLM_SQL_CFLAGS = &nbsp; &nbsp;$(INCLTDL) -I&lt;path to oracle instant client&gt;/sdk/include/<br />
RLM_SQL_LIBS &nbsp; = &nbsp;-L&lt;path to oracle instant client&gt; -lclntsh -lm</div>
</div>
<p>After making these changes I could then do a make and make install. You can verify that the module is installed by looking for the module file named rlm_sql_oracle.a in your lib directory (in my case /usr/local/lib/). After you have verified that the module is compiled and in place you are ready to move on to the configuration.</p>
<p>The first thing to do is load the provided Oracle schema. That schema can be found in: freeradius-server-2.0.3/raddb/sql/oracle</p>
<p>Next read over the <a href="http://wiki.freeradius.org/Rlm_sql">RLM SQL configuration</a> information to get a general idea of what is going on in the configuration files and how FreeRADIUS uses the queries to find the correct information for a given request. </p>
<p>The following steps are needed to configure the Oracle access and have FreeRADIUS use that configuration for data (I assume that you have installed with a base of /usr/local):</p>
<ol>
<li>Edit /usr/local/etc/raddb/sql.conf set database = &#034;oracle&#034;, set the server, login, password, and radius_db values. The following is an example of the connection information needed:
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;"># Connection info:<br />
server = &quot;127.0.0.1&quot;<br />
login = &quot;username&quot;<br />
password = &quot;password&quot;</p>
<p># Database table configuration for everything except Oracle<br />
#radius_db = &quot;radius&quot;<br />
# If you are using Oracle then use this instead<br />
radius_db = &quot;(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SID=MYDB01)))&quot;</div>
</div>
</li>
<li>Search for the following and uncomment the SQL load line in the file /usr/local/etc/raddb/sites-enabled/default as follows:
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;"># See &quot;Authorization Queries&quot; in sql.conf<br />
&nbsp; sql</div>
</div>
<p>Note: This is something that I didn&#039;t find in the documentation. I believe that is due to the documentation being for an older version and this being a new requirement.</p>
<li>Load sample data into the database:
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> radusergroup <span class="kw1">VALUES</span><span class="br0">&#40;</span>radusergroup_seq<span class="sy0">.</span><span class="kw1">NEXTVAL</span><span class="sy0">,</span> <span class="st0">&#039;dynamic&#039;</span><span class="sy0">,</span> <span class="st0">&#039;fredf&#039;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> radcheck <span class="kw1">VALUES</span><span class="br0">&#40;</span>radcheck_seq<span class="sy0">.</span><span class="kw1">NEXTVAL</span><span class="sy0">,</span> <span class="st0">&#039;fredf&#039;</span><span class="sy0">,</span> <span class="st0">&#039;Cleartext-Password&#039;</span><span class="sy0">,</span> <span class="st0">&#039;:=&#039;</span><span class="sy0">,</span> <span class="st0">&#039;wilma&#039;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> radreply <span class="kw1">VALUES</span><span class="br0">&#40;</span>radreply_seq<span class="sy0">.</span><span class="kw1">NEXTVAL</span><span class="sy0">,</span> <span class="st0">&#039;fredf&#039;</span><span class="sy0">,</span> <span class="st0">&#039;Framed-IP-Address&#039;</span><span class="sy0">,</span> <span class="st0">&#039;:=&#039;</span><span class="sy0">,</span> <span class="st0">&#039;1.2.3.4&#039;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> radgroupreply <span class="kw1">VALUES</span><span class="br0">&#40;</span>radgroupreply_seq<span class="sy0">.</span><span class="kw1">NEXTVAL</span><span class="sy0">,</span> <span class="st0">&#039;dynamic&#039;</span><span class="sy0">,</span> <span class="st0">&#039;Framed-Compression&#039;</span><span class="sy0">,</span> <span class="st0">&#039;:=&#039;</span><span class="sy0">,</span> <span class="st0">&#039;Van-Jacobsen-TCP-IP&#039;</span><span class="br0">&#41;</span>;</div>
</div>
</li>
<li>Start the daemon in debug mode:
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">radius -X</div>
</div>
<p>If you don&#039;t have the oracle libraries in your path you will need to start radius with the correct LD path entry like this:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">LD_LIBRARY_PATH=&lt;path to oracle instant client&gt; radiusd -X</div>
</div>
<li>Send a test query:
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">radtest fredf wilma localhost 0 radpassword</div>
</div>
<p>On the console for radius -X you will see debug and you should receive a valid response from the test that looks like this:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">User-Name = &quot;fredf&quot;<br />
User-Password = &quot;wilma&quot;<br />
NAS-IP-Address = 127.0.0.1<br />
NAS-Port = 0<br />
Framed-IP-Address = 1.2.3.4</div>
</div>
</li>
<p>At this point you are ready to load your data into FreeRADIUS via Oracle.</p>
<p>Other Notes:</p>
<p>Depending on how large your configuration values are the provided schema may not give you enough room to store everything. You may need to alter the tables to increase the space available for values like this:</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">ALTER</span> <span class="kw1">TABLE</span> radreply <span class="kw1">MODIFY</span> value varchar<span class="br0">&#40;</span>128<span class="br0">&#41;</span>;</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/system-administration/136/freeradius-with-oracle/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Oracle Instant Client and SQLPlus</title>
		<link>http://www.ioncannon.net/system-administration/114/using-oracle-instant-client-and-sqlplus/</link>
		<comments>http://www.ioncannon.net/system-administration/114/using-oracle-instant-client-and-sqlplus/#comments</comments>
		<pubDate>Fri, 19 Jan 2007 15:35:53 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[system administration]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sqlplus]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/system-administration/114/using-oracle-instant-client-and-sqlplus/</guid>
		<description><![CDATA[Some time ago Oracle introduced their Instant Client as an alternative to needing a full install of the Oracle client to run your own apps or SQLPlus. I&#039;ve been putting the Instant Client to good use these days and figured I would give a quick howto on getting it set up. First you should download [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago Oracle introduced their <a href="http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html">Instant Client</a> as an alternative to needing a full install of the Oracle client to run your own apps or SQLPlus. I&#039;ve been putting the Instant Client to good use these days and figured I would give a quick howto on getting it set up.</p>
<p><span id="more-114"></span></p>
<p>First you should <a href="http://www.oracle.com/technology/software/tech/oci/instantclient/index.html">download the correct binary</a> for what you want to do. They have a build for a lot of different platforms and then they also split the libraries themselves out from SQLPlus. If all you need is the OCI library then all you have to do is download that part. I find it nice to get both the libraries and SQLPlus so I can verify that the setup works.</p>
<p>After you have downloaded that client you will need to unzip it. In the current case the directory it creates is &#034;instantclient_10_2&#034; and in this example I&#039;m going to assume you got both the libraries and SQLPlus. After unziping both packages change into the instantclient_10_2 directory.</p>
<p>Now that you are in the instantclient_10_2 directory you will need to set up some environment variables to be able to run SQLPlus. The following will do the trick (just remember that I&#039;m assuming that everything you need is in your current directory, you would of course need to change these if you want to install in a different location):</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">export LD_LIBRARY_PATH=.<br />
export TNS_ADMIN=.<br />
export ORACLE_HOME=.</div>
</div>
<p>Now you will need to create of copy a tnsnames.ora file into your current directory. Here is an example assuming you have Oracle running on a box at 192.168.1.100 with a listener on port 1521 and a SID of XE:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">XE.WORLD =<br />
&nbsp; (DESCRIPTION =<br />
&nbsp; &nbsp; (ADDRESS_LIST =<br />
&nbsp; &nbsp; &nbsp; &nbsp; (ADDRESS =<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (COMMUNITY = tcp.world)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (PROTOCOL = TCP)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Host = 192.168.1.100)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Port = 1521)<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
&nbsp; &nbsp; )<br />
&nbsp; &nbsp; (CONNECT_DATA = (SID = XE)<br />
&nbsp; &nbsp; )<br />
&nbsp; )</div>
</div>
<p>Now you are ready to go. Just run the following to connect to your Oracle database:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">./sqlplus username/password@XE.WORLD</div>
</div>
<p>Oracle&#039;s Instant Client makes the world much easier now. In just a few steps you are able to connect to an Oracle database with SQLPlus and don&#039;t have to go through a ton of installation mess.</p>
<p>As a side note, I also found that if you grab the header files from a full installation you can easily compile against the Instant Client libraries when using OCI calls. This can make developing an application that connects to Oracle very easy to set up.</p>
<p>Tags: <a href="http://technorati.com/tag/oracle" rel="tag">oracle</a>, <a href="http://technorati.com/tag/sqlplus" rel="tag"> sqlplus</a>, <a href="http://technorati.com/tag/instant+client" rel="tag"> instant client</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/system-administration/114/using-oracle-instant-client-and-sqlplus/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to get the next date for a weekday using Oracle</title>
		<link>http://www.ioncannon.net/database-administration/93/how-to-get-the-next-date-for-a-weekday-using-oracle/</link>
		<comments>http://www.ioncannon.net/database-administration/93/how-to-get-the-next-date-for-a-weekday-using-oracle/#comments</comments>
		<pubDate>Wed, 02 Aug 2006 19:26:57 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[database administration]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/oracle/93/how-to-get-the-next-date-for-a-weekday-using-oracle/</guid>
		<description><![CDATA[If you ever need to find the next weekday from a given date in Oracle it turns out they have a built in function for doing just that. If you want the next Sunday from yesterday you would do: Valid entries for the day are: SUN, MON, TUE, WED, THU, FRI, and SAT Tags: Oracle, [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever need to find the next weekday from a given date in Oracle it turns out they have a built in function for doing just that. If you want the next Sunday from yesterday you would do:</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> NEXT_DAY<span class="br0">&#40;</span>SYSDATE <span class="sy0">-</span> <span class="nu0">1</span><span class="sy0">,</span> <span class="st0">&#039;SUN&#039;</span><span class="br0">&#41;</span> <span class="kw1">FROM</span> dual;</div>
</div>
<p>Valid entries for the day are: SUN, MON, TUE, WED, THU, FRI, and SAT</p>
<p>Tags: <a href="http://technorati.com/tag/Oracle" rel="tag">Oracle</a>, <a href="http://technorati.com/tag/SQL" rel="tag"> SQL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/database-administration/93/how-to-get-the-next-date-for-a-weekday-using-oracle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Beautifier</title>
		<link>http://www.ioncannon.net/database-administration/67/sql-beautifier/</link>
		<comments>http://www.ioncannon.net/database-administration/67/sql-beautifier/#comments</comments>
		<pubDate>Thu, 06 Jul 2006 17:35:24 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[database administration]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/oracle/67/sql-beautifier/</guid>
		<description><![CDATA[I pulled a large amount of SQL out of some existing code and wanted to have it formated nicely for me. I figured there had to be some type of pretty printer for SQL available outside of applications like TOAD. I have access to TOAD but it would have required a reboot so I figured [...]]]></description>
			<content:encoded><![CDATA[<p>I pulled a large amount of SQL out of some existing code and wanted to have it formated nicely for me. I figured there had to be some type of pretty printer for SQL available outside of applications like TOAD. I have access to TOAD but it would have required a reboot so I figured I would ask google what to do. There seem to be a number of different formaters out there. I found a couple people talking about current options <a href="http://awads.net/wp/2005/12/12/format-your-sql-the-easy-way/">a list here</a> and <a href="http://blogs.ittoolbox.com/database/solutions/archives/free-sql-code-beautifier-3240">here</a>.  The following list is a summary of the options I found:</p>
<ul>
<li><a href="http://www.sqlinform.com/">SQLInform</a></li>
<li><a href="http://www.wangz.net/cgi-bin/pp/gsqlparser/sqlpp/sqlformat.tpl">Online SQL Formatter</>a</li>
<li><a href="http://www.dbainfopower.com/dbaip_SQLreview.php">SQL Review</>a</li>
<li><a href="http://psti.equinoxbase.com/">Pl/Sql tidy</a></li>
<li><a href="http://vsbabu.org/software/lsqlb.html">Make your own with C</a></li>
<li>Use TOAD</li>
</ul>
<p>I decided that since SQLInform was mentioned a lot I would give it a try. I was suprised at how well it did. I gave it a huge amount of SQL to re-format and it gave it back to me formated. There is a limit on how much you can ask it to format at once but that shouldn&#039;t be a big issue. I didn&#039;t see that it choked on anything that I gave it and there were all kinds of nasty things going on in the SQL.</p>
<p>One interesting thing to come from this is that it seems there are no open source or truely free unix based formaters out there. It seems like it would be easy enough to get going since the SQL grammar is available for a number of different parser generators.</p>
<p>Tags: <a href="http://technorati.com/tag/SQL" rel="tag">SQL</a>, <a href="http://technorati.com/tag/TOAD" rel="tag"> TOAD</a>, <a href="http://technorati.com/tag/SQLInform" rel="tag"> SQLInform</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/database-administration/67/sql-beautifier/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Converting unix or java timestamps (time since the epoch) to real dates with Oracle</title>
		<link>http://www.ioncannon.net/database-administration/27/converting-unix-or-java-timestamps-time-since-the-epoch-to-real-dates-with-oracle/</link>
		<comments>http://www.ioncannon.net/database-administration/27/converting-unix-or-java-timestamps-time-since-the-epoch-to-real-dates-with-oracle/#comments</comments>
		<pubDate>Sat, 29 Apr 2006 22:12:17 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[database administration]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/27/converting-unix-or-java-timestamps-time-since-the-epoch-to-real-dates-with-oracle/</guid>
		<description><![CDATA[A few days ago I made use of a couple Oracle built in functions and it made me happy I didn&#039;t have to write a stored proc or some type of mini-app to do it. I needed to parse a timestamp out of a field that was put there by a java program. The timestamp [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I made use of a couple Oracle built in functions and it made me happy I didn&#039;t have to write a stored proc or some type of mini-app to do it. I needed to parse a timestamp out of a field that was put there by a java program. The timestamp was just the output of System.currentTimeInMillis()  and was concatenated onto some other information. </p>
<p>It took a little digging to find out how to convert a <a href="http://en.wikipedia.org/wiki/Unix_time">epoch</a> style timestamp but here it is:</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> new_time<span class="br0">&#40;</span> to_date<span class="br0">&#40;</span><span class="st0">&#039;01011970&#039;</span><span class="sy0">,</span> <span class="st0">&#039;ddmmyyyy&#039;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">1</span><span class="sy0">/</span><span class="nu0">24</span><span class="sy0">/</span><span class="nu0">60</span><span class="sy0">/</span><span class="nu0">60</span> <span class="sy0">*</span> :currenttimeinmillis<span class="sy0">/</span><span class="nu0">1000</span><span class="sy0">,</span> <span class="st0">&#039;GMT&#039;</span><span class="sy0">,</span> <span class="st0">&#039;EDT&#039;</span> <span class="br0">&#41;</span> <span class="kw1">FROM</span> dual</div>
</div>
<p>Note that I convert the output from GMT to EDT here.
</p>
<p>Tags: <a href="http://technorati.com/tag/java" rel="tag">java</a>, <a href="http://technorati.com/tag/oracle" rel="tag"> oracle</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/database-administration/27/converting-unix-or-java-timestamps-time-since-the-epoch-to-real-dates-with-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Oracle DBI ActiveRecord in 7 steps</title>
		<link>http://www.ioncannon.net/programming/41/ruby-oracle-dbi-activerecord-in-7-steps/</link>
		<comments>http://www.ioncannon.net/programming/41/ruby-oracle-dbi-activerecord-in-7-steps/#comments</comments>
		<pubDate>Thu, 23 Mar 2006 05:35:26 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[DBI]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/41/ruby-oracle-dbi-activerecord-in-7-steps/</guid>
		<description><![CDATA[Setting up ruby to work with Oracle seems to be a pain for a lot of people. Here are the steps I follow to set it up on a linux box from nothing to Active Record or DBI in 7 steps. Gather the installation sources you will need. You have to be registered with oracle [...]]]></description>
			<content:encoded><![CDATA[<p>
Setting up ruby to work with Oracle seems to be a pain for a lot of people. Here are the steps I follow to set it up on a linux box from nothing to Active Record or DBI in 7 steps.
</p>
<p><span id="more-41"></span></p>
<ol>
<li>
   Gather the installation sources you will need. You have to be registered with oracle to get their instant client packages.<br />
   Download the <a href="http://www.jiubao.org/ruby-oci8/">ruby oci8 drivers</a><br />
   Download the <a href="http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/linuxsoft.html">oracle instant client</a><br />
   You want the following packages (these examples assume the zip format):</p>
<ul>
<li>Instant Client Package &#8211; Basic or Instant Client Package &#8211; Basic Lite</li>
<li>Instant Client Package &#8211; SDK</li>
<li>Instant Client Package &#8211; SQL*Plus (optional but nice to have)</li>
</ul>
</li>
<li>Install oracle instant client packages
<p>     I unzip them in the /opt directory and assume that for the examples. It doesn&#039;t matter where you put them as long as you know where they are.</p>
<p>     The zip packages have one problem that needs to be solved after they are expanded. You must create a symlink in the install directory for a shared library: ln -s libclntsh.so.10.1 libclntsh.so</p>
</li>
<li>Set up the oracle environment: 
<p>     I created a script called oracleenv.sh with the following lines in it:</p>
<div class="codesnip-container" >
<div class="ini codesnip" style="font-family:monospace;">export ORACLE_HOME<span class="sy0">=</span><span class="re2">/opt/instantclient_10_2/</span><br />
&nbsp; &nbsp; &nbsp;export LD_LIBRARY_PATH<span class="sy0">=</span><span class="re2">/opt/instantclient_10_2/</span><br />
&nbsp; &nbsp; &nbsp;export PATH<span class="sy0">=</span><span class="re2">/opt/instantclient_10_2/:$PATH</span><br />
&nbsp; &nbsp; &nbsp;export SQLPATH<span class="sy0">=</span><span class="re2">/opt/instantclient_10_2/</span><br />
&nbsp; &nbsp; &nbsp;export TNS_ADMIN<span class="sy0">=</span><span class="re2">/etc/</span></div>
</div>
<p>     and then I source it when I want to use something oracle related &#034;. oracleenv.sh&#034;. You may want to just put it in your profile. </p>
<p>     You also need to create a /etc/tnsnames.ora file. This can be tricky. The best option is probably to ask your DBA to create it for you. Here is an example:</p>
<div class="codesnip-container" >
<div class="ini codesnip" style="font-family:monospace;">DEVDB.WORLD <span class="sy0">=</span><br />
&nbsp; <span class="br0">&#40;</span>DESCRIPTION <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="br0">&#40;</span>ADDRESS_LIST <span class="sy0">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>ADDRESS <span class="sy0">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>COMMUNITY <span class="sy0">=</span><span class="re2"> tcp.world <span class="br0">&#41;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>PROTOCOL <span class="sy0">=</span><span class="re2"> TCP<span class="br0">&#41;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>Host <span class="sy0">=</span><span class="re2"> 192.168.1.125<span class="br0">&#41;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>Port <span class="sy0">=</span><span class="re2"> <span class="nu0">1546</span><span class="br0">&#41;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#40;</span>CONNECT_DATA <span class="sy0">=</span><span class="re2"> <span class="br0">&#40;</span>SID <span class="sy0">=</span> DEVDB<span class="br0">&#41;</span></span><br />
&nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#41;</span></div>
</div>
<p>  The main thing you need for ruby later is the SID. At this point you may want to try to use sqlplus to see if you can connect to the database.
  </li>
<li>Build the ruby oci8 package
<p>   untar</p>
<div class="codesnip-container" >
<div class="ini codesnip" style="font-family:monospace;">ruby setup.rb config &#8212; &#8211;with-instant-client<span class="sy0">=</span><span class="re2">/opt/instantclient_10_2/</span></div>
</div>
<p>   make<br />
   make install
  </li>
<li>Give the low level API a test 
<div class="codesnip-container" >
<div class="ini codesnip" style="font-family:monospace;">ruby -r oci8 -e <span class="st0">&quot;OCI8.new(&#039;username&#039;, &#039;password&#039;, &#039;DEVDB.WORLD&#039;).exec(&#039;select sysdate from dual&#039;) do |r| puts r.join(&#039;,&#039;); end&quot;</span></div>
</div>
<p>  Here you provide the username, password and tns name. If this returns the current date and time then the driver is installed correctly.
  </li>
<li>Install DBI
<p>   download the <a href="http://rubyforge.org/projects/ruby-dbi/">dbi tar file</a><br />
   The current version is: dbi-0.1.0.tar.gz<br />
   untar the archive<br />
   configure it (for this example only dbi itself is included): ruby setup.rb config &#8211;with=dbi<br />
   build: ruby setup.rb setup<br />
   install: ruby setup.rb install<br />
   test: </p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">&#039;dbi&#039;</span><br />
dbh = DBI.<span class="me1">connect</span><span class="br0">&#40;</span><span class="st0">&#039;DBI:OCI8:DEVDB.WORLD&#039;</span>, <span class="st0">&#039;username&#039;</span>, <span class="st0">&#039;password&#039;</span><span class="br0">&#41;</span><br />
dbh.<span class="me1">select_all</span><span class="br0">&#40;</span><span class="st0">&#039;select sysdate from dual&#039;</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span> row <span class="sy0">|</span><br />
&nbsp; <span class="kw3">p</span> row<br />
<span class="kw1">end</span><br />
dbh.<span class="me1">disconnect</span></div>
</div>
<p>   If this returns the current date and time then DBI is installed correctly.
  </li>
<li> Install ActiveRecord
<p>  I use gem to install active record: gem install activerecord </p>
<p>  After installing you should be able to use active record. Here is an example use: </p>
<div class="codesnip-container" >
<div class="ruby codesnip" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">&#039;rubygems&#039;</span><br />
<span class="kw3">require</span> <span class="st0">&#039;active_record&#039;</span></p>
<p><span class="re2">ActiveRecord::Base</span>.<span class="me1">establish_connection</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="re3">:adapter</span> =<span class="sy0">&amp;</span>gt; <span class="st0">&quot;oci&quot;</span>,<br />
&nbsp; &nbsp; <span class="re3">:username</span> =<span class="sy0">&amp;</span>gt; <span class="st0">&quot;username&quot;</span>,<br />
&nbsp; &nbsp; <span class="re3">:password</span> =<span class="sy0">&amp;</span>gt; <span class="st0">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span class="re3">:host</span> =<span class="sy0">&amp;</span>gt; <span class="st0">&quot;DEVDB.WORLD&quot;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">class</span> TestTable <span class="sy0">&amp;</span>lt; <span class="re2">ActiveRecord::Base</span><br />
&nbsp; set_table_name <span class="st0">&quot;test_table&quot;</span><br />
&nbsp; set_primary_key <span class="st0">&quot;some_id&quot;</span><br />
<span class="kw1">end</span></p>
<p>TestTable.<span class="me1">find</span><span class="br0">&#40;</span><span class="re3">:all</span><span class="br0">&#41;</span>.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>tt<span class="sy0">|</span><br />
&nbsp; <span class="kw3">p</span> tt<br />
<span class="kw1">end</span></p>
<p>nac = TestTable.<span class="me1">new</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
nac.<span class="me1">id</span> = <span class="nu0">1001</span><br />
nac.<span class="me1">some_column</span> = <span class="st0">&quot;test&quot;</span><br />
nac.<span class="me1">save</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</div>
<p>  One thing I learned that is someone confusing is that active record turns your primary key into a variable named &#034;id&#034;. So if you don&#039;t use auto generated primary keys for some reason you can&#039;t just assign the primary key based on the name of the column as you can any other column in the table. Instead you need to use the id field of the model.</p>
</li>
</ol>
<p>
For more information check out the following links:</p>
<ul>
<li>Info on <a href="http://www.oracle.com/technology/docs/tech/sql_plus/10103/readme_ic.htm">oracle&#039;s instant client</a></li>
<li>Info on <a href="http://www.kitebird.com/articles/ruby-dbi.html">using dbi</a></li>
<li>Info on <a href="http://ruby-dbi.rubyforge.org/">dbi</a> </li>
<li>Info on <a href="http://wiki.rubyonrails.org/rails/pages/Oracle">rails with oracle</a></li>
</ul>
<p>Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/oracle" rel="tag"> oracle</a>, <a href="http://technorati.com/tag/dbi" rel="tag"> dbi</a>, <a href="http://technorati.com/tag/activerecord" rel="tag"> activerecord</a>, <a href="http://technorati.com/tag/rails" rel="tag"> rails</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/41/ruby-oracle-dbi-activerecord-in-7-steps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with Oracle Strings</title>
		<link>http://www.ioncannon.net/database-administration/51/fun-with-oracle-strings/</link>
		<comments>http://www.ioncannon.net/database-administration/51/fun-with-oracle-strings/#comments</comments>
		<pubDate>Tue, 14 Feb 2006 08:14:07 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[database administration]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/51/fun-with-oracle-strings/</guid>
		<description><![CDATA[Today I needed to find a way to count the number of unique email domains in a table. I figured there was a way of getting the index of a string in another string and sure enough there is. This did the trick in Oracle: The INSTR function gives you the location in a string [...]]]></description>
			<content:encoded><![CDATA[<p>Today I needed to find a way to count the number of unique email domains in a table. I figured there was a way of getting the index of a string in another string and sure enough there is. This did the trick in Oracle:</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> count<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">,</span> SUBSTR<span class="br0">&#40;</span>email<span class="sy0">,</span> INSTR<span class="br0">&#40;</span>email<span class="sy0">,</span> <span class="st0">&#039;@&#039;</span><span class="sy0">,</span> 1<span class="sy0">,</span> 1<span class="br0">&#41;</span><span class="sy0">+</span>1<span class="br0">&#41;</span> <span class="kw1">FROM</span> SOMETABLE <span class="kw1">GROUP</span> <span class="kw1">BY</span> SUBSTR<span class="br0">&#40;</span>email<span class="sy0">,</span> INSTR<span class="br0">&#40;</span>email<span class="sy0">,</span> <span class="st0">&#039;@&#039;</span><span class="sy0">,</span> 1<span class="sy0">,</span> 1<span class="br0">&#41;</span><span class="sy0">+</span>1<span class="br0">&#41;</span> <span class="kw1">ORDER</span> <span class="kw1">BY</span> count<span class="br0">&#40;</span>1<span class="br0">&#41;</span> <span class="kw1">DESC</span></div>
</div>
<p>The INSTR function gives you the location in a string where another string is located. See the following link for more on the INSTR function: <a href="http://www.techonthenet.com/oracle/functions/instr.php">http://www.techonthenet.com/oracle/functions/instr.php</a></p>
<p>I&#039;ve always found the way Oracle handles case interesting. It looks like they are changing things a little starting with 10G: <a href="http://blogs.ittoolbox.com/database/solutions/archives/005951.asp">http://blogs.ittoolbox.com/database/solutions/archives/005951.asp</a>
</p>
<p>Tags: <a href="http://technorati.com/tag/oracle" rel="tag">oracle</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/database-administration/51/fun-with-oracle-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

