-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from activecollab/mysqli-extract-port-from-host…
…-string Mysqli extract port from host string
- Loading branch information
Showing
31 changed files
with
408 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection\BatchInsert; | ||
|
||
use ActiveCollab\DatabaseConnection\ConnectionInterface; | ||
use InvalidArgumentException; | ||
use BadMethodCallException; | ||
use InvalidArgumentException; | ||
use RuntimeException; | ||
|
||
/** | ||
|
@@ -28,49 +37,49 @@ class BatchInsert implements BatchInsertInterface | |
private $fields; | ||
|
||
/** | ||
* Cached SQL query foundation | ||
* Cached SQL query foundation. | ||
* | ||
* @var string | ||
*/ | ||
private $sql_foundation; | ||
|
||
/** | ||
* String that's used to prepare statements | ||
* String that's used to prepare statements. | ||
* | ||
* @var string | ||
*/ | ||
private $row_prepare_pattern; | ||
|
||
/** | ||
* Number of fields that are being inserted | ||
* Number of fields that are being inserted. | ||
* | ||
* @var integer | ||
* @var int | ||
*/ | ||
private $fields_num; | ||
|
||
/** | ||
* Numbe of rows that are inserted per single INSERT query | ||
* Numbe of rows that are inserted per single INSERT query. | ||
* | ||
* @var integer | ||
* @var int | ||
*/ | ||
private $rows_per_batch; | ||
|
||
/** | ||
* Insert or replace mode | ||
* Insert or replace mode. | ||
* | ||
* @var string | ||
*/ | ||
private $mode; | ||
|
||
/** | ||
* Array of rows that will need to be inserted into the database | ||
* Array of rows that will need to be inserted into the database. | ||
* | ||
* @var array | ||
*/ | ||
private $rows = []; | ||
|
||
/** | ||
* Total number of rows inserted | ||
* Total number of rows inserted. | ||
* | ||
* @var int | ||
*/ | ||
|
@@ -127,7 +136,7 @@ public function __construct(ConnectionInterface $connection, $table_name, array | |
} | ||
|
||
/** | ||
* Return table name | ||
* Return table name. | ||
* | ||
* @return string | ||
*/ | ||
|
@@ -137,7 +146,7 @@ public function getTableName() | |
} | ||
|
||
/** | ||
* Return the list of files | ||
* Return the list of files. | ||
* | ||
* @return array | ||
*/ | ||
|
@@ -155,7 +164,7 @@ public function getRowsPerBatch() | |
} | ||
|
||
/** | ||
* Return insert or replace mode (default is insert) | ||
* Return insert or replace mode (default is insert). | ||
* | ||
* @return string | ||
*/ | ||
|
@@ -165,14 +174,14 @@ public function getMode() | |
} | ||
|
||
/** | ||
* Insert a row with the given field values | ||
* Insert a row with the given field values. | ||
* | ||
* @param mixed ...$field_values | ||
*/ | ||
public function insert(...$field_values) | ||
{ | ||
if ($this->is_done) { | ||
throw new RuntimeException("This batch insert is already done"); | ||
throw new RuntimeException('This batch insert is already done'); | ||
} | ||
|
||
if (count($field_values) == $this->fields_num) { | ||
|
@@ -187,14 +196,14 @@ public function insert(...$field_values) | |
} | ||
|
||
/** | ||
* Insert array of already escaped values | ||
* Insert array of already escaped values. | ||
* | ||
* @param mixed ...$field_values | ||
*/ | ||
public function insertEscaped(...$field_values) | ||
{ | ||
if ($this->is_done) { | ||
throw new RuntimeException("This batch insert is already done"); | ||
throw new RuntimeException('This batch insert is already done'); | ||
} | ||
|
||
if (count($field_values) == $this->fields_num) { | ||
|
@@ -207,12 +216,12 @@ public function insertEscaped(...$field_values) | |
} | ||
|
||
/** | ||
* Insert rows that are already loaded | ||
* Insert rows that are already loaded. | ||
*/ | ||
public function flush() | ||
{ | ||
if ($this->is_done) { | ||
throw new RuntimeException("This batch insert is already done"); | ||
throw new RuntimeException('This batch insert is already done'); | ||
} | ||
|
||
$count_rows = count($this->rows); | ||
|
@@ -226,12 +235,12 @@ public function flush() | |
} | ||
|
||
/** | ||
* Finish with the batch | ||
* Finish with the batch. | ||
*/ | ||
public function done() | ||
{ | ||
if ($this->is_done) { | ||
throw new RuntimeException("This batch insert is already done"); | ||
throw new RuntimeException('This batch insert is already done'); | ||
} | ||
|
||
$this->flush(); | ||
|
@@ -241,7 +250,7 @@ public function done() | |
} | ||
|
||
/** | ||
* Check whether we should insert rows and insert them if we do | ||
* Check whether we should insert rows and insert them if we do. | ||
*/ | ||
private function checkAndInsert() | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection\BatchInsert; | ||
|
||
/** | ||
|
@@ -8,14 +17,14 @@ | |
interface BatchInsertInterface | ||
{ | ||
/** | ||
* Return table name | ||
* Return table name. | ||
* | ||
* @return string | ||
*/ | ||
public function getTableName(); | ||
|
||
/** | ||
* Return the list of files | ||
* Return the list of files. | ||
* | ||
* @return array | ||
*/ | ||
|
@@ -27,33 +36,33 @@ public function getFields(); | |
public function getRowsPerBatch(); | ||
|
||
/** | ||
* Return insert or replace mode (default is insert) | ||
* Return insert or replace mode (default is insert). | ||
* | ||
* @return string | ||
*/ | ||
public function getMode(); | ||
|
||
/** | ||
* Insert a row with the given field values | ||
* Insert a row with the given field values. | ||
* | ||
* @param mixed ...$field_values | ||
*/ | ||
public function insert(...$field_values); | ||
|
||
/** | ||
* Insert array of already escaped values | ||
* Insert array of already escaped values. | ||
* | ||
* @param mixed ...$field_values | ||
*/ | ||
public function insertEscaped(...$field_values); | ||
|
||
/** | ||
* Insert rows that are already loaded | ||
* Insert rows that are already loaded. | ||
*/ | ||
public function flush(); | ||
|
||
/** | ||
* Finish with the batch | ||
* Finish with the batch. | ||
*/ | ||
public function done(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection; | ||
|
||
use ActiveCollab\DatabaseConnection\Connection\MysqliConnection; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection; | ||
|
||
use ActiveCollab\DatabaseConnection\Connection\MysqliConnection; | ||
|
@@ -30,7 +39,17 @@ public function __construct(LoggerInterface &$log = null) | |
*/ | ||
public function mysqli($host, $user, $pass, $select_database = '', $set_connection_encoding = null, $set_connection_encoding_with_query = false) | ||
{ | ||
$link = $this->mysqliConnectFromParams($host, $user, $pass); | ||
if (strpos($host, ':') !== false) { | ||
$host_bits = explode(':', $host); | ||
|
||
if (empty($host_bits[1])) { | ||
$host_bits[1] = 3306; | ||
} | ||
|
||
$link = $this->mysqliConnectFromParams($host_bits[0], (int) $host_bits[1], $user, $pass); | ||
} else { | ||
$link = $this->mysqliConnectFromParams($host, 3306, $user, $pass); | ||
} | ||
|
||
if ($set_connection_encoding && !$set_connection_encoding_with_query) { | ||
$link->set_charset($set_connection_encoding); | ||
|
@@ -50,16 +69,17 @@ public function mysqli($host, $user, $pass, $select_database = '', $set_connecti | |
} | ||
|
||
/** | ||
* @param string $host | ||
* @param string $user | ||
* @param string $pass | ||
* @param string $select_database | ||
* @param string $host | ||
* @param int $port | ||
* @param string $user | ||
* @param string $pass | ||
* @param string $select_database | ||
* @return MysqliLink | ||
* @throws ConnectionException | ||
*/ | ||
private function mysqliConnectFromParams($host, $user, $pass, $select_database = '') | ||
private function mysqliConnectFromParams($host, $port, $user, $pass, $select_database = '') | ||
{ | ||
$link = new MysqliLink($host, $user, $pass); | ||
$link = new MysqliLink($host, $user, $pass, '', $port); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ilijastuden
Author
Member
|
||
|
||
if ($link->connect_error) { | ||
throw new ConnectionException('Failed to connect to database. MySQL said: ' . $link->connect_error); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection; | ||
|
||
use ActiveCollab\DatabaseConnection\Connection\MysqliConnection; | ||
|
@@ -10,14 +19,14 @@ | |
interface ConnectionFactoryInterface | ||
{ | ||
/** | ||
* Connect to MySQL using mysqli extension | ||
* Connect to MySQL using mysqli extension. | ||
* | ||
* @param string $host | ||
* @param string $user | ||
* @param string $pass | ||
* @param string $select_database | ||
* @param string|null $set_connection_encoding | ||
* @param boolean $set_connection_encoding_with_query | ||
* @param bool $set_connection_encoding_with_query | ||
* @return MysqliConnection | ||
*/ | ||
public function mysqli($host, $user, $pass, $select_database = '', $set_connection_encoding = null, $set_connection_encoding_with_query = false); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection\Exception; | ||
|
||
use Exception; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Active Collab DatabaseConnection. | ||
* | ||
* (c) A51 doo <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ActiveCollab\DatabaseConnection\Exception; | ||
|
||
/** | ||
|
Oops, something went wrong.
Correct:
$link = new MysqliLink($host, $user, $pass, $select_database, $port);