/// <summary> /// Builds the specified lazy load proxy for a concrete class with virtual method. /// </summary> /// <param name="dataMapper">The data mapper.</param> /// <param name="selectStatement">The mapped statement used to build the lazy loaded object.</param> /// <param name="param">The parameter object used to build lazy loaded object.</param> /// <param name="target">The target object which contains the property proxydied..</param> /// <param name="setAccessor">The proxified member accessor.</param> /// <returns>Return a proxy object</returns> public object CreateProxy( IDataMapper dataMapper, IMappedStatement selectStatement, object param, object target, ISetAccessor setAccessor) { Type typeProxified = setAccessor.MemberType; if (logger.IsDebugEnabled) { logger.Debug(string.Format("Statement '{0}', create proxy for member {1}.", selectStatement.Id, setAccessor.MemberType)); } // Build the proxy IInterceptor interceptor= new LazyLoadInterceptor(dataMapper, selectStatement, param, target, setAccessor); // if you want to proxy concrete classes, there are also 2 main requirements : // the class can not be sealed and only virtual methods can be intercepted. // The reason is that DynamicProxy will create a subclass of your class overriding all methods // so it can dispatch the invocations to the interceptor. // The proxified type must also have an empty constructor object generatedProxy = proxyGenerator.CreateClassProxy(typeProxified, Type.EmptyTypes, interceptor); return generatedProxy; }
/// <summary> /// Constructor /// </summary> /// <param name="mappedStatement"></param> /// <param name="parameterObject"></param> /// <param name="pageSize"></param> public PaginatedList(IMappedStatement mappedStatement, object parameterObject, int pageSize) { _mappedStatement = mappedStatement; _parameterObject = parameterObject; _pageSize = pageSize; _index = 0; PageTo(0); }
/// <summary> /// Constructor for a lazy list loader /// </summary> /// <param name="mappedSatement">The mapped statement used to build the list</param> /// <param name="param">The parameter object used to build the list</param> /// <param name="setAccessor">The proxified member accessor.</param> /// <param name="target">The target object which contains the property proxydied.</param> internal LazyLoadInterceptor(IMappedStatement mappedSatement, object param, object target, ISetAccessor setAccessor) { _param = param; _statementName = mappedSatement.Id; _sqlMap = mappedSatement.SqlMap; _target = target; _setAccessor = setAccessor; }
/// <summary> /// Create a new proxy instance. /// </summary> /// <param name="dataMapper">The data mapper.</param> /// <param name="mappedStatement">The mapped statement.</param> /// <param name="param">The param.</param> /// <param name="target">The target.</param> /// <param name="setAccessor">The set accessor.</param> /// <returns>Returns a new proxy.</returns> public object CreateProxy( IDataMapper dataMapper, IMappedStatement mappedStatement, object param, object target, ISetAccessor setAccessor) { return new LazyList(dataMapper, mappedStatement, param, target, setAccessor); }
/// <summary> /// Initializes a new instance of the <see cref="T:LazyList"/> class. /// </summary> /// <param name="mappedSatement">The mapped satement.</param> /// <param name="param">The param.</param> /// <param name="target">The target.</param> /// <param name="setAccessor">The set accessor.</param> public LazyList(IMappedStatement mappedSatement, object param, object target, ISetAccessor setAccessor) { _list = new ArrayList(); _param = param; _statementId = mappedSatement.Id; _sqlMap = mappedSatement.SqlMap; _target = target; _setAccessor = setAccessor; }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the sql command text to execute. /// </summary> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISqlMapSession session) { RequestScope request = new RequestScope(_dataExchangeFactory, session, _statement); request.PreparedStatement = _preparedStatement; request.MappedStatement = mappedStatement; return request; }
/// <summary> /// Constructor for a lazy list loader /// </summary> /// <param name="dataMapper">The data mapper.</param> /// <param name="mappedSatement">The mapped statement used to build the list</param> /// <param name="param">The parameter object used to build the list</param> /// <param name="target">The target object which contains the property proxydied.</param> /// <param name="setAccessor">The proxified member accessor.</param> public LazyLoadInterceptor( IDataMapper dataMapper, IMappedStatement mappedSatement, object param, object target, ISetAccessor setAccessor) { this.param = param; statementName = mappedSatement.Id; this.dataMapper = dataMapper; this.target = target; this.setAccessor = setAccessor; }
internal override void Initialize(ConfigurationScope configurationScope) { if (this.PropertyName.Length > 0) { IMappedStatement mappedStatement = configurationScope.SqlMapper.GetMappedStatement(base.Id); Type parameterClass = mappedStatement.Statement.ParameterClass; if ((parameterClass != null) && !ObjectProbe.IsSimpleType(parameterClass)) { configurationScope.ErrorContext.MoreInfo = string.Format("Looking for settable property named '{0}' on type '{1}' for selectKey node of statement id '{2}'.", this.PropertyName, mappedStatement.Statement.ParameterClass.Name, base.Id); ReflectionInfo.GetInstance(mappedStatement.Statement.ParameterClass).GetSetter(this.PropertyName); } } base.Initialize(configurationScope); }
/// <summary> /// Create a new proxy instance. /// </summary> /// <param name="mappedStatement">The mapped statement.</param> /// <param name="param">The param.</param> /// <param name="target">The target.</param> /// <param name="setAccessor">The set accessor.</param> /// <returns>Returns a new proxy.</returns> public object CreateProxy(IMappedStatement mappedStatement, object param, object target, ISetAccessor setAccessor) { Type elementType = setAccessor.MemberType.GetGenericArguments()[0]; Type lazyType = typeof(LazyListGeneric<>); Type lazyGenericType = lazyType.MakeGenericType(elementType); Type[] parametersType = { typeof(IMappedStatement), typeof(object), typeof(object), typeof(ISetAccessor) }; IFactory factory = mappedStatement.SqlMap.DataExchangeFactory.ObjectFactory.CreateFactory(lazyGenericType, parametersType); object[] parameters = { mappedStatement, param, target, setAccessor }; return factory.CreateInstance(parameters); }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> text to execute. /// </summary> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISqlMapSession session) { RequestScope request = new RequestScope(_dataExchangeFactory, session, _statement); _paramParser = new InlineParameterMapParser(); string sqlStatement = Process(request, parameterObject); request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement); request.MappedStatement = mappedStatement; return(request); }
public static IList <T> QueryPageList <T>(IDialect dialect, ISqlMapper sqlMap, String statementName, Object parameter, int offset, int limit) { IMappedStatement statement = sqlMap.GetMappedStatement(statementName); if (!sqlMap.IsSessionStarted) { sqlMap.OpenConnection(); } RequestScope request = statement.Statement.Sql.GetRequestScope(statement, parameter, sqlMap.LocalSession); request.PreparedStatement.PreparedSql = dialect.GetLimitString(request.PreparedStatement.PreparedSql, offset, limit); //Console.WriteLine(dialect.GetType().FullName + "------" + request.PreparedStatement.PreparedSql); statement.PreparedCommand.Create(request, sqlMap.LocalSession, statement.Statement, parameter); return(RunQueryForList <T>(request, sqlMap.LocalSession, parameter, statement.Statement)); }
public string GetSql(string statementName, object paramObject) { IMappedStatement statement = SqlMap.GetMappedStatement(statementName); if (!SqlMap.IsSessionStarted) { SqlMap.OpenConnection(); } RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, SqlMap. LocalSession); return(scope.PreparedStatement.PreparedSql); }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> text to execute. /// </summary> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope( IMappedStatement mappedStatement, object parameterObject, ISession session) { RequestScope request = new RequestScope(dataExchangeFactory, session, statement); //处理动态SQL语句 将分散的SQL语句拼接成完整的语句 和完整的参数信息列表 string sql = Process(request, parameterObject); //根据参数属性集合生成对应的IDbParameter集合 request.PreparedStatement = BuildPreparedStatement(session, request, sql); request.MappedStatement = mappedStatement; return(request); }
/// <summary> /// 查询列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="statementName"></param> /// <param name="parameterObject"></param> /// <returns></returns> public virtual IList <T> ExecuteQueryForList <T>(string statementName, object parameterObject) { using (SqlMapDaoSession session = DaoSession) { var sqlMapper = session.SqlMap; IMappedStatement statement = sqlMapper.GetMappedStatement(statementName); if (!sqlMapper.IsSessionStarted) { sqlMapper.OpenConnection(); } RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, parameterObject, sqlMapper.LocalSession); string result = scope.PreparedStatement.PreparedSql; return(session.SqlMap.QueryForList <T>(statementName, parameterObject)); } }
public DataSet SelectDS(string statementName, object paramObject) { DataSet ds = new DataSet(); try { IMappedStatement statement = mapper.GetMappedStatement(statementName); if (!mapper.IsSessionStarted) { mapper.OpenConnection(); } RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession); statement.PreparedCommand.Create(scope, mapper.LocalSession, statement.Statement, paramObject); IDbCommand command = mapper.LocalSession.CreateCommand(CommandType.Text); command.CommandText = scope.IDbCommand.CommandText; foreach (IDataParameter pa in scope.IDbCommand.Parameters) { if (Compare(mapper)) { command.Parameters.Add(new OracleParameter(pa.ParameterName, pa.Value)); } else { command.Parameters.Add(new SqlParameter(pa.ParameterName, pa.Value)); } } mapper.LocalSession.CreateDataAdapter(command).Fill(ds); } catch (Exception ex) { throw new Exception(ex.Message); } finally { try { mapper.CloseConnection(); } catch (Exception ex) { throw new Exception(ex.Message); } } return(ds); }
public void Set(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader, object keys) { IMappedStatement mappedStatement = request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select); PostBindind bindind = new PostBindind { Statement = mappedStatement, Keys = keys, Target = target, ResultProperty = mapping }; if (mapping.IsLazyLoad) { throw new NotImplementedException("Lazy load no supported for System.Array property:" + mapping.SetAccessor.Name); } bindind.Method = PostBindind.ExecuteMethod.ExecuteQueryForArrayList; request.QueueSelect.Enqueue(bindind); }
public DataTable ExecuteDataTable(string statementName, object paramObject) { try { var ds = new DataSet(); IMappedStatement statement = SqlMap.GetMappedStatement(statementName); if (!SqlMap.IsSessionStarted) { SqlMap.OpenConnection(); } RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, SqlMap. LocalSession); statement.PreparedCommand.Create(scope, SqlMap.LocalSession, statement.Statement, paramObject); IDbCommand dc = SqlMap.LocalSession.CreateCommand(scope.IDbCommand.CommandType); dc.CommandText = scope.IDbCommand.CommandText; if (scope.IDbCommand.Parameters != null) { foreach (IDbDataParameter para in scope.IDbCommand.Parameters) { IDbDataParameter param = SqlMap.LocalSession.CreateDataParameter(); param.ParameterName = para.ParameterName; param.Value = para.Value; dc.Parameters.Add(param); } } IDbDataAdapter dda = SqlMap.LocalSession.CreateDataAdapter(dc); dda.Fill(ds); return(ds.Tables[0]); } catch (Exception e) { throw new IBatisNetException( "Error executing query '" + statementName + "' for list. Cause: " + e.Message, e); } }
/// <summary> /// Gets the value of an argument constructor. /// </summary> /// <param name="request">The current <see cref="RequestScope"/>.</param> /// <param name="mapping">The <see cref="ResultProperty"/> with the argument infos.</param> /// <param name="reader">The current <see cref="IDataReader"/>.</param> /// <param name="keys">The keys</param> /// <returns>The paremeter value.</returns> public object GetValue(RequestScope request, ResultProperty mapping, ref IDataReader reader, object keys) { // Get the select statement IMappedStatement selectStatement = request.MappedStatement.ModelStore.GetMappedStatement(mapping.Select); if (mapping.MemberType == typeof(IList)) { reader = DataReaderTransformer.Transform(reader, request.Session.SessionFactory.DataSource.DbProvider); return(selectStatement.ExecuteQueryForList(request.Session, keys)); } reader = DataReaderTransformer.Transform(reader, request.Session.SessionFactory.DataSource.DbProvider); IFactory factory = request.DataExchangeFactory.ObjectFactory.CreateFactory(mapping.MemberType, Type.EmptyTypes); object values = factory.CreateInstance(null); selectStatement.ExecuteQueryForList(request.Session, keys, (IList)values); return(values); }
/// <summary> /// Gets the SQL. /// </summary> /// <param name="mappedStatement">The mapped statement.</param> /// <param name="parameterObject">The parameter object.</param> /// <returns></returns> /// <remarks> /// Paremeters should be typeof IDictionary<string, object> /// </remarks> public string GetSql(IMappedStatement mappedStatement, object parameterObject) { Contract.Assert.That(parameterObject, Is.TypeOf<IDictionary<string, object>>()).When("Processing NVelocity source for statement :" + mappedStatement.Id); StringWriter sw = new StringWriter(); ExternalSql externalSql = (ExternalSql)mappedStatement.Statement.Sql; string commandText = externalSql.CommandText; if (commandText.Contains(VELOCITY_DIRECTIVE)) { VelocityContext velocityContext = new VelocityContext(); IDictionary<string, object> dico = (IDictionary<string, object>)parameterObject; foreach(string key in dico.Keys) { velocityContext.Put(key, dico[key]); } try { velocityEngine.Evaluate(velocityContext, sw, "error", commandText); } catch (Exception ex) { if (logger.IsDebugEnabled) { logger.Debug("Could not parse velocity string '" + commandText + "' for " + mappedStatement.Id); } throw new DataMapperException("Could not parse velocity string '" + commandText + "' for " + mappedStatement.Id); } commandText = sw.GetStringBuilder().ToString(); } if (logger.IsDebugEnabled) { logger.Debug("SQL command text parse by velocity: " + commandText); } return commandText; }
/// <summary> /// Gets the SQL text. /// </summary> /// <param name="mappedStatement">The mapped statement.</param> /// <param name="parameterObject">The parameter object.</param> /// <returns></returns> public string GetSql(IMappedStatement mappedStatement, object parameterObject) { return "select * from Accounts where Account_ID = @{Id}"; }
/// <summary> /// Adds a (named) MappedStatement. /// </summary> /// <param name="mappedStatement">The statement to add</param> public void AddMappedStatement(IMappedStatement mappedStatement) { if (mappedStatements.ContainsKey(mappedStatement.Id)) { throw new DataMapperException("The DataMapper already contains a MappedStatement named " + mappedStatement.Id); } mappedStatements.Add(mappedStatement.Id, mappedStatement); }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the sql command text to execute. /// </summary> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope( IMappedStatement mappedStatement, object parameterObject, ISession session) { string sql = ProcessDynamicElements(parameterObject); RequestScope request = new RequestScope( dataExchangeFactory, session, statement); request.PreparedStatement = BuildPreparedStatement(session, request, sql); request.MappedStatement = mappedStatement; return request; }
/// <summary> /// Event listener /// </summary> /// <param name="mappedStatement">A MappedStatement on which we listen ExecuteEventArgs event.</param> public void RegisterTriggerStatement(IMappedStatement mappedStatement) { mappedStatement.Executed +=FlushHandler; }
/// <summary> /// Event listener /// </summary> /// <param name="mappedStatement">A MappedStatement on which we listen ExecuteEventArgs event.</param> public void RegisterTriggerStatement(IMappedStatement mappedStatement) { mappedStatement.Execute +=new ExecuteEventHandler(FlushHandler); }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> text to execute. /// </summary> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <param name="parameterObject">The parameter object (used by DynamicSql/SimpleDynamicSql). /// Use to complete the sql statement.</param> /// <param name="session">The current session</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISession session) { RequestScope request = new RequestScope(dataExchangeFactory, session, statement); string sqlCommandText = statement.SqlSource.GetSql(mappedStatement, parameterObject); string newSqlCommandText = string.Empty; if (request.ParameterMap==null) { request.ParameterMap = inlineParemeterMapBuilder.BuildInlineParemeterMap(statement, sqlCommandText, out newSqlCommandText); } // Processes $substitutions$ after DynamicSql if (SimpleDynamicSql.IsSimpleDynamicSql(newSqlCommandText)) { newSqlCommandText = new SimpleDynamicSql( dataExchangeFactory, dbHelperParameterCache, newSqlCommandText, statement).GetSql(parameterObject); } request.PreparedStatement = BuildPreparedStatement(session, request, newSqlCommandText); return request; }
/// <summary> /// Creates an IDbCommand fills its parameters based on the parameterObject. /// </summary> /// <param name="mappedStatement"></param> /// <param name="parameterObject"></param> /// <returns>An IDbCommand with all the IDataParameter filled.</returns> public IDbCommand CreateCommand(IMappedStatement mappedStatement, object parameterObject) { IDbProvider dbProvider = mappedStatement.ModelStore.SessionFactory.DataSource.DbProvider; IStatement statement = mappedStatement.Statement; RequestScope request = statement.Sql.GetRequestScope(mappedStatement, parameterObject, null); request.IDbCommand = CreateCommandAndEnlistTransaction(dbProvider, statement.CommandType, null); request.IDbCommand.CommandText = request.PreparedStatement.PreparedSql; request.IDbCommand.CommandType = statement.CommandType; if (log.IsDebugEnabled) { log.Debug("Preparing to apply parameter information to Statement Id: [" + statement.Id + "] based off of PreparedStatement: [" + request.IDbCommand.CommandText + "]"); } ApplyParameterMap(dbProvider, request.IDbCommand, request, statement, parameterObject); return request.IDbCommand; }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> text to execute. /// </summary> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISqlMapSession session) { RequestScope request = new RequestScope( _dataExchangeFactory, session, _statement); _paramParser = new InlineParameterMapParser(); string sqlStatement = Process(request, parameterObject); request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement); request.MappedStatement = mappedStatement; return request; }
/// <summary> /// Initializes a new instance of the <see cref="T:LazyList"/> class. /// </summary> /// <param name="dataMapper">The data mapper.</param> /// <param name="mappedSatement">The mapped satement.</param> /// <param name="param">The param.</param> /// <param name="target">The target.</param> /// <param name="setAccessor">The set accessor.</param> public LazyList( IDataMapper dataMapper, IMappedStatement mappedSatement, object param, object target, ISetAccessor setAccessor) { _list = new ArrayList(); _param = param; _statementId = mappedSatement.Id; this.dataMapper = dataMapper; _target = target; _setAccessor = setAccessor; }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> text to execute. /// </summary> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope( IMappedStatement mappedStatement, object parameterObject, ISession session) { RequestScope request = new RequestScope( dataExchangeFactory, session, statement); //处理动态SQL语句 将分散的SQL语句拼接成完整的语句 和完整的参数信息列表 string sql = Process(request, parameterObject); //根据参数属性集合生成对应的IDbParameter集合 request.PreparedStatement = BuildPreparedStatement(session, request, sql); request.MappedStatement = mappedStatement; return request; }
/// <summary> /// Builds a new <see cref="RequestScope"/> and the sql command text to execute. /// </summary> /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param> /// <param name="parameterObject">The parameter object (used in DynamicSql)</param> /// <param name="session">The current session</param> /// <returns>A new <see cref="RequestScope"/>.</returns> public RequestScope GetRequestScope( IMappedStatement mappedStatement, object parameterObject, ISession session) { RequestScope request = new RequestScope(dataExchangeFactory, session, statement); request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement); request.MappedStatement = mappedStatement; return request; }