/// <summary> /// 获取被修改的字段 /// </summary> /// <returns></returns> public ParameCollection GetUpdateField(DBAdapter.DBAdapterBase dBAdapterBase = null, bool check = true) { var c = new ParameCollection(); if (this.GetChanges().Count > 0)//按手动指定更改 { var fields = TypeCache.GetProperties(GetType(), true); foreach (var item in this.GetChanges()) { var key = item.Key.Replace("$", ""); var f = fields[key]; if (f == null) continue; if (f.IsPrimaryKey) continue; var value = item.Value; //如果表示值为被追加 名称为$name //使用Cumulation扩展方法后按此处理 if (key != item.Key)//按$name=name+'123123' { if (dBAdapterBase != null) { value = dBAdapterBase.GetFieldConcat(dBAdapterBase.KeyWordFormat(f.MapingName), value, f.PropertyType); } } c[item.Key] = value; } return c; } return c; }
/// <summary> /// 根据数据库类型获取适配器 /// </summary> /// <param name="dbContext"></param> /// <returns></returns> public static DBAdapterBase GetDBAdapterBase(DbContext dbContext) { DBAdapterBase db = null; switch (dbContext.DBHelper.CurrentDBType) { case CoreHelper.DBType.MSSQL: db = new MSSQLDBAdapter(dbContext); break; case CoreHelper.DBType.MSSQL2000: db = new MSSQL2000DBAdapter(dbContext); break; case CoreHelper.DBType.ACCESS: break; case CoreHelper.DBType.MYSQL: db = new MySQLDBAdapter(dbContext); break; case CoreHelper.DBType.ORACLE: db = new ORACLEDBAdapter(dbContext); break; case CoreHelper.DBType.MongoDB: db = new MongoDBAdapter(dbContext); break; } if (db == null) { throw new CRLException("找不到对应的DBAdapte" + dbContext.DBHelper.CurrentDBType); } return(db); }
/// <summary> /// 根据数据库类型获取适配器 /// </summary> /// <param name="dbType"></param> /// <returns></returns> public static DBAdapterBase GetDBAdapterBase(CoreHelper.DBType dbType) { DBAdapterBase db = null; switch (dbType) { case CoreHelper.DBType.MSSQL: db = new MSSQLDBAdapter(); break; case CoreHelper.DBType.MSSQL2000: db = new MSSQL2000DBAdapter(); break; case CoreHelper.DBType.ACCESS: break; case CoreHelper.DBType.MYSQL: db = new MySQLDBAdapter(); break; case CoreHelper.DBType.ORACLE: db = new ORACLEDBAdapter(); break; } if (db == null) { throw new Exception("找不到对应的DBAdapte" + dbType); } return(db); }
/// <summary> /// lambda查询 /// </summary> /// <param name="_dbExtend"></param> /// <param name="_useTableAliasesName">查询是否生成别名</param> internal LambdaQuery(DBExtend _dbExtend, bool _useTableAliasesName = true) { dBExtend = _dbExtend; dBAdapter = _dbExtend._DBAdapter; useTableAliasesName = _useTableAliasesName; TypeCache.SetDBAdapterCache(typeof(T), dBAdapter); GetPrefix(typeof(T)); QueryTableName = TypeCache.GetTableName(typeof(T)); startTime = DateTime.Now; }
internal static void SetDBAdapterCache(Type type, DBAdapter.DBAdapterBase dBAdapter) { var table = GetTable(type); if (table.DBAdapter != null) { return; } table.DBAdapter = dBAdapter; }
/// <summary> /// 获取被修改的字段 /// </summary> /// <returns></returns> public ParameCollection GetUpdateField(DBAdapter.DBAdapterBase dBAdapterBase = null, bool check = true) { var c = new ParameCollection(); var fields = TypeCache.GetProperties(GetType(), true); if (this.GetChanges().Count > 0)//按手动指定更改 { foreach (var item in this.GetChanges()) { var key = item.Key.Replace("$", ""); var f = fields[key]; if (f == null) { continue; } if (f.IsPrimaryKey) { continue; } var value = item.Value; //如果表示值为被追加 名称为$name //使用Cumulation扩展方法后按此处理 if (key != item.Key)//按$name=name+'123123' { if (dBAdapterBase != null) { value = dBAdapterBase.GetFieldConcat(dBAdapterBase.KeyWordFormat(f.MapingName), value, f.PropertyType); } } c[item.Key] = value; } return(c); } //按对象对比 var origin = this.OriginClone; if (origin == null && check) { throw new CRLException("_originClone为空,请确认此对象是由查询创建"); } foreach (var f in fields.Values) { if (f.IsPrimaryKey) { continue; } var originValue = f.GetValue(origin); var currentValue = f.GetValue(this); if (!Equals(originValue, currentValue)) { c.Add(f.MapingName, currentValue); } } return(c); }
/// <summary> /// 根据数据库类型获取适配器 /// </summary> /// <param name="dbContext"></param> /// <returns></returns> public static DBAdapterBase GetDBAdapterBase(DbContextInner dbContext) { DBAdapterBase db = null; var a = DBAdapterBaseCache.TryGetValue(dbContext.DBHelper.CurrentDBType, out db); if (a) { return(db); } db = DBConfigRegister.GetDBAdapterBase(dbContext); DBAdapterBaseCache.Add(dbContext.DBHelper.CurrentDBType, db); return(db); }
internal static void SetColumnDbType(DBAdapter.DBAdapterBase dbAdapter, Attribute.FieldInnerAttribute info) { //if (info.FieldType != Attribute.FieldType.数据库字段) //{ // return; //} string defaultValue; Type propertyType = info.PropertyType; var columnType = dbAdapter.GetColumnType(info, out defaultValue); info.ColumnType = columnType; info.DefaultValue = defaultValue; if (info.ColumnType.Contains("{0}")) { throw new Exception(string.Format("属性:{0} 需要指定长度 ColumnType:{1}", info.MemberName, info.ColumnType)); } }
/// <summary> /// 根据数据库类型获取适配器 /// </summary> /// <param name="dbContext"></param> /// <returns></returns> public static DBAdapterBase GetDBAdapterBase(DbContext dbContext) { DBAdapterBase db = null; var a = DBAdapterBaseCache.TryGetValue(dbContext.DBHelper.CurrentDBType, out db); if (a) { return(db); } var configBuilder = SettingConfigBuilder.current; var exists = configBuilder.DBAdapterBaseRegister.TryGetValue(dbContext.DBHelper.CurrentDBType, out Func <DbContext, DBAdapter.DBAdapterBase> func); if (!exists) { throw new CRLException("找不到对应的DBAdapte" + dbContext.DBHelper.CurrentDBType); } return(func(dbContext)); }
/// <summary> /// 获取列 /// </summary> /// <returns></returns> static List <Attribute.FieldAttribute> GetColumns(Type type, DBAdapter.DBAdapterBase dbAdapter) { //var dbAdapter = Base.CurrentDBAdapter; //Type type = this.GetType(); string tableName = TypeCache.GetTableName(type, dbAdapter.dbContext); var typeArry = TypeCache.GetProperties(type, true).Values; var columns = new List <CRL.Attribute.FieldAttribute>(); foreach (var info in typeArry) { if (info.FieldType != Attribute.FieldType.数据库字段) { continue; } SetColumnDbType(dbAdapter, info); columns.Add(info); } return(columns); }
/// <summary> /// 获取列 /// </summary> /// <returns></returns> List <Attribute.FieldAttribute> GetColumns(DBAdapter.DBAdapterBase dbAdapter) { //var dbAdapter = Base.CurrentDBAdapter; Dictionary <Type, string> dic = dbAdapter.GetFieldMapping(); Type type = this.GetType(); string tableName = TypeCache.GetTableName(type); var typeArry = TypeCache.GetProperties(type, true).Values; var columns = new List <CRL.Attribute.FieldAttribute>(); foreach (var info in typeArry) { if (info.FieldType == Attribute.FieldType.虚拟字段) { continue; } SetColumnDbType(dbAdapter, info, dic); columns.Add(info); } return(columns); }
internal static void SetColumnDbType(DBAdapter.DBAdapterBase dbAdapter, Attribute.FieldAttribute info, Dictionary <Type, string> dic) { if (info.FieldType != Attribute.FieldType.数据库字段) { return; } string defaultValue; Type propertyType = info.PropertyType; if (propertyType.FullName.IndexOf("System.") > -1 && !dic.ContainsKey(propertyType)) { throw new Exception(string.Format("找不到对应的字段类型映射 {0} 在 {1}", propertyType, dbAdapter)); } var columnType = dbAdapter.GetColumnType(info, out defaultValue); info.ColumnType = columnType; info.DefaultValue = defaultValue; if (info.ColumnType.Contains("{0}")) { throw new Exception(string.Format("属性:{0} 需要指定长度 ColumnType:{1}", info.Name, info.ColumnType)); } }