/// <summary>
        ///
        /// </summary>
        /// <param name="index"></param>
        /// <param name="localPageSize"></param>
        /// <returns></returns>
        private IList GetList(int index, int localPageSize)
        {
            bool isSessionLocal = false;

            ISqlMapSession session = _mappedStatement.SqlMap.LocalSession;

            if (session == null)
            {
                session = new SqlMapSession(_mappedStatement.SqlMap);
                session.OpenConnection();
                isSessionLocal = true;
            }

            IList list = null;

            try
            {
                list = _mappedStatement.ExecuteQueryForList(session, _parameterObject, (index) * _pageSize, localPageSize);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (isSessionLocal)
                {
                    session.CloseConnection();
                }
            }

            return(list);
        }
Beispiel #2
0
        public void TestMappedStatementQueryWithReadWriteCacheWithSession()
        {
            IMappedStatement statement = sqlMap.GetMappedStatement("GetRWNSCachedAccountsViaResultMap");
            ISqlMapSession   session   = new SqlMapSession(sqlMap);

            session.OpenConnection();

            int firstId  = 0;
            int secondId = 0;

            try
            {
                // execute the statement twice; the second call should result in a cache hit
                IList list = statement.ExecuteQueryForList(session, null);
                firstId = HashCodeProvider.GetIdentityHashCode(list);

                list     = statement.ExecuteQueryForList(session, null);
                secondId = HashCodeProvider.GetIdentityHashCode(list);
            }
            finally
            {
                session.CloseConnection();
            }

            Assert.AreEqual(firstId, secondId);
        }
        private IList GetList(int index, int localPageSize)
        {
            bool           flag         = false;
            ISqlMapSession localSession = this._mappedStatement.SqlMap.LocalSession;

            if (localSession == null)
            {
                localSession = new SqlMapSession(this._mappedStatement.SqlMap);
                localSession.OpenConnection();
                flag = true;
            }
            IList list = null;

            try
            {
                list = this._mappedStatement.ExecuteQueryForList(localSession, this._parameterObject, index * this._pageSize, localPageSize);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (flag)
                {
                    localSession.CloseConnection();
                }
            }
            return(list);
        }
Beispiel #4
0
        private IDbCommand GetDbCommand(string statementName, object paramObject)
        {
            IStatement statement = SqlMap.GetMappedStatement(statementName).Statement;

            IMappedStatement mapStatement = SqlMap.GetMappedStatement(statementName);

            ISqlMapSession session = new SqlMapSession(SqlMap);

            if (SqlMap.LocalSession != null)
            {
                session = SqlMap.LocalSession;
            }
            else
            {
                session = SqlMap.OpenConnection();
            }

            RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session);

            mapStatement.PreparedCommand.Create(request, session, statement, paramObject);

            IDbCommand command = request.IDbCommand;

            // Ibatis 这里做了个装饰,所以得到的类型不是SqlCommand之类的类型
            // 只能暂时使用反射把它装饰的类型(即真实类型)反射出来
            Type       t              = command.GetType();
            FieldInfo  commandField   = t.GetField("_innerDbCommand", BindingFlags.Instance | BindingFlags.NonPublic);
            IDbCommand innerDbCommand = commandField.GetValue(command) as IDbCommand;

            return(innerDbCommand);    // request.IDbCommand;
        }
Beispiel #5
0
        /**/
        /// <summary>
        /// 得到参数化后的SQL
        /// </summary>
        public static string QueryForSql(string tag, object paramObject)
        {
            IStatement       statement    = SqlMap.GetMappedStatement(tag).Statement;
            IMappedStatement mapStatement = SqlMap.GetMappedStatement(tag);
            ISqlMapSession   session      = new SqlMapSession(SqlMap);
            RequestScope     request      = statement.Sql.GetRequestScope(mapStatement, paramObject, session);

            return(request.PreparedStatement.PreparedSql);
        }
Beispiel #6
0
        /// <summary>
        /// 获取查询语句
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected string GetPreparedSql(string statementName, object parameterObject)
        {
            IMappedStatement mappedStatement = SqlMap.GetMappedStatement(statementName);
            ISqlMapSession   localSession    = SqlMap.LocalSession;
            IStatement       statement       = mappedStatement.Statement;

            if (localSession == null)
            {
                localSession = new SqlMapSession(SqlMap);
            }
            return(statement.Sql.GetRequestScope(mappedStatement, parameterObject, localSession).PreparedStatement.PreparedSql);
        }
