2009-07-10 48 views

回答

9

NHibernate設計用於使用SQL的方言而不是ad hoc查詢語言。這將是非常棘手的事情。但要回答您的問題,您需要:

  1. 您的數據庫的ADO.NET提供程序(這將是一個單獨的問題)。
  2. 驅動程序類(從NHibernate的DriverBase派生)。這是一個我已經寫在過去(有點匿名):

    public sealed class XxxClientDriver : DriverBase 
    { 
        public override IDbConnection CreateConnection() 
        { 
         return XxxClientFactory.Instance.CreateConnection(); 
        } 
        public override IDbCommand CreateCommand() 
        { 
         return XxxClientFactory.Instance.CreateCommand(); 
        } 
        public override bool UseNamedPrefixInSql 
        { 
         get { return true; } 
        } 
        public override bool UseNamedPrefixInParameter 
        { 
         get { return true; } 
        } 
        public override string NamedPrefix 
        { 
         get { return "@"; } 
        } 
    } 
    
  3. 可能(絕對是您的情況)的話,從NHibernate的方言類,它定義類來呈現每個特定語法元素在你的語言派生(但請注意,這仍然是以SQL爲導向的,這是我懷疑你會在哪裏跌倒在這裏)。在我的情況的一個例子:

    public sealed class XxxDialect : Dialect 
    { 
        public override JoinFragment CreateOuterJoinFragment() 
        { 
         return new XxxJoinFragment(); 
        } 
    } 
    
  4. 由於很多不規範的語法元素(相對於普通的SQL)的語言了。再次,從我的情況的一個例子:

    public sealed class XxxJoinFragment : JoinFragment 
    { 
        private readonly SqlStringBuilder _afterFrom; 
        public XxxJoinFragment() 
        { 
         _afterFrom = new SqlStringBuilder(); 
        } 
        private SqlStringBuilder AfterFrom 
        { 
         get { return _afterFrom; } 
        } 
        public override SqlString ToFromFragmentString 
        { 
         get { return _afterFrom.ToSqlString(); } 
        } 
        public override SqlString ToWhereFragmentString 
        { 
         get { return SqlString.Empty; } 
        } 
        public override void AddJoin(string tableName, string alias, 
         string[] fkColumns, string[] pkColumns, 
         JoinType joinType) 
        { 
         AddCrossJoin(tableName, alias); 
        } 
        public override void AddJoin(string tableName, string alias, 
         string[] fkColumns, string[] pkColumns, 
         JoinType joinType, string on) 
        { 
         AddJoin(tableName, alias, fkColumns, pkColumns, joinType); 
        } 
        public override void AddCrossJoin(string tableName, string alias) 
        { 
         AfterFrom.Add(", ").Add(tableName).Add(" ").Add(alias); 
        } 
        public override void AddJoins(SqlString fromFragment, SqlString whereFragment) 
        { 
         AddFromFragmentString(fromFragment); 
        } 
        public override bool AddCondition(string condition) 
        { 
         return true; 
        } 
        public override bool AddCondition(SqlString condition) 
        { 
         return true; 
        } 
        public override void AddFromFragmentString(SqlString fromFragmentString) 
        { 
         AfterFrom.Add(fromFragmentString); 
        } 
    } 
    

正如你所看到的,在我的情況(與隱式連接條件基於SQL的查詢語言),這是不是太難。但在你的情況下,我懷疑你會反對它。祝你好運!