These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Accessing a SQL_Variant
PostPosted: Fri Jun 06, 2008 9:27 pm 
Newbie

Joined: Fri Jun 06, 2008 6:25 pm
Posts: 8
Is there a way to get/set a SQL_VARIANT into/from my class? The class value is of object type.

Scott


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 11:05 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Scottpkuehn,

I have done this before, but I had to use a custom user-type. Here's the code (N.B., it's only been tested against string and int just now, you'd need to test other types yourself.

The property to be mapped to was declared as an obect. The database I was using was SQL server.

The mapping was:
Code:
<property name="ObjectProperty" column="variant_column" type="MyNamespace.SqlVariant, MyAssembly" />


... and the user-type class was:

Code:
public class SqlVariant : IUserType
{

    private SqlType[] variantSqlType = new SqlType[1] { new SqlType(DbType.Object) };

    public SqlType[] SqlTypes
    {
        get
        {
            return variantSqlType;
        }
    }

    public System.Type ReturnedType
    {
        get { return typeof(object); }
    }

    public new bool Equals(object x, object y)
    {
        return object.Equals(x, y);
    }

    public int GetHashCode(object x)
    {
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        int ordinal = rs.GetOrdinal(names[0]);
        if (rs.IsDBNull(ordinal))
        {
            return null;
        }
        else
        {
            return rs[ordinal];
        }
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {

        object valueToSet = (value == null) ? DBNull.Value : value;
        ((IDbDataParameter) cmd.Parameters[index]).Value = valueToSet;
    }

    public object DeepCopy(object value)
    {
        return value;
    }

    public bool IsMutable
    {
        get { return false; }
    }

    public object Replace(object original, object target, object owner)
    {
        return original;
    }

    public object Assemble(object cached, object owner)
    {
        return cached;
    }

    public object Disassemble(object value)
    {
        return value;
    }

}


Hope that helps.

Regards,
Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.