User-defined function to convert from RAW(16) to a GUID in Oracle

by timvasil 1/20/2009 11:32:00 AM

CREATE OR REPLACE
FUNCTION RAWTOGUID
( RawData IN RAW
) RETURN VARCHAR AS

BEGIN

declare HexData varchar(32) := rawtohex(RawData);

begin
return
    substr(HexData, 7, 2)
    || substr(HexData, 5, 2)
    || substr(HexData, 3, 2)
    || substr(HexData, 1, 2)
    || '-'
    || substr(HexData, 11, 2)
    || substr(HexData, 9, 2)
    || '-'
    || substr(HexData, 15, 2)
    || substr(HexData, 13, 2)
    || '-'
    || substr(HexData, 17, 4)
    || '-'
    || substr(HexData, 21, 12);
end;

END RAWTOGUID;

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Oracle

Converting an Oracle interval to total seconds

by timvasil 12/22/2008 5:19:00 PM

If you subtract one timestamp from another in Oracle, the result is an interval type.  This is similar to how subtracting a DateTime from another in .NET yields a TimeSpan.  Unfortunately, unlike .NET, Oracle provides no simple equivalent to TimeSpan.TotalSeconds.

Here's the SQL to accomplish it.  Note that fractions of a second are included in the result.

declare
  diff interval day to second :=
        cast('18-DEC-08 11.00.00 PM' as timestamp) - cast('18-DEC-08 9.59.58.5 PM' as timestamp);
  secs number;
begin
  select
    extract(day from diff) * 86400
    + extract(hour from diff) * 3600
    + extract(minute from diff) * 60
    + extract(second from diff)
 into
  secs
  from dual;
 dbms_output.put_line(secs);
end;

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Oracle

Setting a sequence's next value in Oracle

by timvasil 11/20/2008 12:10:00 PM

You can't set the next value of a sequence directly in Oracle; you have to drop and recreate the sequence.  Here's a way to do it if you need to set the next value to the largest value in a particular column plus 1:

declare
    nextId number;
begin
    select coalesce(max(PrimaryID), 0) + 1 into nextId from Table;
    execute immediate 'drop sequence PrimaryIDSeq';
    execute immediate 'create sequence PrimaryIDSeq minvalue ' || nextId;
end;

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Oracle

Using Spring.NET 1.1.1 with Oracle - Your driver must be this tall

by timvasil 4/23/2008 1:19:00 PM

When I recently upgraded from Spring.NET 1.1.0 to 1.1.1, I started getting a bizarre message:

DbProvider product name = Oracle, Oracle provider V2.102.2.20, could not resolve commandBuilderDeriveParametersMethod DeriveParameters to MethodInfo

It's looking for a method named DeriveParameters on the Oracle.DataAccess.Client.OracleCommandBuilder class.  Poking around with the .NET reflector, the method, sure enough, is not there.

After looking at some forum posts and a logged defect against Spring.NET, there were workarounds.  But surely with a database as common as Oracle these steps shouldn't be necessary!  No one having the issue was using Oracle, and, as it turns out, it's because they're using a newer driver that defines this magical DeriveParameters method.

I was using version 10.1.0.200, which is much newer than the 2.102.2.20 that Spring.NET expects when you look at the dbproviders.xml file.  But, alas, 10.1.0.200 is apparently code for something pre-2.102.2.20.  Oracle has two versioning schemes.  I found that when I downloaded the latest Oracle data access components (ODAC), billed as 11.1.0, I ended up with an Oracle.DataAccess.dll with a version 2.116.6.20 timestamp.  Happily, I found this to work with Spring.NET 1.1 without issue.

As a side note, I upgraded to NHibernate 1.2.1 from 1.2.0 at the same time and found the Oracle dialect suddenly dropped support for the "guid" data type.  Moral of the story:  even point releases have gotchas!

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Hibernate | Oracle | Spring.NET

NHibernate & Spring.NET with Oracle

by timvasil 2/14/2008 1:50:00 PM
The documentation for using NHibernate with Oracle is a bit spotty.  Based on some internet searches and trial & error, here's what I've found to work:
  1. Download the Oracle client (or "instant client"):
     
    http://www.oracle.com/technology/software/products/database/oracle10g/htdocs/winsoft.html
     
    Without this client, you'll see the following error message when using Microsoft's Oracle driver:  Class Initialization method Imd.Tests.ServerApi.DeploymentTest.Initialize threw exception. NHibernate.HibernateException:  NHibernate.HibernateException: System.Data.OracleClient requires Oracle client software version 8.1.7 or greater. --->  System.Exception: System.Data.OracleClient requires Oracle client software version 8.1.7 or greater.
     
  2. Set the ORACLE_HOME environment variable and restart your IDE.  An example value is C:\Oracle\product\10.1.0\Client_1. If you don't set this variable you get an error message stating:
     
    Unable to load DLL
    'OraOps10.dll': The specified module could not be found
     
     
    If you specify the wrong directory, i.e. specify a directory one level too high or too low, you'll see bizarre exceptions, including NullReferenceExceptions circa Oracle.DataAccess.Client.OracleException.get_Source() or Oracle.DataAccess.Client.OracleException.get_Message().
      
  3. Add a project assembly reference to Oracle.DataAccess.dll.  I found this in C:\Oracle\product\10.1.0\Client_1\BIN.
     
  4. Update your App.config (or Web.config) properties to specify the Oracle connection string, driver, dialect, etc:

        <db:provider provider="OracleClient-2.0"
                     connectionString="Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = {hostname})(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = {serviceName})));User Id={username};Password={pass};"/>
     
        <object id="ConfigDbSessionFactory"
                type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
            <property name="HibernateProperties">
                <dictionary>
                    <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
                    <entry key="hibernate.dialect" value="NHibernate.Dialect.Oracle9Dialect"/>
                    <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver"/>
                </dictionary>
            </property>
           . . .
        </object>

These instructions are for using Microsoft's .NET driver (System.Data.OracleClient / V2.0.0.0).  A few settings would have to change if you want to use Oracle's .NET driver driver instead (System.DataAccess.Client / V2.102.2.20):

  1. Change OracleClient-2.0 to OracleODP-2.0.
  2. Change NHibernate.Driver.OracleClientDriver to NHibernate.Driver.OracleDataClientDriver

According to the NHibernate documentation, "Microsoft's driver does not handle long character strings correctly. An error happens in some circumstances when using a string of length 2000-4000 as a parameter value.  Oracle cannot handle empty strings (""), you should use null instead. An IUserType implementation to perform the conversion is contained in Nullables.NHibernate library (part of NHibernateContrib package)."  I've also seen problems with the Microsoft driver not being able to convert a Guid into a byte[].  So pick your poison :-)

Common Pitfalls:
  • You may recieve a perplexing "invalid username/password" error when your username and password is, in fact, correct.  ODP.NET might be adjusting the case of your password before hashing it and sending it to Oracle; you can get around this by turning off case sensitivity:
    ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON = FALSE;
  • You may see NullReferenceExceptions with Oracle.DataAccess.Client.OracleException.get_Source() in the stack trace.  This may be indicative of a mismatch between the Spring.NET provider name and the NHibernate driver, i.e. using an Oracle provider name and a Microsoft driver.  If you specify OracleClient-2.0, use the NHibernate.Driver.OracleDataClientDriver driver; if you specify OracleODP-2.0, use NHibernate.Driver.OracleClientDriver.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET Framework | Hibernate | Oracle | Spring.NET

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments