public static string BuildInsertStatement(this DatabaseEntity dbEntity) { var type = dbEntity.GetType(); if (InsertStatementCache.TryGetValue(dbEntity.GetType(), out var statement)) { return(statement); } var sqlBuilder = new StringBuilder($"INSERT INTO {dbEntity.GetTableName()} ("); var valuesBuilder = new StringBuilder(); foreach (var property in type.GetProperties()) { var propertyName = property.Name; sqlBuilder.Append($"{propertyName},"); valuesBuilder.Append($"@{propertyName},"); } sqlBuilder.Length--; valuesBuilder.Length--; var builtStatement = sqlBuilder.Append($") VALUES ({valuesBuilder});").ToString(); InsertStatementCache.Add(type, builtStatement); return(builtStatement); }
public static string BuildInsertStatement(this DatabaseEntity dbEntity) { var sqlBuilder = new StringBuilder($"INSERT INTO {dbEntity.GetTableName()} ("); var valuesBuilder = new StringBuilder(); foreach (var property in dbEntity.GetType().GetProperties()) { var propertyName = property.Name; sqlBuilder.Append($"{propertyName},"); valuesBuilder.Append($"@{propertyName},"); } sqlBuilder.Length--; valuesBuilder.Length--; sqlBuilder.Append($") VALUES ({valuesBuilder});"); return(sqlBuilder.ToString()); }