Add statistics support for Sqlite by tweeking one query and rewrite an other. The rewrite implied a complete refactor of the MySql query as well.
Now the code is more flexible and make less queries to the database.
See #527
* Calculates entry repartition for all feeds and for main stream.
* The repartition includes:
@ -54,46 +56,15 @@ SQL;
* @return string
*/
public function calculateEntryCount() {
$count = array();
$count = $this->initEntryCountArray();
$period = self::ENTRY_COUNT_PERIOD;
// Generates a list of 30 last day to be sure we always have 30 days.
// If we do not do that kind of thing, we'll end up with holes in the
// days if the user do not have a lot of feeds.
$sql = <<<SQL
SELECT - (tens.val + units.val + 1) AS day
FROM (
SELECT 0 AS val
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS units
CROSS JOIN (
SELECT 0 AS val
UNION ALL SELECT 10
UNION ALL SELECT 20
) AS tens
ORDER BY day ASC
SQL;
$stm = $this->bd->prepare($sql);
$stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($res as $value) {
$count[$value['day']] = 0;
}
// Get stats per day for the last 30 days and applies the result on
// the array created with the last query.
// Get stats per day for the last 30 days
$sql = <<<SQL
SELECT DATEDIFF(FROM_UNIXTIME(e.date), NOW()) AS day,
COUNT(1) AS count
FROM {$this->prefix}entry AS e
WHERE FROM_UNIXTIME(e.date, '%Y%m%d') BETWEEN DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -30 DAY), '%Y%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d')
WHERE FROM_UNIXTIME(e.date, '%Y%m%d') BETWEEN DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -{$period} DAY), '%Y%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d')