Ejemplo n.º 1
0
        public static int BindText(Sqlite3Statement stmt, int index, string val, int n, IntPtr free)
        {
#if USE_WP8_NATIVE_SQLITE
            return(Sqlite3.sqlite3_bind_text(stmt, index, val, n));
#else
            return(Sqlite3.sqlite3_bind_text(stmt, index, val, n, null));
#endif
        }
Ejemplo n.º 2
0
        public static int BindBlob(Sqlite3Statement stmt, int index, byte[] val, int n, IntPtr free)
        {
#if USE_WP8_NATIVE_SQLITE
            return(Sqlite3.sqlite3_bind_blob(stmt, index, val, n));
#else
            return(Sqlite3.sqlite3_bind_blob(stmt, index, val, n, null));
#endif
        }
        public int ExecuteNonQuery(object[] source)
        {
            if (Connection.Trace)
            {
                Debug.WriteLine("Executing: " + CommandText);
            }

            var r = SQLite3.Result.OK;

            if (!Initialized)
            {
                Statement   = Prepare();
                Initialized = true;
            }

            //bind the values.
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    // TODO: Handle null case
                    var sqlType = SQLite3.GetSQLiteType(source[i].GetType());
                    sqlType.Bind(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                }
            }
            r = SQLite3.Step(Statement);

            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(Connection.Handle);
                SQLite3.Reset(Statement);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(Connection.Handle);
                SQLite3.Reset(Statement);
                throw SQLiteException.New(r, msg);
            }
            else if (r == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
            {
                SQLite3.Reset(Statement);
                throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(Connection.Handle));
            }
            else
            {
                SQLite3.Reset(Statement);
                throw SQLiteException.New(r, r.ToString());
            }
        }
Ejemplo n.º 4
0
 private void Dispose(bool disposing)
 {
     if (Statement != NullStatement)
     {
         try
         {
             SQLite3.Finalize(Statement);
         }
         finally
         {
             Statement  = NullStatement;
             Connection = null;
         }
     }
 }
Ejemplo n.º 5
0
        public static bool TryBind(Type t, Sqlite3Statement stmt, int index, object value)
        {
            if (Binders == null)
            {
                Initialize();
                RegisterDefaultTypes();
            }

            if (Binders.ContainsKey(t))
            {
                Binders[t](stmt, index, value);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 6
0
        public static Sqlite3Statement Prepare2(Sqlite3DatabaseHandle db, string query)
        {
            Sqlite3Statement stmt = default(Sqlite3Statement);

#if USE_WP8_NATIVE_SQLITE
            var r = Sqlite3.sqlite3_prepare_v2(db, query, out stmt);
#else
            stmt = new Sqlite3Statement();
            var r = Sqlite3.sqlite3_prepare_v2(db, query, -1, ref stmt, 0);
#endif
            if (r != 0)
            {
                throw SQLiteException.New((Result)r, GetErrmsg(db));
            }
            return(stmt);
        }
Ejemplo n.º 7
0
        public static bool TryRead(Type t, Sqlite3Statement stmt, int index, out object result)
        {
            if (Readers == null)
            {
                Initialize();
                RegisterDefaultTypes();
            }

            if (Readers.ContainsKey(t))
            {
                result = Readers[t](stmt, index);
                return(true);
            }

            result = null;
            return(false);
        }
Ejemplo n.º 8
0
        public static IDbStatement Prepare2(IDbHandle db, string query)
        {
            var internalDbHandle = (Sqlite3DatabaseInternal)db;

#if USE_WP8_NATIVE_SQLITE
            Sqlite.Statement stmt;
            var r = Sqlite3.sqlite3_prepare_v2(db, query, out stmt);
#else
            var stmt = new Sqlite3.Vdbe();
            var r    = Sqlite3.sqlite3_prepare_v2(internalDbHandle.Handle, query, -1, ref stmt, 0);
#endif
            if (r != 0)
            {
                throw SQLiteException.New((Result)r, GetErrmsg(db));
            }
            return(new Sqlite3StatementInternal(stmt));
        }
Ejemplo n.º 9
0
        void BindAll(Sqlite3Statement stmt)
        {
            int nextIdx = 1;

            foreach (var b in _bindings)
            {
                if (b.Name != null)
                {
                    b.Index = SQLite3.BindParameterIndex(stmt, b.Name);
                }
                else
                {
                    b.Index = nextIdx++;
                }

                BindParameter(stmt, b.Index, b.Value, _conn.StoreDateTimeAsTicks);
            }
        }
Ejemplo n.º 10
0
 public static string ColumnString(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_text(stmt, index));
 }
Ejemplo n.º 11
0
 public static int ColumnBytes(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_bytes(stmt, index));
 }
Ejemplo n.º 12
0
 public static byte[] ColumnBlob(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_blob(stmt, index));
 }
Ejemplo n.º 13
0
 public static double ColumnDouble(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_double(stmt, index));
 }
Ejemplo n.º 14
0
 public static long ColumnInt64(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_int64(stmt, index));
 }
Ejemplo n.º 15
0
 public static int ColumnInt(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_int(stmt, index));
 }
