public static string DateTimeToLiteralWithMs(DbTypeInfo typeDef, object value) { var dt = (DateTime)value; // o: roundtrip date/time pattern, includes milliseconds after dot; used by MS SQL, Postgress var result = "'" + dt.ToString("o") + "'"; return result; }
public static string DateTimeOffsetToLiteral(DbTypeInfo typeDef, object value) { var dt = (DateTimeOffset)value; var result = "'" + dt.ToString("o") + "'"; return(result); }
public DbColumnInfo(EntityMemberInfo member, DbTableInfo table, string columnName, DbTypeInfo typeInfo) : base(table.DbModel, table.Schema, DbObjectType.Column, member) { Member = member; Table = table; SetName(columnName); TypeInfo = typeInfo; Table.Columns.Add(this); if (member.Flags.IsSet(EntityMemberFlags.Nullable)) { Flags |= DbColumnFlags.Nullable; } if (member.Flags.IsSet(EntityMemberFlags.Identity)) { Flags |= DbColumnFlags.Identity; } if (member.Flags.IsSet(EntityMemberFlags.ForeignKey)) { Flags |= DbColumnFlags.ForeignKey; if (member.ForeignKeyOwner.ReferenceInfo.ToKey.Entity.Flags.IsSet(EntityFlags.HasIdentity)) { Flags |= DbColumnFlags.IdentityForeignKey; } } if (_sizableTypes.Contains(member.DataType)) { Flags |= DbColumnFlags.UseParamForLongValues; } }
public Func <object, string> ToLiteralConverter; //converter for template public DbParamInfo(EntityCommandParameter owner, string name, DbColumnInfo sourceColumn, int argIndex) : this(owner, name, argIndex) { SourceColumn = sourceColumn; Direction = owner.Direction; TypeInfo = sourceColumn.TypeInfo; }
public static string DateTimeToLiteralNoMs(DbTypeInfo typeDef, object value) { var dt = (DateTime)value; // 's' - Sortable date/time pattern; conforms to ISO 8601. The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss" return("'" + dt.ToString("s") + "'"); }
public static string TimeSpanToLiteralNoMs(DbTypeInfo typeDef, object value) { var ts = (TimeSpan)value; var result = "'" + ts.ToString(@"hh\:mm\:ss") + "'"; return(result); }
private static string BytesToLiteral(DbTypeInfo typeInfo, object value) { if(value == null || value == DBNull.Value) return "NULL"; var bytes = (byte[])value; return @"E'\\x" + HexUtil.ByteArrayToHex(bytes) + "'"; }
public DbColumnInfo(EntityMemberInfo member, DbTableInfo table, string columnName, DbTypeInfo typeInfo) : base(table.DbModel, table.Schema, DbObjectType.Column, member) { Member = member; Table = table; ColumnName = columnName; TypeInfo = typeInfo; Table.Columns.Add(this); base.GlobalName = DbModelHelper.GetGlobalName(Schema, table.TableName, columnName); if (member.Flags.IsSet(EntityMemberFlags.Nullable)) { Flags |= DbColumnFlags.Nullable; } if (member.Flags.IsSet(EntityMemberFlags.Identity)) { Flags |= DbColumnFlags.Identity; } if (member.Flags.IsSet(EntityMemberFlags.ForeignKey)) { Flags |= DbColumnFlags.ForeignKey; if (member.ForeignKeyOwner.ReferenceInfo.ToKey.Entity.Flags.IsSet(EntityFlags.HasIdentity)) { Flags |= DbColumnFlags.IdentityForeignKey; } } }
public static string DateTimeToLiteralWithMs(DbTypeInfo typeDef, object value) { var dt = (DateTime)value; // o: roundtrip date/time pattern, includes milliseconds after dot; used by MS SQL, Postgress var result = "'" + ConvertHelper.DateTimeToUniString(dt) + "'"; return(result); }
public DbSequenceInfo(DbModel model, string name, string schema, DbTypeInfo dbType, long startValue, int increment) : base(model, schema, DbObjectType.Sequence, null) { Name = name; DbType = dbType; StartValue = startValue; Increment = increment; FullName = model.Driver.FormatFullName(schema, name); }
public static string StringToLiteral(DbTypeInfo typeDef, object value) { var str = (string)value; if (!str.Contains('\'')) //fast case { return("'" + str + "'"); } return("'" + str.Replace("'", "''") + "'"); }
public DbSequenceInfo(DbModel model, SequenceDefinition definition) : base(model, GetSchema(model, definition), DbObjectType.Sequence, definition) { Name = definition.Name; DbType = model.Driver.TypeRegistry.GetDbTypeInfo(definition.DataType, 0); StartValue = definition.StartValue; Increment = definition.Increment; Definition = definition; FullName = model.Driver.FormatFullName(Schema, Name); }
public static string DefaultValueToLiteral(DbTypeInfo typeDef, object value) { if (value is string) return StringToLiteral(typeDef, value); if (value is DateTime) return DateTimeToLiteralNoMs(typeDef, value); //Do not use value.ToString()! - it uses current culture and may result in strange output // For example, if computer has Russian format set in 'Region and Language' dialog, then float numbers like '1.2' result in '1,2' // which breaks SQL completely return string.Format(CultureInfo.InvariantCulture, "{0}", value); }
public static string TimeSpanToLiteralWithMs(DbTypeInfo typeDef, object value) { var ts = (TimeSpan)value; var s = ts.ToString(@"hh\:mm\:ss\.FFFFF"); //special case - if milliseconds is 0, we have an ending dot, and SQL does not like it if (s.EndsWith(".")) { s = s + "0"; } return("'" + s + "'"); }
//constructor loader from the database public DbColumnInfo(DbTableInfo table, string columnName, DbTypeInfo typeInfo) : base(table.DbModel, table.Schema, DbObjectType.Column, null) { Table = table; ColumnName = columnName; TypeInfo = typeInfo; if (typeInfo.IsNullable) { this.Flags |= DbColumnFlags.Nullable; } Table.Columns.Add(this); base.GlobalName = DbModelHelper.GetGlobalName(table.Schema, table.TableName, columnName); }
//constructor loader from the database public DbColumnInfo(DbTableInfo table, string columnName, DbTypeInfo typeInfo, bool isNullable) : base(table.DbModel, table.Schema, DbObjectType.Column, null) { Table = table; SetName(columnName); // TO FIX TypeInfo = typeInfo; if (isNullable) { this.Flags |= DbColumnFlags.Nullable; } Table.Columns.Add(this); base.LogRefName = DbModelHelper.JoinNames(table.Schema, table.TableName, columnName); }
public static string DefaultValueToLiteral(DbTypeInfo typeDef, object value) { if (value is string) { return(StringToLiteral(typeDef, value)); } if (value is DateTime) { return(DateTimeToLiteralNoMs(typeDef, value)); } //Do not use value.ToString()! - it uses current culture and may result in strange output // For example, if computer has Russian format set in 'Region and Language' dialog, then float numbers like '1.2' result in '1,2' // which breaks SQL completely return(string.Format(CultureInfo.InvariantCulture, "{0}", value)); }
public virtual DbTypeInfo GetDbTypeInfo(DataRow columnRow) { var charSize = columnRow.GetAsLong("CHARACTER_MAXIMUM_LENGTH"); var byteSize = columnRow.GetAsLong("CHARACTER_OCTET_LENGTH"); var dataTypeString = columnRow.GetAsString("DATA_TYPE").ToLowerInvariant(); var prec = columnRow.GetAsByte("NUMERIC_PRECISION"); var scale = columnRow.GetAsByte("NUMERIC_SCALE"); var isNullable = (columnRow.GetAsString("IS_NULLABLE") == "YES"); bool isMemo = (charSize < 0 || byteSize < 0); var typeDef = Driver.TypeRegistry.FindVendorDbTypeInfo(dataTypeString); if(typeDef == null) typeDef = Driver.TypeRegistry.Types.FirstOrDefault(td => td.Aliases.Contains(dataTypeString)); if(typeDef == null) return null; var isAutoMemo = Driver.TypeRegistry.AutoMemoTypes.Contains(dataTypeString); var size = isAutoMemo? -1 : (charSize != 0 ? charSize : byteSize); var typeSpec = typeDef.FormatTypeSpec(size, prec, scale, isMemo); var typeInfo = new DbTypeInfo(typeDef, typeSpec, isNullable, size, prec, scale, typeDef.DefaultColumnInit); // AssignValueConverters(typeInfo); - no need for value converters return typeInfo; }
public DbParamInfo(EntityCommandParameter owner, string name, DbTypeInfo typeInfo, int argIndex) : this(owner, name, argIndex) { TypeInfo = typeInfo; Direction = ParameterDirection.Input; }
public static string BytesToLiteral(DbTypeInfo typeDef, object value) { var bytes = (byte[])value; return "0x" + HexUtil.ByteArrayToHex(bytes); }
public DbSequenceInfo(DbModel model, string name, string schema, DbTypeInfo dbType, long startValue, int increment) : base(model, schema, DbObjectType.Sequence, null) { Name = name; DbType = dbType; StartValue = startValue; Increment = increment; FullName = model.Driver.GetFullName(schema, name); }
public DbColumnInfo(EntityMemberInfo member, DbTableInfo table, string columnName, DbTypeInfo typeInfo) : base(table.DbModel, table.Schema, DbObjectType.Column, member) { Member = member; Table = table; ColumnName = columnName; TypeInfo = typeInfo; Table.Columns.Add(this); base.GlobalName = DbModelHelper.GetGlobalName(Schema, table.TableName, columnName); if (member.Flags.IsSet(EntityMemberFlags.Nullable)) Flags |= DbColumnFlags.Nullable; if (member.Flags.IsSet(EntityMemberFlags.Identity)) Flags |= DbColumnFlags.Identity; if (member.Flags.IsSet(EntityMemberFlags.ForeignKey)) { Flags |= DbColumnFlags.ForeignKey; if (member.ForeignKeyOwner.ReferenceInfo.ToKey.Entity.Flags.IsSet(EntityFlags.HasIdentity)) Flags |= DbColumnFlags.IdentityForeignKey; } }
//constructor loader from the database public DbColumnInfo(DbTableInfo table, string columnName, DbTypeInfo typeInfo) : base(table.DbModel, table.Schema, DbObjectType.Column, null) { Table = table; ColumnName = columnName; TypeInfo = typeInfo; if (typeInfo.IsNullable) this.Flags |= DbColumnFlags.Nullable; Table.Columns.Add(this); base.GlobalName = DbModelHelper.GetGlobalName(table.Schema, table.TableName, columnName); }
public DbParamInfo(EntityCommandParameter owner, string name, DbColumnInfo sourceColumn, int argIndex) : this(owner, name, argIndex) { SourceColumn = sourceColumn; Direction = owner.Direction; TypeInfo = sourceColumn.TypeInfo; }
public static string StringToLiteral(DbTypeInfo typeDef, object value) { var str = (string)value; if (!str.Contains('\'')) //fast case return "'" + str + "'"; return "'" + str.Replace("'", "''") + "'"; }
private void InitTableTypeInfo() { var msTypeReg = (MsSqlTypeRegistry)this.DbModel.Driver.TypeRegistry; var userDefinedVendorType = msTypeReg.UserDefinedVendorDbType; var customTableType = DbModel.CustomDbTypes.First(t => t.Name == MsSqlDbDriver.ArrayAsTableTypeName); var tableTypeFullName = customTableType.FullName; ArrayAsTableDbTypeInfo = new DbTypeInfo(userDefinedVendorType, tableTypeFullName, false, 0, 0, 0); ArrayAsTableDbTypeInfo.PropertyToColumnConverter = ConvertListToDataTable; }
public static string TimeSpanToLiteralNoMs(DbTypeInfo typeDef, object value) { var ts = (TimeSpan)value; var result = "'" + ts.ToString(@"hh\:mm\:ss") + "'"; return result; }
public override DbTypeInfo GetDbTypeInfo(Type clrType, int size) { var typeInfo = base.GetDbTypeInfo(clrType, size); if (typeInfo != null) return typeInfo; Type elemType; if ((clrType.IsArray || clrType.IsGenericType) && clrType.IsListOfDbPrimitive(out elemType)) { var elemTypeInfo = base.GetDbTypeInfo(elemType, 0); var npgDbType = NpgsqlDbType.Array | (NpgsqlDbType)elemTypeInfo.VendorDbType.VendorDbType; //Pg requires combination of Array and elem type var arrVendDbType = new VendorDbTypeInfo(elemType.Name + "[]", DbType.Object, typeof(object), null, null, null, null, VendorDbTypeFlags.Array, null, null, (int)npgDbType); var arrTypeInfo = new DbTypeInfo(arrVendDbType, elemTypeInfo.SqlTypeSpec + "[]", false, 0, 0, 0); arrTypeInfo.PropertyToColumnConverter = (obj) => ConvertListToArray(elemType, obj); //ConvertListToArray; return arrTypeInfo; } Util.Throw("Failed to match DB type to CLR type {0}.", clrType); return null; }
// Special converters for literal presentations (used in batch mode). Default converter provides too much precision and it blows up public static string DateTimeToLiteralMsSql(DbTypeInfo typeInfo, object value) { if(value == null || value == DBNull.Value) return "NULL"; var dt = (DateTime)value; var str = "'" + dt.ToString("yyyy-MM-ddTHH:mm:ss.FFF") + "'"; return str; }
private static string NumToLiteralWithCast(DbTypeInfo typeInfo, object value) { if(value == null || value == DBNull.Value) return "NULL"; var strValue = value.ToString(); return string.Format("CAST({0} AS {1})", strValue, typeInfo.SqlTypeSpec); }
public DbParamInfo AddParameter(EntityCommandParameter owner, string name, DbTypeInfo typeInfo, int argIndex) { return(AddParameter(new DbParamInfo(owner, name, typeInfo, argIndex))); }
public static string BinaryToLiteral(DbTypeInfo typeDef, object value) { var bin = (Binary)value; return "0x" + HexUtil.ByteArrayToHex(bin.GetBytes()); }
public static string GuidToLiteral(DbTypeInfo typeDef, object value) { var g = (Guid)value; return("'" + g.ToString() + "'"); }
public static string TimeSpanToLiteralWithMs(DbTypeInfo typeDef, object value) { var ts = (TimeSpan)value; var s = ts.ToString(@"hh\:mm\:ss\.FFFFFF"); //special case - if milliseconds is 0, we have an ending dot, and SQL does not like it if(s.EndsWith(".")) s = s + "0"; return "'" + s + "'"; }
public static string BinaryToLiteral(DbTypeInfo typeDef, object value) { var bin = (Binary)value; return("0x" + HexUtil.ByteArrayToHex(bin.GetBytes())); }
private static string ConvertList(object value, DbTypeInfo elemTypeInfo) { var strings = new List<string>(); var iEnum = value as IEnumerable; foreach (var v in iEnum) { strings.Add(elemTypeInfo.ToLiteral(elemTypeInfo.PropertyToColumnConverter(v))); } return string.Join(", ", strings); }
public static string GuidToLiteral(DbTypeInfo typeDef, object value) { var g = (Guid)value; return "'" + g.ToString() + "'"; }
public DbParamInfo AddParameter(EntityCommandParameter owner, string name, DbTypeInfo typeInfo, int argIndex) { return AddParameter(new DbParamInfo(owner, name, typeInfo, argIndex)); }
public static string DateTimeToLiteralNoMs(DbTypeInfo typeDef, object value) { var dt = (DateTime)value; // 's' - Sortable date/time pattern; conforms to ISO 8601. The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss" return "'" + dt.ToString("s") + "'"; }
public static string BytesToLiteral(DbTypeInfo typeDef, object value) { var bytes = (byte[])value; return("0x" + HexUtil.ByteArrayToHex(bytes)); }
public DbSequenceInfo(DbModel model, SequenceDefinition definition) : base(model, GetSchema(model, definition), DbObjectType.Sequence, definition) { Name = definition.Name; DbType = model.Driver.TypeRegistry.GetDbTypeInfo(definition.DataType, 0); StartValue = definition.StartValue; Increment = definition.Increment; Definition = definition; FullName = model.Driver.GetFullName(Schema, Name); }
public static string DateTimeOffsetToLiteral(DbTypeInfo typeDef, object value) { var dt = (DateTimeOffset)value; var result = "'" + dt.ToString("o") + "'"; return result; }
protected virtual void LoadSequences() { if (!Settings.Driver.Supports(DbFeatures.Sequences)) return; var data = GetSequences(); foreach(DataRow row in data.Rows) { var schema = row.GetAsString("SEQUENCE_SCHEMA"); var name = row.GetAsString("SEQUENCE_NAME"); var dataType = row.GetAsString("DATA_TYPE"); var startValue = row.GetAsLong("START_VALUE"); var incr = row.GetAsInt("INCREMENT"); //find type def var typeDef = Driver.TypeRegistry.FindVendorDbTypeInfo(dataType); var typeSpec = typeDef.FormatTypeSpec(0, 0, 0); var typeInfo = new DbTypeInfo(typeDef, dataType, false, 0, 0, 0, typeDef.DefaultColumnInit); // add sequence var seq = new DbSequenceInfo(this.Model, name, schema, typeInfo, startValue, incr); Model.AddSequence(seq); } }
private Type GetMemberType(DbColumnInfo colInfo) { var nullable = colInfo.Flags.IsSet(DbColumnFlags.Nullable); var typeInfo = colInfo.TypeInfo; var mType = typeInfo.VendorDbType.ColumnOutType; if(mType.IsValueType && nullable) mType = ReflectionHelper.GetNullable(mType); Type forcedType; if(_config.ForceDataTypes.TryGetValue(colInfo.ColumnName, out forcedType)) { mType = forcedType; return mType; } if(mType == typeof(byte[])) { var changeToGuid = _config.Options.IsSet(DbFirstOptions.Binary16AsGuid) && typeInfo.Size == 16 || _config.Options.IsSet(DbFirstOptions.BinaryKeysAsGuid) && colInfo.Flags.IsSet(DbColumnFlags.PrimaryKey | DbColumnFlags.ForeignKey); if(changeToGuid) { _guidTypeInfo = _guidTypeInfo ?? _dbSettings.ModelConfig.Driver.TypeRegistry.GetDbTypeInfo(typeof(Guid), 0); colInfo.TypeInfo = _guidTypeInfo; mType = typeof(Guid); } } return mType; }
public static string MySqlStringToLiteral(DbTypeInfo typeDef, object value) { if(value == null || value == DBNull.Value) return "NULL"; var str = (string)value; //Escape backslash - this is MySql specific str = str.Replace(@"\", @"\\"); if(!str.Contains('\'')) //fast case return "'" + str + "'"; return "'" + str.Replace("'", "''") + "'"; }
private static string ConvertBinaryToLiteral(DbTypeInfo typeInfo, object value) { var bytes = (byte[])value; return "x'" + HexUtil.ByteArrayToHex(bytes) + "'"; }