예제 #1
0
        private bool WriteItem(NpgsqlNativeTypeInfo TypeInfo, object item, StringBuilder sb, Boolean ForExtendedQuery)
        {
            //item could be:
            //an Ienumerable - in which case we call WriteEnumeration
            //an element - in which case we call the NpgsqlNativeTypeInfo for that type to serialise it.
            //an array - in which case we call WriteArray,

            // Even an string being an IEnumerable, it shouldn't be processed. It will be processed on the last else.
            // See http://pgfoundry.org/tracker/?func=detail&atid=592&aid=1010514&group_id=1000140 for more info.

            if (item == null || NpgsqlTypesHelper.DefinedType(item))
            {
                sb.Append(_elementConverter.ConvertToBackend(item, ForExtendedQuery));
                return(true);
            }
            else if (item is Array)
            {
                return(WriteArray(TypeInfo, item as Array, sb, ForExtendedQuery));
            }
            else if (item is IEnumerable)
            {
                return(WriteEnumeration(TypeInfo, item as IEnumerable, sb, ForExtendedQuery));
            }
            else
            {//This shouldn't really be reachable.
                sb.Append(_elementConverter.ConvertToBackend(item, ForExtendedQuery));
                return(true);
            }
        }
예제 #2
0
        /// <summary>
        ///	Check if Npgsql support .NET type conversion to Postgres type
        /// </summary>
        public static bool CanConvert(Type type)
        {
            NpgsqlNativeTypeInfo info;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(NpgsqlTypesHelper.TryGetNativeTypeInfo(type.GetGenericArguments()[0], out info));
            }
            return(NpgsqlTypesHelper.TryGetNativeTypeInfo(type, out info));
        }
예제 #3
0
        /// <summary>
        ///	Convert Postgres type name for .NET type
        /// </summary>
        public static string GetTypeName(Type type)
        {
            NpgsqlNativeTypeInfo info;
            bool canConvert;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                canConvert = NpgsqlTypesHelper.TryGetNativeTypeInfo(type.GetGenericArguments()[0], out info);
            }
            else
            {
                canConvert = NpgsqlTypesHelper.TryGetNativeTypeInfo(type, out info);
            }
            if (!canConvert)
            {
                throw new NpgsqlException("Can't convert " + type.FullName + " to native value");
            }
            return(info.Name);
        }
예제 #4
0
        private bool WriteItemText(NpgsqlNativeTypeInfo TypeInfo, object item, MemoryStream array, Boolean forExtendedQuery, NativeToBackendTypeConverterOptions options)
        {
            //item could be:
            //an Ienumerable - in which case we call WriteEnumeration
            //an element - in which case we call the NpgsqlNativeTypeInfo for that type to serialise it.
            //an array - in which case we call WriteArray,

            // Even an string being an IEnumerable, it shouldn't be processed. It will be processed on the last else.
            // See http://pgfoundry.org/tracker/?func=detail&atid=592&aid=1010514&group_id=1000140 for more info.

            if (item == null || NpgsqlTypesHelper.DefinedType(item))
            {
                byte[] element;

                element = _elementConverter.ConvertToBackend(item, forExtendedQuery, options, true);

                array.Write(element, 0, element.Length);

                return(true);
            }
            else if (item is Array)
            {
                return(WriteArrayText(TypeInfo, item as Array, array, forExtendedQuery, options));
            }
            else if (item is IEnumerable)
            {
                return(WriteEnumeration(TypeInfo, item as IEnumerable, array, forExtendedQuery, options));
            }
            else
            {//This shouldn't really be reachable.
                byte[] element;

                element = _elementConverter.ConvertToBackend(item, forExtendedQuery, options, true);

                array.Write(element, 0, element.Length);

                return(true);
            }
        }
예제 #5
0
        /// <summary>
        ///	Convert .NET type to Postgres string representation
        /// </summary>
        public static string Convert(Type type, object value)
        {
            NpgsqlNativeTypeInfo info;
            bool canConvert;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                canConvert = NpgsqlTypesHelper.TryGetNativeTypeInfo(type.GetGenericArguments()[0], out info);
            }
            else
            {
                if (type == typeof(string) && value != null)
                {
                    return("'" + (value as string).Replace("'", "''") + "'");
                }
                canConvert = NpgsqlTypesHelper.TryGetNativeTypeInfo(type, out info);
            }
            if (!canConvert)
            {
                throw new NpgsqlException("Can't convert " + type.FullName + " to native value");
            }
            return(info.ConvertToBackend(value, false));
        }