Beispiel #1
0
        private static string GetErrorString(SQLiteErrorCode errorCode, SqliteDatabaseHandle database)
        {
#if NET45
            string errorString = SQLiteConnection.FromUtf8(NativeMethods.sqlite3_errstr(errorCode));
#else
            string errorString = errorCode.ToString();
#endif
            return(database != null ? "{0}: {1}".FormatInvariant(errorString, SQLiteConnection.FromUtf8(NativeMethods.sqlite3_errmsg(database)))
                                : errorString);
        }
        public override string GetName(int ordinal)
        {
            VerifyHasResult();
            if (ordinal < 0 || ordinal > FieldCount)
            {
                throw new ArgumentOutOfRangeException("ordinal", "value must be between 0 and {0}.".FormatInvariant(FieldCount - 1));
            }

            return(SQLiteConnection.FromUtf8(NativeMethods.sqlite3_column_name(m_currentStatement, ordinal)));
        }
        public override int GetOrdinal(string name)
        {
            VerifyHasResult();

            if (m_columnNames == null)
            {
                var columnNames = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);
                for (int i = 0; i < FieldCount; i++)
                {
                    string columnName = SQLiteConnection.FromUtf8(NativeMethods.sqlite3_column_name(m_currentStatement, i));
                    columnNames[columnName] = i;
                }
                m_columnNames = columnNames;
            }

            int ordinal;

            if (!m_columnNames.TryGetValue(name, out ordinal))
            {
                throw new IndexOutOfRangeException("The column name '{0}' does not exist in the result set.".FormatInvariant(name));
            }
            return(ordinal);
        }
        public override object GetValue(int ordinal)
        {
            VerifyRead();
            if (ordinal < 0 || ordinal > FieldCount)
            {
                throw new ArgumentOutOfRangeException("ordinal", "value must be between 0 and {0}.".FormatInvariant(FieldCount - 1));
            }

            // determine (and cache) the declared type of the column (e.g., from the SQL schema)
            DbType dbType;

            if (m_columnType[ordinal].HasValue)
            {
                dbType = m_columnType[ordinal].Value;
            }
            else
            {
                IntPtr declType = NativeMethods.sqlite3_column_decltype(m_currentStatement, ordinal);
                if (declType != IntPtr.Zero)
                {
                    string type = SQLiteConnection.FromUtf8(declType);

                    // Fixed CHAR(n) or variable VARCHAR(n) type
                    Match match = PatternChar.Match(type);
                    if (match.Success)
                    {
                        dbType = DbType.String;
                    }
                    else if (!s_sqlTypeToDbType.TryGetValue(type, out dbType))
                    {
                        throw new NotSupportedException("The data type name '{0}' is not supported.".FormatInvariant(type));
                    }
                }
                else
                {
                    dbType = DbType.Object;
                }
                m_columnType[ordinal] = dbType;
            }

            var sqliteType = NativeMethods.sqlite3_column_type(m_currentStatement, ordinal);

            if (dbType == DbType.Object)
            {
                dbType = s_sqliteTypeToDbType[sqliteType];
            }

            switch (sqliteType)
            {
            case SQLiteColumnType.Null:
                return(DBNull.Value);

            case SQLiteColumnType.Blob:
                int    byteCount = NativeMethods.sqlite3_column_bytes(m_currentStatement, ordinal);
                byte[] bytes     = new byte[byteCount];
                if (byteCount > 0)
                {
                    IntPtr bytePointer = NativeMethods.sqlite3_column_blob(m_currentStatement, ordinal);
                    Marshal.Copy(bytePointer, bytes, 0, byteCount);
                }
                return(dbType == DbType.Guid && byteCount == 16 ? (object)new Guid(bytes) : (object)bytes);

            case SQLiteColumnType.Double:
                double doubleValue = NativeMethods.sqlite3_column_double(m_currentStatement, ordinal);
                return(dbType == DbType.Single ? (object)(float)doubleValue : (object)doubleValue);

            case SQLiteColumnType.Integer:
                long integerValue = NativeMethods.sqlite3_column_int64(m_currentStatement, ordinal);
                return(dbType == DbType.Int32 ? (object)(int)integerValue :
                       dbType == DbType.Boolean ? (object)(integerValue != 0) :
                       dbType == DbType.Int16 ? (object)(short)integerValue :
                       dbType == DbType.Byte ? (object)(byte)integerValue :
                       dbType == DbType.Single ? (object)(float)integerValue :
                       dbType == DbType.Double ? (object)(double)integerValue :
                       (object)integerValue);

            case SQLiteColumnType.Text:
                int    stringLength = NativeMethods.sqlite3_column_bytes(m_currentStatement, ordinal);
                string stringValue  = SQLiteConnection.FromUtf8(NativeMethods.sqlite3_column_text(m_currentStatement, ordinal), stringLength);
                return(dbType == DbType.DateTime ? (object)DateTime.ParseExact(stringValue, s_dateTimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal) :
                       (object)stringValue);

            default:
                throw new InvalidOperationException();
            }
        }