internal static void LoadSqliteValues <T>(TableInfo tableInfo, T entity, KdbndpCommand command) { var propertyColumnsDict = tableInfo.PropertyColumnNamesDict; foreach (var propertyColumn in propertyColumnsDict) { object value; if (!tableInfo.ShadowProperties.Contains(propertyColumn.Key)) { if (propertyColumn.Key.Contains(".")) // ToDo: change IF clause to check for NavigationProperties, optimise, integrate with same code segment from LoadData method { var ownedPropertyNameList = propertyColumn.Key.Split('.'); var ownedPropertyName = ownedPropertyNameList[0]; var subPropertyName = ownedPropertyNameList[1]; var ownedFastProperty = tableInfo.FastPropertyDict[ownedPropertyName]; var ownedProperty = ownedFastProperty.Property; var propertyType = Nullable.GetUnderlyingType(ownedProperty.GetType()) ?? ownedProperty.GetType(); if (!command.Parameters.Contains("@" + propertyColumn.Value)) { var parameter = new KdbndpParameter($"@{propertyColumn.Value}", propertyType); command.Parameters.Add(parameter); } if (ownedProperty == null) { value = null; } else { var ownedPropertyValue = tableInfo.FastPropertyDict[ownedPropertyName].Get(entity); var subPropertyFullName = $"{ownedPropertyName}_{subPropertyName}"; value = tableInfo.FastPropertyDict[subPropertyFullName]?.Get(ownedPropertyValue); } } else { value = tableInfo.FastPropertyDict[propertyColumn.Key].Get(entity); } } else // IsShadowProperty { value = entity.GetType().Name; } if (tableInfo.ConvertibleProperties.ContainsKey(propertyColumn.Key) && value != DBNull.Value) { value = tableInfo.ConvertibleProperties[propertyColumn.Key].ConvertToProvider.Invoke(value); } command.Parameters[$"@{propertyColumn.Value}"].Value = value ?? DBNull.Value; } }
public override DbCommand GetCommand(string sql, SugarParameter[] parameters) { KdbndpCommand sqlCommand = new KdbndpCommand(sql, (KdbndpConnection)this.Connection); sqlCommand.CommandType = this.CommandType; sqlCommand.CommandTimeout = this.CommandTimeOut; if (this.Transaction != null) { sqlCommand.Transaction = (KdbndpTransaction)this.Transaction; } if (parameters.HasValue()) { IDataParameter[] ipars = ToIDbDataParameter(parameters); sqlCommand.Parameters.AddRange((KdbndpParameter[])ipars); } CheckConnection(); return(sqlCommand); }
/// <summary> /// SqlDataAdapter /// </summary> /// <param name="command"></param> public KdbndpDataAdapter(KdbndpCommand command) { this.command = command; }
internal static KdbndpCommand GetKdbndpCommand <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, KdbndpConnection connection, KdbndpTransaction transaction) { KdbndpCommand command = connection.CreateCommand(); command.Transaction = transaction; var operationType = tableInfo.BulkConfig.OperationType; switch (operationType) { case OperationType.Insert: command.CommandText = SqlQueryBuilderSqlite.InsertIntoTable(tableInfo, OperationType.Insert); break; case OperationType.InsertOrUpdate: command.CommandText = SqlQueryBuilderSqlite.InsertIntoTable(tableInfo, OperationType.InsertOrUpdate); break; case OperationType.InsertOrUpdateDelete: throw new NotSupportedException("Sqlite supports only UPSERT(analog for MERGE WHEN MATCHED) but does not have functionality to do: 'WHEN NOT MATCHED BY SOURCE THEN DELETE'" + "What can be done is to read all Data, find rows that are not in input List, then with those do the BulkDelete."); case OperationType.Update: command.CommandText = SqlQueryBuilderSqlite.UpdateSetTable(tableInfo); break; case OperationType.Delete: command.CommandText = SqlQueryBuilderSqlite.DeleteFromTable(tableInfo); break; } type = tableInfo.HasAbstractList ? entities[0].GetType() : type; var entityType = context.Model.FindEntityType(type); var entityPropertiesDict = entityType.GetProperties().Where(a => tableInfo.PropertyColumnNamesDict.ContainsKey(a.Name)).ToDictionary(a => a.Name, a => a); var properties = type.GetProperties(); foreach (var property in properties) { if (entityPropertiesDict.ContainsKey(property.Name)) { var propertyEntityType = entityPropertiesDict[property.Name]; string columnName = propertyEntityType.GetColumnName(); var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; /*var sqliteType = SqliteType.Text; // "String" || "Decimal" || "DateTime" * if (propertyType.Name == "Int16" || propertyType.Name == "Int32" || propertyType.Name == "Int64") * sqliteType = SqliteType.Integer; * if (propertyType.Name == "Float" || propertyType.Name == "Double") * sqliteType = SqliteType.Real; * if (propertyType.Name == "Guid" ) * sqliteType = SqliteType.Blob; */ var parameter = new KdbndpParameter($"@{columnName}", propertyType); // ,sqliteType // ,null command.Parameters.Add(parameter); } } var shadowProperties = tableInfo.ShadowProperties; foreach (var shadowProperty in shadowProperties) { var parameter = new KdbndpParameter($"@{shadowProperty}", typeof(string)); command.Parameters.Add(parameter); } command.Prepare(); // Not Required (check if same efficiency when removed) return(command); }