BrightSide Workbench Full Report + Source Code
SQLUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.sql;
20 
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.elephant.db.SQLHelper;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.elephant.entities.db.Synonyms;
26 
31 public class SQLUtil {
32 
33  public static final int
35 
36  public static void applySearchToQuery(String search, WhereClause wc, List<String> fields) {
37  applySearchToQuery(search, wc, fields, false, false);
38  }
39 
40  public static void applySearchToQuery(String search, WhereClause wc, List<String> fields, boolean withSynonyms) {
41  applySearchToQuery(search, wc, fields, withSynonyms, false);
42  }
43 
44  public static void applySearchToQuery(String search, WhereClause wc, List<String> fields, boolean withSynonyms, boolean orEd) {
45  String sep = "";
46  wc.addClause(orEd ? "or (" : "and (");
47  for(String field : fields) {
48  wc.addClause(sep + field + " like :search01");
49  sep = "or ";
50  }
51  wc.addNamedValue("search01", SQLHelper.convertToPartialLike(search));
52  if(withSynonyms) {
53  List<Synonyms> synonyms = Synonyms.getSynonymsByWords(search);
54  if(!synonyms.isEmpty() && synonyms.size() <= MAX_SYNONIMS) {
55  for(Synonyms s : synonyms) {
56  for(String field : fields) {
57  for(String w : s.getWords()) {
58  wc.addClause(sep + field + " like :w" + Strings.identifier(w));
59  sep = "or ";
60  wc.addNamedValue("w" + Strings.identifier(w), SQLHelper.convertToPartialLike(w));
61  }
62  }
63  }
64  }
65  }
66  wc.addClause(")");
67  }
68 
69  public static void applyFullsearchToQuery(String search, WhereClause wc, String fields) {
70  applyFullsearchToQuery(search, wc, fields, false, false);
71  }
72 
73  public static void applyFullsearchToQuery(String search, WhereClause wc, String fields, boolean withSynonyms) {
74  applyFullsearchToQuery(search, wc, fields, withSynonyms, false);
75  }
76 
77  public static void applyFullsearchToQuery(String search, WhereClause wc, String fields, boolean withSynonyms, boolean orEd) {
78  if(withSynonyms) {
79  List<Synonyms> synonyms = Synonyms.getSynonymsByWords(search);
80  if(!synonyms.isEmpty() && synonyms.size() <= MAX_SYNONIMS) {
81  for(Synonyms s : synonyms) {
82  for(String w : s.getWords()) {
83  if(!search.toLowerCase().contains(w.toLowerCase())) {
84  search += " " + w;
85  }
86  }
87  }
88  }
89  }
90  wc.addClause(orEd ? "or (" : "and (");
91  wc.addClause("match(" + fields + ") against (:fsearch01)");
92  wc.setOrderByClause("match(" + fields + ") against (:fsearch01) desc");
93  wc.addNamedValue("fsearch01", search);
94  wc.addClause(")");
95  wc.setUseNative(true);
96  }
97 
98  private SQLUtil() {
99  }
100 
101 }
static String convertToPartialLike(String value)
Definition: SQLHelper.java:38
void setUseNative(boolean useNative)
void setOrderByClause(String orderByClause)
void addNamedValue(String name, Object value)
static List< Synonyms > getSynonymsByWords(String phrase)
Definition: Synonyms.java:82
static void applySearchToQuery(String search, WhereClause wc, List< String > fields, boolean withSynonyms, boolean orEd)
Definition: SQLUtil.java:44
static void applyFullsearchToQuery(String search, WhereClause wc, String fields, boolean withSynonyms, boolean orEd)
Definition: SQLUtil.java:77
static void applyFullsearchToQuery(String search, WhereClause wc, String fields, boolean withSynonyms)
Definition: SQLUtil.java:73
static void applyFullsearchToQuery(String search, WhereClause wc, String fields)
Definition: SQLUtil.java:69
static void applySearchToQuery(String search, WhereClause wc, List< String > fields, boolean withSynonyms)
Definition: SQLUtil.java:40
static final int MAX_SYNONIMS
Definition: SQLUtil.java:34
static void applySearchToQuery(String search, WhereClause wc, List< String > fields)
Definition: SQLUtil.java:36