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