Exemple #1
0
        /// <summary>
        /// 插入或更新数据库操作解析方法
        /// </summary>
        /// <param name="_needTran">是否进行回滚插入</param>
        /// <param name="_params">参数值集合</param>
        /// <returns>插入T-SQL语句</returns>
        public virtual string AddOrUpdate(out bool _needTran, out KdtParameterCollection _params)
        {
            if (this.Adapter == null)
            {
                throw new Exception("未启用适配器信息!");
            }

            StringBuilder sqlText = new StringBuilder();

            _needTran = false;
            _params   = new KdtParameterCollection();
            KdtParameterCollection tempparams;

            var _allfeilds           = GetAllKdtFields();
            List <KdtFeildEx> _allex = new List <KdtFeildEx>();

            foreach (var table in _allfeilds)
            {
                _allex.AddRange(table.Fields);
            }

            if (_allfeilds != null && _allfeilds.Count > 0)
            {
                if (_relationFields != null || _allfeilds.Count > 1)
                {
                    // 判断是否为多表情况
                    if (_relationFields.Count > 0)
                    {
                        _needTran = true;
                        // 第一步,获取主表信息
                        var foundtable = _allfeilds.Find(t => t.TableName.Equals(_mainTable, StringComparison.OrdinalIgnoreCase));
                        if (foundtable == null)
                        {
                            throw new Exception("多级插入主表{0}错误!".ToFormat(_mainTable));
                        }

                        sqlText.AppendLine(CreateAddOrUpdateSql(foundtable, _allex, "r0", out tempparams));
                        _params.AddRange(tempparams);
                        _allfeilds.Remove(foundtable);

                        for (int i = 0; i < _allfeilds.Count; i++)
                        {
                            var _table = _allfeilds[i];
                            if (_relationFields.ContainsKey(_table.TableName))
                            {
                                _table.HasRelation = true;
                                _table.RelFeild    = _relationFields[_table.TableName];
                            }
                            string _thisincrstr = "r{0}".ToFormat(i + 1);
                            sqlText.AppendLine(CreateAddOrUpdateSql(_table, _allex, _thisincrstr, out tempparams));
                            _params.AddRange(tempparams);
                        }
                        return(sqlText.ToString());
                    }
                    else
                    {
                        List <KdtTableFeildEx> myList = new List <KdtTableFeildEx>(_allfeilds);
                        foreach (var item in myList)
                        {
                            if (item.Fields.Count < 2)
                            {
                                _allfeilds.Remove(item);
                            }
                        }
                        if (_allfeilds.Count > 1)
                        {
                            _needTran = true;
                            // 进行无关联的多表插入
                            for (int i = 0; i < _allfeilds.Count; i++)
                            {
                                var    _thistable   = _allfeilds[i];
                                string _thisincrstr = "r{0}".ToFormat(i);
                                sqlText.AppendLine(CreateAddOrUpdateSql(_thistable, _allex, _thisincrstr, out tempparams));
                                _params.AddRange(tempparams);
                            }
                            return(sqlText.ToString());
                        }
                    }
                }
                // 单表插入
                var tempTable = _allfeilds.First();
                _needTran = false;
                sqlText.AppendLine(CreateAddOrUpdateSql(tempTable, _allex, "r0", out tempparams));
                _params.AddRange(tempparams);
                return(sqlText.ToString());
            }

            throw new DataException("没有可以添加的字段列信息");
        }
