using System;
using System.CodeDom.Compiler;
using System.Data;
using Microsoft.VisualBasic;
using SubSonic.Utilities;
namespace SubSonic
{
public class VBCodeLanguage : ICodeLanguage
{
#region Constants & enumerations
private readonly string[] keywords = {
// keywords
"alias", "addHandler", "ansi", "as", "assembly", "auto", "binary", "byref", "byval", "case", "catch", "class",
"custom", "date", "datetime", "default", "directcast", "each", "else", "elseif", "end", "error", "false",
"finally", "for", "friend", "global", "handles", "implements", "in", "is", "lib", "loop", "me", "module",
"mustinherit", "mustoverride", "mybase", "myclass", "narrowing", "new", "next", "nothing", "notinheritable",
"notoverridable", "of", "off", "on", "option", "optional", "overloads", "overridable", "overrides", "paramarray",
"partial", "preserve", "private", "property", "protected", "public", "raiseevent", "readonly", "resume", "shadows",
"shared", "static", "step", "structure", "text", "then", "to", "true", "trycast", "unicode", "until", "when",
"while", "widening", "withevents", "writeonly",
// unreserved keywords
"compare", "explicit", "isfalse", "istrue", "mid", "strict"
};
#endregion
#region Properties
public string CodeProvider
{
get { return "VBCodeProvider"; }
}
public string FileExtension
{
get { return SubSonic.FileExtension.DOT_VB; }
}
public string Identifier
{
get { return "VB"; }
}
public string ShortName
{
get { return "vb"; }
}
public string TemplatePrefix
{
get { return "VB_"; }
}
#endregion
#region Methods
public CodeDomProvider CreateCodeProvider()
{
return new VBCodeProvider();
}
public string GetDefaultValue(string colName, DbType dbType, bool isNullableColumn)
{
string variableType = GetDefaultValue(colName, dbType);
if (isNullableColumn && Utility.IsNullableDbType(dbType))
variableType = String.Format(CodeFragment.NULLABLE_VARIABLE_VB, variableType);
return variableType;
}
public string GetUsingStatements(string[] namespaces)
{
string usingStatements = "";
foreach (string space in namespaces)
usingStatements += "Imports " + space + Environment.NewLine;
return usingStatements;
}
public string GetVariableType(DbType dbType, bool isNullableColumn)
{
string variableType = GetVariableType(dbType);
if (isNullableColumn && Utility.IsNullableDbType(dbType))
variableType = String.Format(CodeFragment.NULLABLE_VARIABLE_VB, variableType);
return variableType;
}
public bool IsKeyword(string word)
{
return Array.IndexOf(keywords, word.ToLower()) != -1;
}
#endregion
#region Private methods
private static string GetDefaultValue(string colName, DbType dbType)
{
if (Utility.IsLogicalDeleteColumn(colName))
return "False";
switch (dbType) {
case DbType.Guid:
return "Guid.Empty";
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
return "Nothing";
case DbType.Boolean:
return "False";
case DbType.Binary:
return "Nothing";
case DbType.Xml:
return "";
case DbType.Date:
case DbType.DateTime:
return "New Date(1900,01,01)";
default:
return "0";
}
}
private static string GetVariableType(DbType dbType)
{
switch (dbType) {
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength: return "String";
case DbType.Binary: return "Byte()";
case DbType.Boolean: return "Boolean";
case DbType.Byte: return "Byte";
case DbType.Currency:
case DbType.Decimal:
case DbType.VarNumeric: return "Decimal";
case DbType.Date:
case DbType.DateTime: return "DateTime";
case DbType.Double: return "Double";
case DbType.Guid: return "Guid";
case DbType.Int16: return "Short";
case DbType.Int32: return "Integer";
case DbType.Int64: return "Long";
case DbType.Object: return "Object";
case DbType.SByte: return "SByte";
case DbType.Single: return "Single";
case DbType.Time: return "TimeSpan";
case DbType.UInt16: return "UShort";
case DbType.UInt32: return "UInteger";
case DbType.UInt64: return "ULong";
default: return "String";
}
}
#endregion
}
}