Esempio n. 1
0
        private string GetFrom()
        {
            if (schema.SelectCommand.CommandType == CommandType.TableDirect)
            {
                return(schema.SelectCommand.CommandText);
            }
            else if (schema.SelectCommand.CommandType == CommandType.StoredProcedure)
            {
                throw new E_SQLBuilder_NoSelectCommand();
            }
            else if (string.IsNullOrEmpty(schema.SelectCommand.CommandText))
            {
                throw new E_SQLBuilder_NoSelectCommand();
            }

            SqlParse sql = new SqlParse(schema.SelectCommand.CommandText);

            return(sql.GetFrom());
        }
Esempio n. 2
0
        /// <summary>
        /// 根据CommandSchema创建命令,并将Shema中的参数设置到命令
        /// </summary>
        /// <param name="cmdSchema">命令配置</param>
        /// <returns></returns>
        public DbCommand getCommand(CommandSchema cmdSchema)
        {
            if (cmdSchema == null || string.IsNullOrEmpty(cmdSchema.CommandText))
            {
                throw new XException(Lang.NoUpLoadCommand);
            }

            DbCommand cmd = dbAdmin.getSqlCommand(cmdSchema.CommandText, cmdSchema.CommandType);

            SqlParse sqlp = new SqlParse(cmdSchema.CommandText);

            string[] parameters = sqlp.GetParamNames();

            foreach (string pName in parameters)
            {
                string             fName = pName.Remove(0, 1);
                string             val   = null;
                DbType             pType = DbType.String;
                ParameterDirection pDir  = ParameterDirection.Input;

                ParameterSchema ps   = cmdSchema.QueryParams == null ? null : cmdSchema.QueryParams.FindItem(pName);
                FieldSchema     flds = schema.Fields == null ? null : schema.Fields.FindItem(fName);

                if (pName.StartsWith(XSqlBuilder.OLD_VERSION_PIX))
                {
                    flds = schema.Fields.FindItem(fName.Remove(0, XSqlBuilder.OLD_VERSION_PIX.Length));
                }

                //获取字典定义中的参数类型,参数方向默认为input
                if (flds != null)
                {
                    pType = flds.DataType;
                }

                //获取参数定义中的参数类型,及参数方向;
                if (ps != null)
                {
                    pType = ps.DataType;
                    val   = ps.DefaultValue;
                    pDir  = ps.Direction;
                }
                //if (row.ContainsKey(fName))
                //    val = row[fName];

                //if (realParams.ContainsKey(pName))
                //    val = realParams[pName];


                switch (pDir)
                {
                case ParameterDirection.Input:
                    dbAdmin.addInParameter(cmd, pName, pType, val);
                    break;

                case ParameterDirection.InputOutput:
                    break;

                case ParameterDirection.Output:
                    dbAdmin.AddOutParameter(cmd, pName, pType, int.MaxValue);
                    break;
                }
            }

            return(cmd);
        }
Esempio n. 3
0
        private DbCommand getSelectCommand(int pageSize, int pageNo, string fields, Dictionary <string, string> queryParams, string where, string orderBy, string groupBy)
        {
            string connectionName = _schema.ConnectionName;
            string selectSql;
            //  tableNames = new List<string>();

            DbCommand cmd = null;

            switch (_schema.SelectCommand.CommandType)
            {
            case CommandType.TableDirect:

                if (pageNo > 1 || pageSize > 1)
                {
                    cmd = getPagingSpCommand(_schema.SelectCommand.CommandText, pageSize, pageNo, fields, where, orderBy, groupBy);
                }

                if (cmd == null)
                {
                    selectSql = XSqlBuilder.BuildTableSql(_schema.SelectCommand.CommandText, pageSize, pageNo, fields, where, orderBy, groupBy);
                    cmd       = dbAdmin.getSqlStringCommand(selectSql);
                }
                else
                {
                    _schema.IsPagingByParams = true;
                }
                break;

            case CommandType.Text:
                selectSql = _schema.SelectCommand.CommandText;
                SqlParse xSql = new SqlParse(selectSql);
                cmd = dbAdmin.getSqlStringCommand(selectSql);
                //                    paramNames = xSql.GetParamNames();
                break;

            case CommandType.StoredProcedure:
                selectSql = _schema.SelectCommand.CommandText;
                cmd       = dbAdmin.GetStoredProcCommandWithSourceColumns(selectSql, fields.Split(','));
                break;
            }


            if (_schema.SelectCommand != null && _schema.SelectCommand.QueryParams != null)
            {
                foreach (ParameterSchema ps in _schema.SelectCommand.QueryParams)
                {
                    if (string.IsNullOrEmpty(ps.Id))
                    {
                        continue;
                    }
                    string name = ps.Id;

                    object value = ps.DefaultValue;

                    if (queryParams != null && queryParams.ContainsKey(name))
                    {
                        value = queryParams[name];
                    }

                    //if (name.Equals(DataSourceConst.PagingPageNo, StringComparison.OrdinalIgnoreCase))
                    //    value = pageNo;
                    //if (name.Equals(DataSourceConst.PagingPageSize, StringComparison.OrdinalIgnoreCase))
                    //    value = pageSize;

                    if (cmd.Parameters.Contains(name))
                    {
                        dbAdmin.Database.SetParameterValue(cmd, name, value);
                    }
                    else if (ps.Direction == ParameterDirection.Input)
                    {
                        dbAdmin.addInParameter(cmd, name, ps.DataType, value);
                    }
                    else
                    {
                        dbAdmin.AddOutParameter(cmd, name, ps.DataType, ps.DataSize);
                    }
                }
            }
            return(cmd);
        }