Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit ef6c4f0

Browse files
committed
Override ToString on ColumnSchema
1 parent c5160af commit ef6c4f0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/ServiceStack.OrmLite/ColumnSchema.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using ServiceStack.Text;
23

34
namespace ServiceStack.OrmLite
45
{
@@ -30,6 +31,55 @@ public class ColumnSchema
3031
public object DefaultValue { get; set; }
3132
public string DataTypeName { get; set; }
3233
public string CollationType { get; set; }
34+
35+
public override string ToString()
36+
{
37+
var sql = StringBuilderCache.Allocate();
38+
39+
sql.Append($"{ColumnName.PadRight(18, ' ')} {DataTypeName}");
40+
if (NumericPrecision > 0 || NumericScale > 0)
41+
{
42+
sql.Append("(");
43+
sql.Append(NumericPrecision);
44+
if (NumericScale > 0)
45+
{
46+
sql.Append(",");
47+
sql.Append(NumericScale);
48+
}
49+
sql.Append(")");
50+
}
51+
else if (ColumnSize > 0)
52+
{
53+
sql.Append("(");
54+
sql.Append(ColumnSize);
55+
sql.Append(")");
56+
}
57+
58+
if (IsKey)
59+
{
60+
sql.Append(" PRIMARY KEY");
61+
if (IsAutoIncrement)
62+
{
63+
sql.Append(" ").Append("AUTOINCREMENT");
64+
}
65+
}
66+
else
67+
{
68+
sql.Append(AllowDBNull ? " NULL" : " NOT NULL");
69+
}
70+
71+
if (IsUnique)
72+
{
73+
sql.Append(" UNIQUE");
74+
}
75+
76+
if (DefaultValue != null)
77+
{
78+
sql.AppendFormat(" DEFAULT ({0})", DefaultValue);
79+
}
80+
81+
return StringBuilderCache.ReturnAndFree(sql);
82+
}
3383
}
3484

3585
}

tests/ServiceStack.OrmLite.Tests/SchemaTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public void Can_get_Schema_Table()
149149

150150
var columnSchemas = db.GetTableColumns<Person>();
151151

152+
columnSchemas.Each(x => x.ToString().Print());
153+
152154
columnSchemas.Each(x => x.PrintDump());
153155
}
154156
}

0 commit comments

Comments
 (0)