static void DatabaseCRUD(IDataContext db) { //常规测试 { //删除数据 db.BeginTransaction(); Console.WriteLine("delete count={0}", db.Delete("test", new { name = "xxxxxxxxx", //name为xxxxxxxxx id = "{ '$gt': 200000 }" //id大于200000,C#语法不支持JSON,但我们支持嵌套JSON语句 :) })); db.CommitTransaction(); //插入数据 db.BeginTransaction(); var id = db.Insert("test", new { name = "xxxxxxxxx", count = 9999, data = new {//JSON类型测试 url = "https://www.baidu.com/", guid = System.Guid.NewGuid(), datetime = DateTime.Now, values = FastWrapper.As(new {//嵌套复杂对象测试 nickName = "昵尔", account = "test" }) } }); Console.WriteLine($"insert id={id}"); db.CommitTransaction(); //查询数据 Console.WriteLine("select"); Console.WriteLine(JSON.ToNiceJSON(db.Find("test", new { name = "xxxxxxxxx" }))); //更新数据 db.BeginTransaction(); var updated = db.Update("test", new { name = "fsafhakjshfksjhf", count = 88 }, new { id }) == 1; Console.WriteLine($"update {updated}"); db.CommitTransaction(); //验证是否真的修改到 Console.WriteLine("select new value"); Console.WriteLine(JSON.ToNiceJSON(db.Find("test", new { id }))); } Console.ReadKey(); { //枚举测试 var id = db.Insert("t_user", new { account = "admin", type = UserTypes.Manager, password = "******" }); Console.WriteLine(JSON.ToNiceJSON(db.Find("t_user", new { id }))); } Console.ReadKey(); }
/// <summary> /// 创建参数,仅创建对象,不会追加到参数列表。 /// </summary> /// <param name="name">参数名称,必须以@开头。</param> /// <param name="value">参数的值。</param> /// <param name="properties">属性列表。</param> /// <returns>返回参数实例。</returns> public virtual CommandParameter Create(string name, object value, object properties) { var dialect = Provider?.Dialect; CommandParameter result = value as CommandParameter; if (result != null) { if (string.IsNullOrEmpty(result.Name)) { result.Name = NextName(); } else { result.Name = dialect?.ParameterNameGrammar(result.Name); } if (!result.Created) { OnCreate(result); result.Created = true; } return(result); } result = new CommandParameter() { Value = value, RealType = value == null ? typeof(object) : value.GetType(), }; if (string.IsNullOrEmpty(name)) { result.Name = NextName(); } else { if (name.StartsWith("out_", System.StringComparison.OrdinalIgnoreCase)) { result.Name = dialect?.ParameterNameGrammar(name.Substring(4)); result.IsOut = true; } else { result.Name = dialect?.ParameterNameGrammar(name); } } if (properties != null) { result.Properties = FastWrapper.As(properties); } OnCreate(result); result.Created = true; return(result); }
/// <summary> /// 将绑定后的参数以抽象对象方式输出 /// </summary> /// <returns></returns> public Symbol.Collections.Generic.NameValueCollection <object> ToObject() { var list = FastWrapper.As(null); if (_paramters == null || _paramters.Count == 0) { return(list); } Init(); for (int i = 0; i < _paramters.Count; i++) { list[_paramters[i].Name] = TryGetValue(i, _paramters[i]); } return(list); }
object TryGetValue(int index, IParameterInfo paramter) { object value = null; bool finded = false; if (_tryGetValues != null) { for (int i = 0; i < _tryGetValues.Length; i++) { if (_tryGetValues[i] == null) { Init_Build(i, _datas[i]); } if (_tryGetValues[i](index, paramter, _datas[i], out value)) { finded = true; if (value != null) { value = _convertValue(PreConvertValue(value, paramter.Type), paramter.Type); break; } } } } if (!finded) { var dataBodyAttribute = Symbol.AttributeExtensions.GetCustomAttribute <DataBodyAttribute>(paramter, true); if (dataBodyAttribute != null) { var datas = FastWrapper.As(_bodyData); value = datas; for (int i = 0; i < _paramters.Count; i++) { datas.Remove(_paramters[i].Name); } if (_otherParamters != null) { for (int i = 0; i < _otherParamters.Count; i++) { datas.Remove(_otherParamters[i].Name); } } if (!string.IsNullOrEmpty(dataBodyAttribute.InvalidKeys)) { datas.RemoveKeys(dataBodyAttribute.InvalidKeys.Split(',', ';', ',', ';', '|', '|', ' ')); } //value = _datas[0]; } } if (value == null) { try { value = paramter.DefaultValue; } catch (System.FormatException) { } if (value == null || value is System.DBNull) { value = TypeExtensions.DefaultValue(paramter.Type); } } return(value); }