Completed
Push — master ( bd0260...a2eaf5 )
by Lars
02:43
created

Helper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 3
Metric Value
wmc 17
c 4
b 2
f 3
lcom 0
cbo 1
dl 0
loc 121
ccs 59
cts 59
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getDbFields() 0 34 7
C copyTableRow() 0 62 10
1
<?php
2
3
namespace voku\db;
4
5
/**
6
 * Helper: this handles extra functions that use the "DB"-Class
7
 *
8
 * @package   voku\db
9
 */
10
class Helper
11
{
12
  /**
13
   * return all db-fields from a table
14
   *
15
   * @param string $table
16
   * @param bool   $useStaticCache
17
   * @param DB|null $db
18
   *
19
   * @return array
20
   */
21 1
  public static function getDbFields($table, $useStaticCache = true, DB $db = null)
22
  {
23 1
    static $dbFieldsCache = array();
24
25
    // use the cache
26
    if (
27
        $useStaticCache === true
28 1
        &&
29 1
        isset($dbFieldsCache[$table])
30 1
    ) {
31 1
      return $dbFieldsCache[$table];
32
    }
33
34
    // init
35 1
    $dbFields = array();
36
37 1
    if ($db === null) {
38 1
      $db = DB::getInstance();
39 1
    }
40
41 1
    $sql = 'SHOW COLUMNS FROM `' . $db->escape($table) . '`';
42 1
    $result = $db->query($sql);
43
44 1
    if ($result && $result->num_rows > 0) {
45 1
      foreach ($result->fetchAllArray() as $tmpResult) {
46 1
        $dbFields[] = $tmpResult['Field'];
47 1
      }
48 1
    }
49
50
    // add to static cache
51 1
    $dbFieldsCache[$table] = $dbFields;
52
53 1
    return $dbFields;
54
  }
55
56
  /**
57
   * copy row within a DB table and making updates to columns
58
   *
59
   * @param string  $table
60
   * @param array   $whereArray
61
   * @param array   $updateArray
62
   * @param array   $ignoreArray
63
   * @param DB|null $db
64
   *
65
   * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br />
66
   *                   "false" on error
67
   */
68 1
  public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $db = null)
69
  {
70
    // init
71 1
    $whereSQL = '';
72 1
    $return = false;
73
74 1
    if ($db === null) {
75 1
      $db = DB::getInstance();
76 1
    }
77
78 1
    $table = $db->escape($table);
79
80 1
    foreach ($whereArray as $key => $value) {
81 1
      $whereSQL = ' AND ' . $db->escape($key) . ' = ' . $db->escape($value);
82 1
    }
83
84
    // get the row
85 1
    $query = 'SELECT * FROM ' . $table . '
86
      WHERE 1 = 1
87 1
      ' . $whereSQL . '
88 1
    ';
89 1
    $result = $db->query($query);
90
91
    // make sure the row exists
92 1
    if ($result->num_rows > 0) {
93
94 1
      foreach ($result->fetchAllArray() as $tmpArray) {
95
96
        // re-build a new DB query and ignore some field-names
97 1
        $bindings = array();
98 1
        $insert_keys = '';
99 1
        $insert_values = '';
100
101 1
        foreach ($tmpArray as $fieldName => $value) {
102
103 1
          if (!in_array($fieldName, $ignoreArray, true)) {
104 1
            if (array_key_exists($fieldName, $updateArray)) {
105
106 1
              if ($updateArray[$fieldName] || $updateArray[$fieldName] == 0) {
107 1
                $insert_keys .= ',' . $fieldName;
108 1
                $insert_values .= ',?';
109 1
                $bindings[] = $updateArray[$fieldName];
110 1
              }
111
112 1
            } else {
113 1
              $insert_keys .= ',' . $fieldName;
114 1
              $insert_values .= ',?';
115 1
              $bindings[] = $value;
116
            }
117 1
          }
118 1
        }
119
120
        // insert the "copied" row
121 1
        $new_query = 'INSERT INTO `' . $table . '` (' . ltrim($insert_keys, ',') . ')
122 1
          VALUES (' . ltrim($insert_values, ',') . ')
123 1
        ';
124 1
        $return = $db->query($new_query, $bindings);
125 1
      }
126 1
    }
127
128 1
    return $return;
129
  }
130
}
131