Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 093e0d5

Browse files
create model class and database adapter.
1 parent 69df656 commit 093e0d5

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

‎System/Database/DB/PDO.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of simple-mvc-rest-api for PHP.
6+
*
7+
*/
8+
namespace Database\DB;
9+
10+
/**
11+
* Class PDO
12+
*/
13+
final class PDO {
14+
private $pdo = null;
15+
private $statement = null;
16+
17+
public function __construct($hostname, $username, $password, $database, $port) {
18+
try {
19+
$this->pdo = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true));
20+
} catch(\PDOException $e) {
21+
trigger_error('Error: Could not make a database link ( ' . $e->getMessage() . '). Error Code : ' . $e->getCode() . ' <br />');
22+
exit();
23+
}
24+
25+
$this->pdo->exec("SET NAMES 'utf8'");
26+
$this->pdo->exec("SET CHARACTER SET utf8");
27+
$this->pdo->exec("SET CHARACTER_SET_CONNECTION=utf8");
28+
$this->pdo->exec("SET SQL_MODE = ''");
29+
30+
}
31+
32+
public function prepare($sql) {
33+
$this->statement = $this->pdo->prepare($sql);
34+
}
35+
36+
public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) {
37+
if ($length) {
38+
$this->statement->bindParam($parameter, $variable, $data_type, $length);
39+
} else {
40+
$this->statement->bindParam($parameter, $variable, $data_type);
41+
}
42+
}
43+
44+
public function execute() {
45+
try {
46+
if ($this->statement && $this->statement->execute()) {
47+
$data = array();
48+
49+
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
50+
$data[] = $row;
51+
}
52+
53+
$result = new \stdClass();
54+
$result->row = (isset($data[0])) ? $data[0] : array();
55+
$result->rows = $data;
56+
$result->num_rows = $this->statement->rowCount();
57+
}
58+
} catch(\PDOException $e) {
59+
trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode());
60+
}
61+
}
62+
63+
public function query($sql, $params = array()) {
64+
$this->statement = $this->pdo->prepare($sql);
65+
$result = false;
66+
67+
try {
68+
if ($this->statement && $this->statement->execute($params)) {
69+
$data = array();
70+
71+
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
72+
$data[] = $row;
73+
}
74+
75+
$result = new \stdClass();
76+
$result->row = (isset($data[0]) ? $data[0] : array());
77+
$result->rows = $data;
78+
$result->num_rows = $this->statement->rowCount();
79+
}
80+
} catch (\PDOException $e) {
81+
trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
82+
exit();
83+
}
84+
85+
if ($result) {
86+
return $result;
87+
} else {
88+
$result = new \stdClass();
89+
$result->row = array();
90+
$result->rows = array();
91+
$result->num_rows = 0;
92+
return $result;
93+
}
94+
}
95+
96+
public function escape($value) {
97+
$search = array("\\", "0円", "\n", "\r", "\x1a", "'", '"');
98+
$replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"');
99+
return str_replace($search, $replace, $value);
100+
}
101+
102+
public function getLastId() {
103+
return $this->pdo->lastInsertId();
104+
}
105+
106+
public function __destruct() {
107+
$this->pdo = null;
108+
}
109+
}

‎System/Database/DatabaseAdapter.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of simple-mvc-rest-api for PHP.
6+
*
7+
*/
8+
namespace Database;
9+
10+
class DatabaseAdapter {
11+
/**
12+
* Database Connection
13+
*
14+
* @var
15+
*/
16+
private $dbConnection;
17+
18+
/**
19+
* Database constructor. set connection driver [pdo, mysqli, mysql,...]
20+
*
21+
* @param $driver
22+
* @param $hostname
23+
* @param $username
24+
* @param $password
25+
* @param $database
26+
*/
27+
public function __construct($driver, $hostname, $username, $password, $database, $port) {
28+
$class = '\Database\DB\\' . $driver;
29+
30+
if (class_exists($class)) {
31+
$this->dbConnection = new $class($hostname, $username, $password, $database, $port);
32+
} else {
33+
exit('Error: Could not load database driver ' . $driver . '!');
34+
}
35+
}
36+
37+
/**
38+
* @param $sql
39+
* @return mixed
40+
*/
41+
public function query($sql) {
42+
return $this->dbConnection->query($sql);
43+
}
44+
45+
/**
46+
* @param $value
47+
* @return mixed
48+
*/
49+
public function escape($value) {
50+
return $this->dbConnection->escape($value);
51+
}
52+
53+
/**
54+
* @return mixed
55+
*/
56+
public function getLastId() {
57+
return $this->dbConnection->getLastId();
58+
}
59+
}

‎System/MVC/Model.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of simple-mvc-rest-api for PHP.
6+
*
7+
*/
8+
namespace MVC;
9+
10+
class Model {
11+
12+
/**
13+
* @var
14+
*/
15+
public $db;
16+
17+
/**
18+
* Construct
19+
*/
20+
public function __construct() {
21+
$this->db = new \Database\DatabaseAdapter(
22+
DATABASE['Driver'],
23+
DATABASE['Host'],
24+
DATABASE['User'],
25+
DATABASE['Pass'],
26+
DATABASE['Name'],
27+
DATABASE['Port']
28+
);
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /