Skip to content

Commit

Permalink
Refactory full
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanniramos committed Oct 7, 2013
1 parent 4a0c1be commit f19df13
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
composer.lock
composer.phar
/vendor
/vendor
5 changes: 5 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Blocking access to confidential files
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|inc|bak)$">
Order Allow,Deny
Deny from all
</FilesMatch>
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
language: php

php:
- 5.3
- 5.4
- 5.5

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --prefer-source --no-interaction --dev
- php composer.phar install --dev

script: phpunit
script: "phpunit --colors --coverage-text"
5 changes: 3 additions & 2 deletions README-pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Os drivers suportados serão exibidos na tela.
<?php

// O método abaixo exibe todos os drivers instalados e que são suportados pelo servidor
PDO4You::getAvailableDrivers();
PDO4You::showAvailableDrivers();

?>
~~~
Expand Down Expand Up @@ -357,7 +357,8 @@ MS SQL Server (Versão antiga): http://bit.ly/PDO_MSSQL-PHP53
Dependências
--------------------------------------------------

PHP >= 5.3.2
PHP >= 5.3.2<br />
PHPUnit >= 3.7.0 (necessário para executar o conjunto de testes)



Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Supported drivers will be displayed on the screen.
<?php

// The method below shows all the drivers installed and that are supported by the server
PDO4You::getAvailableDrivers();
PDO4You::showAvailableDrivers();

?>
~~~
Expand Down Expand Up @@ -357,7 +357,8 @@ MS SQL Server (Old version): http://bit.ly/PDO_MSSQL-PHP53
Dependencies
--------------------------------------------------

PHP >= 5.3.2
PHP >= 5.3.2<br />
PHPUnit >= 3.7.0 (needed to run the test suite)



Expand Down
23 changes: 0 additions & 23 deletions bootstrap.php

This file was deleted.

7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "library",
"description": "Access SQL databases using the PDO extension",
"keywords": ["database", "singleton", "pdo", "mysql", "postgresql", "sqlite", "mariadb", "oracle"],
"homepage": "https://github.com/giovanniramos/PDO4You",
"homepage": "https://github.com/giovanniramos",
"license": "MIT",
"authors": [
{
Expand All @@ -19,9 +19,12 @@
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"PDO4You": "src"
"PDO4You\\": "src/"
}
}
}
84 changes: 43 additions & 41 deletions demos/DemoCRUD.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function select($instance = null)
// Execute the SQL query in a "pre-defined instance" and store the result
$result = PDO4You::select($sql, $instance);

echo '<div class="code title">Demo with the method - PDO4You::select()</div>';
echo '<div class="code title">Demo with the method PDO4You::select()</div>';
echo '<div class="code debug">PDO4You::select(' . $this->getQuery($sql) . '); ' . $this->getResult($result) . '</div>';
}

Expand All @@ -63,23 +63,23 @@ public function allSelects()
// SQL query
$sql = self::sql();

// Execute the SQL query in a instance and store the result
$result_1 = PDO4You::select($sql);
$result_2 = PDO4You::selectNum($sql);
$result_3 = PDO4You::selectObj($sql);
$result_4 = PDO4You::selectAll($sql);
// Executes the SQL and stores the result
$result = PDO4You::select($sql);
$result_num = PDO4You::selectNum($sql);
$result_obj = PDO4You::selectObj($sql);
$result_all = PDO4You::selectAll($sql);

echo '<div class="code title">Demo with the method - PDO4You::select()</div>';
echo '<div class="code debug">PDO4You::select(' . $this->getQuery($sql) . '); ' . $this->getResult($result_1) . '</div>';
echo '<div class="code title">Demo with the method PDO4You::select()</div>';
echo '<div class="code debug">PDO4You::select(' . $this->getQuery($sql) . '); ' . $this->getResult($result) . '</div>';

echo '<div class="code title">Demo with the method - PDO4You::selectNum()</div>';
echo '<div class="code debug">PDO4You::selectNum(' . $this->getQuery($sql) . '); ' . $this->getResult($result_2) . '</div>';
echo '<div class="code title">Demo with the method PDO4You::selectNum()</div>';
echo '<div class="code debug">PDO4You::selectNum(' . $this->getQuery($sql) . '); ' . $this->getResult($result_num) . '</div>';

echo '<div class="code title">Demo with the method - PDO4You::selectObj()</div>';
echo '<div class="code debug">PDO4You::selectObj(' . $this->getQuery($sql) . '); ' . $this->getResult($result_3) . '</div>';
echo '<div class="code title">Demo with the method PDO4You::selectObj()</div>';
echo '<div class="code debug">PDO4You::selectObj(' . $this->getQuery($sql) . '); ' . $this->getResult($result_obj) . '</div>';

echo '<div class="code title">Demo with the method - PDO4You::selectAll()</div>';
echo '<div class="code debug">PDO4You::selectAll(' . $this->getQuery($sql) . '); ' . $this->getResult($result_4) . '</div>';
echo '<div class="code title">Demo with the method PDO4You::selectAll()</div>';
echo '<div class="code debug">PDO4You::selectAll(' . $this->getQuery($sql) . '); ' . $this->getResult($result_all) . '</div>';
}

/**
Expand All @@ -88,23 +88,23 @@ public function allSelects()
* */
public function multipleInsert()
{
// SQL insertion in JSON format
// SQL Insert in JSON format
$json = '
insert: [
{
table: "users" ,
values: { firstname: "' . $this->genFakeName() . '", lastname: "' . $this->genFakeName() . '" }
values: { firstname: "' . $this->fakeName(true) . '", lastname: "' . $this->fakeName(true) . '" }
},{
table: "users" ,
values: { firstname: "' . $this->genFakeName() . '", lastname: "' . $this->genFakeName() . '" }
values: { firstname: "' . $this->fakeName(true) . '", lastname: "' . $this->fakeName(true) . '" }
}
]
';

// Store the result
// Executes the SQL and stores the result
$result = PDO4You::execute($json);

echo '<div class="code title">Demo with the method - PDO4You::execute() using the INSERT command</div>';
echo '<div class="code title">Demo with Insert command</div>';
echo '<div class="code debug">PDO4You::execute(' . $this->getQuery($json) . '); ' . $this->getResult($result, true) . '</div>';
}

Expand All @@ -114,33 +114,33 @@ public function multipleInsert()
* */
public function multipleUpdate()
{
// SQL update in JSON format
// SQL Update in JSON format
$json = '
update: [
{
table: "users" ,
values: { mail: "' . strtolower($this->genFakeName()) . '@gmail.com" } ,
values: { mail: "' . $this->fakeName() . '@gmail.com" } ,
where: { id: 2 }
},{
table: "users" ,
values: { mail: "' . strtolower($this->genFakeName()) . '@gmail.com" } ,
values: { mail: "' . $this->fakeName() . '@gmail.com" } ,
where: { id: 12 }
},{
table: "users" ,
values: { mail: "' . strtolower($this->genFakeName()) . '@gmail.com" } ,
values: { mail: "' . $this->fakeName() . '@gmail.com" } ,
where: { id: 30 }
},{
table: "users" ,
values: { mail: "' . strtolower($this->genFakeName()) . '@gmail.com" } ,
values: { mail: "' . $this->fakeName() . '@gmail.com" } ,
where: { id: 1 }
}
]
';

// Store the result
// Executes the SQL and stores the result
$result = PDO4You::execute($json);

echo '<div class="code title">Demo with the method - PDO4You::execute() using the UPDATE command</div>';
echo '<div class="code title">Demo with Update command</div>';
echo '<div class="code debug">PDO4You::execute(' . $this->getQuery($json) . '); ' . $this->getResult($result) . '</div>';
}

