PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC )); } catch (PDOException $e) { $_error = $e->getMessage(); $_page = 'fail'; } }else{ if($_page != 'disconnect') $_page = 'connect'; } function getQuery($query){ global $db; return $db->query($query)->fetchall(); } function getPrepareQuery($query, $args){ global $db; $sth = $db->prepare($query); $sth->execute($args); return $sth->fetchAll(); } function getSchemasList(){ $schemas = array(); foreach (getQuery('SELECT schema_name FROM information_schema.schemata') as $schema) { $schemas[] = $schema['schema_name']; } return $schemas; } function getTablesList(){ global $db; return $db->query("SELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')")->fetchall(); } function getTablesListBySchema($schema){ $tables = array(); foreach (getPrepareQuery("SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = ?", array($schema)) as $table) { $tables[] = $table['table_name']; } return $tables; } function getTableColumnsList($schema, $table){ return getPrepareQuery("SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_scale, datetime_precision FROM information_schema.columns WHERE table_schema = ? AND table_name = ? ORDER BY ordinal_position", array($schema, $table)); } function getTableColumnsNames($schema, $table){ $columns = array(); foreach (getPrepareQuery("SELECT column_name FROM information_schema.columns WHERE table_schema = ? AND table_name = ? ORDER BY ordinal_position", array($schema, $table)) as $column) { $columns[] = $column['column_name']; } return $columns; } function getTableContentList($schema, $table, $offset = 0){ global $config; //Note: check schema and table before !!! return getPrepareQuery("SELECT * FROM \"$schema\".\"$table\" LIMIT ".$config["limit_size"]." OFFSET ?", array($offset)); } function getTableCount($schema, $table){ //Note: check schema and table before !!! return getQuery("SELECT COUNT(*) count FROM \"$schema\".\"$table\"")[0]["count"]; } function insertTableLine($schema, $table, $data){ //REALLY UNSAFE //Note: check schema and table before !!! global $db; $sql = "INSERT INTO \"$schema\".\"$table\"("; $first = key($data); foreach ($data as $key => $value) { if($key !== $first) $sql .= ", "; $sql .= "\"$key\""; } $sql .= ") VALUES ("; foreach ($data as $key => $value) { if($key !== $first) $sql .= ", "; $sql .= $db->quote($value); } $sql .= ")"; return getQuery($sql); } //render function printAssocTable($data, $fn){ $str = ''; $str .= ''; foreach($data as $row){ $str .= ''.($fn != null ? fn($row) : '').''; } return $str.'
'.implode('', array_keys($data[0])).'
'.implode('', $row).'
'; } function checkSchema($schema){ if($schema == null) return '

Any schema selected

'; if(!in_array($schema, getSchemasList())) return '

Unknown schema

'; return true; } function checkTable($schema, $table){ $check_schema = checkSchema($schema); if($check_schema !== true) return $check_schema; if ($table == null) return '

Any base selected

'; if (!in_array($table, getTablesListBySchema($schema))) return '

Unknown base

'; return true; } function printCheck($check){ if($check !== true) echo $check; return $check === true; } function endsWith($haystack, $needle) { $length = strlen($needle); return $length === 0 || (substr($haystack, -$length) === $needle); }