protected IEnumerable <T> EnumerableFor(string query) { try { PrepareConnection(); OdbcCommand command = new OdbcCommand(query, Connection); OdbcDataReader reader = command.ExecuteReader(); while (reader.Read()) { T output = new T(); AttributesExtractor <T> properties = new AttributesExtractor <T>(output); output.Id = Convert.ToInt64(reader["Id"]); foreach (PropertyInfo prop in properties.PersistentProperties()) { prop.SetValue(output, reader[prop.Name], null); } output.SetConnection(Connection); yield return(output); } } finally { CloseConnection(); } }
/// <summary> /// Update a thing on the database /// </summary> /// <typeparam name="TItem">The current shelf type</typeparam> /// <param name="things">The things list to be inserted</param> /// <param name="safe">Update only already saved things</param> /// <exception cref="MissingThing">If there the thing to be inserted has no Id</exception> public void Update <TItem>(IEnumerable <TItem> things, bool safe) where TItem : T { T[] allThings; if (safe) { allThings = things.Where(thing => thing.Id != 0).ToArray(); } else { allThings = things.ToArray(); if (allThings.Any(thing => thing.Id == 0)) { throw new MissingThing(TableName); } } try { PrepareConnection(); foreach (TItem thing in allThings) { thing.Validate(); AttributesExtractor <T> properties = new AttributesExtractor <T>(thing); string query = MySqlBuilder.BuildUpdate(properties.PersistentAttributes(), thing); OdbcCommand command = new OdbcCommand(query, Connection); command.ExecuteNonQuery(); } } finally { CloseConnection(); } }
/// <summary> /// Cache the properties names of the thing /// </summary> /// <returns>Array of properties names</returns> public string[] PropertyNames() { if (AttributesList == null) { AttributesExtractor <IThing> attributes = new AttributesExtractor <IThing>(this); AttributesList = attributes.PersistentAttributesList(); } return(AttributesList); }
/// <summary> /// Generate the CREATE TABLE statement for the current Thing Class /// </summary> /// <returns>Thq SQL Create query</returns> public string GenerateCreateSQL() { if (_createThingQuery == null) { AttributesExtractor <IThing> attributes = new AttributesExtractor <IThing>(this); MysqlBuilder.MysqlCommandBuilder builder = new MysqlBuilder.MysqlCommandBuilder(GetType().Name); _createThingQuery = builder.BuildCreate(attributes.PropertiesWithAttributes()); } return(_createThingQuery); }
/// <summary> /// Validate the properties /// </summary> /// <returns>True if valid</returns> /// <exception cref="NotNullException">If non nullable attributes are null</exception> /// <exception cref="MaxLengthOverflowException">If a attribute is over the seted length</exception> public bool Validate() { foreach (object action in ValidationActionsFor("before")) { action.GetType().GetMethod("Invoke").Invoke(action, new object[] { this }); } foreach (object action in ValidationActionsFor("validation")) { if (!(bool)action.GetType().GetMethod("Invoke").Invoke(action, new object[] { this })) { throw new CustomValidationException(); } } AttributesExtractor <IThing> attributes = new AttributesExtractor <IThing>(this); foreach (PropertyInfo prop in attributes.NotNullProperties()) { if (InvalidNotNullProperty(prop)) { throw new NotNullException(prop.Name); } } foreach (KeyValuePair <PropertyInfo, int> maxprop in attributes.MaxLenghtProperties()) { if (InvalidMaxLengthProperty(maxprop)) { throw new MaxLengthOverflowException(maxprop.Key.Name, maxprop.Value); } } foreach (object action in ValidationActionsFor("after")) { action.GetType().GetMethod("Invoke").Invoke(action, new object[] { this }); } return(true); }
/// <summary> /// Insert a the shelved thing on the database /// </summary> /// <param name="things">The new thing's list to be inserted</param> /// <param name="safe">Only insert the not saved things</param> /// <returns>The same thing updated</returns> /// <exception cref="AlreadySavedException">If the thing already have an id</exception> public IEnumerable <T> Insert(IEnumerable <T> things, bool safe) { T[] allThings; if (safe) { allThings = things.Where(thing => thing.Id != 0).ToArray(); } else { allThings = things.ToArray(); if (allThings.Any(thing => thing.Id != 0)) { throw new AlreadySavedException(TableName); } } try { PrepareConnection(); foreach (T thing in allThings) { thing.Validate(); AttributesExtractor <T> properties = new AttributesExtractor <T>(thing); string query = MySqlBuilder.BuildInsert(properties.PersistentAttributes()); OdbcCommand command = new OdbcCommand(query, Connection); command.ExecuteNonQuery(); command.CommandText = MySqlBuilder.BuildLastId(); Int64 lastId = (Int64)command.ExecuteScalar(); thing.Id = lastId; thing.SetConnection(Connection); } } finally { CloseConnection(); } return(allThings); }