Expand All @@ -150,7 +150,7 @@ public function multipleUpdate()
* */
public function multipleDelete()
{
// SQL delete in JSON format
// SQL Delete in JSON format
$json = '
delete: [
{
Expand All @@ -169,23 +169,23 @@ public function multipleDelete()
]
';

// Store the result
// Executes the SQL and stores the result
$result = PDO4You::execute($json);

echo '<div class="code title">Demo with the method - PDO4You::execute() using the DELETE command</div>';
echo '<div class="code title">Demo with Delete command</div>';
echo '<div class="code debug">PDO4You::execute(' . $this->getQuery($json) . '); ' . $this->getResult($result) . '</div>';
}

/**
* Update Where
*
* */
public function updateWhere($s, $i)
public function updateWhere($desc, $id)
{
// SQL update in JSON format
$json = ' query: [ { table: "books" , values: { description: "' . $s . '" } , where: { id: ' . $i . ' } } ] ';
$json = ' query: [ { table: "books" , values: { description: "' . $desc . '" } , where: { id: ' . $id . ' } } ] ';

// Store the result
// Executes the SQL and stores the result
$result = PDO4You::update($json);

echo '<div class="code title">Demo with the old method PDO4You::update()</div>';
Expand All @@ -198,10 +198,10 @@ public function updateWhere($s, $i)
*/
private function getQuery($sql)
{
$x = '<strong style="color:green;">' . htmlspecialchars($sql) . '</strong>';
$y = '<strong style="color:red;">' . PDO4You::getConnection() . '</strong>';
$sql = '<strong style="color:green;">' . htmlspecialchars($sql) . '</strong>';
$conn = '<strong style="color:red;">' . PDO4You::getConnection() . '</strong>';

return '"' . $x . '", "' . $y . '"';
return '"' . $sql . '", "' . $conn . '"';
}

/**
Expand All @@ -210,11 +210,11 @@ private function getQuery($sql)
*/
private function getResult($result, $show_lastid = false)
{
$s = '<br /><br /> - The code above will output: <pre style="color:blue;">' . print_r($this->sanitize($result), true) . '</pre>';
$s.= 'Total records affected: <strong style="color:red;">' . PDO4You::rowCount() . '</strong>';
$s.= ($show_lastid) ? '&nbsp;&nbsp; Id of the last iteration: <strong style="color:red;">' . PDO4You::lastId() . '</strong>' : null;
$result = '<br /><br /> - The code above will output: <pre style="color:blue;">' . print_r($this->sanitize($result), true) . '</pre>';
$result.= 'Total records affected: <strong style="color:red;">' . PDO4You::rowCount() . '</strong>';
$result.= ($show_lastid) ? '&nbsp;&nbsp; Id of the last iteration: <strong style="color:red;">' . PDO4You::lastId() . '</strong>' : null;

return $s;
return $result;
}

/**
Expand Down Expand Up @@ -242,12 +242,14 @@ private function sanitize($data)
* Random name generator
*
* */
private function genFakeName()
private function fakeName($ucfirst = false)
{
$v = array("a", "e", "i", "o", "u");
$c = array("b", "c", "d", "f", "g", "h", "j", "l", "m", "n", "p", "q", "r", "s", "t", "v", "x", "z");

return ucfirst($c[array_rand($c, 1)] . $v[array_rand($v, 1)] . $c[array_rand($c, 1)] . $v[array_rand($v, 1)] . $v[array_rand($v, 1)]);
$fakename = $c[array_rand($c, 1)] . $v[array_rand($v, 1)] . $c[array_rand($c, 1)] . $v[array_rand($v, 1)] . $v[array_rand($v, 1)];

return ($ucfirst) ? ucfirst($fakename) : $fakename;
}

}
3 changes: 3 additions & 0 deletions demos/demo4.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
// Sample text
$sample_text = '<p style="color: #CBF;">Lorem ipsum "dolor" sit amet</p>';

// Escape double quotes
$sample_text = addslashes($sample_text);

// UPDATE books SET description = ? WHERE id = 1
$demo->updateWhere($sample_text, 1);
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// Load all the files needed
require __DIR__.'/bootstrap.php';
require __DIR__.'/src/bootstrap.php';

// Connection instance imported and available for use
use PDO4You\PDO4You;
Expand All @@ -10,7 +10,7 @@
PDO4You::css();

// Displaying details on the target server's database connected
PDO4You::getServerInfo();
PDO4You::showServerInfo();
?>
<!doctype html>
<html>
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
if [ ! -f "composer.phar" ]; then
curl -s "https://getcomposer.org/installer" | php
curl -sS https://getcomposer.org/installer | php
fi
php composer.phar install --dev
echo ""
Expand Down
Loading

0 comments on commit f19df13

Please sign in to comment.