Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
anis
anis-server
Commits
ce31e59d
Commit
ce31e59d
authored
Jun 25, 2019
by
François Agneray
Browse files
#28
=> done
parent
c038dea1
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/Action/Search/SearchAction.php
View file @
ce31e59d
...
...
@@ -134,41 +134,49 @@ final class SearchAction
$queryBuilder
->
from
(
$dataset
->
getTableRef
());
if
(
array_key_exists
(
'c'
,
$queryParams
))
{
$this
->
where
(
$queryBuilder
,
$dataset
,
explode
(
';'
,
$queryParams
[
'c'
]));
}
$listOfIds
=
explode
(
';'
,
$queryParams
[
'a'
]);
if
(
$args
[
'type'
]
===
'data'
)
{
$this
->
select
(
$queryBuilder
,
$dataset
,
$listOfIds
);
try
{
$searchType
=
$this
->
getAndVerifySearchType
(
$args
[
'type'
]);
if
(
array_key_exists
(
'
o
'
,
$queryParams
))
{
$this
->
ord
er
(
$queryBuilder
,
$dataset
,
explode
(
';'
,
$queryParams
[
'
o
'
]));
if
(
array_key_exists
(
'
c
'
,
$queryParams
))
{
$this
->
wh
er
e
(
$queryBuilder
,
$dataset
,
explode
(
';'
,
$queryParams
[
'
c
'
]));
}
if
(
array_key_exists
(
'p'
,
$queryParams
))
{
$this
->
limit
(
$queryBuilder
,
$queryParams
[
'p'
]);
}
$result
=
$this
->
fetchAll
(
$queryBuilder
);
}
else
if
(
$args
[
'type'
]
===
'meta'
)
{
$queryBuilder
->
select
(
'COUNT(*) as nb'
);
$count
=
$this
->
fetchAll
(
$queryBuilder
);
$result
=
array
();
$result
[
'dataset_selected'
]
=
$dataset
->
getLabel
();
$attributesSelected
=
array
();
foreach
(
$listOfIds
as
$id
)
{
$attribute
=
$this
->
getAttribute
(
$dataset
,
(
int
)
$id
);
$attributesSelected
[]
=
array
(
'name'
=>
$attribute
->
getName
(),
'label'
=>
$attribute
->
getLabel
()
);
$listOfIds
=
explode
(
';'
,
$queryParams
[
'a'
]);
if
(
$searchType
===
'data'
)
{
$this
->
select
(
$queryBuilder
,
$dataset
,
$listOfIds
);
if
(
array_key_exists
(
'o'
,
$queryParams
))
{
$this
->
order
(
$queryBuilder
,
$dataset
,
explode
(
';'
,
$queryParams
[
'o'
]));
}
if
(
array_key_exists
(
'p'
,
$queryParams
))
{
$this
->
limit
(
$queryBuilder
,
$queryParams
[
'p'
]);
}
$result
=
$this
->
fetchAll
(
$queryBuilder
);
}
else
if
(
$searchType
===
'meta'
)
{
$queryBuilder
->
select
(
'COUNT(*) as nb'
);
$count
=
$this
->
fetchAll
(
$queryBuilder
);
$result
=
array
();
$result
[
'dataset_selected'
]
=
$dataset
->
getLabel
();
$attributesSelected
=
array
();
foreach
(
$listOfIds
as
$id
)
{
$attribute
=
$this
->
getAttribute
(
$dataset
,
(
int
)
$id
);
$attributesSelected
[]
=
array
(
'name'
=>
$attribute
->
getName
(),
'label'
=>
$attribute
->
getLabel
()
);
}
$result
[
'attributes_selected'
]
=
$attributesSelected
;
$result
[
'total_items'
]
=
$count
[
0
][
'nb'
];
}
$result
[
'attributes_selected'
]
=
$attributesSelected
;
$result
[
'total_items'
]
=
$count
[
0
][
'nb'
];
}
else
{
throw
SearchException
::
typeOfSearchDoesNotExist
(
$args
[
'type'
]);
}
catch
(
SearchException
$e
)
{
return
$this
->
dispatchHttpError
(
$response
,
'Invalid request'
,
$e
->
getMessage
()
)
->
withStatus
(
400
);
}
$this
->
logger
->
info
(
'SQL: '
.
$queryBuilder
->
getSQL
());
...
...
@@ -176,6 +184,14 @@ final class SearchAction
return
$response
->
withJson
(
$result
);
}
private
function
getAndVerifySearchType
(
string
$type
)
{
if
(
$type
!==
'data'
&&
$type
!==
'meta'
)
{
throw
SearchException
::
typeOfSearchDoesNotExist
(
$type
);
}
return
$type
;
}
private
function
select
(
QueryBuilder
$queryBuilder
,
Dataset
$dataset
,
array
$listOfIds
):
void
{
$columns
=
array
();
...
...
@@ -190,7 +206,7 @@ final class SearchAction
{
$expressions
=
array
();
foreach
(
$criteria
as
$criterion
)
{
$params
=
$
this
->
getCriterionParams
(
$criterion
);
$params
=
$
params
=
explode
(
':'
,
$criterion
);
$attribute
=
$this
->
getAttribute
(
$dataset
,
(
int
)
$params
[
0
]);
$column
=
$attribute
->
getTableName
()
.
'.'
.
$attribute
->
getName
();
$columnType
=
$attribute
->
getType
();
...
...
@@ -209,11 +225,16 @@ final class SearchAction
{
foreach
(
$orders
as
$order
)
{
$o
=
explode
(
':'
,
$order
);
if
(
count
(
$o
)
!=
2
)
{
throw
SearchException
::
badNumberOfParamsForOrder
();
}
$attribute
=
$this
->
getAttribute
(
$dataset
,
(
int
)
$o
[
0
]);
if
(
$o
[
1
]
===
'a'
)
{
$aord
=
'ASC'
;
}
else
if
(
$o
[
1
]
===
'd'
)
{
$aord
=
'DESC'
;
}
else
{
throw
SearchException
::
typeOfOrderDoesNotExist
(
$o
[
1
]);
}
$queryBuilder
->
orderBy
(
$attribute
->
getTableName
()
.
'.'
.
$attribute
->
getName
(),
$aord
);
}
...
...
@@ -222,6 +243,9 @@ final class SearchAction
private
function
limit
(
QueryBuilder
$queryBuilder
,
string
$param
):
void
{
$p
=
explode
(
':'
,
$param
);
if
(
count
(
$p
)
!=
2
)
{
throw
SearchException
::
badNumberOfParamsForLimit
();
}
$limit
=
$p
[
0
];
$offset
=
(
$p
[
1
]
-
1
)
*
$limit
;
$queryBuilder
...
...
@@ -235,16 +259,6 @@ final class SearchAction
return
$stmt
->
fetchAll
();
}
private
function
getCriterionParams
(
string
$criterion
):
array
{
$params
=
explode
(
':'
,
$criterion
);
$numberOfParams
=
count
(
$params
);
if
(
$numberOfParams
<
2
)
{
throw
SearchException
::
numberOfCriterionParams
(
$numberOfParams
,
$criterion
);
}
return
$params
;
}
private
function
getAttribute
(
Dataset
$dataset
,
int
$id
):
Attribute
{
$attributes
=
$dataset
->
getAttributes
();
...
...
src/Utils/Operator/OperatorException.php
View file @
ce31e59d
...
...
@@ -10,9 +10,9 @@
*/
namespace
App\Utils\Operator
;
use
Exception
;
use
App\Utils\Search
Exception
;
class
OperatorException
extends
Exception
class
OperatorException
extends
Search
Exception
{
/**
* @param string $operator
...
...
@@ -21,6 +21,16 @@ class OperatorException extends Exception
*/
public
static
function
unknownOperator
(
$operator
)
{
return
new
self
(
"The given operateor '"
.
$operator
.
"' is unknown"
);
return
new
self
(
"The given operator '"
.
$operator
.
"' is unknown"
);
}
public
static
function
operatorBadNumberOfParameters
(
$operator
,
$numberOfParametersRequired
)
{
return
new
self
(
"Operator '"
.
$operator
.
"' needs "
.
$numberOfParametersRequired
.
' values to work'
);
}
public
static
function
inBadNumberOfParameters
(
$operator
)
{
return
new
self
(
"Operator '"
.
$operator
.
"' needs at least 1 parameter to work"
);
}
}
src/Utils/Operator/OperatorFactory.php
View file @
ce31e59d
...
...
@@ -18,42 +18,81 @@ class OperatorFactory implements IOperatorFactory
{
switch
(
$type
)
{
case
'bw'
:
if
(
count
(
$parameters
)
!=
2
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
2
);
}
return
new
Between
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
],
$parameters
[
1
]);
case
'eq'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
Equal
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'neq'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
NotEqual
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'gt'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
GreaterThan
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'gte'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
GreaterThanEqual
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'lt'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
LessThan
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'lte'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
LessThanEqual
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'lk'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
Like
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'nlk'
:
if
(
count
(
$parameters
)
!=
1
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
NotLike
(
$expr
,
$column
,
$columnType
,
$parameters
[
0
]);
case
'in'
:
if
(
count
(
$parameters
)
<
1
)
{
throw
OperatorException
::
inBadNumberOfParameters
(
$type
);
}
return
new
In
(
$expr
,
$column
,
$columnType
,
$parameters
);
case
'nin'
:
if
(
count
(
$parameters
)
<
1
)
{
throw
OperatorException
::
inBadNumberOfParameters
(
$type
);
}
return
new
NotIn
(
$expr
,
$column
,
$columnType
,
$parameters
);
case
'nl'
:
if
(
count
(
$parameters
)
!=
0
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
OperatorNull
(
$expr
,
$column
,
$columnType
);
case
'nnl'
:
if
(
count
(
$parameters
)
!=
0
)
{
throw
OperatorException
::
operatorBadNumberOfParameters
(
$type
,
1
);
}
return
new
OperatorNotNull
(
$expr
,
$column
,
$columnType
);
default
:
...
...
src/Utils/SearchException.php
View file @
ce31e59d
...
...
@@ -21,7 +21,7 @@ class SearchException extends Exception
*/
public
static
function
paramDoesNotExist
(
$param
)
{
return
new
self
(
"The param '"
.
$param
.
"' does not exist
s
in the URL"
);
return
new
self
(
"The param '"
.
$param
.
"' does not exist in the URL"
);
}
public
static
function
numberOfCriterionParams
(
$numberOfParams
,
$criterion
)
...
...
@@ -35,13 +35,23 @@ class SearchException extends Exception
return
new
self
(
"Attribute with the id "
.
$id
.
" is not found for the dataset "
.
$datasetLabel
);
}
public
static
function
listOfIdsIsNotCorrect
(
)
public
static
function
typeOfSearchDoesNotExist
(
$param
)
{
return
new
self
(
"
List of ids is not correc
t"
);
return
new
self
(
"
The type of search '"
.
$param
.
"' does not exis
t"
);
}
public
static
function
typeOfSearchDoesNotExist
(
$param
)
public
static
function
badNumberOfParamsForOrder
()
{
return
new
self
(
"Order (o) needs 2 params to work"
);
}
public
static
function
typeOfOrderDoesNotExist
(
$order
)
{
return
new
self
(
"The type of order '"
.
$order
.
"' does not exist"
);
}
public
static
function
badNumberOfParamsForLimit
()
{
return
new
self
(
"
The type of search '"
.
$param
.
"' does not exists
"
);
return
new
self
(
"
Limit (p) needs 2 params to work
"
);
}
}
Write
Preview
Supports
Markdown
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