private static IEnumerable <ReaderItem> ExecuteReader(this SQLiteCommand sqliteCommand, SQLiteConnection connection)
        {
            sqlite3_stmt stmt = Prepare(connection, sqliteCommand.CommandText);

            try
            {
                // We need to manage columns dynamically in order to create columns
                ColumnLite[] cols = new ColumnLite[SQLite3.ColumnCount(stmt)];

                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    ReaderItem obj = new ReaderItem();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            // We try to create column mapping if it's not already created :
                            string name = SQLite3.ColumnName16(stmt, i);
                            cols[i] = new ColumnLite(name, SQLite3.ColumnType(stmt, i).ToType());
                        }
                        SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                        object          val     = ReadCol(connection, stmt, i, colType, cols[i].ColumnType);
                        obj[cols[i].Name] = val;
                    }
                    yield return(obj);
                }
            }
            finally
            {
                SQLite3.Finalize(stmt);
            }
        }
예제 #2
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                _conn.InvokeTrace("Executing Query: " + this);
            }
            T result = default(T);

            lock (_conn.SyncObject)
            {
                IntPtr stmt = Prepare();
                try
                {
                    SQLite3.Result result2 = SQLite3.Step(stmt);
                    switch (result2)
                    {
                    case SQLite3.Result.Row:
                    {
                        SQLite3.ColType type = SQLite3.ColumnType(stmt, 0);
                        return((T)ReadCol(stmt, 0, type, typeof(T)));
                    }

                    default:
                        throw SQLiteException.New(result2, SQLite3.GetErrmsg(_conn.Handle));

                    case SQLite3.Result.Done:
                        return(result);
                    }
                }
                finally
                {
                    Finalize(stmt);
                }
            }
        }
예제 #3
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            T val = default(T);

            IntPtr stmt = Prepare();

            try {
                SQLite3.Result r = SQLite3.Step(stmt);
                if (r == SQLite3.Result.Row)
                {
                    SQLite3.ColType colType = SQLite3.ColumnType(stmt, 0);
                    val = (T)ReadCol(stmt, 0, colType, typeof(T));
                }
                else if (r == SQLite3.Result.Done)
                {
                }
                else
                {
                    throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle));
                }
            } finally {
                Finalize(stmt);
            }

            return(val);
        }
        /// <summary>
        /// execute a query</summary>
        /// <returns>
        /// return a JArray which contains all objects </returns>
        /// <param name="query"> contains the query to execute</param>
        /// <param name="param"> contains the parameters needed to execute the query</param>
        public JArray execute(String query, object[] param)
        {
            JArray array = new JArray();

            if (query != null && query.Length > 0)
            {
                stmt = SQLite3.Prepare2(this.Handle, query);
                if (stmt != null)
                {
                    if (param != null && param.Length > 0)
                    {
                        for (int i = 1; i <= param.Length; i++)
                        {
                            bindValue(param.GetValue(i - 1), i);
                        }
                    }

                    while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                    {
                        int     count = SQLite3.ColumnCount(stmt);
                        JObject obj   = new JObject();
                        for (int i = 0; i < count; i++)
                        {
                            string          name    = SQLite3.ColumnName(stmt, i);
                            SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                            switch (colType)
                            {
                            case SQLite3.ColType.Blob:
                                byte[] bytes = SQLite3.ColumnByteArray(stmt, i);
                                obj.Add(name, bytes);
                                break;

                            case SQLite3.ColType.Integer:
                                int intValue = SQLite3.ColumnInt(stmt, i);
                                obj.Add(name, intValue);
                                break;

                            case SQLite3.ColType.Float:
                                double doubleValue = SQLite3.ColumnDouble(stmt, i);
                                obj.Add(name, doubleValue);
                                break;

                            case SQLite3.ColType.Text:
                                string text = SQLite3.ColumnString(stmt, i);
                                obj.Add(name, text);
                                break;

                            case SQLite3.ColType.Null:
                            default:
                                obj.Add(name, null);
                                break;
                            }
                        }
                        array.Add(obj);
                    }
                }
                SQLite3.Finalize(stmt);
            }
            return(array);
        }
        /// <summary>
        /// Get a typeof() element from ColType enumeration.
        /// </summary>
        /// <param name="colType"></param>
        /// <returns></returns>
        private static Type ToType(this SQLite3.ColType colType)
        {
            switch (colType)
            {
            case SQLite3.ColType.Blob:
                return(typeof(byte[]));

            case SQLite3.ColType.Float:
                return(typeof(double));

            case SQLite3.ColType.Integer:
                return(typeof(long));

            default:
                return(typeof(string));
            }
        }
        private Type SQLiteColumnTypeToType(SQLite3.ColType colType)
        {
            switch (colType)
            {
            case SQLite3.ColType.Blob:
                return(typeof(byte[]));

            case SQLite3.ColType.Float:
                return(typeof(double));

            case SQLite3.ColType.Integer:
                return(typeof(Int32));

            case SQLite3.ColType.Null:
                return(typeof(string));
            }
            return(typeof(string));
        }
