sypherlev/blueprint

DBAL package that performs data operations through applying patterns

1.2.0 2017-07-11 19:42 UTC

This package is auto-updated.

Last update: 2024-03-20 23:34:07 UTC


README

PDS Skeleton License: MIT Build Status

Blueprint Query Compiler

Blueprint is an extended query builder that allows you to define, reuse, and override chunks of query code at will.

Install via Composer

composer require sypherlev/blueprint

Background

I found that, while building a number of large, data-driven apps, an ORM just got in the way most of the time, while a query builder meant writing lots of difficult-to-maintain, copy-pasted and possibly-insecure code. As using an ORM didn’t lend itself to what I was trying to do, I started with a query builder and set about fixing those issues instead. The end result is Blueprint.

Blueprint’s core is a fairly robust query builder that allows for most common SQL operations, and a fallback to raw SQL. Blueprint is extended with additional elements that allow for reusable, configured query sections, which cuts the amount of copy-pasting code to a minimum, and includes some built-in security out of the box.

  • Patterns: A Pattern defines tables, columns, and joins for a query, and the primary table and its columns double as a whitelist when used with INSERT/UPDATE queries.
  • Filters: A Filter defines additional where clauses, and the limit and orderBy clauses for a query.
  • Transformations: A Transformation is a simple closure that modifies data going into an INSERT/UPDATE query, and modifies data coming out of a SELECT query.

Patterns, Filters and Transformations are normally defined in the constructor of a Blueprint-extending class and then used as needed in specific methods.

All queries generated by the builder use prepared statements, and there is an additional function, raw(), that will accept any arbitrary SQL string with or without bindings.

It currently supports MySQL/MariaDB/PostgreSQL, with an interface definition so that more can be added. The pattern/filter setup can only define whatever comes out of a single query, which means defining one-to-many or many-to-many relationships requires the use of a transformation after the initial query result to append more data as needed.

Note: there is no functionality to create or modify tables, and I don’t intend to add any at this time. Blueprint assumes you have a fairly solid working knowledge of SQL already, and you’ve created the schema externally or you’re working with a pre-existing database.

As it’s purpose-built for managing and processing large blocks of complex relational data, Blueprint is not recommended for basic CRUD work. Please look at Propel or RedBean instead.