Skip to content

Commit 7b20019

Browse files
Merge pull request #7 from javanile/next
Next
2 parents 0bc1c4c + 46a9e8d commit 7b20019

15 files changed

+406
-222
lines changed

${Forkfile.name}.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
build/
33
vendor/
4+
tests/fixtures/*.lock
45
composer.lock
56
*.log

Forkfile.0

Lines changed: 0 additions & 3 deletions
This file was deleted.

Makefile.0

Lines changed: 0 additions & 5 deletions
This file was deleted.

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
The best way to import SQL file on your database.
99

10-
## UsageA
11-
1210
## Get started
1311

1412
```bash
@@ -19,11 +17,6 @@ $ composer require javanile/mysql-import
1917
$ ./vendor/bin/mysql-import database.sql
2018
```
2119

22-
## Usage
23-
This is how to use this
24-
This is how to use this
25-
26-
2720
## Testing
2821

2922
```bash
@@ -37,3 +30,17 @@ $ docker-compose run --rm composer install
3730
```bash
3831
$ docker-compose run --rm phpunit tests
3932
```
33+
34+
## TDD
35+
36+
```bash
37+
$ docker-compose run --rm phpunit tests --filter ::testLoader
38+
```
39+
40+
```bash
41+
$ docker-compose run --rm phpunit tests --stop-on-failure
42+
```
43+
44+
```bash
45+
docker-compose run --rm php ./bin/mysql-import ./tests/fixtures/database.sql -psecret -utest --do-while
46+
```

RELEASE

Lines changed: 0 additions & 1 deletion
This file was deleted.

mysql-import renamed to bin/mysql-import

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
* @copyright 2015-2019 Javanile
1313
*/
1414

15-
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
16-
require_once __DIR__ . '/vendor/autoload.php';
17-
} elseif (file_exists(__DIR__ . '/../autoload.php')) {
18-
require_once __DIR__ . '/../autoload.php';
19-
} else {
15+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
16+
require_once __DIR__ . '/../vendor/autoload.php';
17+
} elseif (file_exists(__DIR__ . '/../../autoload.php')) {
2018
require_once __DIR__ . '/../../autoload.php';
19+
} else {
20+
require_once __DIR__ . '/../../../autoload.php';
2121
}
2222

2323
use Javanile\MysqlImport\MysqlImport;

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "javanile/mysql-import",
33
"description": "Import SQL file from command-line",
4-
"version": "0.0.15",
4+
"version": "0.0.16",
55
"license": "MIT",
66
"type": "library",
77
"authors": [
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"bin": [
14-
"mysql-import"
14+
"bin/mysql-import"
1515
],
1616
"autoload": {
1717
"psr-4": {

src/DatabaseAdapter.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
/**
3+
* MysqlImport.
4+
*
5+
* Import database from command-line.
6+
*
7+
* @category Command-line
8+
*
9+
* @author Francesco Bianco
10+
* @copyright 2015-2019 Javanile
11+
*/
12+
13+
namespace Javanile\MysqlImport;
14+
15+
class DatabaseAdapter
16+
{
17+
/**
18+
* @var
19+
*/
20+
protected $link;
21+
22+
/**
23+
* @var string|null
24+
*/
25+
protected $host;
26+
27+
/**
28+
* @var string|null
29+
*/
30+
protected $port;
31+
32+
/**
33+
* @var string|null
34+
*/
35+
protected $database;
36+
37+
/**
38+
* @var string|null
39+
*/
40+
protected $user;
41+
42+
/**
43+
* @var string|null
44+
*/
45+
protected $password;
46+
47+
/**
48+
* @var string|null
49+
*/
50+
protected $rootPassword;
51+
52+
/**
53+
* @var string|null
54+
*/
55+
protected $exists;
56+
57+
/**
58+
* @var string|null
59+
*/
60+
protected $empty;
61+
62+
/**
63+
* @var string
64+
*/
65+
protected $connectError;
66+
67+
/**
68+
* DatabaseAdapter constructor.
69+
*
70+
* @param $properties
71+
*/
72+
public function __construct($properties)
73+
{
74+
// Fill all properties
75+
foreach ($properties as $property => $value) {
76+
$this->{$property} = $value;
77+
}
78+
79+
// Set rootPassword using password as default
80+
if (is_null($this->rootPassword) && !is_null($this->password)) {
81+
$this->rootPassword = $this->password;
82+
}
83+
84+
// Set fix host port
85+
if (preg_match('/:([0-9]+)$/', $this->host, $matches)) {
86+
$this->host = substr($this->host, 0, -1 - strlen($matches[1]));
87+
$this->port = $matches[1];
88+
}
89+
}
90+
91+
/**
92+
* Connect to database.
93+
*
94+
* @param $user
95+
* @param $password
96+
*
97+
* @return mysqli
98+
*/
99+
protected function connect($user, $password)
100+
{
101+
try {
102+
$this->connectError = null;
103+
$this->link = @mysqli_connect($this->host, $user, $password, '', $this->port);
104+
if (!$this->link) {
105+
$this->connectError = mysqli_connect_errno();
106+
}
107+
} catch (\Throwable $e) {
108+
$this->logs[] = '['.date('Y-m-d H:i:s').'] ERROR - Message: '.$e->getMessage()."\n".$e->getTraceAsString();
109+
}
110+
111+
return $this->link;
112+
}
113+
114+
/**
115+
* Check if database exists.
116+
*
117+
* @return array|null
118+
*/
119+
protected function exists()
120+
{
121+
$this->exists = @mysqli_fetch_assoc(@mysqli_query($this->link, "SHOW DATABASES LIKE '{$this->database}'"));
122+
123+
return $this->exists;
124+
}
125+
126+
/**
127+
* Check if database is blank.
128+
*
129+
* @return bool
130+
*/
131+
protected function blank()
132+
{
133+
mysqli_select_db($this->link, $this->database);
134+
135+
$tables = @mysqli_fetch_all(@mysqli_query($this->link, 'SHOW TABLES'));
136+
137+
$this->empty = count($tables) == 0;
138+
139+
return $this->empty;
140+
}
141+
142+
/**
143+
* Create new database.
144+
*
145+
* @return bool|mysqli_result
146+
*/
147+
protected function create()
148+
{
149+
$create = mysqli_query(
150+
$this->link,
151+
"CREATE DATABASE `{$this->database}` CHARACTER SET utf8 COLLATE utf8_general_ci"
152+
);
153+
154+
return $create;
155+
}
156+
157+
/**
158+
* Drop the database.
159+
*
160+
* @param null|mixed $agree
161+
*
162+
* @return bool|mysqli_result
163+
*/
164+
public function drop($agree = null)
165+
{
166+
if ($agree != 'yes') {
167+
return;
168+
}
169+
170+
$sql = "DROP DATABASE `{$this->database}`";
171+
172+
return mysqli_query($this->link, $sql);
173+
}
174+
175+
/**
176+
* Get database information.
177+
*/
178+
public function getInfo()
179+
{
180+
return [
181+
'host' => $this->host,
182+
'port' => $this->port,
183+
'database' => $this->database,
184+
];
185+
}
186+
}

src/Loader.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* MysqlImport.
4+
*
5+
* Import database from command-line.
6+
*
7+
* @category Command-line
8+
*
9+
* @author Francesco Bianco
10+
* @copyright 2015-2019 Javanile
11+
*/
12+
13+
namespace Javanile\MysqlImport;
14+
15+
class Loader
16+
{
17+
/**
18+
* @var string
19+
*/
20+
protected $loader;
21+
22+
/**
23+
* Loader constructor.
24+
*/
25+
public function __construct()
26+
{
27+
$this->loader = '/\______';
28+
}
29+
30+
/**
31+
* @param mixed $second
32+
* @param string $message
33+
*
34+
* @return string
35+
*/
36+
public function waiting($second = 10, $message = 'Waiting...')
37+
{
38+
$freq = 5;
39+
for ($i = 0; $i < $second * $freq; $i++) {
40+
$text = '['.substr($this->loader, 0, -1).'] ' . $message;
41+
$this->print($text);
42+
usleep(1000000 / $freq);
43+
$this->loader = substr($this->loader, -1) . substr($this->loader, 0, -1);
44+
$cleaner = str_repeat("\010", strlen($text));
45+
$this->print($cleaner . str_repeat(' ', strlen($text)) . $cleaner);
46+
}
47+
}
48+
49+
/**
50+
* @param $input
51+
*
52+
* @return mixed
53+
*/
54+
protected function print($input)
55+
{
56+
if (defined('PHPUNIT_MYSQL_IMPORT') && PHPUNIT_MYSQL_IMPORT) {
57+
return;
58+
}
59+
60+
echo $input;
61+
}
62+
}

0 commit comments

Comments
 (0)