SelectBuilder::select()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 24
nop 4
dl 0
loc 12
rs 9.2222
c 0
b 0
f 0
1
<?php
2
namespace Maphper\Lib;
3
class SelectBuilder {
4
	public function select($table, $criteria, $args, $options = []) {
5
		$where = $criteria ? ' WHERE ' . $criteria : '';
6
		$limit = (isset($options['limit'])) ? ' LIMIT ' . $options['limit'] : '';
7
8
		if (isset($options['offset'])) {
9
			$offset = ' OFFSET ' . $options['offset'];
10
			if (!$limit) $limit = ' LIMIT  1000';
11
		}
12
		else $offset = '';
13
14
		$order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : '';
15
		return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args);
16
	}
17
18
	public function aggregate($table, $function, $field, $where, $args, $group) {
19
		if ($group == true) $groupBy = ' GROUP BY ' . $field;
20
		else $groupBy = '';
21
		return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . '   FROM ' . $table . ($where != null ? ' WHERE ' . $where : '') . ' ' . $groupBy, $args);
22
	}
23
}
24