| 1: | <?php
|
| 2: |
|
| 3: | namespace ngatngay\database;
|
| 4: |
|
| 5: | use PDO;
|
| 6: | use PDOStatement;
|
| 7: |
|
| 8: | class database
|
| 9: | {
|
| 10: | |
| 11: | |
| 12: |
|
| 13: | private $pdo;
|
| 14: |
|
| 15: | |
| 16: | |
| 17: |
|
| 18: | public function __construct($pdo = null)
|
| 19: | {
|
| 20: | $this->pdo = $pdo;
|
| 21: | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
|
| 22: | $this->pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [statement::class]);
|
| 23: | }
|
| 24: |
|
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: |
|
| 30: | public function query($sql, $params = null)
|
| 31: | {
|
| 32: | $stmt = $this->pdo->prepare($sql);
|
| 33: | $stmt->execute($params);
|
| 34: |
|
| 35: | return $stmt;
|
| 36: | }
|
| 37: |
|
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: |
|
| 43: | public function insert($table, $params)
|
| 44: | {
|
| 45: | $sql = 'insert into "' . $table . '"'
|
| 46: | . ' (' . implode(',', $this->buildName(array_keys($params))) . ')'
|
| 47: | . ' values (' . implode(',', array_fill(0, count($params), '?')) . ')';
|
| 48: |
|
| 49: | $this->query($sql, array_values($params));
|
| 50: |
|
| 51: | return $this->pdo->lastInsertId();
|
| 52: | }
|
| 53: |
|
| 54: |
|
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: |
|
| 61: | public function update_or_insert($table, $con, $arr)
|
| 62: | {
|
| 63: | $whereConditions = [];
|
| 64: | $whereParams = [];
|
| 65: |
|
| 66: | foreach ($con as $column => $value) {
|
| 67: | $whereConditions[] = sprintf('"%s" = ?', $column);
|
| 68: | $whereParams[] = $value;
|
| 69: | }
|
| 70: |
|
| 71: | $whereClause = implode(' AND ', $whereConditions);
|
| 72: | $checkSql = sprintf('SELECT COUNT(*) FROM "%s" WHERE %s', $table, $whereClause);
|
| 73: | $count = $this->fetch_column($checkSql, $whereParams);
|
| 74: |
|
| 75: | if ($count > 0) {
|
| 76: | $updateParts = [];
|
| 77: | $updateParams = [];
|
| 78: |
|
| 79: | foreach ($arr as $column => $value) {
|
| 80: | $updateParts[] = sprintf('"%s" = ?', $column);
|
| 81: | $updateParams[] = $value;
|
| 82: | }
|
| 83: |
|
| 84: | $updateClause = implode(', ', $updateParts);
|
| 85: | $updateSql = sprintf('UPDATE "%s" SET %s WHERE %s', $table, $updateClause, $whereClause);
|
| 86: |
|
| 87: | $this->query($updateSql, array_merge($updateParams, $whereParams));
|
| 88: | } else {
|
| 89: | $this->insert($table, array_merge($con, $arr));
|
| 90: | }
|
| 91: | }
|
| 92: |
|
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: |
|
| 98: | public function update($sql, $params = null)
|
| 99: | {
|
| 100: | $stmt = $this->query($sql, $params);
|
| 101: |
|
| 102: | return $stmt->rowCount();
|
| 103: | }
|
| 104: |
|
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: |
|
| 110: | public function fetch($sql, $params = null)
|
| 111: | {
|
| 112: | return $this->query($sql, $params)
|
| 113: | ->fetch();
|
| 114: | }
|
| 115: |
|
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: |
|
| 121: | public function fetchAll($sql, $params = null)
|
| 122: | {
|
| 123: | return $this->query($sql, $params)
|
| 124: | ->fetchAll();
|
| 125: | }
|
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: |
|
| 131: | public function fetch_all($sql, $params = null)
|
| 132: | {
|
| 133: | return $this->query($sql, $params)
|
| 134: | ->fetchAll();
|
| 135: | }
|
| 136: |
|
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: |
|
| 143: | public function fetchColumn($sql, $params = null, $column = 0)
|
| 144: | {
|
| 145: | $stmt = $this->query($sql, $params);
|
| 146: | return $stmt->fetchColumn($column);
|
| 147: | }
|
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: |
|
| 154: | public function fetch_column($sql, $params = null, $column = 0)
|
| 155: | {
|
| 156: | $stmt = $this->query($sql, $params);
|
| 157: | return $stmt->fetchColumn($column);
|
| 158: | }
|
| 159: |
|
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: |
|
| 165: | public function getOffset($page, $perPage)
|
| 166: | {
|
| 167: | return $page * $perPage - $perPage;
|
| 168: | }
|
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: |
|
| 174: | public function get_offset($page, $perPage)
|
| 175: | {
|
| 176: | return $page * $perPage - $perPage;
|
| 177: | }
|
| 178: |
|
| 179: | |
| 180: | |
| 181: | |
| 182: |
|
| 183: | public function quote($str)
|
| 184: | {
|
| 185: | return $this->pdo->quote($str);
|
| 186: | }
|
| 187: |
|
| 188: | |
| 189: | |
| 190: | |
| 191: |
|
| 192: | public function buildName($arr)
|
| 193: | {
|
| 194: | return array_map(function ($item) {
|
| 195: | return '"' . $item . '"';
|
| 196: | }, $arr);
|
| 197: | }
|
| 198: | |
| 199: | |
| 200: | |
| 201: |
|
| 202: | public function build_name($arr)
|
| 203: | {
|
| 204: | return array_map(function ($item) {
|
| 205: | return '"' . $item . '"';
|
| 206: | }, $arr);
|
| 207: | }
|
| 208: | }
|
| 209: | |