Exemplo n.º 1
0
        public static DbParam[] BuildParams(IDbContext dbContext, object parameter)
        {
            if (parameter == null)
            {
                return(new DbParam[0]);
            }

            if (parameter is IEnumerable <DbParam> )
            {
                return(((IEnumerable <DbParam>)parameter).ToArray());
            }

            IDatabaseProvider databaseProvider = ((DbContext)dbContext).DatabaseProvider;

            List <DbParam> parameters    = new List <DbParam>();
            Type           parameterType = parameter.GetType();
            var            props         = parameterType.GetProperties();

            foreach (var prop in props)
            {
                if (prop.GetGetMethod() == null)
                {
                    continue;
                }

                object value = ReflectionExtension.GetMemberValue(prop, parameter);

                string paramName = databaseProvider.CreateParameterName(prop.Name);

                DbParam p = new DbParam(paramName, value, prop.PropertyType);
                parameters.Add(p);
            }

            return(parameters.ToArray());
        }
Exemplo n.º 2
0
        static (string Sql, DbParam[] Parameters) BuildSqlAndParameters(IDbContext dbContext, FormattableString sql)
        {
            List <string>  formatArgs = new List <string>(sql.ArgumentCount);
            List <DbParam> parameters = new List <DbParam>(sql.ArgumentCount);

            IDatabaseProvider databaseProvider = ((DbContext)dbContext).DatabaseProvider;
            string            parameterPrefix  = "P_";

            foreach (var arg in sql.GetArguments())
            {
                object paramValue = arg;
                if (paramValue == null || paramValue == DBNull.Value)
                {
                    formatArgs.Add("NULL");
                    continue;
                }

                Type paramType = arg.GetType();

                if (paramType.IsEnum)
                {
                    paramType = Enum.GetUnderlyingType(paramType);
                    if (paramValue != null)
                    {
                        paramValue = Convert.ChangeType(paramValue, paramType);
                    }
                }

                DbParam p;
                p = parameters.Where(a => Utils.AreEqual(a.Value, paramValue)).FirstOrDefault();

                if (p != null)
                {
                    formatArgs.Add(p.Name);
                    continue;
                }

                string paramName = databaseProvider.CreateParameterName(parameterPrefix + parameters.Count.ToString());
                p = DbParam.Create(paramName, paramValue, paramType);
                parameters.Add(p);
                formatArgs.Add(p.Name);
            }

            string runSql = string.Format(sql.Format, formatArgs.ToArray());

            return(runSql, parameters.ToArray());
        }
