• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aimeos / aimeos-base / 7872fff5-bf18-48e3-82a6-6b18032a5719

03 Dec 2025 01:35PM UTC coverage: 88.568%. Remained the same
7872fff5-bf18-48e3-82a6-6b18032a5719

push

circleci

aimeos
TrustServerCertificate=True

0 of 1 new or added line in 1 file covered. (0.0%)

1689 of 1907 relevant lines covered (88.57%)

11.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/DB/Connection/PDO.php
1
<?php
2

3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage DB
8
 */
9

10

11
namespace Aimeos\Base\DB\Connection;
12

13

14
/**
15
 * Database connection class for \PDO connections.
16
 *
17
 * @package Base
18
 * @subpackage DB
19
 */
20
class PDO extends Base implements Iface
21
{
22
        private ?\PDO $connection = null;
23
        private int $txnumber = 0;
24
        private array $stmts = [];
25

26

27
        /**
28
         * Initializes the PDO connection object.
29
         *
30
         * @param array $params Associative list of connection parameters
31
         */
32
        public function __construct( array $params )
33
        {
34
                if( !isset( $params['dsn'] ) ) {
54✔
35
                        $params['dsn'] = $this->dsn( $params );
54✔
36
                }
37

38
                parent::__construct( $params );
54✔
39

40
                $this->stmts = $params['stmt'] ?? [];
54✔
41
                $this->connect();
54✔
42
        }
43

44

45
        /**
46
         * Closes the connection to the database server
47
         *
48
         * @return \Aimeos\Base\DB\Connection\Iface Connection instance for method chaining
49
         */
50
        public function close() : Iface
51
        {
52
                if( $this->inTransaction() ) {
6✔
53
                        $this->rollback();
×
54
                }
55

56
                unset( $this->connection );
6✔
57
                return $this;
6✔
58
        }
59

60

61
        /**
62
         * Connects (or reconnects) to the database server
63
         *
64
         * @return \Aimeos\Base\DB\Connection\Iface Connection instance for method chaining
65
         */
66
        public function connect() : Iface
67
        {
68
                $param = $this->getParameters();
54✔
69
                $param['driverOptions'][\PDO::ATTR_CASE] = \PDO::CASE_NATURAL;
54✔
70
                $param['driverOptions'][\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
54✔
71
                $param['driverOptions'][\PDO::ATTR_ORACLE_NULLS] = \PDO::NULL_NATURAL;
54✔
72
                $param['driverOptions'][\PDO::ATTR_STRINGIFY_FETCHES] = false;
54✔
73

74
                $pdo = new \PDO( $param['dsn'], $param['username'] ?? '', $param['password'] ?? '', $param['driverOptions'] );
54✔
75
                $conn = $this->connection;
54✔
76

77
                $this->connection = $pdo;
54✔
78
                $this->txnumber = 0;
54✔
79

80
                unset( $conn );
54✔
81

82
                foreach( $this->stmts as $stmt ) {
54✔
83
                        $this->create( $stmt )->execute()->finish();
54✔
84
                }
85

86
                return $this;
54✔
87
        }
88

89

90
        /**
91
         * Creates a \PDO database statement.
92
         *
93
         * @param string $sql SQL statement, maybe with place holders
94
         * @return \Aimeos\Base\DB\Statement\Iface PDO statement object
95
         * @throws \Aimeos\Base\DB\Exception if type is invalid or the \PDO object throws an exception
96
         */
97
        public function create( string $sql ) : \Aimeos\Base\DB\Statement\Iface
98
        {
99
                try
100
                {
101
                        if( strpos( $sql, '?' ) === false ) {
54✔
102
                                return new \Aimeos\Base\DB\Statement\PDO\Simple( $this, $sql );
54✔
103
                        }
104

105
                        return new \Aimeos\Base\DB\Statement\PDO\Prepared( $this, $sql );
24✔
106
                }
107
                catch( \PDOException $e )
×
108
                {
109
                        throw new \Aimeos\Base\DB\Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
×
110
                }
111
        }
112

113

114
        /**
115
         * Returns the underlying connection object
116
         *
117
         * @return \PDO Underlying connection object
118
         */
119
        public function getRawObject()
120
        {
121
                return $this->connection;
54✔
122
        }
123

124

125
        /**
126
         * Checks if a transaction is currently running
127
         *
128
         * @return bool True if transaction is currently running, false if not
129
         */
130
        public function inTransaction() : bool
131
        {
132
                return $this->connection->inTransaction();
6✔
133
        }
134

135

136
        /**
137
         * Starts a transaction for this connection.
138
         *
139
         * Transactions can't be nested and a new transaction can only be started
140
         * if the previous transaction was committed or rolled back before.
141
         *
142
         * @return \Aimeos\Base\DB\Connection\Iface Connection instance for method chaining
143
         */
144
        public function begin() : Iface
145
        {
146
                if( $this->txnumber === 0 )
10✔
147
                {
148
                        if( $this->connection->beginTransaction() === false ) {
10✔
149
                                throw new \Aimeos\Base\DB\Exception( 'Unable to start new transaction' );
×
150
                        }
151
                }
152

153
                $this->txnumber++;
10✔
154
                return $this;
10✔
155
        }
156

157

158
        /**
159
         * Commits the changes done inside of the transaction to the storage.
160
         *
161
         * @return \Aimeos\Base\DB\Connection\Iface Connection instance for method chaining
162
         */
163
        public function commit() : Iface
164
        {
165
                if( $this->txnumber === 1 )
6✔
166
                {
167
                        if( $this->connection->commit() === false ) {
6✔
168
                                throw new \Aimeos\Base\DB\Exception( 'Failed to commit transaction' );
×
169
                        }
170
                }
171

172
                $this->txnumber--;
6✔
173
                return $this;
6✔
174
        }
175

176

177
        /**
178
         * Discards the changes done inside of the transaction.
179
         *
180
         * @return \Aimeos\Base\DB\Connection\Iface Connection instance for method chaining
181
         */
182
        public function rollback() : Iface
183
        {
184
                if( $this->txnumber === 1 )
4✔
185
                {
186
                        if( $this->connection->rollBack() === false ) {
4✔
187
                                throw new \Aimeos\Base\DB\Exception( 'Failed to roll back transaction' );
×
188
                        }
189
                }
190

191
                $this->txnumber--;
4✔
192
                return $this;
4✔
193
        }
194

195

196
        /**
197
         * Returns the connection DSN
198
         *
199
         * @param array $params Associative list of connection parameters
200
         * @return string Connection DSN
201
         */
202
        protected function dsn( array $params ) : string
203
        {
204
                $adapter = $params['adapter'] ?? 'mysql';
54✔
205
                $host = $params['host'] ?? null;
54✔
206
                $port = $params['port'] ?? null;
54✔
207
                $sock = $params['socket'] ?? null;
54✔
208
                $dbase = $params['database'] ?? null;
54✔
209

210
                $dsn = $adapter . ':';
54✔
211

212
                if( $adapter === 'sqlsrv' )
54✔
213
                {
214
                        $dsn .= 'Database=' . $dbase;
×
215
                        $dsn .= isset( $host ) ? ';Server=' . $host . ( isset( $port ) ? ',' . $port : '' ) : '';
×
NEW
216
                        $dsn .= ( $params['encrypt'] ?? false ) ? ';Encrypt=True;TrustServerCertificate=True' : ';Encrypt=False;TrustServerCertificate=True';
×
217
                }
218
                elseif( $sock == null )
54✔
219
                {
220
                        $dsn .= 'dbname=' . $dbase;
54✔
221
                        $dsn .= isset( $host ) ? ';host=' . $host : '';
54✔
222
                        $dsn .= isset( $port ) ? ';port=' . $port : '';
54✔
223
                }
224
                else
225
                {
226
                        $dsn .= 'dbname=' . $dbase . ';unix_socket=' . $sock;
×
227
                }
228

229
                return $dsn;
54✔
230
        }
231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc