Exemplo n.º 1
0
        public void Set(Expression <Func <T, string[]> > setColumn, object[] values)
        {
            string columeNames = setColumn.Body.ToString();
            int    startIndex  = columeNames.IndexOf("{", StringComparison.Ordinal) + 1;
            int    endIndex    = columeNames.IndexOf("}", StringComparison.Ordinal);

            columeNames = columeNames.Substring(startIndex, endIndex - startIndex);
            columeNames = columeNames.Replace("&&", "AND").Replace("(", "").Replace(")", "");
            int    pointIndex   = columeNames.IndexOf(".", StringComparison.Ordinal) + 1;
            string proprotyName = columeNames.Substring(0, pointIndex);

            columeNames = columeNames.Replace(proprotyName, "");
            string[] properties = columeNames.Split(',');
            string   sql        = string.Empty;

            for (int index = 0; index < properties.Length; index++)
            {
                var writeFunc = Orm.GetWriteFunc(values[index].GetType());
                sql += properties[index] + "=" + writeFunc(values[index]);
                if (index < properties.Length - 1)
                {
                    sql += ",";
                }
            }
            m_commandText += string.Format(" SET {0}", sql);
        }
Exemplo n.º 2
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);
        }