Completed
Push — master ( 6f2964...ca99e9 )
by Rasmus
02:54
created

Database::sql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace mindplay\sql\framework;
4
5
use mindplay\sql\model\DeleteQuery;
6
use mindplay\sql\model\InsertQuery;
7
use mindplay\sql\model\Schema;
8
use mindplay\sql\model\SelectQuery;
9
use mindplay\sql\model\SQLQuery;
10
use mindplay\sql\model\Table;
11
use mindplay\sql\model\UpdateQuery;
12
use UnexpectedValueException;
13
14
/**
15
 * This class implements the primary public API of the database model.
16
 */
17
class Database
18
{
19
    /**
20
     * @var DatabaseContainer
21
     */
22
    protected $container;
23
    
24
    /**
25
     * @param DatabaseContainer $container
26
     */
27 1
    public function __construct(DatabaseContainer $container)
28
    {
29 1
        $this->container = $container;
30 1
    }
31
32
    /**
33
     * @param string Schema class-name
34
     *
35
     * @return Schema
36
     */
37 1 View Code Duplication
    public function getSchema($schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39 1
        if (! $this->container->has($schema)) {
40 1
            $this->container->register($schema); // auto-wiring (for Schema with no special constructor dependencies)
41
        }
42
43 1
        $schema = $this->container->get($schema);
44
45 1
        if (! $schema instanceof Schema) {
46
            $class_name = get_class($schema);
47
48
            throw new UnexpectedValueException("{$class_name} does not extend the Schema class");
49
        }
50
51 1
        return $schema;
52
    }
53
    
54
    /**
55
     * @param Table $from
56
     *
57
     * @return SelectQuery
58
     */
59 1
    public function select(Table $from)
60
    {
61 1
        return $this->container->create(SelectQuery::class, ['root' => $from]);
62
    }
63
64
    /**
65
     * @param Table                  $into
66
     * @param mixed[]|mixed[][]|null $record optional record map (or list of record maps) where Column name => value
67
     *
68
     * @return InsertQuery
69
     */
70 1
    public function insert(Table $into, $record = null)
71
    {
72 1
        return $this->container->create(InsertQuery::class, ['table' => $into, 'record' => $record]);
73
    }
74
75
    /**
76
     * @param Table $table
77
     * 
78
     * @return UpdateQuery
79
     */
80 1
    public function update(Table $table)
81
    {
82 1
        return $this->container->create(UpdateQuery::class, ['root' => $table]);
83
    }
84
85
    /**
86
     * @param Table $table
87
     * 
88
     * @return DeleteQuery
89
     */
90 1
    public function delete(Table $table)
91
    {
92 1
        return $this->container->create(DeleteQuery::class, ['root' => $table]);
93
    }
94
95
    /**
96
     * @param string $sql
97
     * 
98
     * @return SQLQuery
99
     */
100 1
    public function sql($sql)
101
    {
102 1
        return $this->container->create(SQLQuery::class, ['sql' => $sql]);
103
    }
104
}
105