Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Stud.IP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan-Hendrik Willms
Stud.IP
Commits
73c350e2
Commit
73c350e2
authored
2 years ago
by
Jan-Hendrik Willms
Browse files
Options
Downloads
Patches
Plain Diff
adjust sorm methods to count/return only distinct values, fixes #1885
Closes #1885 Merge request
studip/studip!1233
parent
205b1e60
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/models/BlubberThread.php
+3
-3
3 additions, 3 deletions
lib/models/BlubberThread.php
lib/models/OERHost.php
+2
-2
2 additions, 2 deletions
lib/models/OERHost.php
lib/models/SimpleORMap.class.php
+45
-29
45 additions, 29 deletions
lib/models/SimpleORMap.class.php
with
50 additions
and
34 deletions
lib/models/BlubberThread.php
+
3
−
3
View file @
73c350e2
...
...
@@ -101,11 +101,11 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
/**
* @return BlubberThread[]
*/
public
static
function
findBySQL
(
$
sql
,
$params
=
[])
public
static
function
findBySQL
(
$
condition
,
$params
=
[])
{
return
parent
::
findAndMapBySQL
(
function
(
$thread
)
{
return
parent
::
findAndMapBySQL
(
function
(
BlubberThread
$thread
)
{
return
self
::
upgradeThread
(
$thread
);
},
$
sql
,
$params
);
},
$
condition
,
$params
);
}
/**
...
...
This diff is collapsed.
Click to expand it.
lib/models/OERHost.php
+
2
−
2
View file @
73c350e2
...
...
@@ -46,9 +46,9 @@ class OERHost extends OERIdentity
]);
}
public
static
function
findBySQL
(
$
sql
,
$params
=
[])
public
static
function
findBySQL
(
$
condition
,
$params
=
[])
{
$hosts
=
parent
::
findBySQL
(
$
sql
,
$params
);
$hosts
=
parent
::
findBySQL
(
$
condition
,
$params
);
foreach
(
$hosts
as
$key
=>
$host
)
{
$class
=
$host
[
'sorm_class'
];
if
(
$class
&&
(
$class
!==
'OERHost'
)
&&
is_subclass_of
(
$class
,
'OERHost'
))
{
...
...
This diff is collapsed.
Click to expand it.
lib/models/SimpleORMap.class.php
+
45
−
29
View file @
73c350e2
...
...
@@ -484,22 +484,29 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
/**
* returns number of records
*
* @param
?
string $
sql
sql clause to use on the right side of WHERE
* @param
?
array $params params for query
* @param string $
condition
sql clause to use on the right side of WHERE
* @param array $params params for query
* @return int
*/
public
static
function
countBySql
(
$
sql
=
'1'
,
$params
=
[])
public
static
function
countBySql
(
$
condition
=
'1'
,
$params
=
[])
{
$db_table
=
static
::
db_table
();
$db
=
DBManager
::
get
();
$has_join
=
stripos
(
$
sql
,
'JOIN '
);
$has_join
=
stripos
(
$
condition
,
'JOIN '
);
if
(
$has_join
===
false
||
$has_join
>
10
)
{
$sql
=
'WHERE '
.
$sql
;
$sql
=
"SELECT COUNT(*) FROM `
{
$db_table
}
` WHERE
{
$condition
}
"
;
}
else
{
$pk
=
implode
(
','
,
array_map
(
function
(
$key
)
use
(
$db_table
)
{
return
"`
{
$db_table
}
`.`
{
$key
}
`"
;
},
static
::
pk
()
));
$sql
=
"SELECT COUNT(DISTINCT
{
$pk
}
) FROM `
{
$db_table
}
`
{
$condition
}
"
;
}
$sql
=
"SELECT count(*) FROM `"
.
$db_table
.
"` "
.
$sql
;
$st
=
$db
->
prepare
(
$sql
);
$st
->
execute
(
$params
);
return
(
int
)
$st
->
fetchColumn
();
return
(
int
)
DBManager
::
get
()
->
fetchColumn
(
$sql
,
$params
);
}
/**
...
...
@@ -608,26 +615,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
/**
* returns array of instances of given class filtered by given sql
* @param string $
sql
sql clause to use on the right side of WHERE
* @param
?
array $params parameters for query
* @param string $
condition
sql clause to use on the right side of WHERE
* @param array $params parameters for query
* @return array array of "self" objects
*/
public
static
function
findBySQL
(
$
sql
,
$params
=
[])
public
static
function
findBySQL
(
$
condition
,
$params
=
[])
{
$db_table
=
static
::
db_table
();
$class
=
get_called_class
();
$record
=
new
$class
();
$db
=
DBManager
::
get
();
$has_join
=
stripos
(
$sql
,
'JOIN '
);
$has_join
=
stripos
(
$condition
,
'JOIN '
);
if
(
$has_join
===
false
||
$has_join
>
10
)
{
$sql
=
'WHERE '
.
$sql
;
$condition
=
"WHERE
{
$condition
}
"
;
$distinct
=
''
;
}
else
{
$distinct
=
'DISTINCT'
;
}
$sql
=
"SELECT `"
.
$db_table
.
"`.* FROM `"
.
$db_table
.
"` "
.
$sql
;
$ret
=
[];
$sql
=
"SELECT
{
$distinct
}
`
{
$db_table
}
`.* FROM `
{
$db_table
}
`
{
$condition
}
"
;
$record
=
new
static
();
$record
->
setNew
(
false
);
$stmt
=
DBManager
::
get
()
->
prepare
(
$sql
);
$stmt
->
execute
(
$params
);
$stmt
->
setFetchMode
(
PDO
::
FETCH_INTO
,
$record
);
$record
->
setNew
(
false
);
$ret
=
[];
while
(
$record
=
$stmt
->
fetch
())
{
// Reset all relations
$record
->
cleanup
();
...
...
@@ -695,23 +707,27 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
* passes objects for given sql through given callback
*
* @param callable $callable callback which gets the current record as param
* @param string $
sql
where clause of sql
* @param string $
condition
where clause of sql
* @param ?array $params sql statement parameters
* @return integer number of found records
*/
public
static
function
findEachBySQL
(
$callable
,
$
sql
,
$params
=
[])
public
static
function
findEachBySQL
(
$callable
,
$
condition
,
$params
=
[])
{
$has_join
=
stripos
(
$sql
,
'JOIN '
);
$db_table
=
static
::
db_table
();
$has_join
=
stripos
(
$condition
,
'JOIN '
);
if
(
$has_join
===
false
||
$has_join
>
10
)
{
$sql
=
"WHERE
{
$sql
}
"
;
$condition
=
"WHERE
{
$condition
}
"
;
$distinct
=
''
;
}
else
{
$distinct
=
'DISTINCT'
;
}
$sql
=
"SELECT
{
$distinct
}
`
{
$db_table
}
`.* FROM `
{
$db_table
}
`
{
$condition
}
"
;
$class
=
get_called_class
();
$record
=
new
$class
();
$record
=
new
static
();
$record
->
setNew
(
false
);
$db_table
=
static
::
db_table
();
$st
=
DBManager
::
get
()
->
prepare
(
"SELECT `
{
$db_table
}
`.* FROM `
{
$db_table
}
`
{
$sql
}
"
);
$st
=
DBManager
::
get
()
->
prepare
(
$sql
);
$st
->
execute
(
$params
);
$st
->
setFetchMode
(
PDO
::
FETCH_INTO
,
$record
);
...
...
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