Passed
Branch master (1f48b5)
by P.R.
05:45
created

ColumnMetadata::getColumnDefinition()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 32
nop 0
dl 0
loc 27
ccs 11
cts 12
cp 0.9167
crap 7.0283
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace SetBased\Audit\MySql\Metadata;
4
5
use SetBased\Audit\Metadata\ColumnMetadata as BaseColumnMetadata;
6
7
/**
8
 * Metadata of table columns.
9
 */
10
class ColumnMetadata extends BaseColumnMetadata
11
{
12
  //--------------------------------------------------------------------------------------------------------------------
13
  /**
14
   * The properties of the column that are stored by this class.
15
   *
16
   * var string[]
17
   */
18
  protected static $fields = ['column_name',
19
                              'column_type',
20
                              'is_nullable',
21
                              'character_set_name',
22
                              'collation_name'];
23
24
  //--------------------------------------------------------------------------------------------------------------------
25
  /**
26
   * @inheritdoc
27
   */
28 4
  public function getColumnDefinition()
29
  {
30 4
    $parts = [];
31
32 4
    if ($this->getProperty('column_type')!==null)
33
    {
34 4
      $parts[] = $this->getProperty('column_type');
35
    }
36
37 4
    if ($this->getProperty('character_set_name')!==null)
38
    {
39 2
      $parts[] = 'character set '.$this->getProperty('character_set_name');
40
    }
41
42 4
    if ($this->getProperty('collation_name')!==null)
43
    {
44 2
      $parts[] = 'collate '.$this->getProperty('collation_name');
45
    }
46
47 4
    $parts[] = ($this->getProperty('is_nullable')=='YES') ? 'null' : 'not null';
48
49 4
    if ($this->getProperty('column_default')!==null && $this->getProperty('column_default')!='NULL')
50
    {
51
      $parts[] = 'default '.$this->getProperty('column_default');
52
    }
53
54 4
    return implode(' ', $parts);
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  /**
59
   * @inheritdoc
60
   */
61 22
  public function getColumnAuditDefinition()
62
  {
63 22
    $parts = [];
64
65 22
    if ($this->getProperty('column_type')!==null)
66
    {
67 22
      $parts[] = $this->getProperty('column_type');
68
    }
69
70 22
    if ($this->getProperty('character_set_name')!==null)
71
    {
72 16
      $parts[] = 'character set '.$this->getProperty('character_set_name');
73
    }
74
75 22
    if ($this->getProperty('collation_name')!==null)
76
    {
77 16
      $parts[] = 'collate '.$this->getProperty('collation_name');
78
    }
79
80 22
    $parts[] = ($this->getProperty('is_nullable')=='YES') ? 'null' : 'not null';
81
82 22
    if ($this->getProperty('column_default')!==null && $this->getProperty('column_default')!='NULL')
83
    {
84 2
      $parts[] = 'default '.$this->getProperty('column_default');
85
    }
86 22
    elseif ($this->getProperty('column_type')=='timestamp')
87
    {
88
      // Prevent automatic updates of timestamp columns.
89 3
      $parts[] = 'default now()';
90
    }
91
92 22
    return implode(' ', $parts);
93
  }
94
95
  //--------------------------------------------------------------------------------------------------------------------
96
  /**
97
   * @inheritdoc
98
   */
99 4
  public function getTypeInfo1()
100
  {
101 4
    if ($this->getProperty('is_nullable')=='YES')
102
    {
103 4
      return $this->getProperty('column_type');
104
    }
105
106
    return $this->getProperty('column_type').' not null';
107
  }
108
109
  //--------------------------------------------------------------------------------------------------------------------
110
  /**
111
   * @inheritdoc
112
   */
113 4
  public function getTypeInfo2()
114
  {
115 4
    if ($this->getProperty('collation_name')!==null)
116
    {
117 2
      return sprintf('[%s] [%s]', $this->getProperty('character_set_name'), $this->getProperty('collation_name'));
118
    }
119
120 4
    return null;
121
  }
122
123
  //--------------------------------------------------------------------------------------------------------------------
124
}
125
126
//----------------------------------------------------------------------------------------------------------------------
127