Wednesday, April 20, 2011

How to do a 'ExecuteNonQuery' in Castle Active Record

I have the following code to perform a database level operation through our active record ORM layer.

public static void Vacuum() {
  Execute(
    delegate(ISession session, object instance) {
      ISQLQuery query =
        session.CreateSQLQuery(@"
          VACUUM billableaddresses;
          ")
      query.List();
      return null;
    }, null);
}

Normally when I need to do a non-query like this (its very rare I admit) I simple put a select '1'; after the query which appeases Active Record enough to execute the query asa nonquery.

However, the postgres 'vacuum' command, must be run on its own and cannot be part of a multi statement query.

looking ISQLQuery interface, there doesnt seem to be a method to execute a nonquery, so I was wondering how this can be done?

From stackoverflow
  • For specific DB calls you can get a raw IDbConnection from

    ActiveRecordMediator.GetSessionFactoryHolder().GetSessionFactory(typeof (object)).ConnectionProvider.GetConnection()
    
    Ash : Awesome. This is exactly what I was after, thanks.
    Mauricio Scheffer : ActiveRecord FAQ: http://using.castleproject.org/display/AR/FAQ

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.