public async Task <Table> GetTableData(int tableId, int offset = 0, int limit = 0)
        {
            var tableMeta = await _dbContext.TableInfos.Where(x => x.Id == tableId)
                            .Include(x => x.FieldInfos)
                            .ThenInclude(x => x.FieldType)
                            .FirstAsync();

            if (tableMeta == null)
            {
                throw new TableNotFoundException(tableId);
            }

            var table = _mapper.Map <Table>(tableMeta);

            string selectQuery;

            if (offset == 0 && limit == 0)
            {
                selectQuery = _commandDirector.BuildSelectCommand(table, tableMeta.ViewQuery);
            }
            else
            {
                selectQuery = _commandDirector.BuildPaginationSelectCommand(table, offset, limit, tableMeta.ViewQuery);
            }
            var unhandledRows = _dbContext.CollectFromExecuteSql(selectQuery);

            var rows = new List <Row>();

            foreach (var unhandledRow in unhandledRows)
            {
                var row = new Row {
                    TableId = table.Id
                };

                foreach (var field in table.Fields)
                {
                    unhandledRow.TryGetValue(field.Name, out object obj);
                    if (obj != null)
                    {
                        row.Data[field] = obj;
                    }
                }
                rows.Add(row);
            }

            table.Rows = rows;

            return(table);
        }
Esempio n. 2
0
        public void TestCreateSelectCommand()
        {
            var sqlQuery = _crudCommandDirector.BuildSelectCommand(_table);

            Assert.Equal("SELECT * FROM data.Car ORDER BY gid", sqlQuery);
        }