/* get the sql search condition of this/these field/fields */ string IFreeTextSearchField.GetSqlSearchCondition(string searchString, TextCompareOperator compareOperator, MatchRule fieldMatchRule) { StringBuilder sql = new StringBuilder(); if (this.ContainsField(Title)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Title", searchString), fieldMatchRule); } if (this.ContainsField(LoanedTo)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Loaned_To", searchString), fieldMatchRule); } if (this.ContainsField(Description)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Description", searchString), fieldMatchRule); } if (this.ContainsField(Keywords)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Keywords", searchString), fieldMatchRule); } return(sql.ToString()); }
public FreeTextSearchCriteria(string searchString, IFreeTextSearchField fields, TextCompareOperator compareOperator, MatchRule fieldMatchRule) { if (searchString == null) throw new ArgumentNullException("searchString"); if (searchString.Length < VolumeDatabase.MIN_SEARCHSTR_LENGTH) throw new ArgumentException(string.Format("Length of a searchstring must be at least {0}", VolumeDatabase.MIN_SEARCHSTR_LENGTH), "searchString"); // if (fields == FreeTextSearchField.None) if (fields == null || fields.IsEmpty) throw new ArgumentException("No searchfield specified", "fields"); this.searchString = searchString.Replace("'","''"); this.fields = fields; this.compareOperator = compareOperator; this.fieldMatchRule = fieldMatchRule; }
/* get the sql search condition of this/these field/fields */ string IFreeTextSearchField.GetSqlSearchCondition(string searchString, TextCompareOperator compareOperator, MatchRule fieldMatchRule) { StringBuilder sql = new StringBuilder(); if (this.ContainsField(AnyName)) { // search name fields of _all_ possible volume itemtypes (e.g. DirectoryName, FileName, ...) SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.Name", searchString), fieldMatchRule); } else { if (this.ContainsField(DirectoryName)) { SearchUtils.Append( sql, compareOperator.GetSqlCompareString("Items.Name", searchString) + string.Format(" AND (Items.ItemType = {0})", (int)VolumeItemType.DirectoryVolumeItem), fieldMatchRule ); } if (this.ContainsField(FileName)) { SearchUtils.Append( sql, compareOperator.GetSqlCompareString("Items.Name", searchString) + string.Format(" AND (Items.ItemType = {0})", (int)VolumeItemType.FileVolumeItem), fieldMatchRule ); } } if (this.ContainsField(Keywords)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.Keywords", searchString), fieldMatchRule); } if (this.ContainsField(Location)) { SearchUtils.Append( sql, compareOperator.GetSqlCompareString("Items.Location", searchString) + string.Format(" AND ((Items.ItemType = {0}) OR (Items.ItemType = {1}))", (int)VolumeItemType.FileVolumeItem, (int)VolumeItemType.DirectoryVolumeItem), fieldMatchRule ); } if (this.ContainsField(Note)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.Note", searchString), fieldMatchRule); } #if ALLOW_FREETEXTSEARCH_MIMETYPE if (this.ContainsField(MimeType)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.MimeType", searchString), fieldMatchRule); } #endif #if ALLOW_FREETEXTSEARCH_METADATA if (this.ContainsField(MetaData)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.MetaData", searchString), fieldMatchRule); } #endif return(sql.ToString()); }
// TODO : // word, number and phrase terms are currently only compared against database field 'Title'. // they should probably compared to other common database fields as well. // but this would slow down searching a lot.. // maybe some smarter table indexers settings would help. internal override void OnTermParsed(TermParsedEventArgs e, out ISearchCriteria criteria) { criteria = null; switch (e.TermType) { case TermType.Number: criteria = new FreeTextSearchCriteria(e.Number.ToString(), FreeTextSearchField.Title, TextCompareOperator.Contains); break; case TermType.Phrase: criteria = new FreeTextSearchCriteria(e.Phrase, FreeTextSearchField.Title, TextCompareOperator.Contains); break; case TermType.Word: criteria = new FreeTextSearchCriteria(e.Word, FreeTextSearchField.Title, TextCompareOperator.Contains); break; case TermType.Select: string keyword = e.Keyword.ToUpper(); // try to map the keyword to a volumes quantity field if (quantityFields.ContainsKey(keyword)) { long byteSize = e.Number; if (byteSize == -1L) { try { byteSize = GetByteSize(e.Word); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Operand for keyword '{0}' must be a number with an optional multiplier"), e.Keyword), "euslQuery"); } } CompareOperator cOp = CompareOperator.Equal; try { cOp = GetNumericCompareOpForRelation(e.Relation); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Invalid compare operator for keyword '{0}'"), e.Keyword), "euslQuery"); } criteria = new QuantitySearchCriteria(quantityFields[keyword], byteSize, cOp); } else { // try to map the keyword to freetextsearch fields IFreeTextSearchField sf; try { sf = FreeTextSearchField.FromString(e.Keyword); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Unknown keyword '{0}'"), e.Keyword), "euslQuery"); } TextCompareOperator tcOp = TextCompareOperator.Contains; try { tcOp = GetTextCompareOpForRelation(e.Relation); } catch (ArgumentException) { throw new ArgumentException( S._("Invalid compare operator for a keyword that maps to textual content"), "euslQuery"); } string s; if (e.Number != -1L) { s = e.Number.ToString(); } else if (e.Word != null) { s = e.Word; } else { s = e.Phrase; } // throws argument exception if searchstr is too short criteria = new FreeTextSearchCriteria(s, sf, tcOp); } break; } if (e.ExcludeTerm) { criteria = new ExcludedSearchCriteria(criteria); } }
public FreeTextSearchCriteria(string searchString, IFreeTextSearchField fields, TextCompareOperator compareOperator) : this(searchString, fields, compareOperator, MatchRule.AnyMustMatch) { }
/* get the sql search condition of this/these field/fields */ string IFreeTextSearchField.GetSqlSearchCondition(string searchString, TextCompareOperator compareOperator, MatchRule fieldMatchRule) { StringBuilder sql = new StringBuilder(); if (this.ContainsField(AnyName)) { // search name fields of _all_ possible volume itemtypes (e.g. DirectoryName, FileName, ...) SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.Name", searchString), fieldMatchRule); } else { if (this.ContainsField(DirectoryName)) { SearchUtils.Append( sql, compareOperator.GetSqlCompareString("Items.Name", searchString) + string.Format(" AND (Items.ItemType = {0})", (int)VolumeItemType.DirectoryVolumeItem), fieldMatchRule ); } if (this.ContainsField(FileName)) { SearchUtils.Append( sql, compareOperator.GetSqlCompareString("Items.Name", searchString) + string.Format(" AND (Items.ItemType = {0})", (int)VolumeItemType.FileVolumeItem), fieldMatchRule ); } } if (this.ContainsField(Keywords)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.Keywords", searchString), fieldMatchRule); } if (this.ContainsField(Location)) { SearchUtils.Append( sql, compareOperator.GetSqlCompareString("Items.Location", searchString) + string.Format(" AND ((Items.ItemType = {0}) OR (Items.ItemType = {1}))", (int)VolumeItemType.FileVolumeItem, (int)VolumeItemType.DirectoryVolumeItem), fieldMatchRule ); } if (this.ContainsField(Note)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.Note", searchString), fieldMatchRule); } #if ALLOW_FREETEXTSEARCH_MIMETYPE if (this.ContainsField(MimeType)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.MimeType", searchString), fieldMatchRule); } #endif #if ALLOW_FREETEXTSEARCH_METADATA if (this.ContainsField(MetaData)) { SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Items.MetaData", searchString), fieldMatchRule); } #endif return sql.ToString(); }
// TODO : // word, number and phrase terms are currently only compared against database field 'Name'. // they should probably compared to other common database fields as well. // but this would slow down searching a lot.. // maybe some smarter table indexers settings would help. internal override void OnTermParsed(TermParsedEventArgs e, out ISearchCriteria criteria) { criteria = null; switch (e.TermType) { case TermType.Number: criteria = new FreeTextSearchCriteria(e.Number.ToString(), FreeTextSearchField.AnyName, TextCompareOperator.Contains); break; case TermType.Phrase: criteria = new FreeTextSearchCriteria(e.Phrase, FreeTextSearchField.AnyName, TextCompareOperator.Contains); break; case TermType.Word: criteria = new FreeTextSearchCriteria(e.Word, FreeTextSearchField.AnyName, TextCompareOperator.Contains); break; case TermType.Select: string keyword = e.Keyword.ToUpper(); if (keyword == "FILESIZE") { long byteSize = e.Number; if (byteSize == -1L) { try { byteSize = GetByteSize(e.Word); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Operand for keyword '{0}' must be a number with an optional multiplier"), e.Keyword), "euslQuery"); } } CompareOperator cOp = CompareOperator.Equal; try { cOp = GetNumericCompareOpForRelation(e.Relation); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Invalid compare operator for keyword '{0}'"), e.Keyword), "euslQuery"); } criteria = new FileSizeSearchCriteria(byteSize, cOp); } else if (keyword == "TYPE") { if (string.IsNullOrEmpty(e.Word)) { throw new ArgumentException( string.Format(S._("Operand for keyword '{0}' must be a string"), e.Keyword), "euslQuery"); } // try to map the word of the type selector to a MediaType MediaType type = MediaType.None; try { type = MediaType.FromString(e.Word); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Unknown type '{0}'"), e.Word), "euslQuery"); } if (e.Relation != Relation.Equal) { throw new ArgumentException( string.Format(S._("Keyword '{0}' only supports the equality operator ('=')"), e.Keyword), "euslQuery"); } criteria = new MediaTypeSearchCriteria(type); } else { // try to map the keyword to freetextsearch fields IFreeTextSearchField sf; // try to map the keyword to a VOLUME freetextsearchfield. // note: keywords mapping to foreign table fields // should be prefixed with the respective table name. if (keyword == "VOLUME-TITLE") { sf = VolumeSearchCriteria.FreeTextSearchField.Title; } else { // try to map the keyword to an ITEM freetextsearchfield try { sf = FreeTextSearchField.FromString(e.Keyword); } catch (ArgumentException) { throw new ArgumentException( string.Format(S._("Unknown keyword '{0}'"), e.Keyword), "euslQuery"); } } TextCompareOperator tcOp = TextCompareOperator.Contains; try { tcOp = GetTextCompareOpForRelation(e.Relation); } catch (ArgumentException) { throw new ArgumentException( S._("Invalid compare operator for a keyword that maps to textual content"), "euslQuery"); } string s; if (e.Number != -1L) { s = e.Number.ToString(); } else if (e.Word != null) { s = e.Word; } else { s = e.Phrase; } // throws argument exception if searchstr is too short criteria = new FreeTextSearchCriteria(s, sf, tcOp); } break; } if (e.ExcludeTerm) { criteria = new ExcludedSearchCriteria(criteria); } }
/* get the sql search condition of this/these field/fields */ string IFreeTextSearchField.GetSqlSearchCondition(string searchString, TextCompareOperator compareOperator, MatchRule fieldMatchRule) { StringBuilder sql = new StringBuilder(); if (this.ContainsField(Title)) SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Title", searchString), fieldMatchRule); if (this.ContainsField(LoanedTo)) SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Loaned_To", searchString), fieldMatchRule); if (this.ContainsField(Description)) SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Description", searchString), fieldMatchRule); if (this.ContainsField(Keywords)) SearchUtils.Append(sql, compareOperator.GetSqlCompareString("Volumes.Keywords", searchString), fieldMatchRule); return sql.ToString(); }