Download the PHP package somnambulist/query-builder without Composer

On this page you can find all versions of the php package somnambulist/query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package query-builder

Somnambulist Query Builder

GitHub Actions Build Status Issues License PHP Version Current Version

An SQL query builder implementation for building SQL queries programmatically. Primarily focused on SELECT queries, the query builder provides a core that can be extended with custom functionality and per database dialects via event hooking or overriding the various compilers.

This library does not provide a driver implementation: it is a pure query builder / compiler to a given dialect. A driver implementation is required such as DBAL, Laminas etc. Please note: the query builder does not enforce any portability between database servers, a query built for one may not function if run on another. Default setups are included for SQlite, MySQL, and Postgres.

This query builder is derived from the excellent work done by the Cake Software Foundation. See Cake License for the original license notice.

Requirements

Installation

Install using composer, or checkout / pull the files from github.com.

Usage

Configuration

Before this library can be used a TypeCaster must be registered with the TypeCaster manager. Type casting is used to convert values to data types suitable for use in queries. Specifically: it is used to handle custom data types that should be converted to Expression instances during query compilation.

A Doctrine DBAL caster is included (this library is intended to be used with Doctrine DBAL), allowing DBAL types to be used with the query builder and compiler. For other DB drivers, you will need to implement your own type caster for that driver, or submit a request to have one added to the project.

The StringTypeCaster is extremely basic and will only cast everything to strings. Alternatively: register an anonymous class that only returns the value back:

To register a type caster, you must add to your applications bootstrap:

To compile queries, the compiler must be configured. See compiler setup for details.

Querying

SELECT, INSERT, UPDATE, and DELETE queries can be created using this library. Each is represented by a query class e.g. SelectQuery from the Query\Type namespace. Queries are built up by adding object representations through the available methods. Not all methods or functions are compatible with each query type. You must know ahead of time which dialect you are targeting.

The builder and compiler do not perform any checks for whether the query you create is valid. There is no guarantee that any particular combination will work for any given database. You must compile and run the query against your chosen database to avoid issues.

Helper functions are included to make it a little nice to create queries. For example:

See query builder for more details of using the query builder.

Compiling Queries

Query objects must be compiled to SQL for execution. The compiler must be configured for a given database. See query compiler for details and an example.

Executable Queries

As an experiment, an example of making Query objects self-executing is included. This is accomplished by extending the separate query objects to include an execute() method. These are then instantiated via an adapter (both Doctrine DBAL and PDO are included) that injects the connection automatically. The adapter includes the connection and a compiler instance allowing the query object to be compiled and run via the connection.

For example:

Note: this is an experimental addition and may be removed in a future update.

Extending

Queries and the compilers can be extended easily by either replacing classes, or components, or hooking into the event system of the compiler.

For example: to add bespoke database feature support you would want to consider adding an expression specifically for the feature and then a compiler to handle it, or you may extend existing functionality to cover the bases and then handle the details.

In more complex cases where the query itself is needed as reference, then the event system must be used. Events are raised for:

Note that individual expression compilers do not fire events.

In the case of post events, the generated SQL is provided and may be revised as needed by the listener. For pre events, the execution flow can be early terminated by providing compiled SQL. This is useful when altering the main part for a given SQL dialect, for example: Postgres HAVINGs cannot work with aliased fields. The listener converts these and returns pre-built SQL avoiding the need for further processing.

If you have multiple listeners per event, then you should consider using an event dispatcher that allows setting the priority to avoid collisions, or ensure that the listeners are registered in the correct order.

One use case would be to add a Pre*QueryCompile listener to check for function usage for a given dialect and ensure that invalid or unsupported types are detected ahead of time. Another could be to add smart join functionality where a separate schema object is used to automatically resolve joins based on aliases etc.

Tests

PHPUnit 10+ is used for testing. Run tests via vendor/bin/phpunit.


All versions of query-builder with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
psr/event-dispatcher Version ^1.0
psr/log Version ^1.0|^2.0|^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package somnambulist/query-builder contains the following files

Loading the files please wait ....