Compare commits

..

165 Commits
0.9 ... master

Author SHA1 Message Date
Damian Mooyman
34fb10580a
Merge pull request #54 from dhensby/pulls/transaction-fixes
FIX Make sure nested transactions get reset on implicit commits
2018-06-28 10:56:18 +12:00
Daniel Hensby
9d76e2a042
FIX Make sure nested transactions get reset on implicit commits 2018-06-20 12:49:08 +01:00
Damian Mooyman
8b519f9bcf
Add license file
Fixes #52
2018-06-07 10:01:22 +12:00
Loz Calver
579050c8f7
Update branch alias 2018-05-24 12:09:46 +01:00
Daniel Hensby
7be531dd7f
Merge pull request #49 from kinglozzer/generators
Update SQLServerQuery to use generators
2018-03-17 15:06:11 +00:00
Loz Calver
321b9fe890 Update SQLServerQuery to use generators 2018-03-16 16:52:47 +00:00
Damian Mooyman
23d4614204
Merge pull request #48 from dhensby/pulls/nested-transactions
FIX Support nested transactions
2018-02-15 14:21:52 +13:00
Daniel Hensby
6b3959ba48
FIX Support nested transactions 2018-02-08 21:37:59 +00:00
Daniel Hensby
406fcee3cd
Merge pull request #47 from NightJar/twowards-ss4-support
WIP: Stable SilverStripe 4 Support
2018-01-19 15:38:56 +00:00
Dylan Wagstaff
76fb2b29dc Fix class loading issues 2018-01-19 16:27:18 +13:00
Damian Mooyman
2b6a70e529
Merge remote-tracking branch 'origin/1'
# Conflicts:
#	code/MSSQLDatabase.php
#	code/MSSQLSchemaManager.php
#	composer.json
2017-12-07 16:05:30 +13:00
Damian Mooyman
aa21b10005
Merge pull request #1 from silverstripe-security/patch/1/SS-2017-008
[SS-2017-008] Fix SQL injection in search engine
2017-12-07 15:58:57 +13:00
Daniel Hensby
ada270c884
[SS-2017-008] Fix SQL injection in search engine 2017-11-22 11:52:50 +00:00
Damian Mooyman
0f8c146e99 Merge pull request #43 from brettt89/fix-dropping-indexes-without-checking
Fix Dropping indexes on dev/build upgrade path
2017-02-15 15:34:17 +13:00
Brett Tasker
dac4be1a51 Fix DROP INDEX not checking correct schema 2017-02-15 15:24:46 +13:00
Sean Harvey
761b42f24a Merge pull request #42 from ajoneil/1
Fix index issues in /dev/build
2017-02-13 08:39:51 +13:00
Andrew O'Neil
d9ec128735 Fix index issues in /dev/build 2017-02-03 11:47:48 +11:00
Damian Mooyman
6a25ac4ab4 BUG Fix installer for 4.0 (#40) 2016-10-26 14:24:22 +13:00
Damian Mooyman
a34d67c753 Rename SS_ prefixed classes (#39) 2016-09-09 18:55:53 +12:00
Ingo Schommer
bdc3197351 Merge pull request #37 from open-sausages/pulls/4.0/namespace-everything
Upgrade code for core namespaces
2016-09-08 16:11:32 +12:00
Damian Mooyman
da6cefac06 Upgrade code for core namespaces 2016-08-30 13:08:11 +12:00
Sean Harvey
551ccfd352 Merge pull request #35 from tractorcow/pulls/docs
Updated docs
2016-08-17 09:40:54 +12:00
Damian Mooyman
97d8750f3d
Updated docs
Fix support for freetds
Fixes #19
Fixes #32
2016-08-16 18:11:58 +12:00
Damian Mooyman
9309cf3a50 API Namespace MSSQL connector (#31)
API Update to support customtable names
Update to support new namespaced orm
Cleanup PHPDoc
2016-07-15 13:57:54 +12:00
Daniel Hensby
f3574a616b Merge pull request #33 from silverstripe/fixes/ifindexexists
FIX: Check index exists before dropping
2016-07-13 10:02:34 +01:00
Will Rossiter
2e64e44087 FIX: Check index exists before dropping 2016-07-13 14:47:38 +12:00
Damian Mooyman
a3931654b7
Lock 1 branch to ^4 2016-07-04 10:17:46 +12:00
Damian Mooyman
04cf84be71 Merge pull request #30 from helpfulrobot/add-standard-code-of-conduct-file
Added standard code of conduct file
2016-02-16 16:04:36 +13:00
helpfulrobot
95a28e3ffe Added standard code of conduct file 2016-02-16 12:30:27 +13:00
Damian Mooyman
1d5d71c956 Merge pull request #29 from helpfulrobot/add-standard-gitattributes-file
Added standard .gitattributes file
2016-01-18 17:34:31 +13:00
helpfulrobot
23e867a918 Added standard .gitattributes file 2016-01-16 18:01:41 +13:00
Damian Mooyman
31eaa833ed Merge pull request #27 from helpfulrobot/convert-to-psr-2
Converted to PSR-2
2015-12-18 10:16:25 +13:00
helpfulrobot
4badb07597 Converted to PSR-2 2015-12-18 07:50:59 +13:00
Damian Mooyman
8da02fba62 Merge pull request #26 from helpfulrobot/add-standard-editorconfig-file
Added standard .editorconfig file
2015-12-17 14:06:40 +13:00
helpfulrobot
2eaa607a67 Added standard .editorconfig file 2015-12-17 10:22:29 +13:00
Sam Minnee
2cdd30f7d3 Added basic scrutinizer config.
This scrutinizer config should start generating scrutinizer badges for the mssql module.
2015-08-12 14:09:32 +12:00
Sam Minnee
1fab860174 FIX: Fix errors in Appveyor config
There were unfortunately a few errors in the Appveyor config. This commit
fixes them.
2015-08-12 14:03:47 +12:00
Sean Harvey
8101ca851d Merge pull request #25 from sminnee/master
Add Appveyor test configuration for Windows-based CI
2015-08-12 11:23:23 +12:00
Sam Minnee
4357cd38c3 Add composer hints to let 'composer install' run on module.
Scrutinizer will download the module and run 'composer install' to check
the module's code. Without these changes, it is looking for a stable version
of 3.2, which doesn't currently exist.
2015-08-12 11:16:31 +12:00
Sam Minnee
22b367be66 Add AppVeyor build status badge 2015-08-12 11:13:05 +12:00
Sam Minnee
1ec1ca86aa Add Appveyor test configuration for Windows-based CI
We can't use Travis for our Windows testing, so I have configured this
test run for Appveyor. It will run Windows + MSSQL!
2015-08-12 10:53:53 +12:00
Daniel Hensby
ade1ea69d7 Merge pull request #23 from tractorcow/pulls/1.0/update-api
Update API for 3.2 release
2015-06-17 11:13:13 +01:00
Damian Mooyman
be2e9469fd Update API for 3.2 release
See https://github.com/silverstripe/silverstripe-framework/pull/4288
2015-06-17 13:58:45 +12:00
Damian Mooyman
dd23af4278 Merge pull request #18 from ClaySolutions/master
Add bigint support
2015-02-25 08:51:58 +13:00
Ingo Schommer
1fedcfb37e Merge pull request #20 from dhensby/master
Adding .editorconfig
2015-01-04 11:07:17 +13:00
Damian Mooyman
16513227cf Relax framework requirement to include 4.0 2014-12-05 09:32:33 +13:00
Daniel Hensby
b7eb89cd17 Adding .editorconfig 2014-12-04 17:02:13 +00:00
ClayLennart
15ff8640a2 Add bigint support 2014-07-15 09:34:19 +02:00
Sean Harvey
05faf24483 Merge pull request #17 from halkyon/int_fix2
Fixing use of "bigint" and "decimal" for pure integer types.
2014-07-15 09:25:39 +12:00
Sean Harvey
093df443ea Fixing use of "bigint" and "decimal" for pure integer types.
See https://github.com/silverstripe/silverstripe-mssql/issues/14
for more info.

"bigint" doesn't work for PHP 32-bit, as the integer could get too big
for PHP to be able to interpret the value as an integer type. In that
case it would try to approximate it as a float, but that could lead
to a bad assumption for identity columns.

For compatibility, stick with standard "int" type which has a maximum
value of 2147483647.

Note that MySQLAdapter already has this maximum, so changing it to
this keeps this database adapter consistent with MySQL.
2014-07-11 13:46:44 +12:00
Sean Harvey
a6a9b25901 Removing reference to postgres, it doesn't belong in the mssql module. 2014-07-11 13:41:45 +12:00
Simon Welsh
73976c111a Merge pull request #7 from tractorcow/3.2-pdo-connector
API Upgraded module to use new database ORM
2014-07-11 09:31:06 +10:00
Simon Welsh
d50ba80512 Merge pull request #15 from tractorcow/pulls/alias-master
Alias master as 1.0
2014-07-11 09:21:54 +10:00
Damian Mooyman
0bf7212b6e Alias master as 1.0 2014-07-11 09:49:04 +12:00
Damian Mooyman
758fe810a5 API Upgraded module to use new database ORM 2014-07-11 09:46:32 +12:00
Simon Welsh
a9b1e03202 Minimum version is now 3.2 2014-07-09 19:39:10 +10:00
Sean Harvey
752664613e Merge pull request #13 from nikrolls/bug_indexes-not-fully-escaped
BUG: Indexes were not being fully escaped
2014-01-20 13:39:45 -08:00
Nik Rolls
e884a1cc31 BUG: Indexes were not being fully escaped (the parent table name was, but if the relational table is also namespaced then it would trigger an error). 2014-01-18 21:30:27 +13:00
Sean Harvey
e3db6f916d Updating README with more up to date instructions and requirements. 2013-09-13 16:33:34 +12:00
Sean Harvey
fc58ae6571 Removing redundant information from README 2013-09-13 16:16:30 +12:00
Sean Harvey
988d36d0d3 Upping requirements to 3.0+ in latest version of the module. 2013-09-13 16:10:35 +12:00
Sean Harvey
91a2a6ceb4 Merge branch '3.0-index-fixes' of git://github.com/tractorcow/silverstripe-mssql into tractorcow-3.0-index-fixes 2013-09-13 15:15:10 +12:00
Sean Harvey
696c889fc1 Merge pull request #6 from ss23/patch-1
Update MSSQLDatabaseConfigurationHelper.php
2013-06-20 20:04:47 -07:00
Sean Harvey
01d376f501 BUG Fixing problem where duplicate items appear on pages
Reverts 13cdb07539 and instead calls
setLimitItems(false) on PaginatedList to tell it we've already
limited the number of items to be shown.
2013-05-27 15:21:24 +12:00
Sean Harvey
1560bad60c Free the result when at the end of a set using mssql drivers 2013-05-22 22:38:05 +12:00
Sean Harvey
7119d5ac80 Merge pull request #11 from mattgunn/patch-1
Removing paging subset occuring before pagination is set
2013-05-16 17:07:27 -07:00
Matt Gunn
13cdb07539 Removing paging subset occuring before pagination is set 2013-05-17 11:45:15 +12:00
Sean Harvey
9ae0cecc36 Revert fc3df54eab 2013-04-25 11:33:54 +12:00
Sean Harvey
55c56b9b66 default float spec to 53 so dev/build doesn't continually show as changed
otherwise it continually shows as changed from "float(53) not null..." to
"float not null"
2013-04-25 11:28:05 +12:00
Sean Harvey
fc3df54eab Removing dropping of indexes, has problems with running dev/build twice 2013-04-05 17:23:13 +13:00
Stephen Shkardoon
e500b024e3 Update MSSQLDatabaseConfigurationHelper.php
In preparation for https://github.com/silverstripe/sapphire/pull/1319
Probably should accept this at the same time.

If someone knows of the relevant ALTER permissions in MSSQL, feel free to implement.
2013-03-24 03:05:04 +13:00
Sean Harvey
bd18c0a1f3 removing double up of quotes in getIndexSqlDefinition() 2013-01-07 17:40:30 +13:00
Sean Harvey
d6a075f9c7 Fixing MSSQLDatabase to conform to dev/build change detection
This involves changing a few things around so that it's closer to
how the MySQLDatabase adapter works. indexList() should return an
array in the same format instead of using arbitrary "indexname"
and "spec". indexNames() has also been introduced to MSSQLDatabase
so that we can drop all indexes (except for fulltext and primary key
indexes) on a table when altering the table columns, this helps when
constraints not directly related to a modified column refuse to allow changes.
2013-01-07 16:25:31 +13:00
Sean Harvey
91548f76be Checking for underscore in SortColumn just to be sure 2012-12-20 16:04:44 +13:00
Sean Harvey
7c369a383a BUG Fix SortColumn ORDER BY not working when using a limit
MSSQL doesn't support putting an alias into the OVER clause,
something which is required when limiting results with an offset.
The workaround is to just put the aggregate inline, taken from
the select list.
2012-12-20 15:57:50 +13:00
Sean Harvey
baf3d39cc0 Adding location of known issues to README.md 2012-12-19 20:56:41 +13:00
Sean Harvey
e527665c5c Show the last error a bit more clearly if available 2012-12-19 11:39:51 +13:00
Sean Harvey
f64c662124 Tidying up error reporting in databaseError() and adding getLastError()
Whenever databaseError() is called when a database problem occurred
then the last error is automatically appended to the end of the
general error message.
2012-12-19 11:27:49 +13:00
Sean Harvey
f34996ae1c BUG Remove "empty" keywords when building search SQL 2012-12-19 11:19:48 +13:00
Ingo Schommer
d17ae37411 BUG Support for case sensitive searches
Through newly added Database->comparisonClause() API
2012-12-11 01:47:35 +01:00
Sean Harvey
334c2634ac Removed deprecated transaction methods on MSSQLDatabase 2012-11-16 14:25:13 +13:00
Sam Minnee
37b4005052 Added composer file 2012-11-03 14:14:17 +13:00
Damian Mooyman
93eab9c132 FIXED: Repaired failing test cases (DataObjectSchemaGenerationTest)
FIXED: Incorrect implementation of alterIndex
UPDATED: Improved parsing of index specification by including code borrowed from the postgres database connector module. Possibly a candidate for refactor into SS_Database.
2012-09-24 15:36:59 +12:00
Sean Harvey
4de7829e4c Use more robust Format() in test instead of strtotime() 2012-09-07 16:23:59 +12:00
Sean Harvey
08eea5cde3 Ensure date test is Y-m-d 2012-09-07 16:21:13 +12:00
Sean Harvey
a74470ff52 Merge pull request #3 from silverstripe-rebelalliance/7813
BUG: open ticket 7813 fixing dobule quoting issue in getIndexSqlDefiniti...
2012-08-28 16:28:20 -07:00
Kirk Mayo
357482d6c9 BUG: open ticket 7813 fixing dobule quoting issue in getIndexSqlDefinition 2012-08-27 15:05:37 +12:00
Ingo Schommer
530c97f671 Added supportsTimezoneOverride() method 2012-07-06 11:36:07 +02:00
Ingo Schommer
e752144a6e Added Macports locations to README 2012-07-02 10:24:25 +03:00
Sam Minnee
05e706ea18 BUG: Fixed MSSQL index name generation for tables with \ in the name.
Right now the name of a table with a namespace will contain a '\' character.  The internal index name generated clashes with this, so we've dealt with it by turning the '\' into a '_'
2012-06-26 14:52:18 +12:00
Sam Minnee
22d53b036b BUG: Fixed index generation to meet new SS3 expectations.
In SS3, the column lists passed through for index specifications are now quoted.  The database adaptor needed to be updated to meet this change.
2012-06-26 14:35:59 +12:00
Sean Harvey
0465f1b1a2 MINOR Performance optimisation in MSSQLDatabase::nextRecord() by
building field meta-data only on construct of MSSQLQuery
2012-06-12 13:31:29 +12:00
Sean Harvey
a4c269bf62 BUGFIX Fixing date/datetime handling in MSSQLDatabase
Fixing the date type to actually be "date", and not "datetime" so
that values are returned as "Y-m-d" for dates, and not "Y-m-d H:i:s".

Additionally, fixing a case where using the mssql functions would
return strange datetime formats. We need the datetime field values
in Y-m-d H:i:s. To get this working, we need to inspect the field type
when retrieving rows from the database and re-format the datetime
value at this point. This is done in MSSQLQuery::nextRecord()
2012-06-12 12:57:53 +12:00
Sean Harvey
49625242c1 MINOR Updating README to include error message 2012-06-12 10:05:40 +12:00
Sean Harvey
84ec3a2014 BUGFIX Fixed undefined notice regression 2012-05-08 17:41:01 +12:00
Sean Harvey
f1daa29592 BUGFIX Fixing MSSQLDatabase to work with latest SQLQuery changes in SS3 2012-05-07 16:40:47 +12:00
Sean Harvey
05d24b0843 BUGFIX Ensure DateTime objects are handled correctly 2012-05-04 21:58:48 +12:00
Sean Harvey
30f017335b BUGFIX Fixing SS3 broken search, use simpler query syntax 2012-05-03 14:33:17 +12:00
Sean Harvey
731874772c BUGFIX Fixing broken SS3 query code in MSSQLDatabase 2012-05-03 12:01:50 +12:00
Sean Harvey
a872e30256 BUGFIX Fixing database to use prepareWhere instead which is renamed 2012-05-01 12:08:19 +12:00
Sean Harvey
cb01716d39 BUGFIX Fixing order by fields for SS 3.0 in MSSQLDatabase::sqlQueryToString() 2012-04-27 17:25:50 +12:00
Sean Harvey
c7ae517c99 BUGFIX Fixed support for SS 3.0 with SQLQuery changes in 9938b64cf6ed0e05583633f1b894f89bd1e9f71f 2012-04-27 17:17:55 +12:00
Sean Harvey
b6082a023e ENHANCEMENT Use CONTAINSTABLE for fulltext searches, as it provides better fuzzy search results. 2012-04-26 17:31:12 +12:00
Sean Harvey
375d440677 BUGFIX Ensure that values with unicode characters like macrons are saved
to the database correctly. Overload the prepStringForDB() method and
include an "N" prefix.
2012-03-21 12:03:11 +13:00
Sean Harvey
4ae94d286f MINOR Tidy 2012-03-05 18:31:04 +13:00
Sean Harvey
cdf1c475a5 MINOR Cleaned up question on connecting with non-default instance name 2012-03-05 18:28:39 +13:00
Sean Harvey
e4349f83a6 MINOR Updated README on setting the server without adding more freetds entries 2012-03-05 18:21:25 +13:00
Sean Harvey
2312b49b9c BUGFIX Fixes to searchEngine() to support SS 3.0 ORM changes 2011-12-17 12:43:09 +13:00
Sean Harvey
a9444a8313 MINOR Removed unused variables from MSSQLDatabase::alterTable() 2011-10-13 14:13:33 +13:00
Sean Harvey
6cbe886a33 BUGFIX Fixed case where DROP FULLTEXT INDEX failed because it was being called twice, unncessarily. Do it once when altering a table structure.
MINOR Cleanup of code in MSSQLDatabase
2011-10-13 14:12:58 +13:00
Sean Harvey
c1ba0fafc9 BUGFIX MSSQLDatabase::getIndexSqlDefinition() should check a primary key exists before trying to use an empty variable and causing dev/build failures 2011-10-13 13:57:04 +13:00
Ingo Schommer
4b933d7e73 ENHANCEMENT Optionally filtering by new File.ShowInSearch flag in MSSQLDatabase->searcnEngine() 2011-09-15 16:02:23 +02:00
Sean Harvey
00b81bbdd4 BUGFIX MSSQLDatabase::alterTableAlterColumn() creates ALTER COLUMN clauses twice in the same query for the same column unnecessarily 2011-06-09 13:56:55 +12:00
Ingo Schommer
b772956ce8 BUGFIX Allow omitting FROM clause in sqlQueryToString() 2011-05-19 11:36:14 +12:00
Ingo Schommer
6f5de764d2 API CHANGE Renamed transactions methods from endTransaction() to transactionEnd(), startTransaction() to transactionStart() to comply with new sapphire trunk API 2011-03-11 16:41:14 +13:00
Sean Harvey
e15e444acc MINOR added link to extension page on silverstripe.org 2011-02-23 11:03:26 +13:00
Sean Harvey
718de0b232 Fixed minor formatting issue 2011-02-23 11:02:31 +13:00
Sean Harvey
c8d1fbe03f MINOR Fixed formatting of troubleshooting section of README.md 2011-02-23 11:00:01 +13:00
Sean Harvey
ddd0794720 MINOR Migrated most of the content from http://doc.silverstripe.org/old/modules:mssql to README.md 2011-02-23 10:58:38 +13:00
Sean Harvey
3542d25c4c MINOR Added short intro to mssql README.md file 2011-02-23 10:37:41 +13:00
Geoff Munn
7ea6652e56 MINOR: MSSQL stub function created for Postgres-specific functionality 2011-01-12 02:05:32 +00:00
Sean Harvey
c104222019 MINOR Clean up code of MSSQLDatabase::addslashes() 2010-12-15 23:18:27 +00:00
Andreas Piening
bff9718dd6 MINOR: allow backslashes 2010-12-15 21:48:05 +00:00
Sean Harvey
8f3b255c5e ENHANCEMENT Implemented MSSQLDatabase::allDatabaseNames() and MSSQLDatabase::dropDatabaseByName() so that dev/tests/cleanupdb will work on this database 2010-11-04 22:06:29 +00:00
Sean Harvey
9e1cdc8cfa BUGFIX Ensure that Relevance field check in MSSQLDatabase::searchEngine() also checks for Relevance escaped in double quotes 2010-11-01 03:20:52 +00:00
Sean Harvey
7b6899b3f0 MINOR Clearer documentation on MSSQLDatabase::getPrimaryKey() 2010-10-20 03:21:54 +00:00
Sean Harvey
9a86a6d9a5 MINOR Update on compatibility of MSSQLAzureDatabase 2010-10-20 03:17:19 +00:00
Mateusz Uzdowski
058c29bf69 ENHANCEMENT: allow custom columns in search, honour the classesToSearch, join all objects with base class, so we can query ClassName 2010-10-20 00:52:44 +00:00
Andreas Piening
e83c14236c MINOR: fixed case of tableList(), MINOR: added getIdentityColumn() 2010-10-15 00:55:49 +00:00
Sean Harvey
247470ccc8 API CHANGE Removed MSSQLDatabase::getNextID() as this is deprecated
MINOR Removed commented out code
2010-10-15 00:25:29 +00:00
Sean Harvey
ecea643f76 API CHANGE Renamed EnumValuesFromConstraint() to enumValuesFromCheckClause() as it's more specific to what the method is actually doing, which is parsing a constraint clause 2010-10-14 22:36:55 +00:00
Sean Harvey
14cf1d243c ENHANCEMENT Performance improvements to MSSQLDatabase, mostly involves per-request caching of constraint check clauses since it's such an expensive query. Getting constraint names has been split out into a separate method, as it's a relatively cheap query to make without caching 2010-10-14 21:11:20 +00:00
Sean Harvey
ae1494c313 BUGFIX Renamed MSSQLQuery::destroy() renamed to destruct() so that it is called properly after the object is destroyed 2010-10-14 05:03:01 +00:00
Sean Harvey
35d04ac387 MINOR Tidied up comments, added is_resource() check on MSSQLQuery::seek() to be consistent with the other methods 2010-10-05 00:55:16 +00:00
Sean Harvey
d2082107b5 API CHANGE Re-enabled transaction support in MSSQLDatabase transaction methods. TransactionTest appears to pass on sqlsrv and mssql now 2010-10-02 03:36:04 +00:00
Sean Harvey
e8ba221024 MINOR Reverted r111461, as it's not specific to SQL Server, but a framework level issue. FulltextSearchable decorator should be used instead to decorate existing SiteTree for example 2010-10-02 01:59:20 +00:00
Geoff Munn
159dc13659 ENHANCEMENT: FTS now supports external tables to be specified as part of the value 2010-09-30 23:58:21 +00:00
Sean Harvey
ba4f843819 BUGFIX MSSQLDatabase::formattedDatetimeClause() should account for the timezone when returning a UNIX timestamp 2010-09-29 10:10:51 +00:00
Sean Harvey
083517d02b MINOR Tidy up of code in MSSQLDatabase::__construct() 2010-09-29 04:30:05 +00:00
Sean Harvey
75b1933ca3 BUGFIX #6042 Use single quotes for MSSQLDatabase::$noiseWords as some PHP versions/settings might have trouble with this (thanks mattclegg!) 2010-09-28 19:44:08 +00:00
Mateusz Uzdowski
27b44a3722 BUGFIX: numRecords not supported on sqlsrv, need to count manually 2010-09-01 04:49:07 +00:00
Mateusz Uzdowski
2b5f25455f BUGFIX: limit the query results for pagination 2010-09-01 04:06:36 +00:00
Mateusz Uzdowski
2aff1a9196 ENHANCEMENT: check for empty clause which will break the sql query 2010-08-31 22:19:58 +00:00
Mateusz Uzdowski
bef5b0ed05 BUGFIX: fix TranslatableSearchFormText by supporting fulltext search for MSSQL and using extendedSQL function call that augments queries properly (previously it was using DB::query which does not augment). Added wait to TranslatableSearchFormText so the test actually passes. 2010-08-31 05:52:48 +00:00
Mateusz Uzdowski
da356e5a01 BUGFIX: repair the transactions for sqlsrv driver on Windows. They are still not fully supported, so mark the driver as not supporting transactions. 2010-08-31 03:18:00 +00:00
Mateusz Uzdowski
d066f99d5e BUGFIX: re-enabling MARS, framework assumes it can run DB::query without fetching the results, which leaves the result set open and prohibits executing another query in the mean time 2010-08-30 21:51:09 +00:00
Sean Harvey
1cab98c219 MINOR Updated class documentation for MSSQLDatabase 2010-08-30 05:47:57 +00:00
Sean Harvey
81de1e073f BUGFIX When MSSQL_USE_WINDOWS_AUTHENTICATION is enabled, ensure the connection parameters include CharacterSet, MultipleActiveResultSets as "UTF-U" and boolean false respectively 2010-08-30 05:29:21 +00:00
Mateusz Uzdowski
410d15cf08 BUGFIX: disable MARS, we don't need it/don't support it 2010-08-30 04:18:29 +00:00
Mateusz Uzdowski
94355edcd2 BUGFIX: busy wait for indexing to complete before continuing with the test, so we can be sure the FTS returns up to date results 2010-08-26 20:54:40 +00:00
Mateusz Uzdowski
2745036563 BUGFIX: adjust the full-text search for mssql by stripping out non-alphabetic characters which are ignored by mssql anyway (and some of them crash the search engine) 2010-08-25 23:21:06 +00:00
Mateusz Uzdowski
0b28428a16 BUGFIX: the search for "quoted string" would fail. Changed to ANSI compliant quotes and prevented double-quoting. 2010-08-25 20:44:51 +00:00
Sean Harvey
27c3997061 BUGFIX Fixed fulltext not being dropped before re-adding it in MSSQLDatabase::getIndexSqlDefinition() 2010-07-18 23:14:51 +00:00
Sean Harvey
c44f5ca9ed MINOR Code syntax formatting 2010-07-13 05:46:33 +00:00
Sean Harvey
f211d2cea8 BUGFIX Fixed dev/build always showing changes in nvarchar fields because of a stray whitespace in the spec value 2010-07-13 05:07:40 +00:00
Andreas Piening
77f453c036 ENHANCEMENT: added transaction support 2010-06-24 05:37:45 +00:00
Sean Harvey
1e22a12cd6 BUGFIX MSSQLDatabase::__construct() should use "UTF-8" as a CharacterSet parameter to sqlsrv_connect(), not "utf8" as it's not recognised by that API 2010-06-03 12:06:26 +00:00
Sean Harvey
7c7e81ac70 MINOR Removed stray $error variable that isn't necessary in MSSQLDatabase::query() 2010-06-03 12:05:12 +00:00
Sean Harvey
13f90299cc MINOR Better error debugging message when sqlsrv_errors() is used on MSSQLDatabase::query() on a failed query 2010-06-03 12:03:27 +00:00
Sam Minnee
78d47d312f BUGFIX: Fixed index creation in MSSQL. 2010-06-03 00:58:33 +00:00
Sam Minnee
53156eb94e BUGFIX: Fixed MSSQLDatabase::hasTable() 2010-06-03 00:58:32 +00:00
Sam Minnee
9a1ee37cbd BUGFIX: Prevent dev/build errors with fulltext indexes on MSSQL. 2010-06-03 00:58:32 +00:00
Sam Minnee
feebe26208 API CHANGE: Added MSSQLDatabase::set_collation() to let you set the collation for the nvarchar fields. 2010-06-03 00:58:31 +00:00
Sam Minnee
07ea9353e1 ENHANCEMENT: Switch MSSQL to use the utf8 character set for the connection. 2010-06-03 00:58:31 +00:00
Sean Harvey
8e28fc70f6 MINOR Updated class doc 2010-05-27 23:05:32 +00:00
21 changed files with 2708 additions and 1717 deletions

44
.appveyor.yml Normal file
View File

@ -0,0 +1,44 @@
build: false
shallow_clone: false
platform: 'x86'
clone_folder: C:\projects\silverstripe
services:
- mssql2014
before_test:
- sqlcmd -S "(local)\SQL2014" -Q "Use [master]; CREATE DATABASE [appveyor]"
- SET PATH=C:\Program Files\OpenSSL;C:\tools\php;%PATH%
- cinst -y php -version 5.6.11
- cd c:\tools\php
- ps: cat php.ini-production | %{$_ -replace "memory_limit = 128M","memory_limit = 256M"} | Out-File -Encoding "Default" php.ini
- echo date.timezone="UTC" >> php.ini
- echo extension_dir=ext >> php.ini
- echo extension=php_openssl.dll >> php.ini
- echo extension=php_mbstring.dll >> php.ini
- echo extension=php_curl.dll >> php.ini
- echo extension=php_gd2.dll >> php.ini
- echo extension=php_tidy.dll >> php.ini
- echo extension=php_fileinfo.dll >> php.ini
- php -r "readfile('http://getcomposer.org/installer');" | php
- php -r "readfile('https://dl.dropboxusercontent.com/u/7129062/sqlsrv_unofficial_3.0.2.2.zip');" > sqlsrv.zip
- unzip sqlsrv.zip
- copy sqlsrv_unofficial_3.0.2.2\x64\*.dll ext
- echo extension=php_sqlsrv_56_nts.dll >> php.ini
- echo extension=php_pdo_sqlsrv_56_nts.dll >> php.ini
- ps: echo "php c:\tools\php\composer.phar %*" | Out-File -Encoding "Default" composer.bat
- type composer.bat
- composer --version
- git clone --branch=windows-support git://github.com/silverstripe-labs/silverstripe-travis-support.git c:\projects\travis-support
- php c:\projects\travis-support\travis_setup.php --source c:\projects\silverstripe --target c:\projects\assembled
- cd c:\projects\assembled
test_script:
- cd c:\projects\assembled
- rename phpunit.xml.dist phpunit.xml.dist.orig
- ps: cat phpunit.xml.dist.orig | %{$_ -replace "colors=`"true`"","colors=`"false`""} | Out-File -Encoding "Default" phpunit.xml.dist
- php .\vendor\phpunit\phpunit\composer\bin\phpunit framework/tests
environment:
DB: MSSQL
CORE_RELEASE: master

17
.editorconfig Normal file
View File

@ -0,0 +1,17 @@
# 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
[{*.yml,package.json}]
indent_size = 2
# The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
/tests export-ignore
/.scrutinizer.yml export-ignore

5
.scrutinizer.yml Normal file
View File

@ -0,0 +1,5 @@
inherit: true
checks:
php:
code_rating: true
duplication: true

8
.upgrade.yml Normal file
View File

@ -0,0 +1,8 @@
mappings:
MSSQLAzureDatabase: SilverStripe\MSSQL\MSSQLAzureDatabase
MSSQLDatabase: SilverStripe\MSSQL\MSSQLDatabase
MSSQLDatabaseConfigurationHelper: SilverStripe\MSSQL\MSSQLDatabaseConfigurationHelper
MSSQLQueryBuilder: SilverStripe\MSSQL\MSSQLQueryBuilder
MSSQLSchemaManager: SilverStripe\MSSQL\MSSQLSchemaManager
SQLServerConnector: SilverStripe\MSSQL\SQLServerConnector
SQLServerQuery: SilverStripe\MSSQL\SQLServerQuery

29
LICENSE Normal file
View File

@ -0,0 +1,29 @@
BSD 3-Clause License
Copyright (c) 2018, SilverStripe Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,14 +1,78 @@
# SQL Server Database Module
Allows SilverStripe to use SQL Server databases.
[![Build status](https://ci.appveyor.com/api/projects/status/hep0l5kbhu64n7l3/branch/master?svg=true)](https://ci.appveyor.com/project/sminnee/silverstripe-mssql-nwvfq/branch/master)
## Maintainer Contact
* Sean Harvey (Nickname: halkyon)
<sean (at) silverstripe (dot) com>
* Damian Mooyman (@tractorcow)
## Requirements
Requirements list can be found here: http://doc.silverstripe.org/modules:mssql
* SilverStripe 4+
* SQL Server 2008, 2008 R2, or 2012.
`mssql` PHP api is no longer supported as of 2.0
### *nix
Linux support is only available via the PDO extension. This requires:
* [dblib](http://www.php.net/manual/en/ref.pdo-dblib.php)
* [FreeTDS](http://freetds.org)
### Windows
On windows you can either connect via PDO or `sqlsrv`. Both options require the
[SQL Server Driver for PHP](https://msdn.microsoft.com/library/dn865013.aspx?f=255&MSPPError=-2147217396). "sqlsrv" 3.0+
Note: [SQL Server Express](http://www.microsoft.com/express/Database/) can also be used which is provided free by Microsoft. However, it has limitations such as 10GB maximum database storage.
## Installation
Installation instructions can be found here: http://doc.silverstripe.org/modules:mssql
These steps will install the latest SilverStripe stable, along with this module using [Composer](http://getcomposer.org/):
* Install SilverStripe: `composer create-project silverstripe/installer /my/website/folder`
* Install module: `cd /my/website/folder && composer require silverstripe/mssql ^2`
* Open the SilverStripe installer by browsing to install.php, e.g. **http://localhost/silverstripe/install.php**
* Select **SQL Server 2008+** in the database list and enter your SQL Server database details
## Troubleshooting
*Q: SQL Server resides on a remote host (a different machine) and I can't connect to it from mine.*
A: Please ensure you have enabled TCP access using **SQL Server Configuration Manager** and [opened firewall ports](http://msdn.microsoft.com/en-us/library/ms175043.aspx).
*Q: I just installed SQL Server, but it says that it cannot connect*
A: Sometimes SQL Server will be installed as a non-default instance name, e.g. "SQLExpress" instead of "MSSQLSERVER" (the default.)
If this is the case, you'll need to declare the instance name when setting the server in your PHP database configuration. For example: **(local)\SQLExpress**. The first part before the slash indicates the server host, or IP address. In this case, (local) indicates localhost, which is the same server PHP is running on. The second part is the SQL Server instance name to connect to.
*Q: I'm getting unicode SQL Server errors connecting to SQL Server database (e.g. Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier)*
A: If you are using FreeTDS make sure you're using TDS version 8.0 in **freetds.conf**. If on Windows, ensure you use the [SQL Server Driver for PHP](http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=ccdf728b-1ea0-48a8-a84a-5052214caad9) and **NOT** the mssql drivers provided by PHP.
*Q: Using FreeTDS I can't connect to my SQL Server database. An error in PHP says the server doesn't exist*
A: Make sure you've got an entry in **/etc/freetds/freetds.conf** that points to your server. For example:
[myserver]
host = myserver.mydomain.com
port = 1433
tds version = 8.0
Then you can use "myserver" (the bit in square brackets above) as the server name when connecting to the database.
Note that if you're running Macports, the file is located in **/opt/local/etc/freetds/freetds.conf**.
Alternatively, if you don't want to keep adding more entries to the freetds.conf to nominate more SQL Server locations,
you can instead use the full the host/ip and port combination, such as "myserver:1433" (1433 being the default SQL Server port.)
and ensure the "tds version = 8.0" is set globally in the freetds.conf file.
**Note**: Use *tabs* not spaces when editing freetds.conf, otherwise it will not load the configuration you have specified!
**Note**: Certain distributions of Linux use [SELinux](http://fedoraproject.org/wiki/SELinux) which could block access to your SQL Server database. A rule may need to be added to allow this traffic through.

32
_config/connectors.yml Normal file
View File

@ -0,0 +1,32 @@
---
name: mssqlconnectors
---
SilverStripe\Core\Injector\Injector:
# Connect using PDO
MSSQLPDODatabase:
class: 'SilverStripe\MSSQL\MSSQLDatabase'
properties:
connector: %$PDOConnector
schemaManager: %$MSSQLSchemaManager
queryBuilder: %$MSSQLQueryBuilder
# Uses sqlsrv_connect
MSSQLDatabase:
class: 'SilverStripe\MSSQL\MSSQLDatabase'
properties:
connector: %$SQLServerConnector
schemaManager: %$MSSQLSchemaManager
queryBuilder: %$MSSQLQueryBuilder
# Uses sqlsrv_connect to connect to a MS Azure Database
MSSQLAzureDatabase:
class: 'SilverStripe\MSSQL\MSSQLAzureDatabase'
properties:
connector: %$SQLServerConnector
schemaManager: %$MSSQLSchemaManager
queryBuilder: %$MSSQLQueryBuilder
SQLServerConnector:
class: 'SilverStripe\MSSQL\SQLServerConnector'
type: prototype
MSSQLSchemaManager:
class: 'SilverStripe\MSSQL\MSSQLSchemaManager'
MSSQLQueryBuilder:
class: 'SilverStripe\MSSQL\MSSQLQueryBuilder'

2
_config/mssql.yml Normal file
View File

@ -0,0 +1,2 @@
MSSQLDatabase:
collation: null

54
_register_database.php Normal file
View File

@ -0,0 +1,54 @@
<?php
use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
use SilverStripe\MSSQL\MSSQLDatabaseConfigurationHelper;
// PDO connector for MS SQL Server
/** @skipUpgrade */
DatabaseAdapterRegistry::register(array(
'class' => 'MSSQLPDODatabase',
'module' => 'mssql',
'title' => 'SQL Server 2008 (using PDO)',
'helperPath' => __DIR__.'/code/MSSQLDatabaseConfigurationHelper.php',
'helperClass' => MSSQLDatabaseConfigurationHelper::class,
'supported' => !!MSSQLDatabaseConfigurationHelper::getPDODriver(),
'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.'
));
// Basic driver using sqlsrv connector
/** @skipUpgrade */
DatabaseAdapterRegistry::register(array(
'class' => 'MSSQLDatabase',
'module' => 'mssql',
'title' => 'SQL Server 2008 (using sqlsrv)',
'helperPath' => __DIR__.'/code/MSSQLDatabaseConfigurationHelper.php',
'helperClass' => MSSQLDatabaseConfigurationHelper::class,
'supported' => function_exists('sqlsrv_connect'),
'missingExtensionText' =>
'The <a href="http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx">sqlsrv</a>
PHP extensions is not available. Please install or enable it and refresh this page.',
'fields' => array_merge(DatabaseAdapterRegistry::get_default_fields(), array(
// @todo - do we care about windows authentication for PDO/SQL Server?
'windowsauthentication' => array(
'title' => 'Use Windows authentication? (leave blank for false)',
'default' => ''
)
))
));
// MS Azure uses an online database
/** @skipUpgrade */
DatabaseAdapterRegistry::register(array(
'class' => 'MSSQLAzureDatabase',
'module' => 'mssql',
'title' => 'MS Azure Database (using sqlsrv)',
'helperPath' => __DIR__.'/code/MSSQLDatabaseConfigurationHelper.php',
'helperClass' => MSSQLDatabaseConfigurationHelper::class,
'supported' => function_exists('sqlsrv_connect'),
'missingExtensionText' =>
'The <a href="http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx">sqlsrv</a>
PHP extension is not available. Please install or enable it and refresh this page.'
));

1
code-of-conduct.md Normal file
View File

@ -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).

View File

@ -1,71 +1,113 @@
<?php
/**
* Specific support for SQL Azure databases running on Windows Azure.
* "sqlsrv" for PHP MUST be installed to use SQL Azure. It does not support
* the mssql_*() functions in PHP, as SQL Azure is Windows only.
*
* Some important things about SQL Azure:
*
* Selecting a database is not supported.
* In order to change the database currently in use, you need to connect to
* the database using the "Database" parameter with sqlsrv_connect()
*
* Multiple active result sets are not supported. This means you can't
* have two query results open at once.
*
* Fulltext indexes are not supported.
*
* @author Sean Harvey <sean at silverstripe dot com>
* @package mssql
*/
class MSSQLAzureDatabase extends MSSQLDatabase {
protected $fullTextEnabled = false;
public function __construct($parameters) {
$this->connectDatabase($parameters);
}
/**
* Connect to a SQL Azure database with the given parameters.
* @param array $parameters Connection parameters set by environment
* @return resource SQL Azure database connection link
*/
protected function connectDatabase($parameters) {
$this->dbConn = sqlsrv_connect($parameters['server'], array(
'Database' => $parameters['database'],
'UID' => $parameters['username'],
'PWD' => $parameters['password'],
'MultipleActiveResultSets' => '0'
));
$this->tableList = $this->fieldList = $this->indexList = null;
$this->database = $parameters['database'];
$this->active = true;
$this->mssql = false; // mssql functions don't work with this database
$this->fullTextEnabled = false;
$this->query('SET QUOTED_IDENTIFIER ON');
$this->query('SET TEXTSIZE 2147483647');
}
/**
* Switches to the given database.
*
* If the database doesn't exist, you should call
* createDatabase() after calling selectDatabase()
*
* IMPORTANT: SQL Azure doesn't support "USE", so we need
* to reinitialize the database connection with the requested
* database name.
*
* @param string $dbname The database name to switch to
*/
public function selectDatabase($dbname) {
global $databaseConfig;
$parameters = $databaseConfig;
$parameters['database'] = $dbname;
$this->connectDatabase($parameters);
}
}
<?php
namespace SilverStripe\MSSQL;
/**
* Specific support for SQL Azure databases running on Windows Azure.
* Currently only supports the SQLSRV driver from Microsoft.
*
* Some important things about SQL Azure:
*
* Selecting a database is not supported.
* In order to change the database currently in use, you need to connect to
* the database using the "Database" parameter with sqlsrv_connect()
*
* Multiple active result sets are not supported. This means you can't
* have two query results open at once.
*
* Fulltext indexes are not supported.
*
* @author Sean Harvey <sean at silverstripe dot com>
*/
class MSSQLAzureDatabase extends MSSQLDatabase
{
/**
* List of parameters used to create new Azure connections between databases
*
* @var array
*/
protected $parameters = array();
public function fullTextEnabled()
{
return false;
}
public function __construct($parameters)
{
$this->connectDatabase($parameters);
}
/**
* Connect to a SQL Azure database with the given parameters.
* @param array $parameters Connection parameters set by environment
* - 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
* - windowsauthentication: Not supported for Azure
*/
public function connect($parameters)
{
$this->parameters = $parameters;
$this->connectDatabase($parameters['database']);
}
/**
* Connect to a database using the provided parameters
*
* @param string $database
*/
protected function connectDatabase($database)
{
$parameters = $this->parameters;
$parameters['database'] = $database;
$parameters['multipleactiveresultsets'] = 0;
// Ensure that driver is available (required by PDO)
if (empty($parameters['driver'])) {
$parameters['driver'] = $this->getDatabaseServer();
}
// Notify connector of parameters, instructing the connector
// to connect immediately to the Azure database
$this->connector->connect($parameters, true);
// Configure the connection
$this->query('SET QUOTED_IDENTIFIER ON');
$this->query('SET TEXTSIZE 2147483647');
}
/**
* Switches to the given database.
*
* IMPORTANT: SQL Azure doesn't support "USE", so we need
* to reinitialize the database connection with the requested
* database name.
* @see http://msdn.microsoft.com/en-us/library/windowsazure/ee336288.aspx
*
* @param string $name The database name to switch to
* @param bool $create
* @param bool|int $errorLevel
* @return bool
*/
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR)
{
$this->fullTextEnabled = null;
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);
}
$this->connectDatabase($name);
return true;
}
}

View File

@ -1,1557 +1,719 @@
<?php
namespace SilverStripe\MSSQL;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\Connect\Database;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\Queries\SQLSelect;
/**
* Microsoft SQL Server 2008+ connector class.
*
*
* <h2>Connecting using Windows</h2>
*
*
* If you've got your website running on Windows, it's highly recommended you
* use Microsoft SQL Server Driver for PHP "sqlsrv".
*
*
* A complete guide to installing a Windows IIS + PHP + SQL Server web stack can be
* found here: http://doc.silverstripe.org/installation-on-windows-server-manual-iis
*
* @see http://sqlsrvphp.codeplex.com/
*
*
* <h2>Connecting using Linux or Mac OS X</h2>
*
*
* The following commands assume you used the default package manager
* to install PHP with the operating system.
*
*
* Debian, and Ubuntu:
* <code>apt-get install php5-sybase</code>
*
* Fedora, CentOS and RedHat:
* <code>yum install php-mssql</code>
*
*
* Mac OS X (MacPorts):
* <code>port install php5-mssql</code>
*
*
* These packages will install the mssql extension for PHP, as well
* as FreeTDS, which will let you connect to SQL Server from Linux.
*
* as FreeTDS, which will let you connect to SQL Server.
*
* More information available in the SilverStripe developer wiki:
* @see http://doc.silverstripe.org/modules:mssql
*
* @see http://doc.silverstripe.org/installation-on-windows-server-manual-iis
*
* References:
* @see http://freetds.org
*
* @package mssql
*/
class MSSQLDatabase extends SS_Database {
/**
* Connection to the DBMS.
* @var resource
*/
protected $dbConn;
/**
* True if we are connected to a database.
* @var boolean
*/
protected $active;
/**
* The name of the database.
* @var string
*/
protected $database;
/**
* If true, use the mssql_... functions.
* If false use the sqlsrv_... functions
*/
protected $mssql = null;
/**
* Stores the affected rows of the last query.
* Used by sqlsrv functions only, as sqlsrv_rows_affected
* accepts a result instead of a database handle.
*/
protected $lastAffectedRows;
class MSSQLDatabase extends Database
{
use Configurable;
use Injectable;
/**
* Words that will trigger an error if passed to a SQL Server fulltext search
*/
public static $noiseWords = array("about", "1", "after", "2", "all", "also", "3", "an", "4", "and", "5", "another", "6", "any", "7", "are", "8", "as", "9", "at", "0", "be", "$", "because", "been", "before", "being", "between", "both", "but", "by", "came", "can", "come", "could", "did", "do", "does", "each", "else", "for", "from", "get", "got", "has", "had", "he", "have", "her", "here", "him", "himself", "his", "how", "if", "in", "into", "is", "it", "its", "just", "like", "make", "many", "me", "might", "more", "most", "much", "must", "my", "never", "no", "now", "of", "on", "only", "or", "other", "our", "out", "over", "re", "said", "same", "see", "should", "since", "so", "some", "still", "such", "take", "than", "that", "the", "their", "them", "then", "there", "these", "they", "this", "those", "through", "to", "too", "under", "up", "use", "very", "want", "was", "way", "we", "well", "were", "what", "when", "where", "which", "while", "who", "will", "with", "would", "you", "your", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
protected $supportsTransactions = false;
/**
* Cached flag to determine if full-text is enabled. This is set by
* {@link MSSQLDatabase::fullTextEnabled()}
*
* @var boolean
*/
protected $fullTextEnabled = null;
/**
* Connect to a MS SQL 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) {
if(function_exists('mssql_connect')) {
$this->mssql = true;
} else if(function_exists('sqlsrv_connect')) {
$this->mssql = false;
} else {
user_error("Neither the mssql_connect() nor the sqlsrv_connect() functions are available. Please install the PHP native mssql module, or the Microsoft-provided sqlsrv module.", E_USER_ERROR);
}
if($this->mssql) {
$this->dbConn = mssql_connect($parameters['server'], $parameters['username'], $parameters['password'], true);
} else {
// Disable default warnings as errors behaviour for sqlsrv to keep it in line with mssql functions
if(ini_get('sqlsrv.WarningsReturnAsErrors')) {
ini_set('sqlsrv.WarningsReturnAsErrors', 'Off');
}
/**
* Words that will trigger an error if passed to a SQL Server fulltext search
*/
public static $noiseWords = array('about', '1', 'after', '2', 'all', 'also', '3', 'an', '4', 'and', '5', 'another', '6', 'any', '7', 'are', '8', 'as', '9', 'at', '0', 'be', '$', 'because', 'been', 'before', 'being', 'between', 'both', 'but', 'by', 'came', 'can', 'come', 'could', 'did', 'do', 'does', 'each', 'else', 'for', 'from', 'get', 'got', 'has', 'had', 'he', 'have', 'her', 'here', 'him', 'himself', 'his', 'how', 'if', 'in', 'into', 'is', 'it', 'its', 'just', 'like', 'make', 'many', 'me', 'might', 'more', 'most', 'much', 'must', 'my', 'never', 'no', 'now', 'of', 'on', 'only', 'or', 'other', 'our', 'out', 'over', 're', 'said', 'same', 'see', 'should', 'since', 'so', 'some', 'still', 'such', 'take', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those', 'through', 'to', 'too', 'under', 'up', 'use', 'very', 'want', 'was', 'way', 'we', 'well', 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'will', 'with', 'would', 'you', 'your', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
// Windows authentication doesn't require a username and password
if(defined('MSSQL_USE_WINDOWS_AUTHENTICATION') && MSSQL_USE_WINDOWS_AUTHENTICATION == true) {
$connectionInfo = array();
} else {
$connectionInfo = array(
'UID' => $parameters['username'],
'PWD' => $parameters['password'],
);
}
$this->dbConn = sqlsrv_connect($parameters['server'], $connectionInfo);
}
/**
* Transactions will work with FreeTDS, but not entirely with sqlsrv driver on Windows with MARS enabled.
* TODO:
* - after the test fails with open transaction, the transaction should be rolled back,
* otherwise other tests will break claiming that transaction is still open.
* - figure out SAVEPOINTS
* - READ ONLY transactions
*/
protected $supportsTransactions = true;
if(!$this->dbConn) {
$this->databaseError("Couldn't connect to MS SQL database");
/**
* Cached flag to determine if full-text is enabled. This is set by
* {@link MSSQLDatabase::fullTextEnabled()}
*
* @var boolean
*/
protected $fullTextEnabled = null;
} else {
$this->database = $parameters['database'];
$this->selectDatabase($this->database);
/**
* @var bool
*/
protected $transactionNesting = 0;
// Configure the connection
$this->query('SET QUOTED_IDENTIFIER ON');
$this->query('SET TEXTSIZE 2147483647');
}
}
public function __destruct() {
if(is_resource($this->dbConn)) {
if($this->mssql) {
mssql_close($this->dbConn);
} else {
sqlsrv_close($this->dbConn);
}
}
}
/**
* Checks whether the current SQL Server version has full-text
* support installed and full-text is enabled for this database.
*
* @return boolean
*/
public function fullTextEnabled() {
if($this->fullTextEnabled === null) {
$isInstalled = (boolean) DB::query("SELECT fulltextserviceproperty('isfulltextinstalled')")->value();
$enabledForDb = (boolean) DB::query("
SELECT is_fulltext_enabled
FROM sys.databases
WHERE name = '$this->database'
")->value();
$this->fullTextEnabled = (boolean) ($isInstalled && $enabledForDb);
}
return $this->fullTextEnabled;
}
/**
* Throw a database error
*/
function databaseError($message, $errorLevel = E_USER_ERROR) {
if(!$this->mssql) {
$errorMessages = array();
foreach(sqlsrv_errors() as $error) {
$errorMessages[] = $error['message'];
}
$message .= ": \n" . implode("; ",$errorMessages);
}
return parent::databaseError($message, $errorLevel);
}
/**
* This will set up the full text search capabilities.
*
* TODO: make this a _config.php setting
* TODO: VERY IMPORTANT: move this so it only gets called upon a dev/build action
*/
function createFullTextCatalog() {
if($this->fullTextEnabled()) {
$result = $this->query("SELECT name FROM sys.fulltext_catalogs WHERE name = 'ftCatalog';")->value();
if(!$result) $this->query("CREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;");
}
}
/**
* Not implemented, needed for PDO
*/
public function getConnect($parameters) {
return null;
}
/**
* Returns true if this database supports collations
* @return boolean
*/
public function supportsCollations() {
return true;
}
/**
* Get the version of MSSQL.
* @return string
*/
public function getVersion() {
return trim($this->query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))")->value());
}
/**
* Get the database server, namely mssql.
* @return string
*/
public function getDatabaseServer() {
return "mssql";
}
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;
}
/**
* Set the default collation of the MSSQL nvarchar fields that we create.
* We don't apply this to the database as a whole, so that we can use unicode collations.
*
* @param string $collation
*/
public static function set_collation($collation)
{
static::config()->set('collation', $collation);
}
if(isset($_REQUEST['showqueries'])) {
$starttime = microtime(true);
}
/**
* The default collation of the MSSQL nvarchar fields that we create.
* We don't apply this to the database as a whole, so that we can use
* unicode collations.
*
* @return string
*/
public static function get_collation()
{
return static::config()->get('collation');
}
$error = '';
if($this->mssql) {
$handle = mssql_query($sql, $this->dbConn);
$error = mssql_get_last_message();
} else {
$handle = sqlsrv_query($this->dbConn, $sql);
if($handle) $this->lastAffectedRows = sqlsrv_rows_affected($handle);
if(function_exists('sqlsrv_errors')) {
$error = sqlsrv_errors();
}
}
/**
* Connect to a MS SQL 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
* - windowsauthentication: Set to true to use windows authentication
* instead of username/password
*/
public function connect($parameters)
{
parent::connect($parameters);
if(isset($_REQUEST['showqueries'])) {
$endtime = round(microtime(true) - $starttime,4);
Debug::message("\n$sql\n{$endtime}ms\n", false);
}
// Configure the connection
$this->query('SET QUOTED_IDENTIFIER ON');
$this->query('SET TEXTSIZE 2147483647');
}
if(!$handle && $errorLevel) $this->databaseError("Couldn't run query ($error): $sql", $errorLevel);
return new MSSQLQuery($this, $handle, $this->mssql);
}
public function getGeneratedID($table) {
//return $this->query("SELECT @@IDENTITY FROM \"$table\"")->value();
return $this->query("SELECT IDENT_CURRENT('$table')")->value();
}
/*
* This is a handy helper function which will return the primary key for any paricular table
* In MSSQL, the primary key is often an internal identifier, NOT the standard name (ie, 'ID'),
* so we need to do a lookup for it.
*/
function getPrimaryKey($tableName){
$indexes=DB::query("EXEC sp_helpindex '$tableName';");
$primary_key='';
foreach($indexes as $this_index){
if($this_index['index_keys']=='ID'){
$primary_key=$this_index['index_name'];
break;
}
}
return $primary_key;
}
/**
* OBSOLETE: Get the ID for the next new record for the table.
* @param string $table The name of 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;
}
/**
* Create the database that is currently selected.
*/
public function createDatabase() {
$this->query("CREATE DATABASE \"$this->database\"");
$this->selectDatabase($this->database);
}
/**
* Checks whether the current SQL Server version has full-text
* support installed and full-text is enabled for this database.
*
* @return boolean
*/
public function fullTextEnabled()
{
if ($this->fullTextEnabled === null) {
$this->fullTextEnabled = $this->updateFullTextEnabled();
}
return $this->fullTextEnabled;
}
/**
* Drop the database that this object is currently connected to.
* Use with caution.
*/
public function dropDatabase() {
$db = $this->database;
$this->selectDatabase('master');
$this->query("DROP DATABASE \"$db\"");
$this->active = false;
}
/**
* 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()
*
* @param string $dbname The database name to switch to
*/
public function selectDatabase($dbname) {
$this->database = $dbname;
if($this->databaseExists($this->database)) {
if($this->mssql) {
if(mssql_select_db($this->database, $this->dbConn)) {
$this->active = true;
}
} else {
$this->query("USE \"$this->database\"");
$this->active = true;
}
}
$this->tableList = $this->fieldList = $this->indexList = $this->fullTextEnabled = null;
}
/**
* Checks whether the current SQL Server version has full-text
* support installed and full-text is enabled for this database.
*
* @return boolean
*/
protected function updateFullTextEnabled()
{
// Check if installed
$isInstalled = $this->query("SELECT fulltextserviceproperty('isfulltextinstalled')")->value();
if (!$isInstalled) {
return false;
}
/**
* Check if the given database exists.
* @param string $name Name of database to check exists
* @return boolean
*/
public function databaseExists($name) {
$listDBs = $this->query('SELECT NAME FROM sys.sysdatabases');
if($listDBs) {
foreach($listDBs as $listedDB) {
if($listedDB['NAME'] == $name) return true;
}
}
return false;
}
/**
* Create a new table.
* @param $tableName The name of the table
* @param $fields A map of field names to field types
* @param $indexes A map of indexes
* @param $options An map of additional options. The available keys are as follows:
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL.
* - 'temporary' - If true, then a temporary table will be created
* @return The table name generated. This may be different from the table name, for example with temporary tables.
*/
public function createTable($tableName, $fields = null, $indexes = null, $options = null, $advancedOptions = null) {
$fieldSchemas = $indexSchemas = "";
if($fields) foreach($fields as $k => $v) $fieldSchemas .= "\"$k\" $v,\n";
// Temporary tables start with "#" in MSSQL-land
if(!empty($options['temporary'])) {
// Randomize the temp table name to avoid conflicts in the tempdb table which derived databases share
$tableName = "#$tableName" . '-' . rand(1000000, 9999999);
}
$this->query("CREATE TABLE \"$tableName\" (
$fieldSchemas
primary key (\"ID\")
);");
//we need to generate indexes like this: CREATE INDEX IX_vault_to_export ON vault (to_export);
//This needs to be done AFTER the table creation, so we can set up the fulltext indexes correctly
if($indexes) foreach($indexes as $k => $v) {
$indexSchemas .= $this->getIndexSqlDefinition($tableName, $k, $v) . "\n";
}
if($indexSchemas) $this->query($indexSchemas);
return $tableName;
}
// Check if current database is enabled
$database = $this->getSelectedDatabase();
$enabledForDb = $this->preparedQuery(
"SELECT is_fulltext_enabled FROM sys.databases WHERE name = ?",
array($database)
)->value();
return $enabledForDb;
}
/**
* 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();
$indexList = $this->indexList($tableName);
if($newFields) foreach($newFields as $k => $v) $alterList[] .= "ALTER TABLE \"$tableName\" ADD \"$k\" $v";
if($alteredFields) foreach($alteredFields as $k => $v) {
$val = $this->alterTableAlterColumn($tableName, $k, $v, $indexList);
if($val != '') $alterList[] .= $val;
}
if($alteredIndexes) foreach($alteredIndexes as $k => $v) $alterList[] .= $this->getIndexSqlDefinition($tableName, $k, $v);
if($newIndexes) foreach($newIndexes as $k =>$v) $alterList[] .= $this->getIndexSqlDefinition($tableName, $k, $v);
public function supportsCollations()
{
return true;
}
if($alterList) {
foreach($alterList as $alteration) {
if($alteration != '') {
$this->query($alteration);
}
}
}
}
/**
* This is a private MSSQL-only function which returns
* specific details about a column's constraints (if any)
* @param string $tableName Name of table the column exists in
* @param string $columnName Name of column to check for
*/
protected function ColumnConstraints($tableName, $columnName) {
$constraint = $this->query("SELECT CC.CONSTRAINT_NAME, CAST(CHECK_CLAUSE AS TEXT) AS CHECK_CLAUSE, COLUMN_NAME FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS CC INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CCU ON CCU.CONSTRAINT_NAME=CC.CONSTRAINT_NAME WHERE TABLE_NAME='$tableName' AND COLUMN_NAME='" . $columnName . "';")->first();
return $constraint;
}
public function supportsTimezoneOverride()
{
return true;
}
/**
* Return the name of the default constraint applied to $tableName.$colName.
* Will return null if no such constraint exists
*/
protected function defaultConstraintName($tableName, $colName) {
return $this->query("SELECT s.name --default name
FROM sys.sysobjects s
join sys.syscolumns c ON s.parent_obj = c.id
WHERE s.xtype = 'd'
and c.cdefault = s.id
and parent_obj= OBJECT_ID('$tableName')
and c.name = '$colName'")->value();
}
/**
* Get the actual enum fields from the constraint value:
*/
protected function EnumValuesFromConstraint($constraint){
$segments=preg_split('/ +OR *\[/i', $constraint);
$constraints=Array();
foreach($segments as $this_segment){
$bits=preg_split('/ *= */', $this_segment);
for($i=1; $i<sizeof($bits); $i+=2)
array_unshift($constraints, substr(rtrim($bits[$i], ')'), 1, -1));
}
return $constraints;
}
/*
* Creates an ALTER expression for a column in MS SQL
*
* @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
*/
protected function alterTableAlterColumn($tableName, $colName, $colSpec, $indexList){
public function getDatabaseServer()
{
return "sqlsrv";
}
// 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';
$matches=Array();
preg_match($pattern, $colSpec, $matches);
// drop the index if it exists
$alterCol='';
$indexName = isset($indexList[$colName]['indexname']) ? $indexList[$colName]['indexname'] : null;
if($indexName && $colName != 'ID') {
$alterCol = "\nDROP INDEX \"$indexName\" ON \"$tableName\";";
}
// fulltext indexes need to be dropped if alterting a table
if($this->fulltextIndexExists($tableName) === true) {
$alterCol .= "\nDROP FULLTEXT INDEX ON \"$tableName\";";
}
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR)
{
$this->fullTextEnabled = null;
$prefix="ALTER TABLE \"" . $tableName . "\" ";
return parent::selectDatabase($name, $create, $errorLevel);
}
// Remove the old default prior to adjusting the column.
if($defaultConstraintName = $this->defaultConstraintName($tableName, $colName)) {
$alterCol .= ";\n$prefix DROP CONSTRAINT \"$defaultConstraintName\"";
}
if(isset($matches[1])) {
//We will prevent any changes being made to the ID column. Primary key indexes will have a fit if we do anything here.
if($colName!='ID'){
$alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1]";
// SET null / not null
if(!empty($matches[2])) $alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1] $matches[2]";
// Add a default back
if(!empty($matches[3])) $alterCol .= ";\n$prefix ADD $matches[3] FOR \"$colName\"";
// SET check constraint (The constraint HAS to be dropped)
if(!empty($matches[4])) {
$constraint=$this->ColumnConstraints($tableName, $colName);
if($constraint)
$alterCol .= ";\n$prefix DROP CONSTRAINT {$constraint['CONSTRAINT_NAME']}";
//NOTE: 'with nocheck' seems to solve a few problems I've been having for modifying existing tables.
$alterCol .= ";\n$prefix WITH NOCHECK ADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]";
}
}
}
return isset($alterCol) ? $alterCol : '';
}
public function renameTable($oldTableName, $newTableName) {
$this->query("EXEC sp_rename \"$oldTableName\", \"$newTableName\"");
}
/**
* Checks a table's integrity and repairs it if necessary.
* NOTE: MSSQL does not appear to support any vacuum or optimise commands
*
* @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) {
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");
}
public function clearTable($table)
{
$this->query("TRUNCATE TABLE \"$table\"");
}
/**
* Change the database column name of the given field.
*
* @param string $tableName The name of the tbale 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("EXEC sp_rename @objname = '$tableName.$oldName', @newname = '$newName', @objtype = 'COLUMN'");
}
}
public function fieldList($table) {
//This gets us more information than we need, but I've included it all for the moment....
$fieldRecords = $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;");
/**
* SQL Server uses CURRENT_TIMESTAMP for the current date/time.
*/
public function now()
{
return 'CURRENT_TIMESTAMP';
}
// Cache the records from the query - otherwise a lack of multiple active result sets
// will cause subsequent queries to fail in this method
$fields = array();
$output = array();
foreach($fieldRecords as $record) {
$fields[] = $record;
}
foreach($fields as $field) {
// Update the data_type field to be a complete column definition string for use by
// SS_Database::requireField()
switch($field['data_type']){
case 'bigint':
case 'numeric':
case 'float':
case 'bit':
if($field['data_type'] != 'bigint' && $sizeSuffix = $field['numeric_precision']) {
$field['data_type'] .= "($sizeSuffix)";
}
/**
* Returns the database-specific version of the random() function
*/
public function random()
{
return 'RAND()';
}
if($field['is_nullable'] == 'YES') {
$field['data_type'] .= ' null';
} else {
$field['data_type'] .= ' not null';
}
if($field['column_default']) {
$default=substr($field['column_default'], 2, -2);
$field['data_type'] .= " default $default";
}
break;
/**
* The core search engine configuration.
* Picks up the fulltext-indexed tables from the database and executes search on all of them.
* Results are obtained as ID-ClassName pairs which is later used to reconstruct the DataObjectSet.
*
* @param array $classesToSearch computes all descendants and includes them. Check is done via WHERE clause.
* @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 DataObjectSet of result pages
*/
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "Relevance DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false)
{
$start = (int)$start;
$pageLength = (int)$pageLength;
$results = new ArrayList();
case 'decimal':
if($field['numeric_precision']) {
$sizeSuffix = $field['numeric_precision'] . ',' . $field['numeric_scale'];
$field['data_type'] .= "($sizeSuffix)";
}
if (!$this->fullTextEnabled()) {
return new PaginatedList($results);
}
if (!in_array(substr($sortBy, 0, 9), array('"Relevanc', 'Relevance'))) {
user_error("Non-relevance sort not supported.", E_USER_ERROR);
}
if($field['is_nullable'] == 'YES') {
$field['data_type'] .= ' null';
} else {
$field['data_type'] .= ' not null';
}
if($field['column_default']) {
$default=substr($field['column_default'], 2, -2);
$field['data_type'] .= " default $default";
}
break;
case 'varchar':
//Check to see if there's a constraint attached to this column:
$constraint=$this->ColumnConstraints($table, $field['column_name']);
if($constraint){
$constraints=$this->EnumValuesFromConstraint($constraint['CHECK_CLAUSE']);
$default=substr($field['column_default'], 2, -2);
$field['data_type']=$this->enum(Array('default'=>$default, 'name'=>$field['column_name'], 'enums'=>$constraints, 'table'=>$table));
break;
}
$allClassesToSearch = array();
foreach ($classesToSearch as $class) {
$allClassesToSearch = array_merge($allClassesToSearch, array_values(ClassInfo::dataClassesFor($class)));
}
$allClassesToSearch = array_unique($allClassesToSearch);
default:
$sizeSuffix = $field['character_maximum_length'];
if($sizeSuffix == '-1') $sizeSuffix = 'max';
if($sizeSuffix) {
$field['data_type'] .= "($sizeSuffix)";
}
//Get a list of all the tables and columns we'll be searching on:
$fulltextColumns = $this->query('EXEC sp_help_fulltext_columns');
$queries = array();
if($field['is_nullable'] == 'YES') {
$field['data_type'] .= ' null';
} else {
$field['data_type'] .= ' not null';
}
if($field['column_default']) {
$default=substr($field['column_default'], 2, -2);
$field['data_type'] .= " default '$default'";
}
}
$output[$field['column_name']]=$field;
}
return $output;
}
/**
* 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 SS_Database::requireIndex() for more details.
*/
public function createIndex($tableName, $indexName, $indexSpec) {
$this->query($this->getIndexSqlDefinition($tableName, $indexName, $indexSpec));
}
/**
* 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){
if(is_array($indexSpec)){
//Here we create a db-specific version of whatever index we need to create.
switch($indexSpec['type']){
case 'fulltext':
$indexSpec='fulltext (' . str_replace(' ', '', $indexSpec['value']) . ')';
break;
case 'unique':
$indexSpec='unique (' . $indexSpec['value'] . ')';
break;
}
}
return $indexSpec;
}
protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec) {
if(!is_array($indexSpec)) {
$indexSpec=trim($indexSpec, '()');
$bits=explode(',', $indexSpec);
$indexes="\"" . implode("\",\"", $bits) . "\"";
$index = 'ix_' . $tableName . '_' . $indexName;
$drop = "IF EXISTS (SELECT name FROM sys.indexes WHERE name = '$index') DROP INDEX $index ON \"" . $tableName . "\";";
return "$drop CREATE INDEX $index ON \"" . $tableName . "\" (" . $indexes . ");";
} else {
//create a type-specific index
if($indexSpec['type'] == 'fulltext') {
if($this->fullTextEnabled()) {
//Enable full text search.
$this->createFullTextCatalog();
$primary_key=$this->getPrimaryKey($tableName);
$drop = '';
if($this->fulltextIndexExists($tableName) === true) {
$drop = "DROP FULLTEXT INDEX ON \"$tableName\";";
}
return $drop . "CREATE FULLTEXT INDEX ON \"$tableName\" ({$indexSpec['value']}) KEY INDEX $primary_key WITH CHANGE_TRACKING AUTO;";
}
}
if($indexSpec['type'] == 'unique') {
return 'CREATE UNIQUE INDEX ix_' . $tableName . '_' . $indexName . " ON \"" . $tableName . "\" (\"" . $indexSpec['value'] . "\");";
}
}
}
function getDbSqlDefinition($tableName, $indexName, $indexSpec){
return $indexName;
}
/**
* 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 SS_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 ON $tableName;");
$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) {
$indexes=DB::query("EXEC sp_helpindex '$table';");
$prefix = '';
$indexList = array();
foreach($indexes as $index) {
//Check for uniques:
if(strpos($index['index_description'], 'unique')!==false)
$prefix='unique ';
$key=str_replace(', ', ',', $index['index_keys']);
$indexList[$key]['indexname']=$index['index_name'];
$indexList[$key]['spec']=$prefix . '(' . $key . ')';
}
//Now we need to check to see if we have any fulltext indexes attached to this table:
if($this->fullTextEnabled()) {
$result=DB::query('EXEC sp_help_fulltext_columns;');
$columns='';
foreach($result as $row){
if($row['TABLE_NAME']==$table)
$columns.=$row['FULLTEXT_COLUMN_NAME'] . ',';
}
if($columns!=''){
$columns=trim($columns, ',');
$indexList['SearchFields']['indexname']='SearchFields';
$indexList['SearchFields']['spec']='fulltext (' . $columns . ')';
}
}
// Sort the columns back into tables.
$tables = array();
foreach ($fulltextColumns as $column) {
// Skip extension tables.
if (substr($column['TABLE_NAME'], -5) == '_Live' || substr($column['TABLE_NAME'], -9) == '_versions') {
continue;
}
return $indexList;
}
// Add the column to table.
$table = &$tables[$column['TABLE_NAME']];
if (!$table) {
$table = array($column['FULLTEXT_COLUMN_NAME']);
} else {
array_push($table, $column['FULLTEXT_COLUMN_NAME']);
}
}
/**
* Returns a list of all the tables in the database.
* Table names will all be in lowercase.
* @return array
*/
public function tableList() {
$tables = array();
foreach($this->query("EXEC sp_tables @table_owner = 'dbo';") as $record) {
$table = strtolower($record['TABLE_NAME']);
$tables[$table] = $table;
}
return $tables;
}
/**
* Empty the given table of call contentTR
*/
public function clearTable($table) {
$this->query("TRUNCATE TABLE \"$table\"");
}
/**
* Return the number of rows affected by the previous operation.
* @return int
*/
public function affectedRows() {
if($this->mssql) {
return mssql_rows_affected($this->dbConn);
} else {
return $this->lastAffectedRows;
}
}
// Create one query per each table, $columns not used. We want just the ID and the ClassName of the object from this query.
foreach ($tables as $tableName => $columns) {
$class = DataObject::getSchema()->tableClass($tableName);
$join = $this->fullTextSearchMSSQL($tableName, $keywords);
if (!$join) {
return new PaginatedList($results);
} // avoid "Null or empty full-text predicate"
/**
* Return a boolean type-formatted string
* We use 'bit' so that we can do numeric-based comparisons
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function boolean($values) {
//Annoyingly, we need to do a good ol' fashioned switch here:
($values['default']) ? $default='1' : $default='0';
return 'bit not null default ' . $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) {
return 'datetime null';
}
/**
* 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) {
// Avoid empty strings being put in the db
if($values['precision'] == '') {
$precision = 1;
} else {
$precision = $values['precision'];
}
$defaultValue = '0';
if(isset($values['default']) && is_numeric($values['default'])) {
$defaultValue = $values['default'];
}
return 'decimal(' . $precision . ') not null default ' . $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
// Check if we need to add ShowInSearch
$where = null;
if ($class === 'SilverStripe\\CMS\\Model\\SiteTree') {
$where = array("\"$tableName\".\"ShowInSearch\"!=0");
} elseif ($class === 'SilverStripe\\Assets\\File') {
// File.ShowInSearch was added later, keep the database driver backwards compatible
// by checking for its existence first
$fields = $this->getSchemaManager()->fieldList($tableName);
if (array_key_exists('ShowInSearch', $fields)) {
$where = array("\"$tableName\".\"ShowInSearch\"!=0");
}
}
$maxLength = max(array_map('strlen', $values['enums']));
$queries[$tableName] = DataList::create($class)->where($where)->dataQuery()->query();
$queries[$tableName]->setOrderBy(array());
return "varchar($maxLength) not null default '" . $values['default']
. "' check(\"" . $values['name'] . "\" in ('" . implode("','", $values['enums'])
. "'))";
}
/**
* @todo Make this work like {@link MySQLDatabase::set()}
*/
public function set($values) {
return $this->enum($values);
}
/**
* 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) {
return 'float not null default ' . $values['default'];
}
/**
* 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) {
//We'll be using an 8 digit precision to keep it in line with the serial8 datatype for ID columns
return 'numeric(8) not null default ' . (int) $values['default'];
}
/**
* Return a datetime type-formatted string
* For MS SQL, 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) {
return 'datetime null';
}
/**
* 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) {
return 'nvarchar(max) null';
}
/**
* 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){
return 'time null';
}
/**
* 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) {
return 'nvarchar(' . $values['precision'] . ') null';
}
/**
* Return a 4 digit numeric type.
* @return string
*/
public function year($values) {
return 'numeric(4)';
}
// Join with CONTAINSTABLE, a full text searcher that includes relevance factor
$queries[$tableName]->setFrom(array("\"$tableName\" INNER JOIN $join AS \"ft\" ON \"$tableName\".\"ID\"=\"ft\".\"KEY\""));
// Join with the base class if needed, as we want to test agains the ClassName
if ($tableName != $tableName) {
$queries[$tableName]->setFrom("INNER JOIN \"$tableName\" ON \"$tableName\".\"ID\"=\"$tableName\".\"ID\"");
}
/**
* 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, $hasAutoIncPK=true){
if($asDbValue)
return 'bigint not null';
else {
if($hasAutoIncPK)
return 'bigint identity(1,1)';
else return 'bigint not null';
}
}
/**
* Returns the SQL command to get all the tables in this database
*/
function allTablesSQL(){
return "SELECT \"name\" FROM \"sys\".\"tables\";";
}
/**
* Returns true if this table exists
* @todo Make a proper implementation
*/
function hasTable($tableName) {
return true;
}
/**
* Returns the values of the given enum field
* NOTE: Experimental; introduced for db-abstraction and may changed before 2.4 is released.
*/
public function enumValuesForField($tableName, $fieldName) {
// Get the enum of all page types from the SiteTree table
$constraints=$this->ColumnConstraints($tableName, $fieldName);
$classes=Array();
if($constraints){
$constraints=$this->EnumValuesFromConstraint($constraints['CHECK_CLAUSE']);
$classes=$constraints;
}
return $classes;
}
$queries[$tableName]->setSelect(array("\"$tableName\".\"ID\""));
$queries[$tableName]->selectField("'$tableName'", 'Source');
$queries[$tableName]->selectField('Rank', 'Relevance');
if ($extraFilter) {
$queries[$tableName]->addWhere($extraFilter);
}
if (count($allClassesToSearch)) {
$classesPlaceholder = DB::placeholders($allClassesToSearch);
$queries[$tableName]->addWhere(array(
"\"$tableName\".\"ClassName\" IN ($classesPlaceholder)" =>
$allClassesToSearch
));
}
// Reset the parameters that would get in the way
}
/**
* SQL Server uses CURRENT_TIMESTAMP for the current date/time.
*/
function now() {
return 'CURRENT_TIMESTAMP';
}
/**
* Returns the database-specific version of the random() function
*/
function random(){
return 'RAND()';
}
/**
* This is a lookup table for data types.
*
* For instance, MSSQL uses 'BIGINT', while MySQL uses 'UNSIGNED'
* and PostgreSQL uses 'INT'.
*/
function dbDataType($type){
$values = array(
'unsigned integer'=>'BIGINT'
);
if(isset($values[$type])) return $values[$type];
else return '';
}
/**
* Convert a SQLQuery object into a SQL statement.
*/
public function sqlQueryToString(SQLQuery $sqlQuery) {
if (!$sqlQuery->from) return '';
if($sqlQuery->orderby && strtoupper(trim($sqlQuery->orderby)) == 'RAND()') $sqlQuery->orderby = "NEWID()";
//Get the limit and offset
$limit='';
$offset='0';
if(is_array($sqlQuery->limit)){
$limit=$sqlQuery->limit['limit'];
if(isset($sqlQuery->limit['start']))
$offset=$sqlQuery->limit['start'];
} else if(preg_match('/^([0-9]+) offset ([0-9]+)$/i', trim($sqlQuery->limit), $matches)) {
$limit = $matches[1];
$offset = $matches[2];
} else {
//could be a comma delimited string
$bits=explode(',', $sqlQuery->limit);
if(sizeof($bits) > 1) {
list($offset, $limit) = $bits;
} else {
$limit = $bits[0];
}
}
$text = '';
$suffixText = '';
$nestedQuery = false;
// Generate SQL
$querySQLs = array();
$queryParameters = array();
foreach ($queries as $query) {
/** @var SQLSelect $query */
$querySQLs[] = $query->sql($parameters);
$queryParameters = array_merge($queryParameters, $parameters);
}
// DELETE queries
if($sqlQuery->delete) {
$text = 'DELETE ';
// SELECT queries
} else {
$distinct = $sqlQuery->distinct ? "DISTINCT " : "";
// If there's a limit but no offset, just use 'TOP X'
// rather than the more complex sub-select method
if ($limit != 0 && $offset == 0) {
$text = "SELECT $distinct TOP $limit";
// If there's a limit and an offset, then we need to do a subselect
} else if($limit && $offset) {
if($sqlQuery->orderby) {
$rowNumber = "ROW_NUMBER() OVER (ORDER BY $sqlQuery->orderby) AS Number";
} else {
$firstCol = reset($sqlQuery->select);
$rowNumber = "ROW_NUMBER() OVER (ORDER BY $firstCol) AS Number";
}
$text = "SELECT * FROM ( SELECT $distinct$rowNumber, ";
$suffixText .= ") AS Numbered WHERE Number BETWEEN " . ($offset+1) ." AND " . ($offset+$limit)
. " ORDER BY Number";
$nestedQuery = true;
// Unite the SQL
$fullQuery = implode(" UNION ", $querySQLs) . " ORDER BY $sortBy";
// Otherwise a simple query
} else {
$text = "SELECT $distinct";
}
// Now add the columns to be selected
$text .= implode(", ", $sqlQuery->select);
}
// Perform the search
$result = $this->preparedQuery($fullQuery, $queryParameters);
$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(!$nestedQuery && $sqlQuery->orderby) $text .= " ORDER BY " . $sqlQuery->orderby;
// $suffixText is used by the nested queries to create an offset limit
if($suffixText) $text .= $suffixText;
return $text;
}
/*
* 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){
$value=stripslashes($value);
$value=str_replace("'","''",$value);
$value=str_replace("\0","[NULL]",$value);
// Regenerate DataObjectSet - watch out, numRecords doesn't work on sqlsrv driver on Windows.
$current = -1;
$objects = array();
foreach ($result as $row) {
$current++;
return $value;
}
/**
* This changes the index name depending on database requirements.
* MSSQL requires underscores to be replaced with commas.
*/
function modifyIndex($index) {
return str_replace('_', ',', $index);
}
/**
* The core search engine configuration.
* @todo There is no result relevancy or ordering as it currently stands.
*
* @param string $keywords Keywords as a space separated string
* @return object DataObjectSet of result pages
*/
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "Relevance DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) {
$results = new DataObjectSet();
if(!$this->fullTextEnabled()) {
return $results;
}
$keywords = Convert::raw2sql(trim($keywords));
$htmlEntityKeywords = htmlentities($keywords);
$keywordList = explode(' ', $keywords);
if($keywordList) {
foreach($keywordList as $index => $keyword) {
$keywordList[$index] = "\"{$keyword}\"";
}
$keywords = implode(' AND ', $keywordList);
}
$htmlEntityKeywordList = explode(' ', $htmlEntityKeywords);
if($htmlEntityKeywordList) {
foreach($htmlEntityKeywordList as $index => $keyword) {
$htmlEntityKeywordList[$index] = "\"{$keyword}\"";
}
$htmlEntityKeywords = implode(' AND ', $htmlEntityKeywordList);
}
//Get a list of all the tables and columns we'll be searching on:
$result = DB::query('EXEC sp_help_fulltext_columns');
$tables = array();
foreach($result as $row){
if(substr($row['TABLE_NAME'], -5)!='_Live' && substr($row['TABLE_NAME'], -9)!='_versions') {
$thisSql = "SELECT ID, '{$row['TABLE_NAME']}' AS Source FROM \"{$row['TABLE_NAME']}\" WHERE (".
"(CONTAINS(\"{$row['FULLTEXT_COLUMN_NAME']}\", '$keywords') OR CONTAINS(\"{$row['FULLTEXT_COLUMN_NAME']}\", '$htmlEntityKeywords'))";
if(strpos($row['TABLE_NAME'], 'SiteTree') === 0) {
$thisSql .= " AND ShowInSearch != 0)";//" OR (Title LIKE '%$keywords%' OR Title LIKE '%$htmlEntityKeywords%')";
} else {
$thisSql .= ')';
}
$tables[] = $thisSql;
}
}
// Select a subset for paging
if ($current >= $start && $current < $start + $pageLength) {
$objects[] = DataObject::get_by_id($row['Source'], $row['ID']);
}
}
$query = implode(' UNION ', $tables);
$result = DB::query($query);
if (isset($objects)) {
$results = new ArrayList($objects);
} else {
$results = new ArrayList();
}
$list = new PaginatedList($results);
$list->setPageStart($start);
$list->setPageLength($pageLength);
$list->setTotalItems($current+1);
return $list;
}
$totalCount = 0;
foreach($result as $row) {
$record = DataObject::get_by_id($row['Source'], $row['ID']);
if($record->canView()) {
$results->push($record);
$totalCount++;
}
}
/**
* Allow auto-increment primary key editing on the given table.
* Some databases need to enable this specially.
*
* @param string $table The name of the table to have PK editing allowed on
* @param bool $allow True to start, false to finish
*/
public function allowPrimaryKeyEditing($table, $allow = true)
{
$this->query("SET IDENTITY_INSERT \"$table\" " . ($allow ? "ON" : "OFF"));
}
$results->setPageLimits($start, $pageLength, $totalCount);
/**
* Returns a SQL fragment for querying a fulltext search index
*
* @param string $tableName specific - table name
* @param string $keywords The search query
* @param array $fields The list of field names to search on, or null to include all
* @return string Clause, or null if keyword set is empty or the string with JOIN clause to be added to SQL query
*/
public function fullTextSearchMSSQL($tableName, $keywords, $fields = null)
{
// Make sure we are getting an array of fields
if (isset($fields) && !is_array($fields)) {
$fields = array($fields);
}
return $results;
}
// Strip unfriendly characters, SQLServer "CONTAINS" predicate will crash on & and | and ignore others anyway.
if (function_exists('mb_ereg_replace')) {
$keywords = mb_ereg_replace('[^\w\s]', '', trim($keywords));
} else {
$keywords = $this->escapeString(str_replace(array('&', '|', '!', '"', '\''), '', trim($keywords)));
}
/**
* Allow auto-increment primary key editing on the given table.
* Some databases need to enable this specially.
* @param $table The name of the table to have PK editing allowed on
* @param $allow True to start, false to finish
*/
function allowPrimaryKeyEditing($table, $allow = true) {
$this->query("SET IDENTITY_INSERT \"$table\" " . ($allow ? "ON" : "OFF"));
}
// Remove stopwords, concat with ANDs
$keywordList = explode(' ', $keywords);
$keywordList = $this->removeStopwords($keywordList);
/**
* Check if a fulltext index exists on a particular table name.
* @return boolean TRUE index exists | FALSE index does not exist | NULL no support
*/
function fulltextIndexExists($tableName) {
// Special case for no full text index support
if(!$this->fullTextEnabled()) return null;
return (bool) $this->query("
SELECT 1 FROM sys.fulltext_indexes i
JOIN sys.objects o ON i.object_id = o.object_id
WHERE o.name = '$tableName'
")->value();
}
// remove any empty values from the array
$keywordList = array_filter($keywordList);
if (empty($keywordList)) {
return null;
}
/**
* Returns a SQL fragment for querying a fulltext search index
* @param $fields array The list of field names to search on
* @param $keywords string The search query
* @param $booleanSearch A MySQL-specific flag to switch to boolean search
*/
function fullTextSearchSQL($fields, $keywords, $booleanSearch = false) {
$fieldNames = '"' . implode('", "', $fields) . '"';
$keywords = implode(' AND ', $keywordList);
if ($fields) {
$fieldNames = '"' . implode('", "', $fields) . '"';
} else {
$fieldNames = "*";
}
$SQL_keywords = Convert::raw2sql($keywords);
return "CONTAINSTABLE(\"$tableName\", ($fieldNames), '$keywords')";
}
return "FREETEXT (($fieldNames), '$SQL_keywords')";
}
/**
* Remove noise words that would kill a MSSQL full-text query
*
* @param string $keywords
* @return string $keywords with noise words removed
* @author Tom Rix
*/
static public function removeNoiseWords($keywords) {
$goodWords = array();
foreach (explode(' ', $keywords) as $word) {
// @todo we may want to remove +'s -'s etc too
if (!in_array($word, self::$noiseWords)) {
$goodWords[] = $word;
}
}
return join(' ', $goodWords);
}
/*
* Does this database support transactions?
*/
public function supportsTransactions(){
return $this->supportsTransactions;
}
/*
* This is a quick lookup to discover if the database supports particular extensions
* Currently, MSSQL supports no extensions
*/
public function supportsExtensions($extensions=Array('partitions', 'tablespaces', 'clustering')){
if(isset($extensions['partitions']))
return false;
elseif(isset($extensions['tablespaces']))
return false;
elseif(isset($extensions['clustering']))
return false;
else
return false;
}
/*
* Start a prepared transaction
* See http://developer.postgresql.org/pgdocs/postgres/sql-set-transaction.html for details on transaction isolation options
*/
public function startTransaction($transaction_mode=false, $session_characteristics=false){
//Transactions not set up for MSSQL yet
}
/*
* Create a savepoint that you can jump back to if you encounter problems
*/
public function transactionSavepoint($savepoint){
//Transactions not set up for MSSQL yet
}
/*
* 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){
//Transactions not set up for MSSQL yet
}
/*
* Commit everything inside this transaction so far
*/
public function endTransaction(){
//Transactions not set up for MSSQL yet
}
/**
* Remove stopwords that would kill a MSSQL full-text query
*
* @param array $keywords
*
* @return array $keywords with stopwords removed
*/
public function removeStopwords($keywords)
{
$goodKeywords = array();
foreach ($keywords as $keyword) {
if (in_array($keyword, self::$noiseWords)) {
continue;
}
$goodKeywords[] = trim($keyword);
}
return $goodKeywords;
}
/**
* Function to return an SQL datetime expression for MSSQL
* 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);
/**
* Does this database support transactions?
*/
public function supportsTransactions()
{
return $this->supportsTransactions;
}
if(preg_match('/^now$/i', $date)) {
$date = "CURRENT_TIMESTAMP";
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
$date = "'$date.000'";
}
/**
* This is a quick lookup to discover if the database supports particular extensions
* Currently, MSSQL supports no extensions
*
* @param array $extensions List of extensions to check for support of. The key of this array
* will be an extension name, and the value the configuration for that extension. This
* could be one of partitions, tablespaces, or clustering
* @return boolean Flag indicating support for all of the above
*/
public function supportsExtensions($extensions = array('partitions', 'tablespaces', 'clustering'))
{
if (isset($extensions['partitions'])) {
return false;
} elseif (isset($extensions['tablespaces'])) {
return false;
} elseif (isset($extensions['clustering'])) {
return false;
} else {
return false;
}
}
if($format == '%U') return "DATEDIFF(s, '19700101 12:00:00:000', $date)";
/**
* Start transaction. READ ONLY not supported.
*
* @param bool $transactionMode
* @param bool $sessionCharacteristics
*/
public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
{
if ($this->transactionNesting > 0) {
$this->transactionSavepoint('NESTEDTRANSACTION' . $this->transactionNesting);
} elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionStart();
} else {
$this->query('BEGIN TRANSACTION');
}
++$this->transactionNesting;
}
$trans = array(
'Y' => 'yy',
'm' => 'mm',
'd' => 'dd',
'H' => 'hh',
'i' => 'mi',
's' => 'ss',
);
public function transactionSavepoint($savepoint)
{
$this->query("SAVE TRANSACTION \"$savepoint\"");
}
$strings = array();
$buffer = $format;
while(strlen($buffer)) {
if(substr($buffer,0,1) == '%') {
$f = substr($buffer,1,1);
$flen = $f == 'Y' ? 4 : 2;
$strings[] = "RIGHT('0' + CAST(DATEPART({$trans[$f]},$date) AS VARCHAR), $flen)";
$buffer = substr($buffer, 2);
} else {
$pos = strpos($buffer, '%');
if($pos === false) {
$strings[] = $buffer;
$buffer = '';
} else {
$strings[] = "'".substr($buffer, 0, $pos)."'";
$buffer = substr($buffer, $pos);
}
}
}
public function transactionRollback($savepoint = false)
{
// Named transaction
if ($savepoint) {
$this->query("ROLLBACK TRANSACTION \"$savepoint\"");
return true;
}
return '(' . implode(' + ', $strings) . ')';
}
/**
* Function to return an SQL datetime expression for MSSQL.
* 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) {
$trans = array(
'year' => 'yy',
'month' => 'mm',
'day' => 'dd',
'hour' => 'hh',
'minute' => 'mi',
'second' => 'ss',
);
// Fail if transaction isn't available
if (!$this->transactionNesting) {
return false;
}
--$this->transactionNesting;
if ($this->transactionNesting > 0) {
$this->transactionRollback('NESTEDTRANSACTION' . $this->transactionNesting);
} elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionRollback();
} else {
$this->query('ROLLBACK TRANSACTION');
}
return true;
}
$singularinterval = preg_replace('/(year|month|day|hour|minute|second)s/i', '$1', $interval);
public function transactionEnd($chain = false)
{
// Fail if transaction isn't available
if (!$this->transactionNesting) {
return false;
}
--$this->transactionNesting;
if ($this->transactionNesting <= 0) {
$this->transactionNesting = 0;
if ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionEnd();
} else {
$this->query('COMMIT TRANSACTION');
}
}
return true;
}
if(
!($params = preg_match('/([-+]\d+) (\w+)/i', $singularinterval, $matches)) ||
!isset($trans[strtolower($matches[2])])
) user_error('datetimeIntervalClause(): invalid interval ' . $interval, E_USER_WARNING);
/**
* In error condition, set transactionNesting to zero
*/
protected function resetTransactionNesting()
{
$this->transactionNesting = 0;
}
if(preg_match('/^now$/i', $date)) {
$date = "CURRENT_TIMESTAMP";
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
$date = "'$date'";
}
public function query($sql, $errorLevel = E_USER_ERROR)
{
$this->inspectQuery($sql);
return parent::query($sql, $errorLevel);
}
return "CONVERT(VARCHAR, DATEADD(" . $trans[strtolower($matches[2])] . ", " . (int)$matches[1] . ", $date), 120)";
}
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
{
$this->inspectQuery($sql);
return parent::preparedQuery($sql, $parameters, $errorLevel);
}
/**
* Function to return an SQL datetime expression for MSSQL.
* 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) {
protected function inspectQuery($sql)
{
// Any DDL discards transactions.
$isDDL = $this->getConnector()->isQueryDDL($sql);
if ($isDDL) {
$this->resetTransactionNesting();
}
}
if(preg_match('/^now$/i', $date1)) {
$date1 = "CURRENT_TIMESTAMP";
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date1)) {
$date1 = "'$date1'";
}
public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null, $parameterised = false)
{
if ($exact) {
$comp = ($negate) ? '!=' : '=';
} else {
$comp = 'LIKE';
if ($negate) {
$comp = 'NOT ' . $comp;
}
}
if(preg_match('/^now$/i', $date2)) {
$date2 = "CURRENT_TIMESTAMP";
} else if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date2)) {
$date2 = "'$date2'";
}
// Field definitions are case insensitive by default,
// change used collation for case sensitive searches.
$collateClause = '';
if ($caseSensitive === true) {
if (self::get_collation()) {
$collation = preg_replace('/_CI_/', '_CS_', self::get_collation());
} else {
$collation = 'Latin1_General_CS_AS';
}
$collateClause = ' COLLATE ' . $collation;
} elseif ($caseSensitive === false) {
if (self::get_collation()) {
$collation = preg_replace('/_CS_/', '_CI_', self::get_collation());
} else {
$collation = 'Latin1_General_CI_AS';
}
$collateClause = ' COLLATE ' . $collation;
}
return "DATEDIFF(s, $date2, $date1)";
}
$clause = sprintf("%s %s %s", $field, $comp, $parameterised ? '?' : "'$value'");
if ($collateClause) {
$clause .= $collateClause;
}
return $clause;
}
/**
* Function to return an SQL datetime expression for MSSQL
* 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);
}
}
if (preg_match('/^now$/i', $date)) {
$date = "CURRENT_TIMESTAMP";
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
$date = "'$date.000'";
}
if ($format == '%U') {
return "DATEDIFF(s, '1970-01-01 00:00:00', DATEADD(hour, DATEDIFF(hour, GETDATE(), GETUTCDATE()), $date))";
}
$trans = array(
'Y' => 'yy',
'm' => 'mm',
'd' => 'dd',
'H' => 'hh',
'i' => 'mi',
's' => 'ss',
);
$strings = array();
$buffer = $format;
while (strlen($buffer)) {
if (substr($buffer, 0, 1) == '%') {
$f = substr($buffer, 1, 1);
$flen = $f == 'Y' ? 4 : 2;
$strings[] = "RIGHT('0' + CAST(DATEPART({$trans[$f]},$date) AS VARCHAR), $flen)";
$buffer = substr($buffer, 2);
} else {
$pos = strpos($buffer, '%');
if ($pos === false) {
$strings[] = $buffer;
$buffer = '';
} else {
$strings[] = "'".substr($buffer, 0, $pos)."'";
$buffer = substr($buffer, $pos);
}
}
}
return '(' . implode(' + ', $strings) . ')';
}
/**
* Function to return an SQL datetime expression for MSSQL.
* 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)
{
$trans = array(
'year' => 'yy',
'month' => 'mm',
'day' => 'dd',
'hour' => 'hh',
'minute' => 'mi',
'second' => 'ss',
);
$singularinterval = preg_replace('/(year|month|day|hour|minute|second)s/i', '$1', $interval);
if (
!($params = preg_match('/([-+]\d+) (\w+)/i', $singularinterval, $matches)) ||
!isset($trans[strtolower($matches[2])])
) {
user_error('datetimeIntervalClause(): invalid interval ' . $interval, E_USER_WARNING);
}
if (preg_match('/^now$/i', $date)) {
$date = "CURRENT_TIMESTAMP";
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date)) {
$date = "'$date'";
}
return "CONVERT(VARCHAR, DATEADD(" . $trans[strtolower($matches[2])] . ", " . (int)$matches[1] . ", $date), 120)";
}
/**
* Function to return an SQL datetime expression for MSSQL.
* 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 = "CURRENT_TIMESTAMP";
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date1)) {
$date1 = "'$date1'";
}
if (preg_match('/^now$/i', $date2)) {
$date2 = "CURRENT_TIMESTAMP";
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/i', $date2)) {
$date2 = "'$date2'";
}
return "DATEDIFF(s, $date2, $date1)";
}
}
/**
* A result-set from a MSSQL database.
* @package sapphire
* @subpackage model
*/
class MSSQLQuery extends SS_Query {
/**
* The MSSQLDatabase object that created this result set.
* @var MSSQLDatabase
*/
private $database;
/**
* The internal MSSQL handle that points to the result set.
* @var resource
*/
private $handle;
/**
* If true, use the mssql_... functions.
* If false use the sqlsrv_... functions
*/
private $mssql = null;
/**
* 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 mssql handle that is points to the resultset.
*/
public function __construct(MSSQLDatabase $database, $handle, $mssql) {
$this->database = $database;
$this->handle = $handle;
$this->mssql = $mssql;
}
public function __destroy() {
if($this->mssql) {
mssql_free_result($this->handle);
} else {
sqlsrv_free_stmt($this->handle);
}
}
public function seek($row) {
if($this->mssql) {
return mssql_data_seek($this->handle, $row);
} else {
user_error("MSSQLQuery::seek() sqlsrv doesn't support seek.", E_USER_WARNING);
}
}
public function numRecords() {
if($this->mssql) {
return mssql_num_rows($this->handle);
} else {
// WARNING: This will only work if the cursor type is NOT forward only!
if(function_exists('sqlsrv_num_rows')) {
return sqlsrv_num_rows($this->handle);
}
}
}
public function nextRecord() {
// Coalesce rather than replace common fields.
$output = array();
if($this->mssql) {
if($data = mssql_fetch_row($this->handle)) {
foreach($data as $columnIdx => $value) {
$columnName = mssql_field_name($this->handle, $columnIdx);
// There are many places in the framework that expect the ID to be a string, not a double
// Do not set this to an integer, or it will cause failures in many tests that expect a string
if($columnName == 'ID') $value = (string) $value;
// $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 {
if($this->handle && $data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_NUMERIC)) {
$fields = sqlsrv_field_metadata($this->handle);
foreach($fields as $columnIdx => $field) {
$value = $data[$columnIdx];
if($value instanceof DateTime) $value = $value->format('Y-m-d H:i:s');
// $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[$field['Name']])) {
$output[$field['Name']] = $value;
}
}
return $output;
} else {
// Free the handle if there are no more results - sqlsrv crashes if there are too many handles
if($this->handle) {
sqlsrv_free_stmt($this->handle);
$this->handle = null;
}
}
}
return false;
}
}

View File

@ -1,192 +1,268 @@
<?php
namespace SilverStripe\MSSQL;
use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
use SilverStripe\Dev\Install\DatabaseConfigurationHelper;
use PDO;
use Exception;
/**
* This is a helper class for the SS installer.
*
*
* It does all the specific checking for MSSQLDatabase
* to ensure that the configuration is setup correctly.
*
* @package mssql
*/
class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper
{
/**
* Ensure that the database function for connectivity 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('mssql_connect') || function_exists('sqlsrv_connect')) ? true : false;
}
protected function isAzure($databaseConfig)
{
/** @skipUpgrade */
return $databaseConfig['type'] === 'MSSQLAzureDatabase';
}
/**
* 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 = '';
if(function_exists('mssql_connect')) {
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
} else {
$conn = @sqlsrv_connect($databaseConfig['server'], array(
'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password']
));
$errors = sqlsrv_errors();
if($errors) {
$error .= "\n";
foreach($errors as $detail) {
$error .= "\n" . @$detail['message'] . "\n";
}
}
}
if($conn) {
$success = true;
} else {
$success = false;
if(!$error) $error = 'SQL Server requires a valid username and password to determine if the server exists.';
}
return array(
'success' => $success,
'error' => $error
);
}
/**
* 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;
try {
switch ($databaseConfig['type']) {
case 'MSSQLDatabase':
case 'MSSQLAzureDatabase':
$parameters = array(
'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password']
);
/**
* 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 = '';
// Azure has additional parameter requirements
if ($this->isAzure($databaseConfig)) {
$parameters['database'] = $databaseConfig['database'];
$parameters['multipleactiveresultsets'] = 0;
}
$conn = @sqlsrv_connect($databaseConfig['server'], $parameters);
if ($conn) {
return $conn;
}
if(function_exists('mssql_connect')) {
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
} else {
$conn = @sqlsrv_connect($databaseConfig['server'], array(
'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password']
));
$errors = sqlsrv_errors();
if($errors) {
$error .= "\n";
foreach($errors as $detail) {
$error .= "\n" . @$detail['message'] . "\n";
}
}
}
if($conn) {
$success = true;
} else {
$success = false;
}
return array(
'success' => $success,
'connection' => $conn,
'error' => $error
);
}
// Get error
if ($errors = sqlsrv_errors()) {
$error = '';
foreach ($errors as $detail) {
$error .= "{$detail['message']}\n";
}
} else {
$error = 'Unknown connection error';
}
return null;
case 'MSSQLPDODatabase':
$driver = $this->getPDODriver();
if (!$driver) {
$error = 'No supported PDO driver';
return null;
}
public function getDatabaseVersion($databaseConfig) {
$version = 0;
// May throw a PDOException if fails
$conn = @new PDO($driver.':Server='.$databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if ($conn) {
return $conn;
} else {
$error = 'Unknown connection error';
return null;
}
default:
$error = 'Invalid connection type: ' . $databaseConfig['type'];
return null;
}
} catch (Exception $ex) {
$error = $ex->getMessage();
return null;
}
}
// Get the version using SERVERPROPERTY() function
if(function_exists('mssql_connect')) {
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
$result = @mssql_query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))", $conn);
$row = @mssql_fetch_array($result);
} else {
$conn = @sqlsrv_connect($databaseConfig['server'], array(
'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password']
));
$result = @sqlsrv_query($conn, "SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))");
$row = @sqlsrv_fetch_array($result);
}
/**
* Get supported PDO driver
*
* @return null
*/
public static function getPDODriver() {
if (!class_exists('PDO')) {
return null;
}
foreach(PDO::getAvailableDrivers() as $driver) {
if(in_array($driver, array('sqlsrv', 'dblib'))) {
return $driver;
}
}
return null;
}
if($row && isset($row[0])) {
$version = trim($row[0]);
}
/**
* Helper function to quote a string value
*
* @param mixed $conn Connection object/resource
* @param string $value Value to quote
* @return string Quoted string
*/
protected function quote($conn, $value)
{
if ($conn instanceof PDO) {
return $conn->quote($value);
} elseif (is_resource($conn)) {
$value = str_replace("'", "''", $value);
$value = str_replace("\0", "[NULL]", $value);
return "N'$value'";
} else {
user_error('Invalid database connection', E_USER_ERROR);
}
return null;
}
return $version;
}
/**
* 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) {
$result = $conn->query($sql);
if ($result) {
foreach ($result as $row) {
$items[] = $row[0];
}
}
} elseif (is_resource($conn)) {
$result = sqlsrv_query($conn, $sql);
if ($result) {
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
$items[] = $row[0];
}
}
}
return $items;
}
/**
* Ensure that the SQL Server version is at least 10.00.2531 (SQL Server 2008 SP1).
* @see http://www.sqlteam.com/article/sql-server-versions
* @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);
public function requireDatabaseFunctions($databaseConfig)
{
$data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']);
return !empty($data['supported']);
}
if($version) {
$success = version_compare($version, '10.00.2531', '>=');
if(!$success) {
$error = "Your SQL Server version is $version. It's recommended you use at least 10.00.2531 (SQL Server 2008 SP1).";
}
} else {
$error = "Your SQL Server version could not be determined.";
}
public function requireDatabaseServer($databaseConfig)
{
$conn = $this->createConnection($databaseConfig, $error);
$success = !empty($conn);
return array(
'success' => $success,
'error' => $error
);
}
return array(
'success' => $success,
'error' => $error
);
}
/**
* 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;
public function requireDatabaseConnection($databaseConfig)
{
$conn = $this->createConnection($databaseConfig, $error);
$success = !empty($conn);
$check = $this->requireDatabaseConnection($databaseConfig);
$conn = $check['connection'];
return array(
'success' => $success,
'connection' => $conn,
'error' => $error
);
}
if(function_exists('mssql_select_db')) {
$exists = @mssql_select_db($databaseConfig['database'], $conn);
} else {
$exists = @sqlsrv_query($conn, "USE \"$databaseConfig[database]\"");
}
public function getDatabaseVersion($databaseConfig)
{
$conn = $this->createConnection($databaseConfig, $error);
$result = $this->query($conn, "SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))");
return empty($result) ? 0 : reset($result);
}
if($exists) {
$success = true;
$alreadyExists = true;
} else {
if(function_exists('mssql_query') && mssql_query("CREATE DATABASE testing123", $conn)) {
mssql_query("DROP DATABASE testing123", $conn);
$success = true;
$alreadyExists = false;
} elseif(function_exists('sqlsrv_query') && @sqlsrv_query($conn, "CREATE DATABASE testing123")) {
sqlsrv_query($conn, "DROP DATABASE testing123");
$success = true;
$alreadyExists = false;
}
}
/**
* Ensure that the SQL Server version is at least 10.00.2531 (SQL Server 2008 SP1).
*
* @see http://www.sqlteam.com/article/sql-server-versions
* @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,
'alreadyExists' => $alreadyExists
);
}
if ($version) {
$success = version_compare($version, '10.00.2531', '>=');
if (!$success) {
$error = "Your SQL Server version is $version. It's recommended you use at least 10.00.2531 (SQL Server 2008 SP1).";
}
} else {
$error = "Your SQL Server version could not be determined.";
}
}
return array(
'success' => $success,
'error' => $error
);
}
public function requireDatabaseOrCreatePermissions($databaseConfig)
{
$conn = $this->createConnection($databaseConfig, $error);
/** @skipUpgrade */
if (empty($conn)) {
$success = false;
$alreadyExists = false;
} elseif ($databaseConfig['type'] == 'MSSQLAzureDatabase') {
// Don't bother with DB selection for azure, as it's not supported
$success = true;
$alreadyExists = true;
} else {
// does this database exist already?
$list = $this->query($conn, 'SELECT NAME FROM sys.sysdatabases');
if (in_array($databaseConfig['database'], $list)) {
$success = true;
$alreadyExists = true;
} else {
$permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions('','') where permission_name like 'CREATE ANY DATABASE' or permission_name like 'CREATE DATABASE';");
$success = $permissions[0] > 0;
$alreadyExists = false;
}
}
return array(
'success' => $success,
'alreadyExists' => $alreadyExists
);
}
public function requireDatabaseAlterPermissions($databaseConfig)
{
$success = false;
$conn = $this->createConnection($databaseConfig, $error);
if (!empty($conn)) {
if (!$this->isAzure($databaseConfig)) {
// Make sure to select the current database when checking permission against this database
$this->query($conn, "USE \"{$databaseConfig['database']}\"");
}
$permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions(NULL,'DATABASE') WHERE permission_name like 'create table';");
$success = $permissions[0] > 0;
}
return array(
'success' => $success,
'applies' => true
);
}
}

124
code/MSSQLQueryBuilder.php Normal file
View File

@ -0,0 +1,124 @@
<?php
namespace SilverStripe\MSSQL;
use InvalidArgumentException;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\ORM\Connect\DBQueryBuilder;
/**
* Builds a SQL query string from a SQLExpression object
*/
class MSSQLQueryBuilder extends DBQueryBuilder
{
protected function buildSelectQuery(SQLSelect $query, array &$parameters)
{
list($limit, $offset) = $this->parseLimit($query);
// If not using ofset then query generation is quite straightforward
if (empty($offset)) {
$sql = parent::buildSelectQuery($query, $parameters);
// Inject limit into SELECT fragment
if (!empty($limit)) {
$sql = preg_replace('/^(SELECT (DISTINCT)?)/i', '${1} TOP '.$limit, $sql);
}
return $sql;
}
// When using offset we must use a subselect
// @see http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server
$orderby = $query->getOrderBy();
// workaround for subselect not working with alias functions
// just use the function directly in the order by instead of the alias
$selects = $query->getSelect();
foreach ($orderby as $field => $dir) {
if (preg_match('/_SortColumn/', $field)) {
unset($orderby[$field]);
$orderby[$selects[str_replace('"', '', $field)]] = $dir;
}
}
// Create order expression, using the first column if none explicitly specified
if ($orderby) {
// Simple implementation of buildOrderByFragment
$statements = array();
foreach ($orderby as $clause => $dir) {
$statements[] = trim("$clause $dir");
}
$orderByClause = "ORDER BY " . implode(', ', $statements);
} else {
$selects = $query->getSelect();
$firstCol = reset($selects);
$orderByClause = "ORDER BY $firstCol";
}
// Build main query SQL
$sql = parent::buildSelectQuery($query, $parameters);
// Inject row number into selection
$sql = preg_replace('/^(SELECT (DISTINCT)?)/i', '${1} ROW_NUMBER() OVER ('.$orderByClause.') AS Number, ', $sql);
// Sub-query this SQL
if (empty($limit)) {
$limitCondition = "Number > ?";
$parameters[] = $offset;
} else {
$limitCondition = "Number BETWEEN ? AND ?";
$parameters[] = $offset + 1;
$parameters[] = $offset + $limit;
}
return "SELECT * FROM ($sql) AS Numbered WHERE $limitCondition ORDER BY Number";
}
public function buildLimitFragment(SQLSelect $query, array &$parameters)
{
// Limit is handled at the buildSelectQuery level
return '';
}
public function buildOrderByFragment(SQLSelect $query, array &$parameters)
{
// If doing a limit/offset at the same time then don't build the orde by fragment here
list($offset, $limit) = $this->parseLimit($query);
if (empty($offset) || empty($limit)) {
return parent::buildOrderByFragment($query, $parameters);
}
return '';
}
/**
* Extracts the limit and offset from the limit clause
*
* @param SQLSelect $query
* @return array Two item array with $limit and $offset as values
* @throws InvalidArgumentException
*/
protected function parseLimit(SQLSelect $query)
{
$limit = '';
$offset = '0';
if (is_array($query->getLimit())) {
$limitArr = $query->getLimit();
if (isset($limitArr['limit'])) {
$limit = $limitArr['limit'];
}
if (isset($limitArr['start'])) {
$offset = $limitArr['start'];
}
} elseif (preg_match('/^([0-9]+) offset ([0-9]+)$/i', trim($query->getLimit()), $matches)) {
$limit = $matches[1];
$offset = $matches[2];
} else {
//could be a comma delimited string
$bits = explode(',', $query->getLimit());
if (sizeof($bits) > 1) {
list($offset, $limit) = $bits;
} else {
$limit = $bits[0];
}
}
return array($limit, $offset);
}
}

964
code/MSSQLSchemaManager.php Normal file
View File

@ -0,0 +1,964 @@
<?php
namespace SilverStripe\MSSQL;
use SilverStripe\ORM\Connect\DBSchemaManager;
/**
* Represents and handles all schema management for a MS SQL database
*/
class MSSQLSchemaManager extends DBSchemaManager
{
/**
* Stores per-request cached constraint checks that come from the database.
*
* @var array
*/
protected static $cached_checks = array();
/**
* Builds the internal MS SQL Server 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 name of the index
*/
public function buildMSSQLIndexName($tableName, $indexName, $prefix = 'ix')
{
// Cleanup names of namespaced tables
$tableName = str_replace('\\', '_', $tableName);
$indexName = str_replace('\\', '_', $indexName);
return "{$prefix}_{$tableName}_{$indexName}";
}
/**
* This will set up the full text search capabilities.
*
* @param string $name Name of full text catalog to use
*/
public function createFullTextCatalog($name = 'ftCatalog')
{
$result = $this->fullTextCatalogExists();
if (!$result) {
$this->query("CREATE FULLTEXT CATALOG \"$name\" AS DEFAULT;");
}
}
/**
* Check that a fulltext catalog has been created yet.
*
* @param string $name Name of full text catalog to use
* @return boolean
*/
public function fullTextCatalogExists($name = 'ftCatalog')
{
return (bool) $this->preparedQuery(
"SELECT name FROM sys.fulltext_catalogs WHERE name = ?;",
array($name)
)->value();
}
/**
* Sleep until the catalog has been fully rebuilt. This is a busy wait designed for situations
* when you need to be sure the index is up to date - for example in unit tests.
*
* TODO: move this to Database class? Can we assume this will be useful for all databases?
* Also see the wrapper functions "waitUntilIndexingFinished" in SearchFormTest and TranslatableSearchFormTest
*
* @param int $maxWaitingTime Time in seconds to wait for the database.
*/
public function waitUntilIndexingFinished($maxWaitingTime = 15)
{
if (!$this->database->fullTextEnabled()) {
return;
}
$this->query("EXEC sp_fulltext_catalog 'ftCatalog', 'Rebuild';");
// Busy wait until it's done updating, but no longer than 15 seconds.
$start = time();
while (time() - $start < $maxWaitingTime) {
$status = $this->query("EXEC sp_help_fulltext_catalogs 'ftCatalog';")->first();
if (isset($status['STATUS']) && $status['STATUS'] == 0) {
// Idle!
break;
}
sleep(1);
}
}
/**
* Check if a fulltext index exists on a particular table name.
*
* @param string $tableName
* @return boolean TRUE index exists | FALSE index does not exist | NULL no support
*/
public function fulltextIndexExists($tableName)
{
// Special case for no full text index support
if (!$this->database->fullTextEnabled()) {
return null;
}
return (bool) $this->preparedQuery("
SELECT 1 FROM sys.fulltext_indexes i
JOIN sys.objects o ON i.object_id = o.object_id
WHERE o.name = ?",
array($tableName)
)->value();
}
/**
* MSSQL stores the primary key column with an internal identifier,
* so a lookup needs to be done to determine it.
*
* @param string $tableName Name of table with primary key column "ID"
* @return string Internal identifier for primary key
*/
public function getPrimaryKey($tableName)
{
$indexes = $this->query("EXEC sp_helpindex '$tableName';");
$indexName = '';
foreach ($indexes as $index) {
if ($index['index_keys'] == 'ID') {
$indexName = $index['index_name'];
break;
}
}
return $indexName;
}
/**
* Gets the identity column of a table
*
* @param string $tableName
* @return string|null
*/
public function getIdentityColumn($tableName)
{
return $this->preparedQuery("
SELECT
TABLE_NAME + '.' + COLUMN_NAME,
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = ? AND
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 AND
TABLE_NAME = ?
", array('dbo', $tableName))->value();
}
public function createDatabase($name)
{
$this->query("CREATE DATABASE \"$name\"");
}
public function dropDatabase($name)
{
$this->query("DROP DATABASE \"$name\"");
}
public function databaseExists($name)
{
$databases = $this->databaseList();
foreach ($databases as $dbname) {
if ($dbname == $name) {
return true;
}
}
return false;
}
public function databaseList()
{
return $this->query('SELECT NAME FROM sys.sysdatabases')->column();
}
/**
* Create a new table.
* @param string $tableName The name of the table
* @param array $fields A map of field names to field types
* @param array $indexes A map of indexes
* @param array $options An map of additional options. The available keys are as follows:
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL.
* - 'temporary' - If true, then a temporary table will be created
* @param array $advancedOptions
* @return string The table name generated. This may be different from the table name, for example with temporary tables.
*/
public function createTable($tableName, $fields = null, $indexes = null, $options = null, $advancedOptions = null)
{
$fieldSchemas = $indexSchemas = "";
if ($fields) {
foreach ($fields as $k => $v) {
$fieldSchemas .= "\"$k\" $v,\n";
}
}
// Temporary tables start with "#" in MSSQL-land
if (!empty($options['temporary'])) {
// Randomize the temp table name to avoid conflicts in the tempdb table which derived databases share
$tableName = "#$tableName" . '-' . rand(1000000, 9999999);
}
$this->query("CREATE TABLE \"$tableName\" (
$fieldSchemas
primary key (\"ID\")
);");
//we need to generate indexes like this: CREATE INDEX IX_vault_to_export ON vault (to_export);
//This needs to be done AFTER the table creation, so we can set up the fulltext indexes correctly
if ($indexes) {
foreach ($indexes as $k => $v) {
$indexSchemas .= $this->getIndexSqlDefinition($tableName, $k, $v) . "\n";
}
}
if ($indexSchemas) {
$this->query($indexSchemas);
}
return $tableName;
}
/**
* Alter a table's schema.
* @param string $tableName The name of the table to alter
* @param array $newFields New fields, a map of field name => field schema
* @param array $newIndexes New indexes, a map of index name => index type
* @param array $alteredFields Updated fields, a map of field name => field schema
* @param array $alteredIndexes Updated indexes, a map of index name => index type
* @param array $alteredOptions
* @param array $advancedOptions
*/
public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null, $alteredOptions=null, $advancedOptions=null)
{
$alterList = array();
// drop any fulltext indexes that exist on the table before altering the structure
if ($this->fulltextIndexExists($tableName)) {
$alterList[] = "\nDROP FULLTEXT INDEX ON \"$tableName\";";
}
if ($newFields) {
foreach ($newFields as $k => $v) {
$alterList[] = "ALTER TABLE \"$tableName\" ADD \"$k\" $v";
}
}
if ($alteredFields) {
foreach ($alteredFields as $k => $v) {
$alterList[] = $this->alterTableAlterColumn($tableName, $k, $v);
}
}
if ($alteredIndexes) {
foreach ($alteredIndexes as $k => $v) {
$alterList[] = $this->getIndexSqlDefinition($tableName, $k, $v);
}
}
if ($newIndexes) {
foreach ($newIndexes as $k => $v) {
$alterList[] = $this->getIndexSqlDefinition($tableName, $k, $v);
}
}
if ($alterList) {
foreach ($alterList as $alteration) {
if ($alteration != '') {
$this->query($alteration);
}
}
}
}
/**
* Given the table and column name, retrieve the constraint name for that column
* in the table.
*
* @param string $tableName Table name column resides in
* @param string $columnName Column name the constraint is for
* @return string|null
*/
public function getConstraintName($tableName, $columnName)
{
return $this->preparedQuery("
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME = ? AND COLUMN_NAME = ?",
array($tableName, $columnName)
)->value();
}
/**
* Given a table and column name, return a check constraint clause for that column in
* the table.
*
* This is an expensive query, so it is cached per-request and stored by table. The initial
* call for a table that has not been cached will query all columns and store that
* so subsequent calls are fast.
*
* @param string $tableName Table name column resides in
* @param string $columnName Column name the constraint is for
* @return string The check string
*/
public function getConstraintCheckClause($tableName, $columnName)
{
// Check already processed table columns
if (isset(self::$cached_checks[$tableName])) {
if (!isset(self::$cached_checks[$tableName][$columnName])) {
return null;
}
return self::$cached_checks[$tableName][$columnName];
}
// Regenerate cehcks for this table
$checks = array();
foreach ($this->preparedQuery("
SELECT CAST(CHECK_CLAUSE AS TEXT) AS CHECK_CLAUSE, COLUMN_NAME
FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS CC
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CCU ON CCU.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
WHERE TABLE_NAME = ?",
array($tableName)
) as $record) {
$checks[$record['COLUMN_NAME']] = $record['CHECK_CLAUSE'];
}
self::$cached_checks[$tableName] = $checks;
// Return via cached records
return $this->getConstraintCheckClause($tableName, $columnName);
}
/**
* Return the name of the default constraint applied to $tableName.$colName.
* Will return null if no such constraint exists
*
* @param string $tableName Name of the table
* @param string $colName Name of the column
* @return string|null
*/
protected function defaultConstraintName($tableName, $colName)
{
return $this->preparedQuery("
SELECT s.name --default name
FROM sys.sysobjects s
join sys.syscolumns c ON s.parent_obj = c.id
WHERE s.xtype = 'd'
and c.cdefault = s.id
and parent_obj = OBJECT_ID(?)
and c.name = ?",
array($tableName, $colName)
)->value();
}
/**
* Get enum values from a constraint check clause.
*
* @param string $clause Check clause to parse values from
* @return array Enum values
*/
protected function enumValuesFromCheckClause($clause)
{
$segments = preg_split('/ +OR *\[/i', $clause);
$constraints = array();
foreach ($segments as $segment) {
$bits = preg_split('/ *= */', $segment);
for ($i = 1; $i < sizeof($bits); $i += 2) {
array_unshift($constraints, substr(rtrim($bits[$i], ')'), 1, -1));
}
}
return $constraints;
}
/*
* Creates an ALTER expression for a column in MS SQL
*
* @param string $tableName Name of the table to be altered
* @param string $colName Name of the column to be altered
* @param string $colSpec String which contains conditions for a column
* @return string
*/
protected 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 = '/^(?<definition>[\w()]+)\s?(?<null>(?:not\s)?null)?\s?(?<default>default\s[\w\']+)?\s?(?<check>check\s?[\w()\'",\s]+)?$/i';
$matches = array();
preg_match($pattern, $colSpec, $matches);
// drop the index if it exists
$alterQueries = array();
// drop *ALL* indexes on a table before proceeding
// this won't drop primary keys, though
$indexes = $this->indexNames($tableName);
$indexes = array_filter($indexes);
foreach ($indexes as $indexName) {
$alterQueries[] = "IF EXISTS (SELECT name FROM sys.indexes WHERE name = '$indexName' AND object_id = object_id(SCHEMA_NAME() + '.$tableName')) DROP INDEX \"$indexName\" ON \"$tableName\";";
}
$prefix = "ALTER TABLE \"$tableName\" ";
// Remove the old default prior to adjusting the column.
if ($defaultConstraintName = $this->defaultConstraintName($tableName, $colName)) {
$alterQueries[] = "$prefix DROP CONSTRAINT \"$defaultConstraintName\";";
}
if (isset($matches['definition'])) {
//We will prevent any changes being made to the ID column. Primary key indexes will have a fit if we do anything here.
if ($colName != 'ID') {
// SET null / not null
$nullFragment = empty($matches['null']) ? '' : " {$matches['null']}";
$alterQueries[] = "$prefix ALTER COLUMN \"$colName\" {$matches['definition']}$nullFragment;";
// Add a default back
if (!empty($matches['default'])) {
$alterQueries[] = "$prefix ADD {$matches['default']} FOR \"$colName\";";
}
// SET check constraint (The constraint HAS to be dropped)
if (!empty($matches['check'])) {
$constraint = $this->getConstraintName($tableName, $colName);
if ($constraint) {
$alterQueries[] = "$prefix DROP CONSTRAINT {$constraint};";
}
//NOTE: 'with nocheck' seems to solve a few problems I've been having for modifying existing tables.
$alterQueries[] = "$prefix WITH NOCHECK ADD CONSTRAINT \"{$tableName}_{$colName}_check\" {$matches['check']};";
}
}
}
return implode("\n", $alterQueries);
}
public function renameTable($oldTableName, $newTableName)
{
$this->query("EXEC sp_rename \"$oldTableName\", \"$newTableName\"");
}
/**
* Checks a table's integrity and repairs it if necessary.
* NOTE: MSSQL does not appear to support any vacuum or optimise commands
*
* @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)
{
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");
}
public function renameField($tableName, $oldName, $newName)
{
$this->query("EXEC sp_rename @objname = '$tableName.$oldName', @newname = '$newName', @objtype = 'COLUMN'");
}
public function fieldList($table)
{
//This gets us more information than we need, but I've included it all for the moment....
$fieldRecords = $this->preparedQuery("SELECT ordinal_position, column_name, data_type, column_default,
is_nullable, character_maximum_length, numeric_precision, numeric_scale, collation_name
FROM information_schema.columns WHERE table_name = ?
ORDER BY ordinal_position;",
array($table)
);
// Cache the records from the query - otherwise a lack of multiple active result sets
// will cause subsequent queries to fail in this method
$fields = array();
$output = array();
foreach ($fieldRecords as $record) {
$fields[] = $record;
}
foreach ($fields as $field) {
// Update the data_type field to be a complete column definition string for use by
// SS_Database::requireField()
switch ($field['data_type']) {
case 'int':
case 'bigint':
case 'numeric':
case 'float':
case 'bit':
if ($field['data_type'] != 'bigint' && $field['data_type'] != 'int' && $sizeSuffix = $field['numeric_precision']) {
$field['data_type'] .= "($sizeSuffix)";
}
if ($field['is_nullable'] == 'YES') {
$field['data_type'] .= ' null';
} else {
$field['data_type'] .= ' not null';
}
if ($field['column_default']) {
$default=substr($field['column_default'], 2, -2);
$field['data_type'] .= " default $default";
}
break;
case 'decimal':
if ($field['numeric_precision']) {
$sizeSuffix = $field['numeric_precision'] . ',' . $field['numeric_scale'];
$field['data_type'] .= "($sizeSuffix)";
}
if ($field['is_nullable'] == 'YES') {
$field['data_type'] .= ' null';
} else {
$field['data_type'] .= ' not null';
}
if ($field['column_default']) {
$default=substr($field['column_default'], 2, -2);
$field['data_type'] .= " default $default";
}
break;
case 'nvarchar':
case 'varchar':
//Check to see if there's a constraint attached to this column:
$clause = $this->getConstraintCheckClause($table, $field['column_name']);
if ($clause) {
$constraints = $this->enumValuesFromCheckClause($clause);
$default=substr($field['column_default'], 2, -2);
$field['data_type'] = $this->enum(array(
'default' => $default,
'name' => $field['column_name'],
'enums' => $constraints,
'table' => $table
));
break;
}
default:
$sizeSuffix = $field['character_maximum_length'];
if ($sizeSuffix == '-1') {
$sizeSuffix = 'max';
}
if ($sizeSuffix) {
$field['data_type'] .= "($sizeSuffix)";
}
if ($field['is_nullable'] == 'YES') {
$field['data_type'] .= ' null';
} else {
$field['data_type'] .= ' not null';
}
if ($field['column_default']) {
$default=substr($field['column_default'], 2, -2);
$field['data_type'] .= " default '$default'";
}
}
$output[$field['column_name']] = $field;
}
return $output;
}
/**
* 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 SS_Database::requireIndex() for more details.
*/
public function createIndex($tableName, $indexName, $indexSpec)
{
$this->query($this->getIndexSqlDefinition($tableName, $indexName, $indexSpec));
}
/**
* Return SQL for dropping and recreating an index
*
* @param string $tableName Name of table to create this index against
* @param string $indexName Name of this index
* @param array|string $indexSpec Index specification, either as a raw string
* or parsed array form
* @return string The SQL required to generate this index
*/
protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec)
{
// Determine index name
$index = $this->buildMSSQLIndexName($tableName, $indexName);
// Consolidate/Cleanup spec into array format
$indexSpec = $this->parseIndexSpec($indexName, $indexSpec);
$drop = "IF EXISTS (SELECT name FROM sys.indexes WHERE name = '$index' AND object_id = object_id(SCHEMA_NAME() + '.$tableName')) DROP INDEX $index ON \"$tableName\";";
// create a type-specific index
if ($indexSpec['type'] == 'fulltext') {
if(!$this->database->fullTextEnabled()) {
return '';
}
// enable fulltext on this table
$this->createFullTextCatalog();
$primary_key = $this->getPrimaryKey($tableName);
if ($primary_key) {
return "$drop CREATE FULLTEXT INDEX ON \"$tableName\" ({$indexSpec['value']})"
. "KEY INDEX $primary_key WITH CHANGE_TRACKING AUTO;";
}
}
if ($indexSpec['type'] == 'unique') {
return "$drop CREATE UNIQUE INDEX $index ON \"$tableName\" ({$indexSpec['value']});";
}
return "$drop CREATE INDEX $index ON \"$tableName\" ({$indexSpec['value']});";
}
public function alterIndex($tableName, $indexName, $indexSpec)
{
$this->createIndex($tableName, $indexName, $indexSpec);
}
/**
* Return the list of indexes in a table.
* @param string $table The table name.
* @return array
*/
public function indexList($table)
{
$indexes = $this->query("EXEC sp_helpindex '$table';");
$indexList = array();
// Enumerate all basic indexes
foreach ($indexes as $index) {
if (strpos($index['index_description'], 'unique') !== false) {
$indexType = 'unique ';
} else {
$indexType = 'index ';
}
// Extract name from index
$baseIndexName = $this->buildMSSQLIndexName($table, '');
$indexName = substr($index['index_name'], strlen($baseIndexName));
// Extract columns
$columns = $this->quoteColumnSpecString($index['index_keys']);
$indexList[$indexName] = $this->parseIndexSpec($indexName, array(
'name' => $indexName,
'value' => $columns,
'type' => $indexType
));
}
// Now we need to check to see if we have any fulltext indexes attached to this table:
if ($this->database->fullTextEnabled()) {
$result = $this->query('EXEC sp_help_fulltext_columns;');
// Extract columns from this fulltext definition
$columns = array();
foreach ($result as $row) {
if ($row['TABLE_NAME'] == $table) {
$columns[] = $row['FULLTEXT_COLUMN_NAME'];
}
}
if (!empty($columns)) {
$indexList['SearchFields'] = $this->parseIndexSpec('SearchFields', array(
'name' => 'SearchFields',
'value' => $this->implodeColumnList($columns),
'type' => 'fulltext'
));
}
}
return $indexList;
}
/**
* For a given table name, get all the internal index names,
* except for those that are primary keys and fulltext indexes.
*
* @param string $tableName
* @return array
*/
public function indexNames($tableName)
{
return $this->preparedQuery('
SELECT ind.name FROM sys.indexes ind
INNER JOIN sys.tables t ON ind.object_id = t.object_id
WHERE is_primary_key = 0 AND t.name = ? AND ind.name IS NOT NULL',
array($tableName)
)->column();
}
public function tableList()
{
$tables = array();
foreach ($this->query("EXEC sp_tables @table_owner = 'dbo';") as $record) {
$tables[strtolower($record['TABLE_NAME'])] = $record['TABLE_NAME'];
}
return $tables;
}
/**
* Return a boolean type-formatted string
* We use 'bit' so that we can do numeric-based comparisons
*
* @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 'bit not null 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 null';
}
/**
* 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 = '0';
if (isset($values['default']) && is_numeric($values['default'])) {
$defaultValue = $values['default'];
}
return "decimal($precision) not null default $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)
{
// 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
$maxLength = max(array_map('strlen', $values['enums']));
return "varchar($maxLength) not null default '" . $values['default']
. "' check(\"" . $values['name'] . "\" in ('" . implode("','", $values['enums'])
. "'))";
}
/**
* @todo Make this work like {@link MySQLDatabase::set()}
*
* @param array $values
* @return string
*/
public function set($values)
{
return $this->enum($values);
}
/**
* 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(53) not null default ' . $values['default'];
}
/**
* 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 'int not null 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 not null default ' . (int) $values['default'];
}
/**
* Return a datetime type-formatted string
* For MS SQL, 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 'datetime null';
}
/**
* 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)
{
$collation = MSSQLDatabase::get_collation();
$collationSQL = $collation ? " COLLATE $collation" : "";
return "nvarchar(max)$collationSQL null";
}
/**
* 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 null';
}
/**
* 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)
{
$collation = MSSQLDatabase::get_collation();
$collationSQL = $collation ? " COLLATE $collation" : "";
return "nvarchar(" . $values['precision'] . ")$collationSQL null";
}
/**
* Return a 4 digit numeric type.
*
* @param array $values
* @return string
*/
public function year($values)
{
return 'numeric(4)';
}
/**
* This returns the column which is the primary key for each table
*
* @param bool $asDbValue
* @param bool $hasAutoIncPK
* @return string
*/
public function IdColumn($asDbValue = false, $hasAutoIncPK = true)
{
if ($asDbValue) {
return 'int not null';
} elseif ($hasAutoIncPK) {
return 'int identity(1,1)';
} else {
return 'int not null';
}
}
public function hasTable($tableName)
{
return (bool)$this->preparedQuery(
"SELECT table_name FROM information_schema.tables WHERE table_name = ?",
array($tableName)
)->value();
}
/**
* Returns the values of the given enum field
* NOTE: Experimental; introduced for db-abstraction and may changed before 2.4 is released.
*
* @param string $tableName
* @param string $fieldName
* @return array
*/
public function enumValuesForField($tableName, $fieldName)
{
$classes = array();
// Get the enum of all page types from the SiteTree table
$clause = $this->getConstraintCheckClause($tableName, $fieldName);
if ($clause) {
$classes = $this->enumValuesFromCheckClause($clause);
}
return $classes;
}
/**
* This is a lookup table for data types.
*
* For instance, MSSQL uses 'BIGINT', while MySQL uses 'UNSIGNED'
* and PostgreSQL uses 'INT'.
*
* @param string $type
* @return string
*/
public function dbDataType($type)
{
$values = array(
'unsigned integer'=>'BIGINT'
);
if (isset($values[$type])) {
return $values[$type];
} else {
return '';
}
}
protected function indexKey($table, $index, $spec)
{
return $index;
}
}

