|
3 | 3 | using System.Data;
|
4 | 4 | using System.Data.SqlClient;
|
5 | 5 | using System.IO;
|
| 6 | +using ServiceStack.DataAnnotations; |
6 | 7 | using ServiceStack.OrmLite.SqlServer.Converters;
|
7 | 8 | using ServiceStack.Text;
|
8 | 9 | #if NETSTANDARD1_3
|
@@ -200,11 +201,43 @@ public override string ToChangeColumnNameStatement(Type modelType, FieldDefiniti
|
200 | 201 |
|
201 | 202 | public override string GetColumnDefinition(FieldDefinition fieldDef)
|
202 | 203 | {
|
| 204 | + // https://msdn.microsoft.com/en-us/library/ms182776.aspx |
203 | 205 | if (fieldDef.IsRowVersion)
|
204 | 206 | return $"{fieldDef.FieldName} rowversion NOT NULL";
|
205 | 207 |
|
206 |
| - var definition = base.GetColumnDefinition(fieldDef); |
207 |
| - return definition; |
| 208 | + var fieldDefinition = fieldDef.CustomFieldDefinition ?? |
| 209 | + GetColumnTypeDefinition(fieldDef.ColumnType, fieldDef.FieldLength, fieldDef.Scale); |
| 210 | + |
| 211 | + var sql = StringBuilderCache.Allocate(); |
| 212 | + sql.Append($"{GetQuotedColumnName(fieldDef.FieldName)} {fieldDefinition}"); |
| 213 | + |
| 214 | + if (fieldDef.IsPrimaryKey) |
| 215 | + { |
| 216 | + sql.Append(" PRIMARY KEY"); |
| 217 | + if (fieldDef.AutoIncrement) |
| 218 | + { |
| 219 | + sql.Append(" ").Append(AutoIncrementDefinition); |
| 220 | + } |
| 221 | + } |
| 222 | + else |
| 223 | + { |
| 224 | + sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL"); |
| 225 | + } |
| 226 | + |
| 227 | + // https://msdn.microsoft.com/en-us/library/ms184391.aspx |
| 228 | + var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation; |
| 229 | + if (!string.IsNullOrEmpty(collation)) |
| 230 | + { |
| 231 | + sql.Append($" COLLATE {collation}"); |
| 232 | + } |
| 233 | + |
| 234 | + var defaultValue = GetDefaultValue(fieldDef); |
| 235 | + if (!string.IsNullOrEmpty(defaultValue)) |
| 236 | + { |
| 237 | + sql.AppendFormat(DefaultValueFormat, defaultValue); |
| 238 | + } |
| 239 | + |
| 240 | + return StringBuilderCache.ReturnAndFree(sql); |
208 | 241 | }
|
209 | 242 |
|
210 | 243 | public override string ToSelectStatement(ModelDefinition modelDef,
|
|
0 commit comments