Completed
Push — master ( a49ccc...dd28ca )
by Ondřej
03:34
created

StdCoreFactory::createSqlPatternParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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