Exemple #2
0
        /// <summary>
        /// 插入数据库操作解析方法
        /// </summary>
        /// <param name="_needTran">是否进行回滚插入</param>
        /// <param name="_isIncr">自定义参数</param>
        /// <param name="_params">参数值集合</param>
        /// <returns>插入T-SQL语句</returns>
        public virtual string Add(out bool _needTran, out bool _isIncr, out KdtParameterCollection _params)
        {
            if (this.Adapter == null)
            {
                throw new Exception("未启用适配器信息!");
            }

            StringBuilder sqlText = new StringBuilder();

            _needTran = false;
            _isIncr   = false;
            _params   = new KdtParameterCollection();
            KdtParameterCollection tempparams;

            var _allfeilds = GetAllKdtFields();

            if (_allfeilds != null && _allfeilds.Count > 0)
            {
                if (_relationFields != null || _allfeilds.Count > 1)
                {
                    // 判断是否为多表情况
                    if (_relationFields.Count > 0)
                    {
                        _needTran = true;
                        _isIncr   = false;
                        // 进行有关联的多表插入
                        StringBuilder _returnedsel = new StringBuilder("select");
                        // 第一步,获取主表信息
                        var foundtable = _allfeilds.Find(t => t.TableName.Equals(_mainTable, StringComparison.OrdinalIgnoreCase));
                        if (foundtable == null)
                        {
                            throw new Exception("多级插入主表{0}错误!".ToFormat(_mainTable));
                        }

                        sqlText.AppendLine(CreateAddSql(foundtable, "r0", out tempparams));
                        _params.AddRange(tempparams);

                        if (foundtable.HasIncr)
                        {
                            _returnedsel.AppendFormat(" {0}{1} as p{2},", this.Adapter.Prefix, "r0", foundtable.TableName);
                        }
                        _allfeilds.Remove(foundtable);

                        for (int i = 0; i < _allfeilds.Count; i++)
                        {
                            var _table = _allfeilds[i];
                            if (_relationFields.ContainsKey(_table.TableName))
                            {
                                _table.HasRelation = true;
                                _table.RelFeild    = _relationFields[_table.TableName];
                            }
                            string _thisincrstr = "r{0}".ToFormat(i + 1);
                            sqlText.AppendLine(CreateAddSql(_table, _thisincrstr, out tempparams));
                            _params.AddRange(tempparams);
                            if (_table.HasIncr)
                            {
                                _returnedsel.AppendFormat(" {0}{1} as p{2},", this.Adapter.Prefix, _thisincrstr, _table.TableName);
                            }
                        }

                        if (_returnedsel.Length > 6)
                        {
                            _isIncr = true;
                            sqlText.Append(_returnedsel.ToString().TrimEnd(','));
                        }
                        return(sqlText.ToString());
                    }
                    else
                    {
                        List <KdtTableFeildEx> myList = new List <KdtTableFeildEx>(_allfeilds);
                        foreach (var item in myList)
                        {
                            if (item.Fields.Count < 2)
                            {
                                _allfeilds.Remove(item);
                            }
                        }
                        if (_allfeilds.Count > 1)
                        {
                            _needTran = true;
                            _isIncr   = false;
                            // 进行无关联的多表插入
                            StringBuilder _returnedsel = new StringBuilder("select");
                            for (int i = 0; i < _allfeilds.Count; i++)
                            {
                                var    _thistable   = _allfeilds[i];
                                string _thisincrstr = "r{0}".ToFormat(i);
                                sqlText.AppendLine(CreateAddSql(_thistable, _thisincrstr, out tempparams));
                                _params.AddRange(tempparams);
                                if (_thistable.HasIncr)
                                {
                                    _returnedsel.AppendFormat(" {0}{1} as p{2},", this.Adapter.Prefix, _thisincrstr, _thistable.TableName);
                                }
                            }
                            if (_returnedsel.Length > 6)
                            {
                                _isIncr = true;
                                sqlText.Append(_returnedsel.ToString().TrimEnd(','));
                            }
                            return(sqlText.ToString());
                        }
                    }
                }
                // 单表插入
                var tempTable = _allfeilds.First();
                _needTran = false;
                _isIncr   = tempTable.HasIncr;
                sqlText.Append(CreateAddSql(tempTable, "r0", out tempparams));
                _params.AddRange(tempparams);
                if (_isIncr)
                {
                    sqlText.AppendFormat(this.Adapter.Select("r0", "p{0}".ToFormat(tempTable.TableName), "", true, true));
                }
                return(sqlText.ToString());
            }

            throw new DataException("没有可以添加的字段列信息");
        }