/// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="key">Search KEY.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>key</b> is null reference.</exception>
        public IMAP_Search_Key_Not(IMAP_Search_Key key)
        {
            if(key == null){
                throw new ArgumentNullException("key");
            }

            m_pSearchKey = key;
        }
Esempio n. 2
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="key">Search KEY.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>key</b> is null reference.</exception>
        public IMAP_Search_Key_Not(IMAP_Search_Key key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            m_pSearchKey = key;
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="key1">Search key1.</param>
        /// <param name="key2">Search key2.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>key1</b> or <b>key2</b> is null reference.</exception>
        public IMAP_Search_Key_Or(IMAP_Search_Key key1,IMAP_Search_Key key2)
        {
            if(key1 == null){
                throw new ArgumentNullException("key1");
            }
            if(key2 == null){
                throw new ArgumentNullException("key2");
            }

            m_pSearchKey1 = key1;
            m_pSearchKey2 = key2;
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="key1">Search key1.</param>
        /// <param name="key2">Search key2.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>key1</b> or <b>key2</b> is null reference.</exception>
        public IMAP_Search_Key_Or(IMAP_Search_Key key1, IMAP_Search_Key key2)
        {
            if (key1 == null)
            {
                throw new ArgumentNullException("key1");
            }
            if (key2 == null)
            {
                throw new ArgumentNullException("key2");
            }

            m_pSearchKey1 = key1;
            m_pSearchKey2 = key2;
        }
        /// <summary>
        /// Returns parsed IMAP SEARCH <b>OR (search-key1) (search-key2)</b> key.
        /// </summary>
        /// <param name="r">String reader.</param>
        /// <returns>Returns parsed IMAP SEARCH <b>OR (search-key1) (search-key2)</b> key.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>r</b> is null reference.</exception>
        /// <exception cref="ParseException">Is raised when parsing fails.</exception>
        internal static IMAP_Search_Key_Or Parse(StringReader r)
        {
            if (r == null)
            {
                throw new ArgumentNullException("r");
            }

            string word = r.ReadWord();

            if (!string.Equals(word, "OR", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ParseException("Parse error: Not a SEARCH 'OR' key.");
            }

            return(new IMAP_Search_Key_Or(IMAP_Search_Key.ParseKey(r), IMAP_Search_Key.ParseKey(r)));
        }
Esempio n. 6
0
        /// <summary>
        /// Returns parsed IMAP SEARCH <b>NOT (search-key)</b> key.
        /// </summary>
        /// <param name="r">String reader.</param>
        /// <returns>Returns parsed IMAP SEARCH <b>NOT (search-key)</b> key.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>r</b> is null reference.</exception>
        /// <exception cref="ParseException">Is raised when parsing fails.</exception>
        internal static IMAP_Search_Key_Not Parse(StringReader r)
        {
            if (r == null)
            {
                throw new ArgumentNullException("r");
            }

            string word = r.ReadWord();

            if (!string.Equals(word, "NOT", Helpers.GetDefaultIgnoreCaseComparison()))
            {
                throw new ParseException("Parse error: Not a SEARCH 'NOT' key.");
            }

            return(new IMAP_Search_Key_Not(IMAP_Search_Key.ParseKey(r)));
        }
        /// <summary>
        /// Returns parsed IMAP SEARCH <b>AND</b> key group.
        /// </summary>
        /// <param name="r">String reader.</param>
        /// <returns>Returns parsed IMAP SEARCH <b>AND</b> key group.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>r</b> is null reference.</exception>
        /// <exception cref="ParseException">Is raised when parsing fails.</exception>
        public static IMAP_Search_Key_Group Parse(StringReader r)
        {
            if (r == null)
            {
                throw new ArgumentNullException("r");
            }

            // Remove parenthesis, if any.
            if (r.StartsWith("("))
            {
                r = new StringReader(r.ReadParenthesized());
            }

            IMAP_Search_Key_Group retVal = new IMAP_Search_Key_Group();

            r.ReadToFirstChar();
            while (r.Available > 0)
            {
                retVal.m_pKeys.Add(IMAP_Search_Key.ParseKey(r));
            }

            return(retVal);
        }
Esempio n. 8
0
        private string SearchCriteriaToSql(IMAP_Search_Key key, Dictionary<long, long> seqNo_to_UID)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            StringBuilder stringBuilder = new StringBuilder();
            if (key is IMAP_Search_Key_Group)
            {
                stringBuilder.Append("(");
                IMAP_Search_Key_Group iMAP_Search_Key_Group = (IMAP_Search_Key_Group)key;
                for (int i = 0; i < iMAP_Search_Key_Group.Keys.Count; i++)
                {
                    if (i > 0)
                    {
                        stringBuilder.Append(" AND ");
                    }
                    stringBuilder.Append(this.SearchCriteriaToSql(iMAP_Search_Key_Group.Keys[i], seqNo_to_UID));
                }
                stringBuilder.Append(")");
            }
            else
            {
                if (key is IMAP_Search_Key_All)
                {
                    stringBuilder.Append("UID > -1");
                }
                else
                {
                    if (key is IMAP_Search_Key_Answered)
                    {
                        stringBuilder.Append("Flags like '%Answered%'");
                    }
                    else
                    {
                        if (key is IMAP_Search_Key_Before)
                        {
                            stringBuilder.Append("InternalDate < " + ((IMAP_Search_Key_Before)key).Date.ToString("yyyyMMdd"));
                        }
                        else
                        {
                            if (key is IMAP_Search_Key_Bcc)
                            {
                                stringBuilder.Append("Header_Bcc like '%" + ((IMAP_Search_Key_Bcc)key).Value + "%'");
                            }
                            else
                            {
                                if (key is IMAP_Search_Key_Body)
                                {
                                    stringBuilder.Append("TextParts like '%" + ((IMAP_Search_Key_Body)key).Value + "%'");
                                }
                                else
                                {
                                    if (key is IMAP_Search_Key_Cc)
                                    {
                                        stringBuilder.Append("Header_Cc like '%" + ((IMAP_Search_Key_Cc)key).Value + "%'");
                                    }
                                    else
                                    {
                                        if (key is IMAP_Search_Key_Deleted)
                                        {
                                            stringBuilder.Append("Flags like '%Deleted%'");
                                        }
                                        else
                                        {
                                            if (key is IMAP_Search_Key_Draft)
                                            {
                                                stringBuilder.Append("Flags like '%Draft%'");
                                            }
                                            else
                                            {
                                                if (key is IMAP_Search_Key_Flagged)
                                                {
                                                    stringBuilder.Append("Flags like '%Flagged%'");
                                                }
                                                else
                                                {
                                                    if (key is IMAP_Search_Key_From)
                                                    {
                                                        stringBuilder.Append("Header_From like '%" + ((IMAP_Search_Key_From)key).Value + "%'");
                                                    }
                                                    else
                                                    {
                                                        if (key is IMAP_Search_Key_Header)
                                                        {
                                                            IMAP_Search_Key_Header iMAP_Search_Key_Header = (IMAP_Search_Key_Header)key;
                                                            if (string.IsNullOrEmpty(iMAP_Search_Key_Header.Value))
                                                            {
                                                                stringBuilder.Append("HeaderDecoded REGEXP '(\\n)*" + Regex.Escape(iMAP_Search_Key_Header.FieldName) + "\\s*:{1}.*'");
                                                            }
                                                            else
                                                            {
                                                                stringBuilder.Append(string.Concat(new string[]
																{
																	"HeaderDecoded REGEXP '(\\n)*",
																	Regex.Escape(iMAP_Search_Key_Header.FieldName),
																	"\\s*:{1}.*(\\r\\n\\s|\\n\\s)*.*",
																	Regex.Escape(iMAP_Search_Key_Header.Value),
																	".*'"
																}));
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (key is IMAP_Search_Key_Keyword)
                                                            {
                                                                stringBuilder.Append("Flags like '%" + ((IMAP_Search_Key_Keyword)key).Value + "%'");
                                                            }
                                                            else
                                                            {
                                                                if (key is IMAP_Search_Key_Larger)
                                                                {
                                                                    stringBuilder.Append("Size > " + ((IMAP_Search_Key_Larger)key).Value);
                                                                }
                                                                else
                                                                {
                                                                    if (key is IMAP_Search_Key_New)
                                                                    {
                                                                        stringBuilder.Append("(Flags like '%Recent%' and Flags not like '%Seen%')");
                                                                    }
                                                                    else
                                                                    {
                                                                        if (key is IMAP_Search_Key_Not)
                                                                        {
                                                                            stringBuilder.Append("not " + this.SearchCriteriaToSql(((IMAP_Search_Key_Not)key).SearchKey, seqNo_to_UID));
                                                                        }
                                                                        else
                                                                        {
                                                                            if (key is IMAP_Search_Key_Old)
                                                                            {
                                                                                stringBuilder.Append("Flags not like '%Recent%'");
                                                                            }
                                                                            else
                                                                            {
                                                                                if (key is IMAP_Search_Key_On)
                                                                                {
                                                                                    stringBuilder.Append("InternalDate = " + ((IMAP_Search_Key_On)key).Date.ToString("yyyyMMdd"));
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (key is IMAP_Search_Key_Or)
                                                                                    {
                                                                                        stringBuilder.Append(string.Concat(new string[]
																						{
																							"(",
																							this.SearchCriteriaToSql(((IMAP_Search_Key_Or)key).SearchKey1, seqNo_to_UID),
																							" or ",
																							this.SearchCriteriaToSql(((IMAP_Search_Key_Or)key).SearchKey2, seqNo_to_UID),
																							")"
																						}));
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        if (key is IMAP_Search_Key_Recent)
                                                                                        {
                                                                                            stringBuilder.Append("Flags like '%Recent%'");
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (key is IMAP_Search_Key_Seen)
                                                                                            {
                                                                                                stringBuilder.Append("Flags like '%Seen%'");
                                                                                            }
                                                                                            else
                                                                                            {
                                                                                                if (key is IMAP_Search_Key_SentBefore)
                                                                                                {
                                                                                                    stringBuilder.Append("Header_Date < " + ((IMAP_Search_Key_SentBefore)key).Date.ToString("yyyyMMdd"));
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (key is IMAP_Search_Key_SentOn)
                                                                                                    {
                                                                                                        stringBuilder.Append("Header_Date = " + ((IMAP_Search_Key_SentOn)key).Date.ToString("yyyyMMdd"));
                                                                                                    }
                                                                                                    else
                                                                                                    {
                                                                                                        if (key is IMAP_Search_Key_SentSince)
                                                                                                        {
                                                                                                            stringBuilder.Append("Header_Date >= " + ((IMAP_Search_Key_SentSince)key).Date.ToString("yyyyMMdd"));
                                                                                                        }
                                                                                                        else
                                                                                                        {
                                                                                                            if (key is IMAP_Search_Key_Since)
                                                                                                            {
                                                                                                                stringBuilder.Append("InternalDate >= " + ((IMAP_Search_Key_Since)key).Date.ToString("yyyyMMdd"));
                                                                                                            }
                                                                                                            else
                                                                                                            {
                                                                                                                if (key is IMAP_Search_Key_Smaller)
                                                                                                                {
                                                                                                                    stringBuilder.Append("Size < " + ((IMAP_Search_Key_Smaller)key).Value);
                                                                                                                }
                                                                                                                else
                                                                                                                {
                                                                                                                    if (key is IMAP_Search_Key_Subject)
                                                                                                                    {
                                                                                                                        stringBuilder.Append("Header_Subject like '%" + ((IMAP_Search_Key_Subject)key).Value + "%'");
                                                                                                                    }
                                                                                                                    else
                                                                                                                    {
                                                                                                                        if (key is IMAP_Search_Key_Text)
                                                                                                                        {
                                                                                                                            stringBuilder.Append(string.Concat(new string[]
																															{
																																"(StructureDecoded like '%",
																																((IMAP_Search_Key_Text)key).Value,
																																"%' OR TextParts like '%",
																																((IMAP_Search_Key_Text)key).Value,
																																"%')"
																															}));
                                                                                                                        }
                                                                                                                        else
                                                                                                                        {
                                                                                                                            if (key is IMAP_Search_Key_To)
                                                                                                                            {
                                                                                                                                stringBuilder.Append("Header_To like '%" + ((IMAP_Search_Key_To)key).Value + "%'");
                                                                                                                            }
                                                                                                                            else
                                                                                                                            {
                                                                                                                                if (key is IMAP_Search_Key_SeqSet)
                                                                                                                                {
                                                                                                                                    stringBuilder.Append("(");
                                                                                                                                    IMAP_t_SeqSet value = ((IMAP_Search_Key_SeqSet)key).Value;
                                                                                                                                    for (int j = 0; j < value.Items.Length; j++)
                                                                                                                                    {
                                                                                                                                        Range_long range_long = value.Items[j];
                                                                                                                                        if (j > 0)
                                                                                                                                        {
                                                                                                                                            stringBuilder.Append(" OR ");
                                                                                                                                        }
                                                                                                                                        long num = 0L;
                                                                                                                                        long num2 = 0L;
                                                                                                                                        if (!seqNo_to_UID.TryGetValue(range_long.Start, out num))
                                                                                                                                        {
                                                                                                                                            num = 2147483647L;
                                                                                                                                        }
                                                                                                                                        if (!seqNo_to_UID.TryGetValue(range_long.End, out num2))
                                                                                                                                        {
                                                                                                                                            num2 = 2147483647L;
                                                                                                                                        }
                                                                                                                                        if (num == num2)
                                                                                                                                        {
                                                                                                                                            stringBuilder.Append("UID = " + num);
                                                                                                                                        }
                                                                                                                                        else
                                                                                                                                        {
                                                                                                                                            stringBuilder.Append(string.Concat(new object[]
																																			{
																																				"UID >= ",
																																				num,
																																				" AND UID <= ",
																																				num2
																																			}));
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                    stringBuilder.Append(")");
                                                                                                                                }
                                                                                                                                else
                                                                                                                                {
                                                                                                                                    if (key is IMAP_Search_Key_Uid)
                                                                                                                                    {
                                                                                                                                        stringBuilder.Append("(");
                                                                                                                                        IMAP_t_SeqSet value2 = ((IMAP_Search_Key_Uid)key).Value;
                                                                                                                                        for (int k = 0; k < value2.Items.Length; k++)
                                                                                                                                        {
                                                                                                                                            Range_long range_long2 = value2.Items[k];
                                                                                                                                            if (k > 0)
                                                                                                                                            {
                                                                                                                                                stringBuilder.Append(" OR ");
                                                                                                                                            }
                                                                                                                                            if (range_long2.Start == range_long2.End)
                                                                                                                                            {
                                                                                                                                                stringBuilder.Append("UID = " + range_long2.Start);
                                                                                                                                            }
                                                                                                                                            else
                                                                                                                                            {
                                                                                                                                                stringBuilder.Append(string.Concat(new object[]
																																				{
																																					"UID >= ",
																																					range_long2.Start,
																																					" AND UID <= ",
																																					range_long2.End
																																				}));
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                        stringBuilder.Append(")");
                                                                                                                                    }
                                                                                                                                    else
                                                                                                                                    {
                                                                                                                                        if (key is IMAP_Search_Key_Unanswered)
                                                                                                                                        {
                                                                                                                                            stringBuilder.Append("Flags not like '%Answered%'");
                                                                                                                                        }
                                                                                                                                        else
                                                                                                                                        {
                                                                                                                                            if (key is IMAP_Search_Key_Undeleted)
                                                                                                                                            {
                                                                                                                                                stringBuilder.Append("Flags not like '%Deleted%'");
                                                                                                                                            }
                                                                                                                                            else
                                                                                                                                            {
                                                                                                                                                if (key is IMAP_Search_Key_Undraft)
                                                                                                                                                {
                                                                                                                                                    stringBuilder.Append("Flags not like '%Draft%'");
                                                                                                                                                }
                                                                                                                                                else
                                                                                                                                                {
                                                                                                                                                    if (key is IMAP_Search_Key_Unflagged)
                                                                                                                                                    {
                                                                                                                                                        stringBuilder.Append("Flags not like '%Flagged%'");
                                                                                                                                                    }
                                                                                                                                                    else
                                                                                                                                                    {
                                                                                                                                                        if (key is IMAP_Search_Key_Keyword)
                                                                                                                                                        {
                                                                                                                                                            stringBuilder.Append("Flags not like '%" + ((IMAP_Search_Key_Unkeyword)key).Value + "%'");
                                                                                                                                                        }
                                                                                                                                                        else
                                                                                                                                                        {
                                                                                                                                                            if (key is IMAP_Search_Key_Unseen)
                                                                                                                                                            {
                                                                                                                                                                stringBuilder.Append("Flags not like '%Seen%'");
                                                                                                                                                            }
                                                                                                                                                            else
                                                                                                                                                            {
                                                                                                                                                                stringBuilder.Append("'' = ''");
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return stringBuilder.ToString();
        }
Esempio n. 9
0
        /// <summary>
        /// Converts IMAP search criteria to "sql where" filter.
        /// </summary>
        /// <param name="key">IMAP search key.</param>
        /// <param name="seqNo_to_UID">Sequence number to UI map table.</param>
        /// <returns>Returns "sql where" filter.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>key</b> is null reference.</exception>
        private string SearchCriteriaToSql(IMAP_Search_Key key,Dictionary<long,long> seqNo_to_UID)
        {
            if(key == null){
                throw new ArgumentNullException("key");
            }

            StringBuilder retVal = new StringBuilder();                             
                
            // KEY GROUP
            if(key is IMAP_Search_Key_Group){
                retVal.Append("(");

                IMAP_Search_Key_Group group = ((IMAP_Search_Key_Group)key);
                for(int i=0;i<group.Keys.Count;i++){
                    if(i > 0){
                        retVal.Append(" AND ");
                    }

                    retVal.Append(SearchCriteriaToSql(group.Keys[i],seqNo_to_UID));
                }

                retVal.Append(")");
            }
            // ALL
            else if(key is IMAP_Search_Key_All){
                // Just dummy true expression.
                retVal.Append("UID > -1");
            }
            // ANSWERED
            else if(key is IMAP_Search_Key_Answered){
                retVal.Append("Flags like '%Answered%'");
            }
            // BEFORE
            else if(key is IMAP_Search_Key_Before){
                retVal.Append("InternalDate < " + ((IMAP_Search_Key_Before)key).Date.ToString("yyyyMMdd"));
            }
            // BCC
            else if(key is IMAP_Search_Key_Bcc){
                retVal.Append("Header_Bcc like '%" + ((IMAP_Search_Key_Bcc)key).Value + "%'");
            }
            // BODY
            else if(key is IMAP_Search_Key_Body){
                retVal.Append("TextParts like '%" + ((IMAP_Search_Key_Body)key).Value + "%'");
            }
            // CC
            else if(key is IMAP_Search_Key_Cc){
                retVal.Append("Header_Cc like '%" + ((IMAP_Search_Key_Cc)key).Value + "%'");
            }
            // DELETED
            else if(key is IMAP_Search_Key_Deleted){
                 retVal.Append("Flags like '%Deleted%'");
            }
            // DRAFT
            else if(key is IMAP_Search_Key_Draft){
                 retVal.Append("Flags like '%Draft%'");
            }
            // FLAGGED
            else if(key is IMAP_Search_Key_Flagged){
                retVal.Append("Flags like '%Flagged%'");
            }
            // FROM
            else if(key is IMAP_Search_Key_From){
                retVal.Append("Header_From like '%" + ((IMAP_Search_Key_From)key).Value + "%'");
            }
            // HEADER
            else if(key is IMAP_Search_Key_Header){                
                IMAP_Search_Key_Header k = (IMAP_Search_Key_Header)key;
                if(string.IsNullOrEmpty(k.Value)){
                    retVal.Append("HeaderDecoded REGEXP '(\\n)*" + Regex.Escape(k.FieldName) +"\\s*:{1}.*'");
                }
                else{                    
                    retVal.Append("HeaderDecoded REGEXP '(\\n)*" + Regex.Escape(k.FieldName) +"\\s*:{1}.*(\\r\\n\\s|\\n\\s)*.*" + Regex.Escape(k.Value) + ".*'");
                }         
            }
            // KEYWORD
            else if(key is IMAP_Search_Key_Keyword){
                retVal.Append("Flags like '%" + ((IMAP_Search_Key_Keyword)key).Value + "%'");
            }
            // LARGER
            else if(key is IMAP_Search_Key_Larger){
                retVal.Append("Size > " + ((IMAP_Search_Key_Larger)key).Value);
            }
            // NEW
            else if(key is IMAP_Search_Key_New){
                retVal.Append("(Flags like '%Recent%' and Flags not like '%Seen%')");
            }
            // NOT
            else if(key is IMAP_Search_Key_Not){
                retVal.Append("not " + SearchCriteriaToSql(((IMAP_Search_Key_Not)key).SearchKey,seqNo_to_UID));
            }
            // OLD
            else if(key is IMAP_Search_Key_Old){
                retVal.Append("Flags not like '%Recent%'");
            }
            // ON
            else if(key is IMAP_Search_Key_On){
                retVal.Append("InternalDate = " + ((IMAP_Search_Key_On)key).Date.ToString("yyyyMMdd"));
            }
            // OR
            else if(key is IMAP_Search_Key_Or){
                retVal.Append("(" + SearchCriteriaToSql(((IMAP_Search_Key_Or)key).SearchKey1,seqNo_to_UID) + " or " + SearchCriteriaToSql(((IMAP_Search_Key_Or)key).SearchKey2,seqNo_to_UID) + ")");
            }
            // RECENT
            else if(key is IMAP_Search_Key_Recent){
                retVal.Append("Flags like '%Recent%'");
            }
            // SEEN
            else if(key is IMAP_Search_Key_Seen){
                retVal.Append("Flags like '%Seen%'");
            }
            // SENTBEFORE
            else if(key is IMAP_Search_Key_SentBefore){
                retVal.Append("Header_Date < " + ((IMAP_Search_Key_SentBefore)key).Date.ToString("yyyyMMdd"));
            }
            // SENTON
            else if(key is IMAP_Search_Key_SentOn){
                retVal.Append("Header_Date = " + ((IMAP_Search_Key_SentOn)key).Date.ToString("yyyyMMdd"));
            }
            // SENTSINCE
            else if(key is IMAP_Search_Key_SentSince){
                retVal.Append("Header_Date >= " + ((IMAP_Search_Key_SentSince)key).Date.ToString("yyyyMMdd"));
            }
            // SINCE
            else if(key is IMAP_Search_Key_Since){
                retVal.Append("InternalDate >= " + ((IMAP_Search_Key_Since)key).Date.ToString("yyyyMMdd"));
            }
            // SMALLER
            else if(key is IMAP_Search_Key_Smaller){
                retVal.Append("Size < " + ((IMAP_Search_Key_Smaller)key).Value);
            }
            // SUBJECT
            else if(key is IMAP_Search_Key_Subject){
                retVal.Append("Header_Subject like '%" + ((IMAP_Search_Key_Subject)key).Value + "%'");
            }
            // TEXT
            else if(key is IMAP_Search_Key_Text){
                retVal.Append("(StructureDecoded like '%" + ((IMAP_Search_Key_Text)key).Value + "%' OR TextParts like '%" + ((IMAP_Search_Key_Text)key).Value + "%')");
            }
            // TO
            else if(key is IMAP_Search_Key_To){
                retVal.Append("Header_To like '%" + ((IMAP_Search_Key_To)key).Value + "%'");
            }
            else if(key is IMAP_Search_Key_SeqSet){
                retVal.Append("(");

                IMAP_t_SeqSet seqSet = ((IMAP_Search_Key_SeqSet)key).Value;
                for(int i=0;i<seqSet.Items.Length;i++){
                    Range_long range = seqSet.Items[i];

                    if(i > 0){
                        retVal.Append(" OR ");
                    }

                    long start = 0;                    
                    long end   = 0;
                    if(!seqNo_to_UID.TryGetValue(range.Start,out start)){
                        start = int.MaxValue;
                    }
                    if(!seqNo_to_UID.TryGetValue(range.End,out end)){
                        end = int.MaxValue;
                    }

                    if(start == end){
                        retVal.Append("UID = " + start);
                    }
                    else{
                        retVal.Append("UID >= " + start + " AND UID <= " + end);
                    }
                }

                retVal.Append(")");
            }
            // UID
            else if(key is IMAP_Search_Key_Uid){
                retVal.Append("(");

                IMAP_t_SeqSet seqSet = ((IMAP_Search_Key_Uid)key).Value;
                for(int i=0;i<seqSet.Items.Length;i++){
                    Range_long range = seqSet.Items[i];

                    if(i > 0){
                        retVal.Append(" OR ");
                    }

                    if(range.Start == range.End){
                        retVal.Append("UID = " + range.Start);
                    }
                    else{
                        retVal.Append("UID >= " + range.Start + " AND UID <= " + range.End);
                    }
                }

                retVal.Append(")");
            }
            // UNANSWERED
            else if(key is IMAP_Search_Key_Unanswered){
                retVal.Append("Flags not like '%Answered%'");
            }
            // UNDELETED
            else if(key is IMAP_Search_Key_Undeleted){
                retVal.Append("Flags not like '%Deleted%'");
            }
            // UNDRAFT
            else if(key is IMAP_Search_Key_Undraft){
                retVal.Append("Flags not like '%Draft%'");
            }
            // UNFLAGGED
            else if(key is IMAP_Search_Key_Unflagged){
                retVal.Append("Flags not like '%Flagged%'");
            }
            // UNKEYWORD
            else if(key is IMAP_Search_Key_Keyword){
                retVal.Append("Flags not like '%" + ((IMAP_Search_Key_Unkeyword)key).Value + "%'");
            }
            // UNSEEN
            else if(key is IMAP_Search_Key_Unseen){
                retVal.Append("Flags not like '%Seen%'");
            }
            else{
                // Because SQL lite won't allow empty (), add some true-condition.
                retVal.Append("'' = ''");
            }

            return retVal.ToString();
        }