| 1: | <?php | 
| 2: |  | 
| 3: |  | 
| 4: |  | 
| 5: |  | 
| 6: |  | 
| 7: |  | 
| 8: |  | 
| 9: |  | 
| 10: |  | 
| 11: |  | 
| 12: | namespace Xmf\Database; | 
| 13: |  | 
| 14: | use Xmf\Yaml; | 
| 15: |  | 
| 16: |  | 
| 17: |  | 
| 18: |  | 
| 19: |  | 
| 20: |  | 
| 21: |  | 
| 22: |  | 
| 23: |  | 
| 24: |  | 
| 25: |  | 
| 26: |  | 
| 27: |  | 
| 28: | class TableLoad | 
| 29: | { | 
| 30: |  | 
| 31: |  | 
| 32: |  | 
| 33: |  | 
| 34: |  | 
| 35: |  | 
| 36: |  | 
| 37: |  | 
| 38: |  | 
| 39: |  | 
| 40: |  | 
| 41: | public static function loadTableFromArray($table, $data) | 
| 42: | { | 
| 43: |  | 
| 44: | $db = \XoopsDatabaseFactory::getDatabaseConnection(); | 
| 45: |  | 
| 46: | $prefixedTable = $db->prefix($table); | 
| 47: | $count = 0; | 
| 48: |  | 
| 49: | foreach ($data as $row) { | 
| 50: | $insertInto = 'INSERT INTO ' . $prefixedTable . ' ('; | 
| 51: | $valueClause = ' VALUES ('; | 
| 52: | $first = true; | 
| 53: | foreach ($row as $column => $value) { | 
| 54: | if ($first) { | 
| 55: | $first = false; | 
| 56: | } else { | 
| 57: | $insertInto .= ', '; | 
| 58: | $valueClause .= ', '; | 
| 59: | } | 
| 60: |  | 
| 61: | $insertInto .= '`' . $column . '`'; | 
| 62: | $valueClause .= $db->quote($value); | 
| 63: | } | 
| 64: |  | 
| 65: | $sql = $insertInto . ') ' . $valueClause . ')'; | 
| 66: |  | 
| 67: | $result = $db->queryF($sql); | 
| 68: | if (false !== $result) { | 
| 69: | ++$count; | 
| 70: | } | 
| 71: | } | 
| 72: |  | 
| 73: | return $count; | 
| 74: | } | 
| 75: |  | 
| 76: |  | 
| 77: |  | 
| 78: |  | 
| 79: |  | 
| 80: |  | 
| 81: |  | 
| 82: |  | 
| 83: |  | 
| 84: | public static function loadTableFromYamlFile($table, $yamlFile) | 
| 85: | { | 
| 86: | $count = 0; | 
| 87: |  | 
| 88: | $data = Yaml::readWrapped($yamlFile); | 
| 89: | if (false !== $data) { | 
| 90: | $count = static::loadTableFromArray($table, $data); | 
| 91: | } | 
| 92: |  | 
| 93: | return $count; | 
| 94: | } | 
| 95: |  | 
| 96: |  | 
| 97: |  | 
| 98: |  | 
| 99: |  | 
| 100: |  | 
| 101: |  | 
| 102: |  | 
| 103: | public static function truncateTable($table) | 
| 104: | { | 
| 105: |  | 
| 106: | $db = \XoopsDatabaseFactory::getDatabaseConnection(); | 
| 107: |  | 
| 108: | $prefixedTable = $db->prefix($table); | 
| 109: | $sql = 'TRUNCATE TABLE ' . $prefixedTable; | 
| 110: | $result = $db->queryF($sql); | 
| 111: | if (false !== $result) { | 
| 112: | $result = $db->getAffectedRows(); | 
| 113: | } | 
| 114: | return $result; | 
| 115: | } | 
| 116: |  | 
| 117: |  | 
| 118: |  | 
| 119: |  | 
| 120: |  | 
| 121: |  | 
| 122: |  | 
| 123: |  | 
| 124: |  | 
| 125: | public static function countRows($table, $criteria = null) | 
| 126: | { | 
| 127: |  | 
| 128: | $db = \XoopsDatabaseFactory::getDatabaseConnection(); | 
| 129: |  | 
| 130: | $prefixedTable = $db->prefix($table); | 
| 131: | $sql = 'SELECT COUNT(*) as `count` FROM ' . $prefixedTable . ' '; | 
| 132: | if (isset($criteria) && is_subclass_of($criteria, '\CriteriaElement')) { | 
| 133: |  | 
| 134: | $sql .= $criteria->renderWhere(); | 
| 135: | } | 
| 136: | $result = $db->query($sql); | 
| 137: | $row = $db->fetchArray($result); | 
| 138: | $count = $row['count']; | 
| 139: | $db->freeRecordSet($result); | 
| 140: | return (int)$count; | 
| 141: | } | 
| 142: |  | 
| 143: |  | 
| 144: |  | 
| 145: |  | 
| 146: |  | 
| 147: |  | 
| 148: |  | 
| 149: |  | 
| 150: |  | 
| 151: |  | 
| 152: | public static function extractRows($table, $criteria = null, $skipColumns = array()) | 
| 153: | { | 
| 154: |  | 
| 155: | $db = \XoopsDatabaseFactory::getDatabaseConnection(); | 
| 156: |  | 
| 157: | $prefixedTable = $db->prefix($table); | 
| 158: | $sql = 'SELECT * FROM ' . $prefixedTable . ' '; | 
| 159: | if (isset($criteria) && is_subclass_of($criteria, '\CriteriaElement')) { | 
| 160: |  | 
| 161: | $sql .= $criteria->renderWhere(); | 
| 162: | } | 
| 163: | $rows = array(); | 
| 164: | $result = $db->query($sql); | 
| 165: | if ($result) { | 
| 166: | while (false !== ($row = $db->fetchArray($result))) { | 
| 167: | $rows[] = $row; | 
| 168: | } | 
| 169: | } | 
| 170: |  | 
| 171: | $db->freeRecordSet($result); | 
| 172: |  | 
| 173: | if (!empty($skipColumns)) { | 
| 174: | foreach ($rows as $index => $row) { | 
| 175: | foreach ($skipColumns as $column) { | 
| 176: | unset($rows[$index][$column]); | 
| 177: | } | 
| 178: | } | 
| 179: | } | 
| 180: |  | 
| 181: | return $rows; | 
| 182: | } | 
| 183: |  | 
| 184: |  | 
| 185: |  | 
| 186: |  | 
| 187: |  | 
| 188: |  | 
| 189: |  | 
| 190: |  | 
| 191: |  | 
| 192: |  | 
| 193: |  | 
| 194: | public static function saveTableToYamlFile($table, $yamlFile, $criteria = null, $skipColumns = array()) | 
| 195: | { | 
| 196: | $rows = static::extractRows($table, $criteria, $skipColumns); | 
| 197: |  | 
| 198: | $count = Yaml::save($rows, $yamlFile); | 
| 199: |  | 
| 200: | return (false !== $count); | 
| 201: | } | 
| 202: | } | 
| 203: |  |