private string GetTableName <T>() { Attribute attr = Attribute.GetCustomAttribute(typeof(T), typeof(TableNameAttribute)); TableNameAttribute tn = (TableNameAttribute)attr; return(tn.Value); }
/// <summary> /// Determines the table name to use on the data store /// </summary> /// <param name="type">The type</param> /// <param name="toAdd">What to add the data to</param> protected void ParseTableName(Type type, DatabaseTypeInfo toAdd) { TableNameAttribute tName = type.GetCustomAttributes(typeof(TableNameAttribute), true).FirstOrDefault() as TableNameAttribute; if (tName != null) { toAdd.UnescapedTableName = tName.TableName; toAdd.UnEscapedSchema = tName.Schema; } if (string.IsNullOrEmpty(toAdd.UnEscapedSchema)) { toAdd.UnEscapedSchema = _connection.Connection.DefaultSchema; } if (string.IsNullOrEmpty(toAdd.UnescapedTableName)) { toAdd.UnescapedTableName = GenTableName(type); } //escape table name toAdd.TableName = string.Concat(_connection.Connection.LeftEscapeCharacter, toAdd.UnescapedTableName, _connection.Connection.RightEscapeCharacter); if (!string.IsNullOrEmpty(toAdd.UnEscapedSchema)) { toAdd.Schema = string.Concat(_connection.Connection.LeftEscapeCharacter, toAdd.UnEscapedSchema, _connection.Connection.RightEscapeCharacter); } }
string GetTableName(IEntity e) { Type t = e.GetType(); TableNameAttribute tableNameAtt = t.GetTypeInfo().GetCustomAttribute <TableNameAttribute>(); return(tableNameAtt != null ? tableNameAtt.TableName : t.Name + "s"); }
public virtual bool Delete <T>(long keyField) { Type typeFromHandle = typeof(T); TableNameAttribute tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(typeFromHandle, typeof(TableNameAttribute)); PropertyInfo[] array = HiCache.Get <PropertyInfo[]>($"Table-PropertyInfo-{tableNameAttribute.TableName}"); if (array == null) { array = typeFromHandle.GetProperties(BindingFlags.Instance | BindingFlags.Public); HiCache.Insert($"Table-PropertyInfo-{tableNameAttribute.TableName}", array); } DbCommand sqlStringCommand = this.database.GetSqlStringCommand(" "); string text = ""; PropertyInfo[] array2 = array; foreach (PropertyInfo propertyInfo in array2) { object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(FieldTypeAttribute), true); if (customAttributes.Length != 0 && this.IsKeyField(customAttributes)) { text = text + propertyInfo.Name + "=@" + propertyInfo.Name; this.database.AddInParameter(sqlStringCommand, propertyInfo.Name, this.GetDbType(propertyInfo.PropertyType), keyField); break; } } sqlStringCommand.CommandText = "DELETE FROM " + tableNameAttribute.TableName + " WHERE " + text; return(this.database.ExecuteNonQuery(sqlStringCommand) >= 1); }
public static object GetNameByType(Type type, object where = null) { object[] objs = type.GetCustomAttributes(typeof(TableNameAttribute), false); if (objs != null && objs.Length > 0) { TableNameAttribute tableName = (TableNameAttribute)objs[0]; if (tableName != null) { if (!tableName.IsUserDateCreateTable) { return(tableName.Name); } else { string[] tableNames = tableName.Name.Split(new Char[] { '{', '}' }); if (tableNames.Length == 3) { if (where != null) { var property = where.GetType().GetProperty("CreateTime", BindingFlags.IgnoreCase); if (property != null) { DateTime dt = (DateTime)property.GetValue(where, null); return(string.Format("{0}{1}{2}", tableNames[0], dt.ToString(tableNames[1]), tableNames[2])); } } return(string.Format("{0}{1}{2}", tableNames[0], DateTime.Now.ToString(tableNames[1]), tableNames[2])); } } } } return(type.Name); }
public void TableName_Is_As_Expected() { const string tableName = "TestTableName"; var attribute = new TableNameAttribute(tableName); Assert.AreEqual(tableName, attribute.TableName); }
private static object GetNameByObj(object obj) { object[] objs = obj.GetType().GetCustomAttributes(typeof(TableNameAttribute), false); if (objs != null && objs.Length > 0) { TableNameAttribute tableName = (TableNameAttribute)objs[0]; if (tableName != null) { if (!tableName.IsUserDateCreateTable) { return(tableName.Name); } else { string[] tableNames = tableName.Name.Split(new Char[] { '{', '}' }); var fileinfo = obj.GetType().GetProperty("CreateTime"); DateTime dt = DateTime.Now; if (fileinfo != null) { dt = Convert.ToDateTime(fileinfo.GetValue(obj, null)); } if (tableNames.Length == 3) { return(string.Format("{0}{1}{2}", tableNames[0], dt.ToString(tableNames[1]), tableNames[2])); } } } } return(obj.GetType().Name); }
public static string GetNameByType(PropertyInfo info) { var attributes = info.GetCustomAttributes(typeof(ColNameAttribute), false); if (attributes != null && attributes.Length > 0) { ColNameAttribute colName = attributes[0] as ColNameAttribute; if (colName != null) { return(colName.Name ?? info.Name); } attributes = info.GetCustomAttributes(typeof(TableNameAttribute), false); if (attributes != null && attributes.Length > 0) { TableNameAttribute tableName = attributes[0] as TableNameAttribute; if (tableName != null) { if (!tableName.IsUserDateCreateTable) { return(tableName.Name); } else { string[] tableNames = tableName.Name.Split(new Char[] { '{', '}' }); if (tableNames.Length == 3) { return(string.Format("{0}{1}{2}", tableNames[0], DateTime.Now.ToString(tableNames[1]), tableNames[2])); } } } } } return(info.Name); }
static EntityType GetSmallEntityType(Type runtimeType) { if (runtimeType.TypeIsNotCorrect()) { return(null); } return(new EntityType { Type = runtimeType, AssemblyFullName = runtimeType.AssemblyQualifiedName, ClassFullName = runtimeType.FullName, ClassName = runtimeType.Name, TableName = TableNameAttribute.GetTableName(runtimeType), Name = runtimeType.Name.ToPlural(), PluralName = runtimeType.Name.ToPlural(), IsTransient = Attribute.IsDefined(runtimeType, typeof(TransientEntityAttribute)), IsAbstract = runtimeType.GetTypeInfo().IsAbstract, SoftDelete = Attribute.IsDefined(runtimeType, typeof(SoftDeleteAttribute)), HasDataAccessClass = Attribute.IsDefined(runtimeType, typeof(DatabaseGeneratedAttribute)), //HasKnownDatabase = Attribute.IsDefined(runtimeType, typeof(HasKnownDatabaseAttribute)), BaseType = GetSmallEntityType(runtimeType.BaseType), }); }
/// <summary> /// Creates a new TypeDefinition for the POCO Type specified using any attributes on the class to determine mappings. /// </summary> public AttributeBasedTypeDefinition(Type pocoType) { if (pocoType == null) { throw new ArgumentNullException("pocoType"); } _pocoType = pocoType; // Look for supported attributes on the Type and set any properties appropriately PrimaryKeyAttribute primaryKeyAttribute = pocoType.GetCustomAttributes <PrimaryKeyAttribute>(true).FirstOrDefault(); if (primaryKeyAttribute != null) { _primaryKeyColumns = primaryKeyAttribute.ColumnNames; } ExplicitColumnsAttribute explicitColumnsAttribute = pocoType.GetCustomAttributes <ExplicitColumnsAttribute>(true).FirstOrDefault(); if (explicitColumnsAttribute != null) { _explicitColumns = true; } TableNameAttribute tableNameAttribute = pocoType.GetCustomAttributes <TableNameAttribute>(true).FirstOrDefault(); if (tableNameAttribute != null) { _tableName = tableNameAttribute.Value; } }
private string BuildCommandStart() { var command = ""; var tableName = typeof(T).Name; var attrs = typeof(T).GetCustomAttributes(true); foreach (var attr in attrs) { TableNameAttribute name = attr as TableNameAttribute; if (name != null) { tableName = name.Value; } } switch (_transactionType) { case SqlTransactionType.Insert: return(string.Concat(command, "INSERT INTO ", tableName)); case SqlTransactionType.Update: return(string.Concat(command, "UPDATE ", tableName)); case SqlTransactionType.Delete: return(string.Concat(command, "DELETE ", tableName)); } return(""); }
private SchemaName GetSchemaName(TableNameAttribute tn) { ServerName server = tn.ServerName == null ? null : new ServerName(tn.ServerName); DatabaseName dataBase = tn.DatabaseName == null && server == null ? null : new DatabaseName(server, tn.DatabaseName); SchemaName schema = tn.SchemaName == null && dataBase == null ? SchemaName.Default : new SchemaName(dataBase, tn.SchemaName); return(schema); }
public virtual ObjectName GenerateTableName(Type type, TableNameAttribute tn) { SchemaName sn = tn != null?GetSchemaName(tn) : SchemaName.Default; string name = tn?.Name ?? EnumEntity.Extract(type)?.Name ?? Reflector.CleanTypeName(type); return(new ObjectName(sn, name)); }
private void GetTableData() { TableMapping = Mapper.GetTableMapping <T>(); TableNameAttribute tableNameAtt = typeof(T).GetTypeInfo().GetCustomAttribute <TableNameAttribute>(); TableName = tableNameAtt != null ? tableNameAtt.TableName : typeof(T).Name + "s"; alias = TableName.First().ToString().ToLower(); }
public virtual IList <T> Gets <T>(string sortBy, SortAction sortOrder, int?maxNum = default(int?)) where T : new() { Type typeFromHandle = typeof(T); List <T> list = new List <T>(); TableNameAttribute tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(typeFromHandle, typeof(TableNameAttribute)); string text = "SELECT"; if (maxNum.HasValue) { text = text + " TOP " + maxNum.Value; } text = text + " * FROM " + tableNameAttribute.TableName + " ORDER BY " + sortBy + " " + sortOrder.ToString(); DbCommand sqlStringCommand = this.database.GetSqlStringCommand(text); using (IDataReader dataReader = this.database.ExecuteReader(sqlStringCommand)) { while (dataReader.Read()) { T val = new T(); int fieldCount = dataReader.FieldCount; for (int i = 0; i < fieldCount; i++) { if (((IDataRecord)dataReader)[i] != DBNull.Value) { PropertyInfo property = typeFromHandle.GetProperty(dataReader.GetName(i), BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); if (property != (PropertyInfo)null) { Type type = property.PropertyType; if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { NullableConverter nullableConverter = new NullableConverter(type); type = nullableConverter.UnderlyingType; } if (type.IsEnum) { object value = Enum.ToObject(type, ((IDataRecord)dataReader)[i]); property.SetValue(val, value, null); } else { object obj = Convert.ChangeType(((IDataRecord)dataReader)[i], type); if (type.Equals(typeof(string)) && obj == null) { obj = string.Empty; } property.SetValue(val, obj, null); } } } } list.Add(val); } } return(list); }
public Repository() { tableType = typeof(TTable); TableNameAttribute tableNameAttrib = tableType.GetCustomAttribute(typeof(TableNameAttribute), true) as TableNameAttribute; TableName = tableNameAttrib.Name; PropertyInfo[] allTableFieldsProperties = tableType.GetProperties(); SetupTablePrimaryField(allTableFieldsProperties); SetupTableNonPrimaryFields(allTableFieldsProperties); }
internal string GetTableName(Type type) { string tableName = type.Name; TableNameAttribute tableAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(type, typeof(TableNameAttribute)); if (tableAttribute != null) { tableName = tableAttribute.Value; } return(tableName); }
public virtual long Add(object model, DbTransaction dbTran = null) { Type type = model.GetType(); TableNameAttribute tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(type, typeof(TableNameAttribute)); PropertyInfo[] array = HiCache.Get <PropertyInfo[]>($"Table-PropertyInfo-{tableNameAttribute.TableName}"); if (array == null) { array = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); HiCache.Insert($"Table-PropertyInfo-{tableNameAttribute.TableName}", array); } DbCommand sqlStringCommand = this.database.GetSqlStringCommand(" "); string text = ""; string text2 = ""; bool flag = false; PropertyInfo[] array2 = array; foreach (PropertyInfo propertyInfo in array2) { object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(FieldTypeAttribute), true); if (customAttributes.Length != 0) { if (this.IsIncrementField(customAttributes)) { flag = true; } else { text = text + propertyInfo.Name + ","; this.database.AddInParameter(sqlStringCommand, propertyInfo.Name, this.GetDbType(propertyInfo.PropertyType), propertyInfo.GetValue(model, null)); } } } text = text.Remove(text.Length - 1); text2 = "@" + text.Replace(",", ",@"); sqlStringCommand.CommandText = "INSERT INTO " + tableNameAttribute.TableName + "(" + text + ")VALUES(" + text2 + ")"; if (flag) { DbCommand dbCommand = sqlStringCommand; dbCommand.CommandText += " SELECT @@IDENTITY"; if (dbTran != null) { return(this.database.ExecuteScalar(sqlStringCommand, dbTran).ToLong(0)); } return(this.database.ExecuteScalar(sqlStringCommand).ToLong(0)); } if (dbTran != null) { return(this.database.ExecuteNonQuery(sqlStringCommand, dbTran)); } return(this.database.ExecuteNonQuery(sqlStringCommand)); }
//通过反射获取表名 public static string GetName(Type type) { if (type.IsDefined(typeof(TableNameAttribute), true)) { TableNameAttribute attribute = (TableNameAttribute)type.GetCustomAttribute(typeof(TableNameAttribute), true); return(attribute.GetTableName()); } else { return(type.Name); } }
public virtual int GetMaxDisplaySequence <T>() where T : new() { Type typeFromHandle = typeof(T); TableNameAttribute tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(typeFromHandle, typeof(TableNameAttribute)); DbCommand sqlStringCommand = this.database.GetSqlStringCommand(" "); if (!string.IsNullOrEmpty(tableNameAttribute.TableName)) { sqlStringCommand.CommandText = "select isnull(max(DisplaySequence),0)+1 from " + tableNameAttribute.TableName; return((int)this.database.ExecuteScalar(sqlStringCommand)); } return(0); }
public override ObjectName GenerateTableName(Type type, TableNameAttribute tn) { if (tn != null) { if (tn.SchemaName == "sys") { DatabaseName db = Administrator.sysViewDatabase.Value; return(new ObjectName(new SchemaName(db, tn.SchemaName ?? "dbo"), tn.Name)); } } return(base.GenerateTableName(type, tn)); }
/// <summary> /// 获取数据库名 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static string GetTableName <T>(T data) { string tableName = string.Empty; //数据库名 var sd = data.GetType().GetCustomAttributes(true); for (int i = 0; i < sd.Count(); i++) { if (sd.GetValue(i).GetType() == typeof(TableNameAttribute)) { TableNameAttribute tableNameTmp = sd[i] as TableNameAttribute; tableName = tableNameTmp.tableName; } } return(tableName); }
internal static DataProviderMetaData Generate(Type type) { var tableName = TableNameAttribute.GetTableName(type); return(new DataProviderMetaData(type) { BaseClassTypesInOrder = GetParents(type), DrivedClassTypes = GetDrivedClasses(type), Properties = GetProperties(type).ToArray(), Schema = SchemaAttribute.GetSchema(type), TableName = tableName, TableAlias = tableName, IsSoftDeleteEnabled = SoftDeleteAttribute.IsEnabled(type, inherit: false) }); }
/// <summary> /// 读取表名 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="t">参数</param> /// <returns></returns> public string GetTableName <T>(T t) { Type type = t.GetType(); var tableName = string.Empty; if (type.IsDefined(typeof(TableNameAttribute), true)) //判断该方法是否被PrintLogAttribute标记 { TableNameAttribute attribute = type.GetCustomAttribute(typeof(TableNameAttribute)) as TableNameAttribute; //实例化PrintLogAttribute tableName = attribute.Name; } else { tableName = type.Name; } return(tableName); }
static AutoAssignedRecord() { AutoAssignedRecord <T> .logger = LogManager.GetCurrentClassLogger(); if (AutoAssignedRecord <T> .IdProvider == null) { System.Type typeFromHandle = typeof(T); TableNameAttribute customAttribute = typeFromHandle.GetCustomAttribute <TableNameAttribute>(); if (customAttribute == null) { AutoAssignedRecord <T> .logger.Error("TableNameAttribute not found in {0}", typeFromHandle.Name); } else { AutoAssignedRecord <T> .IdProvider = new PrimaryKeyIdProvider("Id", customAttribute.TableName); } } }
public static string GetTableName <T>() { var name = ""; System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(T)); // Displaying output. foreach (System.Attribute attr in attrs) { if (attr is TableNameAttribute) { TableNameAttribute a = (TableNameAttribute)attr; name = a.GetName(); } } return(name); }
public string GetTableName(Type obj) { string result = ""; string tableNmae = obj.FullName; if (CacheHelper.CachePool.Exists(tableNmae)) { result = CacheHelper.CachePool.Get(tableNmae) as string; } else { TableNameAttribute attribute = obj.GetCustomAttribute(typeof(TableNameAttribute)) as TableNameAttribute; CacheHelper.CachePool.Set(tableNmae, attribute.Name, TimeSpan.MaxValue, true); result = CacheHelper.CachePool.Get(tableNmae) as string; } this.TableName = result; return(result); }
private void GetTableData() { TableMapping = new Dictionary <string, string>(); PropertyInfo[] properties = typeof(T).GetTypeInfo().GetProperties(); foreach (PropertyInfo p in properties) { NotMappedAttribute nm = p.GetCustomAttribute <NotMappedAttribute>(); if (nm == null) { ColumnNameAttribute cma = p.GetCustomAttribute <ColumnNameAttribute>(); TableMapping.Add(p.Name, cma != null ? cma.ColumnName : p.Name); } } TableNameAttribute tableNameAtt = typeof(T).GetTypeInfo().GetCustomAttribute <TableNameAttribute>(); TableName = tableNameAtt != null ? tableNameAtt.TableName : typeof(T).Name + "s"; alias = TableName.First().ToString().ToLower(); }
/// <summary> /// 获取表名 /// </summary> /// <returns></returns> private string GetTableName() { Type type = typeof(T); string tableName = string.Empty; //数据库名 var sd = type.GetCustomAttributes(true); for (int i = 0; i < sd.Count(); i++) { if (sd.GetValue(i).GetType().Name == "TableNameAttribute") { TableNameAttribute tableNameTmp = sd[i] as TableNameAttribute; if (tableNameTmp != null) { tableName = tableNameTmp.TableName; } } } return(tableName); }
public virtual bool Update(object model, DbTransaction dbTran = null) { Type type = model.GetType(); TableNameAttribute tableNameAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(type, typeof(TableNameAttribute)); PropertyInfo[] array = HiCache.Get <PropertyInfo[]>($"Table-PropertyInfo-{tableNameAttribute.TableName}"); if (array == null) { array = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); HiCache.Insert($"Table-PropertyInfo-{tableNameAttribute.TableName}", array); } DbCommand sqlStringCommand = this.database.GetSqlStringCommand(" "); string text = ""; string text2 = ""; PropertyInfo[] array2 = array; foreach (PropertyInfo propertyInfo in array2) { object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(FieldTypeAttribute), true); if (customAttributes.Length != 0) { if (this.IsKeyField(customAttributes)) { text2 = text2 + propertyInfo.Name + "=@" + propertyInfo.Name + " AND "; } else { text = text + propertyInfo.Name + "=@" + propertyInfo.Name + ","; } this.database.AddInParameter(sqlStringCommand, propertyInfo.Name, this.GetDbType(propertyInfo.PropertyType), propertyInfo.GetValue(model, null)); } } text = text.Remove(text.Length - 1); text2 = text2.Remove(text2.Length - 4); sqlStringCommand.CommandText = "UPDATE " + tableNameAttribute.TableName + " SET " + text + " WHERE " + text2; if (dbTran == null) { return(this.database.ExecuteNonQuery(sqlStringCommand) >= 1); } return(this.database.ExecuteNonQuery(sqlStringCommand, dbTran) >= 1); }
public override ObjectName GenerateTableNameCollection(Table table, NameSequence name, TableNameAttribute tn) { return base.GenerateTableNameCollection(table, name, tn).OnSchema(GetSchemaName(table.Type)); }
public override ObjectName GenerateTableName(Type type, TableNameAttribute tn) { return base.GenerateTableName(type, tn).OnSchema(GetSchemaName(type)); }