-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a8d2ff
commit 53401a1
Showing
9 changed files
with
170 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
function flatten(array $items) { | ||
return array_merge(...$items); | ||
} | ||
|
||
$fruits = [ | ||
[ | ||
'fjjfx' => [ | ||
'name' => 'apple', | ||
'color' => 'red', | ||
] | ||
], | ||
[ | ||
'sizoekcv' => [ | ||
'name' => 'banana', | ||
'color' => 'yellow', | ||
] | ||
], | ||
[ | ||
'bshzu' => [ | ||
'name' => 'cherry', | ||
'color' => 'red', | ||
], | ||
], | ||
]; | ||
|
||
var_dump(flatten($fruits)); |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
database_parameters.ini |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/get_connection.php'; | ||
|
||
function findColumns(PDO $connection, string $columnsHint) { | ||
$hint = "%$columnsHint%"; | ||
$getColumnsStatement = $connection->prepare("SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE :hint"); | ||
$getColumnsStatement->bindParam(':hint', $hint); | ||
$getColumnsStatement->execute(); | ||
$getColumnsStatement->setFetchMode(PDO::FETCH_NUM); | ||
|
||
foreach ($getColumnsStatement->fetchAll() as $record) { | ||
$records[$record[0]][] = $record[1]; | ||
} | ||
|
||
return $records; | ||
} | ||
|
||
$connection = getConnectionFromIniFile(__DIR__ . '/database_parameters.ini'); | ||
|
||
print_r(findColumns($connection, $argv[1])); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/get_connection.php'; | ||
|
||
function getColumnsByTable(PDO $connection) { | ||
$getColumnsStatement = $connection->prepare("SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS"); | ||
$getColumnsStatement->execute(); | ||
$getColumnsStatement->setFetchMode(PDO::FETCH_NUM); | ||
|
||
foreach ($getColumnsStatement->fetchAll() as $record) { | ||
$records[$record[0]][] = $record[1]; | ||
} | ||
|
||
return $records; | ||
} | ||
|
||
$connection = getConnectionFromIniFile(__DIR__ . '/database_parameters.ini'); | ||
|
||
print_r(getColumnsByTable($connection)); |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
function getConnection($host, $port, $database, $user, $password) { | ||
$connectionParameters = sprintf( | ||
"pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s", | ||
$host, | ||
$port, | ||
$database, | ||
$user, | ||
$password | ||
); | ||
|
||
return new PDO($connectionParameters); | ||
} | ||
|
||
function getConnectionFromIniFile($iniFilePath) { | ||
$parameters = parse_ini_file($iniFilePath); | ||
$connectionParameters = sprintf( | ||
"pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s", | ||
$parameters['host'], | ||
$parameters['port'], | ||
$parameters['database'], | ||
$parameters['user'], | ||
$parameters['password'] | ||
); | ||
|
||
return new PDO($connectionParameters); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/get_connection.php'; | ||
|
||
function getTables(PDO $connection) { | ||
$getTablesStatement = $connection->prepare("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';"); | ||
$getTablesStatement->execute(); | ||
|
||
return array_map(function ($record) { | ||
return $record[0]; | ||
}, $getTablesStatement->fetchAll()); | ||
} | ||
|
||
function getTableColumns(PDO $connection, string $tableName) { | ||
$getColumnsStatement = $connection->prepare("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :tableName"); | ||
$getColumnsStatement->bindParam(':tableName', $tableName); | ||
$getColumnsStatement->execute(); | ||
$getColumnsStatement->setFetchMode(PDO::FETCH_NUM); | ||
|
||
return array_map(function ($record) { | ||
return $record[0]; | ||
}, $getColumnsStatement->fetchAll()); | ||
} | ||
|
||
$connection = getConnectionFromIniFile(__DIR__ . '/database_parameters.ini'); | ||
$tables = getTables($connection); | ||
|
||
foreach ($tables as $table) { | ||
$columns[$table] = getTableColumns($connection, $table); | ||
} | ||
|
||
print_r($columns); |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/get_connection.php'; | ||
|
||
$connection = getConnectionFromIniFile(__DIR__.'/database_parameters.ini'); | ||
|
||
$statement = $connection->prepare('select 3'); | ||
$result = $statement->execute(); | ||
|
||
var_dump($statement->fetchAll()); | ||
|
||
$sentence = "hello world!"; | ||
|
||
$statement = $connection->prepare("select :myParameter as greetings"); | ||
$statement->bindParam(':myParameter', $sentence, PDO::PARAM_STR); | ||
$statement->execute(); | ||
|
||
var_dump($statement->fetchAll()); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
function isAlphaNumeric($value) { | ||
return ctype_alnum($value); | ||
} | ||
|
||
$values = [ | ||
'Hello World!', | ||
'apple', | ||
'123', | ||
'123apple', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
if (!isAlphaNumeric($value)) { | ||
echo "$value is not alphanumeric\n"; | ||
continue; | ||
} | ||
|
||
echo "$value is alphanumeric\n"; | ||
} |