Completed
Push — work-fleets ( ff9a05...837dd8 )
by SuperNova.WS
05:12
created

DbSqlLiteral::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
class DbSqlLiteral extends DbSqlAware {
4
5
  const SQL_LITERAL_ALIAS_NONE = null;
6
  const SQL_LITERAL_ALIAS_AUTO = '';
7
8
  public $literal = '';
9
10
  /**
11
   * DbSqlLiteral constructor.
12
   *
13
   * @param db_mysql|null $db
14
   * @param string        $literal
15
   */
16 1
  public function __construct($db = null, $literal = '') {
17 1
    parent::__construct($db);
18 1
    $this->literal = $literal;
19 1
  }
20
21
  /**
22
   * Renders single argument function
23
   * - supports autobuild alias
24
   * - supports direct alias
25
   * - supports dotted field names like 'table.field'
26
   * - supports all fields with '*' and 'table.*'
27
   *
28
   * @param string      $functionName
29
   * @param string      $field
30
   *  - '*' - all fields
31
   *  - string - quoted as field name
32
   * @param null|string $alias
33
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias
34
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field
35
   * - other value: using supplied alias
36
   *
37
   * @return $this
38
   */
39 12
  protected function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
40 12
    if ($alias === self::SQL_LITERAL_ALIAS_AUTO) {
41 4
      $alias = $this->makeAliasFromField($functionName, $field);
42 4
    }
43
44 12
    $this->literal = strtoupper($functionName) . '(' . $this->makeFieldFromString($field) . ')';
45
46 12
    if (!empty($alias)) {
47 8
      $this->literal .= ' AS `' . $alias . '`';
48 8
    }
49
50 12
    return $this;
51
  }
52
53
  /**
54
   * @param string      $field
55
   * @param null|string $alias
56
   *
57
   * @return $this
58
   *
59
   * @see buildSingleArgument
60
   */
61 12
  public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
62 12
    $this->buildSingleArgument('max', $field, $alias);
63
64 12
    return $this;
65
  }
66
67
68
  /**
69
   * @return string
70
   */
71 12
  public function __toString() {
72 12
    return $this->literal;
73
  }
74
75
}
76