forked from sethreno/schemazen
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script and recreate user defined types
- Loading branch information
1 parent
aec5c07
commit 2eec3bc
Showing
3 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace SchemaZen.Library.Models { | ||
|
||
public class UserDefinedType : INameable, IHasOwner, IScriptable { | ||
|
||
public string Name { get; set; } | ||
|
||
public string Owner { get; set; } | ||
|
||
public string BaseTypeName { get; set; } | ||
|
||
public string MaxLength { | ||
get { | ||
switch (BaseTypeName) { | ||
case "nvarchar": | ||
return _maxLength == -1 ? "max" : ((short) (_maxLength / 2)).ToString(); | ||
case "varchar": | ||
case "binary": | ||
case "char": | ||
case "nchar": | ||
case "varbinary": | ||
return _maxLength == -1 ? "max" : _maxLength.ToString(); | ||
default: | ||
return _maxLength.ToString(); | ||
} | ||
} | ||
} | ||
private short _maxLength; | ||
|
||
private bool HasMaxLength() { | ||
switch (BaseTypeName) { | ||
case "bigint": | ||
case "bit": | ||
case "date": | ||
case "datetime": | ||
case "datetime2": | ||
case "datetimeoffset": | ||
case "float": | ||
case "hierarchyid": | ||
case "image": | ||
case "int": | ||
case "money": | ||
case "ntext": | ||
case "real": | ||
case "smalldatetime": | ||
case "smallint": | ||
case "smallmoney": | ||
case "sql_variant": | ||
case "text": | ||
case "time": | ||
case "timestamp": | ||
case "tinyint": | ||
case "uniqueidentifier": | ||
case "geography": | ||
case "xml": | ||
case "sysname": | ||
return false; | ||
case "decimal": | ||
case "numberic": | ||
throw new Exception("Precision and Scale not handled yet."); | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
public string Nullable { get { return _nullable ? "NULL" : "NOT NULL"; } } | ||
private readonly bool _nullable; | ||
|
||
public UserDefinedType(string owner, | ||
string name, | ||
string baseTypeName, | ||
short maxLength, | ||
bool nullable) { | ||
Owner = owner; | ||
Name = name; | ||
BaseTypeName = baseTypeName; | ||
_maxLength = maxLength; | ||
_nullable = nullable; | ||
} | ||
|
||
public string ScriptCreate() { | ||
var text = new StringBuilder(); | ||
|
||
text.AppendFormat("CREATE TYPE [{0}].[{1}] FROM [{2}]", | ||
Owner, Name, BaseTypeName); | ||
|
||
if (HasMaxLength()) { | ||
text.AppendFormat(" ({0})", MaxLength); | ||
} | ||
|
||
text.AppendFormat(" {0}", Nullable); | ||
|
||
return text.ToString(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters