/// <summary> /// Procedure of processing the given HTTP input. /// </summary> /// <param name="str">Body of the HTTP request.</param> /// <param name="contentType">MIME type of the event. /// You can find them in <see cref="Server.ContentTypes"/>.</param> private void ProcessRequest(string str, string contentType) { // Find serialization for this MIME type Serialization type = ContentTypes[contentType]; // Get all the events List <object> events = DeserializeEvents(str, type); // Connect to the db using (var db = new SqlConnection(dbConnectionString)) { try { db.Open(); foreach (var eventObj in events) { var flat = eventObj.ToDictionary(); // Find the table for this object string tableName = PrepareTableForInsert(flat); if (!options.IncludeEventNameAsTableProperty) { flat = RemoveTableNameProperties(flat); } // Get insert command var insert = SqlDynamic.GetInsert(tableName, flat, db); insert.ExecuteNonQuery(); } } finally { db.Close(); } } }
/// <summary> /// Searches the table for this object to insert in. /// If there is no table found, the one will be created /// automagically. /// </summary> /// <param name="flat">Dictionary with keys as object's /// properties, values as values of these properties. /// All the values must return true when /// called on <see cref="SqlDynamic.IsSupportedType(Type)"/>.</param> /// <returns>Table name to which this object must be inserted.</returns> private string PrepareTableForInsert(IDictionary <string, object> flat) { var db = new SqlConnection(dbConnectionString); db.Open(); // Find out table name for this event string tableName = GetTableName(flat); // If not include table names if (!options.IncludeEventNameAsTableProperty) { // Leave only not names flat = RemoveTableNameProperties(flat); } // If table is not created yet if (!SqlDynamic.IsTableCreated(tableName, db)) { // Create this table var createCommand = db.CreateCommand(); createCommand.CommandText = SqlDynamic.CreateTableCommand( name: tableName, idCol: options.EventPrimaryKeyColumn, scheme: flat.ToDictionary(k => k.Key, v => v.Value.GetType())); createCommand.ExecuteNonQuery(); // Tell that the table was tried to create System.Diagnostics.Debug.WriteLine(createCommand.CommandText); } db.Close(); return(tableName); }