Exemplo n.º 1
0
        public static void Reset(this object self, ObjectMapping mapping = null)
        {
            var prop       = self.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var properties = prop.Where(c => typeof(IField).IsAssignableFrom(c.PropertyType))
                             .ToArray()
            ;

            foreach (var item in properties)
            {
                var field = item.GetValue(self) as IField;

                if (field == null)
                {
                    continue;
                }

                if (field.HasModified)
                {
                    field.Reset();
                }

                if (mapping != null)
                {
                    field.Properties = mapping.IndexByName[item.Name];
                }
            }
        }
Exemplo n.º 2
0
 public PredicateVisitor(StringBuilder sb, List <DbParameter> arguments, IQueryPredicateGenerator generator, ObjectMapping mapping, SqlManager manager)
 {
     _sb        = sb;
     _arguments = arguments;
     _generator = generator;
     _mapping   = mapping;
     _manager   = manager;
 }
        public static QuerySqlCommand Remove(object instance, ObjectMapping mapping)
        {
            QuerySqlCommand result = new QuerySqlCommand()
            {
                Kind  = QueryKindEnum.Delete,
                Table = mapping.TableName,
            };

            foreach (PropertyMapping field in mapping.Fields)
            {
                if (field.IsPrimaryKey)
                {
                    Field _field = new Field()
                    {
                        Name         = field.Name,
                        VariableName = field.VariableName,
                        DbType       = field.DbType,
                        Value        = new FieldValue(),
                    };

                    var value = field.GetValue(instance);
                    if (value is IField f)
                    {
                        if (f.IsDefaultValue(f.OldValue))
                        {
                            _field.Value.Data = f.Value;
                        }
                        else
                        {
                            _field.Value.Data = f.OldValue;
                        }
                    }

                    result.FilterFields.Add(_field);
                }
                else if (field.SecurityCoherence)
                {
                    Field _field = new Field()
                    {
                        Name         = field.Name,
                        DbType       = field.DbType,
                        VariableName = field.VariableName,
                        Value        = new FieldValue(),
                    };

                    var value = field.GetValue(instance);
                    if (value is IField f)
                    {
                        _field.Value.Data = f.OldValue;
                    }

                    result.FilterFields.Add(_field);
                }
            }

            return(result);
        }
        public static QuerySqlCommand Insert(object instance, ObjectMapping mapping)
        {
            var result = new QuerySqlCommand()
            {
                Kind  = QueryKindEnum.Insert,
                Table = mapping.TableName,
            };

            List <DbParameter> _arguments = new List <DbParameter>();

            foreach (PropertyMapping field in mapping.Fields)
            {
                var _field = new Field()
                {
                    Name         = field.FieldName,
                    VariableName = field.VariableName,
                    DbType       = field.DbType,
                    Value        = new FieldValue()
                };

                result.Fields.Add(_field);

                if (field.SecurityCoherence)
                {
                    if (field.Type == typeof(Guid))
                    {
                        if (Guid.Empty.Equals(field.GetValue(instance)))
                        {
                            field.SetValue(instance, Guid.NewGuid());
                        }
                    }
                    else if (field.Type == typeof(FieldValue <Guid>))
                    {
                        var _f = field.GetValue(instance) as IField;
                        if (Guid.Empty.Equals(_f.Value))
                        {
                            _f.Value = Guid.NewGuid();
                        }
                    }
                }

                if (field.LastChangeDate)
                {
                    _field.Value = FieldValue.CURRENT_TIMESTAMP;
                }

                else
                {
                    _field.Value.Data = ReadValue(instance, field);
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        public IEnumerable <T> Select <T>(params Expression <Func <T, bool> >[] e)
            where T : new()
        {
            ObjectMapping mapping = GetMapping(typeof(T));

            var query = _generator.Select(mapping, e);

            var result = _manager.Read <T>(query.CommandText.ToString(), mapping, query.Arguments);

            return(result);
        }
Exemplo n.º 6
0
        public IEnumerable <T> Read <T>(string sql, ObjectMapping mapping, params DbParameter[] parameters)
            where T : new()
        {
            List <T> _results = new List <T>();

            bool         initialized = false;
            DbConnection cnx         = null;

            if (_cnx != null)
            {
                cnx = _cnx;
            }
            else
            {
                cnx = GetConnection();
                cnx.Open();
                initialized = true;
            }

            try
            {
                using (var cmd = GetCommand(sql, cnx))
                {
                    if (parameters != null)
                    {
                        foreach (var parameter in parameters)
                        {
                            cmd.Parameters.Add(parameter);
                        }
                    }

                    var ctx = new DbDataReaderContext();

                    foreach (DbDataReader item in GetReader(cmd))
                    {
                        ctx.Reader = item;
                        T result = mapping.Create <T>(ctx);
                        mapping.Map <T>(ctx, result);
                        result.Reset(mapping);
                        _results.Add(result);
                    }
                }
            }
            finally
            {
                if (initialized)
                {
                    cnx.Dispose();
                }
            }

            return(_results);
        }
Exemplo n.º 7
0
 public static ObjectMapping GetMapping(Type type)
 {
     if (!_mappings.TryGetValue(type, out ObjectMapping mapping))
     {
         lock (_lock)
             if (!_mappings.TryGetValue(type, out mapping))
             {
                 _mappings.Add(type, mapping = new ObjectMapping(type));
                 mapping.Build();
             }
     }
     return(mapping);
 }
Exemplo n.º 8
0
        public bool Insert <T>(T item)
        {
            ObjectMapping mapping = GetMapping(typeof(T));

            mapping.Validate(item);

            var          queryModel = ExtractQueryGenerator.Insert(item, mapping);
            QueryCommand query      = _generator.Generate(queryModel);
            var          result     = _manager.Update(query.CommandText.ToString(), query.Arguments);

            if (result)
            {
                item.Reset();
            }

            return(result);
        }
Exemplo n.º 9
0
        public bool Update <T>(T item)
        {
            ObjectMapping mapping = GetMapping(typeof(T));

            mapping.Validate(item);

            var          queryModel = ExtractQueryGenerator.Update(item, mapping);
            QueryCommand query      = _generator.Generate(queryModel);

            bool result = false;

            if (query != null && query.CommandText.Length > 0)
            {
                result = _manager.Update(query.CommandText.ToString(), query.Arguments);

                if (result)
                {
                    item.Reset();
                }
            }

            return(result);
        }
        public static QuerySqlCommand Update(object instance, ObjectMapping mapping)
        {
            QuerySqlCommand result = new QuerySqlCommand()
            {
                Kind  = QueryKindEnum.Update,
                Table = mapping.TableName
            };

            List <Field> _fields = new List <Field>();

            var c = instance.Changed().ToDictionary(c1 => c1.Item1);

            foreach (PropertyMapping field in mapping.Fields)
            {
                var _field = new Field()
                {
                    Name         = field.Name,
                    VariableName = field.VariableName,
                    DbType       = field.DbType,
                    Value        = new FieldValue(),
                };

                var    value = field.GetValue(instance);
                IField f1    = value as IField;

                if (f1 != null)
                {
                    _field.Value.Data    = f1.Value;
                    _field.Value.OldData = f1.OldValue;
                }
                else
                {
                    _field.Value.OldData = _field.Value.Data = value;
                }

                if (field.IsPrimaryKey)
                {
                    if (field.IsGuid() && f1 != null && (Guid)f1.Value == Guid.Empty)
                    {
                        _field.Value.Data    = f1.Value = Guid.NewGuid();
                        _field.Value.OldData = f1.OldValue;
                    }

                    result.FilterFields.Add(new Field()
                    {
                        Name         = _field.Name,
                        DbType       = field.DbType,
                        VariableName = field.VariableName,
                        Value        = new FieldValue()
                        {
                            Data = _field.Value.OldData,
                        }
                    });

                    _field.VariableName = f1.HasModified ? "new" + field.FieldName : field.VariableName;

                    if (f1.HasModified)
                    {
                        _fields.Add(_field);
                    }
                }
                if (field.SecurityCoherence)
                {
                    if (field.IsGuid() && f1 != null && !f1.HasModified)
                    {
                        _field.Value.Data    = f1.Value = Guid.NewGuid();
                        _field.Value.OldData = f1.OldValue;
                    }

                    result.FilterFields.Add(new Field()
                    {
                        Name         = _field.Name,
                        DbType       = field.DbType,
                        VariableName = field.VariableName,
                        Value        = new FieldValue()
                        {
                            Data = _field.Value.OldData,
                        }
                    });

                    _field.VariableName = f1.HasModified ? "new" + field.FieldName : field.VariableName;
                    _fields.Add(_field);
                }
                else if (field.LastChangeDate)
                {
                    _field.Value = FieldValue.CURRENT_TIMESTAMP;
                    _fields.Add(_field);
                }

                else if (c.ContainsKey(field.Name))
                {
                    result.Fields.Add(_field);
                }
            }

            if (result.Fields.Count > 0)
            {
                result.Fields.AddRange(_fields);
            }

            return(result);
        }