public void ConnectionEncodingWorksCorrectly() { Connection.Encoding = Encoding.GetEncoding("windows-1250"); ITikCommand readCmd = Connection.CreateCommand("/system/identity/print"); var originalIdentity = readCmd.ExecuteScalar(); //modify const string testStringWithExoticCharacters = "Příliš žluťoučký kůň úpěl ďábelské ódy."; ITikCommand setCmd = Connection.CreateCommand("/system/identity/set"); setCmd.AddParameterAndValues("name", testStringWithExoticCharacters); setCmd.ExecuteNonQuery(); //read modified var newIdentity = readCmd.ExecuteScalar(); Assert.AreEqual(testStringWithExoticCharacters, newIdentity); //cleanup setCmd.Parameters.Clear(); setCmd.AddParameterAndValues("name", originalIdentity); setCmd.ExecuteNonQuery(); }
public void ConnectionEncodingWorksCorrectly() { using (var connection = ConnectionFactory.OpenConnection(TikConnectionType.ApiSsl, ConfigurationManager.AppSettings["host"], ConfigurationManager.AppSettings["user"], ConfigurationManager.AppSettings["pass"])) { connection.Encoding = Encoding.GetEncoding("windows-1250"); ITikCommand readCmd = connection.CreateCommand("/system/identity/print"); var originalIdentity = readCmd.ExecuteScalar(); //modify const string testStringWithExoticCharacters = "Příliš žluťoučký kůň úpěl ďábelské ódy."; ITikCommand setCmd = connection.CreateCommand("/system/identity/set"); setCmd.AddParameterAndValues("name", testStringWithExoticCharacters); setCmd.ExecuteNonQuery(); //read modified var newIdentity = readCmd.ExecuteScalar(); Assert.AreEqual(testStringWithExoticCharacters, newIdentity); //cleanup setCmd.Parameters.Clear(); setCmd.AddParameterAndValues("name", originalIdentity); setCmd.ExecuteNonQuery(); } }
/// <summary> /// Deletes entity (.id is the key) on mikrotik router. /// </summary> /// <typeparam name="TEntity">Deleted entity type.</typeparam> /// <param name="connection">Tik connection used to delete entity.</param> /// <param name="entity">Entity to be deleted (.id property is the key)</param> public static void Delete <TEntity>(this ITikConnection connection, TEntity entity) { var metadata = TikEntityMetadataCache.GetMetadata <TEntity>(); EnsureNotReadonlyEntity(metadata); string id = metadata.IdProperty.GetEntityValue(entity); if (string.IsNullOrEmpty(id)) { throw new ArgumentException("Entity has no .id (entity is not loaded from mikrotik router)", "entity"); } ITikCommand cmd = connection.CreateCommandAndParameters(metadata.EntityPath + "/remove", TikCommandParameterFormat.NameValue, TikSpecialProperties.Id, id); cmd.ExecuteNonQuery(); }
/// <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> /// 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(); } } }