Exemplo n.º 3
0
        private static AnalyzeResult Analyze(IDatabaseProvider provider, AnalyzeResult previousAnalyze, BinaryExpression expression)
        {
            var analyzeLeft  = Analyze(provider, previousAnalyze, expression.Left);
            var analyzeRight = Analyze(provider, analyzeLeft, expression.Right);

            var leftSql = analyzeLeft.GetSql();

            if (leftSql == null)
            {
                leftSql = analyzeLeft.ParameterName;
            }
            var rightSql = analyzeRight.GetSql();

            if (rightSql == null)
            {
                rightSql = provider.CreateParameterName(analyzeLeft.ParameterName);
            }

            var analyzeResult = new AnalyzeResult(analyzeLeft, analyzeRight);

            if (!analyzeLeft.HasParameterCombination && !analyzeRight.HasParameterCombination)
            {
                analyzeResult.SetParameterCombination(analyzeLeft.ParameterName, analyzeRight.ParameterValue);
            }

            switch (expression.NodeType)
            {
            case ExpressionType.Equal:
                if (analyzeRight.ParameterValue == null)
                {
                    analyzeResult.SetSql(leftSql + " is null");
                }
                else
                {
                    analyzeResult.SetSql(leftSql + " = " + rightSql);
                }
                break;

            case ExpressionType.NotEqual:
                if (analyzeRight.ParameterValue == null)
                {
                    analyzeResult.SetSql(leftSql + " is not null");
                }
                else
                {
                    analyzeResult.SetSql(leftSql + " <> " + rightSql);
                }
                break;

            case ExpressionType.GreaterThan:
                analyzeResult.SetSql(leftSql + " > " + rightSql);
                break;

            case ExpressionType.GreaterThanOrEqual:
                analyzeResult.SetSql(leftSql + " >= " + rightSql);
                break;

            case ExpressionType.LessThan:
                analyzeResult.SetSql(leftSql + " < " + rightSql);
                break;

            case ExpressionType.LessThanOrEqual:
                analyzeResult.SetSql(leftSql + " <= " + rightSql);
                break;

            case ExpressionType.AndAlso:
            case ExpressionType.And:
                analyzeResult.SetSql("(" + leftSql + ") and (" + rightSql + ")");
                break;

            case ExpressionType.OrElse:
            case ExpressionType.Or:
                analyzeResult.SetSql("(" + leftSql + ") or (" + rightSql + ")");
                break;

            default:
                return(null);
            }

            return(analyzeResult);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 准备数据库指令
        /// </summary>
        /// <param name="statement">上层指令</param>
        /// <returns>数据库指令</returns>
        private DbCommand PrepareCommand(Statement statement)
        {
            DbCommand command = m_DatabaseProvider.CreateCommand();

            command.CommandText    = statement.StatementText;
            command.CommandType    = statement.StatementType == StatementType.Sql ? CommandType.Text : CommandType.StoredProcedure;
            command.CommandTimeout = statement.Timeout;
            String providerName = m_DatabaseProvider.GetType().Name;

            foreach (var p in statement.Parameters)
            {
                if (p.ExtendType == 1)
                {
                    var parameter = (SqlParameter)command.CreateParameter();
                    parameter.ParameterName = m_DatabaseProvider.CreateParameterName(p.Name);
                    parameter.SqlDbType     = (SqlDbType)p.ExtendTypeValue;

                    if (!String.IsNullOrEmpty(p.TypeName))
                    {
                        parameter.TypeName = p.TypeName;
                    }
                    parameter.Size       = p.Size;
                    parameter.Value      = p.Value ?? DBNull.Value;
                    parameter.Direction  = p.Direction;
                    parameter.IsNullable = p.IsNullable;
                    command.Parameters.Add(parameter);
                }
                else
                {
                    if (p.DbType == DbType.Time && providerName == "SqlDatabaseProvider")
                    {
                        var parameter = (SqlParameter)command.CreateParameter();
                        parameter.ParameterName = m_DatabaseProvider.CreateParameterName(p.Name);
                        parameter.SqlDbType     = SqlDbType.Time;
                        parameter.Size          = p.Size;
                        parameter.Value         = p.Value ?? DBNull.Value;
                        parameter.Direction     = p.Direction;
                        parameter.IsNullable    = p.IsNullable;
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        var parameter = command.CreateParameter();
                        parameter.ParameterName = m_DatabaseProvider.CreateParameterName(p.Name);
                        parameter.DbType        = p.DbType;
                        parameter.Size          = p.Size;
                        parameter.Value         = p.Value ?? DBNull.Value;
                        parameter.Direction     = p.Direction;
                        parameter.IsNullable    = p.IsNullable;

                        if (providerName.Equals("MySqlDatabaseProvider"))
                        {
                            command.Parameters.Insert(-1, parameter);   //work around for legacy mysql driver versions
                        }
                        else
                        {
                            command.Parameters.Add(parameter);
                        }
                    }
                }
            }

            return(command);
        }
Exemplo n.º 5
0
        public override void BuildQuery(IDatabaseProvider provider)
        {
            if (_fieldValues == null || !_fieldValues.Any())
            {
                return;
            }

            var primaryKeyValueParameter = "PrimaryKeyValue";

            Builder.AppendFormat("UPDATE {0} SET {1} WHERE {2} = {3};", provider.CombineSchemaAndTable(_schema, _table), _fieldValues.Select(x => string.Format("{0} = {1}", x.Key, provider.CreateParameterName(x.Key.Replace("\"", "").Replace(" ", "_")))).Join(", "), _primaryKeyField, provider.CreateParameterName(primaryKeyValueParameter));

            Parameters.Add(primaryKeyValueParameter, _primaryKeyValue);

            foreach (var x in _fieldValues)
            {
                Parameters.Add(x.Key.Replace("\"", "").Replace(" ", "_"), x.Value);
            }
        }