WstępPrzydatne klasy:
Podstawowe możliwości
W powyższym przykładzie w wyniku otrzymamy wartości tylko kolumn, dla których zdefiniowaliśmy scalar'y nawet jeżeli tabela testtable posiada więcej kolumn. Rekord jest reprezentowany jako mapa, w której kluczem jest nazwa kolumn(lub alias jeżeli został zdefiniowany). Podanie scalar'ów jest opcjonalne, lecz zalecane.
Przy podawaniu parametrów ważne jest, aby ich typy zgadzały się z typami kolumn. Np. nie możemy podać "true" jako String, gdy kolumna jest typu boolean. Definiowanie zwracanego typuDodawanie do każdego zapytania scalar'ów może być czasem uciążliwe, a odnoszenie się do wartości rekordów poprzez ich nazwę może powodować błędy wynikające np. z literówek. Dlatego innym podejściem jest utworzenie klasy, która będzie przechowywała zwracane z bazy danych informacje. Przejdźmy do przykładu:
Aby, przedstawiony kod zadziałał musimy oczywiście utworzyć klasę TestDto(nazwa może być inna FiltrowanieAPI umożliwia również dodatkowe funkcje tj. budowanie klauzuli where, order by, oraz ograniczanie zbioru wynikowego(offset i limit). Zacznijmy od filtrowania. Klasa SQLBuilder pozwala zbudować filtry za pomocą klas:
W przykładzie zdefiniowaliśmy dwa proste warunki. Domyślnie operatorem porównania jest znak równości, a operatorem logicznym, który łączy warunki jest AND. Oczywiście możemy zmienić domyślne ustawienia.
Dostępne operatory są opisane w dokumentacji FilterOperator. Przejdźmy do grupowania filtrów:
Powyższy kod wygeneruje następujące zapytanie: select textcol,boolcol from testtable where ( ( intcol > :intcol_0 and intcol < :intcol_1 ) or ( textcol in (:textcol_2) and boolcol != :boolcol_3 ) ) Jak widzimy, możliwe jest zagnieżdżanie grup w innych grupach co pozwala na tworzenie drzewa warunków. Należy pamiętać, że gdy zdefiniujemy filtry, zapytanie, które podaliśmy poprzez setQuery ulegnie zmianie. Doklejona zostanie klauzula WHERE, wraz z warunkami. Dlatego nie możemy w setQuery podać zapytania z już istniejącą klauzulą WHERE. Jeżeli jednak pojawi się taka potrzeba możemy niezależnie zbudować fragment zapytania dotyczący warunków, w następujący sposób:
Efekt będzie dokładnie taki sam jak w przykładzie poprzednim. Należy zwrócić uwagę na parametry użyte w filtrach. Spójrzmy ponownie na zapytanie SQL wygenerowwane w przykładzie: select textcol,boolcol from testtable where ( ( intcol > :intcol_0 and intcol < :intcol_1 ) or ( textcol in (:textcol_2) and boolcol != :boolcol_3 ) ) Parametry są poprzedzone są znakiem dwukropka. Widzimy, że nazwy parametrów różnią się od nazw podanych w definicji filtrów. Dzieje się tak dlatego, że podczas budowania zapytania parametry są numerowane. Celem numerowania jest umożliwienie definiowania wielu warunków dla jednej kolumny. Gdybyśmy nie wprowadzili numerowania zapytanie wyglądało by tak: select textcol,boolcol from testtable where ( ( intcol > :intcol and intcol < :intcol ) or ( textcol in (:textcol) and boolcol != :boolcol ) ) Problem pojawiłby się przy kolumnie intcol, której dotyczą dwa różne warunki. Bez numeracji parametr :intcol przyjmie wartość 5(podany jako pierwszy) w dwóch miejscach co spowoduje, że nie otrzymamy żadnych wyników. Z tego powody zaleca się korzystanie z metody builder.setParameters(List<SQLFilter> filters) do ustawienia wartości filtrów. Sortowanie
Paging
Przykład prezentuje sposób ograniczenia ilości zwracanych wyników. Poza parametrami oznaczającymi odpowiednio przesunięcie w zbiorze wynikowym(offset) i maksymalną ilość wyników(limit), widzimy, że zmienił się typ zwracanej wartości. Klasa CountedResult jest generyczna i służy przechowywaniu danych, wraz z ilością ogólną elementów. W tym przypadku otrzymamy maksymalnie 5 elementów, a wartość total będzie liczbą wszystkich elementów. |
IntroductionUseful classes:
Basic capabilities
In the above example, the result will be the values of only the columns for which we have defined scalars even if the testtable has more columns. The record is represented as a map, in which the key is the name of the columns(or alias if defined). Specifying scalars is optional, but recommended.
When specifying parameters, it is important that their types match the column types. E.g., we cannot specify "true" as String when the column is of type boolean. Defining the returned typeAdding scalars to each query can sometimes be troublesome, and referring to record values by their name can cause errors due to typos, for example. Therefore, another approach is to create a class that will store the information returned from the database. Let's turn to an example:
So that, the presented code works you need to create a class TestDto (the name can be different). The fields of the class should correspond to the aliases of the columns. In this case, textcol and nazwawlasna. The character size does not matter, but it is necessary to stick to the JavaBean standard( getters and setters). This will give you a list of objects with fields populated by values from the database. You don't have to worry about column types, as they will be automatically cast to the types of the corresponding fields. FilteringThe API also allows additional functions, i.e. building a where clause, order by, and limiting the resulting set(offset and limit). Let's start with filtering. SQLBuilder allows you to build filters using classes:
In the example, we defined two simple conditions. By default, the comparison operator is the equal sign, and the boolean operator that connects the conditions is AND. Of course, we can change the defaults.
DThe available operators are described in the FilterOperator. Let's move on to grouping filters:
The above code will generate the following query: select textcol,boolcol from testtable where ( ( intcol > :intcol_0 and intcol < :intcol_1 ) or ( textcol in (:textcol_2) and boolcol != :boolcol_3 ) ) As you can see, it is possible to nest groups in other groups which allows you to create a tree of conditions. Note that when we define filters, the query we specified via setQuery will change. The WHERE clause will be pasted, along with the conditions. Therefore, you cannot specify a query with an already existing WHERE clause in setQuery. However, if the need arises, we can independently build a query fragment with conditions, as follows:
The result will be exactly the same as in the previous example. Note the parameters used in the filters. Let's look again at the SQL query generated in the example: select textcol,boolcol from testtable where ( ( intcol > :intcol_0 and intcol < :intcol_1 ) or ( textcol in (:textcol_2) and boolcol != :boolcol_3 ) ) Parameters are preceded by a colon sign. Notice that the names of the parameters differ from the names given in the filter definition. This is because the parameters are numbered when building the query. The purpose of numbering is to allow multiple conditions to be defined for a single column. If we had not introduced numbering the query would look like this: select textcol,boolcol from testtable where ( ( intcol > :intcol and intcol < :intcol ) or ( textcol in (:textcol) and boolcol != :boolcol ) ) The problem would arise with the intcol column, which is affected by two different conditions. Without numbering, the :intcol parameter will take the value of 5(given first) in two places which will result in no results. For this reason, it is recommended to use the builder.setParameters(List<SQLFilter> filters) method to set the filter values. Sorting
Paging
The example shows how to limit the number of returned results. In addition to the parameters indicating the offset in the result set(offset) and the maximum number of results(limit), we see that the type of the returned value has changed, accordingly. The CountedResult class is generic and is used to store data, along with the total number of elements. In this case, we will get a maximum of 5 elements, and the value of total will be the number of all elements. |