Ejemplo n.º 1
0
        public async Task <IDictionary <string, object> > UpdateRow(string table, string id, List <KeyValuePair <string, object> > formValues)
        {
            CheckColumnNames(table, formValues);
            var colNames = formValues.Select(kvp => kvp.Key).ToList();
            var conn     = await _connectionProvider.Get();

            var tableInfo  = _dbInspector.GetSchema().FindTableByName(table);
            var parameters = ToDbParameters(formValues, tableInfo);
            var pk         = _dbInspector.GetPkColumn(table);

            parameters.Add(pk.Name, Coerce(pk, id));

            var sql = new[]
            {
                $"UPDATE {table}",
                // $"({colNames.JoinToString()})",
                $"SET {colNames.Select(x => $"{x} = @" + x).JoinToString()} " +
                $"WHERE id = @id " +
                $"RETURNING {table}.*"
            }.JoinToString(" \r\n");
            var obj = await conn.QuerySingleAsync(sql, parameters);

            return(obj);
        }
Ejemplo n.º 2
0
        private HtmlElement DbValueToElement(string table, IDictionary <string, object> obj, KeyValuePair <string, object> kvp)
        {
            var col = _dbInspector.GetColumn(table, kvp.Key);

            if (col == null)
            {
                // throw new ArgumentException(nameof(col), kvp.Key);
                // return new Span(kvp.Value?.ToString());
            }

            if (_dbInspector.IsFk(table, kvp.Key))
            {
                var trg = _dbInspector.GetFkTarget(table, kvp.Key);
                return(new A
                {
                    // Text = trg + "#" + kvp.Value,
                    rel = trg,
                    Href = _linkManager.LinkToItem(trg, kvp.Value),
                    Subs = new Span(kvp.Value?.ToString())
                    {
                        Itemscope = false,
                        Itemprop = kvp.Key
                                   // Itemtype = kvp.Key,
                    }.ToArray()
                });
            }

            if (_dbInspector.GetPkColumn(table).Name == kvp.Key)
            {
                return(new A
                {
                    // Text = kvp.Value.ToString(),
                    rel = "self",
                    Href = _linkManager.LinkToItem(table, kvp.Value),
                    Subs = WithItemProp(new Span(kvp.Value?.ToString()), kvp.Key).ToArray(),
                    Itemscope = false,
                });
            }

            if (_dbInspector.IsLob(table, kvp.Key))
            {
                return(new A
                {
                    Text = "Download " + kvp.Key,
                    Href = _linkManager.LinkToLob(table, _dbInspector.GetId(table, obj), kvp.Key),
                    Itemscope = true, Itemprop = kvp.Key,
                });
            }

            if (kvp.Value is HtmlElement e)
            {
                return(WithItemProp(e, kvp.Key));
            }

            if (kvp.Value is HtmlElement[] ea)
            {
                return(new Ul(ea.Select(x => new Li(WithItemProp(x, kvp.Key)))));
            }

            return(WithItemProp(new Span(kvp.Value?.ToString()), kvp.Key));
        }