Skip to content

Commit

Permalink
[skip ci] basics of data model query is added
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Apr 6, 2020
1 parent 25d2d9b commit 2013aa2
Show file tree
Hide file tree
Showing 89 changed files with 5,663 additions and 4,056 deletions.
File renamed without changes.
43 changes: 43 additions & 0 deletions docs/data/query.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Query
==========

- view
- filter
- order
- start and limit

View
================================

Name of view which is defined in a data model.

Filter
================================

Here is general form of data filter

$filter =[
[[{property}, {operation}, {value}], [{property}, {operation}, {value}], ..],
[{property}, {operation}, {value}],
...
]

Order
================================

THe general form of order is:

$order =[
'{property}' => '{order}'
];

Example:

$order = [
'id' => 'aes'
];

Start and limit
================================

You may limit the result set by adding start and limit
File renamed without changes.
247 changes: 247 additions & 0 deletions docs/data/view.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
Data View
========

By adding view into a model, you can extend and add some virtual attributes.

A view is consist of:

- group: list of field to group by
- join: models to join
- field: how to map result values into the model
- where: list of where clouse
- field: List of model field to fetch from db

Join to a model
=======================

- model: name of class that you want to join to
- alias: a name to referin
- property: to use in joing
- masterProperty: to join into the current model
- type: the join type (left, inner, ..)



Group
=======================

An array of property to group

The general pattern of a property in the list is:

$view = [
'group' => ['{alias}.{property}]
];

Where alias is optionall.

Here is an example

$view = [
'group' => ['name', 'a.title']
];


Fields
=======================

Binds query value into the view.

The general form to add a field:

$view = [
'field' => [
'property' => 'field'
]
]


using expression:

$view = [
'field' => [
'property' => new Expression('now()')
]
]

using model properties:

$view = [
'field' => [
'userName' => 'account.login'
]
]

where userName is a property in the current model and account.login is a {alias}.{property}
from join and where.

Where
=======================

Where is list of common Data query filters to select a group of items
from a repository.

Where clouse will be merged with filters used to query an object repository.

A common where clouse to add to the query

$view = [
'where' => [
[{property}, {operation}, {value}],
[{property}, {value}]
]
]

NOTE: The merge of where and filter will be logically AND.

Field
=======================

It is easy to bind custom data to the data model by field attribute.

Field is list of key-value where key is the data model property name and the
value is data layer expression.

General form of field is:

$view = [
'field' =>[
'{property}' => '{alias}.{property}',
]
];


TO bind database layer values directly into the model properties you must use
DB expression.

For example:

$view = [
'field' =>[
'date' => new Expression('now()'),
]
];

Sets current data base date to the date attribute of the model.

NOTE: you must define all required attribures if you add field attribute.

Examples
=======================

Following data model is used in this part:


class A{
public int $id;
public string $title
}

class B{
public int $id;
public string $title;
public in $aId;
}


/**
* @Model(
* table='b',
* mapped=true,
* )
* @View(
* name='count',
* field=[
* 'count' => new Expression('count(*)')
* ]
* )
*/
class C{
public int $count = 0;
}

Select Related models
----------------------------
Here is our data models:


A view to select B related to A;


$view = [
'join' => [
'model' => 'A',
'alias' => 'a',
'property' => 'id',
'masterProperty' => 'aId',
'type' => 'left'
]
];
$this->setView('relatedToA', $view);

The final query is :

select * from B left join A as a on a.id = aId

To use view with repository

$repo = Repository::getInstance('B');
$list = $repo->getList([
'filter'=>[
['a.id', 12]
],
'view' => 'relatedToA',
]);
var_dump($list);

Selects all B which are related to A with id=12.




Virtual Model
----------------------------

It is possible to mount several models on a table.

One of them must be a real model and the others must be mapped.

In this example C is a mapped model in which the attributes computed with
expression.


$repo = Repository::getInstance('C');
$model = $repo->getOne([
'view' => 'counter',
]);
var_dump($model);

Appendex
=================================

How mapped Data Join to DB coin
---------------------------------

Data query is defines based on data model (defined in bussines layer) and finally mapped
into the DB query.

Join is part of Data View and must converted to a DB join. Here is mapp of Data to DB join:

$query->join('{model}.{property} {alias}', '{masterProperty}', '{kind}')

For example, suppose the following view:

$view = [
'join' =>[
'model' => '\Pluf\NotBook\Book',
'property' => 'id',
'alias' => 'book',
'masterProperty' => 'book_id',
'kind' => 'left'
]
];

With a simple schema, the following join will be added to a DB query:

$query->join('book.id book', 'id', 'left');
6 changes: 3 additions & 3 deletions docs/db/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Advanced Topics
===============

DSQL has huge capabilities in terms of extending. This chapter explains just
Pluf DB has huge capabilities in terms of extending. This chapter explains just
some of the ways how you can extend this already incredibly powerful library.

Advanced Connections
====================
:php:class:`Connection` is incredibly lightweight and powerful in DSQL.
:php:class:`Connection` is incredibly lightweight and powerful in Pluf DB.
The class tries to get out of your way as much as possible.

Using DSQL without Connection
Using Pluf DB without Connection
-----------------------------
You can use :php:class:`Query` and :php:class:`Expression` without connection
at all. Simply create expression::
Expand Down
8 changes: 4 additions & 4 deletions docs/db/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Connection
==========

DSQL supports various database vendors natively but also supports 3rd party
Pluf Db supports various database vendors natively but also supports 3rd party
extensions.
For current status on database support see: :ref:`databases`.

Expand All @@ -16,7 +16,7 @@ Connection class is handy to have if you plan on building and executing
queries in your application. It's more appropriate to store
connection in a global variable or global class::

$app->db = atk4\dsql\Connection::connect($dsn, $user, $pass);
$app->db = Pluf\Db\Connection::connect($dsn, $user, $pass);


.. php:staticmethod:: connect($dsn, $user = null, $password = null, $args = [])
Expand All @@ -34,7 +34,7 @@ connection in a global variable or global class::
This should allow you to access this class from anywhere and generate either
new Query or Expression class::

$query = $app->db->dsql();
$query = $app->db->query();

// or

Expand Down Expand Up @@ -62,7 +62,7 @@ Here is how you can use all of this together::

$dsn = 'mysql:host=localhost;port=3307;dbname=testdb';

$c = atk4\dsql\Connection::connect($dsn, 'root', 'root');
$c = Pluf\Db\Connection::connect($dsn, 'root', 'root');
$expr = $c -> expr("select now()");

echo "Time now is : ". $expr;
Expand Down
8 changes: 4 additions & 4 deletions docs/db/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Expressions

Expression class implements a flexible way for you to define any custom
expression then execute it as-is or as a part of another query or expression.
Expression is supported anywhere in DSQL to allow you to express SQL syntax
Expression is supported anywhere in Pluf DB to allow you to express SQL syntax
properly.

Quick Example::
Expand All @@ -29,7 +29,7 @@ Another use of expression is to supply field instead of value and vice versa::

// Produces: where :a between time_from and time_to

Yet another curious use for the DSQL library is if you have certain object in
Yet another curious use for the Pluf DB library is if you have certain object in
your ORM implementing :php:class:`Expressionable` interface. Then you can also
use it within expressions::

Expand Down Expand Up @@ -70,7 +70,7 @@ Parameters
Because some values are un-safe to use in the query and can contain dangerous
values they are kept outside of the SQL query string and are using
`PDO's bindParam <http://php.net/manual/en/pdostatement.bindparam.php>`_
instead. DSQL can consist of multiple objects and each object may have
instead. Pluf DB can consist of multiple objects and each object may have
some parameters. During `rendering`_ those parameters are joined together to
produce one complete query.

Expand All @@ -86,7 +86,7 @@ Creating Expression

::

use atk4\dsql\Expression;
use Pluf\Db\Expression;

$expr = new Expression("NOW()");

Expand Down
Loading

0 comments on commit 2013aa2

Please sign in to comment.