-->
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: Stored procedure wrapper
PostPosted: Mon May 15, 2006 1:34 pm 
Newbie

Joined: Tue Mar 21, 2006 4:18 pm
Posts: 11
Hello all,
i need to execute several stored procedures in my database (my app uses mssql and oracle, so i allways have stored procedure support). I would like to use HN engine to ensure platform independence. I tried to use some classes from IDriver class (using CreateCommand and other methods), but i failed with IDbCommand creation. Can you help me, please, and write me, how to create command for stored procedure with parameters? Or there is another way to execute stored procedures? This have nothing to do with mapping, I only need stered proc to execute. Thanks.


Top
 Profile  
 
 Post subject: Temporarily solution
PostPosted: Tue May 16, 2006 6:37 am 
Newbie

Joined: Tue Mar 21, 2006 4:18 pm
Posts: 11
so there is my temporarily solution, until stored procedures will be native supported by NH. This is of course not ideal, but it meet all my requirements:

In my class i have created new interface and implemented it:
Code:
  public interface IStoredProcSupport
    {
      IDbCommand CreateStoredProcCommand(string procedureName, IDbConnection connection);
      IDataParameter CreateStoredProcParameter(string name, int size, ParameterDirection direction, object value);
    }

    public class SqlClientDriver : NHibernate.Driver.SqlClientDriver, IStoredProcSupport
    {
        public SqlClientDriver() : base()
        {
        }

        public IDbCommand CreateStoredProcCommand(string procedureName, IDbConnection connection)
        {
            IDbCommand cmd = base.CreateCommand();
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = procedureName;
            cmd.Connection = connection;
            return cmd;
        }

        public IDataParameter CreateStoredProcParameter(string name, int size, ParameterDirection direction, object value)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = base.FormatNameForParameter(name);
            param.Size = size;
            param.Direction = direction;
            param.Value = value;
            return param;
        }
    }



in app.config i must change driver:
Code:
  <nhibernate>
    ...
<!--<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />-->
    <add key="hibernate.connection.driver_class" value="Msmis.Nhb.SqlClientDriver, Msmis.Nhb" />
    ...
  </nhibernate>


in my DAL layer:
Code:
        protected virtual IDbCommand CreateStoredProcCommand(string procName)
        {
            CheckForIStoredProcSupport();
            return ((IStoredProcSupport)m_session.SessionFactory.ConnectionProvider.Driver).CreateStoredProcCommand(procName, m_session.Connection);
        }

        protected virtual IDataParameter CreateStoredProcParameter(string name, int size, ParameterDirection direction, object value)
        {
            CheckForIStoredProcSupport();
            return ((IStoredProcSupport)m_session.SessionFactory.ConnectionProvider.Driver).CreateStoredProcParameter(name, size, direction, value);
        }

        private void CheckForIStoredProcSupport()
        {
            NHibernate.Driver.IDriver driver = m_session.SessionFactory.ConnectionProvider.Driver;
            if (!(driver is IStoredProcSupport))
                throw new NotSupportedException("Used database driver '" + driver.GetType().ToString() + "' does not implement interface 'Msmis.Nhb.IStoredProcSupport' and therefore don't support stored procedures.");
        }


and of course there is a fragment stored procedure wrapper:
Code:
        public bool IsInRole(string userName, string roleName)
        {
            IDbCommand cmd = CreateStoredProcCommand("sp_SEC_IsInRole");
            cmd.Parameters.Add(CreateStoredProcParameter("UserName", 30, ParameterDirection.Input, userName));
            cmd.Parameters.Add(CreateStoredProcParameter("RoleName", 30, ParameterDirection.Input, roleName));
            IDataParameter output = CreateStoredProcParameter("Result", 0, ParameterDirection.Output, (int)0);
            cmd.Parameters.Add(output);
            cmd.ExecuteNonQuery();
            return Convert.ToBoolean((int)output.Value);
        }


This is just only for MsSql..., but could be created for any of the existing driver.
Maybe this code fragments will be usefull for somebody who has the same problem.


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.