/// <summary> /// 获取插入语法 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override object InsertObject <T>(DbContext dbContext, T obj) { Type type = obj.GetType(); var helper = dbContext.DBHelper; var table = TypeCache.GetTable(type); var primaryKey = table.PrimaryKey; object id; if (primaryKey.KeepIdentity) { id = primaryKey.GetValue(obj); } else { string sequenceName = string.Format("{0}_sequence", table.TableName); var sqlGetIndex = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句 id = SqlStopWatch.ExecScalar(helper, sqlGetIndex); primaryKey.SetValue(obj, Convert.ChangeType(id, primaryKey.PropertyType)); } var sql = GetInsertSql(dbContext, table, obj); //helper.SetParam(primaryKey.MapingName, id); SqlStopWatch.Execute(helper, sql); //var helper2 = helper as OracleHelper; //int id = helper2.Insert(sql,sequenceName); return(id); }
/// <summary> /// 指定替换对象更新 /// </summary> /// <param name="sql"></param> /// <param name="types"></param> /// <returns></returns> public override int Execute(string sql, params Type[] types) { sql = AutoFormat(sql, types); sql = _DBAdapter.SqlFormat(sql); var db = GetDBHelper(); int count = SqlStopWatch.Execute(db, sql); ClearParame(); return(count); }
/// <summary> /// 指定替换对象更新 /// </summary> /// <param name="sql"></param> /// <returns></returns> public override int Execute(string sql) { sql = _DBAdapter.SqlFormat(sql); var db = GetDBHelper(); sql = _DBAdapter.ReplaceParameter(db, sql); int count = SqlStopWatch.Execute(db, sql); ClearParame(); return(count); }
/// <summary> /// 按条件删除 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="where"></param> /// <returns></returns> internal int Delete <TModel>(string where) where TModel : IModel, new() { CheckTableCreated <TModel>(); string table = TypeCache.GetTableName(typeof(TModel), dbContext); string sql = _DBAdapter.GetDeleteSql(table, where); sql = _DBAdapter.SqlFormat(sql); var db = GetDBHelper(); var n = SqlStopWatch.Execute(db, sql); ClearParame(); return(n); }
/// <summary> /// 指定拼接条件更新 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="setValue"></param> /// <param name="where"></param> /// <returns></returns> internal int Update <TModel>(ParameCollection setValue, string where) where TModel : IModel, new() { CheckTableCreated <TModel>(); Type type = typeof(TModel); string table = TypeCache.GetTableName(type, dbContext); string setString = ForamtSetValue <TModel>(setValue); string sql = _DBAdapter.GetUpdateSql(table, setString, where); sql = _DBAdapter.SqlFormat(sql); var db = GetDBHelper(); var n = SqlStopWatch.Execute(db, sql); ClearParame(); return(n); }
/// <summary> /// 获取插入语法 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override object InsertObject <T>(DbContext dbContext, T obj) { Type type = obj.GetType(); var helper = dbContext.DBHelper; var table = TypeCache.GetTable(type); var primaryKey = table.PrimaryKey; var sql = GetInsertSql(dbContext, table, obj); if (primaryKey.KeepIdentity) { SqlStopWatch.Execute(helper, sql); return(primaryKey.GetValue(obj)); } else { sql += ";SELECT LAST_INSERT_ID();"; return(SqlStopWatch.ExecScalar(helper, sql)); } }
/// <summary> /// 获取插入语法 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override object InsertObject <T>(DbContext dbContext, T obj) { Type type = obj.GetType(); var helper = dbContext.DBHelper; var table = TypeCache.GetTable(type); //Type type = obj.GetType(); //string table = TypeCache.GetTableName(type, dbContext); //var typeArry = TypeCache.GetProperties(type, true).Values; //string sql = string.Format("insert into {0}(", table); //string sql1 = ""; //string sql2 = ""; var primaryKey = table.PrimaryKey; object id; if (primaryKey.KeepIdentity) { id = primaryKey.GetValue(obj); } else { string sequenceName = string.Format("{0}_sequence", table.TableName); var sqlGetIndex = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句 id = SqlStopWatch.ExecScalar(helper, sqlGetIndex); } //foreach (Attribute.FieldAttribute info in typeArry) //{ // //if (info.FieldType != Attribute.FieldType.数据库字段) // //{ // // continue; // //} // string name = info.MapingName; // if (info.IsPrimaryKey && !info.KeepIdentity) // { // //continue;//手动插入ID // } // //if (!string.IsNullOrEmpty(info.VirtualField)) // //{ // // continue; // //} // object value = info.GetValue(obj); // if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入 // { // if (value == null) // { // continue; // } // } // value = ObjectConvert.CheckNullValue(value, info.PropertyType); // sql1 += string.Format("{0},", info.MapingName); // sql2 += string.Format("@{0},", info.MapingName); // helper.AddParam(info.MapingName, value); //} //sql1 = sql1.Substring(0, sql1.Length - 1); //sql2 = sql2.Substring(0, sql2.Length - 1); //sql += sql1 + ") values( " + sql2 + ")"; //sql = SqlFormat(sql); var sql = GetInsertSql(dbContext, table, obj); //helper.SetParam(primaryKey.MapingName, id); SqlStopWatch.Execute(helper, sql); //var helper2 = helper as CoreHelper.OracleHelper; //int id = helper2.Insert(sql,sequenceName); return(id); }
/// <summary> /// 插入对象,并返回主键 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override object InsertObject <T>(DbContext dbContext, T obj) { Type type = obj.GetType(); var helper = dbContext.DBHelper; var table = TypeCache.GetTable(type); var primaryKey = table.PrimaryKey; //string table = TypeCache.GetTableName(type, dbContext); //var typeArry = TypeCache.GetProperties(type, true).Values; //Attribute.FieldAttribute primaryKey = null; //string sql = string.Format("insert into [{0}](", table); //string sql1 = ""; //string sql2 = ""; //foreach (Attribute.FieldAttribute info in typeArry) //{ // //if (info.FieldType != Attribute.FieldType.数据库字段) // //{ // // continue; // //} // string name = info.MapingName; // if (info.IsPrimaryKey) // { // primaryKey = info; // } // if (info.IsPrimaryKey && !info.KeepIdentity) // { // continue; // } // //if (!string.IsNullOrEmpty(info.VirtualField)) // //{ // // continue; // //} // object value = info.GetValue(obj); // if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入 // { // if (value == null) // { // continue; // } // } // value = ObjectConvert.CheckNullValue(value, info.PropertyType); // sql1 += string.Format("{0},", FieldNameFormat(info)); // sql2 += string.Format("@{0},", name); // helper.AddParam(name, value); //} //sql1 = sql1.Substring(0, sql1.Length - 1); //sql2 = sql2.Substring(0, sql2.Length - 1); //sql += sql1 + ") values( " + sql2 + ") ; "; //sql = SqlFormat(sql); var sql = GetInsertSql(dbContext, table, obj); if (primaryKey.KeepIdentity) { SqlStopWatch.Execute(helper, sql); return(primaryKey.GetValue(obj)); } else { sql += ";SELECT scope_identity() ;"; return(SqlStopWatch.ExecScalar(helper, sql)); } }