Completed
Push — master ( 7cd110...80ad88 )
by Ondřej
07:00
created

StdCoreFactory   C

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 19

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 19
dl 0
loc 118
rs 6.875
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createGlobalTypeRegister() 0 70 2
A createSqlPatternParser() 0 8 2
A createStatementExceptionFactory() 0 4 1
A createConnection() 0 4 1
A createCacheControl() 0 18 2
A createTransactionHandle() 0 6 1
1
<?php
2
namespace Ivory;
3
4
use Ivory\Cache\CacheControl;
5
use Ivory\Cache\ICacheControl;
6
use Ivory\Connection\Connection;
7
use Ivory\Connection\ConnectionParameters;
8
use Ivory\Connection\IConnection;
9
use Ivory\Connection\IObservableTransactionControl;
10
use Ivory\Connection\IStatementExecution;
11
use Ivory\Connection\ITxHandle;
12
use Ivory\Connection\TxHandle;
13
use Ivory\Exception\StatementExceptionFactory;
14
use Ivory\Lang;
15
use Ivory\Lang\SqlPattern\CachingSqlPatternParser;
16
use Ivory\Lang\SqlPattern\ISqlPatternParser;
17
use Ivory\Lang\SqlPattern\SqlPatternParser;
18
use Ivory\Type\Ivory\CommandSerializer;
19
use Ivory\Type\Ivory\IdentifierSerializer;
20
use Ivory\Type\Ivory\LikeExpressionSerializer;
21
use Ivory\Type\Ivory\QuotedIdentifierSerializer;
22
use Ivory\Type\Ivory\RelationSerializer;
23
use Ivory\Type\Ivory\SqlSerializer;
24
use Ivory\Type\Std\StdRangeCanonicalFuncProvider;
25
use Ivory\Type\Std\StdTypeLoader;
26
use Ivory\Type\TypeRegister;
27
28
/**
29
 * The standard factory for the core Ivory objects.
30
 *
31
 * The factory may either be inherited or replaced altogether to change any default behaviour of Ivory. The user
32
 * supplied factory may be activated with {@link Ivory::setCoreFactory()}. Note, however, that {@link Ivory} caches the
33
 * objects so setting another core factory is only effective at the very beginning of working with Ivory.
34
 */
35
class StdCoreFactory implements ICoreFactory
36
{
37
    public function createGlobalTypeRegister(): TypeRegister
38
    {
39
        $reg = new TypeRegister();
40
        $reg->registerTypeLoader(new StdTypeLoader());
41
        $reg->registerRangeCanonicalFuncProvider(new StdRangeCanonicalFuncProvider());
42
43
        $reservedTypes = Lang\Sql\Types::getReservedTypes();
44
        foreach ($reservedTypes as $alias => list($implSchema, $implName)) {
45
            $reg->registerTypeAbbreviation($alias, $implSchema, $implName);
46
        }
47
48
        // standard value serializers
49
        $reg->registerValueSerializer('sql', new SqlSerializer());
50
        $reg->registerValueSerializer('ident', new IdentifierSerializer());
51
        $reg->registerValueSerializer('qident', new QuotedIdentifierSerializer());
52
        $reg->registerValueSerializer('rel', new RelationSerializer());
53
        $reg->registerValueSerializer('cmd', new CommandSerializer());
54
        $reg->registerValueSerializer('like', new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_NONE));
55
        $reg->registerValueSerializer('like_', new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_APPEND));
56
        $reg->registerValueSerializer('_like', new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_PREPEND));
57
        $reg->registerValueSerializer('_like_', new LikeExpressionSerializer(LikeExpressionSerializer::WILDCARD_BOTH));
58
59
        // standard type abbreviations
60
        $reg->registerTypeAbbreviation('s', 'pg_catalog', 'text');
61
        $reg->registerTypeAbbreviation('i', 'pg_catalog', 'bigint');
62
        $reg->registerTypeAbbreviation('num', 'pg_catalog', 'numeric');
63
        $reg->registerTypeAbbreviation('f', 'pg_catalog', 'float8');
64
        $reg->registerTypeAbbreviation('ts', 'pg_catalog', 'timestamp');
65
        $reg->registerTypeAbbreviation('tstz', 'pg_catalog', 'timestamptz');
66
67
        // standard type recognition rules
68
        $reg->addTypeRecognitionRule('int', 'pg_catalog', 'int8');
69
        $reg->addTypeRecognitionRule('string', 'pg_catalog', 'text');
70
        $reg->addTypeRecognitionRule('bool', 'pg_catalog', 'bool');
71
        $reg->addTypeRecognitionRule('float', 'pg_catalog', 'float8');
72
        $reg->addTypeRecognitionRule('null', 'pg_catalog', 'text');
73
        $reg->addTypeRecognitionRule('array', 'pg_catalog', 'text[]');
74
        $reg->addTypeRecognitionRule(\Ivory\Value\Decimal::class, 'pg_catalog', 'numeric');
75
        $reg->addTypeRecognitionRule(\Ivory\Value\Date::class, 'pg_catalog', 'date');
