/// One,Two, "Three, Four", Five
///
/// The resulting array would contain
/// [0] One
/// [1] Two
/// [2] Three, Four
/// [3] Five
///
/// Note that the leading and trailing spaces were removed from each item during the split. ///
/// Source string to split apart /// Separator character /// A string array of the split up elements public static string[] Split(string source, char separator) { char[] toks = new char[2] { '\"', separator }; char[] quot = new char[1] { '\"' }; int n = 0; List ls = new List(); 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 + 1); if (n == -1) { //source = "\"" + source; break; } n++; //source = source.Remove(n, 1); } else { s = source.Substring(0, n).Trim(); if (s.Length > 1 && s[0] == quot[0] && s[s.Length - 1] == s[0]) s = s.Substring(1, s.Length - 2); source = source.Substring(n + 1).Trim(); if (s.Length > 0) ls.Add(s); n = 0; } } if (source.Length > 0) { s = source.Trim(); if (s.Length > 1 && s[0] == quot[0] && s[s.Length - 1] == s[0]) s = s.Substring(1, s.Length - 2); ls.Add(s); } string[] ar = new string[ls.Count]; ls.CopyTo(ar, 0); return ar; } /// /// Convert a value to true or false. /// /// A string or number representing true or false /// public static bool ToBoolean(object source) { if (source is bool) return (bool)source; return ToBoolean(source.ToString()); } /// /// Convert a string to true or false. /// /// A string representing true or false /// /// /// "yes", "no", "y", "n", "0", "1", "on", "off" as well as Boolean.FalseString and Boolean.TrueString will all be /// converted to a proper boolean value. /// public static bool ToBoolean(string source) { if (String.Compare(source, bool.TrueString, StringComparison.OrdinalIgnoreCase) == 0) return true; else if (String.Compare(source, bool.FalseString, StringComparison.OrdinalIgnoreCase) == 0) return false; switch(source.ToLower()) { case "yes": case "y": case "1": case "on": return true; case "no": case "n": case "0": case "off": return false; default: throw new ArgumentException("source"); } } #region Type Conversions /// /// Determines the data type of a column in a statement /// /// The statement to retrieve information for /// The column to retrieve type information on /// The SQLiteType to receive the affinity for the given column internal static void ColumnToType(SQLiteStatement stmt, int i, SQLiteType typ) { typ.Type = TypeNameToDbType(stmt._sql.ColumnType(stmt, i, out typ.Affinity)); } /// /// Converts a SQLiteType to a .NET Type object /// /// The SQLiteType to convert /// Returns a .NET Type object internal static Type SQLiteTypeToType(SQLiteType t) { if (t.Type == DbType.Object) return _affinitytotype[(int)t.Affinity]; else return SQLiteConvert.DbTypeToType(t.Type); } private static Type[] _affinitytotype = { typeof(object), typeof(Int64), typeof(Double), typeof(string), typeof(byte[]), typeof(object), typeof(DateTime), typeof(object) }; /// /// For a given intrinsic type, return a DbType /// /// The native type to convert /// The corresponding (closest match) DbType 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, }; /// /// Returns the ColumnSize for the given DbType /// /// The DbType to get the size of /// 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) }; internal static object DbTypeToNumericPrecision(DbType typ) { return _dbtypetonumericprecision[(int)typ]; } private static object[] _dbtypetonumericprecision = { DBNull.Value, // 0 DBNull.Value, // 1 3, DBNull.Value, 19, DBNull.Value, // 5 DBNull.Value, // 6 53, 53, DBNull.Value, 5, 10, 19, DBNull.Value, 3, 24, DBNull.Value, DBNull.Value, 5, 10, 19, 53, DBNull.Value, DBNull.Value, DBNull.Value }; internal static object DbTypeToNumericScale(DbType typ) { return _dbtypetonumericscale[(int)typ]; } private static object[] _dbtypetonumericscale = { DBNull.Value, // 0 DBNull.Value, // 1 0, DBNull.Value, 4, DBNull.Value, // 5 DBNull.Value, // 6 DBNull.Value, DBNull.Value, DBNull.Value, 0, 0, 0, DBNull.Value, 0, DBNull.Value, DBNull.Value, DBNull.Value, 0, 0, 0, 0, DBNull.Value, DBNull.Value, DBNull.Value }; internal static string DbTypeToTypeName(DbType typ) { for (int n = 0; n < _dbtypeNames.Length; n++) { if (_dbtypeNames[n].dataType == typ) return _dbtypeNames[n].typeName; } return String.Empty; } private static SQLiteTypeNames[] _dbtypeNames = { new SQLiteTypeNames("INTEGER", DbType.Int64), new SQLiteTypeNames("TINYINT", DbType.Byte), new SQLiteTypeNames("INT", DbType.Int32), new SQLiteTypeNames("VARCHAR", DbType.AnsiString), new SQLiteTypeNames("NVARCHAR", DbType.String), new SQLiteTypeNames("CHAR", DbType.AnsiStringFixedLength), new SQLiteTypeNames("NCHAR", DbType.StringFixedLength), new SQLiteTypeNames("FLOAT", DbType.Double), new SQLiteTypeNames("REAL", DbType.Single), new SQLiteTypeNames("BIT", DbType.Boolean), new SQLiteTypeNames("DECIMAL", DbType.Decimal), new SQLiteTypeNames("DATETIME", DbType.DateTime), new SQLiteTypeNames("BLOB", DbType.Binary), new SQLiteTypeNames("UNIQUEIDENTIFIER", DbType.Guid), new SQLiteTypeNames("SMALLINT", DbType.Int16), }; /// /// Convert a DbType to a Type /// /// The DbType to convert from /// The closest-match .NET type 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) }; /// /// For a given type, return the closest-match SQLite TypeAffinity, which only understands a very limited subset of types. /// /// The type to evaluate /// The SQLite type affinity for that type. 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, }; /// /// For a given type name, return a closest-match .NET type /// /// The name of the type to match /// The .NET DBType the text evaluates to. 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), }; } /// /// SQLite has very limited types, and is inherently text-based. The first 5 types below represent the sum of all types SQLite /// understands. The DateTime extension to the spec is for internal use only. /// public enum TypeAffinity { /// /// Not used /// Uninitialized = 0, /// /// All integers in SQLite default to Int64 /// Int64 = 1, /// /// All floating point numbers in SQLite default to double /// Double = 2, /// /// The default data type of SQLite is text /// Text = 3, /// /// Typically blob types are only seen when returned from a function /// Blob = 4, /// /// Null types can be returned from functions /// Null = 5, /// /// Used internally by this provider /// DateTime = 10, /// /// Used internally /// None = 11, } /// /// This implementation of SQLite for ADO.NET can process date/time fields in databases in only one of three formats. Ticks, ISO8601 /// and JulianDay. /// /// /// ISO8601 is more compatible, readable, fully-processable, but less accurate as it doesn't provide time down to fractions of a second. /// JulianDay is the numeric format the SQLite uses internally and is arguably the most compatible with 3rd party tools. It is /// not readable as text without post-processing. /// Ticks less compatible with 3rd party tools that query the database, and renders the DateTime field unreadable as text without post-processing. /// /// The preferred order of choosing a datetime format is JulianDay, ISO8601, and then Ticks. Ticks is mainly present for legacy /// code support. /// public enum SQLiteDateFormats { /// /// Using ticks is not recommended and is not well supported with LINQ. /// Ticks = 0, /// /// The default format for this provider. /// ISO8601 = 1, /// /// JulianDay format, which is what SQLite uses internally /// JulianDay = 2 } /// /// This enum determines how SQLite treats its journal file. /// /// /// By default SQLite will create and delete the journal file when needed during a transaction. /// However, for some computers running certain filesystem monitoring tools, the rapid /// creation and deletion of the journal file can cause those programs to fail, or to interfere with SQLite. /// /// If a program or virus scanner is interfering with SQLite's journal file, you may receive errors like "unable to open database file" /// when starting a transaction. If this is happening, you may want to change the default journal mode to Persist. /// public enum SQLiteJournalModeEnum { /// /// The default mode, this causes SQLite to create and destroy the journal file as-needed. /// Delete = 0, /// /// When this is set, SQLite will keep the journal file even after a transaction has completed. It's contents will be erased, /// and the journal re-used as often as needed. If it is deleted, it will be recreated the next time it is needed. /// Persist = 1, /// /// This option disables the rollback journal entirely. Interrupted transactions or a program crash can cause database /// corruption in this mode! /// Off = 2 } /// /// Struct used internally to determine the datatype of a column in a resultset /// internal class SQLiteType { /// /// The DbType of the column, or DbType.Object if it cannot be determined /// internal DbType Type; /// /// The affinity of a column, used for expressions or when Type is DbType.Object /// internal TypeAffinity Affinity; } internal struct SQLiteTypeNames { internal SQLiteTypeNames(string newtypeName, DbType newdataType) { typeName = newtypeName; dataType = newdataType; } internal string typeName; internal DbType dataType; } }