예제 #7
0
        public static Type convertTypeToCLRType(SQLite3.ColType colType)
        {
            switch (colType)
            {
            case SQLite3.ColType.Blob:
                return(typeof(byte[]));

            case SQLite3.ColType.Float:
                return(typeof(float));

            case SQLite3.ColType.Integer:
                return(typeof(int));

            case SQLite3.ColType.Text:
                return(typeof(string));

            case SQLite3.ColType.Null:
            default:
                return(typeof(string));
            }
        }
        private object ReadCol(SQLitePCL.sqlite3_stmt stmt, int index, SQLite3.ColType type)
        {
            switch (type)
            {
            case SQLite3.ColType.Blob:
                return(SQLite3.ColumnByteArray(stmt, index));

            case SQLite3.ColType.Float:
                return(SQLite3.ColumnDouble(stmt, index));

            case SQLite3.ColType.Integer:
                return(SQLite3.ColumnInt64(stmt, index));

            case SQLite3.ColType.Null:
                return(null);

            case SQLite3.ColType.Text:
                return(SQLite3.ColumnString(stmt, index));
            }
            return(null);
        }
예제 #9
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            IntPtr stmt = Prepare();

            try {
                TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

                for (int i = 0; i < cols.Length; i++)
                {
                    string name = SQLite3.ColumnName16(stmt, i);
                    cols[i] = map.FindColumn(name);
                }

                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    object obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                        object          val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        cols[i].SetValue(obj, val);
                    }

                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            } finally {
                SQLite3.Finalize(stmt);
            }
        }
예제 #10
0
 public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
 {
     if (_conn.Trace)
     {
         _conn.InvokeTrace("Executing Query: " + this);
     }
     lock (_conn.SyncObject)
     {
         IntPtr stmt = Prepare();
         try
         {
             TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];
             for (int j = 0; j < cols.Length; j++)
             {
                 string name = SQLite3.ColumnName16(stmt, j);
                 cols[j] = map.FindColumn(name);
             }
             while (SQLite3.Step(stmt) == SQLite3.Result.Row)
             {
                 object obj = Activator.CreateInstance(map.MappedType);
                 for (int i = 0; i < cols.Length; i++)
                 {
                     if (cols[i] != null)
                     {
                         SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                         object          val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                         cols[i].SetValue(obj, val);
                     }
                 }
                 OnInstanceCreated(obj);
                 yield return((T)obj);
             }
         }
         finally
         {
             ((_003CExecuteDeferredQuery_003Ec__Iterator3 <T>) /*Error near IL_0234: stateMachine*/)._003C_003E__Finally0();
         }
     }
 }
