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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
f5e39f88
Commit
f5e39f88
authored
Jun 09, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fourteenmeister-patch-1'
parents
20d11ed0
a4f23f1c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
0 deletions
+87
-0
QueryBuilder.php
framework/db/mssql/QueryBuilder.php
+87
-0
No files found.
framework/db/mssql/QueryBuilder.php
View file @
f5e39f88
...
@@ -139,4 +139,91 @@ class QueryBuilder extends \yii\db\QueryBuilder
...
@@ -139,4 +139,91 @@ class QueryBuilder extends \yii\db\QueryBuilder
return
"ALTER TABLE
{
$table
}
{
$enable
}
CONSTRAINT ALL"
;
return
"ALTER TABLE
{
$table
}
{
$enable
}
CONSTRAINT ALL"
;
}
}
public
function
buildOrderBy
(
$columns
)
{
if
(
empty
(
$columns
))
{
return
'ORDER BY (SELECT NULL)'
;
// hack so limit will work if no order by is specified
}
else
{
return
parent
::
buildOrderBy
(
$columns
);
}
}
public
function
build
(
$query
,
$params
=
[])
{
$query
->
prepareBuild
(
$this
);
$params
=
empty
(
$params
)
?
$query
->
params
:
array_merge
(
$params
,
$query
->
params
);
$clauses
=
[
$this
->
buildSelect
(
$query
->
select
,
$params
,
$query
->
distinct
,
$query
->
selectOption
),
$this
->
buildFrom
(
$query
->
from
,
$params
),
$this
->
buildJoin
(
$query
->
join
,
$params
),
$this
->
buildWhere
(
$query
->
where
,
$params
),
$this
->
buildGroupBy
(
$query
->
groupBy
),
$this
->
buildHaving
(
$query
->
having
,
$params
),
$this
->
buildOrderBy
(
$query
->
orderBy
),
$this
->
olderMssql
()
?
''
:
$this
->
buildLimit
(
$query
->
limit
,
$query
->
offset
),
];
$sql
=
implode
(
$this
->
separator
,
array_filter
(
$clauses
));
if
(
$this
->
olderMssql
())
$sql
=
$this
->
applyLimit
(
$sql
,
$query
);
$union
=
$this
->
buildUnion
(
$query
->
union
,
$params
);
if
(
$union
!==
''
)
{
$sql
=
"(
$sql
)
{
$this
->
separator
}
$union
"
;
}
return
[
$sql
,
$params
];
}
public
function
applyLimit
(
$sql
,
$query
)
{
$limit
=
$query
->
limit
!==
null
?
(
int
)
$query
->
limit
:
-
1
;
$offset
=
$query
->
offset
!==
null
?
(
int
)
$query
->
offset
:
-
1
;
if
(
$limit
>
0
||
$offset
>=
0
)
$sql
=
$this
->
rewriteLimitOffsetSql
(
$sql
,
$limit
,
$offset
,
$query
);
return
$sql
;
}
protected
function
rewriteLimitOffsetSql
(
$sql
,
$limit
,
$offset
,
$query
)
{
$originalOrdering
=
$this
->
buildOrderBy
(
$query
->
orderBy
);
if
(
$query
->
select
)
{
$select
=
implode
(
', '
,
$query
->
select
);
}
else
{
$select
=
$query
->
select
=
'*'
;
}
if
(
$select
===
'*'
)
{
$columns
=
$this
->
getAllColumnNames
(
$query
->
modelClass
);
if
(
$columns
&&
is_array
(
$columns
))
$select
=
implode
(
', '
,
$columns
);
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
);
$sql
=
"SELECT TOP
{
$limit
}
{
$select
}
FROM (
$sql
) sub WHERE rowNum >
{
$offset
}
"
;
return
$sql
;
}
protected
function
getAllColumnNames
(
$modelClass
=
null
)
{
if
(
!
$modelClass
)
{
return
null
;
}
$model
=
new
$modelClass
;
$schema
=
$model
->
getTableSchema
();
$columns
=
array_keys
(
$schema
->
columns
);
return
$columns
;
}
protected
function
olderMssql
()
{
$this
->
db
->
open
();
$version
=
preg_split
(
"/\./"
,
$this
->
db
->
pdo
->
getAttribute
(
\PDO
::
ATTR_SERVER_VERSION
));
return
$version
[
0
]
<
11
;
}
}
}
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