Last active
November 22, 2016 18:41
-
-
Save geilt/5987586 to your computer and use it in GitHub Desktop.
SimpulSections (Wordpress Post Type Database)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Database | |
| * @package SimpulFramework | |
| * @version 1.0 | |
| */ | |
| //Start the Database Singleton. Yes I know they are evil. | |
| try { | |
| $dbh = new SimpulPDO_mySQL("mysql:host=" . DB_HOST . ";dbname=". DB_NAME, DB_USER, DB_PASSWORD); | |
| $dbh->setAttribute( SimpulPDO_mySQL::ATTR_ERRMODE, SimpulPDO_mySQL::ERRMODE_EXCEPTION ); | |
| } | |
| catch(PDOException $e) { | |
| echo $e->getMessage(); | |
| } | |
| class SimpulPDO_mySQL extends PDO{ | |
| /* | |
| * return false/int/array(int) | |
| */ | |
| function insert($table = false, $data = array()){ | |
| try{ | |
| if(empty($table)): | |
| throw new PDOException('No table selected.'); | |
| return false; | |
| endif; | |
| if(!empty($data) && is_array($data)): | |
| if(empty($data[0])): | |
| $data = array($data); | |
| endif; | |
| $columns = self::columnType($table); | |
| $valid_keys = array_keys($columns); | |
| //Start Statement | |
| $statement = 'INSERT INTO ' . $table; | |
| //Prepare Columns Statement | |
| $insert_keys = implode(',',array_keys($data[0])); | |
| $statement .= '(' . $insert_keys . ')' . ' VALUES '; | |
| //Prepare Values Statement | |
| $row = 0; | |
| foreach($data as $insert): | |
| $insert_values = ':' . implode($row . ',:', array_keys($insert)) . $row; | |
| $statement_values[] = '(' . $insert_values . ')'; | |
| $row++; | |
| endforeach; | |
| $statement .= implode(',', $statement_values); | |
| //Prepare Statement | |
| $sth = parent::prepare($statement); | |
| //Bind Parameters | |
| $row = 0; | |
| foreach($data as $insert): | |
| foreach($insert as $key => $value): | |
| if(!in_array($key,$valid_keys)): | |
| throw new PDOException("Column does not exist: " . $key . '.'); | |
| return false; | |
| else: | |
| $sth->bindValue(':' . $key . $row, $value, $columns[$key]); | |
| endif; | |
| endforeach; | |
| $row++; | |
| endforeach; | |
| $sth->execute(); | |
| $total_inserted = $sth->rowCount(); | |
| if($total_inserted > 1): | |
| $last_insert_id = parent::lastInsertId(); | |
| $range = range($last_insert_id, ($last_insert_id + $total_inserted), 1); | |
| return $range; | |
| elseif($total_inserted == 1): | |
| return parent::lastInsertId(); | |
| else: | |
| return false; | |
| endif; | |
| else: | |
| throw new PDOException("Data provided was empty or not an array."); | |
| return false; | |
| endif; | |
| } | |
| catch(PDOException $e){ | |
| error_log( 'SimpulPDO_mySQL::insert error: ' . $e->getMessage() ); | |
| } | |
| } | |
| function update($table = false, $data = array(), $where = array(), $type){ | |
| try{ | |
| if(empty($table)): | |
| throw new PDOException('No table selected.'); | |
| return false; | |
| endif; | |
| if(!empty($data) && is_array($data)): | |
| if(empty($data[0])): | |
| $data = array($data); | |
| endif; | |
| $statement = 'UPDATE ' . $table . ' SET '; | |
| foreach($data as $updates): | |
| $row = 0; | |
| foreach($updates as $key => $value): | |
| $statement .= $key . '=' . ':' . $row . $key; | |
| endforeach; | |
| endforeach; | |
| endif; | |
| } | |
| catch(PDOException $e){ | |
| error_log( 'SimpulPDO_mySQL::update error: ' . $e->getMessage() ); | |
| } | |
| } | |
| function get_var($query = null){ | |
| try{ | |
| if(!empty($query)): | |
| $sth->prepare($query); | |
| $sth->execute(); | |
| $var = $sth->fetch(PDO::FETCH_NUM); | |
| return $var[0]; | |
| else: | |
| return false; | |
| endif; | |
| } | |
| catch(PDOException $e){ | |
| error_log( 'SimpulPDO_mySQL::get_var error: ' . $e->getMessage() ); | |
| } | |
| } | |
| function get_results($query = null, $output = 'OBJECT'){ | |
| try{ | |
| if(!empty($query)): | |
| $sth->prepare($query); | |
| $sth->execute(); | |
| if ( $output == ARRAY_A || $output == ARRAY_N ): | |
| $var = $sth->fetchAll(PDO::FETCH_ASSOC); | |
| else: | |
| $var = $sth->fetchAll(PDO::FETCH_OBJ); | |
| endif; | |
| return $var; | |
| else: | |
| return false; | |
| endif; | |
| } | |
| catch(PDOException $e){ | |
| error_log( 'SimpulPDO_mySQL::get_results error: ' . $e->getMessage() ); | |
| } | |
| } | |
| function get_row($query = null, $output = 'OBJECT'){ | |
| try{ | |
| if(!empty($query)): | |
| $sth->prepare($query); | |
| $sth->execute(); | |
| if ( $output == ARRAY_A || $output == ARRAY_N ): | |
| $var = $sth->fetch(PDO::FETCH_ASSOC); | |
| else: | |
| $var = $sth->fetch(PDO::FETCH_OBJ); | |
| endif; | |
| return $var; | |
| else: | |
| return false; | |
| endif; | |
| } | |
| catch(PDOException $e){ | |
| error_log( 'SimpulPDO_mySQL::get_row error: ' . $e->getMessage() ); | |
| } | |
| } | |
| function delete($table = false, $where = array() ){ | |
| } | |
| function columnType($table) | |
| { | |
| try{ | |
| if(empty($table)): | |
| throw new PDOException('No table selected.'); | |
| return false; | |
| endif; | |
| $table_columns = parent::query("SHOW COLUMNS FROM " . $table); | |
| if(empty($table_columns)): | |
| throw new PDOException('Table ' . $table . ' does not exist.'); | |
| return false; | |
| endif; | |
| foreach($table_columns as $column): | |
| if(strpos($column['Type'], 'int')): | |
| $tmptype = parent::PARAM_INT; | |
| else: | |
| $tmptype = parent::PARAM_STR; | |
| endif; | |
| $columns[$column['Field']] = $tmptype; | |
| endforeach; | |
| return $columns; | |
| } | |
| catch(PDOException $e){ | |
| error_log( 'SimpulPDO_mySQL::insert error: ' . $e->getMessage() ); | |
| } | |
| } | |
| public static function groupFetch($data, $column){ | |
| if(!empty($data) && !empty($column)): | |
| $result = array(); | |
| foreach($data as $row): | |
| $result[$row->{$column}] = $row; | |
| endforeach; | |
| $data = $result; | |
| endif; | |
| return $data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment