public object FindById(Type type, params object[] keys) { Table tableOrCreate = TableCache.GetTableOrCreate(type); if (tableOrCreate.KeyColumns.Count != keys.Length) { throw new ApplicationException("主键值的数量不正确"); } object obj2 = Activator.CreateInstance(type); if (tableOrCreate.KeyColumns.Count == 1) { tableOrCreate.KeyColumns[0].PropertyInfo.SetValue(obj2, keys[0], null); } else { for (int i = 0; i < tableOrCreate.KeyColumns.Count; i++) { tableOrCreate.KeyColumns[i].PropertyInfo.SetValue(obj2, keys[i], null); } } if (this._dataAccess.Fill(this._connection, obj2)) { return(obj2); } return(null); }
public override bool ExistTable(DbConnection conn, Type type) { Table tableOrCreate = TableCache.GetTableOrCreate(type); string sql = string.Format("select count(*) from user_tables where table_name = '{0}'", tableOrCreate.TableName.ToUpper()); return(Convert.ToInt32(base.ExecuteScalar(conn, null, sql, new DbParameter[0])) > 0); }
public T GetAndLock <T>(int?wait, params object[] keys) where T : class { Column column; Table tableOrCreate = TableCache.GetTableOrCreate(typeof(T)); if (tableOrCreate.KeyColumns.Count != keys.Length) { throw new ApplicationException("主键值的数量不正确"); } T local = Activator.CreateInstance <T>(); if (tableOrCreate.KeyColumns.Count == 1) { column = tableOrCreate.KeyColumns[0]; column.SetAction(local, column.TypeHelper.ConvertFrom(keys[0], column.DataType)); } else { for (int i = 0; i < tableOrCreate.KeyColumns.Count; i++) { column = tableOrCreate.KeyColumns[i]; column.SetAction(local, column.TypeHelper.ConvertFrom(keys[i], column.DataType)); } } if (this._dataAccess.FillAndLock(this._connection, local, wait)) { return(local); } return(default(T)); }
internal static object EntityDataToObject(EntityData data) { Type tableType = TableCache.GetTableType(data.EntityName); object obj2 = Activator.CreateInstance(tableType); foreach (Column column in TableCache.GetTableOrCreate(tableType).Columns) { column.SetAction(obj2, data[column.ColumnName]); } return(obj2); }
private Dictionary <MemberInfo, Column> GetColumnMapper(MappingEntity entity) { Dictionary <MemberInfo, Column> dictionary = new Dictionary <MemberInfo, Column>(); lock (lockObj) { Table tableOrCreate = TableCache.GetTableOrCreate(entity.EntityType); foreach (Column column in tableOrCreate.Columns) { dictionary.Add(column.PropertyInfo, column); } } return(dictionary); }
private bool LoadReferenceNavigation <T>(T obj, PropertyInfo prop, KingReferenceAttribute attr) { object obj3; object obj2 = TableCache.GetTableOrCreate(typeof(T)).GetColumnByProperty(attr.RefPropertyName).GetFunction(obj); if (obj2 == null) { obj3 = null; } else { obj3 = this.FindById(prop.PropertyType, new object[] { obj2 }); } prop.SetValue(obj, obj3); return(obj3 == null); }
public override void CreateTable(DbConnection conn, Type type) { Table tableOrCreate = TableCache.GetTableOrCreate(type); StringBuilder builder = new StringBuilder(0x800); builder.AppendFormat("CREATE TABLE {0}(", tableOrCreate.TableName); foreach (Column column in tableOrCreate.Columns) { builder.AppendFormat(" {0} {1} {2} NULL, ", column.ColumnName, column.GetOracleTypeString(), column.IsPrimaryKey ? "NOT" : string.Empty); } string tableName = tableOrCreate.TableName; if (tableOrCreate.TableName.Length > 0x19) { tableName = tableOrCreate.TableName.Substring(0, 0x19); } builder.AppendFormat(" CONSTRAINT PK_{0} PRIMARY KEY ( ", tableName); int num = -1; foreach (Column column in tableOrCreate.KeyColumns) { num++; if (num > 0) { builder.Append(","); } builder.AppendFormat(" {0} ", column.ColumnName); } builder.AppendFormat(" ))", new object[0]); using (DbCommand command = conn.CreateCommand()) { command.Connection = conn; try { command.CommandType = CommandType.Text; command.CommandText = builder.ToString(); Trace.WriteLine(command.CommandText); command.ExecuteNonQuery(); } finally { command.Connection = null; command.Transaction = null; } } }
/// <summary> /// 收集条件,查询出基本数据 /// </summary> /// <param name="_Report"></param> /// <param name="divCondition"></param> /// <param name="DataHelper"></param> /// <param name="_ReportQueryConditions"></param> /// <returns></returns> private DataTable GetBaseData(Control divCondition) { var _Parameters = new List <DbParameter>(); foreach (var _Condition in _ReportQueryConditions) { var _control = divCondition.FindControl(_Condition.ControlName) as IDrisionControl; if (_control != null) { var _value = _control.GetValue(); //是空值什么都不管,直接传空值查询 if (_value == null || string.IsNullOrEmpty(_value.ToString())) { _value = DBNull.Value; } //不是空值,如果是含子查询则将SysLevelCode查出来传进去,否则_value还是输入框的值 else if (_control.Tag == "IsSubQuery") { //这个控件的FieldName其实存的是FieldId var _Field = _entityCache.FindById <SysField>(_control.FieldName.ToLong()); Type entityType = TableCache.GetTableType(_Field.RefEntity.EntityName); //查出实体 var obj = this._DataHelper.FindById(entityType, _value.ToInt()); //获取SysLevelCode的值 _value = TableCache.GetTableOrCreate(entityType).GetColumnByColumnName("SystemLevelCode").GetFunction(obj); } _Parameters.Add(this._DataHelper.CreateParameter(this._DataHelper.AddPrefixToParameterName(_Condition.ControlName), _value)); } } var strSQL = _Report.SQLScript.Replace("@CurrentUser", this.LoginUser.User_ID.ToString()); //if (LoadEmpty) //{ // var _EntitySchema = Drision.Framework.Manager.IEntitySchemaHelper.Get(this._Report.EntityName); // if (this._ReportQueryConditions.Count == 0 && this._ReportFixedConditions.Count == 0) // { // strSQL = string.Format("{0} where a.{1} = -1", strSQL, _EntitySchema.KeyName); // } // else // { // strSQL = string.Format("{0} and a.{1} = -1", strSQL, _EntitySchema.KeyName); // } //} return(this._DataHelper.ExecuteDataTable(strSQL, _Parameters.ToArray())); }
public void UpdatePartial <T>(Expression <Func <T> > columns) { Table tableOrCreate = TableCache.GetTableOrCreate(typeof(T)); Dictionary <Column, object> tempDict = new Dictionary <Column, object>(); MemberInitExpression body = columns.Body as MemberInitExpression; if (body == null) { throw new ApplicationException("部分更新只支持对象初始化的方式"); } foreach (MemberBinding binding in body.Bindings) { if (binding.BindingType != MemberBindingType.Assignment) { throw new ApplicationException("暂不支持复杂的属性初始化方式"); } MemberAssignment assignment = binding as MemberAssignment; ConstantExpression expression2 = PartialEvaluator.Eval(assignment.Expression) as ConstantExpression; string name = assignment.Member.Name; if (expression2 == null) { throw new ApplicationException(string.Format("无法计算属性{0}的值", name)); } object obj2 = expression2.Value; Column columnByColumnName = tableOrCreate.GetColumnByColumnName(name); if (columnByColumnName == null) { throw new ApplicationException(string.Format("在表{0}中找不到名为{1}的列", tableOrCreate.TableName, name)); } tempDict[columnByColumnName] = obj2; Trace.WriteLine(string.Format("{0}-{1}", name, obj2)); } Dictionary <Column, object> keyDict = tableOrCreate.KeyColumns.ToDictionary <Column, Column, object>(p => p, delegate(Column p) { if (!tempDict.ContainsKey(p)) { throw new ApplicationException(string.Format("未给主键字段{0}赋值", p.ColumnName)); } return(tempDict[p]); }); Dictionary <Column, object> valueDict = (from p in tempDict where !p.Key.IsPrimaryKey select p).ToDictionary <KeyValuePair <Column, object>, Column, object>(p => p.Key, p => p.Value); this.InternalUpdatePartial(typeof(T), keyDict, valueDict); }
internal static EntityData ObjectToEntityData(object obj, SysEntity entity) { if (obj == null) { throw new ArgumentNullException("obj"); } Table tableOrCreate = TableCache.GetTableOrCreate(obj.GetType()); EntityData data = new EntityData(entity); foreach (Column column in tableOrCreate.Columns) { data[column.ColumnName] = column.GetFunction(obj); } object obj2 = tableOrCreate.KeyColumns[0].GetFunction(obj); data.ObjectId = new int?(Convert.ToInt32(obj2)); return(data); }
public override void DeleteAll(DbConnection conn, Type type) { Table tableOrCreate = TableCache.GetTableOrCreate(type); DbCommand command = new OracleCommand(string.Format("delete from {0}", tableOrCreate.TableName)) { Connection = (OracleConnection)conn }; try { Trace.WriteLine(command.CommandText); command.ExecuteNonQuery(); } finally { command.Connection = null; } }
public override void Truncate(DbConnection conn, Type type) { Table tableOrCreate = TableCache.GetTableOrCreate(type); DbCommand command = new OracleCommand { Connection = (OracleConnection)conn }; try { command.CommandType = CommandType.Text; command.CommandText = string.Format("truncate table {0}", tableOrCreate.TableName); command.ExecuteNonQuery(); } finally { command.Connection = null; command.Transaction = null; } }
public void UpdatePartial(object obj, List <string> columns) { Dictionary <Column, object> valueDict = new Dictionary <Column, object>(); Table tableOrCreate = TableCache.GetTableOrCreate(obj.GetType()); foreach (string str in columns) { Column columnByColumnName = tableOrCreate.GetColumnByColumnName(str); if (columnByColumnName == null) { throw new ApplicationException(string.Format("在表{0}中找不到名为{1}的列", tableOrCreate.TableName, str)); } object obj2 = columnByColumnName.GetFunction(obj); valueDict[columnByColumnName] = obj2; Trace.WriteLine(string.Format("{0}-{1}", str, obj2)); } Dictionary <Column, object> keyDict = tableOrCreate.KeyColumns.ToDictionary <Column, Column, object>(p => p, p => p.GetFunction(obj)); this.InternalUpdatePartial(obj.GetType(), keyDict, valueDict); }
private void LoadCollectionNavigation <T>(T obj, PropertyInfo prop) { object[] customAttributes = prop.GetCustomAttributes(typeof(KingCollectionAttribute), false); if (customAttributes.Length <= 0) { throw new ApplicationException("未指定KingReference或KingCollection属性"); } KingCollectionAttribute attribute = customAttributes[0] as KingCollectionAttribute; Table tableOrCreate = TableCache.GetTableOrCreate(typeof(T)); if (tableOrCreate.KeyColumns.Count > 1) { throw new ApplicationException("只支持单主键的集合属性"); } object firstKeyValue = tableOrCreate.GetFirstKeyValue(obj); Type type = prop.PropertyType.GetGenericArguments()[0]; Column columnByProperty = TableCache.GetTableOrCreate(type).GetColumnByProperty(attribute.RefPropertyName); IList list = this._dataAccess.FilterWithOneField(this._connection, type, columnByProperty.ColumnName, firstKeyValue); if (prop.CanWrite) { prop.SetValue(obj, list); } else { object obj3 = prop.GetValue(obj); if (obj3 == null) { throw new ApplicationException(string.Format("属性{0}不可写,并且为空.", prop)); } IList list2 = obj3 as IList; if (obj3 == null) { throw new ApplicationException(string.Format("属性{0}未实现IList", new object[0])); } foreach (object obj4 in list) { list2.Add(obj4); } } }
public override List <ColumnWrapper> GetSortedColumns(DbConnection conn, Type type) { List <ColumnWrapper> sortedColumns; Table tableOrCreate = TableCache.GetTableOrCreate(type); string str = string.Format("select * from {0} where rownum = 1", tableOrCreate.TableName); using (DbCommand command = conn.CreateCommand()) { command.CommandText = str; command.CommandType = CommandType.Text; try { using (DbDataReader reader = command.ExecuteReader()) { sortedColumns = BaseDataAccess.GetSortedColumns(type, reader); } } finally { command.Connection = null; } } return(sortedColumns); }
public override IEnumerable <MemberInfo> GetPrimaryKeyMembers(MappingEntity entity) { return(TableCache.GetTableOrCreate(entity.EntityType).KeyMembers); }
public override string GetTableId(Type type) { return(TableCache.GetTableOrCreate(type).TableName); }
public override MappingEntity GetEntity(Type type) { Table tableOrCreate = TableCache.GetTableOrCreate(type); return(this.GetEntity(type, tableOrCreate.TableName)); }
public override string GetTableName(MappingEntity entity) { Table tableOrCreate = TableCache.GetTableOrCreate(entity.EntityType); return(string.Format("{0}.{1}", tableOrCreate.SchemaName, tableOrCreate.TableName)); }