Ejemplo n.º 16
0
 public static int BindParameterIndex(Sqlite3Statement stmt, string name)
 {
     return(Sqlite3.sqlite3_bind_parameter_index(stmt, name));
 }
Ejemplo n.º 17
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);
                }
            }
        }
Ejemplo n.º 18
0
		public static IDbStatement Prepare2(IDbHandle db, string query)
        {
            var internalDbHandle = (Sqlite3DatabaseInternal)db;
#if USE_WP8_NATIVE_SQLITE
			Sqlite.Statement stmt;
			var r = Sqlite3.sqlite3_prepare_v2(db, query, out stmt);
#else
            var stmt = new Sqlite3.Vdbe();
            var r = Sqlite3.sqlite3_prepare_v2(internalDbHandle.Handle, query, -1, ref stmt, 0);
#endif
			if (r != 0)
			{
				throw SQLiteException.New((Result)r, GetErrmsg(db));
			}
		    return new Sqlite3StatementInternal(stmt);
		}
Ejemplo n.º 19
0
 public static Result Reset(Sqlite3Statement stmt)
 {
     return((Result)Sqlite3.sqlite3_reset(stmt));
 }
Ejemplo n.º 20
0
 public static int BindDouble(Sqlite3Statement stmt, int index, double val)
 {
     return(Sqlite3.sqlite3_bind_double(stmt, index, val));
 }
Ejemplo n.º 21
0
 public static int BindInt64(Sqlite3Statement stmt, int index, long val)
 {
     return(Sqlite3.sqlite3_bind_int64(stmt, index, val));
 }
Ejemplo n.º 22
0
 public static int BindNull(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_bind_null(stmt, index));
 }
Ejemplo n.º 23
0
 public static byte[] ColumnByteArray(Sqlite3Statement stmt, int index)
 {
     return(ColumnBlob(stmt, index));
 }
Ejemplo n.º 24
0
 public static Result Step(Sqlite3Statement stmt)
 {
     return((Result)Sqlite3.sqlite3_step(stmt));
 }
Ejemplo n.º 25
0
 void Finalize(Sqlite3Statement stmt)
 {
     SQLite3.Finalize(stmt);
 }
Ejemplo n.º 26
0
 public static Result Finalize(Sqlite3Statement stmt)
 {
     return((Result)Sqlite3.sqlite3_finalize(stmt));
 }
Ejemplo n.º 27
0
        internal static void BindParameter(Sqlite3Statement stmt, int index, object value, bool storeDateTimeAsTicks)
        {
            if (value == null)
            {
                SQLite3.BindNull(stmt, index);
            }
            else
            {
                if (value is Int32)
                {
                    SQLite3.BindInt(stmt, index, (int)value);
                }
                else if (value is String)
                {
                    SQLite3.BindText(stmt, index, (string)value, -1, NegativePointer);
                }
                else if (value is Byte || value is UInt16 || value is SByte || value is Int16)
                {
                    SQLite3.BindInt(stmt, index, Convert.ToInt32(value));
                }
                else if (value is Boolean)
                {
                    SQLite3.BindInt(stmt, index, (bool)value ? 1 : 0);
                }
                else if (value is UInt32 || value is Int64)
                {
                    SQLite3.BindInt64(stmt, index, Convert.ToInt64(value));
                }
                else if (value is Single || value is Double || value is Decimal)
                {
                    SQLite3.BindDouble(stmt, index, Convert.ToDouble(value));
                }
                else if (value is TimeSpan)
                {
                    SQLite3.BindInt64(stmt, index, ((TimeSpan)value).Ticks);
                }
                else if (value is DateTime)
                {
                    if (storeDateTimeAsTicks)
                    {
                        SQLite3.BindInt64(stmt, index, ((DateTime)value).Ticks);
                    }
                    else
                    {
                        SQLite3.BindText(stmt, index, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"), -1, NegativePointer);
                    }
                }
                else if (value is DateTimeOffset)
                {
                    SQLite3.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks);
#if !NETFX_CORE
                }
                else if (value.GetType().IsEnum)
                {
#else
                }
                else if (value.GetType().GetTypeInfo().IsEnum)
                {
#endif
                    SQLite3.BindInt(stmt, index, Convert.ToInt32(value));
                }
                else if (value is byte[])
                {
                    SQLite3.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
                }
                else if (value is Guid)
                {
                    SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, NegativePointer);
                }
                else
                {
                    throw new NotSupportedException("Cannot store type: " + value.GetType());
                }
            }
        }
Ejemplo n.º 28
0
 public static string ColumnName16(Sqlite3Statement stmt, int index)
 {
     return(Sqlite3.sqlite3_column_name(stmt, index));
 }
Ejemplo n.º 29
0
 public static int ColumnCount(Sqlite3Statement stmt)
 {
     return(Sqlite3.sqlite3_column_count(stmt));
 }
Ejemplo n.º 30
0
 public static ColType ColumnType(Sqlite3Statement stmt, int index)
 {
     return((ColType)Sqlite3.sqlite3_column_type(stmt, index));
 }