예제 #1
0
        internal static object GetAs(object o, Type type)
        {
            if (o != null && o.GetType() == type)
            {
                return(o);
            }

            if (o == null)
            {
                o = null;
            }
            else if (type == typeof(int))
            {
                o = (int)(long)o;
            }
            else if (type == typeof(uint))
            {
                o = (uint)(long)o;
            }
            else if (type == typeof(ulong))
            {
                o = (ulong)(long)o;
            }
            else if (type == typeof(float))
            {
                o = (float)(double)o;
            }

            if (o != null && o.GetType() == type)
            {
                return(o);
            }

            return(SqliteUtils.FromDbFormat(type, o));
        }
예제 #2
0
        public void SetValue(object target, IDataReader reader, int column)
        {
            // FIXME should we insist on nullable types?
            object value = reader.IsDBNull(column) ? null : reader.GetValue(column);

            SetValue(target, SqliteUtils.FromDbFormat(type, value));
        }
예제 #3
0
 public T Query <T> ()
 {
     Reset();
     using (reader) {
         return(reader.Read() ? reader.Get <T> (0) : (T)SqliteUtils.FromDbFormat <T> (null));
     }
 }
예제 #4
0
        public T Query <T> (HyenaSqliteCommand command, params object [] param_values)
        {
            command.CommandType = HyenaCommandType.Scalar;
            QueueCommand(command, param_values);
            object result = command.WaitForResult(this);

            return((T)SqliteUtils.FromDbFormat(typeof(T), result));
        }
예제 #5
0
 public T Get <T> (int i)
 {
     if (!read)
     {
         Read();
     }
     return((T)SqliteUtils.FromDbFormat(typeof(T), reader[i]));
 }
예제 #6
0
        public object GetValue(object target)
        {
            object result = field_info != null
                ? field_info.GetValue(target)
                : property_info.GetValue(target, null);

            return(SqliteUtils.ToDbFormat(type, result));
        }
예제 #7
0
 void CheckProperty(Type type, DbColumn column)
 {
     if (!Connection.ColumnExists(TableName, column.Name))
     {
         AddColumnToTable(SqliteUtils.BuildColumnSchema(
                              SqliteUtils.GetType(type),
                              column.Name, column.DefaultValue, column.Constraints));
     }
 }
예제 #8
0
        public void SetProperty <U> (T item, U value, DbColumn column)
        {
            CheckProperty(typeof(U), column);

            Connection.Execute(string.Format(
                                   "UPDATE {0} SET {1}='{2}' WHERE {3}={4}",
                                   TableName, column.Name,
                                   SqliteUtils.ToDbFormat(typeof(U), value),
                                   key.Name, key.GetValue(item)));
        }
예제 #9
0
 private AbstractDatabaseColumn(AbstractDatabaseColumnAttribute attribute, MemberInfo member_info, Type type)
 {
     try {
         column_type = SqliteUtils.GetType(type);
     } catch (Exception e) {
         throw new Exception(string.Format(
                                 "{0}.{1}: {2}", member_info.DeclaringType, member_info.Name, e.Message));
     }
     this.name = attribute.ColumnName ?? member_info.Name;
     this.type = type;
 }
예제 #10
0
        public IEnumerable <T> QueryEnumerable <T> (HyenaSqliteCommand command, params object [] param_values)
        {
            Type type = typeof(T);

            using (IDataReader reader = Query(command, param_values)) {
                while (reader.Read())
                {
                    yield return((T)SqliteUtils.FromDbFormat(type, reader[0]));
                }
            }
        }
예제 #11
0
 public void ClearProperty <U> (DbColumn column)
 {
     if (!Connection.ColumnExists(TableName, column.Name))
     {
         AddColumnToTable(SqliteUtils.BuildColumnSchema(
                              SqliteUtils.GetType(typeof(U)),
                              column.Name, column.DefaultValue, column.Constraints));
     }
     else
     {
         Connection.Execute(string.Format(
                                "UPDATE {0} SET {1}='{2}'",
                                TableName, column.Name, column.DefaultValue));
     }
 }
예제 #12
0
 /// <summary>
 /// Takes the return value from Invoke() and Final() and figures out how to return it to Sqlite's context.
 /// </summary>
 /// <param name="context">The context the return value applies to</param>
 /// <param name="returnValue">The parameter to return to Sqlite</param>
 void SetReturnValue(IntPtr context, object r)
 {
     if (r is Exception)
     {
         Native.sqlite3_result_error16(context, ((Exception)r).Message, -1);
     }
     else
     {
         var o = SqliteUtils.ToDbFormat(r);
         if (o == null)
         {
             Native.sqlite3_result_null(context);
         }
         else if (r is int || r is uint)
         {
             Native.sqlite3_result_int(context, (int)r);
         }
         else if (r is long || r is ulong)
         {
             Native.sqlite3_result_int64(context, (long)r);
         }
         else if (r is double || r is float)
         {
             Native.sqlite3_result_double(context, (double)r);
         }
         else if (r is string)
         {
             string str = (string)r;
             // -1 for the last arg is the TRANSIENT destructor type so that sqlite will make its own copy of the string
             Native.sqlite3_result_text16(context, str, str.Length * 2, (IntPtr)(-1));
         }
         else if (r is byte[])
         {
             var bytes = (byte[])r;
             Native.sqlite3_result_blob(context, bytes, bytes.Length, (IntPtr)(-1));
         }
     }
 }
예제 #13
0
        public Statement Bind(params object [] vals)
        {
            Reset();

            if (vals == null && ParameterCount == 1)
            {
                vals = null_val;
            }

            if (vals == null || vals.Length != ParameterCount || ParameterCount == 0)
            {
                throw new ArgumentException("vals", string.Format("Statement has {0} parameters", ParameterCount));
            }

            for (int i = 1; i <= vals.Length; i++)
            {
                object o = SqliteUtils.ToDbFormat(vals[i - 1]);

                int code;
                if (o == null)
                {
                    code = Native.sqlite3_bind_null(Ptr, i);
                }
                else if (o is double)
                {
                    code = Native.sqlite3_bind_double(Ptr, i, (double)o);
                }
                else if (o is float)
                {
                    code = Native.sqlite3_bind_double(Ptr, i, (double)(float)o);
                }
                else if (o is int)
                {
                    code = Native.sqlite3_bind_int(Ptr, i, (int)o);
                }
                else if (o is uint)
                {
                    code = Native.sqlite3_bind_int(Ptr, i, (int)(uint)o);
                }
                else if (o is long)
                {
                    code = Native.sqlite3_bind_int64(Ptr, i, (long)o);
                }
                else if (o is ulong)
                {
                    code = Native.sqlite3_bind_int64(Ptr, i, (long)(ulong)o);
                }
                else if (o is byte[])
                {
                    byte[] bytes = o as byte[];
                    code = Native.sqlite3_bind_blob(Ptr, i, bytes, bytes.Length, (IntPtr)(-1));
                }
                else
                {
                    // C# strings are UTF-16, so 2 bytes per char
                    // -1 for the last arg is the TRANSIENT destructor type so that sqlite will make its own copy of the string
                    string str = (o as string) ?? o.ToString();
                    code = Native.sqlite3_bind_text16(Ptr, i, str, str.Length * 2, (IntPtr)(-1));
                }

                CheckError(code);
            }

            bound = true;
            return(this);
        }
예제 #14
0
        public object GetValue(object target)
        {
            object result = GetRawValue(target);

            return(SqliteUtils.ToDbFormat(type, result));
        }