/// <summary> /// 从TypeDefine取类型的SQL表信息,包括表名和主键定义 /// </summary> /// <param name="typeName"></param> /// <returns></returns> public static TableInfo GetTableInfo(Type t, int pkCount = 0, string tableName = "") { if (null == t) { return(null); } string key = $"tblInfo:{t.FullName}:{pkCount}:{tableName}"; TableInfo result = null; if (dicTableInfo.TryGetValue(key, out result)) { return(result); } result = EntityReader.GetEntityMapping(t); if (!string.IsNullOrEmpty(tableName)) { result.TableName = tableName; } if (0 == (result?.PKeys?.Count ?? 0))//取前N个属性 { if (0 < pkCount) { foreach (KeyValuePair <string, PropertyInfo> ele in result.PropertyMappings.Take(pkCount)) { result.PKeys.Add(ele.Key); } } } dicTableInfo.TryAdd(key, result); return(result); }
/// <summary> /// 会排除自动增长列 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="connInfo"></param> /// <param name="Record"></param> /// <param name="tableName"></param> /// <returns></returns> public static int InsertPrimitive <T>(this DbContext connInfo, IEnumerable <T> records, SqlFunction repl = null, IEnumerable <string> onlyFields = null, string tableName = "") where T : new() { string sql = connInfo.GetInsertSql <T>(repl, onlyFields, tableName); if (connInfo.DbType != DbConnType.ODPNET) { return(connInfo.Db.Execute(sql, records, connInfo.Transaction, commandType: CommandType.Text)); } else { Type t = typeof(T); TableInfo tblInfo = EntityReader.GetEntityMapping(t, true); Dictionary <string, PropertyInfo> fields = tblInfo.PropertyMappings; int count = 0; foreach (T record in records) { RequestBase param = new RequestBase(); foreach (KeyValuePair <string, PropertyInfo> item in fields) { if (tblInfo.IsCurrentTimeField(item.Key) || (null != repl && repl.ExistsField(item.Key))) { continue; } param.Add(item.Key, SqlBuilder.GetPropertyValue <T>(record, item, connInfo.DbType)); } count += connInfo.Db.Execute(sql, param, connInfo.Transaction, commandType: CommandType.Text); } return(count); } }
/// <summary> /// 取INSERT语句的定义串 /// </summary> /// <param name="t"></param> /// <returns></returns> public static string GetInsertSql <T>(this DbContext connInfo, SqlFunction sqlFunc = null, IEnumerable <string> onlyFields = null, string tableName = "") where T : new() { Type t = typeof(T); string key = $"{t.FullName}:insert:{connInfo.DbType.ToString()}:{tableName}:{GetFieldsKey(onlyFields)}:{sqlFunc?.ToString()}"; string result = ""; if (insertStr.TryGetValue(key, out result)) { return(result); } TableInfo tableInfo = EntityReader.GetEntityMapping(t, true); bool specFields = (0 < (onlyFields?.Count() ?? 0)); //指定栏位 Dictionary <string, PropertyInfo> fieldDefine = tableInfo.PropertyMappings; StringBuilder strbld = new StringBuilder(256); strbld.Concat("INSERT INTO ", GetTableName <T>(connInfo.DbType, tableName, true), " ("); if (!specFields) { strbld.Append(string.Join(",", fieldDefine.Select(c => connInfo.DbDelimiter(c.Key)).ToArray())); } else { strbld.Append(string.Join(",", onlyFields)); } strbld.Append(") VALUES ("); if (!specFields) { strbld.Append(string.Join(",", fieldDefine.Select(c => tableInfo.IsCurrentTimeField(c.Key) ? connInfo.DbType.GetCurrentTimeFuncName() : (sqlFunc?.ExistsField(c.Key) ?? false) ? sqlFunc?.GetSqlFunction(c.Key) : connInfo.DbType.TreatParaName(c.Key) ).ToArray())); } else { strbld.Append(string.Join(",", onlyFields.Select(c => tableInfo.IsCurrentTimeField(c) ? connInfo.DbType.GetCurrentTimeFuncName() : (sqlFunc?.ExistsField(c) ?? false) ? sqlFunc?.GetSqlFunction(c) : connInfo.DbType.TreatParaName(c) ).ToArray())); } strbld.Append(" )"); result = strbld.ToString(); insertStr.TryAdd(key, result); return(result); }