214
code/SQLServerConnector.php Normal file
View File

@ -0,0 +1,214 @@
<?php
namespace SilverStripe\MSSQL;
use SilverStripe\ORM\Connect\DBConnector;
/**
* Database connector driver for sqlsrv_ library
*/
class SQLServerConnector extends DBConnector
{
/**
* Connection to the DBMS.
*
* @var resource
*/
protected $dbConn = null;
/**
* Stores the affected rows of the last query.
* Used by sqlsrv functions only, as sqlsrv_rows_affected
* accepts a result instead of a database handle.
*
* @var integer
*/
protected $lastAffectedRows;
/**
* Name of the currently selected database
*
* @var string
*/
protected $selectedDatabase = null;
public function connect($parameters, $selectDB = false)
{
// Disable default warnings as errors behaviour for sqlsrv to keep it in line with mssql functions
if (ini_get('sqlsrv.WarningsReturnAsErrors')) {
ini_set('sqlsrv.WarningsReturnAsErrors', 'Off');
}
$charset = isset($parameters['charset']) ? $parameters : 'UTF-8';
$multiResultSets = isset($parameters['multipleactiveresultsets'])
? $parameters['multipleactiveresultsets']
: true;
$options = array(
'CharacterSet' => $charset,
'MultipleActiveResultSets' => $multiResultSets
);
if (!(defined('MSSQL_USE_WINDOWS_AUTHENTICATION') && MSSQL_USE_WINDOWS_AUTHENTICATION == true)
&& empty($parameters['windowsauthentication'])
) {
$options['UID'] = $parameters['username'];
$options['PWD'] = $parameters['password'];
}
// Required by MS Azure database
if ($selectDB && !empty($parameters['database'])) {
$options['Database'] = $parameters['database'];
}
$this->dbConn = sqlsrv_connect($parameters['server'], $options);
if (empty($this->dbConn)) {
$this->databaseError("Couldn't connect to SQL Server database");
} elseif ($selectDB && !empty($parameters['database'])) {
// Check selected database (Azure)
$this->selectedDatabase = $parameters['database'];
}
}
/**
* Start transaction. READ ONLY not supported.
*/
public function transactionStart()
{
$result = sqlsrv_begin_transaction($this->dbConn);
if (!$result) {
$this->databaseError("Couldn't start the transaction.");
}
}
/**
* Commit everything inside this transaction so far
*/
public function transactionEnd()
{
$result = sqlsrv_commit($this->dbConn);
if (!$result) {
$this->databaseError("Couldn't commit the transaction.");
}
}
/**
* 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()
{
$result = sqlsrv_rollback($this->dbConn);
if (!$result) {
$this->databaseError("Couldn't rollback the transaction.");
}
}
public function affectedRows()
{
return $this->lastAffectedRows;
}
public function getLastError()
{
$errorMessages = array();
$errors = sqlsrv_errors();
if ($errors) {
foreach ($errors as $info) {
$errorMessages[] = implode(', ', array($info['SQLSTATE'], $info['code'], $info['message']));
}
}
return implode('; ', $errorMessages);
}
public function isActive()
{
return $this->dbConn && $this->selectedDatabase;
}
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
{
// Reset state
$this->lastAffectedRows = 0;
// Run query
$parsedParameters = $this->parameterValues($parameters);
if (empty($parsedParameters)) {
$handle = sqlsrv_query($this->dbConn, $sql);
} else {
$handle = sqlsrv_query($this->dbConn, $sql, $parsedParameters);
}
// Check for error
if (!$handle) {
$this->databaseError($this->getLastError(), $errorLevel, $sql, $parsedParameters);
return null;
}
// Report result
$this->lastAffectedRows = sqlsrv_rows_affected($handle);
return new SQLServerQuery($this, $handle);
}
public function query($sql, $errorLevel = E_USER_ERROR)
{
return $this->preparedQuery($sql, array(), $errorLevel);
}
public function selectDatabase($name)
{
$this->query("USE \"$name\"");
$this->selectedDatabase = $name;
return true;
}
public function __destruct()
{
if (is_resource($this->dbConn)) {
sqlsrv_close($this->dbConn);
}
}
public function getVersion()
{
// @todo - use sqlsrv_server_info?
return trim($this->query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))")->value());
}
public function getGeneratedID($table)
{
return $this->query("SELECT IDENT_CURRENT('$table')")->value();
}
public function getSelectedDatabase()
{
return $this->selectedDatabase;
}
public function unloadDatabase()
{
$this->selectDatabase('Master');
$this->selectedDatabase = null;
}
/**
* Quotes a string, including the "N" prefix so unicode
* strings are saved to the database correctly.
*
* @param string $value String to be encoded
* @return string Processed string ready for DB
*/
public function quoteString($value)
{
return "N'" . $this->escapeString($value) . "'";
}
public function escapeString($value)
{
$value = str_replace("'", "''", $value);
$value = str_replace("\0", "[NULL]", $value);
return $value;
}
}

76
code/SQLServerQuery.php Normal file
View File

@ -0,0 +1,76 @@
<?php
namespace SilverStripe\MSSQL;
use DateTime;
use SilverStripe\ORM\Connect\Query;
/**
* A result-set from a MSSQL database.
*/
class SQLServerQuery extends Query
{
/**
* The SQLServerConnector object that created this result set.
*
* @var SQLServerConnector
*/
private $connector;
/**
* The internal MSSQL 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 SQLServerConnector $connector The database object that created this query.
* @param resource $handle the internal mssql handle that is points to the resultset.
*/
public function __construct(SQLServerConnector $connector, $handle)
{
$this->connector = $connector;
$this->handle = $handle;
}
public function __destruct()
{
if (is_resource($this->handle)) {
sqlsrv_free_stmt($this->handle);
}
}
public function getIterator()
{
if (is_resource($this->handle)) {
while ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) {
// special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers,
// so we convert to the usual Y-m-d H:i:s value!
foreach ($data as $name => $value) {
if ($value instanceof DateTime) {
$data[$name] = $value->format('Y-m-d H:i:s');
}
}
yield $data;
}
}
}
public function numRecords()
{
if (!is_resource($this->handle)) {
return false;
}
// WARNING: This will only work if the cursor type is scrollable!
if (function_exists('sqlsrv_num_rows')) {
return sqlsrv_num_rows($this->handle);
} else {
user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING);
}
}
}

35
composer.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "silverstripe/mssql",
"description": "Adds MSSQL support to SilverStripe",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "mssql", "database"],
"authors": [
{
"name": "Sam Minnee",
"email": "sam@silverstripe.com"
},
{
"name": "Sean Harvey",
"email": "sean@silverstripe.com"
}
],
"require": {
"silverstripe/framework": "^4"
},
"suggest": {
"ext-sqlsrv": "Required to support MSSQLDatabase as the server type",
"ext-pdo_sqlsrv": "Required to support MSSQLPDODatabase as the server type"
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"SilverStripe\\MSSQL\\": "code/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}

View File

@ -0,0 +1,35 @@
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
class MSSQLDatabaseQueryTest extends SapphireTest
{
public static $fixture_file = 'MSSQLDatabaseQueryTest.yml';
protected $extraDataObjects = array(
'MSSQLDatabaseQueryTestDataObject'
);
public function testDateValueFormatting()
{
$obj = $this->objFromFixture('MSSQLDatabaseQueryTestDataObject', 'test-data-1');
$this->assertEquals('2012-01-01', $obj->obj('TestDate')->Format('Y-m-d'), 'Date field value is formatted correctly (Y-m-d)');
}
public function testDatetimeValueFormatting()
{
$obj = $this->objFromFixture('MSSQLDatabaseQueryTestDataObject', 'test-data-1');
$this->assertEquals('2012-01-01 10:30:00', $obj->obj('TestDatetime')->Format('Y-m-d H:i:s'), 'Datetime field value is formatted correctly (Y-m-d H:i:s)');
}
}
class MSSQLDatabaseQueryTestDataObject extends DataObject implements TestOnly
{
private static $db = array(
'TestDate' => 'Date',
'TestDatetime' => 'Datetime'
);
}

View File

@ -0,0 +1,5 @@
MSSQLDatabaseQueryTestDataObject:
test-data-1:
TestDate: 2012-01-01
TestDatetime: 2012-01-01 10:30:00