예제 #1
0
        public static OutDbArg Create(int size, Type type)
        {
            var _result = new OutDbArg {
                DataParameter = new System.Data.Odbc.OdbcParameter()
                {
                    ParameterName = "?", Direction = ParameterDirection.Output, Size = size
                }
            };

            if (type == typeof(DateTime))
            {
                _result.DataParameter.DbType = DbType.DateTime;
            }

            return(_result);
        }
        public T Add(T value)
        {
            var _cols = EnumerableUtil.ToArray(EnumerableUtil.OrderBy(this._Columns, x => x.IsKey ? x.KeyInfo.Order : -1));

            var _columnSet = string.Empty;

            EnumerableUtil.Each(_cols, x => _columnSet += $"{x.Name}, ");

            var _valueSet = string.Empty;
            var _in       = new List <_Column>();
            var _out      = new List <_Column>();

            EnumerableUtil.Each(_cols, x => {
                if (x.IsKey)
                {
                    switch (x.KeyInfo.Method)
                    {
                    case DatabaseGeneratedMethod.MaxInc:
                        _valueSet += $"(SELECT NVL(MAX({x.Name})+1,1) FROM {this._Table}), ";
                        _out.Add(x);
                        break;

                    case DatabaseGeneratedMethod.Select:
                        _valueSet += $"({x.KeyInfo.Expresion}), ";
                        _out.Add(x);
                        break;

                    case DatabaseGeneratedMethod.Seq:
                        _valueSet += $"{x.KeyInfo.Expresion}.NEXTVAL, ";
                        _out.Add(x);
                        break;

                    default:
                        _valueSet += "?, ";
                        _in.Add(x);
                        _out.Add(x);
                        break;
                    }
                }
                else
                {
                    _valueSet += "?, ";
                    _in.Add(x);
                }
            });

            var _outSet1 = string.Empty;
            var _outSet2 = string.Empty;

            EnumerableUtil.Each(_out, x => {
                _outSet1 += $"{x.Name}, ";
                _outSet2 += $"?, ";
            });

            var _args =
                EnumerableUtil.ToArray(
                    EnumerableUtil.UnionAll(
                        EnumerableUtil.Select(_in, x => x.GetValue(value)),
                        EnumerableUtil.Select(_out, x => OutDbArg.Create(x.KeyInfo.Size, x.PropertyInfo.PropertyType) as object)));

            this.Execute($@"
                INSERT INTO {this._Table}
                    ({_columnSet.TrimEnd(' ', ',')})
                VALUES ({_valueSet.TrimEnd(' ', ',')})
                RETURNING {_outSet1.TrimEnd(' ', ',')} INTO {_outSet2.TrimEnd(' ', ',')}", _args);

            return(this.GetFromId(
                       EnumerableUtil.ToArray(
                           EnumerableUtil.Select(
                               EnumerableUtil.Where(_args, x => x is OutDbArg),
                               x => (x as OutDbArg).Value))));
        }