public ConstantExpression(object value, TypeUsage edmType, NpgsqlProviderManifest providerManifest) { if (edmType == null) { throw new ArgumentNullException("edmType"); } if (edmType.EdmType == null || edmType.EdmType.BuiltInTypeKind != BuiltInTypeKind.PrimitiveType) { throw new ArgumentException("Require primitive EdmType", "edmType"); } _providerManifest = providerManifest; _primitiveType = ((PrimitiveType)edmType.EdmType).PrimitiveTypeKind; _value = value; }
public SqlUpdateGenerator(DbUpdateCommandTree commandTree, NpgsqlProviderManifest providerManifest) : base(providerManifest) { _commandTree = commandTree; }
public SqlDeleteGenerator(DbDeleteCommandTree commandTree, NpgsqlProviderManifest providerManifest) : base(providerManifest) { _commandTree = commandTree; }
public SqlInsertGenerator(DbInsertCommandTree commandTree, NpgsqlProviderManifest providerManifest) : base(providerManifest) { _commandTree = commandTree; }
public SqlSelectGenerator(DbQueryCommandTree commandTree, NpgsqlProviderManifest providerManifest) : base(providerManifest) { _commandTree = commandTree; }
internal override void WriteSql(StringBuilder sqlText) { NpgsqlNativeTypeInfo typeInfo; System.Globalization.NumberFormatInfo ni = NpgsqlNativeTypeInfo.NumberFormat; object value = _value; switch (_primitiveType) { case PrimitiveTypeKind.Binary: { sqlText.AppendFormat("decode('{0}', 'base64')", Convert.ToBase64String((byte[])_value)); } break; case PrimitiveTypeKind.DateTime: sqlText.AppendFormat(ni, "TIMESTAMP '{0:o}'", _value); break; case PrimitiveTypeKind.DateTimeOffset: sqlText.AppendFormat(ni, "TIMESTAMP WITH TIME ZONE '{0:o}'", _value); break; case PrimitiveTypeKind.Decimal: if ((decimal)_value < 0) { sqlText.AppendFormat(ni, "({0})::numeric", _value); } else { sqlText.AppendFormat(ni, "{0}::numeric", _value); } break; case PrimitiveTypeKind.Double: if (double.IsNaN((double)_value)) { sqlText.AppendFormat("'NaN'::float8"); } else if (double.IsPositiveInfinity((double)_value)) { sqlText.AppendFormat("'Infinity'::float8"); } else if (double.IsNegativeInfinity((double)_value)) { sqlText.AppendFormat("'-Infinity'::float8"); } else if ((double)_value < 0) { sqlText.AppendFormat(ni, "({0:r})::float8", _value); } else { sqlText.AppendFormat(ni, "{0:r}::float8", _value); } break; // PostgreSQL has no support for bytes. int2 is used instead in Npgsql. case PrimitiveTypeKind.Byte: value = (short)(byte)_value; goto case PrimitiveTypeKind.Int16; case PrimitiveTypeKind.SByte: value = (short)(sbyte)_value; goto case PrimitiveTypeKind.Int16; case PrimitiveTypeKind.Int16: if ((short)value < 0) { sqlText.AppendFormat(ni, "({0})::int2", _value); } else { sqlText.AppendFormat(ni, "{0}::int2", _value); } break; case PrimitiveTypeKind.Int32: sqlText.AppendFormat(ni, "{0}", _value); break; case PrimitiveTypeKind.Int64: if ((long)_value < 0) { sqlText.AppendFormat(ni, "({0})::int8", _value); } else { sqlText.AppendFormat(ni, "{0}::int8", _value); } break; case PrimitiveTypeKind.Single: if (float.IsNaN((float)_value)) { sqlText.AppendFormat("'NaN'::float4"); } else if (float.IsPositiveInfinity((float)_value)) { sqlText.AppendFormat("'Infinity'::float4"); } else if (float.IsNegativeInfinity((float)_value)) { sqlText.AppendFormat("'-Infinity'::float4"); } else if ((float)_value < 0) { sqlText.AppendFormat(ni, "({0:r})::float4", _value); } else { sqlText.AppendFormat(ni, "{0:r}::float4", _value); } break; case PrimitiveTypeKind.Boolean: sqlText.Append(((bool)_value) ? "TRUE" : "FALSE"); break; case PrimitiveTypeKind.Guid: NpgsqlTypesHelper.TryGetNativeTypeInfo(NpgsqlProviderManifest.GetDbType(_primitiveType), out typeInfo); sqlText.Append(BackendEncoding.UTF8Encoding.GetString(typeInfo.ConvertToBackend(_value, false))); sqlText.Append("::uuid"); break; case PrimitiveTypeKind.String: NpgsqlTypesHelper.TryGetNativeTypeInfo(NpgsqlProviderManifest.GetDbType(_primitiveType), out typeInfo); // Escape syntax is needed for strings with escape values. // We don't check if there are escaped strings for performance reasons. // Check https://github.com/franciscojunior/Npgsql2/pull/10 for more info. // NativeToBackendTypeConverterOptions.Default should provide the correct // formatting rules for any backend >= 8.0. sqlText.Append(BackendEncoding.UTF8Encoding.GetString(typeInfo.ConvertToBackend(_value, false))); break; case PrimitiveTypeKind.Time: sqlText.AppendFormat(ni, "INTERVAL '{0}'", (NpgsqlInterval)(TimeSpan)_value); break; default: // TODO: must support more constant value types. throw new NotSupportedException(string.Format("NotSupported: {0} {1}", _primitiveType, _value)); } base.WriteSql(sqlText); }
public ConstantExpression(object value, TypeUsage edmType, NpgsqlProviderManifest providerManifest) { if (edmType == null) throw new ArgumentNullException("edmType"); if (edmType.EdmType == null || edmType.EdmType.BuiltInTypeKind != BuiltInTypeKind.PrimitiveType) throw new ArgumentException("Require primitive EdmType", "edmType"); _providerManifest = providerManifest; _primitiveType = ((PrimitiveType)edmType.EdmType).PrimitiveTypeKind; _value = value; }