public void ValidResult() { ScalarQuery query = new ScalarQuery(typeof(Post), validQuery); long result = (long)ActiveRecordMediator <Post> .ExecuteQuery(query); Assert.AreEqual(1, result); }
/// <summary> /// 获得一个统计值 /// </summary> /// <param name="Hql"></param> /// <param name="paramList"></param> /// <returns>数字</returns> #region int DAO_GetCount(string where) public static int DAO_GetCount(string where) { try { T obj = (T)System.Reflection.Assembly.GetAssembly(typeof(T)).CreateInstance(typeof(T).ToString()); string hql = ""; if (where.Trim() == String.Empty) { hql = string.Format(" select count(*) from {0}", obj.GetType().ToString()); } else { hql = string.Format(" select count(*) from {0} {1}", obj.GetType().ToString(), where.ToUpper().StartsWith("WHERE") ? where : "WHERE " + where); } ScalarQuery query = new ScalarQuery(typeof(T), hql); return(Convert.ToInt32(ExecuteQuery(query))); } catch (Exception ex) { //LogHelper.WriteLog(LogTag + ".GetNum(string Hql, ArrayList paramList)", ex); return(-1); } }
private void Init() { var str = string.Format("SELECT max(r.{0}) FROM {1} r", m_idMember, m_table); var query = new ScalarQuery <object>(m_type, str); object highestId; try { highestId = query.Execute(); } catch (Exception e) { OnError(e); highestId = query.Execute(); } if (highestId == null) { m_highestId = 0; } else { m_highestId = (long)Convert.ChangeType(highestId, typeof(long)); } if (m_highestId < m_minId) { m_highestId = m_minId; } }
//public TRateMs getTRateMs(string facCd) //{ // TRateMs rateMs = null; // return rateMs; //} #region Sample public TFactoryMs getFactoryByCd(string facCd) { TFactoryMs factory = null; ISession ss = holder.CreateSession(typeof(TFactoryMsDaoOracleImp)); ITransaction tran = ss.BeginTransaction(); try { //result = (IList<MFunctioncatalog>)FindAll(typeof(MFunctioncatalog)); ScalarQuery <TFactoryMs> q = new ScalarQuery <TFactoryMs>(typeof(TFactoryMs), @" from TFactoryMs where IFacCd=:IFacCd"); q.SetParameter("IFacCd", facCd); factory = q.Execute(); //FindByPrimaryKey找不到数据的时候是抛出ActiveRecordException,不太好处理 //termms = (TTermMs)FindByPrimaryKey(typeof(TTermMs), userid); } catch (Castle.ActiveRecord.Framework.ActiveRecordException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } catch (DbException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } finally { tran.Dispose(); } return(factory); }
public void InvalidResultGeneric() { ScalarQuery <int> query = new ScalarQuery <int>(typeof(Post), invalidQuery); int result = ActiveRecordMediator <Post> .ExecuteQuery2(query); Assert.AreEqual(default(int), result); }
public TSectionMs getSectionByCd(string companyCd, string secCd) { TSectionMs section = null; ISession ss = holder.CreateSession(typeof(TSectionMsDaoOracleImp)); ITransaction tran = ss.BeginTransaction(); try { ScalarQuery <TSectionMs> q = new ScalarQuery <TSectionMs>(typeof(TSectionMs), @" from TSectionMs where Id.ICompanyCd=:ICompanyCd and Id.ISectionCd=:ISectionCd "); q.SetParameter("ICompanyCd", companyCd); q.SetParameter("ISectionCd", secCd); section = q.Execute(); } catch (Castle.ActiveRecord.Framework.ActiveRecordException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } catch (DbException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } finally { tran.Dispose(); holder.ReleaseSession(ss); } return(section); }
public void ValidResultGeneric() { ScalarQuery <long> query = new ScalarQuery <long>(typeof(Post), validQuery); long result = ActiveRecordMediator <Post> .ExecuteQuery2(query); Assert.AreEqual(1, result); }
private static uint GetNextHighestPlayerId() { ScalarQuery idQuery = new ScalarQuery(typeof(CharacterRecord), QueryLanguage.Hql, "select max(chr.EntityId) from CharacterRecord chr"); object highestId = ActiveRecordMediator.ExecuteQuery(idQuery); return(highestId == null ? 0 : (highestId as CharacterRecord).EntityLowId + 1); }
public int placeIdByURL(string url) { String sql = "SELECT s.id FROM Place AS s WHERE s.CustomUrl = '" + url + "'"; ScalarQuery <int> q = new ScalarQuery <int>(typeof(place), sql); int id = q.Execute(); return(id); }
public static uint GetIdByName(string name) { var sql = string.Format("SELECT {0} FROM {1} WHERE {2} = {3} LIMIT 1", DatabaseUtil.Dialect.QuoteForColumnName("EntityLowId"), DatabaseUtil.Dialect.QuoteForTableName(typeof(CharacterRecord).Name), DatabaseUtil.Dialect.QuoteForColumnName("Name"), DatabaseUtil.ToSqlValueString(name)); var query = new ScalarQuery <long>(typeof(CharacterRecord), QueryLanguage.Sql, sql); return((uint)query.Execute()); }
public static uint GetGuildId(uint charId) { var sql = string.Format("SELECT {0} FROM {1} WHERE {2} = {3} LIMIT 1", DatabaseUtil.Dialect.QuoteForColumnName("GuildId"), DatabaseUtil.Dialect.QuoteForTableName(typeof(CharacterRecord).Name), DatabaseUtil.Dialect.QuoteForColumnName("Guid"), charId); var query = new ScalarQuery <int>(typeof(CharacterRecord), QueryLanguage.Sql, sql); return((uint)query.Execute()); }
/// <summary> /// 执行HQL语句 /// </summary> /// <param name="Hql"></param> /// <returns>数字</returns> #region int DAO_ExecuteSql(string Hql) public static int DAO_ExecuteSql(string Hql) { try { ScalarQuery scalarQuery = new ScalarQuery(typeof(T), Hql); object o = ExecuteQuery(scalarQuery); return(int.Parse(o.ToString())); } catch (Exception ex) { //LogHelper.WriteLog(LogTag + ".GetNum(string Hql, ArrayList paramList)", ex); return(-1); } }
public void IncompatibleGenericTypesThrowMeaningfulErrorsOnMediatorCall() { int result; var query = new ScalarQuery <int>(typeof(Post), validQuery); try { result = ActiveRecordMediator <Post> .ExecuteQuery2(query); } catch (Exception ex) { Assert.IsInstanceOf(typeof(NHibernate.QueryException), ex); Assert.IsTrue(ex.Message.Contains("Int32")); Assert.IsTrue(ex.Message.Contains("Int64")); } }
/// <summary> /// Returns all value mail that was sent to the Character with the given Id to their original sender /// </summary> public static void ReturnValueMailFor(uint charId) { var sql = string.Format("UPDATE {0} SET {1} = {2}, {3} = 0 WHERE {4} = {5} AND ({6} > 0 OR {7} > 0 OR {8} > 0)", DatabaseUtil.Dialect.QuoteForTableName(typeof(MailMessage).Name), DatabaseUtil.Dialect.QuoteForColumnName("ReceiverId"), DatabaseUtil.Dialect.QuoteForColumnName("SenderId"), DatabaseUtil.Dialect.QuoteForColumnName("CashOnDelivery"), DatabaseUtil.Dialect.QuoteForColumnName("SenderId"), charId, DatabaseUtil.Dialect.QuoteForColumnName("CashOnDelivery"), DatabaseUtil.Dialect.QuoteForColumnName("IncludedMoney"), DatabaseUtil.Dialect.QuoteForColumnName("IncludedItemCount")); var query = new ScalarQuery <long>(typeof(CharacterRecord), QueryLanguage.Sql, sql); var result = query.Execute(); }
public MLoginUser GetLoginUserInfo(string userid, string password) { MLoginUser result = null; ISession ss = holder.CreateSession(typeof(MLoginUserDaoOracleImp)); ITransaction tran = ss.BeginTransaction(); try { //result = (IList<MFunctioncatalog>)FindAll(typeof(MFunctioncatalog)); ScalarQuery <MLoginUser> q = new ScalarQuery <MLoginUser>(typeof(MLoginUser), @" from MLoginUser where Userid=:userid and Password=:password"); q.SetParameter("userid", userid); q.SetParameter("password", password); result = q.Execute(); // string query = @" // select LANGID,CATALOGID,CATALOGNAME from M_FUNCTIONCATALOG // where // LANGID = :key"; // result = ss.CreateSQLQuery(query) // .AddEntity(typeof(MFunctioncatalog)) // .SetParameter("key", langid) // .List<MFunctioncatalog>(); } catch (Castle.ActiveRecord.Framework.ActiveRecordException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } catch (DbException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } finally { tran.Dispose(); holder.ReleaseSession(ss); } return(result); }
public TTradeMs getTradeByCd(string companyCd, string dlCd) { TTradeMs tradems = null; ISession ss = holder.CreateSession(typeof(TTradeMsDaoOracleImp)); ITransaction tran = ss.BeginTransaction(); try { //result = (IList<MFunctioncatalog>)FindAll(typeof(MFunctioncatalog)); StringBuilder sb = new StringBuilder(); sb.AppendLine("from TTradeMs where Id.ICompanyCd=:ICompanyCd"); sb.AppendLine(" and Id.IDlCd=:IDlCd"); ScalarQuery <TTradeMs> q = new ScalarQuery <TTradeMs>(typeof(TTradeMs), sb.ToString()); q.SetParameter("ICompanyCd", companyCd); q.SetParameter("IDlCd", dlCd); tradems = q.Execute(); //FindByPrimaryKey找不到数据的时候是抛出ActiveRecordException,不太好处理 //termms = (TTermMs)FindByPrimaryKey(typeof(TTermMs), userid); } catch (Castle.ActiveRecord.Framework.ActiveRecordException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } catch (DbException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } finally { tran.Dispose(); holder.ReleaseSession(ss); } return(tradems); }
public TCompanyConditionMs getCompanyCondition(string companyCd) { TCompanyConditionMs companyconditionms = null; ISession ss = holder.CreateSession(typeof(TPersonMsDaoOracleImp)); ITransaction tran = ss.BeginTransaction(); try { //result = (IList<MFunctioncatalog>)FindAll(typeof(MFunctioncatalog)); ScalarQuery <TCompanyConditionMs> q = new ScalarQuery <TCompanyConditionMs>(typeof(TCompanyConditionMs), @" from TCompanyConditionMs where ICompanyCd=:ICompanyCd"); q.SetParameter("ICompanyCd", companyCd); companyconditionms = q.Execute(); //FindByPrimaryKey找不到数据的时候是抛出ActiveRecordException,不太好处理 //termms = (TTermMs)FindByPrimaryKey(typeof(TTermMs), userid); } catch (Castle.ActiveRecord.Framework.ActiveRecordException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } catch (DbException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } finally { tran.Dispose(); holder.ReleaseSession(ss); } return(companyconditionms); }
private void Init() { ScalarQuery <object> scalarQuery = new ScalarQuery <object>(this.m_type, string.Format("SELECT max(r.{0}) FROM {1} r", (object)this.m_idMember, (object)this.m_table)); object obj; try { obj = scalarQuery.Execute(); } catch (Exception ex) { NHIdGenerator.OnError(ex); obj = scalarQuery.Execute(); } this.m_highestId = obj != null ? (long)Convert.ChangeType(obj, typeof(long)) : 0L; if (this.m_highestId >= this.m_minId) { return; } this.m_highestId = this.m_minId; }
public void ValidGenericQueryWithDirectCall() { var query = new ScalarQuery <long>(typeof(Post), validQuery); Assert.AreEqual(1, query.Execute()); }
public void InvalidResultWithNonGenericQueryYieldsNull() { ScalarQuery query = new ScalarQuery(typeof(Post), invalidQuery); Assert.IsNull(ActiveRecordMediator <Post> .ExecuteQuery(query)); }
public void InvalidGenericQueryWithDirectCall() { var query = new ScalarQuery <int>(typeof(Post), invalidQuery); Assert.AreEqual(default(int), query.Execute()); }
/// <summary> /// 根据ClsCd取得DetailList /// </summary> /// <param name="langId"></param> /// <param name="clsCd"></param> /// <returns></returns> public IList <CClsDetailNoAR> GetClsDetailList(string langId, string clsCd) { IList <CClsDetailNoAR> result = new List <CClsDetailNoAR>(); ISession ss = holder.CreateSession(typeof(CClsDetailNoARDaoOracleImp)); ITransaction tran = ss.BeginTransaction(); try { //IList<TClsMs> clsmslist = new List<TClsMs>(); TClsMs clsms = null; //get role by userid ScalarQuery <TClsMs> queryclsms = new ScalarQuery <TClsMs>(typeof(TClsMs), @" from TClsMs where IClsCd=:clsCd"); queryclsms.SetParameter("clsCd", clsCd); clsms = queryclsms.Execute(); if (clsms == null) { throw new ApplicationException(MessageUtils.GetMessage("E0001")); } StringBuilder sb = new StringBuilder(); //select f.catalogid,f.functionid,f.functionimage,f.functionindex,f.functionname,f.functionpath,f.langid,c.catalogimage,c.catalogname from (M_FUNCTION f inner join (select functionid from m_role_function //where roleid=1 group by functionid) r on (f.functionid=r.functionid)) left join //m_functioncatalog c on (f.catalogid=c.catalogid and f.langid=c.langid) //where f.langid='zh-CN' //order by f.catalogid,f.functionindex if ("1".Equals(clsms.IClsCls)) { sb.Append(" select d.i_cls_cd as iClsCd,"); sb.Append(" d.i_language_cd as iLanguageCd,"); sb.Append(" d.i_cls_detail_cd as iClsDetailCd,"); sb.Append(" d.i_cls_detail_desc as iClsDetailDesc,"); sb.Append(" d.i_inq_item as iInqItem,"); sb.Append(" '' as iClsNameDesc"); sb.Append(" from t_cls_detail_ms d"); sb.Append(" where d.i_cls_cd=:clsCd"); sb.Append(" and d.i_language_cd=:langId"); } else if ("2".Equals(clsms.IClsCls)) { sb.Append(" select d.i_cls_cd as iClsCd ,"); sb.Append(" d.i_language_cd as iLanguageCd ,"); sb.Append(" d.i_name_cd as iClsDetailCd,"); sb.Append(" d.i_name_desc as iClsDetailDesc,"); sb.Append(" d.i_inq_item as iInqItem,"); sb.Append(" d.i_name_kana as iClsNameDesc"); sb.Append(" from t_desc_ms d"); sb.Append(" where d.i_cls_cd=:clsCd"); sb.Append(" and d.i_language_cd=:langId"); } ISQLQuery querycatalogfunction = ss.CreateSQLQuery(sb.ToString()); querycatalogfunction.AddScalar("iClsCd", NHibernateUtil.String); querycatalogfunction.AddScalar("iLanguageCd", NHibernateUtil.String); querycatalogfunction.AddScalar("iClsDetailCd", NHibernateUtil.String); querycatalogfunction.AddScalar("iClsDetailDesc", NHibernateUtil.String); querycatalogfunction.AddScalar("iInqItem", NHibernateUtil.String); querycatalogfunction.AddScalar("iClsNameDesc", NHibernateUtil.String); querycatalogfunction.SetParameter("langId", langId); querycatalogfunction.SetParameter("clsCd", clsms.IClsCd); result = querycatalogfunction.SetResultTransformer(Transformers.AliasToBean <CClsDetailNoAR>()).List <CClsDetailNoAR>(); tran.Commit(); } catch (Castle.ActiveRecord.Framework.ActiveRecordException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } catch (DbException ex) { tran.Rollback(); throw new ApplicationException(ex.Message, ex); } finally { tran.Dispose(); holder.ReleaseSession(ss); } return(result); }
/// <summary> /// HQL查询分页 /// </summary> /// <param name="baseQuery">HQL</param> /// <param name="queryParameters">参数对象集合</param> /// <param name="orders">排序对象集合</param> /// <param name="pageSize">页面大小</param> /// <param name="currentPage">当前页</param> /// <param name="rowCount">返回记录总数 </param> /// <param name="resultPage">返回 当前页码 </param> /// <returns>对象集合</returns> #region IList<T> DAO_GetListByPage(String where, String order, int pageSize, int currentPage, out int rowCount, out int resultPage) public static IList <T> DAO_GetListByPage(String where, String order, int pageSize, int currentPage, out int rowCount, out int resultPage) { try { T obj = (T)System.Reflection.Assembly.GetAssembly(typeof(T)).CreateInstance(typeof(T).ToString()); string hqlCount = ""; if (where.Trim() == String.Empty) { hqlCount = string.Format("select count(*) from {0}", obj.GetType().ToString()); } else { hqlCount = string.Format("select count(*) from {0} {1}", obj.GetType().ToString(), where.ToUpper().StartsWith("WHERE") ? where : "WHERE " + where); } ScalarQuery scalarQuery = new ScalarQuery(typeof(T), hqlCount); object o = ExecuteQuery(scalarQuery); rowCount = int.Parse(o.ToString()); IList <T> result = new List <T>(); string hql = ""; if (where.Trim() == String.Empty) { hql = string.Format("from {0}", obj.GetType().ToString()); } else { hql = string.Format("from {0} {1}", obj.GetType().ToString(), where.ToUpper().StartsWith("WHERE") ? where : "WHERE " + where); } if (order != String.Empty) { hql += " " + order; } SimpleQuery simpleQuery = new SimpleQuery(typeof(T), hql); resultPage = currentPage > 0 ? currentPage - 1 : 0; int pageCount = (rowCount - 1) / pageSize + 1; if (resultPage < 0) { resultPage = 0; } if (resultPage >= pageCount) { resultPage = pageCount - 1; } int startRow = resultPage * pageSize; if (startRow >= rowCount) { startRow = rowCount - 1; } simpleQuery.SetQueryRange(startRow, pageSize); return((IList <T>)ExecuteQuery(simpleQuery)); } catch (Exception ex) { rowCount = 0; resultPage = 0; //LogHelper.WriteLog(LogTag + ".GetListByProperty(string baseQuery, object[] queryParameters, IList<QueryOrder> orders, int pageSize, int currentPage, out int rowCount, out int resultPage)", ex); return(default(IList <T>)); } }