Skip to content

Commit

Permalink
add postresql
Browse files Browse the repository at this point in the history
  • Loading branch information
nrenvoise-ubitransport committed Jan 12, 2022
1 parent 6a8d2ff commit 53401a1
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 3 deletions.
28 changes: 28 additions & 0 deletions array/array_flatten.php
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));
1 change: 1 addition & 0 deletions postgresql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
database_parameters.ini
21 changes: 21 additions & 0 deletions postgresql/find_columns.php
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]));
19 changes: 19 additions & 0 deletions postgresql/get_columns_by_table.php
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));
28 changes: 28 additions & 0 deletions postgresql/get_connection.php
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);
}
32 changes: 32 additions & 0 deletions postgresql/get_table_columns.php
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);
18 changes: 18 additions & 0 deletions postgresql/pdo_pgsql.php
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());
5 changes: 2 additions & 3 deletions time/measure_execution_time/measure_execution_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ function myBigFunction($durationInSeconds)
$duration = microtime(true) - $start;
echo "Elapsed time $duration s\n";



// Snippet
$start = microtime(true);
myBigFunction(3);
var_dump(microtime(true) - $start);
exit();
exit();
21 changes: 21 additions & 0 deletions types/type_check.php
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";
}

0 comments on commit 53401a1

Please sign in to comment.