/// <summary> /// Generates an table sync statement for this table. /// </summary> /// <param name="context">The request context.</param> /// <param name="bulkContext"></param> /// <param name="records"></param> protected override void DoToSyncStatement(IMansionContext context, BulkOperationContext bulkContext, List<Record> records) { // start by clearing the table bulkContext.Add(command => { command.CommandType = CommandType.Text; command.CommandText = string.Format("TRUNCATE TABLE [{0}]", Name); }); // loop through all the properties foreach (var record in records) { // prepare the query var columnText = new StringBuilder(); var valueText = new StringBuilder(); // finish the query var currentRecord = record; bulkContext.Add(command => { // loop through all the columns foreach (var column in Columns) column.ToSyncStatement(context, command, currentRecord, columnText, valueText); // construct the command command.CommandType = CommandType.Text; command.CommandText = string.Format("INSERT INTO [{0}] ({1}) VALUES ({2});", Name, columnText.Trim(), valueText.Trim()); }); } }
/// <summary> /// Generates an table sync statement for this table. /// </summary> /// <param name="context">The request context.</param> /// <param name="bulkContext"></param> /// <param name="records"></param> protected override void DoToSyncStatement(IMansionContext context, BulkOperationContext bulkContext, List<Record> records) { // this is only invoked when there is a issue in mansion framwork throw new NotSupportedException("Root tables will not be synced"); }
/// <summary> /// Generates an table sync statement for this table. /// </summary> /// <param name="context">The request context.</param> /// <param name="bulkContext"></param> /// <param name="records"></param> protected virtual void DoToSyncStatement(IMansionContext context, BulkOperationContext bulkContext, List<Record> records) { throw new NotSupportedException(); }
/// <summary> /// Generates an table sync statement for this table. /// </summary> /// <param name="context">The request context.</param> /// <param name="bulkContext"></param> /// <param name="records"></param> public void ToSyncStatement(IMansionContext context, BulkOperationContext bulkContext, List<Record> records) { // validate arguments if (context == null) throw new ArgumentNullException("context"); if (bulkContext == null) throw new ArgumentNullException("bulkContext"); if (records == null) throw new ArgumentNullException("records"); // invoke template method DoToSyncStatement(context, bulkContext, records); }
/// <summary> /// Generates an table sync statement for this table. /// </summary> /// <param name="context">The request context.</param> /// <param name="bulkContext"></param> /// <param name="records"></param> protected override void DoToSyncStatement(IMansionContext context, BulkOperationContext bulkContext, List<Record> records) { // loop through all the properties foreach (var propertyName in Columns.Select(column => column.PropertyName)) { // loop through all the nodes var currentPropertyName = propertyName; foreach (var record in records) { // start by cleaning up the table var currentRecord = record; bulkContext.Add(command => { command.CommandType = CommandType.Text; command.CommandText = string.Format("DELETE FROM [{0}] WHERE [id] = {1} AND [name] = '{2}'", Name, currentRecord.Id, currentPropertyName); }); // check if there are any properties var values = record.Get(context, currentPropertyName, string.Empty).Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray(); if (values.Length == 0) continue; // create the command bulkContext.Add(command => { command.CommandType = CommandType.Text; var nameColumnValue = command.AddParameter(currentPropertyName); // loop through each value and write an insert statement var buffer = new StringBuilder(); foreach (var value in values) buffer.AppendFormat("INSERT INTO [{0}] ([id], [name], [value]) VALUES ({1}, @{2}, @{3});", Name, currentRecord.Id, nameColumnValue, command.AddParameter(value)); command.CommandText = buffer.ToString(); }); } } }
/// <summary> /// Generates an table sync statement for this table. /// </summary> /// <param name="context">The request context.</param> /// <param name="bulkContext"></param> /// <param name="records"></param> protected override void DoToSyncStatement(IMansionContext context, BulkOperationContext bulkContext, List<Record> records) { // start by clearing the table bulkContext.Add(command => { command.CommandType = CommandType.Text; command.CommandText = string.Format("TRUNCATE TABLE [{0}]", Name); }); // loop through all the properties foreach (var record in records) { // check if there are any properties var values = record.Get(context, PropertyName, string.Empty).Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray(); if (values.Length == 0) continue; // loop through each value and write an insert statement foreach (var value in values) { var currentRecord = record; var value1 = value; bulkContext.Add(command => { command.CommandType = CommandType.Text; command.CommandText = string.Format("INSERT INTO [{0}] ([id], [value]) VALUES ({1}, @{2});", Name, currentRecord.Id, command.AddParameter(value1)); }); } } }