示例#1
0
        public void SetCommandParameter(string text, IDataParameterCollection Parameters, object entity = null)
        {
            var s = Regex.Matches(text, "@\\w+(?=[\\s|,|)])|@\\w+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            var d = new DynamicHandlerCompiler <object>(entity);

            foreach (Match a in s)
            {
                var name = a.Value.Replace("@", string.Empty);

                if (this.where != null)
                {
                    if (where.Contains(name))
                    {
                        var p = CreaterParamger(a.Value, where.GetValue(name)) as IDataParameter;

                        Parameters.Add(p);
                        continue;
                    }
                }
                if (entity != null)
                {
                    var f = d.CreaterGetPropertyHandler <dynamic>(name);
                    if (Parameters.Contains(a.Value))
                    {
                        (Parameters[a.Value] as DbParameter).Value = f(entity) ?? string.Empty;
                    }
                    else
                    {
                        Parameters.Add(CreaterParamger(a.Value, f(entity) ?? string.Empty) as IDataParameter);
                    }
                }
            }
        }
示例#2
0
        private string GetExecuteSQL(string sql = null)
        {
            sql = sql ?? this.Command.CommandText;
            if (string.IsNullOrWhiteSpace(sql))
            {
                return("");
            }
            MatchCollection          matches    = Regex.Matches(sql, @"(?<p>" + this.SqlPrefix.Replace("?", "\\?") + @"\w+)", RegexOptions.Multiline);
            StringBuilder            result     = new StringBuilder();
            int                      offset     = 0;
            IDataParameterCollection parameters = this.Command.Parameters;

            foreach (Match m in matches)
            {
                int    index     = m.Groups["p"].Index;
                int    len       = m.Groups["p"].Length;
                string paramName = m.Groups["p"].Value;
                paramName = Regex.Replace(paramName, @"^" + this.SqlPrefix.Replace("?", "\\?"), this.ParamPrefix);
                object paramValue = paramName;
                if (parameters.Contains(paramName) && (IDataParameter)parameters[paramName] != null)
                {
                    paramValue = ((IDataParameter)parameters[paramName]).Value;
                }
                result.Append(sql.Substring(offset, index - offset));
                result.Append(this.GetSqlValue(paramValue));
                offset = index + len;
            }
            result.Append(sql.Substring(offset, sql.Length - offset));
            return(result.ToString());
        }
示例#3
0
        /// <summary>
        /// Tests the data parameter collection against the command text.
        /// </summary>
        /// <param name="actual">The actual parameter collection.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="resultBuilder">The result builder.</param>
        /// <param name="headerWritten">Indicates whether the header line has been written.</param>
        private static void DataParameterCollectionAgainstCommandTextTester(IDataParameterCollection actual, string commandText, StringBuilder resultBuilder, ref bool headerWritten)
        {
            ArgumentNullGuard.NotNull(actual, nameof(actual))
            .NotNullOrWhiteSpace(commandText, nameof(commandText))
            .NotNull(resultBuilder, nameof(resultBuilder));

            Regex           parameterRegex  = new Regex(ParameterRegex, RegexOptions.Compiled);
            MatchCollection matchCollection = parameterRegex.Matches(commandText);

            if (matchCollection.Count != actual.Count)
            {
                string ParameterTextWriter(int value) => value == 1 ? "parameter" : "parameters";

                headerWritten = WriteHeaderForDataParameterCollection(resultBuilder, headerWritten);
                resultBuilder.AppendLine($"- Expected:\t{matchCollection.Count} {ParameterTextWriter(matchCollection.Count)}");
                resultBuilder.AppendLine($"- But was:\t{actual.Count} {ParameterTextWriter(actual.Count)}");
            }

            foreach (Match match in matchCollection)
            {
                if (actual.Contains(match.Value))
                {
                    continue;
                }

                headerWritten = WriteHeaderForDataParameterCollection(resultBuilder, headerWritten);
                resultBuilder.AppendLine($"- {match.Value} does not exist in Parameters");
            }
        }
        public override int ExecuteNonQuery(ServiceCursorDef cursorDef, IDataParameterCollection parms, CommandBehavior behavior)
        {
            DataStoreHelperFabric.Query fabricQuery = cursorDef.Query as DataStoreHelperFabric.Query;
            if (fabricQuery != null)
            {
                Dictionary <String, String> tempParms = new Dictionary <string, string>();
                payLoad.Clear();
                foreach (KeyValuePair <String, String> kvp in fabricQuery.Parms)
                {
                    String parName = kvp.Value.ToString().Substring(1);
                    if (parms.Contains(parName))
                    {
                        IDataParameter m_par = ((IDataParameter)parms[parName]);
                        if (m_par.DbType == DbType.Date || m_par.DbType == DbType.DateTime || m_par.DbType == DbType.DateTime2)
                        {
                            tempParms.Add(kvp.Key, ((DateTime)m_par.Value).ToString("yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture));
                        }
                        else
                        {
                            tempParms.Add(kvp.Key, m_par.Value.ToString());
                        }
                    }
                }

                payLoad.Add("function", fabricQuery.Method);
                payLoad.Add("type", "invoke");
                payLoad.Add("param", tempParms);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonPayLoad = serializer.Serialize((object)payLoad);

                if (SetServiceData(this.ConnectionString, restClient))
                {
                    restClient.AddString(jsonPayLoad);
                    restClient.Execute("POST", fabricQuery.TableName);
                    if (restClient.StatusCode == 200)
                    {
                        GXLogging.Info(log, restClient.ToString());
                        return(0);
                    }
                    else
                    {
                        if (processError(restClient.StatusCode, restClient.ToString(), out int statusCode, out string msg))
                        {
                            throw new FabricException(statusCode, msg);
                        }
                        else
                        {
                            throw new GxADODataException("Error executing: " + restClient.ToString());
                        }
                    }
                }
 private static void ObjectToParameter(Type ptType, Dictionary <string, object> param, IDataParameterCollection Parameters)
 {
     foreach (var item in param)
     {
         var t = Activator.CreateInstance(ptType, new object[] { item.Key, item.Value });
         if (Parameters.Contains(item.Key))
         {
             Parameters[item.Key] = item.Value;
         }
         else
         {
             Parameters.Add(t);
         }
     }
 }
示例#6
0
        public static string PrepareQuery(string query, IDataParameterCollection parameters)
        {
            var sb        = new StringBuilder(query.Length);
            var pos       = 0;
            var quoteChar = 0;

            while (pos < query.Length)
            {
                var c = query[pos];
                if (quoteChar != 0)
                {
                    // Escape char - just skip one ahead
                    if (c == '\\' && pos < query.Length - 1)
                    {
                        sb.Append(c);
                        c = query[++pos];
                    }
                    else if (c == quoteChar)
                    {
                        quoteChar = 0;
                    }
                }
                else
                {
                    if (c == '"' || c == '\'')
                    {
                        quoteChar = c;
                    }
                    else if (c == '@')
                    {
                        // This is where we actually want to get to... parse @xyz
                        var identifier = PrepareQueryExtractIdentifier(query, ref pos);
                        sb.Append(parameters.Contains(identifier) ? ValueToSQL(parameters[identifier]) : "@" + identifier);
                        continue;
                    }
                }

                sb.Append(c);
                pos++;
            }

            return(sb.ToString());
        }
示例#7
0
        /// <summary>
        /// Sets a parameter value in Parameters C to Value, and if the parameter type is
        /// a string, and the parameter length is greater than the length
        /// of the value, sets the parameter length to the length of the
        /// value.  This is because Text parameter length is always the max
        /// length which causes an excess memory consumption.
        /// </summary>
        /// <param name="C">The parameter collection</param>
        /// <param name="Name">The parameter to set.</param>
        /// <param name="Value">The value to set it to.</param>
        public void SetValue(IDataParameterCollection C, string Name, Object Value)
        {
            if (!C.Contains(Name))
            {
                return;
            }
            IDataParameter P = (IDataParameter)C[Name];

            if (IsTextField(P))
            {
                int          Len = (Value == null)?0:((string)Value).Length;
                PropertyInfo M   = P.GetType().GetProperty("Size");
                if (M != null)
                {
                    M.SetValue(P, Len, null);
                }
            }
            if (Value != null)
            {
                P.Value = Value;
            }
        }
        /// <summary>
        /// 参数对象转 参数集
        /// </summary>
        /// <param name="ptType"></param>
        /// <param name="param"></param>
        /// <param name="Parameters"></param>
        private static void ObjectToParameter(Type ptType, object param, IDataParameterCollection Parameters)
        {
            if (param == null)
            {
                return;
            }
            var type       = param.GetType();
            var properties = type.GetProperties();

            foreach (var property in properties)
            {
                var key   = string.Format("@{0}", property.Name);
                var value = property.GetValue(param, null) ?? "";
                var t     = Activator.CreateInstance(ptType, new object[] { key, value });
                if (Parameters.Contains(key))
                {
                    Parameters[key] = value;
                }
                else
                {
                    Parameters.Add(t);
                }
            }
        }
示例#9
0
 public override bool Contains(string value)
 {
     return(_parameterCollection.Contains(value));
 }
 public bool Contains(string parameterName)
 {
     return(dataParameterCollection.Contains(parameterName));
 }
示例#11
0
 public bool Contains(object value)
 {
     return(_parameters.Contains(value));
 }
示例#12
0
 public bool Contains(string parameterName)
 {
     return(_parameters.Contains(parameterName));
 }