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

aimeos / aimeos-base / 0a03bfa2-d5b9-4ef5-9ea0-db0743c5ee58

03 Dec 2025 01:25PM UTC coverage: 88.574% (-0.05%) from 88.621%
0a03bfa2-d5b9-4ef5-9ea0-db0743c5ee58

push

circleci

aimeos
Add encrypt option

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

1690 of 1908 relevant lines covered (88.57%)

11.96 hits per line

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

83.61
/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
print_r( $param );
54✔
74

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

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

81
                unset( $conn );
54✔
82

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

87
                return $this;
54✔
88
        }
89

90

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

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

114

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

125

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

136

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

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

158

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

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

177

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

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

196

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

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

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

230
                return $dsn;
54✔
231
        }
232
}
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