예제 #11
0
        object ReadCol(Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clrType)
        {
            if (type == SQLite3.ColType.Null)
            {
                return(null);
            }
            else
            {
                if (clrType == typeof(String))
                {
                    return(SQLite3.ColumnString(stmt, index));
                }
                else if (clrType == typeof(Int32))
                {
                    return((int)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(Boolean))
                {
                    return(SQLite3.ColumnInt(stmt, index) == 1);
                }
                else if (clrType == typeof(double))
                {
                    return(SQLite3.ColumnDouble(stmt, index));
                }
                else if (clrType == typeof(float))
                {
                    return((float)SQLite3.ColumnDouble(stmt, index));
                }
                else if (clrType == typeof(TimeSpan))
                {
                    return(new TimeSpan(SQLite3.ColumnInt64(stmt, index)));
                }
                else if (clrType == typeof(DateTime))
                {
                    if (_conn.StoreDateTimeAsTicks)
                    {
                        return(new DateTime(SQLite3.ColumnInt64(stmt, index)));
                    }
                    else
                    {
                        var text = SQLite3.ColumnString(stmt, index);
                        return(DateTime.Parse(text));
                    }
                }
                else if (clrType == typeof(DateTimeOffset))
                {
                    return(new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero));

#if !NETFX_CORE
                }
                else if (clrType.IsEnum)
                {
#else
                }
                else if (clrType.GetTypeInfo().IsEnum)
                {
#endif
                    if (type == SQLite3.ColType.Text)
                    {
                        return((int)Enum.Parse(clrType, SQLite3.ColumnString(stmt, index)));
                    }
                    else
                    {
                        return(SQLite3.ColumnInt(stmt, index));
                    }
                }
                else if (clrType == typeof(Int64))
                {
                    return(SQLite3.ColumnInt64(stmt, index));
                }
                else if (clrType == typeof(UInt32))
                {
                    return((uint)SQLite3.ColumnInt64(stmt, index));
                }
                else if (clrType == typeof(decimal))
                {
                    return((decimal)SQLite3.ColumnDouble(stmt, index));
                }
                else if (clrType == typeof(Byte))
                {
                    return((byte)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(UInt16))
                {
                    return((ushort)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(Int16))
                {
                    return((short)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(sbyte))
                {
                    return((sbyte)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(byte[]))
                {
                    return(SQLite3.ColumnByteArray(stmt, index));
                }
                else if (clrType == typeof(Guid))
                {
                    var text = SQLite3.ColumnString(stmt, index);
                    return(new Guid(text));
                }
                else
                {
                    throw new NotSupportedException("Don't know how to read " + clrType);
                }
            }
        }
예제 #12
0
        public IEnumerator <IDictionary <String, Object> > ExecuteSelectQuery(DatabaseDescriptor databaseDescriptor, EntityDescriptor entityDescriptor, String query)
        {
            #if XAMARIN
            SQLitePCL.sqlite3_stmt statement = SQLite3.Prepare2(sqliteDatabase.Handle, query);
            #elif WINDOWS
            List <SQLiteQueryRow> statement = null;
            #endif

            try
            {
                #if XAMARIN
                statement = SQLite3.Prepare2(sqliteDatabase.Handle, query);
                #elif WINDOWS
                statement = sqliteDatabase.Query2(query);
                #endif
            }
            catch (System.Exception exception)
            {
                if (sqliteDatabase.IsInTransaction)
                {
                    sqliteDatabase.Rollback();
                }

                Log.Log.Error(typeof(Database).FullName, "ExecuteSelectQuery", "Exception caught while executing the select query, QUERY: " + query);
                throw new DatabaseException(typeof(Database).FullName, "ExecuteSelectQuery", exception.Message);
            }

            List <Dictionary <String, Object> > tuples = new List <Dictionary <String, Object> >();

            #if XAMARIN
            SQLite3.Result result;
            while ((result = SQLite3.Step(statement)) == SQLite3.Result.Row)
            {
                IDictionary <String, Object> tuple = new Dictionary <String, Object>();

                String stmtResult = result.ToString();

                //string[] names = SQLite3.ColType.GetNames ();
                //string[] values = SQLite3.ColType.GetValues ();
                int columnsCount = SQLite3.ColumnCount(statement);
                for (int i = 0; i < columnsCount; i++)
                {
                    String          columnName  = SQLite3.ColumnName(statement, i);
                    SQLite3.ColType columnType  = SQLite3.ColumnType(statement, i);
                    Object          columnValue = SQLite3.ColumnText(statement, i);

                    bool isString = false;
                    bool isLong   = false;
                    bool isFloat  = false;
                    bool isBlob   = false;

                    if (columnType == SQLite3.ColType.Text)
                    {
                        isString = true;
                    }
                    else if (columnType == SQLite3.ColType.Integer)
                    {
                        isLong = true;
                    }
                    else if (columnType == SQLite3.ColType.Float)
                    {
                        isFloat = true;
                    }
                    else if (columnType == SQLite3.ColType.Blob)
                    {
                        isBlob = true;
                    }
                    else if (columnType == SQLite3.ColType.Null)
                    {
                    }


                    if (isString)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_PRIMITIVE_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? true : false);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? Boolean.TrueString : Boolean.FalseString);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isLong)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isFloat)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }

                        tuple.Add(columnName, columnValue);
                    }
                    else if (isBlob)
                    {
                        tuple.Add(columnName, columnValue);
                    }
                }

                tuples.Add((Dictionary <String, Object>)tuple);
            }

            SQLite3.Finalize(statement);

            return(tuples.GetEnumerator());
            #elif WINDOWS
            foreach (SQLiteQueryRow cursor in statement)
            {
                IDictionary <String, Object> tuple = new Dictionary <String, Object>();

                List <SQLiteQueryColumn> columns = cursor.column;
                if (columns == null || columns.Count <= 0)
                {
                    continue;
                }

                foreach (SQLiteQueryColumn column in columns)
                {
                    String columnName  = column.Key;
                    Object columnValue = column.Value;


                    bool isString = false;
                    bool isLong   = false;
                    bool isFloat  = false;
                    bool isBlob   = false;

                    if (columnValue != null)
                    {
                        if (columnValue.GetType().FullName.Equals(typeof(String).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isString = true;
                        }
                        else if (columnValue.GetType().FullName.Equals(typeof(long).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isLong = true;
                        }
                        else if (columnValue.GetType().FullName.Equals(typeof(float).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isFloat = true;
                        }
                        else if (columnValue.GetType().FullName.Equals(typeof(byte).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isBlob = true;
                        }
                    }
                    else
                    {
                        isString = true;
                    }


                    if (isString)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_PRIMITIVE_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? true : false);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? Boolean.TrueString : Boolean.FalseString);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isLong)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isFloat)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }

                        tuple.Add(columnName, columnValue);
                    }
                    else if (isBlob)
                    {
                        tuple.Add(columnName, columnValue);
                    }
                }

                tuples.Add((Dictionary <String, Object>)tuple);
            }


            return(tuples.GetEnumerator());
            #endif
        }
