예제 #1
0
        protected string GetColumnsValue(object obj, out string[] columeNames, out string[] columeValues)
        {
            Type type = obj.GetType();

            FieldInfo[]        fieldInfos = type.GetFields();
            IList <CellMapper> cells      = new List <CellMapper>();

            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                var columeAtt = fieldInfo.AttributeOf <ColumnAttribute>();
                if (null != columeAtt)
                {
                    CellMapper cell = new CellMapper();
                    cell.columeName = columeAtt.columnName;
                    if (string.IsNullOrEmpty(columeAtt.columnName))
                    {
                        cell.columeName = fieldInfo.Name;
                    }
                    cell.dbType  = columeAtt.columnType;
                    cell.type    = fieldInfo.FieldType;
                    cell.notNull = columeAtt.columeNotNull;
                    var primaryKeyAtt = fieldInfo.AttributeOf <PrimaryKeyAttribute>();
                    cell.isPrimaryKey = null != primaryKeyAtt;
                    if (columeAtt.columnType == (DbType.Int32 | DbType.Int64 | DbType.Int16))
                    {
                        var autoIncrease = fieldInfo.AttributeOf <AutoIncrementAttribute>();
                        cell.isAutoIncrease = null != autoIncrease;
                    }
                    cell.value = fieldInfo.GetValue(obj);
                    cells.Add(cell);
                }
            }

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                var columeAtt = propertyInfo.AttributeOf <ColumnAttribute>();
                if (null != columeAtt)
                {
                    CellMapper cell = new CellMapper();
                    cell.columeName = columeAtt.columnName;
                    if (string.IsNullOrEmpty(columeAtt.columnName))
                    {
                        cell.columeName = propertyInfo.Name;
                    }
                    cell.dbType  = columeAtt.columnType;
                    cell.type    = propertyInfo.PropertyType;
                    cell.notNull = columeAtt.columeNotNull;
                    var primaryKeyAtt = propertyInfo.AttributeOf <PrimaryKeyAttribute>();
                    cell.isPrimaryKey = null != primaryKeyAtt;
                    if (columeAtt.columnType == (DbType.Int32 | DbType.Int64 | DbType.Int16))
                    {
                        var autoIncrease = propertyInfo.AttributeOf <AutoIncrementAttribute>();
                        cell.isAutoIncrease = null != autoIncrease;
                    }
                    cell.value = propertyInfo.GetValue(obj, null);
                    cells.Add(cell);
                }
            }

            columeNames  = new string[cells.Count];
            columeValues = new string[cells.Count];
            for (int i = 0; i < columeNames.Length; i++)
            {
                columeNames[i]  = cells[i].columeName;
                columeValues[i] = Orm.GetWriteFunc(cells[i].type)(cells[i].value).ToString();
            }

            TableAttribute tableAtt = type.AttributeOf <TableAttribute>();

            if (null != tableAtt)
            {
                return(tableAtt.tableName);
            }
            return(type.Name);
        }
예제 #2
0
        protected string GetColumes(Type type, out string[] columeNames, out string[] columeTypes)
        {
            FieldInfo[]        fieldInfos = type.GetFields();
            IList <CellMapper> cells      = new List <CellMapper>();

            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                var columeAtt = fieldInfo.AttributeOf <ColumnAttribute>();
                if (null != columeAtt)
                {
                    CellMapper cell = new CellMapper();
                    cell.columeName = columeAtt.columnName;
                    if (string.IsNullOrEmpty(columeAtt.columnName))
                    {
                        cell.columeName = fieldInfo.Name;
                    }
                    cell.dbType  = columeAtt.columnType;
                    cell.notNull = columeAtt.columeNotNull;
                    var primaryKeyAtt = fieldInfo.AttributeOf <PrimaryKeyAttribute>();
                    cell.isPrimaryKey = null != primaryKeyAtt;
                    if (columeAtt.columnType == (DbType.Int32 | DbType.Int64 | DbType.Int16))
                    {
                        var autoIncrease = fieldInfo.AttributeOf <AutoIncrementAttribute>();
                        cell.isAutoIncrease = null != autoIncrease;
                    }
                    cells.Add(cell);
                }
            }

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                var columeAtt = propertyInfo.AttributeOf <ColumnAttribute>();
                if (null != columeAtt)
                {
                    CellMapper cell = new CellMapper();
                    cell.columeName = columeAtt.columnName;
                    if (string.IsNullOrEmpty(columeAtt.columnName))
                    {
                        cell.columeName = propertyInfo.Name;
                    }
                    cell.dbType  = columeAtt.columnType;
                    cell.notNull = columeAtt.columeNotNull;
                    var primaryKeyAtt = propertyInfo.AttributeOf <PrimaryKeyAttribute>();
                    cell.isPrimaryKey = null != primaryKeyAtt;
                    if (columeAtt.columnType == DbType.Int32 || columeAtt.columnType == DbType.Int64 | columeAtt.columnType == DbType.Int16)
                    {
                        var autoIncrease = propertyInfo.AttributeOf <AutoIncrementAttribute>();
                        cell.isAutoIncrease = null != autoIncrease;
                    }
                    cells.Add(cell);
                }
            }

            columeNames = new string[cells.Count];
            columeTypes = new string[cells.Count];
            for (int i = 0; i < columeNames.Length; i++)
            {
                columeNames[i] = cells[i].columeName;
                columeTypes[i] = cells[i].dbType.ToString();
                if (cells[i].isPrimaryKey)
                {
                    columeTypes[i] += " PRIMARY KEY";
                }
                if (cells[i].isAutoIncrease)
                {
                    columeTypes[i] += "AUTOINCREMENT";
                }
            }

            TableAttribute tableAtt = type.AttributeOf <TableAttribute>();

            if (null != tableAtt)
            {
                return(tableAtt.tableName);
            }
            return(type.Name);
        }