Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
b01e6de9
Commit
b01e6de9
authored
Jun 09, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #3399: Added support for MS SQL Server older than 2012
parent
f5e39f88
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
13 deletions
+51
-13
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
QueryBuilder.php
framework/db/mssql/QueryBuilder.php
+50
-13
No files found.
framework/CHANGELOG.md
View file @
b01e6de9
...
...
@@ -63,6 +63,7 @@ Yii Framework 2 Change Log
-
Enh #3298: Supported configuring
`View::theme`
using a class name (netyum, qiangxue)
-
Enh #3328:
`BaseMailer`
generates better text body from html body (armab)
-
Enh #3380: Allow
`value`
in
`defaultValueValidator`
to be a closure (Alex-Code)
-
Enh #3399: Added support for MS SQL Server older than 2012 (fourteenmeister, samdark)
-
Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v)
-
Enh #3518:
`yii\helpers\Html::encode()`
now replaces invalid code sequences with "�" (DaSourcerer)
-
Enh #3520: Added
`unlinkAll()`
-method to active record to remove all records of a model relation (NmDimas, samdark, cebe)
...
...
framework/db/mssql/QueryBuilder.php
View file @
b01e6de9
...
...
@@ -139,16 +139,23 @@ class QueryBuilder extends \yii\db\QueryBuilder
return
"ALTER TABLE
{
$table
}
{
$enable
}
CONSTRAINT ALL"
;
}
/**
* @inheritdoc
*/
public
function
buildOrderBy
(
$columns
)
{
if
(
empty
(
$columns
))
{
return
'ORDER BY (SELECT NULL)'
;
// hack so limit will work if no order by is specified
// hack so LIMIT will work if no ORDER BY is specified
return
'ORDER BY (SELECT NULL)'
;
}
else
{
return
parent
::
buildOrderBy
(
$columns
);
}
}
/**
* @inheritdoc
*/
public
function
build
(
$query
,
$params
=
[])
{
$query
->
prepareBuild
(
$this
);
...
...
@@ -163,31 +170,48 @@ class QueryBuilder extends \yii\db\QueryBuilder
$this
->
buildGroupBy
(
$query
->
groupBy
),
$this
->
buildHaving
(
$query
->
having
,
$params
),
$this
->
buildOrderBy
(
$query
->
orderBy
),
$this
->
older
Mssql
()
?
''
:
$this
->
buildLimit
(
$query
->
limit
,
$query
->
offset
),
$this
->
isOld
Mssql
()
?
''
:
$this
->
buildLimit
(
$query
->
limit
,
$query
->
offset
),
];
$sql
=
implode
(
$this
->
separator
,
array_filter
(
$clauses
));
if
(
$this
->
olderMssql
())
$sql
=
$this
->
applyLimit
(
$sql
,
$query
);
if
(
$this
->
isOldMssql
())
{
$sql
=
$this
->
applyLimitAndOffset
(
$sql
,
$query
);
}
$union
=
$this
->
buildUnion
(
$query
->
union
,
$params
);
if
(
$union
!==
''
)
{
$sql
=
"(
$sql
)
{
$this
->
separator
}
$union
"
;
}
return
[
$sql
,
$params
];
}
public
function
applyLimit
(
$sql
,
$query
)
/**
* Applies limit and offset to SQL query
*
* @param string $sql SQL query
* @param \yii\db\ActiveQuery $query the [[Query]] object from which the SQL statement generated
* @return string resulting SQL
*/
private
function
applyLimitAndOffset
(
$sql
,
$query
)
{
$limit
=
$query
->
limit
!==
null
?
(
int
)
$query
->
limit
:
-
1
;
$offset
=
$query
->
offset
!==
null
?
(
int
)
$query
->
offset
:
-
1
;
if
(
$limit
>
0
||
$offset
>=
0
)
if
(
$limit
>
0
||
$offset
>=
0
)
{
$sql
=
$this
->
rewriteLimitOffsetSql
(
$sql
,
$limit
,
$offset
,
$query
);
}
return
$sql
;
}
protected
function
rewriteLimitOffsetSql
(
$sql
,
$limit
,
$offset
,
$query
)
/**
* Rewrites limit and offset in SQL query
*
* @param string $sql SQL query
* @param integer $limit
* @param integer $offset
* @param \yii\db\ActiveQuery $query the [[Query]] object from which the SQL statement generated
* @return string resulting SQL query
*/
private
function
rewriteLimitOffsetSql
(
$sql
,
$limit
,
$offset
,
$query
)
{
$originalOrdering
=
$this
->
buildOrderBy
(
$query
->
orderBy
);
if
(
$query
->
select
)
{
...
...
@@ -198,10 +222,11 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
if
(
$select
===
'*'
)
{
$columns
=
$this
->
getAllColumnNames
(
$query
->
modelClass
);
if
(
$columns
&&
is_array
(
$columns
))
if
(
$columns
&&
is_array
(
$columns
))
{
$select
=
implode
(
', '
,
$columns
);
else
}
else
{
$select
=
$columns
;
}
}
$sql
=
str_replace
(
$originalOrdering
,
''
,
$sql
);
$sql
=
preg_replace
(
'/^([\s(])*SELECT( DISTINCT)?(?!\s*TOP\s*\()/i'
,
"
\\
1SELECT
\\
2 rowNum = ROW_NUMBER() over (
{
$originalOrdering
}
),"
,
$sql
);
...
...
@@ -209,18 +234,30 @@ class QueryBuilder extends \yii\db\QueryBuilder
return
$sql
;
}
/**
* Returns an array of column names given model name
*
* @param string $modelClass name of the model class
* @return array|null array of column names
*/
protected
function
getAllColumnNames
(
$modelClass
=
null
)
{
if
(
!
$modelClass
)
{
return
null
;
}
/** @var \yii\db\ActiveRecord $model */
$model
=
new
$modelClass
;
$schema
=
$model
->
getTableSchema
();
$columns
=
array_keys
(
$schema
->
columns
);
return
$columns
;
}
protected
function
olderMssql
()
/**
* @return boolean if MSSQL used is old
* @throws \yii\base\InvalidConfigException
* @throws \yii\db\Exception
*/
protected
function
isOldMssql
()
{
$this
->
db
->
open
();
$version
=
preg_split
(
"/\./"
,
$this
->
db
->
pdo
->
getAttribute
(
\PDO
::
ATTR_SERVER_VERSION
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment