Esempio n. 1
0
        internal QueryInstancesJob(CimJobContext jobContext, CimQuery cimQuery, string wqlCondition)
            : base(jobContext, cimQuery)
        {
            Dbg.Assert(wqlCondition != null, "Caller should verify that wqlCondition is not null");

            var wqlQueryBuilder = new StringBuilder();

            wqlQueryBuilder.Append("SELECT * FROM ");
            wqlQueryBuilder.Append(this.JobContext.ClassName);
            wqlQueryBuilder.Append(" ");
            wqlQueryBuilder.Append(wqlCondition);
            _wqlQuery = wqlQueryBuilder.ToString();

            if (string.IsNullOrWhiteSpace(wqlCondition))
            {
                _useEnumerateInstances = true;
            }
            else
            {
                if (jobContext.CmdletInvocationContext.CmdletDefinitionContext.UseEnumerateInstancesInsteadOfWql)
                {
                    _useEnumerateInstances = true;
                }
            }
        }
Esempio n. 2
0
        internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
        {
            CimQuery cimQuery = baseQuery as CimQuery;

            if (cimQuery != null)
            {
                TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, false);
                if (!tracker.IsSessionTerminated(session))
                {
                    if (this.IsSupportedSession(session, tracker))
                    {
                        CimJobContext cimJobContext = this.CreateJobContext(session, null);
                        StartableJob  queryJob      = cimQuery.GetQueryJob(cimJobContext);
                        return(queryJob);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                throw new ArgumentNullException("baseQuery");
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to query</param>
        /// <param name="baseQuery">Query parameters</param>
        /// <returns><see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model</returns>
        internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
        {
            CimQuery query = baseQuery as CimQuery;

            if (query == null)
            {
                throw new ArgumentNullException("baseQuery");
            }

            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: false);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, targetObject: null);
            StartableJob  queryJob   = query.GetQueryJob(jobContext);

            return(queryJob);
        }
Esempio n. 4
0
 internal EnumerateAssociatedInstancesJob(CimJobContext jobContext, CimQuery cimQuery, CimInstance associatedObject, string associationName, string resultRole, string sourceRole) : base(jobContext, cimQuery)
 {
     this.associatedObject = associatedObject;
     this.associationName  = associationName;
     this.resultRole       = resultRole;
     this.sourceRole       = sourceRole;
 }
Esempio n. 5
0
        private static string GetMatchConditionForEqualityOperator(string propertyName, object propertyValue)
        {
            string str;

            if (propertyValue is char)
            {
                string wqlLiteral = CimQuery.ObjectToWqlLiteral(propertyValue);
                if (!string.IsNullOrWhiteSpace(wqlLiteral))
                {
                    object[] objArray = new object[2];
                    objArray[0] = propertyName;
                    objArray[1] = wqlLiteral;
                    str         = string.Format(CultureInfo.InvariantCulture, "({0} = {1})", objArray);
                    return(str);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                char     chr            = (char)propertyValue;
                char     lowerInvariant = char.ToLowerInvariant(chr);
                char     upperInvariant = char.ToUpperInvariant(chr);
                string   wqlLiteral1    = CimQuery.ObjectToWqlLiteral(lowerInvariant);
                string   str1           = CimQuery.ObjectToWqlLiteral(upperInvariant);
                object[] objArray1      = new object[3];
                objArray1[0] = propertyName;
                objArray1[1] = wqlLiteral1;
                objArray1[2] = str1;
                str          = string.Format(CultureInfo.InvariantCulture, "(({0} = {1}) OR ({0} = {2}))", objArray1);
                return(str);
            }
        }
Esempio n. 6
0
        private static string GetMatchCondition(string propertyName, IEnumerable propertyValues, bool wildcardsEnabled)
        {
            IEnumerable <string> strs = propertyValues.Cast <object>().Select <object, string>((object propertyValue) => {
                if (wildcardsEnabled)
                {
                    return(CimQuery.GetMatchConditionForLikeOperator(propertyName, propertyValue));
                }
                else
                {
                    return(CimQuery.GetMatchConditionForEqualityOperator(propertyName, propertyValue));
                }
            }
                                                                                               );
            List <string> list = strs.Where <string>((string individualCondition) => !string.IsNullOrWhiteSpace(individualCondition)).ToList <string>();

            if (list.Count != 0)
            {
                string str = string.Join(" OR ", list);
                return(str);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 7
0
        public override void FilterByProperty(string propertyName, IEnumerable allowedPropertyValues, bool wildcardsEnabled, BehaviorOnNoMatch behaviorOnNoMatch)
        {
            this.ClientSideQuery.FilterByProperty(propertyName, allowedPropertyValues, wildcardsEnabled, behaviorOnNoMatch);
            string matchCondition = CimQuery.GetMatchCondition(propertyName, allowedPropertyValues, wildcardsEnabled);

            if (!string.IsNullOrWhiteSpace(matchCondition))
            {
                this.AddWqlCondition(matchCondition);
            }
        }
Esempio n. 8
0
        public override void ExcludeByProperty(string propertyName, IEnumerable excludedPropertyValues, bool wildcardsEnabled, BehaviorOnNoMatch behaviorOnNoMatch)
        {
            this.ClientSideQuery.ExcludeByProperty(propertyName, excludedPropertyValues, wildcardsEnabled, behaviorOnNoMatch);
            string matchCondition = CimQuery.GetMatchCondition(propertyName, excludedPropertyValues, wildcardsEnabled);

            if (!string.IsNullOrWhiteSpace(matchCondition))
            {
                object[] objArray = new object[1];
                objArray[0] = matchCondition;
                string str = string.Format(CultureInfo.InvariantCulture, "NOT ({0})", objArray);
                this.AddWqlCondition(str);
            }
        }
Esempio n. 9
0
        private static string GetMatchConditionForLikeOperator(string propertyName, object propertyValue)
        {
            bool            flag            = false;
            string          str             = (string)LanguagePrimitives.ConvertTo(propertyValue, typeof(string), CultureInfo.InvariantCulture);
            WildcardPattern wildcardPattern = new WildcardPattern(str, WildcardOptions.IgnoreCase);
            string          wqlLikeOperand  = CimQuery.WildcardToWqlLikeOperand(wildcardPattern, out flag);

            object[] objArray = new object[2];
            objArray[0] = propertyName;
            objArray[1] = wqlLikeOperand;
            string str1 = string.Format(CultureInfo.InvariantCulture, "({0} LIKE {1})", objArray);

            return(str1);
        }
Esempio n. 10
0
        public override void FilterByMinPropertyValue(string propertyName, object minPropertyValue, BehaviorOnNoMatch behaviorOnNoMatch)
        {
            this.ClientSideQuery.FilterByMinPropertyValue(propertyName, minPropertyValue, behaviorOnNoMatch);
            string wqlLiteral = CimQuery.ObjectToWqlLiteral(minPropertyValue);

            if (!string.IsNullOrWhiteSpace(wqlLiteral))
            {
                object[] objArray = new object[2];
                objArray[0] = propertyName;
                objArray[1] = wqlLiteral;
                string str = string.Format(CultureInfo.InvariantCulture, "{0} >= {1}", objArray);
                this.AddWqlCondition(str);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Modifies the query, so that it does not return objects with a given property value
        /// </summary>
        /// <param name="propertyName">Property name to query on</param>
        /// <param name="excludedPropertyValues">Property values to reject in the query</param>
        /// <param name="wildcardsEnabled">
        /// <c>true</c> if <paramref name="excludedPropertyValues"/> should be treated as a <see cref="System.String"/> containing a wildcard pattern;
        /// <c>false otherwise</c>
        /// </param>
        /// <param name="behaviorOnNoMatch">
        /// Describes how to handle filters that didn't match any objects
        /// </param>
        public override void ExcludeByProperty(string propertyName, IEnumerable excludedPropertyValues, bool wildcardsEnabled, BehaviorOnNoMatch behaviorOnNoMatch)
        {
            this.ClientSideQuery.ExcludeByProperty(propertyName, excludedPropertyValues, wildcardsEnabled, behaviorOnNoMatch);

            string positiveWqlCondition = CimQuery.GetMatchCondition(propertyName, excludedPropertyValues, wildcardsEnabled);

            if (!string.IsNullOrWhiteSpace(positiveWqlCondition))
            {
                string condition = string.Format(
                    CultureInfo.InvariantCulture,
                    "NOT ({0})",
                    positiveWqlCondition);
                this.AddWqlCondition(condition);
            }
        }
Esempio n. 12
0
        internal EnumerateAssociatedInstancesJob(CimJobContext jobContext, CimQuery cimQuery, CimInstance associatedObject, string associationName, string resultRole, string sourceRole)
            : base(jobContext, cimQuery)
        {
            _associatedObject = associatedObject;
            Dbg.Assert(_associatedObject != null, "Caller should verify that associatedObject is not null");

            _associationName = associationName;
            Dbg.Assert(_associationName != null, "Caller should verify that associationName is not null");

            _resultRole = resultRole;
            Dbg.Assert(_resultRole != null, "Caller should verify that resultRole is not null");

            _sourceRole = sourceRole;
            Dbg.Assert(_sourceRole != null, "Caller should verify that sourceRole is not null");
        }
Esempio n. 13
0
        private static string GetMatchConditionForLikeOperator(string propertyName, object propertyValue)
        {
            var expectedPropertyValueAsString             = (string)LanguagePrimitives.ConvertTo(propertyValue, typeof(string), CultureInfo.InvariantCulture);
            var expectedPropertyValueAsPowerShellWildcard = WildcardPattern.Get(expectedPropertyValueAsString, WildcardOptions.IgnoreCase);

            bool needsClientSideFiltering; // not used because for simplicity all results go through post-filtering
            var  expectedPropertyValueAsWqlWildcard = CimQuery.WildcardToWqlLikeOperand(expectedPropertyValueAsPowerShellWildcard, out needsClientSideFiltering);

            string condition = string.Format(
                CultureInfo.InvariantCulture,
                "({0} LIKE {1})",
                propertyName,
                expectedPropertyValueAsWqlWildcard);

            return(condition);
        }
Esempio n. 14
0
        /// <summary>
        /// Modifies the query, so that it returns only objects that have a property value less than or equal to a <paramref name="maxPropertyValue"/> threshold
        /// </summary>
        /// <param name="propertyName">Property name to query on</param>
        /// <param name="maxPropertyValue">Maximum property value</param>
        /// <param name="behaviorOnNoMatch">
        /// Describes how to handle filters that didn't match any objects
        /// </param>
        public override void FilterByMaxPropertyValue(string propertyName, object maxPropertyValue, BehaviorOnNoMatch behaviorOnNoMatch)
        {
            this.ClientSideQuery.FilterByMaxPropertyValue(propertyName, maxPropertyValue, behaviorOnNoMatch);

            string wqlLiteral = CimQuery.ObjectToWqlLiteral(maxPropertyValue);

            if (!string.IsNullOrWhiteSpace(wqlLiteral))
            {
                string condition = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0} <= {1}",
                    propertyName,
                    CimQuery.ObjectToWqlLiteral(maxPropertyValue));
                this.AddWqlCondition(condition);
            }
        }
Esempio n. 15
0
        internal QueryInstancesJob(CimJobContext jobContext, CimQuery cimQuery, string wqlCondition) : base(jobContext, cimQuery)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("SELECT * FROM ");
            stringBuilder.Append(base.JobContext.ClassName);
            stringBuilder.Append(" ");
            stringBuilder.Append(wqlCondition);
            this._wqlQuery = stringBuilder.ToString();
            if (!string.IsNullOrWhiteSpace(wqlCondition))
            {
                if (jobContext.CmdletInvocationContext.CmdletDefinitionContext.UseEnumerateInstancesInsteadOfWql)
                {
                    this._useEnumerateInstances = true;
                }
                return;
            }
            else
            {
                this._useEnumerateInstances = true;
                return;
            }
        }
Esempio n. 16
0
        private static string GetMatchConditionForEqualityOperator(string propertyName, object propertyValue)
        {
            string condition;

            // comparison of 'char' is case-sensitive in WQL (comparison of 'string' is case-insensitive)
            if (propertyValue is char)
            {
                char   c                = (char)propertyValue;
                char   lowerCase        = char.ToLowerInvariant(c);
                char   upperCase        = char.ToUpperInvariant(c);
                string lowerCaseLiteral = CimQuery.ObjectToWqlLiteral(lowerCase);
                string upperCaseLiteral = CimQuery.ObjectToWqlLiteral(upperCase);
                Dbg.Assert(!string.IsNullOrWhiteSpace(lowerCaseLiteral), "All characters are assumed to have valid WQL literals (lower)");
                Dbg.Assert(!string.IsNullOrWhiteSpace(upperCaseLiteral), "All characters are assumed to have valid WQL literals (upper)");
                condition = string.Format(
                    CultureInfo.InvariantCulture,
                    "(({0} = {1}) OR ({0} = {2}))",
                    propertyName,
                    lowerCaseLiteral,
                    upperCaseLiteral);
                return(condition);
            }

            string wqlLiteral = CimQuery.ObjectToWqlLiteral(propertyValue);

            if (string.IsNullOrWhiteSpace(wqlLiteral))
            {
                return(null);
            }

            condition = string.Format(
                CultureInfo.InvariantCulture,
                "({0} = {1})",
                propertyName,
                wqlLiteral);
            return(condition);
        }
Esempio n. 17
0
 internal QueryJobBase(CimJobContext jobContext, CimQuery cimQuery)
     : base(jobContext)
 {
     Dbg.Assert(cimQuery != null, "Caller should verify cimQuery != null");
     _cimQuery = cimQuery;
 }
Esempio n. 18
0
 private static string ObjectToWqlLiteral(object o)
 {
     if (!LanguagePrimitives.IsNull(o))
     {
         o = CimValueConverter.ConvertFromDotNetToCim(o);
         PSObject pSObject = PSObject.AsPSObject(o);
         Type     type     = pSObject.BaseObject.GetType();
         TypeCode typeCode = LanguagePrimitives.GetTypeCode(type);
         if (typeCode != TypeCode.String)
         {
             if (typeCode != TypeCode.Char)
             {
                 if (typeCode != TypeCode.DateTime)
                 {
                     if (type != typeof(TimeSpan))
                     {
                         if (!LanguagePrimitives.IsNumeric(typeCode))
                         {
                             if (!LanguagePrimitives.IsBooleanType(type))
                             {
                                 throw CimValueConverter.GetInvalidCastException(null, "InvalidCimQueryCast", o, CmdletizationResources.CimConversion_WqlQuery);
                             }
                             else
                             {
                                 if (!(bool)LanguagePrimitives.ConvertTo(o, typeof(bool), CultureInfo.InvariantCulture))
                                 {
                                     return("FALSE");
                                 }
                                 else
                                 {
                                     return("TRUE");
                                 }
                             }
                         }
                         else
                         {
                             return((string)LanguagePrimitives.ConvertTo(o, typeof(string), CultureInfo.InvariantCulture));
                         }
                     }
                     else
                     {
                         return(null);
                     }
                 }
                 else
                 {
                     DateTime dateTime     = (DateTime)LanguagePrimitives.ConvertTo(o, typeof(DateTime), CultureInfo.InvariantCulture);
                     string   dmtfDateTime = ManagementDateTimeConverter.ToDmtfDateTime(dateTime);
                     return(string.Concat("'", dmtfDateTime, "'"));
                 }
             }
             else
             {
                 return(CimQuery.ObjectToWqlLiteral(LanguagePrimitives.ConvertTo(o, typeof(string), CultureInfo.InvariantCulture)));
             }
         }
         else
         {
             string str = o.ToString();
             str = str.Replace("\\", "\\\\");
             str = str.Replace("'", "\\'");
             return(string.Concat("'", str, "'"));
         }
     }
     else
     {
         return("null");
     }
 }
Esempio n. 19
0
        private static string WildcardToWqlLikeOperand(WildcardPattern wildcardPattern, out bool needsClientSideFiltering)
        {
            string str = WildcardPatternToCimQueryParser.Parse(wildcardPattern, out needsClientSideFiltering);

            return(CimQuery.ObjectToWqlLiteral(str));
        }
Esempio n. 20
0
 internal QueryJobBase(CimJobContext jobContext, CimQuery cimQuery) : base(jobContext)
 {
     this._cimQuery = cimQuery;
 }