Skip to content

Commit

Permalink
Merge pull request #8 from activecollab/mysqli-extract-port-from-host…
Browse files Browse the repository at this point in the history
…-string

Mysqli extract port from host string
  • Loading branch information
ilijastuden authored Aug 19, 2016
2 parents 3f624cc + 56219ad commit 3a7b78e
Show file tree
Hide file tree
Showing 31 changed files with 408 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .php_cs.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'standardize_not_equal',
'ternary_spaces',
'trim_array_spaces',
'unused_use ',
'unused_use',
'whitespacy_lines',
'ordered_use',
'short_array_syntax',
Expand Down
53 changes: 31 additions & 22 deletions src/BatchInsert/BatchInsert.php
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;

/**
Expand All @@ -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
*/
Expand Down Expand Up @@ -127,7 +136,7 @@ public function __construct(ConnectionInterface $connection, $table_name, array
}

/**
* Return table name
* Return table name.
*
* @return string
*/
Expand All @@ -137,7 +146,7 @@ public function getTableName()
}

/**
* Return the list of files
* Return the list of files.
*
* @return array
*/
Expand All @@ -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
*/
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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()
{
Expand Down
23 changes: 16 additions & 7 deletions src/BatchInsert/BatchInsertInterface.php
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;

/**
Expand All @@ -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
*/
Expand All @@ -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();
}
9 changes: 9 additions & 0 deletions src/Connection.php
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;
Expand Down
34 changes: 27 additions & 7 deletions src/ConnectionFactory.php
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;
Expand Down Expand Up @@ -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);
Expand All @@ -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.

Copy link
@petarslavnic

petarslavnic Aug 19, 2016

Correct: $link = new MysqliLink($host, $user, $pass, $select_database, $port);

This comment has been minimized.

Copy link
@ilijastuden

ilijastuden Aug 19, 2016

Author Member

Ne mora. Ovaj paket ima mogućnost da se okači na server, bez odabira baze. Pogledaj logiku ispod kačenja na bazu.


if ($link->connect_error) {
throw new ConnectionException('Failed to connect to database. MySQL said: ' . $link->connect_error);
Expand Down
13 changes: 11 additions & 2 deletions src/ConnectionFactoryInterface.php
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;
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/ConnectionException.php
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;
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/ExceptionInterface.php
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;

/**
Expand Down
Loading

0 comments on commit 3a7b78e

Please sign in to comment.