76
        $reg->addTypeRecognitionRule(\Ivory\Value\Time::class, 'pg_catalog', 'time');
77
        $reg->addTypeRecognitionRule(\Ivory\Value\TimeTz::class, 'pg_catalog', 'timetz');
78
        $reg->addTypeRecognitionRule(\Ivory\Value\Timestamp::class, 'pg_catalog', 'timestamp');
79
        $reg->addTypeRecognitionRule(\Ivory\Value\TimestampTz::class, 'pg_catalog', 'timestamptz');
80
        $reg->addTypeRecognitionRule(\Ivory\Value\TimeInterval::class, 'pg_catalog', 'interval');
81
        $reg->addTypeRecognitionRule(\Ivory\Value\FixedBitString::class, 'pg_catalog', 'bit');
82
        $reg->addTypeRecognitionRule(\Ivory\Value\VarBitString::class, 'pg_catalog', 'varbit');
83
        $reg->addTypeRecognitionRule(\Ivory\Value\Json::class, 'pg_catalog', 'json');
84
        $reg->addTypeRecognitionRule(\Ivory\Value\XmlContent::class, 'pg_catalog', 'xml');
85
        $reg->addTypeRecognitionRule(\Ivory\Value\XmlDocument::class, 'pg_catalog', 'xml');
86
        $reg->addTypeRecognitionRule(\DOMDocument::class, 'pg_catalog', 'xml');
87
        $reg->addTypeRecognitionRule(\DOMNode::class, 'pg_catalog', 'xml');
88
        $reg->addTypeRecognitionRule(\DOMNodeList::class, 'pg_catalog', 'xml');
89
        $reg->addTypeRecognitionRule(\SimpleXMLElement::class, 'pg_catalog', 'xml');
90
        $reg->addTypeRecognitionRule(\Ivory\Value\Point::class, 'pg_catalog', 'point');
91
        $reg->addTypeRecognitionRule(\Ivory\Value\Line::class, 'pg_catalog', 'line');
92
        $reg->addTypeRecognitionRule(\Ivory\Value\LineSegment::class, 'pg_catalog', 'lseg');
93
        $reg->addTypeRecognitionRule(\Ivory\Value\Box::class, 'pg_catalog', 'box');
94
        $reg->addTypeRecognitionRule(\Ivory\Value\Path::class, 'pg_catalog', 'path');
95
        $reg->addTypeRecognitionRule(\Ivory\Value\Polygon::class, 'pg_catalog', 'polygon');
96
        $reg->addTypeRecognitionRule(\Ivory\Value\Circle::class, 'pg_catalog', 'circle');
97
        $reg->addTypeRecognitionRule(\Ivory\Value\NetAddress::class, 'pg_catalog', 'inet');
98
        $reg->addTypeRecognitionRule(\Ivory\Value\MacAddr::class, 'pg_catalog', 'macaddr');
99
        $reg->addTypeRecognitionRule(\Ivory\Value\Money::class, 'pg_catalog', 'money');
100
        $reg->addTypeRecognitionRule(\Ivory\Value\PgLogSequenceNumber::class, 'pg_catalog', 'pg_lsn');
101
        $reg->addTypeRecognitionRule(\Ivory\Value\TxIdSnapshot::class, 'pg_catalog', 'txid_snapshot');
102
        $reg->addTypeRecognitionRule(\Ivory\Value\TextSearchVector::class, 'pg_catalog', 'tsvector');
103
        $reg->addTypeRecognitionRule(\Ivory\Value\TextSearchQuery::class, 'pg_catalog', 'tsquery');
104
105
        return $reg;
106
    }
107
108
    public function createSqlPatternParser(ICacheControl $cacheControl = null): ISqlPatternParser
109
    {
110
        if ($cacheControl === null) {
111
            return new SqlPatternParser();
112
        } else {
113
            return new CachingSqlPatternParser($cacheControl);
114
        }
115
    }
116
117
    public function createStatementExceptionFactory(): StatementExceptionFactory
118
    {
119
        return new StatementExceptionFactory();
120
    }
121
122
    public function createConnection(string $connName, ConnectionParameters $params): IConnection
123
    {
124
        return new Connection($connName, $params);
125
    }
126
127
    public function createCacheControl(IConnection $connection = null): ICacheControl
128
    {
129
        $prefix = 'Ivory' . Ivory::VERSION . '.';
130
131
        if ($connection === null) {
132
            $postfix = '';
133
        } else {
134
            $params = $connection->getParameters();
135
            $postfix = sprintf(
136
                '.%s.%d.%s',
137
                $params->getHost(),
138
                $params->getPort(),
139
                $params->getDbName()
140
            );
141
        }
142
143
        return new CacheControl(Ivory::getDefaultCacheImpl(), $prefix, $postfix);
144
    }
145
146
    public function createTransactionHandle(
147
        IStatementExecution $stmtExec,
148
        IObservableTransactionControl $observableTxCtl
149
    ): ITxHandle {
150
        return new TxHandle($stmtExec, $observableTxCtl);
151
    }
152
}
153