예제 #13
0
        object ReadCol(Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clrType)
        {
            if (type == SQLite3.ColType.Null)
            {
                return(null);
            }
            else
            {
                var clrTypeInfo = clrType.GetTypeInfo();
                if (clrTypeInfo.IsGenericType && clrTypeInfo.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    clrType     = clrTypeInfo.GenericTypeArguments [0];
                    clrTypeInfo = clrType.GetTypeInfo();
                }

                if (clrType == typeof(String))
                {
                    return(SQLite3.ColumnString(stmt, index));
                }
                else if (clrType == typeof(Int32))
                {
                    return((int)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(Boolean))
                {
                    return(SQLite3.ColumnInt(stmt, index) == 1);
                }
                else if (clrType == typeof(double))
                {
                    return(SQLite3.ColumnDouble(stmt, index));
                }
                else if (clrType == typeof(float))
                {
                    return((float)SQLite3.ColumnDouble(stmt, index));
                }
                else if (clrType == typeof(TimeSpan))
                {
                    if (_conn.StoreTimeSpanAsTicks)
                    {
                        return(new TimeSpan(SQLite3.ColumnInt64(stmt, index)));
                    }
                    else
                    {
                        var      text = SQLite3.ColumnString(stmt, index);
                        TimeSpan resultTime;
                        if (!TimeSpan.TryParseExact(text, "c", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.TimeSpanStyles.None, out resultTime))
                        {
                            resultTime = TimeSpan.Parse(text);
                        }
                        return(resultTime);
                    }
                }
                else if (clrType == typeof(DateTime))
                {
                    if (_conn.StoreDateTimeAsTicks)
                    {
                        return(new DateTime(SQLite3.ColumnInt64(stmt, index)));
                    }
                    else
                    {
                        var      text = SQLite3.ColumnString(stmt, index);
                        DateTime resultDate;
                        if (!DateTime.TryParseExact(text, _conn.DateTimeStringFormat, System.Globalization.CultureInfo.InvariantCulture, _conn.DateTimeStyle, out resultDate))
                        {
                            resultDate = DateTime.Parse(text);
                        }
                        return(resultDate);
                    }
                }
                else if (clrType == typeof(DateTimeOffset))
                {
                    return(new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero));
                }
                else if (clrTypeInfo.IsEnum)
                {
                    if (type == SQLite3.ColType.Text)
                    {
                        var value = SQLite3.ColumnString(stmt, index);
                        return(Enum.Parse(clrType, value.ToString(), true));
                    }
                    else
                    {
                        return(SQLite3.ColumnInt(stmt, index));
                    }
                }
                else if (clrType == typeof(Int64))
                {
                    return(SQLite3.ColumnInt64(stmt, index));
                }
                else if (clrType == typeof(UInt32))
                {
                    return((uint)SQLite3.ColumnInt64(stmt, index));
                }
                else if (clrType == typeof(decimal))
                {
                    return((decimal)SQLite3.ColumnDouble(stmt, index));
                }
                else if (clrType == typeof(Byte))
                {
                    return((byte)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(UInt16))
                {
                    return((ushort)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(Int16))
                {
                    return((short)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(sbyte))
                {
                    return((sbyte)SQLite3.ColumnInt(stmt, index));
                }
                else if (clrType == typeof(byte []))
                {
                    return(SQLite3.ColumnByteArray(stmt, index));
                }
                else if (clrType == typeof(Guid))
                {
                    var text = SQLite3.ColumnString(stmt, index);
                    return(new Guid(text));
                }
                else if (clrType == typeof(Uri))
                {
                    var text = SQLite3.ColumnString(stmt, index);
                    return(new Uri(text));
                }
                else if (clrType == typeof(StringBuilder))
                {
                    var text = SQLite3.ColumnString(stmt, index);
                    return(new StringBuilder(text));
                }
                else if (clrType == typeof(UriBuilder))
                {
                    var text = SQLite3.ColumnString(stmt, index);
                    return(new UriBuilder(text));
                }
                else
                {
                    throw new NotSupportedException("Don't know how to read " + clrType);
                }
            }
        }
예제 #14
0
        public object ReadCol(int index, SQLite3.ColType type, Type clrType)
        {
            var stmt = _stmt;

            if (type == SQLite3.ColType.Null)
            {
                return(null);
            }

            if (clrType == typeof(String))
            {
                return(SQLite3.ColumnString(stmt, index));
            }

            if (clrType == typeof(Int32))
            {
                return(SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(Boolean))
            {
                return(SQLite3.ColumnInt(stmt, index) == 1);
            }

            if (clrType == typeof(double))
            {
                return(SQLite3.ColumnDouble(stmt, index));
            }

            if (clrType == typeof(float))
            {
                return((float)SQLite3.ColumnDouble(stmt, index));
            }

            /*
             * else if (clrType == typeof(TimeSpan))
             * {
             *  return new TimeSpan(SQLite3.ColumnInt64(stmt, index));
             * }
             * else if (clrType == typeof(DateTime))
             * {
             *  if (_conn.StoreDateTimeAsTicks)
             *  {
             *      return new DateTime(SQLite3.ColumnInt64(stmt, index));
             *  }
             *  else
             *  {
             *      var text = SQLite3.ColumnString(stmt, index);
             *      return DateTime.Parse(text);
             *  }
             * }
             * else if (clrType == typeof(DateTimeOffset))
             * {
             *  return new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero);
             * }
             */
            //for ILRuntime
            if (clrType.IsEnum || clrType.IsClass)
            {
                return(SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(Int64))
            {
                return(SQLite3.ColumnInt64(stmt, index));
            }

            if (clrType == typeof(UInt32))
            {
                return((uint)SQLite3.ColumnInt64(stmt, index));
            }

            if (clrType == typeof(decimal))
            {
                return((decimal)SQLite3.ColumnDouble(stmt, index));
            }

            if (clrType == typeof(Byte))
            {
                return((byte)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(UInt16))
            {
                return((ushort)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(Int16))
            {
                return((short)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(sbyte))
            {
                return((sbyte)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(byte[]))
            {
                return(SQLite3.ColumnByteArray(stmt, index));
            }

            /*
             * else if (clrType == typeof(Guid))
             * {
             *  var text = SQLite3.ColumnString(stmt, index);
             *  return new Guid(text);
             * }
             */
            throw new NotSupportedException("Don't know how to read " + clrType);
        }
예제 #15
0
        private object ReadCol(Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clrType)
        {
            if (type == SQLite3.ColType.Null)
            {
                return(null);
            }

            if (clrType == typeof(string))
            {
                return(SQLite3.ColumnString(stmt, index));
            }

            if (clrType == typeof(int))
            {
                return(SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(bool))
            {
                return(SQLite3.ColumnInt(stmt, index) == 1);
            }

            if (clrType == typeof(double))
            {
                return(SQLite3.ColumnDouble(stmt, index));
            }

            if (clrType == typeof(float))
            {
                return((float)SQLite3.ColumnDouble(stmt, index));
            }

            if (clrType == typeof(TimeSpan))
            {
                return(new TimeSpan(SQLite3.ColumnInt64(stmt, index)));
            }

            if (clrType == typeof(DateTime))
            {
                if (_conn.StoreDateTimeAsTicks)
                {
                    return(new DateTime(SQLite3.ColumnInt64(stmt, index)));
                }

                string text = SQLite3.ColumnString(stmt, index);
                return(DateTime.Parse(text));
            }

            if (clrType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero));
            }

            if (clrType.IsEnum)
            {
                return(SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(long))
            {
                return(SQLite3.ColumnInt64(stmt, index));
            }

            if (clrType == typeof(uint))
            {
                return((uint)SQLite3.ColumnInt64(stmt, index));
            }

            if (clrType == typeof(decimal))
            {
                return((decimal)SQLite3.ColumnDouble(stmt, index));
            }

            if (clrType == typeof(byte))
            {
                return((byte)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(ushort))
            {
                return((ushort)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(short))
            {
                return((short)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(sbyte))
            {
                return((sbyte)SQLite3.ColumnInt(stmt, index));
            }

            if (clrType == typeof(byte[]))
            {
                return(SQLite3.ColumnByteArray(stmt, index));
            }

            if (clrType == typeof(Guid))
            {
                string text = SQLite3.ColumnString(stmt, index);
                return(new Guid(text));
            }

            throw new NotSupportedException("Don't know how to read " + clrType);
        }
 private static object ReadCol(SQLiteConnection connection, sqlite3_stmt stmt, int index, SQLite3.ColType type, Type clrType)
 {
     if (type == SQLite3.ColType.Null)
     {
         return(null);
     }
     else
     {
         if (clrType == typeof(String))
         {
             return(SQLite3.ColumnString(stmt, index));
         }
         else if (clrType == typeof(Int32))
         {
             return((int)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(Boolean))
         {
             return(SQLite3.ColumnInt(stmt, index) == 1);
         }
         else if (clrType == typeof(double))
         {
             return(SQLite3.ColumnDouble(stmt, index));
         }
         else if (clrType == typeof(float))
         {
             return((float)SQLite3.ColumnDouble(stmt, index));
         }
         else if (clrType == typeof(TimeSpan))
         {
             return(new TimeSpan(SQLite3.ColumnInt64(stmt, index)));
         }
         else if (clrType == typeof(DateTime))
         {
             if (connection.StoreDateTimeAsTicks)
             {
                 return(new DateTime(SQLite3.ColumnInt64(stmt, index)));
             }
             else
             {
                 string   text = SQLite3.ColumnString(stmt, index);
                 DateTime resultDate;
                 if (!DateTime.TryParseExact(text, DateTimeExactStoreFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out resultDate))
                 {
                     resultDate = DateTime.Parse(text);
                 }
                 return(resultDate);
             }
         }
         //                else if (clrType == typeof(DateTimeOffset))
         //                {
         //                    return new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero);
         //#if !USE_NEW_REFLECTION_API
         //                }
         //                else if (clrType.IsEnum)
         //                {
         //#else
         //				} else if (clrType.GetTypeInfo().IsEnum) {
         //#endif
         //                    if (type == SQLite3.ColType.Text)
         //                    {
         //                        var value = SQLite3.ColumnString(stmt, index);
         //                        return Enum.Parse(clrType, value.ToString(), true);
         //                    }
         //                    else
         //                        return SQLite3.ColumnInt(stmt, index);
         //                }
         else if (clrType == typeof(Int64))
         {
             return(SQLite3.ColumnInt64(stmt, index));
         }
         else if (clrType == typeof(UInt32))
         {
             return((uint)SQLite3.ColumnInt64(stmt, index));
         }
         else if (clrType == typeof(decimal))
         {
             return((decimal)SQLite3.ColumnDouble(stmt, index));
         }
         else if (clrType == typeof(Byte))
         {
             return((byte)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(UInt16))
         {
             return((ushort)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(Int16))
         {
             return((short)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(sbyte))
         {
             return((sbyte)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(byte[]))
         {
             return(SQLite3.ColumnByteArray(stmt, index));
         }
         else if (clrType == typeof(Guid))
         {
             string text = SQLite3.ColumnString(stmt, index);
             return(new Guid(text));
         }
         else
         {
             throw new NotSupportedException("Don't know how to read " + clrType);
         }
     }
 }
예제 #17
0
 object ReadCol(IntPtr stmt, int index, SQLite3.ColType type, Type clrType)
 {
     if (type == SQLite3.ColType.Null)
     {
         return(null);
     }
     else
     {
         if (clrType == typeof(String))
         {
             return(SQLite3.ColumnString(stmt, index));
         }
         else if (clrType == typeof(Int32))
         {
             return((int)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(Boolean))
         {
             return(SQLite3.ColumnInt(stmt, index) == 1);
         }
         else if (clrType == typeof(double))
         {
             return(SQLite3.ColumnDouble(stmt, index));
         }
         else if (clrType == typeof(float))
         {
             return((float)SQLite3.ColumnDouble(stmt, index));
         }
         else if (clrType == typeof(DateTime))
         {
             var text = SQLite3.ColumnString(stmt, index);
             return(DateTime.Parse(text));
         }
         else if (clrType.IsEnum)
         {
             return(SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(Int64))
         {
             return(SQLite3.ColumnInt64(stmt, index));
         }
         else if (clrType == typeof(UInt32))
         {
             return((uint)SQLite3.ColumnInt64(stmt, index));
         }
         else if (clrType == typeof(decimal))
         {
             return((decimal)SQLite3.ColumnDouble(stmt, index));
         }
         else if (clrType == typeof(Byte))
         {
             return((byte)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(UInt16))
         {
             return((ushort)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(Int16))
         {
             return((short)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(sbyte))
         {
             return((sbyte)SQLite3.ColumnInt(stmt, index));
         }
         else if (clrType == typeof(byte[]))
         {
             return(SQLite3.ColumnByteArray(stmt, index));
         }
         else
         {
             throw new NotSupportedException("Don't know how to read " + clrType);
         }
     }
 }