Beispiel #1
0
        public FluentEntityAdoSelect(IDaoHelper daoHelper, bool allColumns, bool autoReset)
        {
            _daoHelper           = daoHelper ?? throw new ArgumentNullException(nameof(daoHelper));
            _whereHelper         = new FluentEntityAdoWhereHelper <T>(autoReset);
            _joinCommandBuilder  = new StringBuilder();
            _orderCommandBuilder = new StringBuilder();

            _tableName  = FluentEntityAdoHelper.GetTableName(typeof(T));
            _tableNames = new HashSet <string>
            {
                _tableName
            };
            _selectedColumns = new Dictionary <string, List <FluentEntityProperty <EntityColumnAttribute> > >
            {
                { _tableName, new List <FluentEntityProperty <EntityColumnAttribute> >() }
            };
            _joinedTableObjects = new Dictionary <string, object>();

            _referenceModel = new T();
            _allColumns     = allColumns;
            _autoReset      = autoReset;

            if (allColumns)
            {
                AddAllColumns <T>(_tableName);
            }
        }
Beispiel #2
0
 private void AddAllColumns <TColumn>(string tableName)
 {
     foreach (var entry in FluentEntityAdoHelper.GetAllColumnProperties(typeof(TColumn)))
     {
         AddColumnData(tableName, entry);
     }
 }
Beispiel #3
0
        private string GetColumnNameWithAs(string source, string key)
        {
            var name   = FluentEntityAdoHelper.ConcatTableColumn(source, key);
            var nameAs = FluentEntityAdoHelper.ConcatTableColumnAs(source, key);

            return($"{name} AS {nameAs}");
        }
Beispiel #4
0
 public FluentEntityAdoUpdate(IDaoHelper daoHelper, IEnumerable <T> entities)
 {
     _daoHelper  = daoHelper ?? throw new ArgumentNullException(nameof(daoHelper));
     _properties = FluentEntityAdoHelper.GetAllColumnProperties(typeof(T)).ToList();
     _commands   = new Dictionary <string, IList <IList <QueryParameter> > >
     {
         { PrepareBaseCommand(), PrepareQueryParameters(entities) }
     };
 }
Beispiel #5
0
 public FluentEntityAdoInsert(IDaoHelper daoHelper, IEnumerable <T> entities)
 {
     _daoHelper      = daoHelper ?? throw new ArgumentNullException(nameof(daoHelper));
     _properties     = FluentEntityAdoHelper.GetAllColumnProperties(typeof(T)).ToList();
     _commandBuilder = new StringBuilder();
     _inserts        = new Dictionary <string, IList <IList <QueryParameter> > >();
     _baseCommand    = $"INSERT INTO {FluentEntityAdoHelper.GetTableName(typeof(T))} (";
     ParseCommands(entities);
 }
Beispiel #6
0
        private string PrepareBaseCommand()
        {
            var commandBuilder = new StringBuilder();
            var tableName      = FluentEntityAdoHelper.GetTableName(typeof(T));

            commandBuilder.Append($"{UpdateCommand} {tableName} {SetCommand} ");
            _properties.Perform(property => commandBuilder.Append($"{property.Attribute.Name} = @{property.Attribute.Name}, "), IsNotKey);
            commandBuilder.Remove(commandBuilder.Length - Offset, Offset);
            commandBuilder.Append($" {WhereCommand} {tableName}.id = @{Id} AND {tableName}.row_version = @{LastUpdate}");
            return(commandBuilder.ToString());
        }
Beispiel #7
0
        private IFluentEntityOrderBy <T> AddOrderBy <TKey>(Expression <Func <T, TKey> > key, string order)
        {
            if (!_isFirstOrder)
            {
                _orderCommandBuilder.Append(" ,");
            }
            _isFirstOrder = false;

            _orderCommandBuilder.Append($" {FluentEntityAdoHelper.GetColumnPropertyNameQualified(key)} {order}");
            return(this);
        }
Beispiel #8
0
        private IList <QueryParameter> GetQueryParams(object entity)
        {
            var queryParams = new List <QueryParameter>(_properties.Count);

            _properties.Perform(property =>
            {
                var value = FluentEntityAdoHelper.GetValue(property, entity);
                queryParams.Add(new QueryParameter(property.Attribute.Name, value));
            }, PostAppend, CanAppend, entity);
            return(queryParams);
        }
Beispiel #9
0
        private IList <QueryParameter> MapQueryParams(T entity)
        {
            var queryParams = new List <QueryParameter>(_properties.Capacity);

            _properties.Perform(property =>
            {
                var value = FluentEntityAdoHelper.GetValue(property, entity);
                queryParams.Add(new QueryParameter(property.Attribute.Name, value));
            });
            queryParams.Add(new QueryParameter(LastUpdate, entity.RowVersion));
            return(queryParams);
        }
Beispiel #10
0
 private static object ConvertDbValue(object value, FluentEntityProperty <EntityColumnAttribute> column)
 {
     if (value == DBNull.Value)
     {
         value = null;
     }
     else if (FluentEntityAdoHelper.IsBoolean(column))
     {
         value = Convert.ToBoolean(value); //we need to ensure that uint64 is properly converted to bool before we apply it to the model
     }
     return(value);
 }
Beispiel #11
0
        public async Task <IEnumerable <T> > QueryAsync()
        {
            var columns = FluentEntityAdoHelper.JoinEnumerable(
                _selectedColumns.SelectMany(keyValue =>
                                            keyValue.Value.Select(value =>
                                                                  GetColumnNameWithAs(keyValue.Key, value.Attribute.Name))),
                ", ");
            var sqlCommand = $"SELECT {columns} FROM {_tableName} {_joinCommandBuilder} {_whereHelper.CommandWhere} {_orderCommandBuilder}";
            var result     = await _daoHelper.SelectAsync <T>(sqlCommand, MapObject);

            if (_autoReset)
            {
                Reset();
            }

            return(result);
        }
Beispiel #12
0
        private T MapObject(IDataRecord record)
        {
            foreach (var tableName in _tableNames)
            {
                var currentModel = _joinedTableObjects.ContainsKey(tableName) ?
                                   _joinedTableObjects[tableName] :
                                   _referenceModel;

                if (_selectedColumns.ContainsKey(tableName))
                {
                    foreach (var column in _selectedColumns[tableName])
                    {
                        var field = FluentEntityAdoHelper.ConcatTableColumnAs(tableName, column.Attribute.Name);
                        var value = record[field];
                        column.Property.SetValue(currentModel, ConvertDbValue(value, column));
                    }
                }
            }

            return((T)_referenceModel.Clone());
        }
Beispiel #13
0
        private IFluentEntityJoin <T> GenericJoin <TJoin1, TJoin2, TKey1, TKey2>(
            Expression <Func <T, TJoin2> > property,
            Expression <Func <TJoin1, TKey1> > onColumn1,
            Expression <Func <TJoin2, TKey2> > onColumn2,
            string joinCommand) where TJoin2 : new()
        {
            FluentEntityProperty <EntityColumnRefAttribute> joinedProperty = null;

            if (property != null)
            {
                joinedProperty = FluentEntityAdoHelper.GetColumnRefProperty(property);
            }
            var tableSource         = FluentEntityAdoHelper.GetTableName(typeof(TJoin1));
            var tableDestination    = FluentEntityAdoHelper.GetTableName(typeof(TJoin2));
            var propertySource      = FluentEntityAdoHelper.ConcatTableColumn(tableSource, FluentEntityAdoHelper.GetColumnPropertyName(onColumn1));
            var propertyDestination = FluentEntityAdoHelper.ConcatTableColumn(tableDestination, FluentEntityAdoHelper.GetColumnPropertyName(onColumn2));

            _joinCommandBuilder.Append($" {joinCommand} {tableDestination} ON {propertySource} = {propertyDestination}");
            TableJoined <TJoin2>(tableSource, tableDestination, joinedProperty, new TJoin2());

            return(this);
        }
 public void Equal <TKey>(TKey value)
 {
     _commandBuilder.Append($" = {FluentEntityAdoHelper.ApplyValueByType(value)}");
 }
 private void AddWhereCondition <TKey>(Expression <Func <T, TKey> > key, string keyWord)
 {
     _commandBuilder.Append($" {keyWord} {FluentEntityAdoHelper.GetColumnPropertyNameQualified(key)}");
 }
Beispiel #16
0
        public IFluentEntitySelect <T> ColumnRef <TRef, TKey>(Expression <Func <TRef, TKey> > key)
        {
            var tableName = FluentEntityAdoHelper.GetTableName(typeof(TRef));

            return(AddColumn(tableName, key));
        }
 public void LowerThanEquals <TKey>(TKey value)
 {
     _commandBuilder.Append($" <= {FluentEntityAdoHelper.ApplyValueByType(value)}");
 }
 public void NotIn <TKey>(IEnumerable <TKey> values)
 {
     _commandBuilder.Append($" NOT IN ({string.Join(", ", FluentEntityAdoHelper.ApplyValuesByType(values))})");
 }
Beispiel #19
0
 public FluentEntityAdoDelete(IDaoHelper daoHelper)
 {
     _daoHelper   = daoHelper ?? throw new ArgumentNullException(nameof(daoHelper));
     _whereHelper = new FluentEntityAdoWhereHelper <T>();
     _baseCommand = $"DELETE FROM {FluentEntityAdoHelper.GetTableName(typeof(T))}";
 }
 public void GreaterThan <TKey>(TKey value)
 {
     _commandBuilder.Append($" > {FluentEntityAdoHelper.ApplyValueByType(value)}");
 }
Beispiel #21
0
 private IFluentEntitySelect <T> AddColumn <TRef, TKey>(string tableName, Expression <Func <TRef, TKey> > key)
 {
     AddColumnData(tableName, FluentEntityAdoHelper.GetColumnProperty(key));
     return(this);
 }