Using Domain-Specific Language Queries

Liferay 7.4+

DSL Query is an acronym for Domain-Specific Language Query. The foundation is based upon using a domain-specific language for the implementation. This makes the writing of queries more natural than Dynamic Query or Custom SQL.

Deploy an Example

Start a new Liferay instance by running

docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.120-ga120

Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.

Then, follow these steps to deploy the example:

  1. Download and unzip the liferay-g4e1.zip example project.

    curl https://resources.learn.liferay.com/dxp/latest/en/liferay-development/building-applications/data-frameworks/advanced-queries/liferay-g4e1.zip -O
    
    unzip liferay-g4e1.zip
    
  2. Build and deploy the project module.

    cd liferay-g4e1
    
    ./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
    
    Note

    This command is the same as copying the deployed jars to /opt/liferay/osgi/modules on the Docker container.

  3. Confirm the deployment in the Liferay Docker container console.

    STARTED com.liferay.g4e1.api_1.0.0 [1461]
    STARTED com.liferay.g4e1.service_1.0.0 [1462]
    STARTED com.liferay.g4e1.web_1.0.0 [1463]
    
  4. To verify the example module is working, open your browser to https://localhost:8080.

  5. add the g4e1 portlet to a page. you can find the example portlet under sample in widgets.

    Add the G4E1 portlet to a page.

  6. Add an entry by entering a name and a description. Leave Hidden unchecked. Click Add, and the new entry appears under G4E1 Entries.

  7. Add another entry with a different name and description. This time, check Hidden. The new entry doesn’t appear under G4E1 Entries.

This example uses DSL Query to retrieve only entries with a specified value in the database (hidden_ = false).

Building Queries with DSLQuery

  1. Open G4E1EntryLocalServiceImpl.java. The code for the DSL Query is defined in the getG4E1Entries method.
@Override
public List<G4E1Entry> getG4E1Entries(boolean hidden) {
	DSLQuery query = DSLQueryFactoryUtil.select(
	).from(
		G4E1EntryTable.INSTANCE
	).where(
		G4E1EntryTable.INSTANCE.hidden.eq(hidden)
	);

	return dslQuery(query);
}
  1. You can create a DSL Query using DSLQueryFactoryUtil’s static methods. This example makes a simple SELECT statement using the select() method to retrieve all data from the chosen rows in the table.

    Note

    DSLQueryFactoryUtil also contains other methods such as selectDistinct(), count(), and countDistinct() for more complex SQL operations.

  2. Choose the table to query using the from() method. It takes a single argument in the form of a Table object.

  3. From there, you can continue to build onto your custom query using Java methods for most SQL operations. See the JoinStep javadocs for a list of all possible operations. This example uses a WHERE clause to check if the value of hidden is equal to the given boolean variable.

    Note

    The DSL Query can handle things like unions, joins, aliases, functions, complex where clauses, group bys sand sort bys, all using this type of domain-specific language to build queries.

DSLQuery or DynamicQuery

All Service Builder code in the portal’s services and your own custom services have built-in support for DSL Query, so you can handle your entities like core entities. Since DSL Query is implemented in Java classes and interfaces, the queries are validated at compile time instead of at runtime, making it less error-prone than Dynamic Query.

In the most recent versions of Liferay, DSL Query is the best way to handle custom queries for all Service Builder entities. It is more intuitive, efficient, and less error-prone than equivalent Dynamic Query/Custom SQL implementations.

Capabilities

Product

Education

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy