Skip to content

Commit

Permalink
Fixed generated properties issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yukozh committed Aug 5, 2016
1 parent 2d8e299 commit d9e07ed
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class MySqlAnnotationNames
public const string Prefix = "MySql:";
public const string Serial = "Serial";
public const string ValueGeneratedOnAdd = "ValueGeneratedOnAdd";
public const string ValueGeneratedOnAddOrUpdate = "ValueGeneratedOnAddOrUpdate";
public const string DefaultSequenceName = "DefaultSequenceName";
public const string DefaultSequenceSchema = "DefaultSequenceSchema";
public const string SequenceName = "SequenceName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected MySqlFullAnnotationNames(string prefix)
MySqlExtensionPrefix = prefix + MySqlAnnotationNames.MySqlExtensionPrefix;
DatabaseTemplate = prefix + MySqlAnnotationNames.DatabaseTemplate;
ValueGeneratedOnAdd = prefix + MySqlAnnotationNames.ValueGeneratedOnAdd;
ValueGeneratedOnAddOrUpdate = prefix + MySqlAnnotationNames.ValueGeneratedOnAddOrUpdate;
}

public new static MySqlFullAnnotationNames Instance { get; } = new MySqlFullAnnotationNames(MySqlAnnotationNames.Prefix);
Expand All @@ -30,5 +31,6 @@ protected MySqlFullAnnotationNames(string prefix)
public readonly string MySqlExtensionPrefix;
public readonly string DatabaseTemplate;
public readonly string ValueGeneratedOnAdd;
public readonly string ValueGeneratedOnAddOrUpdate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public override IEnumerable<IAnnotation> For(IProperty property)
// So we add a custom annotation here to pass the information.
if (property.ValueGenerated == ValueGenerated.OnAdd)
yield return new Annotation(MySqlAnnotationNames.Prefix + MySqlAnnotationNames.ValueGeneratedOnAdd, true);
else if (property.ValueGenerated == ValueGenerated.OnAddOrUpdate)
yield return new Annotation(MySqlAnnotationNames.Prefix + MySqlAnnotationNames.ValueGeneratedOnAddOrUpdate, true);
}

public override IEnumerable<IAnnotation> For(IIndex index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ protected override void ColumnDefinition([CanBeNull] string schema, [NotNull] st

var generatedOnAddAnnotation = annotatable[MySqlAnnotationNames.Prefix + MySqlAnnotationNames.ValueGeneratedOnAdd];
var generatedOnAdd = generatedOnAddAnnotation != null && (bool)generatedOnAddAnnotation;
var generatedOnAddOrUpdateAnnotation = annotatable[MySqlAnnotationNames.Prefix + MySqlAnnotationNames.ValueGeneratedOnAddOrUpdate];
var generatedOnAddOrUpdate = generatedOnAddOrUpdateAnnotation != null && (bool)generatedOnAddOrUpdateAnnotation;
if (generatedOnAdd && string.IsNullOrWhiteSpace(defaultValueSql) && defaultValue == null)
{
switch (type)
Expand All @@ -338,6 +340,26 @@ protected override void ColumnDefinition([CanBeNull] string schema, [NotNull] st
case "int2":
type = "short AUTO_INCREMENT";
break;
case "datetime":
case "timestamp":
defaultValueSql = "CURRENT_TIMESTAMP";
break;

}
}

string onUpdateSql = null;

if (generatedOnAddOrUpdate)
{
switch (type)
{
case "datetime":
case "timestamp":
if (string.IsNullOrWhiteSpace(defaultValueSql) && defaultValue == null)
defaultValueSql = "CURRENT_TIMESTAMP";
onUpdateSql = "CURRENT_TIMESTAMP";
break;
}
}

Expand All @@ -363,6 +385,12 @@ protected override void ColumnDefinition([CanBeNull] string schema, [NotNull] st
.Append(" DEFAULT ")
.Append(SqlGenerationHelper.GenerateLiteral(defaultValue));
}
if (onUpdateSql != null)
{
builder
.Append(" ON UPDATE ")
.Append(onUpdateSql);
}
}

protected override void DefaultValue(object defaultValue, string defaultValueSql, MigrationCommandListBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ResultSetMapping AppendBulkInsertOperation(StringBuilder commandStringBui
}
else if (readOperations.Length > 0)
{
AppendOutputClause(commandStringBuilder, name, schema, readOperations, operations);
AppendInsertOutputClause(commandStringBuilder, name, schema, readOperations, operations);
resultSetCreated = true;
}
}
Expand Down Expand Up @@ -102,24 +102,55 @@ public override ResultSetMapping AppendUpdateOperation(
var readOperations = operations.Where(o => o.IsRead).ToArray();

AppendUpdateCommandHeader(commandStringBuilder, name, schema, writeOperations);
/*if (readOperations.Length > 0)
{
AppendOutputClause(commandStringBuilder, readOperations);
}*/

AppendWhereClause(commandStringBuilder, conditionOperations);
commandStringBuilder.Append(SqlGenerationHelper.StatementTerminator).AppendLine();

if (readOperations.Length == 0)
{
return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
}
commandStringBuilder
.Append("SELECT ");

AppendUpdateOutputClause(commandStringBuilder, schema, name, readOperations, operations);

return ResultSetMapping.LastInResultSet;
}

private void AppendUpdateOutputClause(StringBuilder commandStringBuilder, string schema, string name, IReadOnlyList<ColumnModification> readOperations, IReadOnlyList<ColumnModification> allOperations)
{
if (readOperations.Count() > 0)
{
foreach (var x in readOperations)
{
commandStringBuilder.Append($"{ SqlGenerationHelper.DelimitIdentifier(x.ColumnName) }, ");
}

commandStringBuilder.Append("ROW_COUNT()");

commandStringBuilder
.Append($" FROM { SqlGenerationHelper.DelimitIdentifier(name) }")
.Append(" WHERE ");

var predicates = new List<string>();
foreach (var x in allOperations.Where(y => y.IsKey))
{
predicates.Add($"{SqlGenerationHelper.DelimitIdentifier(x.ColumnName)} = @{ x.ParameterName }");
}

commandStringBuilder
.Append(string.Join(" AND ", predicates));
}
else
{
commandStringBuilder
.Append("ROW_COUNT()");
}

commandStringBuilder
.Append(SqlGenerationHelper.StatementTerminator);
}


// ReSharper disable once ParameterTypeCanBeEnumerable.Local
private void AppendOutputClause(
private void AppendInsertOutputClause(
StringBuilder commandStringBuilder,
string name,
string schema,
Expand Down

0 comments on commit d9e07ed

Please sign in to comment.