ARRAY // Execute a DB command function db_cmd($query) { $stmt = $GLOBALS['pdo']->query($query); $stack = array(); if (is_bool($stmt)) { return $stmt; } else { return $stmt->fetchAll(); } } // ------------------------------------- // STRING STRING --> ARRAY // Return all values of a specific column function db_get_columns($table, $column, $order = null, $ordered = null, $max = null) { $command = "select " . $column . " from " . $table; if (is_string($ordered)) { $command = $command . " order by " . $ordered . " " . $order; } else { $command = $command . " order by " . $column . " " . $order; } if (is_int($max)) { $command = $command . " limit 0," . $max; } $command = $command . ";"; // ----------------- $result = db_cmd($command); $result_nest = function($array) { return $array[0]; }; if (is_array($result)) { return array_map($result_nest, $result); } else { return $result; } } // STRING STRING VARYING --> ARRAY // Return all rows that have an 'identifier' column set to given value function db_get_rows($table, $identifier, $value) { return db_cmd("select * from " . $table . " where " . $identifier . " = " . $value . ";"); } // STRING STRING VARYING STRING --> ARRAY // Return the value of a specific column in a given row, identified by an // 'identifier' column set to the given value function db_get_cell($table, $identifier, $value, $cell) { return db_get_rows($table, $identifier, $value)[0][$cell]; } // !!! // !!! ['id'] is used instead of $cell !!! // STRING STRING VARYING STRING --> ARRAY // Return the value of a specific column in a given row, identified by an // 'identifier' column set to the given value function db_get_cells($table, $identifier, $value, $cell) { $id_pop = function ($row, $cell) { return $row['id']; }; $rows = db_get_rows($table, $identifier, $value); return array_map($id_pop, $rows, $cell); } // -------------------------------------- // STRING STRING VARYING STRING VARYING --> NIL // Edit the value of a cell function db_set_cell($table, $identifier, $value, $cell, $new_value) { if (is_string($value)) { $value = "'" . $value . "'"; } if (is_string($new_value)) { $new_value = "'" . $new_value . "'"; } return db_cmd("update " . $table . " set " . $cell . " = " . $new_value . " where " . $identifier . " = " . $value) . ";"; } // ------------------------------------- // STRING STRING --> VARYING // Return the 'biggest' value in a column, as dictated by 'desc' ordering function db_get_biggest($table, $column) { return db_get_columns($table, $column, "desc")[0]; } // STRING STRING --> VARYING // Return the 'smallest' value in a column, as dictated by 'asc' ordering function db_get_smallest($table, $column) { return db_get_columns($table, $column, "asc")[0]; } // ------------------------------------- // STRING STRING --> INTEGER // When passed a column of numbers, it'll increment the biggest number. Good // for creating IDs. If there aren't any numbers in the column, it'll choose 1. function db_new_id($table, $column) { $biggest = db_get_biggest($table, $column); if (is_nan($biggest)) { return 1; } else { return $biggest + 1; } } // ------------------------------------- // STRING ARRAY ARRAY --> BOOLEAN // Create a table with given values to given columns. // First array is a list of columns (as would be provided to SQL), and the // second is the list of values (as would follow " values " in SQL) function db_insert_row($table, $variables, $values) { $variables = comma_sep($variables, ", "); $values = comma_sep(strings_wrap($values), ", "); return db_cmd("insert into " . $table . " (". $variables .")" . " values (" . $values . ")" . ";"); } // ------------------------------------- // STRING --> BOOLEAN // Return whether or not a table of given name exists function db_table_existant($table) { if (!db_cmd("select * from information_schema.tables " . " where table_name = " . string_wrap($table) . ";")) { return false; } else { return true; } } // STRING ARRAY --> BOOLEAN // Create a table of given name and columns (with array of column-strings) function db_create_table($table, $columns) { return db_cmd("create table " . $table . " (" . comma_sep($columns) . ");"); } ?>