Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
osticket
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
docker
osticket
Commits
edf157ce
Commit
edf157ce
authored
11 years ago
by
Jared Hancock
Browse files
Options
Downloads
Patches
Plain Diff
orm: Refactor WHERE clause compilation
parent
470fd797
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
include/class.orm.php
+27
-40
27 additions, 40 deletions
include/class.orm.php
with
27 additions
and
40 deletions
include/class.orm.php
+
27
−
40
View file @
edf157ce
...
...
@@ -242,7 +242,7 @@ class SqlFunction {
$this
->
args
=
array_slice
(
func_get_args
(),
1
);
}
function
toSql
()
{
function
toSql
(
$compiler
=
false
)
{
$args
=
(
count
(
$this
->
args
))
?
implode
(
','
,
db_input
(
$this
->
args
))
:
""
;
return
sprintf
(
'%s(%s)'
,
$this
->
func
,
$args
);
}
...
...
@@ -329,7 +329,7 @@ class QuerySet implements IteratorAggregate, ArrayAccess {
function
delete
()
{
$class
=
$this
->
compiler
;
$compiler
=
new
$class
();
$ex
=
$compiler
->
compileDelete
(
$this
);
$ex
=
$compiler
->
compile
Bulk
Delete
(
$this
);
$ex
->
execute
();
return
$ex
->
affected_rows
();
}
...
...
@@ -666,7 +666,7 @@ class SqlCompiler {
return
$alias
;
}
function
compile
Where
(
$where
,
$model
)
{
function
compile
Constraints
(
$where
,
$model
)
{
$constraints
=
array
();
foreach
(
$where
as
$constraint
)
{
$filter
=
array
();
...
...
@@ -775,12 +775,15 @@ class MySqlCompiler extends SqlCompiler {
.
' '
.
$alias
.
' ON ('
.
implode
(
' AND '
,
$constraints
)
.
')'
;
}
function
input
(
$what
)
{
function
input
(
&
$what
)
{
if
(
$what
instanceof
QuerySet
)
{
$q
=
$what
->
getQuery
(
array
(
'nosort'
=>
true
));
$this
->
params
+=
$q
->
params
;
return
(
string
)
$q
;
}
elseif
(
$what
instanceof
SqlFunction
)
{
return
$val
->
toSql
(
$this
);
}
else
{
$this
->
params
[]
=
$what
;
return
'?'
;
...
...
@@ -791,17 +794,22 @@ class MySqlCompiler extends SqlCompiler {
return
"`
$what
`"
;
}
function
compileCount
(
$queryset
)
{
/**
* getWhereClause
*
* This builds the WHERE ... part of a DML statement. This should be
* called before ::getJoins(), because it may add joins into the
* statement based on the relationships used in the where clause
*/
protected
function
getWhereClause
(
$queryset
)
{
$model
=
$queryset
->
model
;
$table
=
$model
::
$meta
[
'table'
];
$where_pos
=
array
();
$where_neg
=
array
();
$joins
=
array
();
foreach
(
$queryset
->
constraints
as
$where
)
{
$where_pos
[]
=
$this
->
compile
Where
(
$where
,
$model
);
$where_pos
[]
=
$this
->
compile
Constraints
(
$where
,
$model
);
}
foreach
(
$queryset
->
exclusions
as
$where
)
{
$where_neg
[]
=
$this
->
compile
Where
(
$where
,
$model
);
$where_neg
[]
=
$this
->
compile
Constraints
(
$where
,
$model
);
}
$where
=
''
;
...
...
@@ -809,6 +817,13 @@ class MySqlCompiler extends SqlCompiler {
$where
=
' WHERE '
.
implode
(
' AND '
,
$where_pos
)
.
implode
(
' AND NOT '
,
$where_neg
);
}
return
$where
;
}
function
compileCount
(
$queryset
)
{
$model
=
$queryset
->
model
;
$table
=
$model
::
$meta
[
'table'
];
$where
=
$this
->
getWhereClause
(
$queryset
);
$joins
=
$this
->
getJoins
();
$sql
=
'SELECT COUNT(*) AS count FROM '
.
$this
->
quote
(
$table
)
.
$joins
.
$where
;
$exec
=
new
MysqlExecutor
(
$sql
,
$this
->
params
);
...
...
@@ -818,21 +833,7 @@ class MySqlCompiler extends SqlCompiler {
function
compileSelect
(
$queryset
)
{
$model
=
$queryset
->
model
;
$where_pos
=
array
();
$where_neg
=
array
();
$joins
=
array
();
foreach
(
$queryset
->
constraints
as
$where
)
{
$where_pos
[]
=
$this
->
compileWhere
(
$where
,
$model
);
}
foreach
(
$queryset
->
exclusions
as
$where
)
{
$where_neg
[]
=
$this
->
compileWhere
(
$where
,
$model
);
}
$where
=
''
;
if
(
$where_pos
||
$where_neg
)
{
$where
=
' WHERE '
.
implode
(
' AND '
,
$where_pos
)
.
implode
(
' AND NOT '
,
$where_neg
);
}
$where
=
$this
->
getWhereClause
(
$queryset
);
$sort
=
''
;
if
(
$queryset
->
ordering
&&
!
isset
(
$this
->
options
[
'nosort'
]))
{
...
...
@@ -886,24 +887,10 @@ class MySqlCompiler extends SqlCompiler {
function
compileInsert
()
{
}
function
compileDelete
(
$queryset
)
{
function
compile
Bulk
Delete
(
$queryset
)
{
$model
=
$queryset
->
model
;
$table
=
$model
::
$meta
[
'table'
];
$where_pos
=
array
();
$where_neg
=
array
();
$joins
=
array
();
foreach
(
$queryset
->
constraints
as
$where
)
{
$where_pos
[]
=
$this
->
compileWhere
(
$where
,
$model
);
}
foreach
(
$queryset
->
exclusions
as
$where
)
{
$where_neg
[]
=
$this
->
compileWhere
(
$where
,
$model
);
}
$where
=
''
;
if
(
$where_pos
||
$where_neg
)
{
$where
=
' WHERE '
.
implode
(
' AND '
,
$where_pos
)
.
implode
(
' AND NOT '
,
$where_neg
);
}
$where
=
$this
->
getWhereClause
(
$queryset
);
$joins
=
$this
->
getJoins
();
$sql
=
'DELETE '
.
$this
->
quote
(
$table
)
.
'.* FROM '
.
$this
->
quote
(
$table
)
.
$joins
.
$where
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment