Пример #1
0
        public T[] Builder <T>(Action <SqlPropertyBuilder <T> > propertyBuilder, Action <SqlItemBuilder <T> > itemBuilder)
        {
            if (Ok && selectBuilders != null && select != null)
            {
                var result = First;

                var list = new List <T>();

                foreach (var row in result)
                {
                    T       item    = (T)Activator.CreateInstance(typeof(T));
                    Context context = new();

                    Dictionary <string, SqlCell> rowdict = new();
                    foreach (var(name, index) in result.ColumnIndex)
                    {
                        rowdict.Add(name, row[index]);
                    }

                    List <SqlBuilder>           builders        = new();
                    Dictionary <string, object> formattedValues = new();

                    // get builders
                    foreach (var columnName in result.ColumnNames)
                    {
                        if (columnName != "___select")
                        {
                            var builder = select.GetBuilder(columnName, row);

                            if (builder != null && builder.Name != null)
                            {
                                builders.Add(builder);
                            }
                        }
                    }

                    // formatter
                    foreach (var builder in builders)
                    {
                        var    cell           = row.ContainsKey(builder.Name) ? row[builder.Name] : null;
                        object formattedValue = null;

                        if (builder.Formatter != null)
                        {
                            formattedValue = builder.Formatter(rowdict);

                            if (formattedValue is SqlCell valueCell)
                            {
                                formattedValue = valueCell.GetObject();
                            }
                        }
                        else if (cell != null)
                        {
                            // default formatter
                            formattedValue = cell.GetObject();
                        }

                        formattedValues.Add(builder.Name, formattedValue);
                    }

                    // binder
                    foreach (var builder in builders)
                    {
                        if (builder.Binder != null)
                        {
                            builder.Binder(item, rowdict, formattedValues);
                        }
                        else
                        {
                            // default binder
                        }
                    }

                    // propertyBuilder
                    if (propertyBuilder != null)
                    {
                        foreach (var builder in builders)
                        {
                            propertyBuilder(new SqlPropertyBuilder <T>
                            {
                                Item            = item,
                                Context         = context,
                                Builder         = builder,
                                Values          = rowdict,
                                FormattedValues = formattedValues
                            });
                        }
                    }

                    // invoke itemBuilder
                    itemBuilder?.Invoke(new SqlItemBuilder <T>
                    {
                        Context         = context,
                        Item            = item,
                        Values          = rowdict,
                        FormattedValues = formattedValues
                    });

                    list.Add(item);
                }

                return(list.ToArray());
            }
            else
            {
                return(null);
            }
        }