You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlcontainer-freeform.asciidoc 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ---
  2. title: Making Freeform Queries
  3. order: 7
  4. layout: page
  5. ---
  6. [[sqlcontainer.freeform]]
  7. = Making Freeform Queries
  8. In most cases, the provided [classname]#TableQuery# will be enough to allow a
  9. developer to gain effortless access to an SQL data source. However there may
  10. arise situations when a more complex query with, for example, join expressions
  11. is needed. Or perhaps you need to redefine how the writing or filtering should
  12. be done. The [classname]#FreeformQuery# query delegate is provided for this
  13. exact purpose. Out of the box the [classname]#FreeformQuery# supports read-only
  14. access to a database, but it can be extended to allow writing also.
  15. [[sqlcontainer.freeform.getting-started]]
  16. == Getting started
  17. Getting started with the [classname]#FreeformQuery# may be done as shown in the
  18. following. The connection pool initialization is similar to the
  19. [classname]#TableQuery# example so it is omitted here. Note that the name(s) of
  20. the primary key column(s) must be provided to the [classname]#FreeformQuery#
  21. manually. This is required because depending on the query the result set may or
  22. may not contain data about primary key columns. In this example, there is one
  23. primary key column with a name 'ID'.
  24. ----
  25. FreeformQuery query = new FreeformQuery(
  26. "SELECT * FROM SAMPLE", pool, "ID");
  27. SQLContainer container = new SQLContainer(query);
  28. ----
  29. [[sqlcontainer.freeform.limitations]]
  30. == Limitations
  31. While this looks just as easy as with the [classname]#TableQuery#, do note that
  32. there are some important caveats here. Using [classname]#FreeformQuery# like
  33. this (without providing [classname]#FreeformQueryDelegate# or
  34. [classname]#FreeformStatementDelegate# implementation) it can only be used as a
  35. read-only window to the resultset of the query. Additionally filtering, sorting
  36. and lazy loading features will not be supported, and the row count will be
  37. fetched in quite an inefficient manner. Bearing these limitations in mind, it
  38. becomes quite obvious that the developer is in reality meant to implement the
  39. [classname]#FreeformQueryDelegate# or [classname]#FreeformStatementDelegate#
  40. interface.
  41. The [classname]#FreeformStatementDelegate# interface is an extension of the
  42. [classname]#FreeformQueryDelegate# interface, which returns
  43. [classname]#StatementHelper# objects instead of pure query [classname]#String#s.
  44. This enables the developer to use prepared statetemens instead of regular
  45. statements. It is highly recommended to use the
  46. [classname]#FreeformStatementDelegate# in all implementations. From this chapter
  47. onwards, we will only refer to the [classname]#FreeformStatementDelegate# in
  48. cases where [classname]#FreeformQueryDelegate# could also be applied.
  49. [[sqlcontainer.freeform.custom-freeformstatementdelegate]]
  50. == Creating your own [classname]#FreeformStatementDelegate#
  51. To create your own delegate for [classname]#FreeformQuery# you must implement
  52. some or all of the methods from the [classname]#FreeformStatementDelegate#
  53. interface, depending on which ones your use case requires. The interface
  54. contains eight methods which are shown below. For more detailed requirements,
  55. see the JavaDoc documentation of the interface.
  56. ----
  57. // Read-only queries
  58. public StatementHelper getCountStatement()
  59. public StatementHelper getQueryStatement(int offset, int limit)
  60. public StatementHelper getContainsRowQueryStatement(Object... keys)
  61. // Filtering and sorting
  62. public void setFilters(List<Filter> filters)
  63. public void setFilters(List<Filter> filters,
  64. FilteringMode filteringMode)
  65. public void setOrderBy(List<OrderBy> orderBys)
  66. // Write support
  67. public int storeRow(Connection conn, RowItem row)
  68. public boolean removeRow(Connection conn, RowItem row)
  69. ----
  70. A simple demo implementation of this interface can be found in the SQLContainer
  71. package, more specifically in the class
  72. [classname]#com.vaadin.addon.sqlcontainer.demo.DemoFreeformQueryDelegate#.