2012-01-26 16:09:05 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpenSQLManager
|
|
|
|
*
|
|
|
|
* Free Database manager for Open Source Databases
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
2012-04-02 10:23:27 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-01-26 16:09:05 -05:00
|
|
|
*/
|
2012-01-30 07:57:17 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PostgreSQL specifc class
|
|
|
|
*
|
|
|
|
* @extends DB_PDO
|
|
|
|
*/
|
|
|
|
class pgSQL extends DB_PDO {
|
|
|
|
|
2012-02-02 17:43:09 -05:00
|
|
|
/**
|
|
|
|
* Connect to a PosgreSQL database
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-02 17:43:09 -05:00
|
|
|
* @param string $dsn
|
|
|
|
* @param string $username=null
|
|
|
|
* @param string $password=null
|
|
|
|
* @param array $options=array()
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function __construct($dsn, $username=null, $password=null, $options=array())
|
2012-01-30 07:57:17 -05:00
|
|
|
{
|
2012-02-01 16:36:55 -05:00
|
|
|
parent::__construct("pgsql:$dsn", $username, $password, $options);
|
2012-02-10 13:13:19 -05:00
|
|
|
|
|
|
|
//Get db manip class
|
2012-02-29 14:36:42 -05:00
|
|
|
$class = __CLASS__.'_sql';
|
|
|
|
$this->sql = new $class;
|
2012-01-30 07:57:17 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-01-26 16:09:05 -05:00
|
|
|
|
2012-01-30 14:03:16 -05:00
|
|
|
/**
|
|
|
|
* Empty a table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function truncate($table)
|
2012-01-30 14:03:16 -05:00
|
|
|
{
|
2012-02-02 17:43:09 -05:00
|
|
|
$sql = 'TRUNCATE "' . $table . '"';
|
2012-04-02 10:23:27 -04:00
|
|
|
$this->query($sql);
|
2012-02-02 17:43:09 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-02-02 17:43:09 -05:00
|
|
|
|
2012-02-06 16:17:14 -05:00
|
|
|
/**
|
|
|
|
* Get list of databases for the current connection
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-06 16:17:14 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function get_dbs()
|
2012-02-06 16:17:14 -05:00
|
|
|
{
|
2012-02-13 13:29:31 -05:00
|
|
|
$sql = <<<SQL
|
2012-04-02 10:23:27 -04:00
|
|
|
SELECT "datname" FROM "pg_database"
|
|
|
|
WHERE "datname" NOT IN ('template0','template1')
|
2012-02-13 13:10:48 -05:00
|
|
|
ORDER BY 1
|
|
|
|
SQL;
|
2012-02-13 13:29:31 -05:00
|
|
|
|
2012-02-06 16:17:14 -05:00
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
$dbs = $res->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
return $dbs;
|
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-02-06 16:17:14 -05:00
|
|
|
|
2012-02-02 17:43:09 -05:00
|
|
|
/**
|
2012-02-02 19:07:26 -05:00
|
|
|
* Get the list of tables for the current db
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-02 17:43:09 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function get_tables()
|
2012-02-02 17:43:09 -05:00
|
|
|
{
|
2012-02-13 14:16:29 -05:00
|
|
|
$sql = <<<SQL
|
2012-04-02 10:23:27 -04:00
|
|
|
SELECT "tablename" FROM "pg_tables"
|
|
|
|
WHERE "tablename" NOT LIKE 'pg_%'
|
|
|
|
AND "tablename" NOT LIKE 'sql_%'
|
2012-02-13 14:16:29 -05:00
|
|
|
SQL;
|
2012-02-02 17:43:09 -05:00
|
|
|
|
|
|
|
$res = $this->query($sql);
|
|
|
|
|
2012-02-10 13:13:19 -05:00
|
|
|
$tables = $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-03-23 15:29:34 -04:00
|
|
|
$good_tables = array();
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-03-23 15:29:34 -04:00
|
|
|
foreach($tables as $t)
|
|
|
|
{
|
|
|
|
$good_tables[] = $t['tablename'];
|
|
|
|
}
|
2012-02-02 17:43:09 -05:00
|
|
|
|
2012-03-23 15:29:34 -04:00
|
|
|
return $good_tables;
|
2012-02-10 13:13:19 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-02-10 13:13:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of system tables
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-10 13:13:19 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function get_system_tables()
|
2012-02-10 13:13:19 -05:00
|
|
|
{
|
2012-02-13 14:16:29 -05:00
|
|
|
$sql = <<<SQL
|
|
|
|
SELECT "tablename" FROM "pg_tables"
|
|
|
|
WHERE "tablename" LIKE 'pg\_%'
|
|
|
|
OR "tablename" LIKE 'sql\%'
|
|
|
|
SQL;
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-10 13:13:19 -05:00
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
$tables = $res->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
return $tables;
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-10 13:13:19 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-02-10 13:13:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of schemas, either for the current connection, or
|
|
|
|
* for the current datbase, if specified.
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-10 13:13:19 -05:00
|
|
|
* @param string $database=""
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function get_schemas($database="")
|
2012-02-10 13:13:19 -05:00
|
|
|
{
|
2012-02-10 16:57:54 -05:00
|
|
|
if($database === "")
|
|
|
|
{
|
2012-02-13 13:29:31 -05:00
|
|
|
$sql = <<<SQL
|
2012-04-02 10:23:27 -04:00
|
|
|
SELECT DISTINCT "schemaname" FROM "pg_tables"
|
2012-02-13 13:10:48 -05:00
|
|
|
WHERE "schemaname" NOT LIKE 'pg\_%'
|
|
|
|
SQL;
|
2012-02-13 13:29:31 -05:00
|
|
|
|
2012-02-10 16:57:54 -05:00
|
|
|
}
|
|
|
|
|
2012-02-13 13:29:31 -05:00
|
|
|
$sql = <<<SQL
|
2012-02-13 13:10:48 -05:00
|
|
|
SELECT "nspname" FROM pg_namespace
|
|
|
|
WHERE "nspname" NOT LIKE 'pg\_%'
|
|
|
|
SQL;
|
2012-02-10 13:13:19 -05:00
|
|
|
|
|
|
|
$res = $this->query($sql);
|
|
|
|
$schemas = $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-02-10 16:57:54 -05:00
|
|
|
|
|
|
|
return $schemas;
|
2012-02-02 17:43:09 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-02-02 17:43:09 -05:00
|
|
|
|
|
|
|
/**
|
2012-02-02 19:07:26 -05:00
|
|
|
* Get a list of views for the current db
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-02 17:43:09 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function get_views()
|
2012-02-02 17:43:09 -05:00
|
|
|
{
|
2012-02-13 14:16:29 -05:00
|
|
|
$sql = <<<SQL
|
2012-04-02 10:23:27 -04:00
|
|
|
SELECT "viewname" FROM "pg_views"
|
2012-02-13 14:16:29 -05:00
|
|
|
WHERE "viewname" NOT LIKE 'pg\_%';
|
|
|
|
SQL;
|
2012-02-02 17:43:09 -05:00
|
|
|
|
|
|
|
$res = $this->query($sql);
|
|
|
|
|
|
|
|
$views = $res->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
return $views;
|
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-02-02 17:43:09 -05:00
|
|
|
|
2012-02-06 16:56:16 -05:00
|
|
|
/**
|
|
|
|
* Return the number of rows returned for a SELECT query
|
2012-04-02 10:23:27 -04:00
|
|
|
*
|
2012-02-06 16:56:16 -05:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function num_rows()
|
2012-02-06 16:56:16 -05:00
|
|
|
{
|
2012-02-13 10:53:12 -05:00
|
|
|
return (isset($this->statement)) ? $this->statement->rowCount : FALSE;
|
2012-02-06 16:56:16 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
/**
|
|
|
|
* Create an SQL backup file for the current database's structure
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function backup_structure()
|
|
|
|
{
|
|
|
|
// @todo Implement Backup function
|
2012-04-02 10:23:27 -04:00
|
|
|
return '';
|
2012-02-28 16:02:37 -05:00
|
|
|
}
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-02 10:23:27 -04:00
|
|
|
|
2012-02-28 16:02:37 -05:00
|
|
|
/**
|
|
|
|
* Create an SQL backup file for the current database's data
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function backup_data()
|
|
|
|
{
|
|
|
|
// @todo Implement Backup function
|
|
|
|
return '';
|
|
|
|
}
|
2012-02-02 17:43:09 -05:00
|
|
|
}
|
2012-02-06 17:39:53 -05:00
|
|
|
//End of pgsql.php
|