/// <summary> /// Supprimé tous les objets correspondant aux critères. /// </summary> /// <param name="criteria">Critères de suppression.</param> public virtual void DeleteAllByCriteria(FilterCriteria criteria) { if (criteria == null) { throw new ArgumentNullException("criteria"); } using (ServiceScope tx = new ServiceScope(TransactionScopeOption.Required)) { _store.RemoveAllByCriteria(criteria); tx.Complete(); } if (_hasCache) { FlushCache(); } }
/// <summary> /// Charge dans un objet le bean à partir de sa clef primaire. /// </summary> /// <param name="destination">Objet à charger.</param> /// <param name="primaryKey">Valeur de la clef primaire.</param> public override void Load(T destination, object primaryKey) { if (primaryKey == null) { throw new ArgumentNullException("primaryKey"); } FilterCriteria criteria = new FilterCriteria(); criteria.AddCriteria(_pkName, Expression.Equals, primaryKey); criteria.AddCriteria(_propertyName, Expression.Equals, true); T newObject = GetByCriteria(criteria); BeanFactory <T> factory = new BeanFactory <T>(); factory.CloneBean(newObject, destination); }
/// <summary> /// Charge un bean à partir de sa clef primaire. /// </summary> /// <param name="destination">Le bean à charger.</param> /// <param name="primaryKey">Valeur de la clef primaire.</param> /// <returns>Bean.</returns> public T Load(T destination, object primaryKey) { if (primaryKey == null) { throw new ArgumentNullException("primaryKey"); } _definition.PrimaryKey.CheckValueType(primaryKey); string commandName = ServiceSelect + "_" + _definition.ContractName; // On charge l'objet à partir d'un seul critère // correspondant à sa clé primaire FilterCriteria criteria = new FilterCriteria(_definition.PrimaryKey.MemberName, Expression.Equals, primaryKey); IReadCommand cmd = this.GetCommand(commandName, _definition.ContractName, criteria, BrokerManager.NoLimit, null); return(CollectionBuilder <T> .ParseCommandForSingleObject(destination, cmd)); }
/// <summary> /// Supprime logiquement l'ensemble des éléments correspondant au critère de recherche. /// </summary> /// <param name="criteria">Critère de recherche.</param> public override void DeleteAllByCriteria(FilterCriteria criteria) { if (criteria == null) { throw new ArgumentNullException("criteria"); } bool implementsIBeanState = typeof(IBeanState).IsAssignableFrom(typeof(T)); ICollection <T> list = this.GetAllByCriteria(criteria, null); foreach (T bean in list) { TypeDescriptor.GetProperties(typeof(T))["IsActif"].SetValue(bean, false); if (implementsIBeanState) { ((IBeanState)bean).State = ChangeAction.Update; } } SaveAll(list); }
/// <summary> /// Ajoute les paramètres de la requête select (noms des colonnes, clauses from, where et order by). /// </summary> /// <param name="commandText">Requête SQL à laquelle seront ajoutés les paramètres.</param> /// <param name="tableName">Nom de la table.</param> /// <param name="criteria">Critère de recherche.</param> /// <param name="sortOrder">Ordre de tri.</param> /// <param name="command">Commande d'appel à la base de données.</param> protected void AppendSelectParameters(StringBuilder commandText, string tableName, FilterCriteria criteria, string sortOrder, SqlServerCommand command) { if (commandText == null) { throw new ArgumentNullException("commandText"); } BeanPropertyDescriptorCollection properties = BeanDescriptor.GetDefinition(typeof(T)).Properties; bool hasColumn = false; foreach (BeanPropertyDescriptor property in properties) { if (string.IsNullOrEmpty(property.MemberName)) { continue; } if (property.PropertyType == typeof(byte[])) { continue; } if (hasColumn) { commandText.Append(", "); } commandText.Append(property.MemberName); hasColumn = true; } commandText.Append(" from ").Append(tableName); PrepareFilterCriteria(criteria, command, commandText); // Ajout du Order By si non-nul if (!string.IsNullOrEmpty(sortOrder)) { commandText.Append(" order by "); commandText.Append(sortOrder); } }
/// <summary> /// Supprime tous les objets correspondant aux critères. /// </summary> /// <param name="commandName">Nom de la commande.</param> /// <param name="tableName">Nom de la table.</param> /// <param name="criteria">Critères de suppression.</param> /// <returns>Retourne le nombre de lignes supprimées.</returns> protected virtual int DeleteAllByCriteria(string commandName, string tableName, FilterCriteria criteria) { if (criteria == null) { throw new ArgumentNullException("criteria"); } SqlServerCommand command = CreateSqlCommand(commandName, CommandType.Text); command.CommandTimeout = 0; StringBuilder commandText = new StringBuilder(CurrentUserStatementLog); commandText.Append("delete from "); commandText.Append(tableName); if (criteria.Parameters.Any()) { PrepareFilterCriteria(criteria, command, commandText); } command.CommandText = commandText.ToString(); return(command.ExecuteNonQuery()); }
/// <summary> /// Crée la commande. /// </summary> /// <param name="commandName">Nom de la commande.</param> /// <param name="tableName">Nom de la table.</param> /// <param name="criteria">Liste des critères de recherche.</param> /// <param name="maxRows">Nombre maximum d'enregistrements (BrokerManager.NoLimit = pas de limite).</param> /// <param name="queryParameter">Paramètre de tri des résultats et de limit des résultats.</param> /// <returns>IReadCommand contenant la commande.</returns> protected abstract IReadCommand GetCommand(string commandName, string tableName, FilterCriteria criteria, int maxRows, QueryParameter queryParameter);
/// <summary> /// Récupération d'une liste d'objets d'un certain type correspondant à un critère donnée. /// </summary> /// <param name="collection">Collection à charger.</param> /// <param name="commandName">Nom de la commande.</param> /// <param name="queryParameter">Paramètres de tri et de limite (vide par défaut).</param> /// <param name="criteria">Map de critères auquelle la recherche doit correpondre.</param> /// <returns>Collection.</returns> private ICollection <T> InternalLoadAll(ICollection <T> collection, string commandName, QueryParameter queryParameter, FilterCriteria criteria) { int maxRows = BrokerManager.NoLimit; if (queryParameter != null) { // Définition du tri à partir de la requete. queryParameter.RemapSortColumn(typeof(T)); // Definition du maxRows maxRows = GetMaxRowCount(queryParameter.MaxRows); } IReadCommand cmd = this.GetCommand(commandName, _definition.ContractName, criteria, maxRows, queryParameter); ICollection <T> coll = CollectionBuilder <T> .ParseCommand(collection, cmd); long collCount = coll.Count; if (queryParameter != null && (queryParameter.Offset > 0 || queryParameter.Limit > 0)) { collCount = QueryContext.InlineCount.Value; } if (maxRows > BrokerManager.NoLimit && collCount > maxRows) { throw new BrokerException("Store return too many rows."); } return(coll); }
/// <summary> /// Execute une commande et retourne un reader. /// </summary> /// <param name="commandName">Nom de la commande.</param> /// <param name="tableName">Nom de la table.</param> /// <param name="criteria">Critère de recherche.</param> /// <param name="maxRows">Nombre maximum d'enregistrements (BrokerManager.NoLimit = pas de limite).</param> /// <param name="queryParameter">Paramètre de tri des résultats et de limit des résultats.</param> /// <returns>DataReader contenant le résultat de la commande.</returns> protected override IReadCommand GetCommand(string commandName, string tableName, FilterCriteria criteria, int maxRows, QueryParameter queryParameter) { SqlServerCommand command = new SqlServerCommand(this.DataSourceName, commandName, CommandType.Text); command.QueryParameters = queryParameter; StringBuilder commandText = new StringBuilder("select "); if (maxRows != BrokerManager.NoLimit) { commandText.Append("top(@top) "); command.Parameters.AddWithValue("top", maxRows); } string order = null; if (queryParameter != null && !string.IsNullOrEmpty(queryParameter.SortCondition)) { order = queryParameter.SortCondition; } // Todo : brancher le tri. AppendSelectParameters(commandText, tableName, criteria, order, command); // Set de la requête command.CommandText = commandText.ToString(); return(command); }
/// <summary> /// Execute une commande et retourne un reader. /// </summary> /// <param name="commandName">Nom de la commande.</param> /// <param name="tableName">Nom de la table.</param> /// <param name="criteria">Critère de recherche.</param> /// <param name="maxRows">Nombre maximum d'enregistrements (BrokerManager.NoLimit = pas de limite).</param> /// <param name="queryParameter">Paramètre de la requête.</param> /// <returns>DataReader contenant le résultat de la commande.</returns> protected override IReadCommand GetCommand(string commandName, string tableName, FilterCriteria criteria, int maxRows, QueryParameter queryParameter) { SqlServerCommand command = new SqlServerCommand(this.DataSourceName, commandName, CommandType.Text); StringBuilder commandText = new StringBuilder("select "); if (maxRows != BrokerManager.NoLimit) { commandText.Append("top(@top) "); command.Parameters.AddWithValue("top", maxRows); } string order = null; if (queryParameter != null && !string.IsNullOrEmpty(queryParameter.SortCondition)) { order = queryParameter.SortCondition; } BeanDefinition definition = BeanDescriptor.GetDefinition(typeof(T)); BeanPropertyDescriptorCollection properties = definition.Properties; bool hasColumn = false; bool checkLanguage = false; // Rank sur les langues trouvées. var propTranslatableList = properties.Where(x => x.IsTranslatable); if (propTranslatableList.Any()) { checkLanguage = true; commandText.Append(" * from (select *,"); IEnumerable <string> partitionList = new List <string>() { "temp." + definition.PrimaryKey.MemberName }; ICollection <string> orderList = new List <string>(); foreach (BeanPropertyDescriptor property in propTranslatableList) { orderList.Add("rg" + property.PropertyName); } commandText.Append(GetRankOverPartitionSqlString(partitionList, orderList, "r")); commandText.Append(" from (Select "); } hasColumn = false; foreach (BeanPropertyDescriptor property in properties) { if (!IsPropertyToRetrieve(property)) { continue; } if (hasColumn) { commandText.Append(", "); } if (property.IsTranslatable) { PrepareTranslationColumn(commandText, definition, property); } else { commandText.Append("tab.").Append(property.MemberName); } hasColumn = true; } commandText.Append(" from ").Append(tableName).Append(" tab "); IDictionary <string, FilterCriteriaParam> criteriaMap = new Dictionary <string, FilterCriteriaParam>(); foreach (FilterCriteriaParam param in criteria.Parameters) { if (!criteriaMap.ContainsKey(param.ColumnName)) { criteriaMap.Add(param.ColumnName, param); } } IDictionary <string, string> propertyNameMap = new Dictionary <string, string>(); foreach (BeanPropertyDescriptor property in properties) { if (!IsPropertyToRetrieve(property)) { continue; } if (property.IsTranslatable) { if (!propertyNameMap.ContainsKey(property.MemberName)) { propertyNameMap.Add(property.MemberName, property.PropertyName); } string refTableName = definition.BeanType.FullName + "_." + property.PropertyName; commandText.Append(" left join TRADUCTION_REFERENCE").Append(" ref").Append(property.PropertyName); commandText.Append(" on ref").Append(property.PropertyName).Append(".").Append("TDR_TABLE").Append(" = '").Append(refTableName); commandText.Append("' and ref").Append(property.PropertyName).Append(".").Append("TDR_CODE").Append(" = ").Append("tab.").Append(definition.PrimaryKey.MemberName); } } PrepareFilterCriteria(criteria, command, commandText); if (checkLanguage) { commandText.Append(" ) temp ) t where r = 1"); } // Ajout du Order By si non-nul if (order != null) { commandText.Append(" order by "); commandText.Append(order); } // Set de la requête command.CommandText = commandText.ToString(); return(command); }