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; } }
/// <summary> /// Box. /// </summary> internal static String ToBox(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { if (NativeData is NpgsqlBox) { NpgsqlBox box = (NpgsqlBox)NativeData; return (String.Format(CultureInfo.InvariantCulture, "({0},{1}),({2},{3})", box.LowerLeft.X, box.LowerLeft.Y, box.UpperRight.X, box.UpperRight.Y)); } else { throw new InvalidCastException("Unable to cast data to Rectangle type"); } }
/// <summary> /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, the size, /// and the source column name. /// </summary> /// <param name="parameterName">The name of the parameter to map.</param> /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param> /// <param name="size">The length of the parameter.</param> /// <param name="sourceColumn">The name of the source column.</param> public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn) { resman = new System.Resources.ResourceManager(this.GetType()); NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, parameterType, size, source_column); this.ParameterName = parameterName; type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType); if (type_info == null) { throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType)); } this.size = size; source_column = sourceColumn; }
internal static string ToTimeTZ(NpgsqlNativeTypeInfo typeInfo, object nativeData, Boolean ForExtendedQuery) { if (nativeData is DateTime) { return(BasicNativeToBackendTypeConverter.ToTime(typeInfo, nativeData, ForExtendedQuery)); } NpgsqlTimeTZ time; if (nativeData is TimeSpan) { time = (NpgsqlTimeTZ)(TimeSpan)nativeData; } else { time = (NpgsqlTimeTZ)nativeData; } return(time.ToString()); }
/// <summary> /// Open path. /// </summary> internal static String ToPath(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { StringBuilder B = null; try { B = new StringBuilder(); foreach (NpgsqlPoint P in ((NpgsqlPath)NativeData)) { B.AppendFormat(CultureInfo.InvariantCulture, "{0}({1},{2})", (B.Length > 0 ? "," : ""), P.X, P.Y); } return(String.Format("[{0}]", B)); } finally { B = null; } }
/// <summary> /// Binary data. /// </summary> internal static String ToBinary(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { Byte[] byteArray = (Byte[])NativeData; StringBuilder res = new StringBuilder(byteArray.Length * 5); foreach (byte b in byteArray) { if (b >= 0x20 && b < 0x7F && b != 0x27 && b != 0x5C) { res.Append((char)b); } else { res.Append("\\\\") .Append((char)('0' + (7 & (b >> 6)))) .Append((char)('0' + (7 & (b >> 3)))) .Append((char)('0' + (7 & b))); } } return(res.ToString()); }
/// <summary> /// Serialise the enumeration or array. /// </summary> public string FromArray(NpgsqlNativeTypeInfo TypeInfo, object NativeData, Boolean ForExtendedQuery) { if (ForExtendedQuery) { StringBuilder sb = new StringBuilder("{"); WriteItem(TypeInfo, NativeData, sb, ForExtendedQuery); sb.Append("}"); return(sb.ToString()); } else { //just prepend "array" and then pass to WriteItem. StringBuilder sb = new StringBuilder("array"); if (WriteItem(TypeInfo, NativeData, sb, ForExtendedQuery)) { return(sb.ToString()); } else { return("'{}'"); } } }
/// <summary> /// Serialise the enumeration or array. /// </summary> public string FromArray(NpgsqlNativeTypeInfo TypeInfo, object NativeData, Boolean ForExtendedQuery) { if (ForExtendedQuery) { StringBuilder sb = new StringBuilder("{"); WriteItem(TypeInfo, NativeData, sb, ForExtendedQuery); sb.Append("}"); return sb.ToString(); } else { //just prepend "array" and then pass to WriteItem. StringBuilder sb = new StringBuilder("array"); if (WriteItem(TypeInfo, NativeData, sb, ForExtendedQuery)) { return sb.ToString(); } else { return "'{}'"; } } }
/// <summary> /// Convert to a postgresql timestamp. /// </summary> internal static String ToDateTime(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { if (!(NativeData is DateTime)) { return(ExtendedNativeToBackendTypeConverter.ToTimeStamp(TypeInfo, NativeData, ForExtendedQuery)); } if (DateTime.MaxValue.Equals(NativeData)) { return("infinity"); } if (DateTime.MinValue.Equals(NativeData)) { return("-infinity"); } var dt = (DateTime)NativeData; //HACK: detect dates if (dt.Date == dt) { return(dt.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo)); } return(dt.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.FFFFFF+00", DateTimeFormatInfo.InvariantInfo)); }
/// <summary> /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>. /// </summary> /// <param m_Name="parameterName">The m_Name of the parameter to map.</param> /// <param m_Name="parameterType">One of the <see cref="NpgsqlTypes.NpgsqlDbType">NpgsqlDbType</see> values.</param> /// <param m_Name="size">The length of the parameter.</param> /// <param m_Name="sourceColumn">The m_Name of the source column.</param> /// <param m_Name="direction">One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see> values.</param> /// <param m_Name="isNullable"><b>true</b> if the value of the field can be null, otherwise <b>false</b>.</param> /// <param m_Name="precision">The total number of digits to the left and right of the decimal point to which /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param> /// <param m_Name="scale">The total number of decimal places to which /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param> /// <param m_Name="sourceVersion">One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.</param> /// <param m_Name="value">An <see cref="System.Object">Object</see> that is the value /// of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param> public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value) { this.ParameterName = parameterName; this.Size = size; this.SourceColumn = sourceColumn; this.Direction = direction; this.IsNullable = isNullable; this.Precision = precision; this.Scale = scale; this.SourceVersion = sourceVersion; this.Value = value; if (this.value == null) { this.value = DBNull.Value; type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); } else { NpgsqlDbType = parameterType; //allow the setter to catch exceptions if necessary. } }
/// <summary> /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> /// class with the parameter m_Name, the <see cref="System.Data.DbType">DbType</see>, the size, /// the source column m_Name, a <see cref="System.Data.ParameterDirection">ParameterDirection</see>, /// the precision of the parameter, the scale of the parameter, a /// <see cref="System.Data.DataRowVersion">DataRowVersion</see> to use, and the /// value of the parameter. /// </summary> /// <param m_Name="parameterName">The m_Name of the parameter to map.</param> /// <param m_Name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param> /// <param m_Name="size">The length of the parameter.</param> /// <param m_Name="sourceColumn">The m_Name of the source column.</param> /// <param m_Name="direction">One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see> values.</param> /// <param m_Name="isNullable"><b>true</b> if the value of the field can be null, otherwise <b>false</b>.</param> /// <param m_Name="precision">The total number of digits to the left and right of the decimal point to which /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param> /// <param m_Name="scale">The total number of decimal places to which /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param> /// <param m_Name="sourceVersion">One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.</param> /// <param m_Name="value">An <see cref="System.Object">Object</see> that is the value /// of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param> public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value) { this.ParameterName = parameterName; this.Size = size; this.SourceColumn = sourceColumn; this.Direction = direction; this.IsNullable = isNullable; this.Precision = precision; this.Scale = scale; this.SourceVersion = sourceVersion; this.Value = value; if (this.value == null) { this.value = DBNull.Value; type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); } else { NpgsqlDbType = parameterType; //allow the setter to catch exceptions if necessary. } }
public override void ResetDbType() { type_info = null; this.Value = Value; }
/// <summary> /// Circle. /// </summary> internal static String ToCircle(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { NpgsqlCircle C = (NpgsqlCircle)NativeData; return(String.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", C.Center.X, C.Center.Y, C.Radius)); }
/// <summary> /// Create an ArrayNativeToBackendTypeConverter with the element converter passed /// </summary> /// <param name="elementConverter">The <see cref="NpgsqlNativeTypeInfo"/> that would be used to serialise the element type.</param> public ArrayNativeToBackendTypeConverter(NpgsqlNativeTypeInfo elementConverter) { _elementConverter = elementConverter; }
/// <summary> /// LSeg. /// </summary> internal static String ToLSeg(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { NpgsqlLSeg S = (NpgsqlLSeg)NativeData; return(String.Format(CultureInfo.InvariantCulture, "{0},{1},{2},{3}", S.Start.X, S.Start.Y, S.End.X, S.End.Y)); }
private bool WriteArray(NpgsqlNativeTypeInfo TypeInfo, Array ar, StringBuilder sb, Boolean ForExtendedQuery) { bool writtenSomething = false; //we need to know the size of each dimension. int c = ar.Rank; List <int> lengths = new List <int>(c); do { lengths.Add(ar.GetLength(--c)); }while (c != 0); //c is now zero. Might as well reuse it! foreach (object item in ar) { // As this prcedure handles both prepared and plain query representations, in order to not keep if's inside the loops // we simply set a placeholder here for both openElement ( '{' or '[' ) and closeElement ( '}', or ']' ) Char openElement = ForExtendedQuery ? '{' : '['; Char closeElement = ForExtendedQuery ? '}' : ']'; //to work out how many [ characters we need we need to work where we are compared to the dimensions. //Say we are at position 24 in a 3 * 4 * 5 array. //We're at the end of a row as 24 % 3 == 0 so write one [ for that. //We're at the end of a square as 24 % (3 * 4) == 24 % (12) == 0 so write one [ for that. //We're not at the end of a cube as 24 % (3 * 4 * 5) == 24 % (30) != 0, so we're finished for that pass. int curlength = 1; foreach (int lengthTest in lengths) { if (c % (curlength *= lengthTest) == 0) { sb.Append(openElement); } else { break; } } //Write whatever the element is. writtenSomething |= WriteItem(TypeInfo, item, sb, ForExtendedQuery); ++c; //up our counter for knowing when to write [ and ] //same logic as above for writing [ this time writing ] curlength = 1; foreach (int lengthTest in lengths) { if (c % (curlength *= lengthTest) == 0) { sb.Append(closeElement); } else { break; } } //comma between each item. sb.Append(','); } if (writtenSomething) { //last comma was one too many. sb.Remove(sb.Length - 1, 1); } return(writtenSomething); }
internal static string ToBasicType <T>(NpgsqlNativeTypeInfo TypeInfo, object NativeData, Boolean ForExtendedQuery) { // This double cast is needed in order to get the enum type handled correctly (IConvertible) // and the decimal separator always as "." regardless of culture (IFormattable) return(((IFormattable)((IConvertible)NativeData).ToType(typeof(T), null)).ToString(null, CultureInfo.InvariantCulture.NumberFormat)); }
private bool WriteEnumeration(NpgsqlNativeTypeInfo TypeInfo, IEnumerable col, StringBuilder sb, Boolean ForExtendedQuery) { // As this prcedure handles both prepared and plain query representations, in order to not keep if's inside the loops // we simply set a placeholder here for both openElement ( '{' or '[' ) and closeElement ( '}', or ']' ) Char openElement = ForExtendedQuery ? '{' : '['; Char closeElement = ForExtendedQuery ? '}' : ']'; bool writtenSomething = false; sb.Append(openElement); //write each item with a comma between them. foreach (object item in col) { writtenSomething |= WriteItem(TypeInfo, item, sb, ForExtendedQuery); sb.Append(','); } if (writtenSomething) { //last comma was one too many. Replace it with the final } sb[sb.Length - 1] = closeElement; } return writtenSomething; }
public override void ResetDbType() { //type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); type_info = null; this.Value = Value; }
/// <summary> /// Convert to a postgresql boolean. /// </summary> internal static String ToBoolean(NpgsqlNativeTypeInfo TypeInfo, Object NativeData, Boolean ForExtendedQuery) { return(((bool)NativeData) ? "TRUE" : "FALSE"); }
private bool WriteArray(NpgsqlNativeTypeInfo TypeInfo, Array ar, StringBuilder sb, Boolean ForExtendedQuery) { bool writtenSomething = false; //we need to know the size of each dimension. int c = ar.Rank; List<int> lengths = new List<int>(c); do { lengths.Add(ar.GetLength(--c)); } while (c != 0); //c is now zero. Might as well reuse it! foreach (object item in ar) { // As this prcedure handles both prepared and plain query representations, in order to not keep if's inside the loops // we simply set a placeholder here for both openElement ( '{' or '[' ) and closeElement ( '}', or ']' ) Char openElement = ForExtendedQuery ? '{' : '['; Char closeElement = ForExtendedQuery ? '}' : ']'; //to work out how many [ characters we need we need to work where we are compared to the dimensions. //Say we are at position 24 in a 3 * 4 * 5 array. //We're at the end of a row as 24 % 3 == 0 so write one [ for that. //We're at the end of a square as 24 % (3 * 4) == 24 % (12) == 0 so write one [ for that. //We're not at the end of a cube as 24 % (3 * 4 * 5) == 24 % (30) != 0, so we're finished for that pass. int curlength = 1; foreach (int lengthTest in lengths) { if (c % (curlength *= lengthTest) == 0) { sb.Append(openElement); } else { break; } } //Write whatever the element is. writtenSomething |= WriteItem(TypeInfo, item, sb, ForExtendedQuery); ++c; //up our counter for knowing when to write [ and ] //same logic as above for writing [ this time writing ] curlength = 1; foreach (int lengthTest in lengths) { if (c % (curlength *= lengthTest) == 0) { sb.Append(closeElement); } else { break; } } //comma between each item. sb.Append(','); } if (writtenSomething) { //last comma was one too many. sb.Remove(sb.Length - 1, 1); } return writtenSomething; }