private static string GetWhereExpression(this ISqlObject obj) { string name = null; object value = null; bool isencrypt = false; foreach (var property in obj.GetType().GetProperties()) { if (property.CanWrite && property.CanRead && property.GetCustomAttribute <SqlSearchKeyAttribute>() != null && property.TryGetSqlElementName(out string name1)) { name = name1; value = property.GetValue(obj); isencrypt = property.IsSqlEncrypt(); break; } } if (name != null) { return(GetWhereExpression(name, value, isencrypt)); } else { throw new KeyNotFoundException(); } }
/// <summary> /// 根据<see cref="SqlSearchKeyAttribute"/>将数据库相应行中<see cref="SqlBindingAttribute"/>绑定的属性更新。 /// </summary> /// <param name="obj"></param> /// <param name="binding"></param> public static void Update(this ISqlObject obj, string binding) { //获取成员列表。 NameValueList vs = new NameValueList(); foreach (var property in obj.GetType().GetProperties()) { if (property.CanWrite && property.CanRead && property.TryGetSqlElementName(out string name) && property.SqlBindingExists(binding)) { //加密。 vs.Add(name, GetSqlValue(property.GetValue(obj), property.IsSqlEncrypt())); } } //生成命令语句。 string cmd = $"update {obj.Table} set " + string.Join(',', vs.Map((m) => $"{m.Name}={m.Value}")) + " " + obj.GetWhereExpression(); obj.SqlProvider.Execute(cmd); }
private static void SetValue(this ISqlObject obj, DataRow row) { foreach (var property in obj.GetType().GetProperties()) { if (property.CanRead && property.CanWrite && property.TryGetSqlElementName(out string name)) { //SOLVED BUG DBNull的坑 if (row[name] != DBNull.Value) { if (property.PropertyType == typeof(string)) { bool isencrypt = property.IsSqlEncrypt(); property.SetValue(obj, GetRowValue((string)row[name], isencrypt)); } else { property.SetValue(obj, row[name]); } } } } }
/// <summary> /// 添加到数据库。 /// </summary> /// <param name="obj"></param> public static void Add(this ISqlObject obj) { //获取成员列表。 NameValueList vs = new NameValueList(); foreach (var property in obj.GetType().GetProperties()) { if (property.CanWrite && property.CanRead && property.TryGetSqlElementName(out string name, out bool isreadonly)) { if (property.GetValue(obj) != null && !isreadonly) { //加密。 vs.Add(name, GetSqlValue(property.GetValue(obj), property.IsSqlEncrypt())); } } } //生成命令语句。 //string cmd = $"insert into {obj.Table} set {(string.Join(',', vs.Map((m) => $"{m.Name}={m.Value}")))}"; string cmd = $"insert into {obj.Table} ({ToolUtil.JoinString(',', vs.Names)}) values ({ToolUtil.JoinString(',', vs.Values)})"; obj.SqlProvider.Execute(cmd); }
/// <inheritdoc/> public int CompareTo(ISqlObject other) { if (other is SqlNull) { if (IsNull) { return(0); } return(1); } SqlBoolean otherBoolean; if (other is SqlNumber) { var num = (SqlNumber)other; if (num.IsNull) { otherBoolean = Null; } else if (num == SqlNumber.One) { otherBoolean = True; } else if (num == SqlNumber.Zero) { otherBoolean = False; } else { throw new ArgumentOutOfRangeException("other", "The given numeric value is out of range for a comparison with SQL BOOLEAN."); } } else if (other is SqlBoolean) { otherBoolean = (SqlBoolean)other; } else { throw new ArgumentException(String.Format("Object of type {0} cannot be compared to SQL BOOLEAN", other.GetType().FullName)); } return(CompareTo(otherBoolean)); }
internal override int ColumnSizeOf(ISqlObject obj) { if (obj is SqlNull) return 1; if (!(obj is SqlDateTime)) throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'", obj.GetType())); if (obj.IsNull) return 1; // Type + Length + Bytes return 1 + 4 + 13; }
internal override int ColumnSizeOf(ISqlObject obj) { if (obj is SqlNull || obj == null) return 1; if (!(obj is SqlNumber)) throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'.", obj.GetType())); var number = (SqlNumber) obj; if (number.IsNull) return 1; if (number.CanBeInt32) return 1 + 4; if (number.CanBeInt64) return 1+ 8; // Type + Scale + Precision + Byte Count var length = number.ToByteArray().Length; return 1 + 4 + 4 + 4 + length; }
internal override int ColumnSizeOf(ISqlObject obj) { if (obj is SqlNull) { return(1); } if (!(obj is SqlNumber)) { throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'.", obj.GetType())); } var number = (SqlNumber)obj; if (number.IsNull) { return(1); } if (number.CanBeInt32) { return(1 + 4); } if (number.CanBeInt64) { return(1 + 8); } // Type + Scale + Precision + Byte Count var length = number.ToByteArray().Length; return(1 + 4 + 4 + 4 + length); }
public override void SerializeObject(Stream stream, ISqlObject obj) { var writer = new BinaryWriter(stream); if (obj.IsNull) { if (TypeCode == SqlTypeCode.Clob || TypeCode == SqlTypeCode.LongVarChar) { writer.Write((byte)3); } else { writer.Write((byte)1); } return; } var sqlString = (ISqlString)obj; if (obj is SqlString) { var bytes = ((SqlString) sqlString).ToByteArray(Encoding); writer.Write((byte) 2); writer.Write(bytes.Length); writer.Write(bytes); } else if (obj is SqlLongString) { var longString = (SqlLongString) sqlString; writer.Write((byte) 4); writer.Write(longString.ObjectId.StoreId); writer.Write(longString.ObjectId.Id); } else { throw new FormatException(String.Format("The object of type '{0}' is not handled by {1}", obj.GetType(), ToString())); } }
internal override int ColumnSizeOf(ISqlObject obj) { if (obj is SqlNull) { return(1); } if (!(obj is SqlDateTime)) { throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'", obj.GetType())); } if (obj.IsNull) { return(1); } // Type + Length + Bytes return(1 + 4 + 13); }
public override void SerializeObject(Stream stream, ISqlObject obj) { var writer = new BinaryWriter(stream); if (obj.IsNull) { if (TypeCode == SqlTypeCode.Clob || TypeCode == SqlTypeCode.LongVarChar) { writer.Write((byte)3); } else { writer.Write((byte)1); } return; } var sqlString = (ISqlString)obj; if (obj is SqlString) { var bytes = ((SqlString)sqlString).ToByteArray(Encoding); writer.Write((byte)2); writer.Write(bytes.Length); writer.Write(bytes); } else if (obj is SqlLongString) { var longString = (SqlLongString)sqlString; writer.Write((byte)4); writer.Write(longString.ObjectId.StoreId); writer.Write(longString.ObjectId.Id); } else { throw new FormatException(String.Format("The object of type '{0}' is not handled by {1}", obj.GetType(), ToString())); } }