# phpDox - Source of Driver.php

# Source of file Driver.php

Size: 1,823 Bytes - Last Modified: 2020-04-10T20:54:13-04:00

## src/Drivers/Pgsql/Driver.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    <?php declare(strict_types=1);

    /**

     * Query

     *

     * SQL Query Builder / Database Abstraction Layer

     *

     * PHP version 7.4

     *

     * @package     Query

     * @author      Timothy J. Warren <tim@timshomepage.net>

     * @copyright   2012 - 2020 Timothy J. Warren

     * @license     http://www.opensource.org/licenses/mit-license.html  MIT License

     * @link        https://git.timshomepage.net/aviat/Query

     * @version     3.0.0

     */

    namespace Query\Drivers\Pgsql;

    use Query\Drivers\AbstractDriver;

    /**

     * PostgreSQL specific class

     */

    class Driver extends AbstractDriver {

    	/**

    	 * Connect to a PosgreSQL database

    	 *

    	 * @codeCoverageIgnore

    	 * @param string $dsn

    	 * @param string $username

    	 * @param string $password

    	 * @param array  $options

    	 */

    	public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $options=[])

    	{

    		if (strpos($dsn, 'pgsql') === FALSE)

    		{

    			$dsn = 'pgsql:'.$dsn;

    		}

    		parent::__construct($dsn, $username, $password, $options);

    	}

    	/**

    	 * Get a list of schemas for the current connection

    	 *

    	 * @return array

    	 */

    	public function getSchemas(): ?array

    	{

    		$sql = <<<SQL

    			SELECT DISTINCT "schemaname" FROM "pg_tables"

    			WHERE "schemaname" NOT LIKE 'pg\_%'

    			AND "schemaname" != 'information_schema'

    SQL;

    		return $this->driverQuery($sql);

    	}

    	/**

    	 * Retrieve foreign keys for the table

    	 *

    	 * @param string $table

    	 * @return array

    	 */

    	public function getFks($table): array

    	{

    		$valueMap = [

    			'c' => 'CASCADE',

    			'r' => 'RESTRICT',

    		];

    		$keys = parent::getFks($table);

    		foreach($keys as &$key)

    		{

    			foreach(['update', 'delete'] AS $type)

    			{

    				if ( ! isset($valueMap[$key[$type]]))

    				{

    					// @codeCoverageIgnoreStart

    					continue;

    					// @codeCoverageIgnoreEnd

    				}

    				$key[$type] = $valueMap[$key[$type]];

    			}

    		}

    		return $keys;

    	}

    }