private static ITikCommand CreateLoadCommandWithFilter <TEntity> (ITikConnection connection, ITikCommandParameter[] parameters) { var metadata = TikEntityMetadataCache.GetMetadata <TEntity>(); ITikCommand command = connection.CreateCommand(metadata.EntityPath + metadata.LoadCommand, metadata.LoadDefaultParameneterFormat); // =detail= if (metadata.IncludeDetails) { command.AddParameter("detail", "", TikCommandParameterFormat.NameValue); } //.proplist if (metadata.IncludeProplist) { command.AddParameter(TikSpecialProperties.Proplist, string.Join(",", metadata.Properties.Select(prop => prop.FieldName).ToArray()), TikCommandParameterFormat.NameValue); } //filter //parameters if (parameters != null) { foreach (ITikCommandParameter param in parameters) { command.Parameters.Add(param); } } return(command); }
/// <summary> /// Moves given <paramref name="entityToMove"/> before given <paramref name="entityToMoveBefore"/>. /// </summary> /// <typeparam name="TEntity">Moved entity type.</typeparam> /// <param name="connection">Tik connection used to move entity.</param> /// <param name="entityToMove">Entity to be moved.</param> /// <param name="entityToMoveBefore">Entity before which is given <paramref name="entityToMove"/> moved.</param> public static void Move <TEntity>(this ITikConnection connection, TEntity entityToMove, TEntity entityToMoveBefore) { var metadata = TikEntityMetadataCache.GetMetadata <TEntity>(); EnsureSupportsOrdering(metadata); string idToMove = metadata.IdProperty.GetEntityValue(entityToMove); string idToMoveBefore = entityToMoveBefore != null?metadata.IdProperty.GetEntityValue(entityToMoveBefore) : null; ITikCommand cmd = connection.CreateCommandAndParameters(metadata.EntityPath + "/move", TikCommandParameterFormat.NameValue, "numbers", idToMove); if (entityToMoveBefore != null) { cmd.AddParameter("destination", idToMoveBefore); } cmd.ExecuteNonQuery(); }
/// <summary> /// Adds new instance of parameter with .proplist to <see cref="Parameters"/> list. /// </summary> /// <param name="proplist">Names of the wanted properties</param> /// <returns>Instance of added parameter.</returns> public static ITikCommandParameter AddProplistParameter(this ITikCommand command, params string[] proplist) { ITikCommandParameter result = command.AddParameter(TikSpecialProperties.Proplist, string.Join(",", proplist), TikCommandParameterFormat.NameValue); return(result); }
/// <summary> /// Saves entity to mikrotik router. Does insert (/add) whan entity has empty id and update(/set + /unset) when id is present). /// Behavior of save is modified via <see cref="TikPropertyAttribute"/> on properties. /// See <see cref="TikPropertyAttribute.DefaultValue"/>, <see cref="TikPropertyAttribute.UnsetOnDefault"/>. /// </summary> /// <typeparam name="TEntity">Saved entitie type.</typeparam> /// <param name="connection">Tik connection used to save.</param> /// <param name="entity">Saved entity.</param> /// <param name="usedFieldsFilter">List of field names (on mikrotik) which should be modified. If is not null, only listed fields will be modified.</param> public static void Save <TEntity>(this ITikConnection connection, TEntity entity, IEnumerable <string> usedFieldsFilter = null) where TEntity : new() { var metadata = TikEntityMetadataCache.GetMetadata <TEntity>(); EnsureNotReadonlyEntity(metadata); string id; if (metadata.IsSingleton) { id = null; } else { EnsureHasIdProperty(metadata); id = metadata.IdProperty.GetEntityValue(entity); } if (!metadata.IsSingleton && string.IsNullOrEmpty(id)) { //create ITikCommand createCmd = connection.CreateCommand(metadata.EntityPath + "/add", TikCommandParameterFormat.NameValue); foreach (var property in metadata.Properties .Where(pm => !pm.IsReadOnly) .Where(pm => usedFieldsFilter == null || usedFieldsFilter.Contains(pm.FieldName, StringComparer.OrdinalIgnoreCase))) { if (!property.HasDefaultValue(entity)) { createCmd.AddParameter(property.FieldName, property.GetEntityValue(entity)); } } id = createCmd.ExecuteScalar(); if (metadata.HasIdProperty) { metadata.IdProperty.SetEntityValue(entity, id); // update saved id into entity } } else { //update (set+unset) ITikCommand setCmd = connection.CreateCommand(metadata.EntityPath + "/set", TikCommandParameterFormat.NameValue); if (!metadata.IsSingleton && usedFieldsFilter == null) { //compare state on mikrotik and update different fields only var unmodifiedEntity = connection.LoadById <TEntity>(id); //TODO some kind of "loaded entities" session cache could be used to avoid another load before save. usedFieldsFilter = entity.GetDifferentFields(unmodifiedEntity); } List <string> fieldsToUnset = new List <string>(); foreach (var property in metadata.Properties .Where(pm => !pm.IsReadOnly) .Where(pm => usedFieldsFilter == null || usedFieldsFilter.Contains(pm.FieldName, StringComparer.OrdinalIgnoreCase))) { if (property.HasDefaultValue(entity) && property.UnsetOnDefault) { fieldsToUnset.Add(property.FieldName); } else { setCmd.AddParameter(property.FieldName, property.GetEntityValue(entity)); //full update (all values) } } if (fieldsToUnset.Count > 0) { // this should also work (see http://forum.mikrotik.com/viewtopic.php?t=28821 ) //ip/route/unset //=.id = *1 //= value-name=routing-mark foreach (string fld in fieldsToUnset) { ITikCommand unsetCmd = connection.CreateCommand(metadata.EntityPath + "/unset", TikCommandParameterFormat.NameValue); unsetCmd.AddParameter(TikSpecialProperties.Id, id, TikCommandParameterFormat.NameValue); unsetCmd.AddParameter(TikSpecialProperties.UnsetValueName, fld); unsetCmd.ExecuteNonQuery(); } } if (setCmd.Parameters.Any()) { if (!metadata.IsSingleton) { setCmd.AddParameter(TikSpecialProperties.Id, id, TikCommandParameterFormat.NameValue); } setCmd.ExecuteNonQuery(); } } }