Download the PHP package lushobarlett/query-manager without Composer
On this page you can find all versions of the php package lushobarlett/query-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lushobarlett/query-manager
More information about lushobarlett/query-manager
Files in lushobarlett/query-manager
Package query-manager
Short Description manager for mysql queries to improve database code flexibility, security and automation of simpler database use cases
License MIT
Informations about the package query-manager
PHP query manager
Manager for query building and execution.
Objectives
- Query code flexibility and reusability
- Solve security and fidelity issues
- Force their use, with no penalty to the programmer
- Make simple database use cases quicker to code
Implementation
Query Pieces
The query building block is a QueryPiece
class. In mathematical terms it is nothing more than a Monoid, because it acts as a pair of string and array. First constructor argument is the statement, the rest are the values filling that statement, called fragments. All arguments are optional, if nothing is provided you get the identity, empty string and array.
These can be made smaller and then be merged. Spaces are added automatically.
There's also a lot of static methods to make it look better, like QueryPiece::Select(...)
which is the same as new QueryPiece("SELECT ...")
.
Formatters and Fields
The Formatter
and Field
classes are very helpful tool for sanitizing input. They are quite useful in the Table
class, explained later, but they are not restricted to that use.
A Field
defines a pipeline of operations to be performed on a value. There's maps, replacements, options, type and class restrictions and type casts.
A Formatter
is a just a set of those fields, but we can use new retrictions on those fields. Fields can be optional or required. If they are optional they can have a default value to be used in the pipeline. The Formatter
can also take strings, those represent optional fields with no default and no pipeline.
Following this, you can call the formatting functions with some data. Note that formatting arrays and objects is the same, and they will be returned as such.
Columns
A Column
class just holds a string, the name of the column. It can also specify if it is a primary column (also meaning unique), if it is unique, and if it is foreign. In the latter, it will hold a Name
class referring to said foreign column.
Names
A Name
is a class that holds a database, table, column name, and or alias. It can make a valid string for SQL to use, or just use the data internally.
For database, an IConnection
is also accepted. For columns, a Column
is also accepted.
Not all four are required, any combination will work. Be wary that some combinations don't make sense.
Tables
The Table
is a static base class for any table. It implements many basic static functions, that are available for the subclasses.
The subclasses will need to implement one function, connect
. There, the suclass will construct a TableData
object and pass it to initialize
along with the connection provided in connect
.
Suppose we have a mydb.person
table that has columns id, name, age, fav_food
.
Construction
The Table that execute a query on their own are public, so you can already call from outside, using any subclass. And always remember to $conn->commit()
.
Connections
The Connection holds data necessary to connect to the database. It also prepares statements, passed as QueryPieces and it automatically uses a transaction model. The construction is the same as a normal mysqli class.
But it always performs query preparation, and also has transaction managing functions exposed and used automatically as well. It starts a transaction in constructor, rolls back on any error and closes on destruction. It will not commit on its own, so you have to do it.