Beispiel #7
0
        /// <summary>
        /// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数,注:不支持#变量替换)
        /// </summary>
        /// <param name="statementName">语句ID</param>
        /// <param name="paramObject">语句所需要的参数</param>
        /// <param name="htOutPutParameter">Output参数值哈希表</param>
        /// <returns>得到的DataTable</returns>
        public static DataTable ExecuteQueryForDataTable(string statementName, object paramObject, out Hashtable htOutPutParameter)
        {
            ISqlMapper  sqlMap         = Mapper();
            DataSet     ds             = new DataSet();
            bool        isSessionLocal = false;
            IDalSession session        = sqlMap.LocalSession;

            if (session == null)
            {
                session = new SqlMapSession(sqlMap);
                session.OpenConnection();
                isSessionLocal = true;
            }

            IDbCommand cmd = GetCommand(statementName, paramObject);

            cmd.CommandTimeout = 0;

            try
            {
                cmd.Connection = session.Connection;
                IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch (Exception e)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
            }
            finally
            {
                if (isSessionLocal)
                {
                    session.CloseConnection();
                }
            }

            htOutPutParameter = new Hashtable();
            foreach (IDataParameter parameter in cmd.Parameters)
            {
                if (parameter.Direction == ParameterDirection.Output)
                {
                    htOutPutParameter[parameter.ParameterName] = parameter.Value;
                }
            }


            return(ds.Tables[0]);
        }
Beispiel #8
0
 private System.Data.IDbCommand GetDbCommand(ISqlMapper sqlMapper, string statementName, StrObjectDict paramObject, StrObjectDict dictParam, System.Collections.Generic.IDictionary <string, System.Data.ParameterDirection> dictParmDirection, System.Data.CommandType cmdType)
 {
     System.Data.IDbCommand result;
     if (cmdType == System.Data.CommandType.Text)
     {
         result = this.GetDbCommand(sqlMapper, statementName, paramObject);
     }
     else
     {
         IStatement       statement       = sqlMapper.GetMappedStatement(statementName).Statement;
         IMappedStatement mappedStatement = sqlMapper.GetMappedStatement(statementName);
         ISqlMapSession   sqlMapSession   = new SqlMapSession(sqlMapper);
         if (sqlMapper.LocalSession != null)
         {
             sqlMapSession = sqlMapper.LocalSession;
         }
         else
         {
             sqlMapSession = sqlMapper.OpenConnection();
         }
         RequestScope requestScope = statement.Sql.GetRequestScope(mappedStatement, paramObject, sqlMapSession);
         mappedStatement.PreparedCommand.Create(requestScope, sqlMapSession, statement, paramObject);
         System.Data.IDbCommand dbCommand = sqlMapSession.CreateCommand(cmdType);
         dbCommand.CommandText = requestScope.IDbCommand.CommandText;
         if (cmdType != System.Data.CommandType.StoredProcedure || dictParam == null)
         {
             result = dbCommand;
         }
         else
         {
             foreach (System.Collections.Generic.KeyValuePair <string, object> current in dictParam)
             {
                 string text = current.Key.ToString();
                 System.Data.IDbDataParameter dbDataParameter = dbCommand.CreateParameter();
                 dbDataParameter.ParameterName = text;
                 dbDataParameter.Value         = current.Value;
                 if (dictParmDirection != null && dictParmDirection.ContainsKey(text))
                 {
                     dbDataParameter.Direction = dictParmDirection[text];
                 }
                 dbCommand.Parameters.Add(dbDataParameter);
             }
             result = dbCommand;
         }
     }
     return(result);
 }
Beispiel #9
0
        /// <summary>
        /// 用的以DataSet的方式得到Select的结果(xml文件中参数要使用$标记的占位参数. 注:不支持#变量替换)
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public static DataSet ExecuteQueryForDataSet(string statementName, object paramObject)
        {
            ISqlMapper sqlMap = Mapper();
            DataSet    ds     = new DataSet();


            bool        isSessionLocal = false;
            IDalSession session        = sqlMap.LocalSession;

            if (session == null)
            {
                session = new SqlMapSession(sqlMap);
                session.OpenConnection();
                isSessionLocal = true;
            }


            IDbCommand cmd = GetCommand(statementName, paramObject);

            cmd.CommandTimeout = 0;

            try
            {
                cmd.Connection = session.Connection;

                IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch (Exception e)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for object.  Cause: " + e.Message, e);
            }
            finally
            {
                if (isSessionLocal)
                {
                    session.CloseConnection();
                }
            }

            return(ds);
        }
Beispiel #10
0
        public System.Data.IDbCommand GetDbCommand(ISqlMapper sqlMapper, string statementName, object paramObject)
        {
            IStatement       statement       = sqlMapper.GetMappedStatement(statementName).Statement;
            IMappedStatement mappedStatement = sqlMapper.GetMappedStatement(statementName);
            ISqlMapSession   sqlMapSession   = new SqlMapSession(sqlMapper);

            if (sqlMapper.LocalSession != null)
            {
                sqlMapSession = sqlMapper.LocalSession;
            }
            else
            {
                sqlMapSession = sqlMapper.OpenConnection();
            }
            RequestScope requestScope = statement.Sql.GetRequestScope(mappedStatement, paramObject, sqlMapSession);

            mappedStatement.PreparedCommand.Create(requestScope, sqlMapSession, statement, paramObject);
            System.Data.IDbCommand dbCommand = sqlMapSession.CreateCommand(System.Data.CommandType.Text);
            dbCommand.CommandText = requestScope.IDbCommand.CommandText;
            return(dbCommand);
        }
Beispiel #11
0
        /// <summary>
        /// 用于分页控件使用
        /// </summary>
        /// <param name="tag">语句ID</param>
        /// <param name="paramObject">语句所需要的参数</param>
        /// <param name="PageSize">每页显示数目</param>
        /// <param name="curPage">当前页</param>
        /// <param name="recCount">记录总数</param>
        /// <returns>得到的DataTable</returns>
        public static DataTable QueryForDataTable(string tag, object paramObject, int PageSize, int curPage, out int recCount)
        {
            IDataReader dr             = null;
            bool        isSessionLocal = false;
            string      sql            = QueryForSql(tag, paramObject);
            string      strCount       = "select count(*) " + sql.Substring(sql.ToLower().IndexOf("from"));

            IDalSession session = SqlMap.LocalSession;
            DataTable   dt      = new DataTable();

            if (session == null)
            {
                session = new SqlMapSession(SqlMap);
                session.OpenConnection();
                isSessionLocal = true;
            }
            try
            {
                IDbCommand cmdCount = GetDbCommand(tag, paramObject);
                cmdCount.Connection  = session.Connection;
                cmdCount.CommandText = strCount;
                object count = cmdCount.ExecuteScalar();
                recCount = Convert.ToInt32(count);

                IDbCommand cmd = GetDbCommand(tag, paramObject);
                cmd.Connection = session.Connection;
                dr             = cmd.ExecuteReader();

                dt = QueryForPaging(dr, PageSize, curPage);
            }
            finally
            {
                if (isSessionLocal)
                {
                    session.CloseConnection();
                }
            }
            return(dt);
        }
Beispiel #12
0
        public System.Data.DataTable QueryForDataTable(string statementName, StrObjectDict paramObject, StrObjectDict dictParam, System.Collections.Generic.IDictionary <string, System.Data.ParameterDirection> dictParamDirection, out System.Collections.Hashtable htOutPutParameter)
        {
            ISqlMapper sqlMap = this.GetSqlMap();

            System.Data.DataSet dataSet  = new System.Data.DataSet();
            bool           flag          = false;
            ISqlMapSession sqlMapSession = sqlMap.LocalSession;

            if (sqlMapSession == null)
            {
                sqlMapSession = new SqlMapSession(sqlMap);
                sqlMapSession.OpenConnection();
                flag = true;
            }
            System.Data.IDbCommand dbCommand = this.GetDbCommand(sqlMap, statementName, paramObject, dictParam, dictParamDirection, System.Data.CommandType.StoredProcedure);
            try
            {
                dbCommand.Connection = sqlMapSession.Connection;
                System.Data.IDbDataAdapter dbDataAdapter = sqlMapSession.CreateDataAdapter(dbCommand);
                dbDataAdapter.Fill(dataSet);
            }
            finally
            {
                if (flag)
                {
                    sqlMapSession.CloseConnection();
                }
            }
            htOutPutParameter = new System.Collections.Hashtable();
            foreach (System.Data.IDataParameter dataParameter in dbCommand.Parameters)
            {
                if (dataParameter.Direction == System.Data.ParameterDirection.Output)
                {
                    htOutPutParameter[dataParameter.ParameterName] = dataParameter.Value;
                }
            }
            return(dataSet.Tables[0]);
        }
Beispiel #13
0
            public void Run()
            {
                try
                {
                    IMappedStatement statement = _sqlMap.GetMappedStatement(_statementName);
                    ISqlMapSession   session   = new SqlMapSession(sqlMap);
                    session.OpenConnection();
                    IList list = statement.ExecuteQueryForList(session, null);

                    //int firstId = HashCodeProvider.GetIdentityHashCode(list);

                    list = statement.ExecuteQueryForList(session, null);
                    int secondId = HashCodeProvider.GetIdentityHashCode(list);

                    _results["id"]   = secondId;
                    _results["list"] = list;
                    session.CloseConnection();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
Beispiel #14
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		/// <param name="localPageSize"></param>
		/// <returns></returns>
		private IList GetList(int index, int localPageSize) 
		{
			bool isSessionLocal = false;

			ISqlMapSession session = _mappedStatement.SqlMap.LocalSession;

			if (session == null) 
			{
				session = new SqlMapSession(_mappedStatement.SqlMap);
				session.OpenConnection();
				isSessionLocal = true;
			}

			IList list = null;
			try 
			{
				list = _mappedStatement.ExecuteQueryForList(session, _parameterObject, (index) * _pageSize, localPageSize);
			} 
			catch
			{
				throw;
			}
			finally
			{
				if ( isSessionLocal )
				{
					session.CloseConnection();
				}
			}

			return list;
		}
        /// <summary>
        /// Apply inline paremeterMap
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="sqlStatement"></param>
        private void ApplyInlineParemeterMap( IStatement statement, string sqlStatement)
        {
            string newSql = sqlStatement;

            _configScope.ErrorContext.MoreInfo = "apply inline parameterMap";

            // Check the inline parameter
            if (statement.ParameterMap == null)
            {
                // Build a Parametermap with the inline parameters.
                // if they exist. Then delete inline infos from sqltext.

                SqlText sqlText = _paramParser.ParseInlineParameterMap(_configScope,  statement, newSql );

                if (sqlText.Parameters.Length > 0)
                {
                    ParameterMap map = new ParameterMap(_configScope.DataExchangeFactory);
                    map.Id = statement.Id + "-InLineParameterMap";
                    if (statement.ParameterClass!=null)
                    {
                        map.Class = statement.ParameterClass;
                    }
                    map.Initialize(_configScope.DataSource.DbProvider.UsePositionalParameters,_configScope);
                    if (statement.ParameterClass==null &&
                        sqlText.Parameters.Length==1 && sqlText.Parameters[0].PropertyName=="value")//#value# parameter with no parameterClass attribut
                    {
                        map.DataExchange = _configScope.DataExchangeFactory.GetDataExchangeForClass( typeof(int) );//Get the primitiveDataExchange
                    }
                    statement.ParameterMap = map;

                    int lenght = sqlText.Parameters.Length;
                    for(int index=0;index<lenght;index++)
                    {
                        map.AddParameterProperty( sqlText.Parameters[index] );
                    }
                }
                newSql = sqlText.Text;
            }

            ISql sql = null;

            newSql = newSql.Trim();

            if (SimpleDynamicSql.IsSimpleDynamicSql(newSql))
            {
                sql = new SimpleDynamicSql(_configScope, newSql, statement);
            }
            else
            {
                if (statement is Procedure)
                {
                    sql = new ProcedureSql(_configScope, newSql, statement);
                    // Could not call BuildPreparedStatement for procedure because when NUnit Test
                    // the database is not here (but in theory procedure must be prepared like statement)
                    // It's even better as we can then switch DataSource.
                }
                else if (statement is Statement)
                {
                    sql = new StaticSql(_configScope, statement);
                    ISqlMapSession session = new SqlMapSession(_configScope.SqlMapper);

                    ((StaticSql)sql).BuildPreparedStatement( session, newSql );
                }
            }
            statement.Sql = sql;
        }
        /// <summary>
        /// Generate the command text for CRUD operation
        /// </summary>
        /// <param name="configScope"></param>
        /// <param name="statement"></param>
        private void GenerateCommandText(ConfigurationScope configScope, IStatement statement)
        {
            string generatedSQL;

            //------ Build SQL CommandText
            generatedSQL = SqlGenerator.BuildQuery(statement);

            ISql sql = new StaticSql(configScope, statement);
            ISqlMapSession session = new SqlMapSession(configScope.SqlMapper);

            ((StaticSql)sql).BuildPreparedStatement( session, generatedSQL );
            statement.Sql = sql;
        }
Beispiel #17
0
            public void Run()
            {
                try
                {
                    IMappedStatement statement = _sqlMap.GetMappedStatement( _statementName );
                    ISqlMapSession session = new SqlMapSession(sqlMap);
                    session.OpenConnection();
                    IList list = statement.ExecuteQueryForList(session, null);

                    //int firstId = HashCodeProvider.GetIdentityHashCode(list);

                    list = statement.ExecuteQueryForList(session, null);
                    int secondId = HashCodeProvider.GetIdentityHashCode(list);

                    _results["id"] = secondId ;
                    _results["list"] = list;
                    session.CloseConnection();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }