//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Globalization;
namespace OrmLib{
/// <summary>
/// This is an internal class and should not be used.
/// </summary>
/// <remarks>
/// Wrapper to construct types.
/// </remarks>
public class TypeHelper
{
private TypeHelper(){}
/// <summary>
/// This is an internal method and should not be used.
/// </summary>
/// <remarks>
/// Constructs a an object of the given type.
/// </remarks>
/// <param name="t">Type to construct</param>
/// <returns>A new object of the given type</returns>
public static object ConstructNoParams( System.Type t)
{
return t.GetConstructor( new Type[0] ).Invoke( new object[0] );
}
internal static object EscapeQuotes(object data)
{
if (data is string) return ((string)data).Replace("'", "''");
else return data;
}
/// <summary>
///
/// </summary>
/// <param name="val"></param>
internal static string CastTypeToString(object val)
{
Type t = val.GetType();
// null?
if ( (t.GetInterface("INullable") != null && ((INullable)val).IsNull) || val == null || val == DBNull.Value || t == typeof(DBNull) )
{
return "NULL";
}
// sql type? factor it out
if ( t.Namespace == "System.Data.SqlTypes" && t.GetProperty("Value") != null )
{
val = t.GetProperty("Value").GetValue(val,null);
t = t.GetProperty("Value").PropertyType;
}
switch ( t.Name )
{
case "Boolean":
return (Boolean)val ? "1":"0";
case "Byte":
return ((int)((Byte)val)).ToString();
case "Byte[]":
return "0x" + Formatter.ToHex((Byte[])val );
case "Decimal":
return ((Decimal)val ).ToString(CultureInfo.InvariantCulture.NumberFormat);
case "Double":
return ((Double)val ).ToString(CultureInfo.InvariantCulture.NumberFormat);
case "DateTime":
return string.Format("CONVERT(DATETIME,'{0:yyyy-MM-dd HH:mm:ss:fff}',121)",(DateTime)val );
default:
return val.ToString().Replace("'","''");
}
}
/// <summary>
/// Quote the data for use in an SQL statement.
/// </summary>
/// <param name="input">The data to quote.</param>
internal string Quote(object input)
{
return string.Format("N'{0}'", input);
}
/// <summary>
/// Returns the sql type of a native type, in string format.
/// </summary>
/// <param name="dataType">The type to convert.</param>
/// <returns>The SQL type of the type.</returns>
internal static string GetSqlTypeString(System.Type dataType)
{
switch ( dataType.FullName )
{
case ( "System.Int32" ): return "int";
case ( "System.Int64" ): return "bigint";
case ( "System.Int16" ): return "smallint";
case ( "System.Byte" ): return "tinyint";
case ( "System.Bool" ): return "bit";
case ( "System.Decimal" ): return "decimal";
case ( "System.DateTime" ): return "datetime";
case ( "System.Guid" ): return "varchar(36)";
case ( "System.String"): return "varchar(7000)";
case ( "System.Data.SqlTypes.SqlBinary" ): return "binary";
case ( "System.Data.SqlTypes.SqlInt64" ): return "bigint";
case ( "System.Data.SqlTypes.SqlString" ): return "varchar(7000)";
case ( "System.Data.SqlTypes.SqlDateTime" ): return "datetime";
case ( "System.Data.SqlTypes.SqlDecimal" ): return "decimal";
case ( "System.Data.SqlTypes.SqlDouble" ): return "float";
case ( "System.Data.SqlTypes.SqlInt32" ): return "int";
case ( "System.Data.SqlTypes.SqlMoney" ): return "money";
case ( "System.Data.SqlTypes.SqlSingle" ): return "real";
case ( "System.Data.SqlTypes.SqlInt16" ): return "smallint";
case ( "System.Data.SqlTypes.SqlByte" ): return "tinyint";
case ( "System.Data.SqlTypes.SqlGuid" ): return "uniqueidentifier";
default: return "sql_variant";
}
}
internal static string GetStartWildCard(MatchType matchType)
{
switch ( matchType )
{
case ( MatchType.Partial ):
return "%";
case ( MatchType.EndsWith ):
return "%";
default:
return "";
}
}
internal static string GetEndWildCard(MatchType matchType)
{
switch ( matchType )
{
case ( MatchType.Partial ):
return "%";
case ( MatchType.StartsWith ):
return "%";
default:
return "";
}
}
internal static string GetNotStatement(MatchType matchType)
{
switch ( matchType )
{
case ( MatchType.IsNotNull ):
return "NOT";
case ( MatchType.Not ):
return "NOT";
case ( MatchType.NotIn ):
return "NOT";
case ( MatchType.NotLike ):
return "NOT";
default:
return "";
}
}
/// <summary>
/// Helper function to set the way data should be quoted.
/// </summary>
/// <remarks>
/// SqlTypes will be factored out.
/// </remarks>
/// <param name="t">The type of data</param>
/// <returns>The quote that should be used to wrap the data.</returns>
internal static string QuoteChar(System.Type t, bool startPosition)
{
if ( t.Namespace == "System.Data.SqlTypes" && t.GetProperty("Value") != null )
{
t = t.GetProperty("Value").PropertyType;
}
switch ( t.Name )
{
case "Boolean":
return "";
case "Byte":
return "";
case "Byte[]":
return "";
case "Decimal":
return "";
case "Double":
return "";
case "Int16":
return "";
case "Int32":
return "";
case "Int64":
return "";
case "Single":
return "";
case "DateTime":
return "";
case "String":
if ( startPosition )
return "N'";
else
return "'";
case "Guid":
if ( startPosition )
return "N'";
else
return "'";
default:
if ( startPosition )
return "N'";
else
return "'";
}
}
/// <summary>
/// Maps the enum MatchType to it's SQL operator.
/// </summary>
public static string GetOperator(MatchType matchType)
{
switch ( matchType )
{
case ( MatchType.Exact ):
return "=";
case ( MatchType.Not ):
return "<>";
case ( MatchType.Partial ):
return "LIKE";
case ( MatchType.StartsWith ):
return "LIKE";
case ( MatchType.EndsWith ):
return "LIKE";
case ( MatchType.Greater ):
return ">";
case ( MatchType.Lesser ):
return "<";
case ( MatchType.In ):
return "IN";
case ( MatchType.LesserOrEqual ):
return "<=";
case ( MatchType.GreaterOrEqual ):
return ">=";
case ( MatchType.Like ):
return "LIKE";
case ( MatchType.NotIn ):
return "NOT IN";
case ( MatchType.NotLike ):
return "NOT LIKE";
default:
return "=";
}
}
}
}
|