?? sqliteconvert.cs
字號:
int n = 0;
List<string> ls = new List<string>();
string s;
while (source.Length > 0)
{
n = source.IndexOfAny(toks, n);
if (n == -1) break;
if (source[n] == toks[0])
{
source = source.Remove(n, 1);
n = source.IndexOfAny(quot, n);
if (n == -1)
{
source = "\"" + source;
break;
}
source = source.Remove(n, 1);
}
else
{
s = source.Substring(0, n).Trim();
source = source.Substring(n + 1).Trim();
if (s.Length > 0) ls.Add(s);
n = 0;
}
}
if (source.Length > 0) ls.Add(source);
string[] ar = new string[ls.Count];
ls.CopyTo(ar, 0);
return ar;
}
#region Type Conversions
/// <summary>
/// Determines the data type of a column in a statement
/// </summary>
/// <param name="stmt">The statement to retrieve information for</param>
/// <param name="i">The column to retrieve type information on</param>
/// <returns>Returns a SQLiteType struct</returns>
internal static SQLiteType ColumnToType(SQLiteStatement stmt, int i)
{
SQLiteType typ;
typ.Type = TypeNameToDbType(stmt._sql.ColumnType(stmt, i, out typ.Affinity));
return typ;
}
/// <summary>
/// Converts a SQLiteType to a .NET Type object
/// </summary>
/// <param name="t">The SQLiteType to convert</param>
/// <returns>Returns a .NET Type object</returns>
internal static Type SQLiteTypeToType(SQLiteType t)
{
if (t.Type != DbType.Object)
return SQLiteConvert.DbTypeToType(t.Type);
return _typeaffinities[(int)t.Affinity];
}
static Type[] _typeaffinities = {
null,
typeof(Int64),
typeof(Double),
typeof(string),
typeof(byte[]),
typeof(DBNull),
null,
null,
null,
null,
typeof(DateTime),
null,
};
/// <summary>
/// For a given intrinsic type, return a DbType
/// </summary>
/// <param name="typ">The native type to convert</param>
/// <returns>The corresponding (closest match) DbType</returns>
internal static DbType TypeToDbType(Type typ)
{
TypeCode tc = Type.GetTypeCode(typ);
if (tc == TypeCode.Object)
{
if (typ == typeof(byte[])) return DbType.Binary;
if (typ == typeof(Guid)) return DbType.Guid;
return DbType.String;
}
return _typetodbtype[(int)tc];
}
private static DbType[] _typetodbtype = {
DbType.Object,
DbType.Binary,
DbType.Object,
DbType.Boolean,
DbType.SByte,
DbType.SByte,
DbType.Byte,
DbType.Int16, // 7
DbType.UInt16,
DbType.Int32,
DbType.UInt32,
DbType.Int64, // 11
DbType.UInt64,
DbType.Single,
DbType.Double,
DbType.Decimal,
DbType.DateTime,
DbType.Object,
DbType.String,
};
/// <summary>
/// Returns the ColumnSize for the given DbType
/// </summary>
/// <param name="typ">The DbType to get the size of</param>
/// <returns></returns>
internal static int DbTypeToColumnSize(DbType typ)
{
return _dbtypetocolumnsize[(int)typ];
}
private static int[] _dbtypetocolumnsize = {
2147483647, // 0
2147483647, // 1
1, // 2
1, // 3
8, // 4
8, // 5
8, // 6
8, // 7
8, // 8
16, // 9
2,
4,
8,
2147483647,
1,
4,
2147483647,
8,
2,
4,
8,
8,
2147483647,
2147483647,
2147483647,
2147483647, // 25 (Xml)
};
/// <summary>
/// Convert a DbType to a Type
/// </summary>
/// <param name="typ">The DbType to convert from</param>
/// <returns>The closest-match .NET type</returns>
internal static Type DbTypeToType(DbType typ)
{
return _dbtypeToType[(int)typ];
}
private static Type[] _dbtypeToType = {
typeof(string), // 0
typeof(byte[]), // 1
typeof(byte), // 2
typeof(bool), // 3
typeof(decimal), // 4
typeof(DateTime), // 5
typeof(DateTime), // 6
typeof(decimal), // 7
typeof(double), // 8
typeof(Guid), // 9
typeof(Int16),
typeof(Int32),
typeof(Int64),
typeof(object),
typeof(sbyte),
typeof(float),
typeof(string),
typeof(DateTime),
typeof(UInt16),
typeof(UInt32),
typeof(UInt64),
typeof(double),
typeof(string),
typeof(string),
typeof(string),
typeof(string), // 25 (Xml)
};
/// <summary>
/// For a given type, return the closest-match SQLite TypeAffinity, which only understands a very limited subset of types.
/// </summary>
/// <param name="typ">The type to evaluate</param>
/// <returns>The SQLite type affinity for that type.</returns>
internal static TypeAffinity TypeToAffinity(Type typ)
{
TypeCode tc = Type.GetTypeCode(typ);
if (tc == TypeCode.Object)
{
if (typ == typeof(byte[]) || typ == typeof(Guid))
return TypeAffinity.Blob;
else
return TypeAffinity.Text;
}
return _typecodeAffinities[(int)tc];
}
private static TypeAffinity[] _typecodeAffinities = {
TypeAffinity.Null,
TypeAffinity.Blob,
TypeAffinity.Null,
TypeAffinity.Int64,
TypeAffinity.Int64,
TypeAffinity.Int64,
TypeAffinity.Int64,
TypeAffinity.Int64, // 7
TypeAffinity.Int64,
TypeAffinity.Int64,
TypeAffinity.Int64,
TypeAffinity.Int64, // 11
TypeAffinity.Int64,
TypeAffinity.Double,
TypeAffinity.Double,
TypeAffinity.Double,
TypeAffinity.DateTime,
TypeAffinity.Null,
TypeAffinity.Text,
};
/// <summary>
/// For a given type name, return a closest-match .NET type
/// </summary>
/// <param name="Name">The name of the type to match</param>
/// <returns>The .NET DBType the text evaluates to.</returns>
internal static DbType TypeNameToDbType(string Name)
{
if (String.IsNullOrEmpty(Name)) return DbType.Object;
int x = _typeNames.Length;
for (int n = 0; n < x; n++)
{
if (String.Compare(Name, 0, _typeNames[n].typeName, 0, _typeNames[n].typeName.Length, true, CultureInfo.InvariantCulture) == 0)
return _typeNames[n].dataType;
}
return DbType.Object;
}
#endregion
private static SQLiteTypeNames[] _typeNames = {
new SQLiteTypeNames("COUNTER", DbType.Int64),
new SQLiteTypeNames("AUTOINCREMENT", DbType.Int64),
new SQLiteTypeNames("IDENTITY", DbType.Int64),
new SQLiteTypeNames("LONGTEXT", DbType.String),
new SQLiteTypeNames("LONGCHAR", DbType.String),
new SQLiteTypeNames("LONGVARCHAR", DbType.String),
new SQLiteTypeNames("LONG", DbType.Int64),
new SQLiteTypeNames("TINYINT", DbType.Byte),
new SQLiteTypeNames("INTEGER", DbType.Int64),
new SQLiteTypeNames("INT", DbType.Int32),
new SQLiteTypeNames("VARCHAR", DbType.String),
new SQLiteTypeNames("NVARCHAR", DbType.String),
new SQLiteTypeNames("CHAR", DbType.String),
new SQLiteTypeNames("NCHAR", DbType.String),
new SQLiteTypeNames("TEXT", DbType.String),
new SQLiteTypeNames("NTEXT", DbType.String),
new SQLiteTypeNames("STRING", DbType.String),
new SQLiteTypeNames("DOUBLE", DbType.Double),
new SQLiteTypeNames("FLOAT", DbType.Double),
new SQLiteTypeNames("REAL", DbType.Single),
new SQLiteTypeNames("BIT", DbType.Boolean),
new SQLiteTypeNames("YESNO", DbType.Boolean),
new SQLiteTypeNames("LOGICAL", DbType.Boolean),
new SQLiteTypeNames("BOOL", DbType.Boolean),
new SQLiteTypeNames("NUMERIC", DbType.Decimal),
new SQLiteTypeNames("DECIMAL", DbType.Decimal),
new SQLiteTypeNames("MONEY", DbType.Decimal),
new SQLiteTypeNames("CURRENCY", DbType.Decimal),
new SQLiteTypeNames("TIME", DbType.DateTime),
new SQLiteTypeNames("DATE", DbType.DateTime),
new SQLiteTypeNames("SMALLDATE", DbType.DateTime),
new SQLiteTypeNames("BLOB", DbType.Binary),
new SQLiteTypeNames("BINARY", DbType.Binary),
new SQLiteTypeNames("VARBINARY", DbType.Binary),
new SQLiteTypeNames("IMAGE", DbType.Binary),
new SQLiteTypeNames("GENERAL", DbType.Binary),
new SQLiteTypeNames("OLEOBJECT", DbType.Binary),
new SQLiteTypeNames("GUID", DbType.Guid),
new SQLiteTypeNames("UNIQUEIDENTIFIER", DbType.Guid),
new SQLiteTypeNames("MEMO", DbType.String),
new SQLiteTypeNames("NOTE", DbType.String),
new SQLiteTypeNames("SMALLINT", DbType.Int16),
new SQLiteTypeNames("BIGINT", DbType.Int64),
};
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -