Compare commits
228 Commits
Author | SHA1 | Date |
---|---|---|
Maxime Rainville | a60fc4cf24 | |
Steve Boyd | d2fbce5319 | |
Steve Boyd | beb0f84f2d | |
Guy Sartorelli | 6d5c35116a | |
Steve Boyd | 41fd4718a2 | |
Steve Boyd | 448828c20a | |
Guy Sartorelli | faf9d033ff | |
Steve Boyd | d7b4ccb202 | |
Guy Sartorelli | c9bce8fe57 | |
Sabina Talipova | 4bbabf2421 | |
Steve Boyd | 22e3951244 | |
Steve Boyd | 321d0d853b | |
Guy Sartorelli | 84e13ffde3 | |
Steve Boyd | 17e9f5388c | |
Steve Boyd | 222f20529c | |
Steve Boyd | c46272e751 | |
Steve Boyd | afec73997a | |
Steve Boyd | d3c8e2915e | |
Steve Boyd | a709a741b0 | |
Maxime Rainville | dd4df9800b | |
Steve Boyd | 082742ad23 | |
Robbie Averill | a7c3450d43 | |
Robbie Averill | 0be39423a3 | |
Robbie Averill | 3d50b3f9ec | |
Maxime Rainville | 3e38f845e3 | |
Maxime Rainville | 4e3d3df565 | |
Maxime Rainville | 753d73e1fe | |
Guy Marriott | beed6c7fb7 | |
Ingo Schommer | bf4fb87a01 | |
Ingo Schommer | 66376db094 | |
Ingo Schommer | d607a2bfa9 | |
Sam Minnée | f2ec228c72 | |
Sam Minnee | 82f8a06afa | |
Sam Minnee | 75f4a35f71 | |
Guy Marriott | 08c8293328 | |
Serge Latyntcev | 0ffaf90512 | |
Guy Marriott | 04000ad878 | |
Guy Marriott | 3d6920c121 | |
Guy Marriott | b6bab3561f | |
Sam Minnee | fd27c17a80 | |
Robbie Averill | f85b46d047 | |
Sam Minnee | 32a0aad720 | |
Loz Calver | 0d9fcabc80 | |
Sam Minnee | 8f70ac89ca | |
Sam Minnee | 72787ae83e | |
Robbie Averill | 4c6034f350 | |
Robbie Averill | edfa209a3c | |
Robbie Averill | e123f69b7b | |
Daniel Hensby | 4c89d103c5 | |
Maxime Rainville | 6378003540 | |
Maxime Rainville | b210c7284f | |
Maxime Rainville | 7fe935fc89 | |
Maxime Rainville | 694c4059b9 | |
Robbie Averill | 6cfc30952c | |
Damian Mooyman | e0d5536715 | |
Damian Mooyman | 01cc78ec94 | |
Dylan Wagstaff | 513c969c93 | |
Damian Mooyman | e3825697d0 | |
Daniel Hensby | 47a6ebb4e3 | |
Damian Mooyman | 2bbd73620d | |
Daniel Hensby | 97afbd9a88 | |
Damian Mooyman | f2392eb7c6 | |
Damian Mooyman | 1f6d892609 | |
Damian Mooyman | 2d11336dce | |
Damian Mooyman | aa16771922 | |
Damian Mooyman | 05e15d85d6 | |
Damian Mooyman | a401f7ad24 | |
Loz Calver | 390cb09928 | |
Daniel Hensby | d110b92fc8 | |
Loz Calver | 851309f187 | |
Daniel Hensby | ee356b1ad7 | |
Daniel Hensby | a32f5e556a | |
Damian Mooyman | 1277361a6c | |
Chris Joe | e41df60b40 | |
Damian Mooyman | 685e33cf84 | |
Damian Mooyman | 8c5f95fdaa | |
Neil Gladwin | 7a8bcd1ec5 | |
Damian Mooyman | ff64974e45 | |
Ingo Schommer | 49105eb19b | |
Loz Calver | 4a1968df94 | |
Daniel Hensby | 0c2b48421b | |
Daniel Hensby | d7118b6267 | |
Daniel Hensby | 0b5b6ddad2 | |
Daniel Hensby | e3bebfe453 | |
Daniel Hensby | 5187e747b3 | |
Daniel Hensby | e6dbc0a708 | |
Daniel Hensby | 5dd9b83b57 | |
Daniel Hensby | 1c953025e6 | |
Daniel Hensby | 6a168488ac | |
Daniel Hensby | 1a78f20b8a | |
Daniel Hensby | 4b3fd28b3b | |
Daniel Hensby | 45a57a80f0 | |
Damian Mooyman | c3dbb76ef6 | |
Russell Michell | 45233e4e74 | |
Daniel Hensby | 6beeef75d0 | |
Ingo Schommer | 132c31bc80 | |
Ingo Schommer | 29a2a06f41 | |
Damian Mooyman | 307a6b673d | |
Daniel Hensby | 2cc0199591 | |
Damian Mooyman | e941f0b122 | |
Damian Mooyman | 512f10d745 | |
Daniel Hensby | 35c1428c63 | |
Daniel Hensby | a0b9010b90 | |
Loz Calver | b5214def7c | |
Damian Mooyman | 33e97cc49f | |
Loz Calver | 72ca91981f | |
Daniel Hensby | d3d2875012 | |
Damian Mooyman | f2ba2f6717 | |
Damian Mooyman | a5738dbfd1 | |
Damian Mooyman | d8aab6383e | |
Ingo Schommer | 69a0f136a1 | |
Damian Mooyman | 8c1014b849 | |
Ingo Schommer | 38dcdff18e | |
Damian Mooyman | fc915b856a | |
Daniel Hensby | 7d23f2c97d | |
Damian Mooyman | 586fbce1e8 | |
Damian Mooyman | e2096d690c | |
Sam Minnée | eadb7ac352 | |
Damian Mooyman | 6568b41550 | |
Damian Mooyman | 0abd6a6c68 | |
Damian Mooyman | db2d4574d8 | |
Damian Mooyman | f83fa173d9 | |
Damian Mooyman | f797f49aeb | |
Damian Mooyman | 946d429c41 | |
Damian Mooyman | 85526e7076 | |
helpfulrobot | 9f899b5c97 | |
Daniel Hensby | 377e1ba1ec | |
helpfulrobot | cd17ce4f94 | |
Daniel Hensby | efb3a9dfa2 | |
Damian Mooyman | 3780d1b152 | |
Damian Mooyman | e46a37090c | |
Damian Mooyman | 24caacbf3b | |
Damian Mooyman | 2122e548e2 | |
helpfulrobot | c451c72efa | |
Damian Mooyman | 934fa61433 | |
helpfulrobot | 605ba3eeff | |
Damian Mooyman | 091c4d8a1c | |
helpfulrobot | 8b04cd4bb1 | |
Ingo Schommer | 6f4dc4d8d2 | |
Damian Mooyman | 2b3da3c9a8 | |
Ingo Schommer | 1b7216fcf9 | |
Ingo Schommer | eb4b7cca86 | |
Damian Mooyman | 90a7539ebd | |
Damian Mooyman | f932aa4b04 | |
Damian Mooyman | 28160fa065 | |
Daniel Hensby | 750f4a9d9f | |
Daniel Hensby | db12e5bae3 | |
Damian Mooyman | a596d9d343 | |
Daniel Hensby | 31abb29c5d | |
Daniel Hensby | 606879243a | |
Daniel Hensby | 4f439c1aea | |
Damian Mooyman | f6229a4ebb | |
Damian Mooyman | 52b8a7324c | |
Loz Calver | 0a61b16caf | |
devimust | 9a2dc1adc8 | |
Ingo Schommer | 5a7ea699a0 | |
Ingo Schommer | 6661b0e133 | |
David Preece | 1fe26a8198 | |
David Preece | 7bb29a9288 | |
Damian Mooyman | 1aeb7b1436 | |
Daniel Hensby | 62b87b7ee3 | |
Damian Mooyman | f920d13f7f | |
Sean Harvey | bddbdde5de | |
Damian Mooyman | 48c07ad1ab | |
Sean Harvey | fba93bba72 | |
Sean Harvey | 18fdae9d90 | |
Sean Harvey | 846f31b31c | |
micmania1 | d112ca5d12 | |
James Pluck | b148e2457c | |
torleif | eac309696d | |
Damian Mooyman | 731e25fe5a | |
Damian Mooyman | 010ce575ed | |
ClayLennart | d9699fa28d | |
Simon Welsh | 0c07807e1f | |
Simon Welsh | 4f6a9523f6 | |
Simon Welsh | 042eb98dfe | |
Simon Welsh | 191846ee4a | |
Damian Mooyman | c864f27d61 | |
Damian Mooyman | abe3843012 | |
Simon Welsh | f3be11732c | |
Simon Welsh | 6c966276ab | |
torleif | 7626d74bee | |
Ingo Schommer | 4ca243fc68 | |
Sam Minnee | 863ead3255 | |
Sam Minnée | bc37bf7a4f | |
Sam Minnee | f967c20383 | |
Simon Welsh | b8771b79da | |
Sean Harvey | 65702e4a6b | |
Stig Lindqvist | 082adb4fd6 | |
Simon Welsh | 9e8b755a59 | |
Stephen Shkardoon | 0c7362bbc5 | |
Sam Minnée | c389d79398 | |
Stig Lindqvist | 201e5b7b8b | |
Ingo Schommer | cd7b761bed | |
Ingo Schommer | fc7a21b567 | |
Ingo Schommer | 8673583f13 | |
Ingo Schommer | 618d7f1137 | |
Sam Minnée | 12e2d69c09 | |
Damian Mooyman | 9b623a2b2b | |
Ingo Schommer | 7ba07e905d | |
Sam Minnée | c5eb666447 | |
Damian Mooyman | ef4cd20cfa | |
Ingo Schommer | 9924c8d53a | |
vikas srivastava | 53d86394d2 | |
Sam Minnee | 3fe7671442 | |
Sam Minnée | c46e45f599 | |
Sam Minnee | 06f80d3347 | |
Sam Minnee | 54821bde2d | |
Sam Minnee | 8cd7cc5127 | |
Sam Minnée | c22f7faa53 | |
Damian Mooyman | 37199fc08c | |
Damian Mooyman | 3291147c8e | |
Damian Mooyman | dc7334087c | |
Sam Minnee | 8cd458b818 | |
Sam Minnee | f74e9c260e | |
Hamish Friedlander | 5ba1b80391 | |
Saophalkun Ponlu | fe85c32d5b | |
Saophalkun Ponlu | 0ffaf3d055 | |
Saophalkun Ponlu | eeaa32a148 | |
Ingo Schommer | bb07e6cfd5 | |
Sean Harvey | 65511282a7 | |
Will Rossiter | fa1faecd72 | |
Kirk Mayo | 501c158f7f | |
Sean Harvey | 29512e54b7 | |
Sean Harvey | 200dcf3121 | |
Sean Harvey | 1fd2088c96 | |
Sean Harvey | 8152ddce8c | |
Sean Harvey | 6e30463e3e |
|
@ -0,0 +1,24 @@
|
|||
# For more information about the properties used in this file,
|
||||
# please see the EditorConfig documentation:
|
||||
# http://editorconfig.org
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[*.{yml,js,json,css,scss,feature}]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
|
||||
[composer.json]
|
||||
indent_size = 4
|
||||
|
||||
# The indent size used in the package.json file cannot be changed:
|
||||
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
|
|
@ -0,0 +1,3 @@
|
|||
/tests export-ignore
|
||||
/docs export-ignore
|
||||
/.travis.yml export-ignore
|
|
@ -0,0 +1,25 @@
|
|||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
name: CI
|
||||
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
|
||||
with:
|
||||
# set phpunit to false to prevent automatic generation of mysql phpunit jobs
|
||||
phpunit: false
|
||||
extra_jobs: |
|
||||
- php: 7.4
|
||||
db: pgsql
|
||||
phpunit: true
|
||||
composer_args: --prefer-lowest
|
||||
- php: 8.0
|
||||
db: pgsql
|
||||
phpunit: true
|
||||
- php: 8.1
|
||||
db: pgsql
|
||||
phpunit: true
|
|
@ -0,0 +1,16 @@
|
|||
name: Dispatch CI
|
||||
|
||||
on:
|
||||
# At 12:20 PM UTC, only on Thursday and Friday
|
||||
schedule:
|
||||
- cron: '20 12 * * 4,5'
|
||||
|
||||
jobs:
|
||||
dispatch-ci:
|
||||
name: Dispatch CI
|
||||
# Only run cron on the silverstripe account
|
||||
if: (github.event_name == 'schedule' && github.repository_owner == 'silverstripe') || (github.event_name != 'schedule')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Dispatch CI
|
||||
uses: silverstripe/gha-dispatch-ci@v1
|
|
@ -0,0 +1,17 @@
|
|||
name: Keepalive
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
# The 4th of every month at 10:50am UTC
|
||||
schedule:
|
||||
- cron: '50 10 4 * *'
|
||||
|
||||
jobs:
|
||||
keepalive:
|
||||
name: Keepalive
|
||||
# Only run cron on the silverstripe account
|
||||
if: (github.event_name == 'schedule' && github.repository_owner == 'silverstripe') || (github.event_name != 'schedule')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Keepalive
|
||||
uses: silverstripe/gha-keepalive@v1
|
|
@ -0,0 +1,7 @@
|
|||
mappings:
|
||||
PostgreSQLConnector: SilverStripe\PostgreSQL\PostgreSQLConnector
|
||||
PostgreSQLDatabase: SilverStripe\PostgreSQL\PostgreSQLDatabase
|
||||
PostgreSQLDatabaseConfigurationHelper: SilverStripe\PostgreSQL\PostgreSQLDatabaseConfigurationHelper
|
||||
PostgreSQLQuery: SilverStripe\PostgreSQL\PostgreSQLQuery
|
||||
PostgreSQLQueryBuilder: SilverStripe\PostgreSQL\PostgreSQLQueryBuilder
|
||||
PostgreSQLSchemaManager: SilverStripe\PostgreSQL\PostgreSQLSchemaManager
|
42
README
42
README
|
@ -1,42 +0,0 @@
|
|||
###############################################
|
||||
PostgreSQL Module
|
||||
###############################################
|
||||
|
||||
Maintainer Contact
|
||||
-----------------------------------------------
|
||||
Geoff Munn (Nickname: gmunn)
|
||||
<geoff (at) silverstripe (dot) com>
|
||||
|
||||
Requirements
|
||||
-----------------------------------------------
|
||||
- PostgreSQL 8.3.x or greater must be installed
|
||||
- PostgreSQL <8.3.0 may work if T-Search is manually installed
|
||||
- Known to work on OS X Leopard, Windows Server 2008 R2 and Linux
|
||||
|
||||
Documentation
|
||||
-----------------------------------------------
|
||||
http://doc.silverstripe.org/doku.php?id=postgres
|
||||
|
||||
Installation Instructions
|
||||
-----------------------------------------------
|
||||
|
||||
Move the 'postgres' folder to the root level of the project.
|
||||
You'll need to create a database with the desired name manually.
|
||||
Run dev/build and you should be set.
|
||||
|
||||
Usage Overview
|
||||
-----------------------------------------------
|
||||
|
||||
See the documentation link for examples of PostgreSQL-specific functionality.
|
||||
|
||||
Known issues:
|
||||
-----------------------------------------------
|
||||
|
||||
All column and table names must be double-quoted. PostgreSQL automatically lower-cases columns, and your queries will fail if you don't.
|
||||
|
||||
Ts_vector columns are not automatically detected by the built-in search filters.
|
||||
That means if you're doing a search through the CMS on a ModelAdmin object, it will use LIKE queries which are very slow.
|
||||
If you're writing your own front-end search system, you can specify the columns to use for search purposes, and you get the full benefits of T-Search.
|
||||
|
||||
If you are using unsupported modules, there may be instances of MySQL-specific SQL queries which will need to be made database-agnostic where possible.
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
# PostgreSQL Module Module
|
||||
|
||||
[![CI](https://github.com/silverstripe/silverstripe-postgresql/actions/workflows/ci.yml/badge.svg)](https://github.com/silverstripe/silverstripe-postgresql/actions/workflows/ci.yml)
|
||||
[![Silverstripe supported module](https://img.shields.io/badge/silverstripe-supported-0071C4.svg)](https://www.silverstripe.org/software/addons/silverstripe-commercially-supported-module-list/)
|
||||
|
||||
## Requirements
|
||||
|
||||
* Silverstripe 4.0
|
||||
* PostgreSQL >=9.2
|
||||
* Note: PostgreSQL 10 has not been tested
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
composer require silverstripe/postgresql
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
### Environment file
|
||||
|
||||
Add the following settings to your `.env` file:
|
||||
|
||||
```
|
||||
SS_DATABASE_CLASS=PostgreSQLDatabase
|
||||
SS_DATABASE_USERNAME=
|
||||
SS_DATABASE_PASSWORD=
|
||||
```
|
||||
|
||||
See [environment variables](https://docs.silverstripe.org/en/4/getting_started/environment_management) for more details. Note that a database will automatically be created via `dev/build`.
|
||||
|
||||
### Through the installer
|
||||
|
||||
Open the installer by browsing to install.php, e.g. http://localhost/install.php
|
||||
Select PostgreSQL in the database list and enter your database details
|
||||
|
||||
## Usage Overview
|
||||
|
||||
See [docs/en](docs/en/README.md) for more information about configuring the module.
|
||||
|
||||
## Known issues
|
||||
|
||||
All column and table names must be double-quoted. PostgreSQL automatically
|
||||
lower-cases columns, and your queries will fail if you don't.
|
||||
|
||||
Collations have known issues when installed on Alpine, MacOS X and BSD derivatives
|
||||
(see [PostgreSQL FAQ](https://wiki.postgresql.org/wiki/FAQ#Why_do_my_strings_sort_incorrectly.3F)).
|
||||
We do not support such installations, although they still may work correctly for you.
|
||||
As a workaround for PostgreSQL 10+ you could manually switch to ICU collations (e.g. und-x-icu).
|
||||
There are no known workarounds for PostgreSQL <10.
|
||||
|
||||
Ts_vector columns are not automatically detected by the built-in search
|
||||
filters. That means if you're doing a search through the CMS on a ModelAdmin
|
||||
object, it will use LIKE queries which are very slow. If you're writing your
|
||||
own front-end search system, you can specify the columns to use for search
|
||||
purposes, and you get the full benefits of T-Search.
|
||||
|
||||
If you are using unsupported modules, there may be instances of MySQL-specific
|
||||
SQL queries which will need to be made database-agnostic where possible.
|
|
@ -1,3 +0,0 @@
|
|||
<?php
|
||||
|
||||
?>
|
|
@ -0,0 +1,23 @@
|
|||
---
|
||||
name: postgresqlconnectors
|
||||
---
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
PostgrePDODatabase:
|
||||
class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase'
|
||||
properties:
|
||||
connector: '%$PDOConnector'
|
||||
schemaManager: '%$PostgreSQLSchemaManager'
|
||||
queryBuilder: '%$PostgreSQLQueryBuilder'
|
||||
PostgreSQLDatabase:
|
||||
class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase'
|
||||
properties:
|
||||
connector: '%$PostgreSQLConnector'
|
||||
schemaManager: '%$PostgreSQLSchemaManager'
|
||||
queryBuilder: '%$PostgreSQLQueryBuilder'
|
||||
PostgreSQLConnector:
|
||||
class: 'SilverStripe\PostgreSQL\PostgreSQLConnector'
|
||||
type: prototype
|
||||
PostgreSQLSchemaManager:
|
||||
class: 'SilverStripe\PostgreSQL\PostgreSQLSchemaManager'
|
||||
PostgreSQLQueryBuilder:
|
||||
class: 'SilverStripe\PostgreSQL\PostgreSQLQueryBuilder'
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
|
||||
use SilverStripe\PostgreSQL\PostgreSQLDatabaseConfigurationHelper;
|
||||
|
||||
// PDO Postgre database
|
||||
DatabaseAdapterRegistry::register(array(
|
||||
/** @skipUpgrade */
|
||||
'class' => 'PostgrePDODatabase',
|
||||
'module' => 'postgresql',
|
||||
'title' => 'PostgreSQL 8.3+ (using PDO)',
|
||||
'helperPath' => __DIR__.'/code/PostgreSQLDatabaseConfigurationHelper.php',
|
||||
'helperClass' => PostgreSQLDatabaseConfigurationHelper::class,
|
||||
'supported' => (class_exists('PDO') && in_array('postgresql', PDO::getAvailableDrivers())),
|
||||
'missingExtensionText' =>
|
||||
'Either the <a href="http://www.php.net/manual/en/book.pdo.php">PDO Extension</a> or
|
||||
the <a href="http://www.php.net/manual/en/ref.pdo-sqlsrv.php">SQL Server PDO Driver</a>
|
||||
are unavailable. Please install or enable these and refresh this page.'
|
||||
));
|
||||
|
||||
|
||||
// PDO Postgre database
|
||||
DatabaseAdapterRegistry::register(array(
|
||||
/** @skipUpgrade */
|
||||
'class' => 'PostgreSQLDatabase',
|
||||
'module' => 'postgresql',
|
||||
'title' => 'PostgreSQL 8.3+ (using pg_connect)',
|
||||
'helperPath' => __DIR__.'/code/PostgreSQLDatabaseConfigurationHelper.php',
|
||||
'helperClass' => PostgreSQLDatabaseConfigurationHelper::class,
|
||||
'supported' => function_exists('pg_connect'),
|
||||
'missingExtensionText' =>
|
||||
'The <a href="http://php.net/pgsql">pgsql</a> PHP extension is not
|
||||
available. Please install or enable it and refresh this page.'
|
||||
));
|
|
@ -0,0 +1 @@
|
|||
When having discussions about this module in issues or pull request please adhere to the [SilverStripe Community Code of Conduct](https://docs.silverstripe.org/en/contributing/code_of_conduct).
|
|
@ -0,0 +1,275 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL;
|
||||
|
||||
use SilverStripe\ORM\Connect\DBConnector;
|
||||
use ErrorException;
|
||||
|
||||
/**
|
||||
* PostgreSQL connector class using the PostgreSQL specific api
|
||||
*
|
||||
* The connector doesn't know anything about schema selection, so code related to
|
||||
* masking multiple databases as schemas should be handled in the database controller
|
||||
* and schema manager.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage model
|
||||
*/
|
||||
class PostgreSQLConnector extends DBConnector
|
||||
{
|
||||
/**
|
||||
* Connection to the PG Database database
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $dbConn = null;
|
||||
|
||||
/**
|
||||
* Name of the currently selected database
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $databaseName = null;
|
||||
|
||||
/**
|
||||
* Reference to the last query result (for pg_affected_rows)
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $lastQuery = null;
|
||||
|
||||
/**
|
||||
* Last parameters used to connect
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $lastParameters = null;
|
||||
|
||||
protected $lastRows = 0;
|
||||
|
||||
/**
|
||||
* Escape a parameter to be used in the connection string
|
||||
*
|
||||
* @param array $parameters All parameters
|
||||
* @param string $key The key in $parameters to pull from
|
||||
* @param string $name The connection string parameter name
|
||||
* @param mixed $default The default value, or null if optional
|
||||
* @return string The completed fragment in the form name=value
|
||||
*/
|
||||
protected function escapeParameter($parameters, $key, $name, $default = null)
|
||||
{
|
||||
if (empty($parameters[$key])) {
|
||||
if ($default === null) {
|
||||
return '';
|
||||
}
|
||||
$value = $default;
|
||||
} else {
|
||||
$value = $parameters[$key];
|
||||
}
|
||||
return "$name='" . addslashes($value) . "'";
|
||||
}
|
||||
|
||||
public function connect($parameters, $selectDB = false)
|
||||
{
|
||||
$this->lastParameters = $parameters;
|
||||
|
||||
// Note: Postgres always behaves as though $selectDB = true, ignoring
|
||||
// any value actually passed in. The controller passes in true for other
|
||||
// connectors such as PDOConnector.
|
||||
|
||||
// Escape parameters
|
||||
$arguments = array(
|
||||
$this->escapeParameter($parameters, 'server', 'host', 'localhost'),
|
||||
$this->escapeParameter($parameters, 'port', 'port', 5432),
|
||||
$this->escapeParameter($parameters, 'database', 'dbname', 'postgres'),
|
||||
$this->escapeParameter($parameters, 'username', 'user'),
|
||||
$this->escapeParameter($parameters, 'password', 'password')
|
||||
);
|
||||
|
||||
// Close the old connection
|
||||
if ($this->dbConn) {
|
||||
pg_close($this->dbConn);
|
||||
}
|
||||
|
||||
// Connect
|
||||
$this->dbConn = @pg_connect(implode(' ', $arguments));
|
||||
if ($this->dbConn === false) {
|
||||
// Extract error details from PHP error handling
|
||||
$error = error_get_last();
|
||||
if ($error && preg_match('/function\\.pg-connect\\<\\/a\\>\\]\\: (?<message>.*)/', $error['message'], $matches)) {
|
||||
$this->databaseError(html_entity_decode($matches['message']));
|
||||
} else {
|
||||
$this->databaseError("Couldn't connect to PostgreSQL database.");
|
||||
}
|
||||
} elseif (pg_connection_status($this->dbConn) != PGSQL_CONNECTION_OK) {
|
||||
throw new ErrorException($this->getLastError());
|
||||
}
|
||||
|
||||
//By virtue of getting here, the connection is active:
|
||||
$this->databaseName = empty($parameters['database']) ? PostgreSQLDatabase::MASTER_DATABASE : $parameters['database'];
|
||||
}
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->lastRows;
|
||||
}
|
||||
|
||||
public function getGeneratedID($table)
|
||||
{
|
||||
$result = $this->query("SELECT currval('\"{$table}_ID_seq\"')")->first();
|
||||
return $result['currval'];
|
||||
}
|
||||
|
||||
public function getLastError()
|
||||
{
|
||||
return pg_last_error($this->dbConn);
|
||||
}
|
||||
|
||||
public function getSelectedDatabase()
|
||||
{
|
||||
return $this->databaseName;
|
||||
}
|
||||
|
||||
public function getVersion()
|
||||
{
|
||||
$version = pg_version($this->dbConn);
|
||||
if (isset($version['server'])) {
|
||||
return $version['server'];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function isActive()
|
||||
{
|
||||
return $this->databaseName && $this->dbConn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the SQL fragment either breaks into or out of a string literal
|
||||
* by counting single quotes
|
||||
*
|
||||
* Handles double-quote escaped quotes as well as slash escaped quotes
|
||||
*
|
||||
* @todo Test this!
|
||||
*
|
||||
* @see http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
|
||||
*
|
||||
* @param string $input The SQL fragment
|
||||
* @return boolean True if the string breaks into or out of a string literal
|
||||
*/
|
||||
public function checkStringTogglesLiteral($input)
|
||||
{
|
||||
// Remove escaped backslashes, count them!
|
||||
$input = preg_replace('/\\\\\\\\/', '', $input);
|
||||
|
||||
// Count quotes
|
||||
$totalQuotes = substr_count($input, "'"); // Includes double quote escaped quotes
|
||||
$escapedQuotes = substr_count($input, "\\'");
|
||||
return (($totalQuotes - $escapedQuotes) % 2) !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iteratively replaces all question marks with numerical placeholders
|
||||
* E.g. "Title = ? AND Name = ?" becomes "Title = $1 AND Name = $2"
|
||||
*
|
||||
* @todo Better consider question marks in string literals
|
||||
*
|
||||
* @param string $sql Paramaterised query using question mark placeholders
|
||||
* @return string Paramaterised query using numeric placeholders
|
||||
*/
|
||||
public function replacePlaceholders($sql)
|
||||
{
|
||||
$segments = preg_split('/\?/', $sql);
|
||||
$joined = '';
|
||||
$inString = false;
|
||||
$num = 0;
|
||||
for ($i = 0; $i < count($segments); $i++) {
|
||||
// Append next segment
|
||||
$joined .= $segments[$i];
|
||||
|
||||
// Don't add placeholder after last segment
|
||||
if ($i === count($segments) - 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// check string escape on previous fragment
|
||||
if ($this->checkStringTogglesLiteral($segments[$i])) {
|
||||
$inString = !$inString;
|
||||
}
|
||||
|
||||
// Append placeholder replacement
|
||||
if ($inString) {
|
||||
$joined .= "?";
|
||||
} else {
|
||||
$joined .= '$' . ++$num;
|
||||
}
|
||||
}
|
||||
return $joined;
|
||||
}
|
||||
|
||||
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
|
||||
{
|
||||
// Reset state
|
||||
$this->lastQuery = null;
|
||||
$this->lastRows = 0;
|
||||
|
||||
// Replace question mark placeholders with numeric placeholders
|
||||
if (!empty($parameters)) {
|
||||
$sql = $this->replacePlaceholders($sql);
|
||||
$parameters = $this->parameterValues($parameters);
|
||||
}
|
||||
|
||||
// Execute query
|
||||
// Unfortunately error-suppression is required in order to handle sql errors elegantly.
|
||||
// Please use PDO if you can help it
|
||||
if (!empty($parameters)) {
|
||||
$result = @pg_query_params($this->dbConn, $sql, $parameters);
|
||||
} else {
|
||||
$result = @pg_query($this->dbConn, $sql);
|
||||
}
|
||||
|
||||
// Handle error
|
||||
if (!$result) {
|
||||
$this->databaseError($this->getLastError(), $errorLevel, $sql, $parameters);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Save and return results
|
||||
$this->lastQuery = $result;
|
||||
$this->lastRows = pg_affected_rows($result);
|
||||
return new PostgreSQLQuery($result);
|
||||
}
|
||||
|
||||
public function query($sql, $errorLevel = E_USER_ERROR)
|
||||
{
|
||||
return $this->preparedQuery($sql, array(), $errorLevel);
|
||||
}
|
||||
|
||||
public function quoteString($value)
|
||||
{
|
||||
if (function_exists('pg_escape_literal')) {
|
||||
return pg_escape_literal($this->dbConn, $value);
|
||||
} else {
|
||||
return "'" . $this->escapeString($value) . "'";
|
||||
}
|
||||
}
|
||||
|
||||
public function escapeString($value)
|
||||
{
|
||||
return pg_escape_string($this->dbConn, $value);
|
||||
}
|
||||
|
||||
public function selectDatabase($name)
|
||||
{
|
||||
if ($name !== $this->databaseName) {
|
||||
user_error("PostgreSQLConnector can't change databases. Please create a new database connection", E_USER_ERROR);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function unloadDatabase()
|
||||
{
|
||||
$this->databaseName = null;
|
||||
}
|
||||
}
|
|
@ -1,2126 +1,825 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage model
|
||||
*/
|
||||
namespace SilverStripe\PostgreSQL;
|
||||
|
||||
use SilverStripe\Core\Config\Configurable;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use SilverStripe\ORM\Connect\Database;
|
||||
use SilverStripe\ORM\PaginatedList;
|
||||
use ErrorException;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* PostgreSQL connector class.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage model
|
||||
*/
|
||||
class PostgreSQLDatabase extends SS_Database {
|
||||
|
||||
/**
|
||||
* Connection to the DBMS.
|
||||
* @var resource
|
||||
*/
|
||||
private $dbConn;
|
||||
|
||||
/**
|
||||
* True if we are connected to a database.
|
||||
* @var boolean
|
||||
*/
|
||||
private $active;
|
||||
|
||||
/**
|
||||
* The name of the database.
|
||||
* @var string
|
||||
*/
|
||||
private $database;
|
||||
|
||||
/*
|
||||
* This holds the name of the original database
|
||||
* So if you switch to another for unit tests, you
|
||||
* can then switch back in order to drop the temp database
|
||||
*/
|
||||
private $database_original;
|
||||
|
||||
/**
|
||||
* The database schema name.
|
||||
* @var string
|
||||
*/
|
||||
private $schema;
|
||||
|
||||
/*
|
||||
* This holds the parameters that the original connection was created with,
|
||||
* so we can switch back to it if necessary (used for unit tests)
|
||||
*/
|
||||
private $parameters;
|
||||
|
||||
/*
|
||||
* These two values describe how T-search will work.
|
||||
* You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin)
|
||||
* Combinations of these two will also work, so you'll need to pick
|
||||
* one which works best for you
|
||||
*/
|
||||
public $default_fts_cluster_method='GIN';
|
||||
public $default_fts_search_method='@@@';
|
||||
|
||||
private $supportsTransactions=true;
|
||||
|
||||
/**
|
||||
* Determines whether to check a database exists on the host by
|
||||
* querying the 'postgres' database and running createDatabase.
|
||||
*
|
||||
* Some locked down systems prevent access to the 'postgres' table in
|
||||
* which case you need to set this to false.
|
||||
*/
|
||||
public static $check_database_exists = true;
|
||||
|
||||
/**
|
||||
* This holds a copy of all the constraint results that are returned
|
||||
* via the function constraintExists(). This is a bit faster than
|
||||
* repeatedly querying this column, and should allow the database
|
||||
* to use it's built-in caching features for better queries.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $cached_constraints=array();
|
||||
|
||||
/**
|
||||
*
|
||||
* This holds a copy of all the queries that run through the function orderMoreSpecifically()
|
||||
* It appears to be a performance bottleneck at times.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $cached_ordered_specifically=array();
|
||||
|
||||
/**
|
||||
*
|
||||
* This holds a copy of all the queries that run through the function fieldList()
|
||||
* This is one of the most-often called functions, and repeats itself a great deal in the unit tests.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $cached_fieldlists=array();
|
||||
|
||||
/**
|
||||
* Override the language that tsearch uses. By default it is 'english, but
|
||||
* could be any of the supported languages that can be found in the
|
||||
* pg_catalog.pg_ts_config table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $search_language='english';
|
||||
|
||||
/**
|
||||
* Connect to a PostgreSQL database.
|
||||
* @param array $parameters An map of parameters, which should include:
|
||||
* - server: The server, eg, localhost
|
||||
* - username: The username to log on with
|
||||
* - password: The password to log on with
|
||||
* - database: The database to connect to
|
||||
*/
|
||||
public function __construct($parameters) {
|
||||
|
||||
//We will store these connection parameters for use elsewhere (ie, unit tests)
|
||||
$this->parameters=$parameters;
|
||||
$this->connectDatabase();
|
||||
|
||||
$this->database_original=$this->database;
|
||||
}
|
||||
|
||||
/*
|
||||
* Uses whatever connection details are in the $parameters array to connect to a database of a given name
|
||||
*/
|
||||
function connectDatabase(){
|
||||
|
||||
$parameters=$this->parameters;
|
||||
|
||||
if(!$parameters)
|
||||
return false;
|
||||
|
||||
($parameters['username']!='') ? $username=' user=' . $parameters['username'] : $username='';
|
||||
($parameters['password']!='') ? $password=' password=' . $parameters['password'] : $password='';
|
||||
|
||||
if(!isset($this->database))
|
||||
$dbName=$parameters['database'];
|
||||
else $dbName=$this->database;
|
||||
|
||||
$port = empty($parameters['port']) ? 5432 : $parameters['port'];
|
||||
|
||||
// First, we need to check that this database exists. To do this, we will connect to the 'postgres' database first
|
||||
// some setups prevent access to this database so set PostgreSQLDatabase::$check_database_exists = false
|
||||
if(self::$check_database_exists) {
|
||||
$this->dbConn = pg_connect('host=' . $parameters['server'] . ' port=' . $port . ' dbname=postgres' . $username . $password);
|
||||
|
||||
if(!$this->databaseExists($dbName))
|
||||
$this->createDatabase($dbName);
|
||||
}
|
||||
|
||||
//Now we can be sure that this database exists, so we can connect to it
|
||||
$this->dbConn = pg_connect('host=' . $parameters['server'] . ' port=' . $port . ' dbname=' . $dbName . $username . $password);
|
||||
|
||||
//By virtue of getting here, the connection is active:
|
||||
$this->active=true;
|
||||
$this->database = $dbName;
|
||||
|
||||
if(!$this->dbConn) {
|
||||
$this->databaseError("Couldn't connect to PostgreSQL database");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set up the schema if required
|
||||
$schema = isset($parameters['schema']) ? $parameters['schema'] : $this->currentSchema();
|
||||
// Edge-case - database with no schemas:
|
||||
if(!$schema) $schema = "public";
|
||||
|
||||
if(!$this->schemaExists($schema))
|
||||
$this->createSchema($schema);
|
||||
|
||||
$this->setSchema($schema);
|
||||
|
||||
// Set the timezone if required.
|
||||
if(isset($parameters['timezone'])) $this->query(sprintf("SET SESSION TIME ZONE '%s'", $parameters['timezone']));
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Not implemented, needed for PDO
|
||||
*/
|
||||
public function getConnect($parameters) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parameters used to construct this database connection
|
||||
*/
|
||||
public function getParameters() {
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this database supports collations
|
||||
* TODO: get rid of this?
|
||||
* @return boolean
|
||||
*/
|
||||
public function supportsCollations() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of PostgreSQL.
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion() {
|
||||
$version = pg_version($this->dbConn);
|
||||
if(isset($version['server'])) return $version['server'];
|
||||
else return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database server, namely PostgreSQL.
|
||||
* @return string
|
||||
*/
|
||||
public function getDatabaseServer() {
|
||||
return "postgresql";
|
||||
}
|
||||
|
||||
public function query($sql, $errorLevel = E_USER_ERROR) {
|
||||
|
||||
if(isset($_REQUEST['previewwrite']) && in_array(strtolower(substr($sql,0,strpos($sql,' '))), array('insert','update','delete','replace'))) {
|
||||
Debug::message("Will execute: $sql");
|
||||
return;
|
||||
}
|
||||
|
||||
if(isset($_REQUEST['showqueries'])) {
|
||||
$starttime = microtime(true);
|
||||
}
|
||||
|
||||
$handle = pg_query($this->dbConn, $sql);
|
||||
|
||||
if(isset($_REQUEST['showqueries'])) {
|
||||
$endtime = round((microtime(true) - $starttime) * 1000, 1);
|
||||
Debug::message("\n$sql\n{$endtime}ms\n", false);
|
||||
}
|
||||
|
||||
DB::$lastQuery=$handle;
|
||||
|
||||
if(!$handle && $errorLevel) $this->databaseError("Couldn't run query: $sql | " . pg_last_error($this->dbConn), $errorLevel);
|
||||
|
||||
return new PostgreSQLQuery($this, $handle);
|
||||
}
|
||||
|
||||
public function getGeneratedID($table) {
|
||||
$result=DB::query("SELECT last_value FROM \"{$table}_ID_seq\";");
|
||||
$row=$result->first();
|
||||
return $row['last_value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* OBSOLETE: Get the ID for the next new record for the table.
|
||||
*
|
||||
* @var string $table The name od the table.
|
||||
* @return int
|
||||
*/
|
||||
public function getNextID($table) {
|
||||
user_error('getNextID is OBSOLETE (and will no longer work properly)', E_USER_WARNING);
|
||||
$result = $this->query("SELECT MAX(ID)+1 FROM \"$table\"")->value();
|
||||
return $result ? $result : 1;
|
||||
}
|
||||
|
||||
public function isActive() {
|
||||
return $this->active ? true : false;
|
||||
}
|
||||
|
||||
/*
|
||||
* You can create a database based either on a supplied name, or from whatever is in the $this->database value
|
||||
*/
|
||||
public function createDatabase($name=false) {
|
||||
if(!$name)
|
||||
$name=$this->database;
|
||||
|
||||
$this->query("CREATE DATABASE \"$name\";");
|
||||
|
||||
$this->connectDatabase();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the database that this object is currently connected to.
|
||||
* Use with caution.
|
||||
*/
|
||||
public function dropDatabase() {
|
||||
|
||||
//First, we need to switch back to the original database so we can drop the current one
|
||||
$db_to_drop=$this->database;
|
||||
$this->selectDatabase($this->database_original);
|
||||
$this->connectDatabase();
|
||||
|
||||
$this->query("DROP DATABASE \"$db_to_drop\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the database that this object is currently connected to.
|
||||
* Use with caution.
|
||||
*/
|
||||
public function dropDatabaseByName($dbName) {
|
||||
if($dbName!=$this->database)
|
||||
$this->query("DROP DATABASE \"$dbName\";");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the currently selected database
|
||||
*/
|
||||
public function currentDatabase() {
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to the given database.
|
||||
* If the database doesn't exist, you should call createDatabase() after calling selectDatabase()
|
||||
*/
|
||||
public function selectDatabase($dbname) {
|
||||
$this->database=$dbname;
|
||||
|
||||
$this->tableList = $this->fieldList = $this->indexList = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the named database exists.
|
||||
*/
|
||||
public function databaseExists($name) {
|
||||
// We have to use addslashes here, since there may not be a database connection to base the Convert::raw2sql
|
||||
// function off.
|
||||
$SQL_name=addslashes($name);
|
||||
return $this->query("SELECT datname FROM pg_database WHERE datname='$SQL_name';")->first() ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a column
|
||||
*/
|
||||
public function allDatabaseNames() {
|
||||
return $this->query("SELECT datname FROM pg_database WHERE datistemplate=false;")->column();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the schema exists in the current database
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function schemaExists($name) {
|
||||
$SQL_name = pg_escape_string($this->dbConn, $name);
|
||||
return $this->query("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname = '{$SQL_name}';")->first() ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a schema in the current database
|
||||
* @param string $name
|
||||
*/
|
||||
public function createSchema($name) {
|
||||
$SQL_name = pg_escape_string($this->dbConn, $name);
|
||||
$this->query("CREATE SCHEMA \"{$SQL_name}\";");
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a schema from the database. Use carefully!
|
||||
* @param string $name
|
||||
*/
|
||||
public function dropSchema($name) {
|
||||
$SQL_name = pg_escape_string($this->dbConn, $name);
|
||||
$this->query("DROP SCHEMA \"{$SQL_name}\" CASCADE;");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the current schema in use
|
||||
*/
|
||||
public function currentSchema() {
|
||||
return $this->query('SELECT current_schema()')->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to manually set the schema to an alternative
|
||||
* Check existance & sets search path to the supplied schema name
|
||||
* @param string $schema
|
||||
*/
|
||||
public function setSchema($schema) {
|
||||
if(!$this->schemaExists($schema))
|
||||
$this->databaseError("Schema $schema does not exist");
|
||||
$this->setSchemaSearchPath($schema);
|
||||
$this->schema = $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the schema search path. Search using the arguments supplied.
|
||||
* NOTE: The search path is normally set through setSchema() and only
|
||||
* one schema is selected. The facility to add more than one schema to
|
||||
* the search path is provided as an advanced PostgreSQL feature for raw
|
||||
* SQL queries. Sapphire cannot search for datamodel tables in alternate
|
||||
* schemas, so be wary of using alternate schemas within the ORM environment.
|
||||
* @param string $arg1 First schema to use
|
||||
* @param string $arg2 Second schema to use
|
||||
* @param string $argN Nth schema to use
|
||||
*/
|
||||
public function setSchemaSearchPath() {
|
||||
if(func_num_args() == 0)
|
||||
$this->databaseError('At least one Schema must be supplied to set a search path.');
|
||||
$args = array_values(func_get_args());
|
||||
foreach($args as $key => $schema)
|
||||
$args[$key] = '"' . pg_escape_string($this->dbConn, $schema) . '"';
|
||||
$args_SQL =implode(",", $args);
|
||||
$this->query("SET search_path TO {$args_SQL}");
|
||||
}
|
||||
|
||||
public function createTable($tableName, $fields = null, $indexes = null, $options = null, $extensions = null) {
|
||||
|
||||
$fieldSchemas = $indexSchemas = "";
|
||||
if($fields) foreach($fields as $k => $v) $fieldSchemas .= "\"$k\" $v,\n";
|
||||
if(isset($this->class)){
|
||||
$addOptions = (isset($options[$this->class])) ? $options[$this->class] : null;
|
||||
} else $addOptions=null;
|
||||
|
||||
//First of all, does this table already exist
|
||||
$doesExist=$this->TableExists($tableName);
|
||||
if($doesExist) {
|
||||
// Table already exists, just return the name, in line with baseclass documentation.
|
||||
return $tableName;
|
||||
}
|
||||
|
||||
//If we have a fulltext search request, then we need to create a special column
|
||||
//for GiST searches
|
||||
$fulltexts='';
|
||||
$triggers='';
|
||||
if($indexes){
|
||||
foreach($indexes as $name=>$this_index){
|
||||
if($this_index['type']=='fulltext'){
|
||||
$ts_details=$this->fulltext($this_index, $tableName, $name);
|
||||
$fulltexts.=$ts_details['fulltexts'] . ', ';
|
||||
$triggers.=$ts_details['triggers'];
|
||||
}
|
||||
}
|
||||
}
|
||||
if($indexes) foreach($indexes as $k => $v) $indexSchemas .= $this->getIndexSqlDefinition($tableName, $k, $v) . "\n";
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if($extensions && isset($extensions['tablespace'])){
|
||||
|
||||
$this->createOrReplaceTablespace($extensions['tablespace']['name'], $extensions['tablespace']['location']);
|
||||
$tableSpace=' TABLESPACE ' . $extensions['tablespace']['name'];
|
||||
} else
|
||||
$tableSpace='';
|
||||
|
||||
$this->query("CREATE TABLE \"$tableName\" (
|
||||
$fieldSchemas
|
||||
$fulltexts
|
||||
primary key (\"ID\")
|
||||
)$tableSpace; $indexSchemas $addOptions");
|
||||
|
||||
if($triggers!=''){
|
||||
$this->query($triggers);
|
||||
}
|
||||
|
||||
//If we have a partitioning requirement, we do that here:
|
||||
if($extensions && isset($extensions['partitions'])){
|
||||
$this->createOrReplacePartition($tableName, $extensions['partitions'], $indexes, $extensions);
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if($extensions && isset($extensions['cluster'])){
|
||||
DB::query("CLUSTER \"$tableName\" USING \"{$extensions['cluster']}\";");
|
||||
}
|
||||
|
||||
return $tableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter a table's schema.
|
||||
* @param $table The name of the table to alter
|
||||
* @param $newFields New fields, a map of field name => field schema
|
||||
* @param $newIndexes New indexes, a map of index name => index type
|
||||
* @param $alteredFields Updated fields, a map of field name => field schema
|
||||
* @param $alteredIndexes Updated indexes, a map of index name => index type
|
||||
*/
|
||||
public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null, $alteredOptions = null, $advancedOptions = null) {
|
||||
|
||||
$fieldSchemas = $indexSchemas = "";
|
||||
$alterList = array();
|
||||
if($newFields) foreach($newFields as $k => $v) $alterList[] .= "ADD \"$k\" $v";
|
||||
|
||||
if($alteredFields) {
|
||||
foreach($alteredFields as $k => $v) {
|
||||
|
||||
$val=$this->alterTableAlterColumn($tableName, $k, $v);
|
||||
if($val!='')
|
||||
$alterList[] .= $val;
|
||||
}
|
||||
}
|
||||
|
||||
//Do we need to do anything with the tablespaces?
|
||||
if($alteredOptions && isset($advancedOptions['tablespace'])){
|
||||
$this->createOrReplaceTablespace($advancedOptions['tablespace']['name'], $advancedOptions['tablespace']['location']);
|
||||
$this->query("ALTER TABLE \"$tableName\" SET TABLESPACE {$advancedOptions['tablespace']['name']};");
|
||||
}
|
||||
|
||||
//DB ABSTRACTION: we need to change the constraints to be a separate 'add' command,
|
||||
//see http://www.postgresql.org/docs/8.1/static/sql-altertable.html
|
||||
$alterIndexList=Array();
|
||||
//Pick up the altered indexes here:
|
||||
$fieldList = $this->fieldList($tableName);
|
||||
$fulltexts=false;
|
||||
$drop_triggers=false;
|
||||
$triggers=false;
|
||||
if($alteredIndexes) foreach($alteredIndexes as $key=>$v) {
|
||||
//We are only going to delete indexes which exist
|
||||
$indexes=$this->indexList($tableName);
|
||||
|
||||
if($v['type']=='fulltext'){
|
||||
//For full text indexes, we need to drop the trigger, drop the index, AND drop the column
|
||||
|
||||
//Go and get the tsearch details:
|
||||
$ts_details=$this->fulltext($v, $tableName, $key);
|
||||
|
||||
//Drop this column if it already exists:
|
||||
|
||||
//No IF EXISTS option is available for Postgres <9.0
|
||||
if(array_key_exists($ts_details['ts_name'], $fieldList)){
|
||||
$fulltexts.="ALTER TABLE \"{$tableName}\" DROP COLUMN \"{$ts_details['ts_name']}\";";
|
||||
}
|
||||
|
||||
$drop_triggers.= 'DROP TRIGGER IF EXISTS ts_' . strtolower($tableName) . '_' . strtolower($key) . ' ON "' . $tableName . '";';
|
||||
$alterIndexList[] = 'DROP INDEX IF EXISTS ix_' . strtolower($tableName) . '_' . strtolower($v['value']) . ';';
|
||||
|
||||
//We'll execute these later:
|
||||
$fulltexts.="ALTER TABLE \"{$tableName}\" ADD COLUMN {$ts_details['fulltexts']};";
|
||||
$triggers.=$ts_details['triggers'];
|
||||
} else {
|
||||
if(isset($indexes[$v['value']])){
|
||||
if(is_array($v))
|
||||
$alterIndexList[] = 'DROP INDEX IF EXISTS ix_' . strtolower($tableName) . '_' . strtolower($v['value']) . ';';
|
||||
else
|
||||
$alterIndexList[] = 'DROP INDEX IF EXISTS ix_' . strtolower($tableName) . '_' . strtolower(trim($v, '()')) . ';';
|
||||
|
||||
$k=$v['value'];
|
||||
$createIndex=$this->getIndexSqlDefinition($tableName, $k, $v);
|
||||
if($createIndex!==false)
|
||||
$alterIndexList[] .= $createIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If we have a fulltext search request, then we need to create a special column
|
||||
//for GiST searches
|
||||
//Pick up the new indexes here:
|
||||
if($newIndexes){
|
||||
foreach($newIndexes as $name=>$this_index){
|
||||
if($this_index['type']=='fulltext'){
|
||||
$ts_details=$this->fulltext($this_index, $tableName, $name);
|
||||
if(!isset($fieldList[$ts_details['ts_name']])){
|
||||
$fulltexts.="ALTER TABLE \"{$tableName}\" ADD COLUMN {$ts_details['fulltexts']};";
|
||||
$triggers.=$ts_details['triggers'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Add the new indexes:
|
||||
if($newIndexes) foreach($newIndexes as $k=>$v){
|
||||
//Check that this index doesn't already exist:
|
||||
$indexes=$this->indexList($tableName);
|
||||
if(!is_array($v)){
|
||||
$name=trim($v, '()');
|
||||
} else {
|
||||
$name=(isset($v['name'])) ? $v['name'] : $k;
|
||||
}
|
||||
if(isset($indexes[$name])){
|
||||
if(is_array($v)){
|
||||
$alterIndexList[] = 'DROP INDEX IF EXISTS ix_' . strtolower($tableName) . '_' . strtolower($v['value']) . ';';
|
||||
} else {
|
||||
$alterIndexList[] = 'DROP INDEX IF EXISTS ' . $indexes[$name]['indexname'] . ';';
|
||||
}
|
||||
}
|
||||
|
||||
$createIndex=$this->getIndexSqlDefinition($tableName, $k, $v);
|
||||
if($createIndex!==false)
|
||||
$alterIndexList[] = $createIndex;
|
||||
}
|
||||
|
||||
if($alterList) {
|
||||
$alterations = implode(",\n", $alterList);
|
||||
$this->query("ALTER TABLE \"$tableName\" " . $alterations);
|
||||
}
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if($advancedOptions && isset($advancedOptions['extensions']['tablespace'])){
|
||||
$extensions=$advancedOptions['extensions'];
|
||||
$this->createOrReplaceTablespace($extensions['tablespace']['name'], $extensions['tablespace']['location']);
|
||||
}
|
||||
|
||||
if($alteredOptions && isset($this->class) && isset($alteredOptions[$this->class])) {
|
||||
$this->query(sprintf("ALTER TABLE \"%s\" %s", $tableName, $alteredOptions[$this->class]));
|
||||
Database::alteration_message(
|
||||
sprintf("Table %s options changed: %s", $tableName, $alteredOptions[$this->class]),
|
||||
"changed"
|
||||
);
|
||||
}
|
||||
|
||||
//Create any fulltext columns and triggers here:
|
||||
if($fulltexts)
|
||||
$this->query($fulltexts);
|
||||
if($drop_triggers)
|
||||
$this->query($drop_triggers);
|
||||
|
||||
if($triggers) {
|
||||
$this->query($triggers);
|
||||
|
||||
$triggerbits=explode(';', $triggers);
|
||||
foreach($triggerbits as $trigger){
|
||||
$trigger_fields=$this->triggerFieldsFromTrigger($trigger);
|
||||
|
||||
if($trigger_fields){
|
||||
//We need to run a simple query to force the database to update the triggered columns
|
||||
$this->query("UPDATE \"{$tableName}\" SET \"{$trigger_fields[0]}\"=\"$trigger_fields[0]\";");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($alterIndexList as $alteration)
|
||||
$this->query($alteration);
|
||||
|
||||
//If we have a partitioning requirement, we do that here:
|
||||
if($advancedOptions && isset($advancedOptions['partitions'])){
|
||||
$this->createOrReplacePartition($tableName, $advancedOptions['partitions']);
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if($advancedOptions && isset($advancedOptions['cluster'])){
|
||||
DB::query("CLUSTER \"$tableName\" USING ix_{$tableName}_{$advancedOptions['cluster']};");
|
||||
} else {
|
||||
//Check that clustering is not on this table, and if it is, remove it:
|
||||
|
||||
//This is really annoying. We need the oid of this table:
|
||||
$stats=DB::query("SELECT relid FROM pg_stat_user_tables WHERE relname='$tableName';")->first();
|
||||
$oid=$stats['relid'];
|
||||
|
||||
//Now we can run a long query to get the clustered status:
|
||||
//If anyone knows a better way to get the clustered status, then feel free to replace this!
|
||||
$clustered=DB::query("SELECT c2.relname, i.indisclustered FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i WHERE c.oid = '$oid' AND c.oid = i.indrelid AND i.indexrelid = c2.oid AND indisclustered='t';")->first();
|
||||
|
||||
if($clustered)
|
||||
DB::query("ALTER TABLE \"$tableName\" SET WITHOUT CLUSTER;");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates an ALTER expression for a column in PostgreSQL
|
||||
*
|
||||
* @param $tableName Name of the table to be altered
|
||||
* @param $colName Name of the column to be altered
|
||||
* @param $colSpec String which contains conditions for a column
|
||||
* @return string
|
||||
*/
|
||||
private function alterTableAlterColumn($tableName, $colName, $colSpec){
|
||||
// First, we split the column specifications into parts
|
||||
// TODO: this returns an empty array for the following string: int(11) not null auto_increment
|
||||
// on second thoughts, why is an auto_increment field being passed through?
|
||||
|
||||
$pattern = '/^([\w()]+)\s?((?:not\s)?null)?\s?(default\s[\w\']+)?\s?(check\s[\w()\'",\s]+)?$/i';
|
||||
preg_match($pattern, $colSpec, $matches);
|
||||
|
||||
if(sizeof($matches)==0)
|
||||
return '';
|
||||
|
||||
if($matches[1]=='serial8')
|
||||
return '';
|
||||
|
||||
if(isset($matches[1])) {
|
||||
$alterCol = "ALTER COLUMN \"$colName\" TYPE $matches[1]\n";
|
||||
|
||||
// SET null / not null
|
||||
if(!empty($matches[2])) $alterCol .= ",\nALTER COLUMN \"$colName\" SET $matches[2]";
|
||||
|
||||
// SET default (we drop it first, for reasons of precaution)
|
||||
if(!empty($matches[3])) {
|
||||
$alterCol .= ",\nALTER COLUMN \"$colName\" DROP DEFAULT";
|
||||
$alterCol .= ",\nALTER COLUMN \"$colName\" SET $matches[3]";
|
||||
}
|
||||
|
||||
// SET check constraint (The constraint HAS to be dropped)
|
||||
$existing_constraint=$this->query("SELECT conname FROM pg_constraint WHERE conname='{$tableName}_{$colName}_check';")->value();
|
||||
if(isset($matches[4])) {
|
||||
//Take this new constraint and see what's outstanding from the target table:
|
||||
$constraint_bits=explode('(', $matches[4]);
|
||||
$constraint_values=trim($constraint_bits[2], ')');
|
||||
$constraint_values_bits=explode(',', $constraint_values);
|
||||
$default=trim($constraint_values_bits[0], " '");
|
||||
|
||||
//Now go and convert anything that's not in this list to 'Page'
|
||||
//We have to run this as a query, not as part of the alteration queries due to the way they are constructed.
|
||||
$updateConstraint='';
|
||||
$updateConstraint.="UPDATE \"{$tableName}\" SET \"$colName\"='$default' WHERE \"$colName\" NOT IN ($constraint_values);";
|
||||
if($this->hasTable("{$tableName}_Live"))
|
||||
$updateConstraint.="UPDATE \"{$tableName}_Live\" SET \"$colName\"='$default' WHERE \"$colName\" NOT IN ($constraint_values);";
|
||||
if($this->hasTable("{$tableName}_versions"))
|
||||
$updateConstraint.="UPDATE \"{$tableName}_versions\" SET \"$colName\"='$default' WHERE \"$colName\" NOT IN ($constraint_values);";
|
||||
|
||||
DB::query($updateConstraint);
|
||||
}
|
||||
|
||||
//First, delete any existing constraint on this column, even if it's no longer an enum
|
||||
if($existing_constraint)
|
||||
$alterCol .= ",\nDROP CONSTRAINT \"{$tableName}_{$colName}_check\"";
|
||||
|
||||
//Now create the constraint (if we've asked for one)
|
||||
if(!empty($matches[4]))
|
||||
$alterCol .= ",\nADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]";
|
||||
}
|
||||
|
||||
return isset($alterCol) ? $alterCol : '';
|
||||
}
|
||||
|
||||
public function renameTable($oldTableName, $newTableName) {
|
||||
$this->query("ALTER TABLE \"$oldTableName\" RENAME TO \"$newTableName\"");
|
||||
unset(self::$cached_fieldlists[$oldTableName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repairs and reindexes the table. This might take a long time on a very large table.
|
||||
* @var string $tableName The name of the table.
|
||||
* @return boolean Return true if the table has integrity after the method is complete.
|
||||
*/
|
||||
public function checkAndRepairTable($tableName) {
|
||||
|
||||
$this->runTableCheckCommand("VACUUM FULL ANALYZE \"$tableName\"");
|
||||
$this->runTableCheckCommand("REINDEX TABLE \"$tableName\"");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used by checkAndRepairTable.
|
||||
* @param string $sql Query to run.
|
||||
* @return boolean Returns true no matter what; we're not currently checking the status of the command
|
||||
*/
|
||||
protected function runTableCheckCommand($sql) {
|
||||
$testResults = $this->query($sql);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createField($tableName, $fieldName, $fieldSpec) {
|
||||
$this->query("ALTER TABLE \"$tableName\" ADD \"$fieldName\" $fieldSpec");
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the database type of the given field.
|
||||
* @param string $tableName The name of the tbale the field is in.
|
||||
* @param string $fieldName The name of the field to change.
|
||||
* @param string $fieldSpec The new field specification
|
||||
*/
|
||||
public function alterField($tableName, $fieldName, $fieldSpec) {
|
||||
$this->query("ALTER TABLE \"$tableName\" CHANGE \"$fieldName\" \"$fieldName\" $fieldSpec");
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the database column name of the given field.
|
||||
*
|
||||
* @param string $tableName The name of the table the field is in.
|
||||
* @param string $oldName The name of the field to change.
|
||||
* @param string $newName The new name of the field
|
||||
*/
|
||||
public function renameField($tableName, $oldName, $newName) {
|
||||
$fieldList = $this->fieldList($tableName);
|
||||
if(array_key_exists($oldName, $fieldList)) {
|
||||
$this->query("ALTER TABLE \"$tableName\" RENAME COLUMN \"$oldName\" TO \"$newName\"");
|
||||
|
||||
//Remove this from the cached list:
|
||||
unset(self::$cached_fieldlists[$tableName]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function fieldList($table) {
|
||||
//Query from http://www.alberton.info/postgresql_meta_info.html
|
||||
//This gets us more information than we need, but I've included it all for the moment....
|
||||
|
||||
//if(!isset(self::$cached_fieldlists[$table])){
|
||||
$fields = $this->query("SELECT ordinal_position, column_name, data_type, column_default, is_nullable, character_maximum_length, numeric_precision, numeric_scale FROM information_schema.columns WHERE table_name = '$table' ORDER BY ordinal_position;");
|
||||
|
||||
$output = array();
|
||||
if($fields) foreach($fields as $field) {
|
||||
|
||||
switch($field['data_type']){
|
||||
case 'character varying':
|
||||
//Check to see if there's a constraint attached to this column:
|
||||
//$constraint=$this->query("SELECT conname,pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE r.contype = 'c' AND conname='" . $table . '_' . $field['column_name'] . "_check' ORDER BY 1;")->first();
|
||||
$constraint=$this->constraintExists($table . '_' . $field['column_name'] . '_check');
|
||||
$enum='';
|
||||
if($constraint){
|
||||
//Now we need to break this constraint text into bits so we can see what we have:
|
||||
//Examples:
|
||||
//CHECK ("CanEditType"::text = ANY (ARRAY['LoggedInUsers'::character varying, 'OnlyTheseUsers'::character varying, 'Inherit'::character varying]::text[]))
|
||||
//CHECK ("ClassName"::text = 'PageComment'::text)
|
||||
|
||||
//TODO: replace all this with a regular expression!
|
||||
$value=$constraint['pg_get_constraintdef'];
|
||||
$value=substr($value, strpos($value,'='));
|
||||
$value=str_replace("''", "'", $value);
|
||||
|
||||
$in_value=false;
|
||||
$constraints=Array();
|
||||
$current_value='';
|
||||
for($i=0; $i<strlen($value); $i++){
|
||||
$char=substr($value, $i, 1);
|
||||
if($in_value)
|
||||
$current_value.=$char;
|
||||
|
||||
if($char=="'"){
|
||||
if(!$in_value)
|
||||
$in_value=true;
|
||||
else {
|
||||
$in_value=false;
|
||||
$constraints[]=substr($current_value, 0, -1);
|
||||
$current_value='';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(sizeof($constraints)>0){
|
||||
//Get the default:
|
||||
$default=trim(substr($field['column_default'], 0, strpos($field['column_default'], '::')), "'");
|
||||
$output[$field['column_name']]=$this->enum(Array('default'=>$default, 'name'=>$field['column_name'], 'enums'=>$constraints));
|
||||
}
|
||||
} else{
|
||||
$output[$field['column_name']]='varchar(' . $field['character_maximum_length'] . ')';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'numeric':
|
||||
$output[$field['column_name']]='decimal(' . $field['numeric_precision'] . ',' . $field['numeric_scale'] . ') default ' . (int)$field['column_default'];
|
||||
break;
|
||||
|
||||
case 'integer':
|
||||
$output[$field['column_name']]='integer default ' . (int)$field['column_default'];
|
||||
break;
|
||||
|
||||
case 'timestamp without time zone':
|
||||
$output[$field['column_name']]='timestamp';
|
||||
break;
|
||||
|
||||
case 'smallint':
|
||||
$output[$field['column_name']]='smallint default ' . (int)$field['column_default'];
|
||||
break;
|
||||
|
||||
case 'time without time zone':
|
||||
$output[$field['column_name']]='time';
|
||||
break;
|
||||
|
||||
case 'double precision':
|
||||
$output[$field['column_name']]='float';
|
||||
break;
|
||||
|
||||
default:
|
||||
$output[$field['column_name']] = $field;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// self::$cached_fieldlists[$table]=$output;
|
||||
//}
|
||||
|
||||
//return self::$cached_fieldlists[$table];
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This allows the cached values for a table's field list to be erased.
|
||||
* If $tablename is empty, then the whole cache is erased.
|
||||
*
|
||||
* @param string $tableName
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function clearCachedFieldlist($tableName=false){
|
||||
if($tableName!=false){
|
||||
unset(self::$cached_fieldlists[$tableName]);
|
||||
} else
|
||||
self::$cached_fieldlists=array();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an index on a table.
|
||||
* @param string $tableName The name of the table.
|
||||
* @param string $indexName The name of the index.
|
||||
* @param string $indexSpec The specification of the index, see Database::requireIndex() for more details.
|
||||
*/
|
||||
public function createIndex($tableName, $indexName, $indexSpec) {
|
||||
$createIndex=$this->getIndexSqlDefinition($tableName, $indexName, $indexSpec);
|
||||
if($createIndex!==false)
|
||||
$this->query();
|
||||
}
|
||||
|
||||
/*
|
||||
* This takes the index spec which has been provided by a class (ie static $indexes = blah blah)
|
||||
* and turns it into a proper string.
|
||||
* Some indexes may be arrays, such as fulltext and unique indexes, and this allows database-specific
|
||||
* arrays to be created.
|
||||
*/
|
||||
public function convertIndexSpec($indexSpec, $asDbValue=false, $table=''){
|
||||
|
||||
if(!$asDbValue){
|
||||
if(is_array($indexSpec)){
|
||||
//Here we create a db-specific version of whatever index we need to create.
|
||||
switch($indexSpec['type']){
|
||||
case 'fulltext':
|
||||
//We need to include the fields so if we change the columns it's indexing, but not the name,
|
||||
//then the change will be picked up.
|
||||
$indexSpec='(' . $indexSpec['name'] . ',' . $indexSpec['value'] . ')';
|
||||
break;
|
||||
case 'unique':
|
||||
$indexSpec='unique (' . $indexSpec['value'] . ')';
|
||||
break;
|
||||
case 'hash':
|
||||
$indexSpec='using hash (' . $indexSpec['value'] . ')';
|
||||
break;
|
||||
case 'index':
|
||||
//The default index is 'btree', which we'll use by default (below):
|
||||
default:
|
||||
$indexSpec='using btree (' . $indexSpec['value'] . ')';
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$indexSpec='ix_' . $table . '_' . $indexSpec;
|
||||
}
|
||||
return $indexSpec;
|
||||
}
|
||||
|
||||
protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec, $asDbValue=false) {
|
||||
|
||||
//TODO: create table partition support
|
||||
//TODO: create clustering options
|
||||
|
||||
//NOTE: it is possible for *_renamed tables to have indexes whose names are not updates
|
||||
//Therefore, we now check for the existance of indexes before we create them.
|
||||
//This is techically a bug, since new tables will not be indexed.
|
||||
|
||||
if(!$asDbValue){
|
||||
|
||||
$tableCol= 'ix_' . $tableName . '_' . $indexName;
|
||||
if(strlen($tableCol)>64){
|
||||
$tableCol=substr($indexName, 0, 59) . rand(1000, 9999);
|
||||
}
|
||||
|
||||
//It is possible to specify indexes through strings:
|
||||
if(!is_array($indexSpec)){
|
||||
$indexSpec=trim($indexSpec, '()');
|
||||
$bits=explode(',', $indexSpec);
|
||||
$indexes="\"" . implode("\",\"", $bits) . "\"";
|
||||
|
||||
//One last check:
|
||||
$existing=DB::query("SELECT tablename FROM pg_indexes WHERE indexname='" . strtolower($tableCol) . "';")->first();
|
||||
if(!$existing)
|
||||
return "create index $tableCol ON \"" . $tableName . "\" (" . $indexes . ");";
|
||||
else
|
||||
return false;
|
||||
} else {
|
||||
|
||||
//Arrays offer much more flexibility and many more options:
|
||||
|
||||
//Misc options first:
|
||||
$fillfactor=$where='';
|
||||
if(isset($indexSpec['fillfactor']))
|
||||
$fillfactor='WITH (FILLFACTOR = ' . $indexSpec['fillfactor'] . ')';
|
||||
if(isset($indexSpec['where']))
|
||||
$where='WHERE ' . $indexSpec['where'];
|
||||
|
||||
//Fix up the value entry to be quoted:
|
||||
$value_bits=explode(',', $indexSpec['value']);
|
||||
$new_values=Array();
|
||||
foreach($value_bits as $value){
|
||||
$new_values[]="\"" . trim($value, ' "') . "\"";
|
||||
}
|
||||
$indexSpec['value']=implode(',', $new_values);
|
||||
|
||||
//One last check:
|
||||
$existing=DB::query("SELECT tablename FROM pg_indexes WHERE indexname='" . strtolower($tableCol) . "';");
|
||||
if(!$existing->first()){
|
||||
//create a type-specific index
|
||||
//NOTE: hash should be removed. This is only here to demonstrate how other indexes can be made
|
||||
switch($indexSpec['type']){
|
||||
case 'fulltext':
|
||||
$spec="create index $tableCol ON \"" . $tableName . "\" USING " . $this->default_fts_cluster_method . "(\"ts_" . $indexName . "\") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'unique':
|
||||
$spec="create unique index $tableCol ON \"" . $tableName . "\" (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'btree':
|
||||
$spec="create index $tableCol ON \"" . $tableName . "\" USING btree (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'hash':
|
||||
//NOTE: this is not a recommended index type
|
||||
$spec="create index $tableCol ON \"" . $tableName . "\" USING hash (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'index':
|
||||
//'index' is the same as default, just a normal index with the default type decided by the database.
|
||||
default:
|
||||
$spec="create index $tableCol ON \"" . $tableName . "\" (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||
}
|
||||
|
||||
return trim($spec) . ';';
|
||||
} else {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$indexName=trim($indexName, '()');
|
||||
return $indexName;
|
||||
}
|
||||
}
|
||||
|
||||
function getDbSqlDefinition($tableName, $indexName, $indexSpec){
|
||||
return $this->getIndexSqlDefinition($tableName, $indexName, $indexSpec, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter an index on a table.
|
||||
* @param string $tableName The name of the table.
|
||||
* @param string $indexName The name of the index.
|
||||
* @param string $indexSpec The specification of the index, see Database::requireIndex() for more details.
|
||||
*/
|
||||
public function alterIndex($tableName, $indexName, $indexSpec) {
|
||||
$indexSpec = trim($indexSpec);
|
||||
if($indexSpec[0] != '(') {
|
||||
list($indexType, $indexFields) = explode(' ',$indexSpec,2);
|
||||
} else {
|
||||
$indexFields = $indexSpec;
|
||||
}
|
||||
|
||||
if(!$indexType) {
|
||||
$indexType = "index";
|
||||
}
|
||||
|
||||
$this->query("DROP INDEX $indexName");
|
||||
$this->query("ALTER TABLE \"$tableName\" ADD $indexType \"$indexName\" $indexFields");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of indexes in a table.
|
||||
* @param string $table The table name.
|
||||
* @return array
|
||||
*/
|
||||
public function indexList($table) {
|
||||
|
||||
//Retrieve a list of indexes for the specified table
|
||||
$schema_SQL = pg_escape_string($this->dbConn, $this->schema);
|
||||
$indexes=DB::query("SELECT tablename, indexname, indexdef FROM pg_catalog.pg_indexes WHERE tablename='$table' AND schemaname = '{$schema_SQL}';");
|
||||
|
||||
$indexList=Array();
|
||||
foreach($indexes as $index) {
|
||||
//We don't actually need the entire created command, just a few bits:
|
||||
$prefix='';
|
||||
|
||||
//Check for uniques:
|
||||
if(substr($index['indexdef'], 0, 13)=='CREATE UNIQUE')
|
||||
$prefix='unique ';
|
||||
|
||||
//check for hashes, btrees etc:
|
||||
if(strpos(strtolower($index['indexdef']), 'using hash ')!==false)
|
||||
$prefix='using hash ';
|
||||
|
||||
//TODO: Fix me: btree is the default index type:
|
||||
//if(strpos(strtolower($index['indexdef']), 'using btree ')!==false)
|
||||
// $prefix='using btree ';
|
||||
|
||||
if(strpos(strtolower($index['indexdef']), 'using rtree ')!==false)
|
||||
$prefix='using rtree ';
|
||||
|
||||
$value=explode(' ', substr($index['indexdef'], strpos($index['indexdef'], ' USING ')+7));
|
||||
|
||||
if(sizeof($value)>2){
|
||||
for($i=2; $i<sizeof($value); $i++)
|
||||
$value[1].=$value[$i];
|
||||
}
|
||||
|
||||
$key=substr($value[1], 0, strpos($value[1], ')'));
|
||||
$key=trim(trim(str_replace("\"", '', $key), '()'));
|
||||
$indexList[$key]['indexname']=$index['indexname'];
|
||||
$indexList[$key]['spec']=$prefix . '(' . $key . ')';
|
||||
|
||||
}
|
||||
|
||||
return isset($indexList) ? $indexList : null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all the tables in the database.
|
||||
* Table names will all be in lowercase.
|
||||
* @return array
|
||||
*/
|
||||
public function tableList() {
|
||||
$schema_SQL = pg_escape_string($this->dbConn, $this->schema);
|
||||
$tables=array();
|
||||
foreach($this->query("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = '{$schema_SQL}' AND tablename NOT ILIKE 'pg_%' AND tablename NOT ILIKE 'sql_%'") as $record) {
|
||||
//$table = strtolower(reset($record));
|
||||
$table = reset($record);
|
||||
$tables[$table] = $table;
|
||||
}
|
||||
|
||||
//Return an empty array if there's nothing in this database
|
||||
return $tables;
|
||||
}
|
||||
|
||||
function TableExists($tableName){
|
||||
$schema_SQL = pg_escape_string($this->dbConn, $this->schema);
|
||||
$result=$this->query("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = '{$schema_SQL}' AND tablename='$tableName';")->first();
|
||||
|
||||
if($result)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Find out what the constraint information is, given a constraint name.
|
||||
* We also cache this result, so the next time we don't need to do a
|
||||
* query all over again.
|
||||
*
|
||||
* @param string $constraint
|
||||
*/
|
||||
function constraintExists($constraint){
|
||||
if(!isset(self::$cached_constraints[$constraint])){
|
||||
$exists=DB::query("SELECT conname,pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE r.contype = 'c' AND conname='$constraint' ORDER BY 1;")->first();
|
||||
self::$cached_constraints[$constraint]=$exists;
|
||||
}
|
||||
|
||||
return self::$cached_constraints[$constraint];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of rows affected by the previous operation.
|
||||
* @return int
|
||||
*/
|
||||
public function affectedRows() {
|
||||
return pg_affected_rows(DB::$lastQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* A function to return the field names and datatypes for the particular table
|
||||
*/
|
||||
public function tableDetails($tableName){
|
||||
$schema_SQL = pg_escape_string($this->dbConn, $this->schema);
|
||||
$query="SELECT a.attname as \"Column\", pg_catalog.format_type(a.atttypid, a.atttypmod) as \"Datatype\" FROM pg_catalog.pg_attribute a WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = ( SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ '^($tableName)$' AND pg_catalog.pg_table_is_visible(c.oid) AND n.nspname = '{$schema_SQL}');";
|
||||
|
||||
$result=DB::query($query);
|
||||
|
||||
$table=Array();
|
||||
while($row=pg_fetch_assoc($result)){
|
||||
$table[]=Array('Column'=>$row['Column'], 'DataType'=>$row['DataType']);
|
||||
}
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a legit trigger name and it will be dropped
|
||||
* This assumes that the trigger has been named in a unique fashion
|
||||
*/
|
||||
function dropTrigger($triggerName, $tableName){
|
||||
$exists=DB::query("SELECT tgname FROM pg_trigger WHERE tgname='$triggerName';")->first();
|
||||
if($exists){
|
||||
DB::query("DROP trigger $triggerName ON \"$tableName\";");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This will return the fields that the trigger is monitoring
|
||||
* @param string $trigger
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function triggerFieldsFromTrigger($trigger){
|
||||
|
||||
if($trigger){
|
||||
$tsvector='tsvector_update_trigger';
|
||||
$ts_pos=strpos($trigger, $tsvector);
|
||||
$details=trim(substr($trigger, $ts_pos+strlen($tsvector)), '();');
|
||||
//Now split this into bits:
|
||||
$bits=explode(',', $details);
|
||||
|
||||
$fields=$bits[2];
|
||||
|
||||
$field_bits=explode(',', str_replace('"', '', $fields));
|
||||
$result=array();
|
||||
foreach($field_bits as $field_bit)
|
||||
$result[]=trim($field_bit);
|
||||
|
||||
return $result;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a boolean type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function boolean($values, $asDbValue=false){
|
||||
//Annoyingly, we need to do a good ol' fashioned switch here:
|
||||
($values['default']) ? $default='1' : $default='0';
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'smallint');
|
||||
else {
|
||||
if($values['arrayValue']!='')
|
||||
$default='';
|
||||
else
|
||||
$default=' default ' . (int)$values['default'];
|
||||
|
||||
return "smallint{$values['arrayValue']}" . $default;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a date type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function date($values){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
return "date{$values['arrayValue']}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a decimal type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function decimal($values, $asDbValue=false){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
// Avoid empty strings being put in the db
|
||||
if($values['precision'] == '') {
|
||||
$precision = 1;
|
||||
} else {
|
||||
$precision = $values['precision'];
|
||||
}
|
||||
|
||||
$defaultValue = '';
|
||||
if(isset($values['default']) && is_numeric($values['default'])) {
|
||||
$defaultValue = ' default ' . $values['default'];
|
||||
}
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'numeric', 'precision'=>$precision);
|
||||
else return "decimal($precision){$values['arrayValue']}$defaultValue";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a enum type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function enum($values){
|
||||
//Enums are a bit different. We'll be creating a varchar(255) with a constraint of all the usual enum options.
|
||||
//NOTE: In this one instance, we are including the table name in the values array
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if($values['arrayValue']!='')
|
||||
$default='';
|
||||
else
|
||||
$default=" default '{$values['default']}'";
|
||||
|
||||
return "varchar(255){$values['arrayValue']}" . $default . " check (\"" . $values['name'] . "\" in ('" . implode('\', \'', $values['enums']) . "'))";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a float type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function float($values, $asDbValue=false){
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'double precision');
|
||||
else return "float{$values['arrayValue']}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a float type-formatted string cause double is not supported
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function double($values, $asDbValue=false){
|
||||
return $this->float($values, $asDbValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a int type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function int($values, $asDbValue=false){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'integer', 'precision'=>'32');
|
||||
else {
|
||||
if($values['arrayValue']!='')
|
||||
$default='';
|
||||
else
|
||||
$default=' default ' . (int)$values['default'];
|
||||
|
||||
return "integer{$values['arrayValue']}" . $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime type-formatted string
|
||||
* For PostgreSQL, we simply return the word 'timestamp', no other parameters are necessary
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function SS_Datetime($values, $asDbValue=false){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'timestamp without time zone');
|
||||
else
|
||||
return "timestamp{$values['arrayValue']}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a text type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function text($values, $asDbValue=false){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'text');
|
||||
else
|
||||
return "text{$values['arrayValue']}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a time type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function time($values){
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
return "time{$values['arrayValue']}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a varchar type-formatted string
|
||||
*
|
||||
* @params array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function varchar($values, $asDbValue=false){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
if(!isset($values['precision']))
|
||||
$values['precision']=255;
|
||||
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'varchar', 'precision'=>$values['precision']);
|
||||
else
|
||||
return "varchar({$values['precision']}){$values['arrayValue']}";
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a 4 digit numeric type. MySQL has a proprietary 'Year' type.
|
||||
* For Postgres, we'll use a 4 digit numeric
|
||||
*/
|
||||
public function year($values, $asDbValue=false){
|
||||
|
||||
if(!isset($values['arrayValue']))
|
||||
$values['arrayValue']='';
|
||||
|
||||
//TODO: the DbValue result does not include the numeric_scale option (ie, the ,0 value in 4,0)
|
||||
if($asDbValue)
|
||||
return Array('data_type'=>'decimal', 'precision'=>'4');
|
||||
else
|
||||
return "decimal(4,0){$values['arrayValue']}";
|
||||
}
|
||||
|
||||
function escape_character($escape=false){
|
||||
if($escape)
|
||||
return "\\\"";
|
||||
else
|
||||
return "\"";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fulltext search datatype for PostgreSQL
|
||||
* This will also return a trigger to be applied to this table
|
||||
*
|
||||
* @todo: create custom functions to allow weighted searches
|
||||
*
|
||||
* @param array $spec
|
||||
*/
|
||||
function fulltext($this_index, $tableName, $name){
|
||||
//For full text search, we need to create a column for the index
|
||||
$columns=explode(',', $this_index['value']);
|
||||
for($i=0; $i<sizeof($columns);$i++)
|
||||
$columns[$i]="\"" . trim($columns[$i]) . "\"";
|
||||
|
||||
$columns=implode(', ', $columns);
|
||||
|
||||
$fulltexts="\"ts_$name\" tsvector";
|
||||
$triggerName="ts_{$tableName}_{$name}";
|
||||
$language=$this->get_search_language();
|
||||
|
||||
$this->dropTrigger($triggerName, $tableName);
|
||||
$triggers="CREATE TRIGGER $triggerName BEFORE INSERT OR UPDATE
|
||||
ON \"$tableName\" FOR EACH ROW EXECUTE PROCEDURE
|
||||
tsvector_update_trigger(\"ts_$name\", 'pg_catalog.$language', $columns);";
|
||||
|
||||
return Array('name'=>$name, 'ts_name'=>"ts_{$name}", 'fulltexts'=>$fulltexts, 'triggers'=>$triggers);
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the column which is the primary key for each table
|
||||
* In Postgres, it is a SERIAL8, which is the equivalent of an auto_increment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function IdColumn($asDbValue=false){
|
||||
if($asDbValue)
|
||||
return 'bigint';
|
||||
else return 'serial8 not null';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this table exists
|
||||
*/
|
||||
function hasTable($tableName) {
|
||||
$schema_SQL = pg_escape_string($this->dbConn, $this->schema);
|
||||
$result = $this->query("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = '{$schema_SQL}' AND tablename = '$tableName'");
|
||||
|
||||
if ($result->numRecords() > 0) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SQL command to get all the tables in this database
|
||||
*/
|
||||
function allTablesSQL(){
|
||||
$schema_SQL = pg_escape_string($this->dbConn, $this->schema);
|
||||
return "SELECT table_name FROM information_schema.tables WHERE table_schema='{$schema_SQL}' AND table_type='BASE TABLE';";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return enum values for the given field
|
||||
* @todo Make a proper implementation
|
||||
*/
|
||||
function enumValuesForField($tableName, $fieldName) {
|
||||
//return array('SiteTree','Page');
|
||||
$constraints=$this->constraintExists("{$tableName}_{$fieldName}_check");
|
||||
$classes=Array();
|
||||
if($constraints)
|
||||
$classes=$this->EnumValuesFromConstraint($constraints['pg_get_constraintdef']);
|
||||
|
||||
return $classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actual enum fields from the constraint value:
|
||||
*/
|
||||
private function EnumValuesFromConstraint($constraint){
|
||||
$constraint=substr($constraint, strpos($constraint, 'ANY (ARRAY[')+11);
|
||||
$constraint=substr($constraint, 0, -11);
|
||||
$constraints=Array();
|
||||
$segments=explode(',', $constraint);
|
||||
foreach($segments as $this_segment){
|
||||
$bits=preg_split('/ *:: */', $this_segment);
|
||||
array_unshift($constraints, trim($bits[0], " '"));
|
||||
}
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Because NOW() doesn't always work...
|
||||
* MSSQL, I'm looking at you
|
||||
*
|
||||
*/
|
||||
function now(){
|
||||
return 'NOW()';
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the database-specific version of the random() function
|
||||
*/
|
||||
function random(){
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
/*
|
||||
* This is a lookup table for data types.
|
||||
* For instance, Postgres uses 'INT', while MySQL uses 'UNSIGNED'
|
||||
* So this is a DB-specific list of equivilents.
|
||||
*/
|
||||
function dbDataType($type){
|
||||
$values=Array(
|
||||
'unsigned integer'=>'INT'
|
||||
);
|
||||
|
||||
if(isset($values[$type]))
|
||||
return $values[$type];
|
||||
else return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a SQLQuery object into a SQL statement
|
||||
* @todo There is a lot of duplication between this and MySQLDatabase::sqlQueryToString(). Perhaps they could both call a common
|
||||
* helper function in Database?
|
||||
*/
|
||||
public function sqlQueryToString(SQLQuery $sqlQuery) {
|
||||
$distinct = $sqlQuery->distinct ? "DISTINCT " : "";
|
||||
if($sqlQuery->delete) {
|
||||
$text = "DELETE ";
|
||||
} else if($sqlQuery->select) {
|
||||
$text = "SELECT $distinct" . implode(", ", $sqlQuery->select);
|
||||
}
|
||||
if($sqlQuery->from) $text .= " FROM " . implode(" ", $sqlQuery->from);
|
||||
if($sqlQuery->where) $text .= " WHERE (" . $sqlQuery->getFilter(). ")";
|
||||
if($sqlQuery->groupby) $text .= " GROUP BY " . implode(", ", $sqlQuery->groupby);
|
||||
if($sqlQuery->having) $text .= " HAVING ( " . implode(" ) AND ( ", $sqlQuery->having) . " )";
|
||||
if($sqlQuery->orderby) $text .= " ORDER BY " . $this->orderMoreSpecifically($sqlQuery->select,$sqlQuery->orderby);
|
||||
|
||||
if($sqlQuery->limit) {
|
||||
$limit = $sqlQuery->limit;
|
||||
|
||||
// Pass limit as array or SQL string value
|
||||
if(is_array($limit)) {
|
||||
|
||||
if(isset($limit['start']) && $limit['start']!='')
|
||||
$text.=' OFFSET ' . $limit['start'];
|
||||
if(isset($limit['limit']) && $limit['limit']!='')
|
||||
$text.=' LIMIT ' . $limit['limit'];
|
||||
|
||||
} else {
|
||||
if(strpos($sqlQuery->limit, ',')){
|
||||
$limit=str_replace(',', ' LIMIT ', $sqlQuery->limit);
|
||||
$text .= ' OFFSET ' . $limit;
|
||||
} else {
|
||||
$text.=' LIMIT ' . $sqlQuery->limit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
protected function orderMoreSpecifically($select,$order) {
|
||||
|
||||
//create a key so we can cache this result and quickly return it if we've done it before
|
||||
$cache_key=serialize($select) . $order;
|
||||
if(isset(self::$cached_ordered_specifically[$cache_key]))
|
||||
return self::$cached_ordered_specifically[$cache_key];
|
||||
|
||||
$altered = false;
|
||||
|
||||
// split expression into order terms
|
||||
$terms = explode(',', $order);
|
||||
|
||||
foreach($terms as $i => $term) {
|
||||
$term = trim($term);
|
||||
|
||||
// check if table is unspecified
|
||||
if(!preg_match('/\./', $term)) {
|
||||
$direction = '';
|
||||
if(preg_match('/( ASC)$|( DESC)$/i',$term)) list($term,$direction) = explode(' ', $term);
|
||||
|
||||
// find a match in the SELECT array and replace
|
||||
foreach($select as $s) {
|
||||
if(preg_match('/"[a-z0-9_]+"\.[\'"]' . $term . '[\'"]/i', trim($s))) {
|
||||
$terms[$i] = $s . ' ' . $direction;
|
||||
$altered = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Hold this result in the cache
|
||||
$result=implode(',', $terms);
|
||||
self::$cached_ordered_specifically[$cache_key]=$result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/*
|
||||
* This will return text which has been escaped in a database-friendly manner
|
||||
* Using PHP's addslashes method won't work in MSSQL
|
||||
*/
|
||||
function addslashes($value){
|
||||
return pg_escape_string($value);
|
||||
}
|
||||
|
||||
/*
|
||||
* This changes the index name depending on database requirements.
|
||||
*/
|
||||
function modifyIndex($index, $spec){
|
||||
|
||||
if(is_array($spec) && $spec['type']=='fulltext')
|
||||
return 'ts_' . str_replace(',', '_', $index);
|
||||
else
|
||||
return str_replace('_', ',', $index);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The core search engine configuration.
|
||||
* @todo Properly extract the search functions out of the core.
|
||||
*
|
||||
* @param string $keywords Keywords as a space separated string
|
||||
* @return object DataObjectSet of result pages
|
||||
*/
|
||||
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "ts_rank DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) {
|
||||
//Fix the keywords to be ts_query compatitble:
|
||||
//Spaces must have pipes
|
||||
//@TODO: properly handle boolean operators here.
|
||||
$keywords=trim($keywords);
|
||||
$keywords=str_replace(' ', ' | ', $keywords);
|
||||
$keywords=str_replace('"', "'", $keywords);
|
||||
|
||||
$keywords = Convert::raw2sql(trim($keywords));
|
||||
$htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES);
|
||||
|
||||
//We can get a list of all the tsvector columns though this query:
|
||||
//We know what tables to search in based on the $classesToSearch variable:
|
||||
$result=DB::query("SELECT table_name, column_name, data_type FROM information_schema.columns WHERE data_type='tsvector' AND table_name in ('" . implode("', '", $classesToSearch) . "');");
|
||||
if (!$result->numRecords()) throw new Exception('there are no full text columns to search');
|
||||
|
||||
$tables=Array();
|
||||
|
||||
// Make column selection lists
|
||||
$select = array(
|
||||
'SiteTree' => array("\"ClassName\"","\"SiteTree\".\"ID\"","\"ParentID\"","\"Title\"","\"URLSegment\"","\"Content\"","\"LastEdited\"","\"Created\"","NULL AS \"Filename\"", "NULL AS \"Name\"", "\"CanViewType\""),
|
||||
'File' => array("\"ClassName\"","\"File\".\"ID\"","0 AS \"ParentID\"","\"Title\"","NULL AS \"URLSegment\"","\"Content\"","\"LastEdited\"","\"Created\"","\"Filename\"","\"Name\"", "NULL AS \"CanViewType\""),
|
||||
);
|
||||
|
||||
foreach($result as $row){
|
||||
if($row['table_name']=='SiteTree') {
|
||||
$showInSearch="AND \"ShowInSearch\"=1 ";
|
||||
} elseif($row['table_name']=='File') {
|
||||
// File.ShowInSearch was added later, keep the database driver backwards compatible
|
||||
// by checking for its existence first
|
||||
$fields = $this->fieldList($row['table_name']);
|
||||
if(array_key_exists('ShowInSearch', $fields)) $showInSearch="AND \"ShowInSearch\"=1 ";
|
||||
else $showInSearch='';
|
||||
} else {
|
||||
$showInSearch='';
|
||||
}
|
||||
|
||||
//public function extendedSQL($filter = "", $sort = "", $limit = "", $join = "", $having = ""){
|
||||
$where = "\"" . $row['table_name'] . "\".\"" . $row['column_name'] . "\" " . $this->default_fts_search_method . ' q ' . $showInSearch;
|
||||
if(class_exists('DataList')) {
|
||||
$query = DataList::create($row['table_name'])->where($where, '')->dataQuery()->query();
|
||||
} else {
|
||||
$query = singleton($row['table_name'])->extendedSql($where, '');
|
||||
}
|
||||
|
||||
$query->select=$select[$row['table_name']];
|
||||
$query->from['tsearch']=", to_tsquery('" . $this->get_search_language() . "', '$keywords') AS q";
|
||||
|
||||
$query->select[]="ts_rank(\"{$row['table_name']}\".\"{$row['column_name']}\", q) AS \"Relevance\"";
|
||||
|
||||
$query->orderby=null;
|
||||
|
||||
//Add this query to the collection
|
||||
$tables[] = $query->sql();
|
||||
}
|
||||
|
||||
$limit=$pageLength;
|
||||
$offset=$start;
|
||||
|
||||
if($keywords)
|
||||
$orderBy=" ORDER BY $sortBy";
|
||||
else $orderBy='';
|
||||
|
||||
$fullQuery = "SELECT * FROM (" . implode(" UNION ", $tables) . ") AS q1 $orderBy LIMIT $limit OFFSET $offset";
|
||||
|
||||
// Get records
|
||||
$records = DB::query($fullQuery);
|
||||
$totalCount=0;
|
||||
foreach($records as $record){
|
||||
$objects[] = new $record['ClassName']($record);
|
||||
$totalCount++;
|
||||
}
|
||||
|
||||
if(class_exists('PaginatedList')) {
|
||||
if(isset($objects)) $results = new ArrayList($objects);
|
||||
else $results = new ArrayList();
|
||||
$list = new PaginatedList($results);
|
||||
$list->setPageStart($start);
|
||||
$list->setPageLength($pageLength);
|
||||
$list->setTotalItems($totalCount);
|
||||
return $list;
|
||||
} else {
|
||||
if(isset($objects)) $results = new DataObjectSet($objects);
|
||||
else $results = new DataObjectSet();
|
||||
$results->setPageLimits($start, $pageLength, $current+1);
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Does this database support transactions?
|
||||
*/
|
||||
public function supportsTransactions(){
|
||||
return $this->supportsTransactions;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is a quick lookup to discover if the database supports particular extensions
|
||||
*/
|
||||
public function supportsExtensions($extensions=Array('partitions', 'tablespaces', 'clustering')){
|
||||
if(isset($extensions['partitions']))
|
||||
return true;
|
||||
elseif(isset($extensions['tablespaces']))
|
||||
return true;
|
||||
elseif(isset($extensions['clustering']))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.0 Use transactionStart() (method required for 2.4.x)
|
||||
*/
|
||||
public function startTransaction($transaction_mode=false, $session_characteristics=false){
|
||||
$this->transactionStart($transaction_mode, $session_characteristics);
|
||||
}
|
||||
|
||||
/*
|
||||
* Start a prepared transaction
|
||||
* See http://developer.postgresql.org/pgdocs/postgres/sql-set-transaction.html for details on transaction isolation options
|
||||
*/
|
||||
public function transactionStart($transaction_mode=false, $session_characteristics=false){
|
||||
DB::query('BEGIN;');
|
||||
|
||||
if($transaction_mode)
|
||||
DB::query('SET TRANSACTION ' . $transaction_mode . ';');
|
||||
|
||||
if($session_characteristics)
|
||||
DB::query('SET SESSION CHARACTERISTICS AS TRANSACTION ' . $session_characteristics . ';');
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a savepoint that you can jump back to if you encounter problems
|
||||
*/
|
||||
public function transactionSavepoint($savepoint){
|
||||
DB::query("SAVEPOINT $savepoint;");
|
||||
}
|
||||
|
||||
/*
|
||||
* Rollback or revert to a savepoint if your queries encounter problems
|
||||
* If you encounter a problem at any point during a transaction, you may
|
||||
* need to rollback that particular query, or return to a savepoint
|
||||
*/
|
||||
public function transactionRollback($savepoint=false){
|
||||
|
||||
if($savepoint)
|
||||
DB::query("ROLLBACK TO $savepoint;");
|
||||
else
|
||||
DB::query('ROLLBACK;');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.0 Use transactionEnd() (method required for 2.4.x)
|
||||
*/
|
||||
public function endTransaction(){
|
||||
$this->transactionEnd();
|
||||
}
|
||||
|
||||
/*
|
||||
* Commit everything inside this transaction so far
|
||||
*/
|
||||
public function transactionEnd(){
|
||||
DB::query('COMMIT;');
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a tablespace and and location, either create a new one
|
||||
* or update the existing one
|
||||
*/
|
||||
public function createOrReplaceTablespace($name, $location){
|
||||
$existing=DB::query("SELECT spcname, spclocation FROM pg_tablespace WHERE spcname='$name';")->first();
|
||||
|
||||
//NOTE: this location must be empty for this to work
|
||||
//We can't seem to change the location of the tablespace through any ALTER commands :(
|
||||
|
||||
//If a tablespace with this name exists, but the location has changed, then drop the current one
|
||||
//if($existing && $location!=$existing['spclocation'])
|
||||
// DB::query("DROP TABLESPACE $name;");
|
||||
|
||||
//If this is a new tablespace, or we have dropped the current one:
|
||||
if(!$existing || ($existing && $location!=$existing['spclocation']))
|
||||
DB::query("CREATE TABLESPACE $name LOCATION '$location';");
|
||||
|
||||
}
|
||||
|
||||
public function createOrReplacePartition($tableName, $partitions, $indexes, $extensions){
|
||||
|
||||
//We need the plpgsql language to be installed for this to work:
|
||||
$this->createLanguage('plpgsql');
|
||||
|
||||
$trigger='CREATE OR REPLACE FUNCTION ' . $tableName . '_insert_trigger() RETURNS TRIGGER AS $$ BEGIN ';
|
||||
$first=true;
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if($extensions && isset($extensions['tablespace'])){
|
||||
$this->createOrReplaceTablespace($extensions['tablespace']['name'], $extensions['tablespace']['location']);
|
||||
$tableSpace=' TABLESPACE ' . $extensions['tablespace']['name'];
|
||||
} else
|
||||
$tableSpace='';
|
||||
|
||||
foreach($partitions as $partition_name=>$partition_value){
|
||||
//Check that this child table does not already exist:
|
||||
if(!$this->TableExists($partition_name)){
|
||||
DB::query("CREATE TABLE \"$partition_name\" (CHECK (" . str_replace('NEW.', '', $partition_value) . ")) INHERITS (\"$tableName\")$tableSpace;");
|
||||
} else {
|
||||
//Drop the constraint, we will recreate in in the next line
|
||||
$existing_constraint=$this->query("SELECT conname FROM pg_constraint WHERE conname='{$partition_name}_pkey';");
|
||||
if($existing_constraint){
|
||||
DB::query("ALTER TABLE \"$partition_name\" DROP CONSTRAINT \"{$partition_name}_pkey\";");
|
||||
}
|
||||
$this->dropTrigger(strtolower('trigger_' . $tableName . '_insert'), $tableName);
|
||||
}
|
||||
|
||||
DB::query("ALTER TABLE \"$partition_name\" ADD CONSTRAINT \"{$partition_name}_pkey\" PRIMARY KEY (\"ID\");");
|
||||
|
||||
if($first){
|
||||
$trigger.='IF';
|
||||
$first=false;
|
||||
} else
|
||||
$trigger.='ELSIF';
|
||||
|
||||
$trigger.=" ($partition_value) THEN INSERT INTO \"$partition_name\" VALUES (NEW.*);";
|
||||
|
||||
if($indexes){
|
||||
// We need to propogate the indexes through to the child pages.
|
||||
// Some of this code is duplicated, and could be tidied up
|
||||
foreach($indexes as $name=>$this_index){
|
||||
|
||||
if($this_index['type']=='fulltext'){
|
||||
$fillfactor=$where='';
|
||||
if(isset($this_index['fillfactor']))
|
||||
$fillfactor='WITH (FILLFACTOR = ' . $this_index['fillfactor'] . ')';
|
||||
if(isset($this_index['where']))
|
||||
$where='WHERE ' . $this_index['where'];
|
||||
|
||||
DB::query("CREATE INDEX \"ix_{$partition_name}_{$this_index['name']}\" ON \"" . $partition_name . "\" USING " . $this->default_fts_cluster_method . "(\"ts_" . $name . "\") $fillfactor $where");
|
||||
$ts_details=$this->fulltext($this_index, $partition_name, $name);
|
||||
DB::query($ts_details['triggers']);
|
||||
} else {
|
||||
|
||||
if(is_array($this_index))
|
||||
$index_name=$this_index['name'];
|
||||
else $index_name=trim($this_index, '()');
|
||||
|
||||
$createIndex=$this->getIndexSqlDefinition($partition_name, $index_name, $this_index);
|
||||
if($createIndex!==false)
|
||||
DB::query($createIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if($extensions && isset($extensions['cluster'])){
|
||||
DB::query("CLUSTER \"$partition_name\" USING \"{$extensions['cluster']}\";");
|
||||
}
|
||||
}
|
||||
|
||||
$trigger.='ELSE RAISE EXCEPTION \'Value id out of range. Fix the ' . $tableName . '_insert_trigger() function!\'; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql;';
|
||||
$trigger.='CREATE TRIGGER trigger_' . $tableName . '_insert BEFORE INSERT ON "' . $tableName . '" FOR EACH ROW EXECUTE PROCEDURE ' . $tableName . '_insert_trigger();';
|
||||
|
||||
DB::query($trigger);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* This will create a language if it doesn't already exist.
|
||||
* This is used by the createOrReplacePartition function, which needs plpgsql
|
||||
*/
|
||||
public function createLanguage($language){
|
||||
$result=DB::query("SELECT lanname FROM pg_language WHERE lanname='$language';")->first();
|
||||
|
||||
if(!$result){
|
||||
DB::query("CREATE LANGUAGE $language;");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with Postgres
|
||||
* used for querying a datetime in a certain format
|
||||
* @param string $date to be formated, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @param string $format to be used, supported specifiers:
|
||||
* %Y = Year (four digits)
|
||||
* %m = Month (01..12)
|
||||
* %d = Day (01..31)
|
||||
* %H = Hour (00..23)
|
||||
* %i = Minutes (00..59)
|
||||
* %s = Seconds (00..59)
|
||||
* %U = unix timestamp, can only be used on it's own
|
||||
* @return string SQL datetime expression to query for a formatted datetime
|
||||
*/
|
||||
function formattedDatetimeClause($date, $format) {
|
||||
|
||||
preg_match_all('/%(.)/', $format, $matches);
|
||||
foreach($matches[1] as $match) if(array_search($match, array('Y','m','d','H','i','s','U')) === false) user_error('formattedDatetimeClause(): unsupported format character %' . $match, E_USER_WARNING);
|
||||
|
||||
$translate = array(
|
||||
'/%Y/' => 'YYYY',
|
||||
'/%m/' => 'MM',
|
||||
'/%d/' => 'DD',
|
||||
'/%H/' => 'HH24',
|
||||
'/%i/' => 'MI',
|
||||
'/%s/' => 'SS',
|
||||
);
|
||||
$format = preg_replace(array_keys($translate), array_values($translate), $format);
|
||||
|
||||
if(preg_match('/^now$/i', $date)) {
|
||||
$date = "NOW()";
|
||||
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
|
||||
$date = "TIMESTAMP '$date'";
|
||||
}
|
||||
|
||||
if($format == '%U') return "FLOOR(EXTRACT(epoch FROM $date))";
|
||||
|
||||
return "to_char($date, TEXT '$format')";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with Postgres
|
||||
* used for querying a datetime addition
|
||||
* @param string $date, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @param string $interval to be added, use the format [sign][integer] [qualifier], e.g. -1 Day, +15 minutes, +1 YEAR
|
||||
* supported qualifiers:
|
||||
* - years
|
||||
* - months
|
||||
* - days
|
||||
* - hours
|
||||
* - minutes
|
||||
* - seconds
|
||||
* This includes the singular forms as well
|
||||
* @return string SQL datetime expression to query for a datetime (YYYY-MM-DD hh:mm:ss) which is the result of the addition
|
||||
*/
|
||||
function datetimeIntervalClause($date, $interval) {
|
||||
|
||||
if(preg_match('/^now$/i', $date)) {
|
||||
$date = "NOW()";
|
||||
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
|
||||
$date = "TIMESTAMP '$date'";
|
||||
}
|
||||
|
||||
// ... when being too precise becomes a pain. we need to cut of the fractions.
|
||||
// TIMESTAMP(0) doesn't work because it rounds instead flooring
|
||||
return "CAST(SUBSTRING(CAST($date + INTERVAL '$interval' AS VARCHAR) FROM 1 FOR 19) AS TIMESTAMP)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with Postgres
|
||||
* used for querying a datetime substraction
|
||||
* @param string $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @param string $date2 to be substracted of $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @return string SQL datetime expression to query for the interval between $date1 and $date2 in seconds which is the result of the substraction
|
||||
*/
|
||||
function datetimeDifferenceClause($date1, $date2) {
|
||||
|
||||
if(preg_match('/^now$/i', $date1)) {
|
||||
$date1 = "NOW()";
|
||||
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date1)) {
|
||||
$date1 = "TIMESTAMP '$date1'";
|
||||
}
|
||||
|
||||
if(preg_match('/^now$/i', $date2)) {
|
||||
$date2 = "NOW()";
|
||||
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date2)) {
|
||||
$date2 = "TIMESTAMP '$date2'";
|
||||
}
|
||||
|
||||
return "(FLOOR(EXTRACT(epoch FROM $date1)) - FLOOR(EXTRACT(epoch from $date2)))";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a set type-formatted string
|
||||
* This is used for Multi-enum support, which isn't actually supported by Postgres.
|
||||
* Throws a user error to show our lack of support, and return an "int", specifically for sapphire
|
||||
* tests that test multi-enums. This results in a test failure, but not crashing the test run.
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function set($values){
|
||||
user_error("PostGreSQL does not support multi-enum");
|
||||
return "int";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current language for the tsearch functions
|
||||
*
|
||||
* @todo: somehow link this to the locale options?
|
||||
*
|
||||
* @param string $lang
|
||||
*/
|
||||
public function set_search_language($lang){
|
||||
$this->search_language=$lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current language for the tsearch functions
|
||||
*
|
||||
* @param string $lang
|
||||
*/
|
||||
public function get_search_language(){
|
||||
return $this->search_language;
|
||||
}
|
||||
class PostgreSQLDatabase extends Database
|
||||
{
|
||||
use Configurable;
|
||||
|
||||
/**
|
||||
* Database schema manager object
|
||||
*
|
||||
* @var PostgreSQLSchemaManager
|
||||
*/
|
||||
protected $schemaManager;
|
||||
|
||||
/**
|
||||
* The currently selected database schema name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $schema;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $transactionNesting = 0;
|
||||
|
||||
/**
|
||||
* Toggle if transactions are supported. Defaults to true.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $supportsTransactions = true;
|
||||
|
||||
/**
|
||||
* Determines whether to check a database exists on the host by
|
||||
* querying the 'postgres' database and running createDatabase.
|
||||
*
|
||||
* Some locked down systems prevent access to the 'postgres' table in
|
||||
* which case you need to set this to false.
|
||||
*
|
||||
* If allow_query_master_postgres is false, and model_schema_as_database is also false,
|
||||
* then attempts to create or check databases beyond the initial connection will
|
||||
* result in a runtime error.
|
||||
*
|
||||
* @config
|
||||
* @var bool
|
||||
*/
|
||||
private static $allow_query_master_postgres = true;
|
||||
|
||||
/**
|
||||
* For instances where multiple databases are used beyond the initial connection
|
||||
* you may set this option to true to force database switches to switch schemas
|
||||
* instead of using databases. This may be useful if the database user does not
|
||||
* have cross-database permissions, and in cases where multiple databases are used
|
||||
* (such as in running test cases).
|
||||
*
|
||||
* If this is true then the database will only be set during the initial connection,
|
||||
* and attempts to change to this database will use the 'public' schema instead
|
||||
*
|
||||
* If this is false then errors may be generated during some cross database operations.
|
||||
*/
|
||||
private static $model_schema_as_database = true;
|
||||
|
||||
/**
|
||||
* Override the language that tsearch uses. By default it is 'english, but
|
||||
* could be any of the supported languages that can be found in the
|
||||
* pg_catalog.pg_ts_config table.
|
||||
*/
|
||||
private static $search_language = 'english';
|
||||
|
||||
/*
|
||||
* Describe how T-search will work.
|
||||
* You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin)
|
||||
* Combinations of these two will also work, so you'll need to pick
|
||||
* one which works best for you
|
||||
*/
|
||||
private static $default_fts_cluster_method = 'GIN';
|
||||
|
||||
/*
|
||||
* Describe how T-search will work.
|
||||
* You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin)
|
||||
* Combinations of these two will also work, so you'll need to pick
|
||||
* one which works best for you
|
||||
*/
|
||||
private static $default_fts_search_method = '@@@';
|
||||
|
||||
const MASTER_DATABASE = 'postgres';
|
||||
|
||||
const MASTER_SCHEMA = 'public';
|
||||
|
||||
/**
|
||||
* Full text cluster method. (e.g. GIN or GiST)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function default_fts_cluster_method()
|
||||
{
|
||||
return static::config()->default_fts_cluster_method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Full text search method.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function default_fts_search_method()
|
||||
{
|
||||
return static::config()->default_fts_search_method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether to check a database exists on the host by
|
||||
* querying the 'postgres' database and running createDatabase.
|
||||
*
|
||||
* Some locked down systems prevent access to the 'postgres' table in
|
||||
* which case you need to set this to false.
|
||||
*
|
||||
* If allow_query_master_postgres is false, and model_schema_as_database is also false,
|
||||
* then attempts to create or check databases beyond the initial connection will
|
||||
* result in a runtime error.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function allow_query_master_postgres()
|
||||
{
|
||||
return static::config()->allow_query_master_postgres;
|
||||
}
|
||||
|
||||
/**
|
||||
* For instances where multiple databases are used beyond the initial connection
|
||||
* you may set this option to true to force database switches to switch schemas
|
||||
* instead of using databases. This may be useful if the database user does not
|
||||
* have cross-database permissions, and in cases where multiple databases are used
|
||||
* (such as in running test cases).
|
||||
*
|
||||
* If this is true then the database will only be set during the initial connection,
|
||||
* and attempts to change to this database will use the 'public' schema instead
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function model_schema_as_database()
|
||||
{
|
||||
return static::config()->model_schema_as_database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the language that tsearch uses. By default it is 'english, but
|
||||
* could be any of the supported languages that can be found in the
|
||||
* pg_catalog.pg_ts_config table.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function search_language()
|
||||
{
|
||||
return static::config()->search_language;
|
||||
}
|
||||
|
||||
/**
|
||||
* The database name specified at initial connection
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $databaseOriginal = '';
|
||||
|
||||
/**
|
||||
* The schema name specified at initial construction. When model_schema_as_database
|
||||
* is set to true selecting the $databaseOriginal database will instead reset
|
||||
* the schema to this
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $schemaOriginal = '';
|
||||
|
||||
/**
|
||||
* Connection parameters specified at inital connection
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $parameters = array();
|
||||
|
||||
public function connect($parameters)
|
||||
{
|
||||
// Check database name
|
||||
if (empty($parameters['database'])) {
|
||||
// Check if we can use the master database
|
||||
if (!self::allow_query_master_postgres()) {
|
||||
throw new ErrorException('PostegreSQLDatabase::connect called without a database name specified');
|
||||
}
|
||||
// Fallback to master database connection if permission allows
|
||||
$parameters['database'] = self::MASTER_DATABASE;
|
||||
}
|
||||
$this->databaseOriginal = $parameters['database'];
|
||||
|
||||
// check schema name
|
||||
if (empty($parameters['schema'])) {
|
||||
$parameters['schema'] = self::MASTER_SCHEMA;
|
||||
}
|
||||
$this->schemaOriginal = $parameters['schema'];
|
||||
|
||||
// Ensure that driver is available (required by PDO)
|
||||
if (empty($parameters['driver'])) {
|
||||
$parameters['driver'] = $this->getDatabaseServer();
|
||||
}
|
||||
|
||||
// Ensure port number is set (required by postgres)
|
||||
if (empty($parameters['port'])) {
|
||||
$parameters['port'] = 5432;
|
||||
}
|
||||
|
||||
$this->parameters = $parameters;
|
||||
|
||||
// If allowed, check that the database exists. Otherwise naively assume
|
||||
// that the original database exists
|
||||
if (self::allow_query_master_postgres()) {
|
||||
// Use master connection to setup initial schema
|
||||
$this->connectMaster();
|
||||
if (!$this->schemaManager->postgresDatabaseExists($this->databaseOriginal)) {
|
||||
$this->schemaManager->createPostgresDatabase($this->databaseOriginal);
|
||||
}
|
||||
}
|
||||
|
||||
// Connect to the actual database we're requesting
|
||||
$this->connectDefault();
|
||||
|
||||
// Set up the schema if required
|
||||
$this->setSchema($this->schemaOriginal, true);
|
||||
|
||||
// Set the timezone if required.
|
||||
if (isset($parameters['timezone'])) {
|
||||
$this->selectTimezone($parameters['timezone']);
|
||||
}
|
||||
}
|
||||
|
||||
protected function connectMaster()
|
||||
{
|
||||
$parameters = $this->parameters;
|
||||
$parameters['database'] = self::MASTER_DATABASE;
|
||||
$this->connector->connect($parameters, true);
|
||||
}
|
||||
|
||||
protected function connectDefault()
|
||||
{
|
||||
$parameters = $this->parameters;
|
||||
$parameters['database'] = $this->databaseOriginal;
|
||||
$this->connector->connect($parameters, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the system timezone for the database connection
|
||||
*
|
||||
* @param string $timezone
|
||||
*/
|
||||
public function selectTimezone($timezone)
|
||||
{
|
||||
if (empty($timezone)) {
|
||||
return;
|
||||
}
|
||||
$this->query("SET SESSION TIME ZONE '$timezone';");
|
||||
}
|
||||
|
||||
public function supportsCollations()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function supportsTimezoneOverride()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDatabaseServer()
|
||||
{
|
||||
return "pgsql";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the current schema in use
|
||||
*
|
||||
* @return string Name of current schema
|
||||
*/
|
||||
public function currentSchema()
|
||||
{
|
||||
return $this->schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to manually set the schema to an alternative
|
||||
* Check existance & sets search path to the supplied schema name
|
||||
*
|
||||
* @param string $schema Name of the schema
|
||||
* @param boolean $create Flag indicating whether the schema should be created
|
||||
* if it doesn't exist. If $create is false and the schema doesn't exist
|
||||
* then an error will be raised
|
||||
* @param int|boolean $errorLevel The level of error reporting to enable for
|
||||
* the query, or false if no error should be raised
|
||||
* @return boolean Flag indicating success
|
||||
*/
|
||||
public function setSchema($schema, $create = false, $errorLevel = E_USER_ERROR)
|
||||
{
|
||||
if (!$this->schemaManager->schemaExists($schema)) {
|
||||
// Check DB creation permisson
|
||||
if (!$create) {
|
||||
if ($errorLevel !== false) {
|
||||
user_error("Schema $schema does not exist", $errorLevel);
|
||||
}
|
||||
$this->schema = null;
|
||||
return false;
|
||||
}
|
||||
$this->schemaManager->createSchema($schema);
|
||||
}
|
||||
$this->setSchemaSearchPath($schema);
|
||||
$this->schema = $schema;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the schema search path. Search using the arguments supplied.
|
||||
* NOTE: The search path is normally set through setSchema() and only
|
||||
* one schema is selected. The facility to add more than one schema to
|
||||
* the search path is provided as an advanced PostgreSQL feature for raw
|
||||
* SQL queries. Sapphire cannot search for datamodel tables in alternate
|
||||
* schemas, so be wary of using alternate schemas within the ORM environment.
|
||||
*
|
||||
* @param string ...$arg Schema name to use. Add additional schema names as extra arguments.
|
||||
*/
|
||||
public function setSchemaSearchPath($arg = null)
|
||||
{
|
||||
if (!$arg) {
|
||||
user_error('At least one Schema must be supplied to set a search path.', E_USER_ERROR);
|
||||
}
|
||||
$schemas = array_values(func_get_args());
|
||||
$this->query("SET search_path TO \"" . implode("\",\"", $schemas) . "\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* The core search engine configuration.
|
||||
* @todo Properly extract the search functions out of the core.
|
||||
*
|
||||
* @param array $classesToSearch
|
||||
* @param string $keywords Keywords as a space separated string
|
||||
* @param int $start
|
||||
* @param int $pageLength
|
||||
* @param string $sortBy
|
||||
* @param string $extraFilter
|
||||
* @param bool $booleanSearch
|
||||
* @param string $alternativeFileFilter
|
||||
* @param bool $invertedMatch
|
||||
* @return PaginatedList List of result pages
|
||||
* @throws Exception
|
||||
*/
|
||||
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "ts_rank DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false)
|
||||
{
|
||||
$start = (int)$start;
|
||||
$pageLength = (int)$pageLength;
|
||||
|
||||
//Fix the keywords to be ts_query compatitble:
|
||||
//Spaces must have pipes
|
||||
//@TODO: properly handle boolean operators here.
|
||||
$keywords= trim($keywords);
|
||||
$keywords= str_replace(' ', ' | ', $keywords);
|
||||
$keywords= str_replace('"', "'", $keywords);
|
||||
|
||||
|
||||
$keywords = $this->quoteString(trim($keywords));
|
||||
|
||||
// Get tables
|
||||
$tablesToSearch = [];
|
||||
foreach ($classesToSearch as $class) {
|
||||
$tablesToSearch[$class] = DataObject::getSchema()->baseDataTable($class);
|
||||
}
|
||||
|
||||
//We can get a list of all the tsvector columns though this query:
|
||||
//We know what tables to search in based on the $classesToSearch variable:
|
||||
$classesPlaceholders = DB::placeholders($classesToSearch);
|
||||
$searchableColumns = $this->preparedQuery(
|
||||
"
|
||||
SELECT table_name, column_name, data_type
|
||||
FROM information_schema.columns
|
||||
WHERE data_type='tsvector' AND table_name in ($classesPlaceholders);",
|
||||
array_values($tablesToSearch)
|
||||
);
|
||||
if (!$searchableColumns->numRecords()) {
|
||||
throw new Exception('there are no full text columns to search');
|
||||
}
|
||||
|
||||
$tables = array();
|
||||
$tableParameters = array();
|
||||
|
||||
// Make column selection lists
|
||||
$pageClass = 'SilverStripe\\CMS\\Model\\SiteTree';
|
||||
$fileClass = 'SilverStripe\\Assets\\File';
|
||||
$select = array(
|
||||
$pageClass => array(
|
||||
'"ClassName"',
|
||||
'"' . $tablesToSearch[$pageClass] . '"."ID"',
|
||||
'"ParentID"',
|
||||
'"Title"',
|
||||
'"URLSegment"',
|
||||
'"Content"',
|
||||
'"LastEdited"',
|
||||
'"Created"',
|
||||
'NULL AS "Name"',
|
||||
'"CanViewType"'
|
||||
),
|
||||
$fileClass => array(
|
||||
'"ClassName"',
|
||||
'"' . $tablesToSearch[$fileClass] . '"."ID"',
|
||||
'0 AS "ParentID"',
|
||||
'"Title"',
|
||||
'NULL AS "URLSegment"',
|
||||
'NULL AS "Content"',
|
||||
'"LastEdited"',
|
||||
'"Created"',
|
||||
'"Name"',
|
||||
'NULL AS "CanViewType"'
|
||||
)
|
||||
);
|
||||
|
||||
foreach ($searchableColumns as $searchableColumn) {
|
||||
$conditions = array();
|
||||
$tableName = $searchableColumn['table_name'];
|
||||
$columnName = $searchableColumn['column_name'];
|
||||
$className = DataObject::getSchema()->tableClass($tableName);
|
||||
if (DataObject::getSchema()->fieldSpec($className, 'ShowInSearch')) {
|
||||
$conditions[] = array('"ShowInSearch"' => 1);
|
||||
}
|
||||
|
||||
$method = self::default_fts_search_method();
|
||||
$conditions[] = "\"{$tableName}\".\"{$columnName}\" $method q ";
|
||||
$query = DataObject::get($className, $conditions)->dataQuery()->query();
|
||||
|
||||
// Could parameterise this, but convention is only to to so for where conditions
|
||||
$query->addFrom(array(
|
||||
'q' => ", to_tsquery('" . self::search_language() . "', $keywords)"
|
||||
));
|
||||
$query->setSelect(array());
|
||||
|
||||
foreach ($select[$className] as $clause) {
|
||||
if (preg_match('/^(.*) +AS +"?([^"]*)"?/i', $clause, $matches)) {
|
||||
$query->selectField($matches[1], $matches[2]);
|
||||
} else {
|
||||
$query->selectField($clause);
|
||||
}
|
||||
}
|
||||
|
||||
$query->selectField("ts_rank(\"{$tableName}\".\"{$columnName}\", q)", 'Relevance');
|
||||
$query->setOrderBy(array());
|
||||
|
||||
//Add this query to the collection
|
||||
$tables[] = $query->sql($parameters);
|
||||
$tableParameters = array_merge($tableParameters, $parameters);
|
||||
}
|
||||
|
||||
$limit = $pageLength;
|
||||
$offset = $start;
|
||||
|
||||
if ($keywords) {
|
||||
$orderBy = " ORDER BY $sortBy";
|
||||
} else {
|
||||
$orderBy='';
|
||||
}
|
||||
|
||||
$fullQuery = "SELECT *, count(*) OVER() as _fullcount FROM (" . implode(" UNION ", $tables) . ") AS q1 $orderBy LIMIT $limit OFFSET $offset";
|
||||
|
||||
// Get records
|
||||
$records = $this->preparedQuery($fullQuery, $tableParameters);
|
||||
$totalCount = 0;
|
||||
$objects = [];
|
||||
foreach ($records as $record) {
|
||||
$objects[] = Injector::inst()->createWithArgs($record['ClassName'], [$record]);
|
||||
$totalCount = $record['_fullcount'];
|
||||
}
|
||||
|
||||
if ($objects) {
|
||||
$results = new ArrayList($objects);
|
||||
} else {
|
||||
$results = new ArrayList();
|
||||
}
|
||||
$list = new PaginatedList($results);
|
||||
$list->setLimitItems(false);
|
||||
$list->setPageStart($start);
|
||||
$list->setPageLength($pageLength);
|
||||
$list->setTotalItems($totalCount);
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function supportsTransactions()
|
||||
{
|
||||
return $this->supportsTransactions;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is a quick lookup to discover if the database supports particular extensions
|
||||
*/
|
||||
public function supportsExtensions($extensions = array('partitions', 'tablespaces', 'clustering'))
|
||||
{
|
||||
if (isset($extensions['partitions'])) {
|
||||
return true;
|
||||
} elseif (isset($extensions['tablespaces'])) {
|
||||
return true;
|
||||
} elseif (isset($extensions['clustering'])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function transactionStart($transaction_mode = false, $session_characteristics = false)
|
||||
{
|
||||
if ($this->transactionNesting > 0) {
|
||||
$this->transactionSavepoint('NESTEDTRANSACTION' . $this->transactionNesting);
|
||||
} else {
|
||||
$this->query('BEGIN;');
|
||||
|
||||
if ($transaction_mode) {
|
||||
$this->query("SET TRANSACTION {$transaction_mode};");
|
||||
}
|
||||
|
||||
if ($session_characteristics) {
|
||||
$this->query("SET SESSION CHARACTERISTICS AS TRANSACTION {$session_characteristics};");
|
||||
}
|
||||
}
|
||||
++$this->transactionNesting;
|
||||
}
|
||||
|
||||
public function transactionSavepoint($savepoint)
|
||||
{
|
||||
$this->query("SAVEPOINT {$savepoint};");
|
||||
}
|
||||
|
||||
public function transactionRollback($savepoint = false)
|
||||
{
|
||||
// Named savepoint
|
||||
if ($savepoint) {
|
||||
$this->query('ROLLBACK TO ' . $savepoint);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Abort if unable to unnest, otherwise jump up a level
|
||||
if (!$this->transactionNesting) {
|
||||
return false;
|
||||
}
|
||||
--$this->transactionNesting;
|
||||
|
||||
// Rollback nested
|
||||
if ($this->transactionNesting > 0) {
|
||||
return $this->transactionRollback('NESTEDTRANSACTION' . $this->transactionNesting);
|
||||
}
|
||||
|
||||
// Rollback top level
|
||||
$this->query('ROLLBACK');
|
||||
return true;
|
||||
}
|
||||
|
||||
public function transactionDepth()
|
||||
{
|
||||
return $this->transactionNesting;
|
||||
}
|
||||
|
||||
public function transactionEnd($chain = false)
|
||||
{
|
||||
--$this->transactionNesting;
|
||||
if ($this->transactionNesting <= 0) {
|
||||
$this->transactionNesting = 0;
|
||||
$this->query('COMMIT;');
|
||||
}
|
||||
}
|
||||
|
||||
public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null, $parameterised = false)
|
||||
{
|
||||
if ($exact && $caseSensitive === null) {
|
||||
$comp = ($negate) ? '!=' : '=';
|
||||
} else {
|
||||
$comp = ($caseSensitive === true) ? 'LIKE' : 'ILIKE';
|
||||
if ($negate) {
|
||||
$comp = 'NOT ' . $comp;
|
||||
}
|
||||
$field.='::text';
|
||||
}
|
||||
|
||||
if ($parameterised) {
|
||||
return sprintf("%s %s ?", $field, $comp);
|
||||
} else {
|
||||
return sprintf("%s %s '%s'", $field, $comp, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with Postgres
|
||||
* used for querying a datetime in a certain format
|
||||
* @param string $date to be formated, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @param string $format to be used, supported specifiers:
|
||||
* %Y = Year (four digits)
|
||||
* %m = Month (01..12)
|
||||
* %d = Day (01..31)
|
||||
* %H = Hour (00..23)
|
||||
* %i = Minutes (00..59)
|
||||
* %s = Seconds (00..59)
|
||||
* %U = unix timestamp, can only be used on it's own
|
||||
* @return string SQL datetime expression to query for a formatted datetime
|
||||
*/
|
||||
public function formattedDatetimeClause($date, $format)
|
||||
{
|
||||
preg_match_all('/%(.)/', $format, $matches);
|
||||
foreach ($matches[1] as $match) {
|
||||
if (array_search($match, array('Y','m','d','H','i','s','U')) === false) {
|
||||
user_error('formattedDatetimeClause(): unsupported format character %' . $match, E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
$translate = array(
|
||||
'/%Y/' => 'YYYY',
|
||||
'/%m/' => 'MM',
|
||||
'/%d/' => 'DD',
|
||||
'/%H/' => 'HH24',
|
||||
'/%i/' => 'MI',
|
||||
'/%s/' => 'SS',
|
||||
);
|
||||
$format = preg_replace(array_keys($translate), array_values($translate), $format);
|
||||
|
||||
if (preg_match('/^now$/i', $date)) {
|
||||
$date = "NOW()";
|
||||
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
|
||||
$date = "TIMESTAMP '$date'";
|
||||
}
|
||||
|
||||
if ($format == '%U') {
|
||||
return "FLOOR(EXTRACT(epoch FROM $date))";
|
||||
}
|
||||
|
||||
return "to_char($date, TEXT '$format')";
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with Postgres
|
||||
* used for querying a datetime addition
|
||||
* @param string $date, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @param string $interval to be added, use the format [sign][integer] [qualifier], e.g. -1 Day, +15 minutes, +1 YEAR
|
||||
* supported qualifiers:
|
||||
* - years
|
||||
* - months
|
||||
* - days
|
||||
* - hours
|
||||
* - minutes
|
||||
* - seconds
|
||||
* This includes the singular forms as well
|
||||
* @return string SQL datetime expression to query for a datetime (YYYY-MM-DD hh:mm:ss) which is the result of the addition
|
||||
*/
|
||||
public function datetimeIntervalClause($date, $interval)
|
||||
{
|
||||
if (preg_match('/^now$/i', $date)) {
|
||||
$date = "NOW()";
|
||||
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
|
||||
$date = "TIMESTAMP '$date'";
|
||||
}
|
||||
|
||||
// ... when being too precise becomes a pain. we need to cut of the fractions.
|
||||
// TIMESTAMP(0) doesn't work because it rounds instead flooring
|
||||
return "CAST(SUBSTRING(CAST($date + INTERVAL '$interval' AS VARCHAR) FROM 1 FOR 19) AS TIMESTAMP)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with Postgres
|
||||
* used for querying a datetime substraction
|
||||
* @param string $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @param string $date2 to be substracted of $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"'
|
||||
* @return string SQL datetime expression to query for the interval between $date1 and $date2 in seconds which is the result of the substraction
|
||||
*/
|
||||
public function datetimeDifferenceClause($date1, $date2)
|
||||
{
|
||||
if (preg_match('/^now$/i', $date1)) {
|
||||
$date1 = "NOW()";
|
||||
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date1)) {
|
||||
$date1 = "TIMESTAMP '$date1'";
|
||||
}
|
||||
|
||||
if (preg_match('/^now$/i', $date2)) {
|
||||
$date2 = "NOW()";
|
||||
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date2)) {
|
||||
$date2 = "TIMESTAMP '$date2'";
|
||||
}
|
||||
|
||||
return "(FLOOR(EXTRACT(epoch FROM $date1)) - FLOOR(EXTRACT(epoch from $date2)))";
|
||||
}
|
||||
|
||||
public function now()
|
||||
{
|
||||
return 'NOW()';
|
||||
}
|
||||
|
||||
public function random()
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the name of the current database to be reported externally
|
||||
* by substituting the schema name for the database name.
|
||||
* Should only be used when model_schema_as_database is true
|
||||
*
|
||||
* @param string $schema Name of the schema
|
||||
* @return string Name of the database to report
|
||||
*/
|
||||
public function schemaToDatabaseName($schema)
|
||||
{
|
||||
switch ($schema) {
|
||||
case $this->schemaOriginal:
|
||||
return $this->databaseOriginal;
|
||||
default:
|
||||
return $schema;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a requested database name to a schema name to substitute internally.
|
||||
* Should only be used when model_schema_as_database is true
|
||||
*
|
||||
* @param string $database Name of the database
|
||||
* @return string Name of the schema to use for this database internally
|
||||
*/
|
||||
public function databaseToSchemaName($database)
|
||||
{
|
||||
switch ($database) {
|
||||
case $this->databaseOriginal:
|
||||
return $this->schemaOriginal;
|
||||
default:
|
||||
return $database;
|
||||
}
|
||||
}
|
||||
|
||||
public function dropSelectedDatabase()
|
||||
{
|
||||
if (self::model_schema_as_database()) {
|
||||
// Check current schema is valid
|
||||
$oldSchema = $this->schema;
|
||||
if (empty($oldSchema)) {
|
||||
return;
|
||||
} // Nothing selected to drop
|
||||
|
||||
// Select another schema
|
||||
if ($oldSchema !== $this->schemaOriginal) {
|
||||
$this->setSchema($this->schemaOriginal);
|
||||
} elseif ($oldSchema !== self::MASTER_SCHEMA) {
|
||||
$this->setSchema(self::MASTER_SCHEMA);
|
||||
} else {
|
||||
$this->schema = null;
|
||||
}
|
||||
|
||||
// Remove this schema
|
||||
$this->schemaManager->dropSchema($oldSchema);
|
||||
} else {
|
||||
parent::dropSelectedDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
public function getSelectedDatabase()
|
||||
{
|
||||
if (self::model_schema_as_database()) {
|
||||
return $this->schemaToDatabaseName($this->schema);
|
||||
}
|
||||
return parent::getSelectedDatabase();
|
||||
}
|
||||
|
||||
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR)
|
||||
{
|
||||
// Substitute schema here as appropriate
|
||||
if (self::model_schema_as_database()) {
|
||||
// Selecting the database itself should be treated as selecting the public schema
|
||||
$schemaName = $this->databaseToSchemaName($name);
|
||||
return $this->setSchema($schemaName, $create, $errorLevel);
|
||||
}
|
||||
|
||||
// Database selection requires that a new connection is established.
|
||||
// This is not ideal postgres practise
|
||||
if (!$this->schemaManager->databaseExists($name)) {
|
||||
// Check DB creation permisson
|
||||
if (!$create) {
|
||||
if ($errorLevel !== false) {
|
||||
user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel);
|
||||
}
|
||||
// Unselect database
|
||||
$this->connector->unloadDatabase();
|
||||
return false;
|
||||
}
|
||||
$this->schemaManager->createDatabase($name);
|
||||
}
|
||||
|
||||
// New connection made here, treating the new database name as the new original
|
||||
$this->databaseOriginal = $name;
|
||||
$this->connectDefault();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all entries from the table instead of truncating it.
|
||||
*
|
||||
* This gives a massive speed improvement compared to using TRUNCATE, with
|
||||
* the caveat that primary keys are not reset etc.
|
||||
*
|
||||
* @see DatabaseAdmin::clearAllData()
|
||||
*
|
||||
* @param string $table
|
||||
*/
|
||||
public function clearTable($table)
|
||||
{
|
||||
$this->query('DELETE FROM "'.$table.'";');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A result-set from a PostgreSQL database.
|
||||
* @package sapphire
|
||||
* @subpackage model
|
||||
*/
|
||||
class PostgreSQLQuery extends SS_Query {
|
||||
/**
|
||||
* The MySQLDatabase object that created this result set.
|
||||
* @var PostgreSQLDatabase
|
||||
*/
|
||||
private $database;
|
||||
|
||||
/**
|
||||
* The internal Postgres handle that points to the result set.
|
||||
* @var resource
|
||||
*/
|
||||
private $handle;
|
||||
|
||||
/**
|
||||
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
||||
* @param database The database object that created this query.
|
||||
* @param handle the internal Postgres handle that is points to the resultset.
|
||||
*/
|
||||
public function __construct(PostgreSQLDatabase $database, $handle) {
|
||||
$this->database = $database;
|
||||
$this->handle = $handle;
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
if(is_resource($this->handle)) pg_free_result($this->handle);
|
||||
}
|
||||
|
||||
public function seek($row) {
|
||||
return pg_result_seek($this->handle, $row);
|
||||
}
|
||||
|
||||
public function numRecords() {
|
||||
return pg_num_rows($this->handle);
|
||||
}
|
||||
|
||||
public function nextRecord() {
|
||||
// Coalesce rather than replace common fields.
|
||||
if($data = pg_fetch_row($this->handle)) {
|
||||
foreach($data as $columnIdx => $value) {
|
||||
$columnName = pg_field_name($this->handle, $columnIdx);
|
||||
// $value || !$ouput[$columnName] means that the *last* occurring value is shown
|
||||
// !$ouput[$columnName] means that the *first* occurring value is shown
|
||||
if(isset($value) || !isset($output[$columnName])) {
|
||||
$output[$columnName] = $value;
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,165 +1,194 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL;
|
||||
|
||||
use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
|
||||
use SilverStripe\Dev\Install\DatabaseConfigurationHelper;
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* This is a helper class for the SS installer.
|
||||
*
|
||||
*
|
||||
* It does all the specific checking for PostgreSQLDatabase
|
||||
* to ensure that the configuration is setup correctly.
|
||||
*
|
||||
*
|
||||
* @package postgresql
|
||||
*/
|
||||
class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
|
||||
class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper
|
||||
{
|
||||
/**
|
||||
* Create a connection of the appropriate type
|
||||
*
|
||||
* @skipUpgrade
|
||||
* @param array $databaseConfig
|
||||
* @param string $error Error message passed by value
|
||||
* @return mixed|null Either the connection object, or null if error
|
||||
*/
|
||||
protected function createConnection($databaseConfig, &$error)
|
||||
{
|
||||
$error = null;
|
||||
$username = empty($databaseConfig['username']) ? '' : $databaseConfig['username'];
|
||||
$password = empty($databaseConfig['password']) ? '' : $databaseConfig['password'];
|
||||
$server = $databaseConfig['server'];
|
||||
|
||||
/**
|
||||
* Ensure that the database function pg_connect
|
||||
* is available. If it is, we assume the PHP module for this
|
||||
* database has been setup correctly.
|
||||
*
|
||||
* @param array $databaseConfig Associative array of database configuration, e.g. "server", "username" etc
|
||||
* @return boolean
|
||||
*/
|
||||
public function requireDatabaseFunctions($databaseConfig) {
|
||||
return (function_exists('pg_connect')) ? true : false;
|
||||
}
|
||||
try {
|
||||
switch ($databaseConfig['type']) {
|
||||
case 'PostgreSQLDatabase':
|
||||
$userPart = $username ? " user=$username" : '';
|
||||
$passwordPart = $password ? " password=$password" : '';
|
||||
$connstring = "host=$server port=5432 dbname=postgres{$userPart}{$passwordPart}";
|
||||
$conn = pg_connect($connstring);
|
||||
break;
|
||||
case 'PostgrePDODatabase':
|
||||
// May throw a PDOException if fails
|
||||
$conn = @new PDO('postgresql:host='.$server.';dbname=postgres;port=5432', $username, $password);
|
||||
break;
|
||||
default:
|
||||
$error = 'Invalid connection type: ' . $databaseConfig['type'];
|
||||
return null;
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
$error = $ex->getMessage();
|
||||
return null;
|
||||
}
|
||||
if ($conn) {
|
||||
return $conn;
|
||||
} else {
|
||||
$error = 'PostgreSQL requires a valid username and password to determine if the server exists.';
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the database server exists.
|
||||
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
||||
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
||||
*/
|
||||
public function requireDatabaseServer($databaseConfig) {
|
||||
$success = false;
|
||||
$error = '';
|
||||
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
|
||||
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
|
||||
$server = $databaseConfig['server'];
|
||||
$userPart = $username ? " user=$username" : '';
|
||||
$passwordPart = $password ? " password=$password" : '';
|
||||
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
||||
public function requireDatabaseFunctions($databaseConfig)
|
||||
{
|
||||
$data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']);
|
||||
return !empty($data['supported']);
|
||||
}
|
||||
|
||||
$conn = @pg_connect($connstring);
|
||||
if($conn) {
|
||||
$success = true;
|
||||
} else {
|
||||
$success = false;
|
||||
$error = 'PostgreSQL requires a valid username and password to determine if the server exists.';
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => $success,
|
||||
'error' => $error
|
||||
);
|
||||
}
|
||||
public function requireDatabaseServer($databaseConfig)
|
||||
{
|
||||
$conn = $this->createConnection($databaseConfig, $error);
|
||||
$success = !empty($conn);
|
||||
return array(
|
||||
'success' => $success,
|
||||
'error' => $error
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a database connection is possible using credentials provided.
|
||||
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
||||
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
||||
*/
|
||||
public function requireDatabaseConnection($databaseConfig) {
|
||||
$success = false;
|
||||
$error = '';
|
||||
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
|
||||
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
|
||||
$server = $databaseConfig['server'];
|
||||
$userPart = $username ? " user=$username" : '';
|
||||
$passwordPart = $password ? " password=$password" : '';
|
||||
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
||||
|
||||
$conn = @pg_connect($connstring);
|
||||
if($conn) {
|
||||
$success = true;
|
||||
} else {
|
||||
$success = false;
|
||||
$error = '';
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => $success,
|
||||
'connection' => $conn,
|
||||
'error' => $error
|
||||
);
|
||||
}
|
||||
public function requireDatabaseConnection($databaseConfig)
|
||||
{
|
||||
$conn = $this->createConnection($databaseConfig, $error);
|
||||
$success = !empty($conn);
|
||||
return array(
|
||||
'success' => $success,
|
||||
'connection' => $conn,
|
||||
'error' => $error
|
||||
);
|
||||
}
|
||||
|
||||
public function getDatabaseVersion($databaseConfig) {
|
||||
$version = 0;
|
||||
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
|
||||
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
|
||||
$server = $databaseConfig['server'];
|
||||
$userPart = $username ? " user=$username" : '';
|
||||
$passwordPart = $password ? " password=$password" : '';
|
||||
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
||||
$conn = @pg_connect($connstring);
|
||||
$info = @pg_version($conn);
|
||||
$version = ($info && isset($info['server'])) ? $info['server'] : null;
|
||||
if(!$version) {
|
||||
// fallback to using the version() function
|
||||
$result = @pg_query($conn, "SELECT version()");
|
||||
$row = @pg_fetch_array($result);
|
||||
public function getDatabaseVersion($databaseConfig)
|
||||
{
|
||||
$conn = $this->createConnection($databaseConfig, $error);
|
||||
if (!$conn) {
|
||||
return false;
|
||||
} elseif ($conn instanceof PDO) {
|
||||
return $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
|
||||
} elseif (is_resource($conn)) {
|
||||
$info = pg_version($conn);
|
||||
return $info['server'];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if($row && isset($row[0])) {
|
||||
$parts = explode(' ', trim($row[0]));
|
||||
// ASSUMPTION version number is the second part e.g. "PostgreSQL 8.4.3"
|
||||
$version = trim($parts[1]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Ensure that the PostgreSQL version is at least 8.3.
|
||||
*
|
||||
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
||||
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
||||
*/
|
||||
public function requireDatabaseVersion($databaseConfig)
|
||||
{
|
||||
$success = false;
|
||||
$error = '';
|
||||
$version = $this->getDatabaseVersion($databaseConfig);
|
||||
|
||||
return $version;
|
||||
}
|
||||
if ($version) {
|
||||
$success = version_compare($version, '8.3', '>=');
|
||||
if (!$success) {
|
||||
$error = "Your PostgreSQL version is $version. It's recommended you use at least 8.3.";
|
||||
}
|
||||
} else {
|
||||
$error = "Your PostgreSQL version could not be determined.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the PostgreSQL version is at least 8.3.
|
||||
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
||||
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
||||
*/
|
||||
public function requireDatabaseVersion($databaseConfig) {
|
||||
$success = false;
|
||||
$error = '';
|
||||
$version = $this->getDatabaseVersion($databaseConfig);
|
||||
return array(
|
||||
'success' => $success,
|
||||
'error' => $error
|
||||
);
|
||||
}
|
||||
|
||||
if($version) {
|
||||
$success = version_compare($version, '8.3', '>=');
|
||||
if(!$success) {
|
||||
$error = "Your PostgreSQL version is $version. It's recommended you use at least 8.3.";
|
||||
}
|
||||
} else {
|
||||
$error = "Your PostgreSQL version could not be determined.";
|
||||
}
|
||||
/**
|
||||
* Helper function to execute a query
|
||||
*
|
||||
* @param mixed $conn Connection object/resource
|
||||
* @param string $sql SQL string to execute
|
||||
* @return array List of first value from each resulting row
|
||||
*/
|
||||
protected function query($conn, $sql)
|
||||
{
|
||||
$items = array();
|
||||
if ($conn instanceof PDO) {
|
||||
foreach ($conn->query($sql) as $row) {
|
||||
$items[] = $row[0];
|
||||
}
|
||||
} elseif (is_resource($conn)) {
|
||||
$result = pg_query($conn, $sql);
|
||||
while ($row = pg_fetch_row($result)) {
|
||||
$items[] = $row[0];
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => $success,
|
||||
'error' => $error
|
||||
);
|
||||
}
|
||||
public function requireDatabaseOrCreatePermissions($databaseConfig)
|
||||
{
|
||||
$success = false;
|
||||
$alreadyExists = false;
|
||||
$conn = $this->createConnection($databaseConfig, $error);
|
||||
if ($conn) {
|
||||
// Check if db already exists
|
||||
$existingDatabases = $this->query($conn, "SELECT datname FROM pg_database");
|
||||
$alreadyExists = in_array($databaseConfig['database'], $existingDatabases);
|
||||
if ($alreadyExists) {
|
||||
$success = true;
|
||||
} else {
|
||||
// Check if this user has create privileges
|
||||
$allowedUsers = $this->query($conn, "select rolname from pg_authid where rolcreatedb = true;");
|
||||
$success = in_array($databaseConfig['username'], $allowedUsers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the database connection is able to use an existing database,
|
||||
* or be able to create one if it doesn't exist.
|
||||
*
|
||||
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
||||
* @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true')
|
||||
*/
|
||||
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
||||
$success = false;
|
||||
$alreadyExists = false;
|
||||
$check = $this->requireDatabaseConnection($databaseConfig);
|
||||
$conn = $check['connection'];
|
||||
|
||||
$result = pg_query($conn, "SELECT datname FROM pg_database WHERE datname = '$databaseConfig[database]'");
|
||||
if(pg_fetch_array($result)) {
|
||||
$success = true;
|
||||
$alreadyExists = true;
|
||||
} else {
|
||||
if(@pg_query($conn, "CREATE DATABASE testing123")) {
|
||||
pg_query($conn, "DROP DATABASE testing123");
|
||||
$success = true;
|
||||
$alreadyExists = false;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => $success,
|
||||
'alreadyExists' => $alreadyExists
|
||||
);
|
||||
}
|
||||
return array(
|
||||
'success' => $success,
|
||||
'alreadyExists' => $alreadyExists
|
||||
);
|
||||
}
|
||||
|
||||
public function requireDatabaseAlterPermissions($databaseConfig)
|
||||
{
|
||||
$conn = $this->createConnection($databaseConfig, $error);
|
||||
if ($conn) {
|
||||
// if the account can even log in, it can alter tables
|
||||
return array(
|
||||
'success' => true,
|
||||
'applies' => true
|
||||
);
|
||||
}
|
||||
return array(
|
||||
'success' => false,
|
||||
'applies' => true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL;
|
||||
|
||||
use SilverStripe\ORM\Connect\Query;
|
||||
|
||||
/**
|
||||
* A result-set from a PostgreSQL database.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage model
|
||||
*/
|
||||
class PostgreSQLQuery extends Query
|
||||
{
|
||||
/**
|
||||
* The internal Postgres handle that points to the result set.
|
||||
* @var resource
|
||||
*/
|
||||
private $handle;
|
||||
|
||||
private $columnNames = [];
|
||||
|
||||
/**
|
||||
* Mapping of postgresql types to PHP types
|
||||
* Note that the bool => int mapping is by design, designed to mimic MySQL's behaviour
|
||||
* @var array
|
||||
*/
|
||||
protected static $typeMapping = [
|
||||
'bool' => 'int',
|
||||
'int2' => 'int',
|
||||
'int4' => 'int',
|
||||
'int8' => 'int',
|
||||
'float4' => 'float',
|
||||
'float8' => 'float',
|
||||
'numeric' => 'float',
|
||||
];
|
||||
|
||||
/**
|
||||
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
||||
* @param resource $handle the internal Postgres handle that is points to the resultset.
|
||||
*/
|
||||
public function __construct($handle)
|
||||
{
|
||||
$this->handle = $handle;
|
||||
|
||||
$numColumns = pg_num_fields($handle);
|
||||
for ($i = 0; $i<$numColumns; $i++) {
|
||||
$this->columnNames[$i] = pg_field_name($handle, $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (is_resource($this->handle)) {
|
||||
pg_free_result($this->handle);
|
||||
}
|
||||
}
|
||||
|
||||
public function seek($row)
|
||||
{
|
||||
// Specifying the zero-th record here will reset the pointer
|
||||
$result = pg_fetch_array($this->handle, $row, PGSQL_NUM);
|
||||
|
||||
return $this->parseResult($result);
|
||||
}
|
||||
|
||||
public function numRecords()
|
||||
{
|
||||
return pg_num_rows($this->handle);
|
||||
}
|
||||
|
||||
public function nextRecord()
|
||||
{
|
||||
$row = pg_fetch_array($this->handle, null, PGSQL_NUM);
|
||||
|
||||
// Correct non-string types
|
||||
if ($row) {
|
||||
return $this->parseResult($row);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $row
|
||||
* @return array
|
||||
*/
|
||||
protected function parseResult(array $row)
|
||||
{
|
||||
$record = [];
|
||||
|
||||
foreach ($row as $i => $v) {
|
||||
$k = $this->columnNames[$i];
|
||||
$record[$k] = $v;
|
||||
$type = pg_field_type($this->handle, $i);
|
||||
if (isset(self::$typeMapping[$type])) {
|
||||
if ($type === 'bool' && $record[$k] === 't') {
|
||||
$record[$k] = 1;
|
||||
|
||||
// Note that boolean 'f' will be converted to 0 by this
|
||||
} else {
|
||||
settype($record[$k], self::$typeMapping[$type]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL;
|
||||
|
||||
use SilverStripe\ORM\Queries\SQLConditionalExpression;
|
||||
use SilverStripe\ORM\Queries\SQLExpression;
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
use SilverStripe\ORM\Connect\DBQueryBuilder;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class PostgreSQLQueryBuilder extends DBQueryBuilder
|
||||
{
|
||||
/**
|
||||
* Max table length.
|
||||
* Aliases longer than this will be re-written
|
||||
*/
|
||||
const MAX_TABLE = 63;
|
||||
|
||||
/**
|
||||
* Return the LIMIT clause ready for inserting into a query.
|
||||
*
|
||||
* @param SQLSelect $query The expression object to build from
|
||||
* @param array $parameters Out parameter for the resulting query parameters
|
||||
* @return string The finalised limit SQL fragment
|
||||
*/
|
||||
public function buildLimitFragment(SQLSelect $query, array &$parameters)
|
||||
{
|
||||
$nl = $this->getSeparator();
|
||||
|
||||
// Ensure limit is given
|
||||
$limit = $query->getLimit();
|
||||
if (empty($limit)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// For literal values return this as the limit SQL
|
||||
if (! is_array($limit)) {
|
||||
return "{$nl}LIMIT $limit";
|
||||
}
|
||||
|
||||
// Assert that the array version provides the 'limit' key
|
||||
if (! array_key_exists('limit', $limit) || ($limit['limit'] !== null && ! is_numeric($limit['limit']))) {
|
||||
throw new InvalidArgumentException(
|
||||
'DBQueryBuilder::buildLimitSQL(): Wrong format for $limit: '. var_export($limit, true)
|
||||
);
|
||||
}
|
||||
|
||||
if ($limit['limit'] === null) {
|
||||
$limit['limit'] = 'ALL';
|
||||
}
|
||||
|
||||
$clause = "{$nl}LIMIT {$limit['limit']}";
|
||||
if (isset($limit['start']) && is_numeric($limit['start']) && $limit['start'] !== 0) {
|
||||
$clause .= " OFFSET {$limit['start']}";
|
||||
}
|
||||
return $clause;
|
||||
}
|
||||
|
||||
public function buildSQL(SQLExpression $query, &$parameters)
|
||||
{
|
||||
$sql = parent::buildSQL($query, $parameters);
|
||||
return $this->rewriteLongIdentifiers($query, $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and generate table aliases necessary in the given query
|
||||
*
|
||||
* @param SQLConditionalExpression $query
|
||||
* @return array List of replacements
|
||||
*/
|
||||
protected function findRewrites(SQLConditionalExpression $query)
|
||||
{
|
||||
$rewrites = [];
|
||||
foreach ($query->getFrom() as $alias => $from) {
|
||||
$table = is_array($from) ? $from['table'] : $from;
|
||||
if ($alias === $table || "\"{$alias}\"" === $table) {
|
||||
continue;
|
||||
}
|
||||
// Don't complain about aliases shorter than max length
|
||||
if (strlen($alias) <= self::MAX_TABLE) {
|
||||
continue;
|
||||
}
|
||||
$replacement = substr(sha1($alias), 0, 7) . '_' . substr($alias, 8 - self::MAX_TABLE);
|
||||
$rewrites["\"{$alias}\""] = "\"{$replacement}\"";
|
||||
}
|
||||
return $rewrites;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite all ` AS "Identifier"` with strlen(Identifier) > 63
|
||||
*
|
||||
* @param SQLExpression $query
|
||||
* @param string $sql
|
||||
* @return string
|
||||
*/
|
||||
protected function rewriteLongIdentifiers(SQLExpression $query, $sql)
|
||||
{
|
||||
// Check if this query has aliases
|
||||
if ($query instanceof SQLConditionalExpression) {
|
||||
$rewrites = $this->findRewrites($query);
|
||||
if ($rewrites) {
|
||||
return str_replace(array_keys($rewrites), array_values($rewrites), $sql);
|
||||
}
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,1513 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL;
|
||||
|
||||
use SilverStripe\Dev\Deprecation;
|
||||
use SilverStripe\ORM\Connect\DBSchemaManager;
|
||||
use SilverStripe\ORM\DB;
|
||||
|
||||
/**
|
||||
* PostgreSQL schema manager
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage model
|
||||
*/
|
||||
class PostgreSQLSchemaManager extends DBSchemaManager
|
||||
{
|
||||
/**
|
||||
* Identifier for this schema, used for configuring schema-specific table
|
||||
* creation options
|
||||
*/
|
||||
const ID = 'PostgreSQL';
|
||||
|
||||
/**
|
||||
* Instance of the database controller this schema belongs to
|
||||
*
|
||||
* @var PostgreSQLDatabase
|
||||
*/
|
||||
protected $database = null;
|
||||
|
||||
/**
|
||||
* This holds a copy of all the constraint results that are returned
|
||||
* via the function constraintExists(). This is a bit faster than
|
||||
* repeatedly querying this column, and should allow the database
|
||||
* to use it's built-in caching features for better queries.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $cached_constraints = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* This holds a copy of all the queries that run through the function fieldList()
|
||||
* This is one of the most-often called functions, and repeats itself a great deal in the unit tests.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $cached_fieldlists = array();
|
||||
|
||||
protected function indexKey($table, $index, $spec)
|
||||
{
|
||||
return $this->buildPostgresIndexName($table, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a postgres database, ignoring model_schema_as_database
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function createPostgresDatabase($name)
|
||||
{
|
||||
$this->query("CREATE DATABASE \"$name\";");
|
||||
}
|
||||
|
||||
public function createDatabase($name)
|
||||
{
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemaName = $this->database->databaseToSchemaName($name);
|
||||
return $this->createSchema($schemaName);
|
||||
}
|
||||
return $this->createPostgresDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a postgres database exists, ignoring model_schema_as_database
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function postgresDatabaseExists($name)
|
||||
{
|
||||
$result = $this->preparedQuery("SELECT datname FROM pg_database WHERE datname = ?;", array($name));
|
||||
return $result->first() ? true : false;
|
||||
}
|
||||
|
||||
public function databaseExists($name)
|
||||
{
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemaName = $this->database->databaseToSchemaName($name);
|
||||
return $this->schemaExists($schemaName);
|
||||
}
|
||||
return $this->postgresDatabaseExists($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the list of all postgres databases, ignoring model_schema_as_database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function postgresDatabaseList()
|
||||
{
|
||||
return $this->query("SELECT datname FROM pg_database WHERE datistemplate=false;")->column();
|
||||
}
|
||||
|
||||
public function databaseList()
|
||||
{
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemas = $this->schemaList();
|
||||
$names = array();
|
||||
foreach ($schemas as $schema) {
|
||||
$names[] = $this->database->schemaToDatabaseName($schema);
|
||||
}
|
||||
return array_unique($names);
|
||||
}
|
||||
return $this->postgresDatabaseList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a postgres database, ignoring model_schema_as_database
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function dropPostgresDatabase($name)
|
||||
{
|
||||
$nameSQL = $this->database->escapeIdentifier($name);
|
||||
$this->query("DROP DATABASE $nameSQL;");
|
||||
}
|
||||
|
||||
public function dropDatabase($name)
|
||||
{
|
||||
if (PostgreSQLDatabase::model_schema_as_database()) {
|
||||
$schemaName = $this->database->databaseToSchemaName($name);
|
||||
$this->dropSchema($schemaName);
|
||||
return;
|
||||
}
|
||||
$this->dropPostgresDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the schema exists in the current database
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function schemaExists($name)
|
||||
{
|
||||
return $this->preparedQuery(
|
||||
"SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname = ?;",
|
||||
array($name)
|
||||
)->first() ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a schema in the current database
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function createSchema($name)
|
||||
{
|
||||
$nameSQL = $this->database->escapeIdentifier($name);
|
||||
$this->query("CREATE SCHEMA $nameSQL;");
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a schema from the database. Use carefully!
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function dropSchema($name)
|
||||
{
|
||||
$nameSQL = $this->database->escapeIdentifier($name);
|
||||
$this->query("DROP SCHEMA $nameSQL CASCADE;");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of all available schemas on the current database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function schemaList()
|
||||
{
|
||||
return $this->query(
|
||||
"SELECT nspname
|
||||
FROM pg_catalog.pg_namespace
|
||||
WHERE nspname <> 'information_schema' AND nspname !~ E'^pg_'"
|
||||
)->column();
|
||||
}
|
||||
|
||||
public function createTable($table, $fields = null, $indexes = null, $options = null, $advancedOptions = null)
|
||||
{
|
||||
$fieldSchemas = "";
|
||||
if ($fields) {
|
||||
foreach ($fields as $k => $v) {
|
||||
$fieldSchemas .= "\"$k\" $v,\n";
|
||||
}
|
||||
}
|
||||
if (!empty($options[self::ID])) {
|
||||
$addOptions = $options[self::ID];
|
||||
} else {
|
||||
$addOptions = null;
|
||||
}
|
||||
|
||||
//First of all, does this table already exist
|
||||
$doesExist = $this->hasTable($table);
|
||||
if ($doesExist) {
|
||||
// Table already exists, just return the name, in line with baseclass documentation.
|
||||
return $table;
|
||||
}
|
||||
|
||||
//If we have a fulltext search request, then we need to create a special column
|
||||
//for GiST searches
|
||||
$fulltexts = '';
|
||||
$triggers = [];
|
||||
if ($indexes) {
|
||||
foreach ($indexes as $name => $this_index) {
|
||||
if (is_array($this_index) && $this_index['type'] == 'fulltext') {
|
||||
$ts_details = $this->fulltext($this_index, $table, $name);
|
||||
$fulltexts .= $ts_details['fulltexts'] . ', ';
|
||||
$triggers[] = $ts_details['triggers'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$indexQueries = [];
|
||||
if ($indexes) {
|
||||
foreach ($indexes as $k => $v) {
|
||||
$indexQueries[] = $this->getIndexSqlDefinition($table, $k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if ($advancedOptions && isset($advancedOptions['tablespace'])) {
|
||||
$this->createOrReplaceTablespace(
|
||||
$advancedOptions['tablespace']['name'],
|
||||
$advancedOptions['tablespace']['location']
|
||||
);
|
||||
$tableSpace = ' TABLESPACE ' . $advancedOptions['tablespace']['name'];
|
||||
} else {
|
||||
$tableSpace = '';
|
||||
}
|
||||
|
||||
$this->query(
|
||||
"CREATE TABLE \"$table\" (
|
||||
$fieldSchemas
|
||||
$fulltexts
|
||||
primary key (\"ID\")
|
||||
)$tableSpace $addOptions"
|
||||
);
|
||||
foreach ($indexQueries as $indexQuery) {
|
||||
$this->query($indexQuery);
|
||||
}
|
||||
|
||||
foreach ($triggers as $trigger) {
|
||||
$this->query($trigger);
|
||||
}
|
||||
|
||||
//If we have a partitioning requirement, we do that here:
|
||||
if ($advancedOptions && isset($advancedOptions['partitions'])) {
|
||||
$this->createOrReplacePartition($table, $advancedOptions['partitions'], $indexes, $advancedOptions);
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if ($advancedOptions && isset($advancedOptions['cluster'])) {
|
||||
$this->query("CLUSTER \"$table\" USING \"{$advancedOptions['cluster']}\"");
|
||||
}
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the internal Postgres index name given the silverstripe table and index name
|
||||
*
|
||||
* @param string $tableName
|
||||
* @param string $indexName
|
||||
* @param string $prefix The optional prefix for the index. Defaults to "ix" for indexes.
|
||||
* @return string The postgres name of the index
|
||||
*/
|
||||
protected function buildPostgresIndexName($tableName, $indexName, $prefix = 'ix')
|
||||
{
|
||||
|
||||
// Assume all indexes also contain the table name
|
||||
// MD5 the table/index name combo to keep it to a fixed length.
|
||||
// Exclude the prefix so that the trigger name can be easily generated from the index name
|
||||
$indexNamePG = "{$prefix}_" . md5("{$tableName}_{$indexName}");
|
||||
|
||||
// Limit to 63 characters
|
||||
if (strlen($indexNamePG) > 63) {
|
||||
return substr($indexNamePG, 0, 63);
|
||||
} else {
|
||||
return $indexNamePG;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the internal Postgres trigger name given the silverstripe table and trigger name
|
||||
*
|
||||
* @param string $tableName
|
||||
* @param string $triggerName
|
||||
* @return string The postgres name of the trigger
|
||||
*/
|
||||
public function buildPostgresTriggerName($tableName, $triggerName)
|
||||
{
|
||||
// Kind of cheating, but behaves the same way as indexes
|
||||
return $this->buildPostgresIndexName($tableName, $triggerName, 'ts');
|
||||
}
|
||||
|
||||
public function alterTable(
|
||||
$table,
|
||||
$newFields = null,
|
||||
$newIndexes = null,
|
||||
$alteredFields = null,
|
||||
$alteredIndexes = null,
|
||||
$alteredOptions = null,
|
||||
$advancedOptions = null
|
||||
) {
|
||||
$alterList = [];
|
||||
if ($newFields) {
|
||||
foreach ($newFields as $fieldName => $fieldSpec) {
|
||||
$alterList[] = "ADD \"$fieldName\" $fieldSpec";
|
||||
}
|
||||
}
|
||||
|
||||
if ($alteredFields) {
|
||||
foreach ($alteredFields as $indexName => $indexSpec) {
|
||||
$val = $this->alterTableAlterColumn($table, $indexName, $indexSpec);
|
||||
if (!empty($val)) {
|
||||
$alterList[] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Do we need to do anything with the tablespaces?
|
||||
if ($alteredOptions && isset($advancedOptions['tablespace'])) {
|
||||
$this->createOrReplaceTablespace(
|
||||
$advancedOptions['tablespace']['name'],
|
||||
$advancedOptions['tablespace']['location']
|
||||
);
|
||||
$this->query("ALTER TABLE \"$table\" SET TABLESPACE {$advancedOptions['tablespace']['name']};");
|
||||
}
|
||||
|
||||
//DB ABSTRACTION: we need to change the constraints to be a separate 'add' command,
|
||||
//see http://www.postgresql.org/docs/8.1/static/sql-altertable.html
|
||||
$alterIndexList = [];
|
||||
//Pick up the altered indexes here:
|
||||
$fieldList = $this->fieldList($table);
|
||||
$fulltexts = [];
|
||||
$dropTriggers = [];
|
||||
$triggers = [];
|
||||
if ($alteredIndexes) {
|
||||
foreach ($alteredIndexes as $indexName => $indexSpec) {
|
||||
$indexNamePG = $this->buildPostgresIndexName($table, $indexName);
|
||||
|
||||
if ($indexSpec['type'] == 'fulltext') {
|
||||
//For full text indexes, we need to drop the trigger, drop the index, AND drop the column
|
||||
|
||||
//Go and get the tsearch details:
|
||||
$ts_details = $this->fulltext($indexSpec, $table, $indexName);
|
||||
|
||||
//Drop this column if it already exists:
|
||||
|
||||
//No IF EXISTS option is available for Postgres <9.0
|
||||
if (array_key_exists($ts_details['ts_name'], $fieldList)) {
|
||||
$fulltexts[] = "ALTER TABLE \"{$table}\" DROP COLUMN \"{$ts_details['ts_name']}\";";
|
||||
}
|
||||
|
||||
// We'll execute these later:
|
||||
$triggerNamePG = $this->buildPostgresTriggerName($table, $indexName);
|
||||
$dropTriggers[] = "DROP TRIGGER IF EXISTS \"$triggerNamePG\" ON \"$table\";";
|
||||
$fulltexts[] = "ALTER TABLE \"{$table}\" ADD COLUMN {$ts_details['fulltexts']};";
|
||||
$triggers[] = $ts_details['triggers'];
|
||||
}
|
||||
|
||||
// Create index action (including fulltext)
|
||||
$alterIndexList[] = "DROP INDEX IF EXISTS \"$indexNamePG\";";
|
||||
$createIndex = $this->getIndexSqlDefinition($table, $indexName, $indexSpec);
|
||||
if ($createIndex) {
|
||||
$alterIndexList[] = $createIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Add the new indexes:
|
||||
if ($newIndexes) {
|
||||
foreach ($newIndexes as $indexName => $indexSpec) {
|
||||
$indexNamePG = $this->buildPostgresIndexName($table, $indexName);
|
||||
//If we have a fulltext search request, then we need to create a special column
|
||||
//for GiST searches
|
||||
//Pick up the new indexes here:
|
||||
if ($indexSpec['type'] == 'fulltext') {
|
||||
$ts_details = $this->fulltext($indexSpec, $table, $indexName);
|
||||
if (!isset($fieldList[$ts_details['ts_name']])) {
|
||||
$fulltexts[] = "ALTER TABLE \"{$table}\" ADD COLUMN {$ts_details['fulltexts']};";
|
||||
$triggers[] = $ts_details['triggers'];
|
||||
}
|
||||
}
|
||||
|
||||
//Check that this index doesn't already exist:
|
||||
$indexes = $this->indexList($table);
|
||||
if (isset($indexes[$indexName])) {
|
||||
$alterIndexList[] = "DROP INDEX IF EXISTS \"$indexNamePG\";";
|
||||
}
|
||||
|
||||
$createIndex = $this->getIndexSqlDefinition($table, $indexName, $indexSpec);
|
||||
if ($createIndex) {
|
||||
$alterIndexList[] = $createIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($alterList) {
|
||||
$alterations = implode(",\n", $alterList);
|
||||
$this->query("ALTER TABLE \"$table\" " . $alterations);
|
||||
}
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if ($advancedOptions && isset($advancedOptions['extensions']['tablespace'])) {
|
||||
$extensions = $advancedOptions['extensions'];
|
||||
$this->createOrReplaceTablespace($extensions['tablespace']['name'], $extensions['tablespace']['location']);
|
||||
}
|
||||
|
||||
if ($alteredOptions && isset($this->class) && isset($alteredOptions[$this->class])) {
|
||||
$this->query(sprintf("ALTER TABLE \"%s\" %s", $table, $alteredOptions[$this->class]));
|
||||
DB::alteration_message(
|
||||
sprintf("Table %s options changed: %s", $table, $alteredOptions[$this->class]),
|
||||
"changed"
|
||||
);
|
||||
}
|
||||
|
||||
//Create any fulltext columns and triggers here:
|
||||
foreach ($fulltexts as $fulltext) {
|
||||
$this->query($fulltext);
|
||||
}
|
||||
foreach ($dropTriggers as $dropTrigger) {
|
||||
$this->query($dropTrigger);
|
||||
}
|
||||
|
||||
foreach ($triggers as $trigger) {
|
||||
$this->query($trigger);
|
||||
$triggerFields = $this->triggerFieldsFromTrigger($trigger);
|
||||
if ($triggerFields) {
|
||||
//We need to run a simple query to force the database to update the triggered columns
|
||||
$this->query("UPDATE \"{$table}\" SET \"{$triggerFields[0]}\"=\"$triggerFields[0]\";");
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($alterIndexList as $alteration) {
|
||||
$this->query($alteration);
|
||||
}
|
||||
|
||||
//If we have a partitioning requirement, we do that here:
|
||||
if ($advancedOptions && isset($advancedOptions['partitions'])) {
|
||||
$this->createOrReplacePartition($table, $advancedOptions['partitions']);
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if ($advancedOptions && isset($advancedOptions['cluster'])) {
|
||||
$clusterIndex = $this->buildPostgresIndexName($table, $advancedOptions['cluster']);
|
||||
$this->query("CLUSTER \"$table\" USING \"$clusterIndex\";");
|
||||
} else {
|
||||
//Check that clustering is not on this table, and if it is, remove it:
|
||||
|
||||
//This is really annoying. We need the oid of this table:
|
||||
$stats = $this->preparedQuery(
|
||||
"SELECT relid FROM pg_stat_user_tables WHERE relname = ?;",
|
||||
array($table)
|
||||
)->first();
|
||||
$oid = $stats['relid'];
|
||||
|
||||
//Now we can run a long query to get the clustered status:
|
||||
//If anyone knows a better way to get the clustered status, then feel free to replace this!
|
||||
$clustered = $this->preparedQuery(
|
||||
"
|
||||
SELECT c2.relname, i.indisclustered
|
||||
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
|
||||
WHERE c.oid = ? AND c.oid = i.indrelid AND i.indexrelid = c2.oid AND indisclustered='t';",
|
||||
array($oid)
|
||||
)->first();
|
||||
|
||||
if ($clustered) {
|
||||
$this->query("ALTER TABLE \"$table\" SET WITHOUT CLUSTER;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates an ALTER expression for a column in PostgreSQL
|
||||
*
|
||||
* @param $tableName Name of the table to be altered
|
||||
* @param $colName Name of the column to be altered
|
||||
* @param $colSpec String which contains conditions for a column
|
||||
* @return string
|
||||
*/
|
||||
private function alterTableAlterColumn($tableName, $colName, $colSpec)
|
||||
{
|
||||
// First, we split the column specifications into parts
|
||||
// TODO: this returns an empty array for the following string: int(11) not null auto_increment
|
||||
// on second thoughts, why is an auto_increment field being passed through?
|
||||
$pattern = '/^([\w(\,)]+)\s?((?:not\s)?null)?\s?(default\s[\w\.\'\\\\]+)?\s?(check\s[\w()\'",\s\\\\]+)?$/i';
|
||||
preg_match($pattern, $colSpec, $matches);
|
||||
// example value this regex is expected to parse:
|
||||
// varchar(255) not null default 'SS\Test\Player' check ("ClassName" in ('SS\Test\Player', 'Player', null))
|
||||
// split into:
|
||||
// * varchar(255)
|
||||
// * not null
|
||||
// * default 'SS\Test\Player'
|
||||
// * check ("ClassName" in ('SS\Test\Player', 'Player', null))
|
||||
|
||||
if (sizeof($matches) == 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($matches[1] == 'serial8') {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (isset($matches[1])) {
|
||||
$alterCol = "ALTER COLUMN \"$colName\" TYPE $matches[1] USING \"$colName\"::$matches[1]\n";
|
||||
|
||||
// SET null / not null
|
||||
if (!empty($matches[2])) {
|
||||
$alterCol .= ",\nALTER COLUMN \"$colName\" SET $matches[2]";
|
||||
}
|
||||
|
||||
// SET default (we drop it first, for reasons of precaution)
|
||||
if (!empty($matches[3])) {
|
||||
$alterCol .= ",\nALTER COLUMN \"$colName\" DROP DEFAULT";
|
||||
$alterCol .= ",\nALTER COLUMN \"$colName\" SET $matches[3]";
|
||||
}
|
||||
|
||||
// SET check constraint (The constraint HAS to be dropped)
|
||||
$constraintName = "{$tableName}_{$colName}_check";
|
||||
$constraintExists = $this->constraintExists($constraintName, false);
|
||||
if (isset($matches[4])) {
|
||||
//Take this new constraint and see what's outstanding from the target table:
|
||||
$constraint_bits = explode('(', $matches[4]);
|
||||
$constraint_values = trim($constraint_bits[2], ')');
|
||||
$constraint_values_bits = explode(',', $constraint_values);
|
||||
$default = trim($constraint_values_bits[0], " '");
|
||||
|
||||
//Now go and convert anything that's not in this list to 'Page'
|
||||
//We have to run this as a query, not as part of the alteration queries due to the way they are constructed.
|
||||
$updateConstraint = '';
|
||||
$updateConstraint .= "UPDATE \"{$tableName}\" SET \"$colName\"='$default' WHERE \"$colName\" NOT IN ($constraint_values);";
|
||||
if ($this->hasTable("{$tableName}_Live")) {
|
||||
$updateConstraint .= "UPDATE \"{$tableName}_Live\" SET \"$colName\"='$default' WHERE \"$colName\" NOT IN ($constraint_values);";
|
||||
}
|
||||
if ($this->hasTable("{$tableName}_Versions")) {
|
||||
$updateConstraint .= "UPDATE \"{$tableName}_Versions\" SET \"$colName\"='$default' WHERE \"$colName\" NOT IN ($constraint_values);";
|
||||
}
|
||||
|
||||
$this->query($updateConstraint);
|
||||
}
|
||||
|
||||
//First, delete any existing constraint on this column, even if it's no longer an enum
|
||||
if ($constraintExists) {
|
||||
$alterCol .= ",\nDROP CONSTRAINT \"{$constraintName}\"";
|
||||
}
|
||||
|
||||
//Now create the constraint (if we've asked for one)
|
||||
if (!empty($matches[4])) {
|
||||
$alterCol .= ",\nADD CONSTRAINT \"{$constraintName}\" $matches[4]";
|
||||
}
|
||||
}
|
||||
|
||||
return isset($alterCol) ? $alterCol : '';
|
||||
}
|
||||
|
||||
public function renameTable($oldTableName, $newTableName)
|
||||
{
|
||||
$constraints = $this->getConstraintForTable($oldTableName);
|
||||
$this->query("ALTER TABLE \"$oldTableName\" RENAME TO \"$newTableName\"");
|
||||
|
||||
if ($constraints) {
|
||||
foreach ($constraints as $old) {
|
||||
$new = preg_replace('/^' . $oldTableName . '/', $newTableName, $old);
|
||||
$this->query("ALTER TABLE \"$newTableName\" RENAME CONSTRAINT \"$old\" TO \"$new\";");
|
||||
}
|
||||
}
|
||||
unset(self::$cached_fieldlists[$oldTableName]);
|
||||
unset(self::$cached_constraints[$oldTableName]);
|
||||
}
|
||||
|
||||
public function checkAndRepairTable($tableName)
|
||||
{
|
||||
$this->query("VACUUM FULL ANALYZE \"$tableName\"");
|
||||
$this->query("REINDEX TABLE \"$tableName\"");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createField($table, $field, $spec)
|
||||
{
|
||||
$this->query("ALTER TABLE \"$table\" ADD \"$field\" $spec");
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the database type of the given field.
|
||||
*
|
||||
* @param string $tableName The name of the tbale the field is in.
|
||||
* @param string $fieldName The name of the field to change.
|
||||
* @param string $fieldSpec The new field specification
|
||||
*/
|
||||
public function alterField($tableName, $fieldName, $fieldSpec)
|
||||
{
|
||||
$this->query("ALTER TABLE \"$tableName\" CHANGE \"$fieldName\" \"$fieldName\" $fieldSpec");
|
||||
}
|
||||
|
||||
public function renameField($tableName, $oldName, $newName)
|
||||
{
|
||||
$fieldList = $this->fieldList($tableName);
|
||||
if (array_key_exists($oldName, $fieldList)) {
|
||||
$this->query("ALTER TABLE \"$tableName\" RENAME COLUMN \"$oldName\" TO \"$newName\"");
|
||||
|
||||
//Remove this from the cached list:
|
||||
unset(self::$cached_fieldlists[$tableName]);
|
||||
}
|
||||
}
|
||||
|
||||
public function fieldList($table)
|
||||
{
|
||||
//Query from http://www.alberton.info/postgresql_meta_info.html
|
||||
//This gets us more information than we need, but I've included it all for the moment....
|
||||
|
||||
//if(!isset(self::$cached_fieldlists[$table])){
|
||||
$fields = $this->preparedQuery(
|
||||
"
|
||||
SELECT ordinal_position, column_name, data_type, column_default,
|
||||
is_nullable, character_maximum_length, numeric_precision, numeric_scale
|
||||
FROM information_schema.columns WHERE table_name = ? and table_schema = ?
|
||||
ORDER BY ordinal_position;",
|
||||
array($table, $this->database->currentSchema())
|
||||
);
|
||||
|
||||
$output = array();
|
||||
if ($fields) {
|
||||
foreach ($fields as $field) {
|
||||
switch ($field['data_type']) {
|
||||
case 'character varying':
|
||||
//Check to see if there's a constraint attached to this column:
|
||||
//$constraint=$this->query("SELECT conname,pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE r.contype = 'c' AND conname='" . $table . '_' . $field['column_name'] . "_check' ORDER BY 1;")->first();
|
||||
$constraint = $this->constraintExists($table . '_' . $field['column_name'] . '_check');
|
||||
if ($constraint) {
|
||||
//Now we need to break this constraint text into bits so we can see what we have:
|
||||
//Examples:
|
||||
//CHECK ("CanEditType"::text = ANY (ARRAY['LoggedInUsers'::character varying, 'OnlyTheseUsers'::character varying, 'Inherit'::character varying]::text[]))
|
||||
//CHECK ("ClassName"::text = 'PageComment'::text)
|
||||
|
||||
//TODO: replace all this with a regular expression!
|
||||
$value = $constraint['pg_get_constraintdef'];
|
||||
$value = substr($value, strpos($value, '='));
|
||||
$value = str_replace("''", "'", $value);
|
||||
|
||||
$in_value = false;
|
||||
$constraints = array();
|
||||
$current_value = '';
|
||||
for ($i = 0; $i < strlen($value); $i++) {
|
||||
$char = substr($value, $i, 1);
|
||||
if ($in_value) {
|
||||
$current_value .= $char;
|
||||
}
|
||||
|
||||
if ($char == "'") {
|
||||
if (!$in_value) {
|
||||
$in_value = true;
|
||||
} else {
|
||||
$in_value = false;
|
||||
$constraints[] = substr($current_value, 0, -1);
|
||||
$current_value = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeof($constraints) > 0) {
|
||||
//Get the default:
|
||||
$default = trim(substr(
|
||||
$field['column_default'],
|
||||
0,
|
||||
strpos($field['column_default'], '::')
|
||||
), "'");
|
||||
$output[$field['column_name']] = $this->enum(array(
|
||||
'default' => $default,
|
||||
'name' => $field['column_name'],
|
||||
'enums' => $constraints
|
||||
));
|
||||
}
|
||||
} else {
|
||||
$output[$field['column_name']] = 'varchar(' . $field['character_maximum_length'] . ')';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'numeric':
|
||||
$output[$field['column_name']] = 'decimal(' . $field['numeric_precision'] . ',' . $field['numeric_scale'] . ') default ' . floatval($field['column_default']);
|
||||
break;
|
||||
|
||||
case 'integer':
|
||||
$output[$field['column_name']] = 'integer default ' . (int)$field['column_default'];
|
||||
break;
|
||||
|
||||
case 'timestamp without time zone':
|
||||
$output[$field['column_name']] = 'timestamp';
|
||||
break;
|
||||
|
||||
case 'smallint':
|
||||
$output[$field['column_name']] = 'smallint default ' . (int)$field['column_default'];
|
||||
break;
|
||||
|
||||
case 'time without time zone':
|
||||
$output[$field['column_name']] = 'time';
|
||||
break;
|
||||
|
||||
case 'double precision':
|
||||
$output[$field['column_name']] = 'float';
|
||||
break;
|
||||
|
||||
default:
|
||||
$output[$field['column_name']] = $field;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// self::$cached_fieldlists[$table]=$output;
|
||||
//}
|
||||
|
||||
//return self::$cached_fieldlists[$table];
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public function clearCachedFieldlist($tableName = false)
|
||||
{
|
||||
if ($tableName) {
|
||||
unset(self::$cached_fieldlists[$tableName]);
|
||||
} else {
|
||||
self::$cached_fieldlists = array();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an index on a table.
|
||||
*
|
||||
* @param string $tableName The name of the table.
|
||||
* @param string $indexName The name of the index.
|
||||
* @param string $indexSpec The specification of the index, see Database::requireIndex() for more details.
|
||||
*/
|
||||
public function createIndex($tableName, $indexName, $indexSpec)
|
||||
{
|
||||
$createIndex = $this->getIndexSqlDefinition($tableName, $indexName, $indexSpec);
|
||||
if ($createIndex !== false) {
|
||||
$this->query($createIndex);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec)
|
||||
{
|
||||
|
||||
//TODO: create table partition support
|
||||
//TODO: create clustering options
|
||||
|
||||
//NOTE: it is possible for *_renamed tables to have indexes whose names are not updates
|
||||
//Therefore, we now check for the existance of indexes before we create them.
|
||||
//This is techically a bug, since new tables will not be indexed.
|
||||
|
||||
// Determine index name
|
||||
$tableCol = $this->buildPostgresIndexName($tableName, $indexName);
|
||||
|
||||
//Misc options first:
|
||||
$fillfactor = $where = '';
|
||||
if (isset($indexSpec['fillfactor'])) {
|
||||
$fillfactor = 'WITH (FILLFACTOR = ' . $indexSpec['fillfactor'] . ')';
|
||||
}
|
||||
if (isset($indexSpec['where'])) {
|
||||
$where = 'WHERE ' . $indexSpec['where'];
|
||||
}
|
||||
|
||||
//create a type-specific index
|
||||
// NOTE: hash should be removed. This is only here to demonstrate how other indexes can be made
|
||||
// NOTE: Quote the index name to preserve case sensitivity
|
||||
switch ($indexSpec['type']) {
|
||||
case 'fulltext':
|
||||
// @see fulltext() for the definition of the trigger that ts_$IndexName uses for fulltext searching
|
||||
$clusterMethod = PostgreSQLDatabase::default_fts_cluster_method();
|
||||
$spec = "create index \"$tableCol\" ON \"$tableName\" USING $clusterMethod(\"ts_" . $indexName . "\") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'unique':
|
||||
$spec = "create unique index \"$tableCol\" ON \"$tableName\" (" . $this->implodeColumnList($indexSpec['columns']) . ") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'btree':
|
||||
$spec = "create index \"$tableCol\" ON \"$tableName\" USING btree (" . $this->implodeColumnList($indexSpec['columns']) . ") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'hash':
|
||||
//NOTE: this is not a recommended index type
|
||||
$spec = "create index \"$tableCol\" ON \"$tableName\" USING hash (" . $this->implodeColumnList($indexSpec['columns']) . ") $fillfactor $where";
|
||||
break;
|
||||
|
||||
case 'index':
|
||||
//'index' is the same as default, just a normal index with the default type decided by the database.
|
||||
default:
|
||||
$spec = "create index \"$tableCol\" ON \"$tableName\" (" . $this->implodeColumnList($indexSpec['columns']) . ") $fillfactor $where";
|
||||
}
|
||||
return trim($spec) . ';';
|
||||
}
|
||||
|
||||
public function alterIndex($tableName, $indexName, $indexSpec)
|
||||
{
|
||||
$indexSpec = trim($indexSpec);
|
||||
if ($indexSpec[0] != '(') {
|
||||
list($indexType, $indexFields) = explode(' ', $indexSpec, 2);
|
||||
} else {
|
||||
$indexType = null;
|
||||
$indexFields = $indexSpec;
|
||||
}
|
||||
|
||||
if (!$indexType) {
|
||||
$indexType = "index";
|
||||
}
|
||||
|
||||
$this->query("DROP INDEX \"$indexName\"");
|
||||
$this->query("ALTER TABLE \"$tableName\" ADD $indexType \"$indexName\" $indexFields");
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a trigger name attempt to determine the columns upon which it acts
|
||||
*
|
||||
* @param string $triggerName Postgres trigger name
|
||||
* @param string $table
|
||||
* @return array List of columns
|
||||
*/
|
||||
protected function extractTriggerColumns($triggerName, $table)
|
||||
{
|
||||
$trigger = $this->preparedQuery(
|
||||
"SELECT t.tgargs
|
||||
FROM pg_catalog.pg_trigger t
|
||||
INNER JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid
|
||||
INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relname = ?
|
||||
AND n.nspname = ?
|
||||
AND t.tgname = ?",
|
||||
[
|
||||
$table,
|
||||
$this->database->currentSchema(),
|
||||
$triggerName
|
||||
]
|
||||
)->first();
|
||||
|
||||
// Convert stream to string
|
||||
if (is_resource($trigger['tgargs'])) {
|
||||
$trigger['tgargs'] = stream_get_contents($trigger['tgargs']);
|
||||
}
|
||||
|
||||
if (strpos($trigger['tgargs'], "\000") !== false) {
|
||||
// Option 1: output as a string (PDO)
|
||||
$argList = array_filter(explode("\000", $trigger['tgargs']));
|
||||
} else {
|
||||
// Option 2: hex-encoded (pg_sql non-pdo)
|
||||
$bytes = str_split($trigger['tgargs'], 2);
|
||||
$argList = array();
|
||||
$nextArg = "";
|
||||
foreach ($bytes as $byte) {
|
||||
if ($byte == '\x') {
|
||||
continue;
|
||||
} elseif ($byte == "00") {
|
||||
$argList[] = $nextArg;
|
||||
$nextArg = "";
|
||||
} else {
|
||||
$nextArg .= chr(hexdec($byte));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Drop first two arguments (trigger name and config name) and implode into nice list
|
||||
return array_slice($argList, 2);
|
||||
}
|
||||
|
||||
public function indexList($table)
|
||||
{
|
||||
//Retrieve a list of indexes for the specified table
|
||||
$indexes = $this->preparedQuery(
|
||||
"
|
||||
SELECT tablename, indexname, indexdef
|
||||
FROM pg_catalog.pg_indexes
|
||||
WHERE tablename = ? AND schemaname = ?;",
|
||||
array($table, $this->database->currentSchema())
|
||||
);
|
||||
|
||||
$indexList = array();
|
||||
foreach ($indexes as $index) {
|
||||
// Key for the indexList array. Differs from other DB implementations, which is why
|
||||
// requireIndex() needed to be overridden
|
||||
$indexName = $index['indexname'];
|
||||
|
||||
//We don't actually need the entire created command, just a few bits:
|
||||
$type = '';
|
||||
|
||||
//Check for uniques:
|
||||
if (substr($index['indexdef'], 0, 13) == 'CREATE UNIQUE') {
|
||||
$type = 'unique';
|
||||
}
|
||||
|
||||
//check for hashes, btrees etc:
|
||||
if (strpos(strtolower($index['indexdef']), 'using hash ') !== false) {
|
||||
$type = 'hash';
|
||||
}
|
||||
|
||||
//TODO: Fix me: btree is the default index type:
|
||||
//if(strpos(strtolower($index['indexdef']), 'using btree ')!==false)
|
||||
// $prefix='using btree ';
|
||||
|
||||
if (strpos(strtolower($index['indexdef']), 'using rtree ') !== false) {
|
||||
$type = 'rtree';
|
||||
}
|
||||
|
||||
// For fulltext indexes we need to extract the columns from another source
|
||||
if (stristr($index['indexdef'], 'using gin')) {
|
||||
$type = 'fulltext';
|
||||
// Extract trigger information from postgres
|
||||
$triggerName = preg_replace('/^ix_/', 'ts_', $index['indexname']);
|
||||
$columns = $this->extractTriggerColumns($triggerName, $table);
|
||||
$columnString = $this->implodeColumnList($columns);
|
||||
} else {
|
||||
$columnString = $this->quoteColumnSpecString($index['indexdef']);
|
||||
}
|
||||
|
||||
$indexList[$indexName] = array(
|
||||
'name' => $indexName, // Not the correct name in the PHP, as this will be a mangled postgres-unique code
|
||||
'columns' => $this->explodeColumnString($columnString),
|
||||
'type' => $type ?: 'index',
|
||||
);
|
||||
}
|
||||
|
||||
return $indexList;
|
||||
}
|
||||
|
||||
public function tableList()
|
||||
{
|
||||
$tables = array();
|
||||
$result = $this->preparedQuery(
|
||||
"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = ? AND tablename NOT ILIKE 'pg\\\_%' AND tablename NOT ILIKE 'sql\\\_%'",
|
||||
array($this->database->currentSchema())
|
||||
);
|
||||
foreach ($result as $record) {
|
||||
$table = reset($record);
|
||||
$tables[strtolower($table)] = $table;
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find out what the constraint information is, given a constraint name.
|
||||
* We also cache this result, so the next time we don't need to do a
|
||||
* query all over again.
|
||||
*
|
||||
* @param string $constraint
|
||||
* @param bool $cache Flag whether a cached version should be used. Set to false to cache bust.
|
||||
* @return false|array Either false, if the constraint doesn't exist, or an array
|
||||
* with the keys conname and pg_get_constraintdef
|
||||
*/
|
||||
protected function constraintExists($constraint, $cache = true)
|
||||
{
|
||||
if (!$cache || !isset(self::$cached_constraints[$constraint])) {
|
||||
$value = $this->preparedQuery(
|
||||
"
|
||||
SELECT conname,pg_catalog.pg_get_constraintdef(r.oid, true)
|
||||
FROM pg_catalog.pg_constraint r
|
||||
INNER JOIN pg_catalog.pg_namespace n
|
||||
ON r.connamespace = n.oid
|
||||
WHERE r.contype = 'c' AND conname = ? AND n.nspname = ?
|
||||
ORDER BY 1;",
|
||||
array($constraint, $this->database->currentSchema())
|
||||
)->first();
|
||||
if (!$cache) {
|
||||
return $value;
|
||||
}
|
||||
self::$cached_constraints[$constraint] = $value;
|
||||
}
|
||||
|
||||
return self::$cached_constraints[$constraint];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of constraints for the provided table name.
|
||||
* @param string $tableName
|
||||
* @return array
|
||||
*/
|
||||
private function getConstraintForTable($tableName)
|
||||
{
|
||||
// Note the PostgreSQL `like` operator is case sensitive
|
||||
$constraints = $this->preparedQuery(
|
||||
"
|
||||
SELECT conname
|
||||
FROM pg_catalog.pg_constraint r
|
||||
INNER JOIN pg_catalog.pg_namespace n
|
||||
ON r.connamespace = n.oid
|
||||
WHERE r.contype = 'c' AND conname like ? AND n.nspname = ?
|
||||
ORDER BY 1;",
|
||||
array($tableName . '_%', $this->database->currentSchema())
|
||||
)->column('conname');
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* A function to return the field names and datatypes for the particular table
|
||||
*
|
||||
* @param string $tableName
|
||||
* @return array List of columns an an associative array with the keys Column and DataType
|
||||
*/
|
||||
public function tableDetails($tableName)
|
||||
{
|
||||
$query = "SELECT a.attname as \"Column\", pg_catalog.format_type(a.atttypid, a.atttypmod) as \"Datatype\"
|
||||
FROM pg_catalog.pg_attribute a
|
||||
WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = (
|
||||
SELECT c.oid
|
||||
FROM pg_catalog.pg_class c
|
||||
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relname = ? AND pg_catalog.pg_table_is_visible(c.oid) AND n.nspname = ?
|
||||
);";
|
||||
|
||||
$result = $this->preparedQuery(
|
||||
$query,
|
||||
array($tableName, $this->database->currentSchema())
|
||||
);
|
||||
|
||||
$table = array();
|
||||
foreach ($result as $row) {
|
||||
$table[] = array(
|
||||
'Column' => $row['Column'],
|
||||
'DataType' => $row['DataType']
|
||||
);
|
||||
}
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a legit trigger name and it will be dropped
|
||||
* This assumes that the trigger has been named in a unique fashion
|
||||
*
|
||||
* @param string $triggerName Name of the trigger
|
||||
* @param string $tableName Name of the table
|
||||
*/
|
||||
protected function dropTrigger($triggerName, $tableName)
|
||||
{
|
||||
$exists = $this->preparedQuery(
|
||||
"
|
||||
SELECT trigger_name
|
||||
FROM information_schema.triggers
|
||||
WHERE trigger_name = ? AND trigger_schema = ?;",
|
||||
array($triggerName, $this->database->currentSchema())
|
||||
)->first();
|
||||
if ($exists) {
|
||||
$this->query("DROP trigger IF EXISTS $triggerName ON \"$tableName\";");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This will return the fields that the trigger is monitoring
|
||||
*
|
||||
* @param string $trigger Name of the trigger
|
||||
* @return array
|
||||
*/
|
||||
protected function triggerFieldsFromTrigger($trigger)
|
||||
{
|
||||
if ($trigger) {
|
||||
$tsvector = 'tsvector_update_trigger';
|
||||
$ts_pos = strpos($trigger, $tsvector);
|
||||
$details = trim(substr($trigger, $ts_pos + strlen($tsvector)), '();');
|
||||
//Now split this into bits:
|
||||
$bits = explode(',', $details);
|
||||
|
||||
$fields = $bits[2];
|
||||
|
||||
$field_bits = explode(',', str_replace('"', '', $fields));
|
||||
$result = array();
|
||||
foreach ($field_bits as $field_bit) {
|
||||
$result[] = trim($field_bit);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a boolean type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function boolean($values)
|
||||
{
|
||||
$default = $values['default'] ? '1' : '0';
|
||||
return "smallint default {$default}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a date type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function date($values)
|
||||
{
|
||||
return "date";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a decimal type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function decimal($values)
|
||||
{
|
||||
// Avoid empty strings being put in the db
|
||||
if ($values['precision'] == '') {
|
||||
$precision = 1;
|
||||
} else {
|
||||
$precision = $values['precision'];
|
||||
}
|
||||
|
||||
$defaultValue = '';
|
||||
if (isset($values['default']) && is_numeric($values['default'])) {
|
||||
$defaultValue = ' default ' . floatval($values['default']);
|
||||
}
|
||||
|
||||
return "decimal($precision)$defaultValue";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a enum type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function enum($values)
|
||||
{
|
||||
$default = " default '{$values['default']}'";
|
||||
return "varchar(255)" . $default . " check (\"" . $values['name'] . "\" in ('" . implode(
|
||||
'\', \'',
|
||||
$values['enums']
|
||||
) . "', null))";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a float type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function float($values)
|
||||
{
|
||||
return "float";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a float type-formatted string cause double is not supported
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function double($values)
|
||||
{
|
||||
return $this->float($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a int type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function int($values)
|
||||
{
|
||||
return "integer default " . (int)$values['default'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a bigint type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function bigint($values)
|
||||
{
|
||||
return "bigint default " . (int)$values['default'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime type-formatted string
|
||||
* For PostgreSQL, we simply return the word 'timestamp', no other parameters are necessary
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function datetime($values)
|
||||
{
|
||||
return "timestamp";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a text type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function text($values)
|
||||
{
|
||||
return "text";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a time type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function time($values)
|
||||
{
|
||||
return "time";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a varchar type-formatted string
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function varchar($values)
|
||||
{
|
||||
if (!isset($values['precision'])) {
|
||||
$values['precision'] = 255;
|
||||
}
|
||||
|
||||
return "varchar({$values['precision']})";
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a 4 digit numeric type. MySQL has a proprietary 'Year' type.
|
||||
* For Postgres, we'll use a 4 digit numeric
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function year($values)
|
||||
{
|
||||
return "decimal(4,0)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fulltext search datatype for PostgreSQL
|
||||
* This will also return a trigger to be applied to this table
|
||||
*
|
||||
* @todo: create custom functions to allow weighted searches
|
||||
*
|
||||
* @param array $this_index Index specification for the fulltext index
|
||||
* @param string $tableName
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
protected function fulltext($this_index, $tableName, $name)
|
||||
{
|
||||
//For full text search, we need to create a column for the index
|
||||
$columns = $this->implodeColumnList($this_index['columns']);
|
||||
|
||||
$fulltexts = "\"ts_$name\" tsvector";
|
||||
$triggerName = $this->buildPostgresTriggerName($tableName, $name);
|
||||
$language = PostgreSQLDatabase::search_language();
|
||||
|
||||
$this->dropTrigger($triggerName, $tableName);
|
||||
$triggers = "CREATE TRIGGER \"$triggerName\" BEFORE INSERT OR UPDATE
|
||||
ON \"$tableName\" FOR EACH ROW EXECUTE PROCEDURE
|
||||
tsvector_update_trigger(\"ts_$name\", 'pg_catalog.$language', $columns);";
|
||||
|
||||
return array(
|
||||
'name' => $name,
|
||||
'ts_name' => "ts_{$name}",
|
||||
'fulltexts' => $fulltexts,
|
||||
'triggers' => $triggers
|
||||
);
|
||||
}
|
||||
|
||||
public function IdColumn($asDbValue = false, $hasAutoIncPK = true)
|
||||
{
|
||||
if ($asDbValue) {
|
||||
return 'bigint';
|
||||
} else {
|
||||
return 'serial8 not null';
|
||||
}
|
||||
}
|
||||
|
||||
public function hasTable($tableName)
|
||||
{
|
||||
$result = $this->preparedQuery(
|
||||
"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = ? AND tablename = ?;",
|
||||
array($this->database->currentSchema(), $tableName)
|
||||
);
|
||||
return ($result->numRecords() > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the values of the given enum field
|
||||
*
|
||||
* @todo Make a proper implementation
|
||||
*
|
||||
* @param string $tableName Name of table to check
|
||||
* @param string $fieldName name of enum field to check
|
||||
* @return array List of enum values
|
||||
*/
|
||||
public function enumValuesForField($tableName, $fieldName)
|
||||
{
|
||||
//return array('SiteTree','Page');
|
||||
$constraints = $this->constraintExists("{$tableName}_{$fieldName}_check");
|
||||
if ($constraints) {
|
||||
return $this->enumValuesFromConstraint($constraints['pg_get_constraintdef']);
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actual enum fields from the constraint value:
|
||||
*
|
||||
* @param string $constraint
|
||||
* @return array
|
||||
*/
|
||||
protected function enumValuesFromConstraint($constraint)
|
||||
{
|
||||
$constraint = substr($constraint, strpos($constraint, 'ANY (ARRAY[') + 11);
|
||||
$constraint = substr($constraint, 0, -11);
|
||||
$constraints = array();
|
||||
$segments = explode(',', $constraint);
|
||||
foreach ($segments as $this_segment) {
|
||||
$bits = preg_split('/ *:: */', $this_segment);
|
||||
array_unshift($constraints, trim($bits[0], " '"));
|
||||
}
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
public function dbDataType($type)
|
||||
{
|
||||
$values = array(
|
||||
'unsigned integer' => 'INT'
|
||||
);
|
||||
|
||||
if (isset($values[$type])) {
|
||||
return $values[$type];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a tablespace and and location, either create a new one
|
||||
* or update the existing one
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $location
|
||||
*/
|
||||
public function createOrReplaceTablespace($name, $location)
|
||||
{
|
||||
$existing = $this->preparedQuery(
|
||||
"SELECT spcname, spclocation FROM pg_tablespace WHERE spcname = ?;",
|
||||
array($name)
|
||||
)->first();
|
||||
|
||||
//NOTE: this location must be empty for this to work
|
||||
//We can't seem to change the location of the tablespace through any ALTER commands :(
|
||||
|
||||
//If a tablespace with this name exists, but the location has changed, then drop the current one
|
||||
//if($existing && $location!=$existing['spclocation'])
|
||||
// DB::query("DROP TABLESPACE $name;");
|
||||
|
||||
//If this is a new tablespace, or we have dropped the current one:
|
||||
if (!$existing || ($existing && $location != $existing['spclocation'])) {
|
||||
$this->query("CREATE TABLESPACE $name LOCATION '$location';");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $tableName
|
||||
* @param array $partitions
|
||||
* @param array $indexes
|
||||
* @param array $extensions
|
||||
*/
|
||||
public function createOrReplacePartition($tableName, $partitions, $indexes = [], $extensions = [])
|
||||
{
|
||||
|
||||
//We need the plpgsql language to be installed for this to work:
|
||||
$this->createLanguage('plpgsql');
|
||||
|
||||
$trigger = 'CREATE OR REPLACE FUNCTION ' . $tableName . '_insert_trigger() RETURNS TRIGGER AS $$ BEGIN ';
|
||||
$first = true;
|
||||
|
||||
//Do we need to create a tablespace for this item?
|
||||
if ($extensions && isset($extensions['tablespace'])) {
|
||||
$this->createOrReplaceTablespace($extensions['tablespace']['name'], $extensions['tablespace']['location']);
|
||||
$tableSpace = ' TABLESPACE ' . $extensions['tablespace']['name'];
|
||||
} else {
|
||||
$tableSpace = '';
|
||||
}
|
||||
|
||||
foreach ($partitions as $partition_name => $partition_value) {
|
||||
//Check that this child table does not already exist:
|
||||
if (!$this->hasTable($partition_name)) {
|
||||
$this->query("CREATE TABLE \"$partition_name\" (CHECK (" . str_replace(
|
||||
'NEW.',
|
||||
'',
|
||||
$partition_value
|
||||
) . ")) INHERITS (\"$tableName\")$tableSpace;");
|
||||
} else {
|
||||
//Drop the constraint, we will recreate in in the next line
|
||||
$constraintName = "{$partition_name}_pkey";
|
||||
$constraintExists = $this->constraintExists($constraintName, false);
|
||||
if ($constraintExists) {
|
||||
$this->query("ALTER TABLE \"$partition_name\" DROP CONSTRAINT \"{$constraintName}\";");
|
||||
}
|
||||
$this->dropTrigger(strtolower('trigger_' . $tableName . '_insert'), $tableName);
|
||||
}
|
||||
|
||||
$this->query("ALTER TABLE \"$partition_name\" ADD CONSTRAINT \"{$partition_name}_pkey\" PRIMARY KEY (\"ID\");");
|
||||
|
||||
if ($first) {
|
||||
$trigger .= 'IF';
|
||||
$first = false;
|
||||
} else {
|
||||
$trigger .= 'ELSIF';
|
||||
}
|
||||
|
||||
$trigger .= " ($partition_value) THEN INSERT INTO \"$partition_name\" VALUES (NEW.*);";
|
||||
|
||||
if ($indexes) {
|
||||
// We need to propogate the indexes through to the child pages.
|
||||
// Some of this code is duplicated, and could be tidied up
|
||||
foreach ($indexes as $name => $this_index) {
|
||||
if ($this_index['type'] == 'fulltext') {
|
||||
$fillfactor = $where = '';
|
||||
if (isset($this_index['fillfactor'])) {
|
||||
$fillfactor = 'WITH (FILLFACTOR = ' . $this_index['fillfactor'] . ')';
|
||||
}
|
||||
if (isset($this_index['where'])) {
|
||||
$where = 'WHERE ' . $this_index['where'];
|
||||
}
|
||||
$clusterMethod = PostgreSQLDatabase::default_fts_cluster_method();
|
||||
$this->query("CREATE INDEX \"" . $this->buildPostgresIndexName(
|
||||
$partition_name,
|
||||
$this_index['name']
|
||||
) . "\" ON \"" . $partition_name . "\" USING $clusterMethod(\"ts_" . $name . "\") $fillfactor $where");
|
||||
$ts_details = $this->fulltext($this_index, $partition_name, $name);
|
||||
$this->query($ts_details['triggers']);
|
||||
} else {
|
||||
if (is_array($this_index)) {
|
||||
$index_name = $this_index['name'];
|
||||
} else {
|
||||
$index_name = trim($this_index, '()');
|
||||
}
|
||||
|
||||
$createIndex = $this->getIndexSqlDefinition($partition_name, $index_name, $this_index);
|
||||
if ($createIndex !== false) {
|
||||
$this->query($createIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Lastly, clustering goes here:
|
||||
if ($extensions && isset($extensions['cluster'])) {
|
||||
$this->query("CLUSTER \"$partition_name\" USING \"{$extensions['cluster']}\";");
|
||||
}
|
||||
}
|
||||
|
||||
$trigger .= 'ELSE RAISE EXCEPTION \'Value id out of range. Fix the ' . $tableName . '_insert_trigger() function!\'; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql;';
|
||||
$trigger .= 'CREATE TRIGGER trigger_' . $tableName . '_insert BEFORE INSERT ON "' . $tableName . '" FOR EACH ROW EXECUTE PROCEDURE ' . $tableName . '_insert_trigger();';
|
||||
|
||||
$this->query($trigger);
|
||||
}
|
||||
|
||||
/*
|
||||
* This will create a language if it doesn't already exist.
|
||||
* This is used by the createOrReplacePartition function, which needs plpgsql
|
||||
*
|
||||
* @param string $language Language name
|
||||
*/
|
||||
public function createLanguage($language)
|
||||
{
|
||||
$result = $this->preparedQuery(
|
||||
"SELECT lanname FROM pg_language WHERE lanname = ?;",
|
||||
array($language)
|
||||
)->first();
|
||||
|
||||
if (!$result) {
|
||||
$this->query("CREATE LANGUAGE $language;");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a set type-formatted string
|
||||
* This is used for Multi-enum support, which isn't actually supported by Postgres.
|
||||
* Throws a user error to show our lack of support, and return an "int", specifically for sapphire
|
||||
* tests that test multi-enums. This results in a test failure, but not crashing the test run.
|
||||
*
|
||||
* @param array $values Contains a tokenised list of info about this data type
|
||||
* @return string
|
||||
*/
|
||||
public function set($values)
|
||||
{
|
||||
user_error("PostGreSQL does not support multi-enum", E_USER_ERROR);
|
||||
return "int";
|
||||
}
|
||||
}
|
|
@ -1,17 +1,37 @@
|
|||
{
|
||||
"name": "silverstripe/postgresql",
|
||||
"description": "SilverStripe now has tentative support for PostgreSQL ('Postgres')",
|
||||
"type": "silverstripe-module",
|
||||
"keywords": ["silverstripe", "postgresql", "database"],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sam Minnée",
|
||||
"email": "sam@silverstripe.com"
|
||||
}
|
||||
],
|
||||
|
||||
"require":
|
||||
{
|
||||
"silverstripe/framework": "2.3.*,2.4.*"
|
||||
}
|
||||
}
|
||||
"name": "silverstripe/postgresql",
|
||||
"description": "SilverStripe now has tentative support for PostgreSQL ('Postgres')",
|
||||
"type": "silverstripe-vendormodule",
|
||||
"keywords": [
|
||||
"silverstripe",
|
||||
"postgresql",
|
||||
"database"
|
||||
],
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sam Minnée",
|
||||
"email": "sam@silverstripe.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"silverstripe/framework": "^4",
|
||||
"silverstripe/vendor-plugin": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.5",
|
||||
"squizlabs/php_codesniffer": "^3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"SilverStripe\\PostgreSQL\\": "code/",
|
||||
"SilverStripe\\PostgreSQL\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "phpcs code/ tests/",
|
||||
"lint-clean": "phpcbf code/ tests/"
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
}
|
||||
|
|
195
docs/README.md
195
docs/README.md
|
@ -1,14 +1,20 @@
|
|||
# PostgreSQL Database Module
|
||||
|
||||
SilverStripe now has tentative support for PostgreSQL ('Postgres'). This available though a module which you can use after completing a successful MySQL installation.
|
||||
SilverStripe now has tentative support for PostgreSQL ('Postgres').
|
||||
|
||||
## Requirements
|
||||
|
||||
SilverStripe 2.4.0 or greater. (PostgreSQL support is NOT available in 2.3.)
|
||||
SilverStripe 2.4.0 or greater. (PostgreSQL support is NOT available
|
||||
in 2.3.).
|
||||
|
||||
SilverStripe supports Postgres versions 8.3.x, 8.4.x and onwards. Postgres 8.3.0 launched in February 2008, so SilverStripe has a fairly modern but not bleeding edge Postgres version requirement.
|
||||
SilverStripe supports Postgres versions 8.3.x, 8.4.x and onwards.
|
||||
Postgres 8.3.0 launched in February 2008, so SilverStripe has a fairly
|
||||
modern but not bleeding edge Postgres version requirement.
|
||||
|
||||
Support for 8.2.x is theoretically possible if you're willing to manually install T-search. 8.2.x has not been tested either, so there may be other compatibility issues. The EnterpriseDB versions of Postgres also work, if you'd prefer a tuned version.
|
||||
Support for 8.2.x is theoretically possible if you're willing to manually
|
||||
install T-search. 8.2.x has not been tested either, so there may be other
|
||||
compatibility issues. The EnterpriseDB versions of Postgres also work, if
|
||||
you'd prefer a tuned version.
|
||||
|
||||
## Installation
|
||||
|
||||
|
@ -16,21 +22,31 @@ You have three options to install PostgreSQL support with SilverStripe.
|
|||
|
||||
### Option 1 - Installer
|
||||
|
||||
The first option is to use the installer. However, this is currently only supported since SilverStripe 2.4.0 beta2 (or using the daily builds).
|
||||
The first option is to use the installer. However, this is currently only
|
||||
supported since SilverStripe 2.4.0 beta2 (or using the daily builds).
|
||||
|
||||
1. Set up SilverStripe somewhere where you can start the installer - you should only see one database “MySQL” to install with
|
||||
2. Download a copy of the “postgresql” module from here: http://silverstripe.org/postgresql-module
|
||||
3. Extract the archive you downloaded. Rename the directory from “postgresql-trunk-rxxxx” to “postgresql” and copy it into the SilverStripe directory you just set up
|
||||
4. Open the installer once again, and a new option “PostgreSQL” should appear. You can now proceed through the installation without having to change any code.
|
||||
1. Set up SilverStripe somewhere where you can start the installer - you
|
||||
should only see one database “MySQL” to install with.
|
||||
2. Download a copy of the “postgresql” module from here:
|
||||
http://silverstripe.org/postgresql-module
|
||||
3. Extract the archive you downloaded. Rename the directory from
|
||||
“postgresql-trunk-rxxxx” to “postgresql” and copy it into the SilverStripe
|
||||
directory you just set up
|
||||
4. Open the installer once again, and a new option “PostgreSQL” should appear.
|
||||
You can now proceed through the installation without having to change any code.
|
||||
|
||||
### Option 2 - Manual
|
||||
|
||||
The second option is to setup PostgreSQL support manually. This can be achieved by following these instructions:
|
||||
The second option is to setup PostgreSQL support manually. This can be achieved
|
||||
by following these instructions:
|
||||
|
||||
1. Set up a fresh working copy of SilverStripe
|
||||
2. Download a copy of the “postgresql” module from here: http://silverstripe.org/postgresql-module
|
||||
3. Extract the archive you downloaded. Rename the directory from “postgresql-trunk-rxxxx” to “postgresql” and copy it into the SilverStripe directory you just set up
|
||||
4. Open up your mysite/_config.php file and add (or update) the $databaseConfig array like so:
|
||||
3. Extract the archive you downloaded. Rename the directory from
|
||||
“postgresql-trunk-rxxxx” to “postgresql” and copy it into the SilverStripe
|
||||
directory you just set up.
|
||||
4. Open up your mysite/_config.php file and add (or update) the $databaseConfig
|
||||
array like so:
|
||||
|
||||
> $databaseConfig = array(
|
||||
> 'type' => 'PostgreSQLDatabase',
|
||||
|
@ -40,27 +56,35 @@ The second option is to setup PostgreSQL support manually. This can be achieved
|
|||
> 'database' => 'SS_mysite'
|
||||
> );
|
||||
|
||||
Finally, visit dev/build so that SilverStripe can build the database schema and default records.
|
||||
Finally, visit dev/build so that SilverStripe can build the database schema and
|
||||
default records.
|
||||
|
||||
### Option 3 - Environment file
|
||||
|
||||
Finally, the third option is to change your environment to point to PostgreSQLDatabase as a database class. Do this if you're currently using an _ss_environment.php file.
|
||||
Finally, the third option is to change your environment to point to
|
||||
PostgreSQLDatabase as a database class. Do this if you're currently using an
|
||||
_ss_environment.php file.
|
||||
|
||||
1. Download a copy of the “postgresql” module from here: http://silverstripe.org/postgresql-module
|
||||
2. Extract the archive you downloaded. Rename the directory from “postgresql-trunk-rxxxx” to “postgresql” and copy it into your SS directory
|
||||
2. Extract the archive you downloaded. Rename the directory from
|
||||
postgresql-trunk-rxxxx” to “postgresql” and copy it into your SS directory
|
||||
3. Add the following to your existing _ss_environment.php file:
|
||||
|
||||
> define('SS_DATABASE_CLASS', 'PostgreSQLDatabase');
|
||||
|
||||
Last steps:
|
||||
|
||||
1. Ensure your SS_DATABASE_USERNAME and SS_DATABASE_PASSWORD defines in _ss_environment.php are correct to the PostgreSQL server
|
||||
2. Ensure that your mysite/_config.php file has a database name defined, such as “SS_mysite”
|
||||
3. Visit dev/build so that SilverStripe can build the database schema and default records
|
||||
1. Ensure your SS_DATABASE_USERNAME and SS_DATABASE_PASSWORD defines in
|
||||
_ss_environment.php are correct to the PostgreSQL server.
|
||||
2. Ensure that your mysite/_config.php file has a database name defined, such
|
||||
as “SS_mysite”.
|
||||
3. Visit dev/build so that SilverStripe can build the database schema and
|
||||
default records
|
||||
|
||||
## Features
|
||||
|
||||
Here is a quick list of what's different in the Postgres module (a full description follows afterwards):
|
||||
Here is a quick list of what's different in the Postgres module (a full
|
||||
description follows afterwards):
|
||||
|
||||
* T-Search
|
||||
* Extended index support
|
||||
|
@ -70,26 +94,41 @@ Here is a quick list of what's different in the Postgres module (a full descript
|
|||
* Tablespaces
|
||||
* Index clustering
|
||||
|
||||
If you don't know much about databases, or don't want to use any of the advanced features that this module provides, then you don't need to read any further.
|
||||
If you don't know much about databases, or don't want to use any of the
|
||||
advanced features that this module provides, then you don't need to read
|
||||
any further.
|
||||
|
||||
The use of any of these features, especially the advanced options, implies that you have some level of comfort in administrating a Postgres database.
|
||||
The use of any of these features, especially the advanced options, implies
|
||||
that you have some level of comfort in administrating a Postgres database.
|
||||
|
||||
### T-Search
|
||||
|
||||
T-Search support is provided via both GiST and GIN. You can cluster and search columns with combinations of these methods. It is up to you to decide which is most appropriate for your data.
|
||||
T-Search support is provided via both GiST and GIN. You can cluster and
|
||||
search columns with combinations of these methods. It is up to you to
|
||||
decide which is most appropriate for your data.
|
||||
|
||||
The dev/build process automatically creates a special column on each table, and a trigger is automatically set up to update this column whenever the targeted columns are changed. T-Search uses this column to return matches for search criteria.
|
||||
The dev/build process automatically creates a special column on each table,
|
||||
and a trigger is automatically set up to update this column whenever the
|
||||
targeted columns are changed. T-Search uses this column to return matches
|
||||
for search criteria.
|
||||
|
||||
Please see tutorial 4 for information how to enable fulltext search and the necessary controller hooks.
|
||||
Please see tutorial 4 for information how to enable fulltext search and the
|
||||
necessary controller hooks.
|
||||
|
||||
### Extended index support
|
||||
|
||||
Indexes have been extended to include support for more options. These new options include:
|
||||
Indexes have been extended to include support for more options. These new
|
||||
options include:
|
||||
|
||||
* The ability to specify index methods (btree/hash/). Btree is probably fine nearly all indexes, and it is the default. 'Unique' is also supported.
|
||||
* Partial indexes. This is especially handy for creating an index while ignoring nulls or default data.
|
||||
* Multiple column indexing. If your WHERE clauses always use the same columns, then you can create one index covering all of these at once.
|
||||
* Fill factor. If your table content is static, then you can reduce the physical disk space your index uses. Also, if you use clustering, giving the fillfactor a low number may help performance for updates.
|
||||
* The ability to specify index methods (btree/hash/). Btree is probably
|
||||
fine nearly all indexes, and it is the default. 'Unique' is also supported.
|
||||
* Partial indexes. This is especially handy for creating an index while i
|
||||
gnoring nulls or default data.
|
||||
* Multiple column indexing. If your WHERE clauses always use the same
|
||||
columns, then you can create one index covering all of these at once.
|
||||
* Fill factor. If your table content is static, then you can reduce the
|
||||
physical disk space your index uses. Also, if you use clustering, giving the
|
||||
fillfactor a low number may help performance for updates.
|
||||
|
||||
Examples:
|
||||
|
||||
|
@ -113,7 +152,8 @@ Examples:
|
|||
|
||||
### Array data types
|
||||
|
||||
Nearly all data types in SilverStripe can now be expressed as an array. For example, you can specify an int as this:
|
||||
Nearly all data types in SilverStripe can now be expressed as an array. For
|
||||
example, you can specify an int as this:
|
||||
|
||||
> $db = array (
|
||||
> 'Quantity'=>'Int[]'
|
||||
|
@ -123,26 +163,34 @@ You would populate this like so:
|
|||
|
||||
> $item->Quantity='Array[1,2,3...]';
|
||||
|
||||
It also takes object literals if you're more familiar with that or it suits your purpose better, like this:
|
||||
It also takes object literals if you're more familiar with that or it suits
|
||||
your purpose better, like this:
|
||||
|
||||
> $item->Quantity='{1,2,3}';
|
||||
|
||||
Using arrays as data types means that you can avoid join tables. This is not recommended if the SilverStripe ORM would expect a has_one or has_many etc under normal circumstances, but it could be useful in the case where you have a very large join table. You can also index these arrays with GIN indexes.
|
||||
Using arrays as data types means that you can avoid join tables. This is not
|
||||
recommended if the SilverStripe ORM would expect a has_one or has_many etc under
|
||||
normal circumstances, but it could be useful in the case where you have a very
|
||||
large join table. You can also index these arrays with GIN indexes.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
### Transactions
|
||||
|
||||
Transactions are supported at the database connection level. The relevant functions are:
|
||||
Transactions are supported at the database connection level. The relevant
|
||||
functions are:
|
||||
|
||||
* DB::getConn()→startTransaction($transaction_mode, $session_characteristics)
|
||||
* DB::getConn()→transactionSavepoint($name)
|
||||
* DB::getConn()→transactionRollback($savepoint)
|
||||
* DB::getConn()→endTransaction();
|
||||
* DB::get_conn()→startTransaction($transaction_mode, $session_characteristics)
|
||||
* DB::get_conn()→transactionSavepoint($name)
|
||||
* DB::get_conn()→transactionRollback($savepoint)
|
||||
* DB::get_conn()→endTransaction();
|
||||
|
||||
You can create a savepoint by passing a name to the function, and then rollback either all of the uncommited transactions, or if you pass a savepoint name, jump back to the point you'd prefer.
|
||||
You can create a savepoint by passing a name to the function, and then rollback
|
||||
either all of the uncommited transactions, or if you pass a savepoint name,
|
||||
jump back to the point you'd prefer.
|
||||
|
||||
$transaction_mode and $session_characteristic take the full range of isolation levels supported by Postgres.
|
||||
$transaction_mode and $session_characteristic take the full range of isolation
|
||||
levels supported by Postgres.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
|
@ -150,7 +198,9 @@ Please consult the official Postgres documentation for more information.
|
|||
|
||||
**This is an experimental feature.**
|
||||
|
||||
If you have a very large table, you can split it into many child tables. The advantages of this depend on your particular situation. Generally speaking, if your table is very large, queries should be faster.
|
||||
If you have a very large table, you can split it into many child tables. The
|
||||
advantages of this depend on your particular situation. Generally speaking,
|
||||
if your table is very large, queries should be faster.
|
||||
|
||||
You can create a partitioned table like this:
|
||||
|
||||
|
@ -163,7 +213,8 @@ You can create a partitioned table like this:
|
|||
|
||||
'NEW.' is a required part of the configuration string.
|
||||
|
||||
Partitioning should be set up right from the beginning. Partitioning a table which already has data may have unpredictable results.
|
||||
Partitioning should be set up right from the beginning. Partitioning a table
|
||||
which already has data may have unpredictable results.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
|
@ -171,15 +222,21 @@ Please consult the official Postgres documentation for more information.
|
|||
|
||||
**This is an experimental feature.**
|
||||
|
||||
Tablespaces are good for moving the physical files to a faster device (or slower and less used if that's a better option). You can set up a tablespace like this:
|
||||
Tablespaces are good for moving the physical files to a faster device (or slower
|
||||
and less used if that's a better option). You can set up a tablespace like this:
|
||||
|
||||
> public static $database_extensions = array(
|
||||
> 'tablespace'=>Array('name'=>'fastspace', 'location'=>'/faster_location'),
|
||||
> );
|
||||
|
||||
The '/faster_location' path must be owned by the postgres user. If you try to delete a tablespace via the 'drop tablespace' command, then this directory must be empty.
|
||||
The '/faster_location' path must be owned by the postgres user. If you try to
|
||||
delete a tablespace via the 'drop tablespace' command, then this directory must
|
||||
be empty.
|
||||
|
||||
Changing the location of the tablespace through the SilverStripe $database_extensions array will cause the dev/build process to attempt to delete the old location. An error message will be displayed if this location is not empty.
|
||||
Changing the location of the tablespace through the SilverStripe
|
||||
$database_extensions array will cause the dev/build process to attempt to delete
|
||||
the old location. An error message will be displayed if this location is not
|
||||
empty.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
|
@ -187,32 +244,49 @@ Please consult the official Postgres documentation for more information.
|
|||
|
||||
**This is an experimental feature.**
|
||||
|
||||
Index clustering allows you to reorganise the way rows are ordered inside a table according to an index specification. This can be a very intensive disk operation. You specify an index cluster like this:
|
||||
Index clustering allows you to reorganise the way rows are ordered inside a
|
||||
table according to an index specification. This can be a very intensive disk
|
||||
operation. You specify an index cluster like this:
|
||||
|
||||
> public static $database_extensions = array(
|
||||
> 'cluster'=>'index_name'
|
||||
> );
|
||||
|
||||
Clustering is only applied on a table on the second instance of a dev/build command being run on it (running a cluster command on an empty table is pointless).
|
||||
Clustering is only applied on a table on the second instance of a dev/build
|
||||
command being run on it (running a cluster command on an empty table is
|
||||
pointless).
|
||||
|
||||
Clustering needs to be reapplied on a regular basis if you're updating this table. You can also decrease the fillfactor on that index as well for potential performance gains.
|
||||
Clustering needs to be reapplied on a regular basis if you're updating this
|
||||
table. You can also decrease the fillfactor on that index as well for
|
||||
potential performance gains.
|
||||
|
||||
As an alternative, clustering isn't necessary if you rebuild a table with an ORDER BY clause, where the ORDER BY column is the same as what you'd be clustering it by. The dev/build process does not do table rebuilds, so this is something you'd have to do yourself.
|
||||
As an alternative, clustering isn't necessary if you rebuild a table with
|
||||
an ORDER BY clause, where the ORDER BY column is the same as what you'd be
|
||||
clustering it by. The dev/build process does not do table rebuilds, so this
|
||||
is something you'd have to do yourself.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
**A note about these advanced features**
|
||||
|
||||
The advanced features are here as an experimental offering. They have not been fully tested and their functionality and purpose may change in the future. They are primarily here to offer the ability to handle very large datasets.
|
||||
The advanced features are here as an experimental offering. They have not
|
||||
been fully tested and their functionality and purpose may change in the
|
||||
future. They are primarily here to offer the ability to handle very large
|
||||
datasets.
|
||||
|
||||
They are also features which require the user to be very familiar with both Postgres and how their data works. If you can't predict how your database will be populated, then most of these features will be of little use.
|
||||
They are also features which require the user to be very familiar with both
|
||||
Postgres and how their data works. If you can't predict how your database
|
||||
will be populated, then most of these features will be of little use.
|
||||
|
||||
## User contributed information
|
||||
|
||||
**Provided by dompie**
|
||||
|
||||
If you want to install this on a more secure postgresql server, go to PostgreSQLDatabase.php and set "public static $check_database_exists = false;"
|
||||
Moreover you have to replace in PostgreSQLDatabaseConfigurationHelper.php occourences of
|
||||
If you want to install this on a more secure postgresql server, go to
|
||||
PostgreSQLDatabase.php and set "public static $check_database_exists = false;"
|
||||
|
||||
Moreover you have to replace in PostgreSQLDatabaseConfigurationHelper.php
|
||||
occurrences of
|
||||
|
||||
> $connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
||||
|
||||
|
@ -222,19 +296,26 @@ with
|
|||
> $connstring = "host=$server port=5432 dbname=$dbname {$userPart}{$passwordPart}";
|
||||
|
||||
|
||||
Otherwise this extension will try to connect to "postgres" Database to check DB connection, no matter what you entered in the "Database Name" field during installation.
|
||||
Otherwise this extension will try to connect to "postgres" Database to check DB
|
||||
connection, no matter what you entered in the "Database Name" field during
|
||||
installation.
|
||||
|
||||
Make sure you have set the "search_path" correct for your database user.
|
||||
|
||||
## Known Issues
|
||||
|
||||
All column and table names must be double-quoted. PostgreSQL automatically lower-cases columns, and your queries will fail if you don't.
|
||||
All column and table names must be double-quoted. PostgreSQL automatically
|
||||
lower-cases columns, and your queries will fail if you don't.
|
||||
|
||||
Ts_vector columns are not automatically detected by the built-in search filters.
|
||||
That means if you're doing a search through the CMS on a ModelAdmin object, it will use LIKE queries which are very slow.
|
||||
If you're writing your own front-end search system, you can specify the columns to use for search purposes, and you get the full benefits of T-Search.
|
||||
That means if you're doing a search through the CMS on a ModelAdmin object, it
|
||||
will use LIKE queries which are very slow.
|
||||
|
||||
If you are using unsupported modules, there may be instances of MySQL-specific SQL queries which will need to be made database-agnostic where possible.
|
||||
If you're writing your own front-end search system, you can specify the columns
|
||||
to use for search purposes, and you get the full benefits of T-Search.
|
||||
|
||||
If you are using unsupported modules, there may be instances of MySQL-specific
|
||||
SQL queries which will need to be made database-agnostic where possible.
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,222 @@
|
|||
# PostgreSQL Database Module
|
||||
|
||||
## Features
|
||||
|
||||
Here is a quick list of what's different in the Postgres module (a full
|
||||
description follows afterwards):
|
||||
|
||||
* T-Search
|
||||
* Extended index support
|
||||
* Array data types
|
||||
* Transactions
|
||||
* Table partitioning
|
||||
* Tablespaces
|
||||
* Index clustering
|
||||
|
||||
If you don't know much about databases, or don't want to use any of the
|
||||
advanced features that this module provides, then you don't need to read
|
||||
any further.
|
||||
|
||||
The use of any of these features, especially the advanced options, implies
|
||||
that you have some level of comfort in administrating a Postgres database.
|
||||
|
||||
### T-Search
|
||||
|
||||
T-Search support is provided via both GiST and GIN. You can cluster and
|
||||
search columns with combinations of these methods. It is up to you to
|
||||
decide which is most appropriate for your data.
|
||||
|
||||
The dev/build process automatically creates a special column on each table,
|
||||
and a trigger is automatically set up to update this column whenever the
|
||||
targeted columns are changed. T-Search uses this column to return matches
|
||||
for search criteria.
|
||||
|
||||
Please see tutorial 4 for information how to enable fulltext search and the
|
||||
necessary controller hooks.
|
||||
|
||||
### Extended index support
|
||||
|
||||
Indexes have been extended to include support for more options. These new
|
||||
options include:
|
||||
|
||||
* The ability to specify index methods (btree/hash/). Btree is probably
|
||||
fine nearly all indexes, and it is the default. 'Unique' is also supported.
|
||||
* Partial indexes. This is especially handy for creating an index while i
|
||||
gnoring nulls or default data.
|
||||
* Multiple column indexing. If your WHERE clauses always use the same
|
||||
columns, then you can create one index covering all of these at once.
|
||||
* Fill factor. If your table content is static, then you can reduce the
|
||||
physical disk space your index uses. Also, if you use clustering, giving the
|
||||
fillfactor a low number may help performance for updates.
|
||||
|
||||
Examples:
|
||||
|
||||
**Hash index**:
|
||||
|
||||
> public static $indexes = array(
|
||||
> 'Address'=>Array('type'=>'hash', 'name'=>'Address'),
|
||||
> );
|
||||
|
||||
**Where clause**:
|
||||
|
||||
> public static $indexes = array(
|
||||
> 'Address'=>Array('type'=>'unique', 'name'=>'Address', 'where'=>"\"Address\" IS NOT NULL"),
|
||||
> );
|
||||
|
||||
**Fill factor**:
|
||||
|
||||
> public static $indexes = array(
|
||||
> 'Address'=>Array('type'=>'unique', 'name'=>'Address', 'fillfactor'=>'50'),
|
||||
> );
|
||||
|
||||
### Array data types
|
||||
|
||||
Nearly all data types in SilverStripe can now be expressed as an array. For
|
||||
example, you can specify an int as this:
|
||||
|
||||
> $db = array (
|
||||
> 'Quantity'=>'Int[]'
|
||||
> )
|
||||
|
||||
You would populate this like so:
|
||||
|
||||
> $item->Quantity='Array[1,2,3...]';
|
||||
|
||||
It also takes object literals if you're more familiar with that or it suits
|
||||
your purpose better, like this:
|
||||
|
||||
> $item->Quantity='{1,2,3}';
|
||||
|
||||
Using arrays as data types means that you can avoid join tables. This is not
|
||||
recommended if the SilverStripe ORM would expect a has_one or has_many etc under
|
||||
normal circumstances, but it could be useful in the case where you have a very
|
||||
large join table. You can also index these arrays with GIN indexes.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
### Transactions
|
||||
|
||||
Transactions are supported at the database connection level. The relevant
|
||||
functions are:
|
||||
|
||||
* DB::get_conn()→startTransaction($transaction_mode, $session_characteristics)
|
||||
* DB::get_conn()→transactionSavepoint($name)
|
||||
* DB::get_conn()→transactionRollback($savepoint)
|
||||
* DB::get_conn()→endTransaction();
|
||||
|
||||
You can create a savepoint by passing a name to the function, and then rollback
|
||||
either all of the uncommited transactions, or if you pass a savepoint name,
|
||||
jump back to the point you'd prefer.
|
||||
|
||||
$transaction_mode and $session_characteristic take the full range of isolation
|
||||
levels supported by Postgres.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
### Table Partitioning
|
||||
|
||||
**This is an experimental feature.**
|
||||
|
||||
If you have a very large table, you can split it into many child tables. The
|
||||
advantages of this depend on your particular situation. Generally speaking,
|
||||
if your table is very large, queries should be faster.
|
||||
|
||||
You can create a partitioned table like this:
|
||||
|
||||
> public static $database_extensions = array(
|
||||
> 'partitions'=>array(
|
||||
> 'child_table_1'=>'NEW."ID">0 AND NEW."ID"<=100',
|
||||
> 'child_table_2'=>'NEW."ID">100 AND NEW."ID"<=200'
|
||||
> )
|
||||
> );
|
||||
|
||||
'NEW.' is a required part of the configuration string.
|
||||
|
||||
Partitioning should be set up right from the beginning. Partitioning a table
|
||||
which already has data may have unpredictable results.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
### Tablespaces
|
||||
|
||||
**This is an experimental feature.**
|
||||
|
||||
Tablespaces are good for moving the physical files to a faster device (or slower
|
||||
and less used if that's a better option). You can set up a tablespace like this:
|
||||
|
||||
> public static $database_extensions = array(
|
||||
> 'tablespace'=>Array('name'=>'fastspace', 'location'=>'/faster_location'),
|
||||
> );
|
||||
|
||||
The '/faster_location' path must be owned by the postgres user. If you try to
|
||||
delete a tablespace via the 'drop tablespace' command, then this directory must
|
||||
be empty.
|
||||
|
||||
Changing the location of the tablespace through the SilverStripe
|
||||
$database_extensions array will cause the dev/build process to attempt to delete
|
||||
the old location. An error message will be displayed if this location is not
|
||||
empty.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
### Index Clustering
|
||||
|
||||
**This is an experimental feature.**
|
||||
|
||||
Index clustering allows you to reorganise the way rows are ordered inside a
|
||||
table according to an index specification. This can be a very intensive disk
|
||||
operation. You specify an index cluster like this:
|
||||
|
||||
> public static $database_extensions = array(
|
||||
> 'cluster'=>'index_name'
|
||||
> );
|
||||
|
||||
Clustering is only applied on a table on the second instance of a dev/build
|
||||
command being run on it (running a cluster command on an empty table is
|
||||
pointless).
|
||||
|
||||
Clustering needs to be reapplied on a regular basis if you're updating this
|
||||
table. You can also decrease the fillfactor on that index as well for
|
||||
potential performance gains.
|
||||
|
||||
As an alternative, clustering isn't necessary if you rebuild a table with
|
||||
an ORDER BY clause, where the ORDER BY column is the same as what you'd be
|
||||
clustering it by. The dev/build process does not do table rebuilds, so this
|
||||
is something you'd have to do yourself.
|
||||
|
||||
Please consult the official Postgres documentation for more information.
|
||||
|
||||
**A note about these advanced features**
|
||||
|
||||
The advanced features are here as an experimental offering. They have not
|
||||
been fully tested and their functionality and purpose may change in the
|
||||
future. They are primarily here to offer the ability to handle very large
|
||||
datasets.
|
||||
|
||||
They are also features which require the user to be very familiar with both
|
||||
Postgres and how their data works. If you can't predict how your database
|
||||
will be populated, then most of these features will be of little use.
|
||||
|
||||
## User contributed information
|
||||
|
||||
**Provided by dompie**
|
||||
|
||||
If you want to install this on a more secure postgresql server, go to
|
||||
PostgreSQLDatabase.php and set "public static $check_database_exists = false;"
|
||||
|
||||
Moreover you have to replace in PostgreSQLDatabaseConfigurationHelper.php
|
||||
occurrences of
|
||||
|
||||
> $connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
||||
|
||||
with
|
||||
|
||||
> $dbname = $databaseConfig['database']?$databaseConfig['database']: 'postgres';
|
||||
> $connstring = "host=$server port=5432 dbname=$dbname {$userPart}{$passwordPart}";
|
||||
|
||||
|
||||
Otherwise this extension will try to connect to "postgres" Database to check DB
|
||||
connection, no matter what you entered in the "Database Name" field during
|
||||
installation.
|
||||
|
||||
Make sure you have set the "search_path" correct for your database user.
|
|
@ -0,0 +1,33 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ruleset name="SilverStripe">
|
||||
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
|
||||
|
||||
<file>code</file>
|
||||
<file>tests</file>
|
||||
|
||||
<!-- base rules are PSR-2 -->
|
||||
<rule ref="PSR2" >
|
||||
<!-- Current exclusions -->
|
||||
<exclude name="PSR1.Methods.CamelCapsMethodName" />
|
||||
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
|
||||
<exclude name="PSR2.Classes.PropertyDeclaration" />
|
||||
<exclude name="PSR2.ControlStructures.SwitchDeclaration" /> <!-- causes php notice while linting -->
|
||||
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase" />
|
||||
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenerdefault" />
|
||||
<exclude name="PSR2.ControlStructures.SwitchDeclaration.TerminatingComment" />
|
||||
<exclude name="PSR2.Methods.MethodDeclaration.Underscore" />
|
||||
<exclude name="Squiz.Scope.MethodScope" />
|
||||
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
|
||||
<exclude name="Generic.Files.LineLength.TooLong" />
|
||||
<exclude name="PEAR.Functions.ValidDefaultValue.NotAtEnd" />
|
||||
</rule>
|
||||
|
||||
<!-- include php files only -->
|
||||
<arg name="extensions" value="php,lib,inc,php5"/>
|
||||
|
||||
<!-- PHP-PEG generated file not intended for human consumption -->
|
||||
<exclude-pattern>*/SSTemplateParser.php$</exclude-pattern>
|
||||
<exclude-pattern>*/_fakewebroot/*</exclude-pattern>
|
||||
<exclude-pattern>*/fixtures/*</exclude-pattern>
|
||||
</ruleset>
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
|
||||
|
||||
<testsuite name="Default">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
|
||||
<filter>
|
||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">.</directory>
|
||||
<exclude>
|
||||
<directory suffix=".php">tests/</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
</phpunit>
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\PostgreSQL\PostgreSQLConnector;
|
||||
|
||||
class PostgreSQLConnectorTest extends SapphireTest
|
||||
{
|
||||
public function testSubstitutesPlaceholders()
|
||||
{
|
||||
$connector = new PostgreSQLConnector();
|
||||
|
||||
// basic case
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1",
|
||||
$connector->replacePlaceholders("SELECT * FROM Table WHERE ID = ?")
|
||||
);
|
||||
|
||||
// Multiple variables
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Name = $2",
|
||||
$connector->replacePlaceholders("SELECT * FROM Table WHERE ID = ? AND Name = ?")
|
||||
);
|
||||
|
||||
// Ignoring question mark placeholders within string literals
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Name = $2 AND Content = '<p>What is love?</p>'",
|
||||
$connector->replacePlaceholders(
|
||||
"SELECT * FROM Table WHERE ID = ? AND Name = ? AND Content = '<p>What is love?</p>'"
|
||||
)
|
||||
);
|
||||
|
||||
// Ignoring question mark placeholders within string literals with escaped slashes
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Title = '\\'' AND Content = '<p>What is love?</p>' AND Name = $2",
|
||||
$connector->replacePlaceholders(
|
||||
"SELECT * FROM Table WHERE ID = ? AND Title = '\\'' AND Content = '<p>What is love?</p>' AND Name = ?"
|
||||
)
|
||||
);
|
||||
|
||||
// same as above, but use double single quote escape syntax
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Title = '''' AND Content = '<p>What is love?</p>' AND Name = $2",
|
||||
$connector->replacePlaceholders(
|
||||
"SELECT * FROM Table WHERE ID = ? AND Title = '''' AND Content = '<p>What is love?</p>' AND Name = ?"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @package postgresql
|
||||
* @subpackage tests
|
||||
*/
|
||||
class PostgreSQLDatabaseTest extends SapphireTest {
|
||||
function testReadOnlyTransaction(){
|
||||
|
||||
if(
|
||||
DB::getConn()->supportsTransactions() == true
|
||||
&& DB::getConn() instanceof PostgreSQLDatabase
|
||||
){
|
||||
|
||||
$page=new Page();
|
||||
$page->Title='Read only success';
|
||||
$page->write();
|
||||
|
||||
DB::getConn()->transactionStart('READ ONLY');
|
||||
|
||||
try {
|
||||
$page=new Page();
|
||||
$page->Title='Read only page failed';
|
||||
$page->write();
|
||||
} catch (Exception $e) {
|
||||
//could not write this record
|
||||
//We need to do a rollback or a commit otherwise we'll get error messages
|
||||
DB::getConn()->transactionRollback();
|
||||
}
|
||||
|
||||
DB::getConn()->transactionEnd();
|
||||
|
||||
DataObject::flush_and_destroy_cache();
|
||||
|
||||
$success=DataObject::get('Page', "\"Title\"='Read only success'");
|
||||
$fail=DataObject::get('Page', "\"Title\"='Read only page failed'");
|
||||
|
||||
//This page should be in the system
|
||||
$this->assertTrue(is_object($success) && $success->exists());
|
||||
|
||||
//This page should NOT exist, we had 'read only' permissions
|
||||
$this->assertFalse(is_object($fail) && $fail->exists());
|
||||
|
||||
} else {
|
||||
$this->markTestSkipped('Current database is not PostgreSQL');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
use SilverStripe\PostgreSQL\PostgreSQLQueryBuilder;
|
||||
|
||||
class PostgreSQLQueryBuilderTest extends SapphireTest
|
||||
{
|
||||
public function testLongAliases()
|
||||
{
|
||||
$query = new SQLSelect();
|
||||
$longstring = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
$alias2 = $longstring . $longstring;
|
||||
$query->selectField('*');
|
||||
$query->addFrom('"Base"');
|
||||
$query->addLeftJoin(
|
||||
'Joined',
|
||||
"\"Base\".\"ID\" = \"{$alias2}\".\"ID\"",
|
||||
$alias2
|
||||
);
|
||||
$query->addWhere([
|
||||
"\"{$alias2}\".\"Title\" = ?" => 'Value',
|
||||
]);
|
||||
|
||||
$identifier = "c4afb43_hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
$this->assertEquals(PostgreSQLQueryBuilder::MAX_TABLE, strlen($identifier));
|
||||
|
||||
$expected = <<<SQL
|
||||
SELECT *
|
||||
FROM "Base" LEFT JOIN "Joined" AS "c4afb43_hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
ON "Base"."ID" = "c4afb43_hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"."ID"
|
||||
WHERE ("c4afb43_hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"."Title" = ?)
|
||||
SQL;
|
||||
$builder = new PostgreSQLQueryBuilder();
|
||||
$sql = $builder->buildSQL($query, $params);
|
||||
|
||||
$this->assertSQLEquals($expected, $sql);
|
||||
$this->assertEquals(['Value'], $params);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
namespace SilverStripe\PostgreSQL\Tests;
|
||||
|
||||
use SilverStripe\Core\ClassInfo;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\ORM\Connect\Database;
|
||||
use SilverStripe\ORM\Connect\DatabaseException;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\PostgreSQL\PostgreSQLConnector;
|
||||
use SilverStripe\PostgreSQL\PostgreSQLSchemaManager;
|
||||
|
||||
class PostgreSQLSchemaManagerTest extends SapphireTest
|
||||
{
|
||||
|
||||
protected $usesTransactions = false;
|
||||
|
||||
public function testAlterTable()
|
||||
{
|
||||
try {
|
||||
/** @var PostgreSQLSchemaManager $dbSchema */
|
||||
$dbSchema = DB::get_schema();
|
||||
$dbSchema->quiet();
|
||||
|
||||
$this->createSS3Table();
|
||||
|
||||
try {
|
||||
DB::query('INSERT INTO "ClassNamesUpgrade" ("ClassName") VALUES (\'App\MySite\FooBar\')');
|
||||
$this->assertFalse(true, 'SS3 Constaint should have blocked the previous insert.');
|
||||
} catch (DatabaseException $ex) {
|
||||
}
|
||||
|
||||
$dbSchema->schemaUpdate(function () use ($dbSchema) {
|
||||
$dbSchema->requireTable(
|
||||
'ClassNamesUpgrade',
|
||||
[
|
||||
'ID' => 'PrimaryKey',
|
||||
'ClassName' => 'Enum(array("App\\\\MySite\\\\FooBar"))',
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
DB::query('INSERT INTO "ClassNamesUpgrade" ("ClassName") VALUES (\'App\MySite\FooBar\')');
|
||||
$count = DB::query('SELECT count(*) FROM "ClassNamesUpgrade" WHERE "ClassName" = \'App\MySite\FooBar\'')
|
||||
->value();
|
||||
|
||||
$this->assertEquals(1, $count);
|
||||
} finally {
|
||||
DB::query('DROP TABLE IF EXISTS "ClassNamesUpgrade"');
|
||||
DB::query('DROP SEQUENCE IF EXISTS "ClassNamesUpgrade_ID_seq"');
|
||||
}
|
||||
}
|
||||
|
||||
private function createSS3Table()
|
||||
{
|
||||
DB::query(<<<SQL
|
||||
CREATE SEQUENCE "ClassNamesUpgrade_ID_seq" start 1 increment 1;
|
||||
CREATE TABLE "ClassNamesUpgrade"
|
||||
(
|
||||
"ID" bigint NOT NULL DEFAULT nextval('"ClassNamesUpgrade_ID_seq"'::regclass),
|
||||
"ClassName" character varying(255) DEFAULT 'ClassNamesUpgrade'::character varying,
|
||||
CONSTRAINT "ClassNamesUpgrade_pkey" PRIMARY KEY ("ID"),
|
||||
CONSTRAINT "ClassNamesUpgrade_ClassName_check" CHECK ("ClassName"::text = ANY (ARRAY['FooBar'::character varying::text]))
|
||||
)
|
||||
WITH (
|
||||
OIDS=FALSE
|
||||
);
|
||||
SQL
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenameTable()
|
||||
{
|
||||
try {
|
||||
/** @var PostgreSQLSchemaManager $dbSchema */
|
||||
$dbSchema = DB::get_schema();
|
||||
$dbSchema->quiet();
|
||||
|
||||
$this->createSS3VersionedTable();
|
||||
|
||||
$this->assertConstraintCount(1, 'ClassNamesUpgrade_versioned_ClassName_check');
|
||||
|
||||
$dbSchema->schemaUpdate(function () use ($dbSchema) {
|
||||
$dbSchema->renameTable(
|
||||
'ClassNamesUpgrade_versioned',
|
||||
'ClassNamesUpgrade_Versioned'
|
||||
);
|
||||
});
|
||||
|
||||
$this->assertTableCount(0, 'ClassNamesUpgrade_versioned');
|
||||
$this->assertTableCount(1, 'ClassNamesUpgrade_Versioned');
|
||||
$this->assertConstraintCount(0, 'ClassNamesUpgrade_versioned_ClassName_check');
|
||||
$this->assertConstraintCount(1, 'ClassNamesUpgrade_Versioned_ClassName_check');
|
||||
} finally {
|
||||
DB::query('DROP TABLE IF EXISTS "ClassNamesUpgrade_Versioned"');
|
||||
DB::query('DROP TABLE IF EXISTS "ClassNamesUpgrade_versioned"');
|
||||
DB::query('DROP SEQUENCE IF EXISTS "ClassNamesUpgrade_versioned_ID_seq"');
|
||||
}
|
||||
}
|
||||
|
||||
private function assertConstraintCount($expected, $constraintName)
|
||||
{
|
||||
$count = DB::prepared_query(
|
||||
'SELECT count(*) FROM pg_catalog.pg_constraint WHERE conname like ?',
|
||||
[$constraintName]
|
||||
)->value();
|
||||
|
||||
$this->assertEquals($expected, $count);
|
||||
}
|
||||
|
||||
private function assertTableCount($expected, $tableName)
|
||||
{
|
||||
$count = DB::prepared_query(
|
||||
'SELECT count(*) FROM pg_catalog.pg_tables WHERE "tablename" like ?',
|
||||
[$tableName]
|
||||
)->value();
|
||||
|
||||
$this->assertEquals($expected, $count);
|
||||
}
|
||||
|
||||
private function createSS3VersionedTable()
|
||||
{
|
||||
DB::query(<<<SQL
|
||||
CREATE SEQUENCE "ClassNamesUpgrade_versioned_ID_seq" start 1 increment 1;
|
||||
CREATE TABLE "ClassNamesUpgrade_versioned"
|
||||
(
|
||||
"ID" bigint NOT NULL DEFAULT nextval('"ClassNamesUpgrade_versioned_ID_seq"'::regclass),
|
||||
"ClassName" character varying(255) DEFAULT 'ClassNamesUpgrade'::character varying,
|
||||
CONSTRAINT "ClassNamesUpgrade_pkey" PRIMARY KEY ("ID"),
|
||||
CONSTRAINT "ClassNamesUpgrade_versioned_ClassName_check" CHECK ("ClassName"::text = ANY (ARRAY['FooBar'::character varying::text]))
|
||||
)
|
||||
WITH (
|
||||
OIDS=FALSE
|
||||
);
|
||||
SQL
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue