Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Tomáš Trávníček
doctrine-dbal
Commits
1473df19
Commit
1473df19
authored
Aug 11, 2007
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new DQL WHERE parser
parent
b83321e5
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
162 deletions
+86
-162
From.php
lib/Doctrine/Query/From.php
+4
-3
JoinCondition.php
lib/Doctrine/Query/JoinCondition.php
+1
-1
Where.php
lib/Doctrine/Query/Where.php
+81
-158
No files found.
lib/Doctrine/Query/From.php
View file @
1473df19
...
...
@@ -73,13 +73,14 @@ class Doctrine_Query_From extends Doctrine_Query_Part
foreach
(
Doctrine_Tokenizer
::
bracketExplode
(
$part
,
','
)
as
$reference
)
{
$reference
=
trim
(
$reference
);
$e
=
explode
(
'.'
,
$reference
);
$e
=
explode
(
' '
,
$reference
);
$e2
=
explode
(
'.'
,
$e
[
0
]);
if
(
$operator
)
{
$
reference
=
array_shift
(
$e
)
.
$operator
.
implode
(
'.'
,
$e
);
$
e
[
0
]
=
array_shift
(
$e2
)
.
$operator
.
implode
(
'.'
,
$e2
);
}
$table
=
$this
->
query
->
load
(
$reference
);
$table
=
$this
->
query
->
load
(
implode
(
' '
,
$e
)
);
}
$operator
=
(
$last
==
'INNER'
)
?
':'
:
'.'
;
...
...
lib/Doctrine/Query/JoinCondition.php
View file @
1473df19
...
...
@@ -58,7 +58,7 @@ class Doctrine_Query_JoinCondition extends Doctrine_Query_Condition
if
(
substr
(
$trimmed
,
0
,
4
)
==
'FROM'
||
substr
(
$trimmed
,
0
,
6
)
==
'SELECT'
)
{
// subquery found
$q
=
new
Doctrine_Q
uery
();
$q
=
$this
->
query
->
createSubq
uery
();
$value
=
'('
.
$q
->
parseQuery
(
$trimmed
)
->
getQuery
()
.
')'
;
}
elseif
(
substr
(
$trimmed
,
0
,
4
)
==
'SQL:'
)
{
$value
=
'('
.
substr
(
$trimmed
,
4
)
.
')'
;
...
...
lib/Doctrine/Query/Where.php
View file @
1473df19
...
...
@@ -32,107 +32,65 @@ Doctrine::autoload('Doctrine_Query_Condition');
*/
class
Doctrine_Query_Where
extends
Doctrine_Query_Condition
{
/**
* load
* returns the parsed query part
*
* @param string $where
* @return string
*/
public
function
load
(
$where
)
{
$where
=
trim
(
$where
);
$where
=
Doctrine_Tokenizer
::
bracketTrim
(
trim
(
$where
)
);
$conn
=
$this
->
query
->
getConnection
();
$terms
=
Doctrine_Tokenizer
::
sqlExplode
(
$where
);
$e
=
Doctrine_Tokenizer
::
sqlExplode
(
$where
);
if
(
count
(
$e
)
>
1
)
{
$tmp
=
$e
[
0
]
.
' '
.
$e
[
1
];
if
(
substr
(
$tmp
,
0
,
6
)
==
'EXISTS'
)
{
if
(
count
(
$terms
)
>
1
)
{
if
(
substr
(
$where
,
0
,
6
)
==
'EXISTS'
)
{
return
$this
->
parseExists
(
$where
,
true
);
}
elseif
(
substr
(
$where
,
0
,
10
)
==
'NOT EXISTS'
)
{
return
$this
->
parseExists
(
$where
,
false
);
}
}
if
(
count
(
$
e
)
<
3
)
{
$
e
=
Doctrine_Tokenizer
::
sqlExplode
(
$where
,
array
(
'='
,
'<
'
,
'>'
,
'!='
));
if
(
count
(
$
terms
)
<
3
)
{
$
terms
=
Doctrine_Tokenizer
::
sqlExplode
(
$where
,
array
(
'='
,
'<'
,
'<>
'
,
'>'
,
'!='
));
}
$r
=
array_shift
(
$e
);
$a
=
explode
(
'.'
,
$r
);
if
(
count
(
$a
)
>
1
)
{
$field
=
array_pop
(
$a
);
$count
=
count
(
$e
);
$slice
=
array_slice
(
$e
,
-
1
,
1
);
$value
=
implode
(
''
,
$slice
);
$operator
=
trim
(
substr
(
$where
,
strlen
(
$r
),
-
strlen
(
$value
)));
$reference
=
implode
(
'.'
,
$a
);
$count
=
count
(
$a
);
$pos
=
strpos
(
$field
,
'('
);
if
(
count
(
$terms
)
>
1
)
{
$first
=
array_shift
(
$terms
);
$value
=
array_pop
(
$terms
);
$operator
=
trim
(
substr
(
$where
,
strlen
(
$first
),
-
strlen
(
$value
)));
$table
=
null
;
$field
=
null
;
if
(
$pos
!==
false
)
{
$func
=
substr
(
$field
,
0
,
$pos
);
$value
=
trim
(
substr
(
$field
,
(
$pos
+
1
),
-
1
));
$values
=
Doctrine_Query
::
sqlExplode
(
$value
,
','
);
if
(
strpos
(
$first
,
"'"
)
===
false
&&
strpos
(
$first
,
'('
)
===
false
)
{
// normal field reference found
$a
=
explode
(
'.'
,
$first
);
$field
=
array_pop
(
$a
);
$reference
=
implode
(
'.'
,
$a
);
$table
=
$this
->
query
->
load
(
$reference
,
false
);
$field
=
$table
->
getColumnName
(
$field
);
array_pop
(
$a
);
$reference2
=
implode
(
'.'
,
$a
);
$alias
=
$this
->
query
->
getTableAlias
(
$reference2
);
$map
=
$this
->
query
->
load
(
$reference
,
false
);
$
stack
=
$this
->
query
->
getRelationStack
(
);
$
relation
=
end
(
$stack
)
;
$
alias
=
$this
->
query
->
getTableAlias
(
$reference
);
$
table
=
$map
[
'table'
]
;
$stack
=
$this
->
query
->
getTableStack
();
$first
=
$conn
->
quoteIdentifier
(
$alias
)
.
'.'
.
$conn
->
quoteIdentifier
(
$table
->
getColumnName
(
$field
));
}
else
{
$first
=
$this
->
query
->
parseClause
(
$first
);
}
$sql
=
$first
.
' '
.
$operator
.
' '
.
$this
->
parseValue
(
$value
,
$table
,
$field
);
switch
(
$func
)
{
case
'contains'
:
case
'regexp'
:
case
'like'
:
$operator
=
$this
->
getOperator
(
$func
);
return
$sql
;
}
else
{
if
(
empty
(
$relation
))
{
throw
new
Doctrine_Query_Exception
(
'DQL functions contains/regexp/like can only be used for fields of related components'
);
}
$where
=
array
();
foreach
(
$values
as
$value
)
{
$where
[]
=
$conn
->
quoteIdentifier
(
$alias
.
'.'
.
$relation
->
getLocal
())
.
' IN (SELECT '
.
$conn
->
quoteIdentifier
(
$relation
->
getForeign
())
.
' FROM '
.
$conn
->
quoteIdentifier
(
$relation
->
getTable
()
->
getTableName
())
.
' WHERE '
.
$field
.
$operator
.
$value
.
')'
;
}
$where
=
implode
(
' AND '
,
$where
);
break
;
default
:
throw
new
Doctrine_Query_Exception
(
'Unknown DQL function: '
.
$func
);
}
}
else
{
$map
=
$this
->
query
->
load
(
$reference
,
false
);
$alias
=
$this
->
query
->
getTableAlias
(
$reference
);
$table
=
$map
[
'table'
];
$field
=
$table
->
getColumnName
(
$field
);
// check if value is enumerated value
$enumIndex
=
$table
->
enumIndex
(
$field
,
trim
(
$value
,
"'"
));
public
function
parseValue
(
$value
,
Doctrine_Table
$table
=
null
,
$field
=
null
)
{
if
(
substr
(
$value
,
0
,
1
)
==
'('
)
{
// trim brackets
$trimmed
=
Doctrine_Tokenizer
::
bracketTrim
(
$value
);
if
(
substr
(
$trimmed
,
0
,
4
)
==
'FROM'
||
substr
(
$trimmed
,
0
,
6
)
==
'SELECT'
)
{
if
(
substr
(
$trimmed
,
0
,
4
)
==
'FROM'
||
substr
(
$trimmed
,
0
,
6
)
==
'SELECT'
)
{
// subquery found
$q
=
new
Doctrine_Query
();
$value
=
'('
.
$q
->
isSubquery
(
true
)
->
parseQuery
(
$trimmed
)
->
getQuery
()
.
')'
;
...
...
@@ -144,8 +102,13 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition
$e
=
Doctrine_Tokenizer
::
sqlExplode
(
$trimmed
,
','
);
$value
=
array
();
$index
=
false
;
foreach
(
$e
as
$part
)
{
if
(
isset
(
$table
)
&&
isset
(
$field
))
{
$index
=
$table
->
enumIndex
(
$field
,
trim
(
$part
,
"'"
));
}
if
(
$index
!==
false
)
{
$value
[]
=
$index
;
...
...
@@ -157,41 +120,22 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition
}
}
elseif
(
substr
(
$value
,
0
,
1
)
==
':'
||
$value
===
'?'
)
{
// placeholder found
if
(
$table
->
getTypeOf
(
$field
)
==
'enum'
)
{
if
(
isset
(
$table
)
&&
isset
(
$field
)
&&
$table
->
getTypeOf
(
$field
)
==
'enum'
)
{
$this
->
query
->
addEnumParam
(
$value
,
$table
,
$field
);
}
else
{
$this
->
query
->
addEnumParam
(
$value
,
null
,
null
);
}
}
else
{
if
(
$enumIndex
!==
false
)
{
$value
=
$enumIndex
;
}
else
{
$value
=
$this
->
parseLiteralValue
(
$value
);
}
}
// check if value is enumerated value
$enumIndex
=
$table
->
enumIndex
(
$field
,
trim
(
$value
,
"'"
));
switch
(
$operator
)
{
case
'<'
:
case
'>'
:
case
'='
:
case
'!='
:
if
(
$enumIndex
!==
false
)
{
$value
=
$enumIndex
;
}
default
:
if
(
$this
->
query
->
getType
()
===
Doctrine_Query
::
SELECT
)
{
$fieldname
=
$alias
?
$conn
->
quoteIdentifier
(
$alias
.
'.'
.
$field
)
:
$field
;
}
else
{
$fieldname
=
$field
;
}
$where
=
$fieldname
.
' '
.
$operator
.
' '
.
$value
;
}
$value
=
$this
->
parseLiteralValue
(
$value
);
}
}
return
$
wher
e
;
return
$
valu
e
;
}
/**
* parses an EXISTS expression
...
...
@@ -207,32 +151,11 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition
$pos
=
strpos
(
$where
,
'('
);
if
(
$pos
==
false
)
{
throw
new
Doctrine_Query_Exception
(
"Unknown expression, expected '('"
);
throw
new
Doctrine_Query_Exception
(
'Unknown expression, expected a subquery with () -marks'
);
}
$sub
=
Doctrine_Tokenizer
::
bracketTrim
(
substr
(
$where
,
$pos
));
return
$operator
.
' ('
.
$this
->
query
->
createSubquery
()
->
parseQuery
(
$sub
,
false
)
->
getQuery
()
.
')'
;
}
/**
* getOperator
*
* @param string $func
* @return string
*/
public
function
getOperator
(
$func
)
{
switch
(
$func
)
{
case
'contains'
:
$operator
=
' = '
;
break
;
case
'regexp'
:
$operator
=
$this
->
query
->
getConnection
()
->
getRegexpOperator
();
break
;
case
'like'
:
$operator
=
' LIKE '
;
break
;
}
return
$operator
;
}
}
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