1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xmf\Database;
13:
14: use Xoops\Core\Yaml;
15:
16: /**
17: * Xmf\Database\TableLoad
18: *
19: * load a database table
20: *
21: * @category Xmf\Database\TableLoad
22: * @package Xmf
23: * @author Richard Griffith <richard@geekwright.com>
24: * @copyright 2013 XOOPS Project (http://xoops.org)
25: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26: * @version Release: 1.0
27: * @link http://xoops.org
28: */
29: class TableLoad
30: {
31:
32: /**
33: * loadTableFromArray
34: *
35: * @param string $table name of table to load without prefix
36: * @param array $data array of rows to insert
37: * Each element of the outer array represents a single table row.
38: * Each row is an associative array in 'column' => 'value' format.
39: *
40: * @return int number of rows inserted
41: */
42: public static function loadTableFromArray($table, $data)
43: {
44: $db = \Xoops::getInstance()->db();
45: $count = 0;
46: $db->beginTransaction();
47: foreach ($data as $row) {
48: $count += $db->insertPrefix($table, $row);
49: }
50: $db->commit();
51: return $count;
52: }
53:
54: /**
55: * loadTableFromYamlFile
56: *
57: * @param string $table name of table to load without prefix
58: * @param string $yamlFile name of file containing data dump in YAML format
59: *
60: * @return int number of rows inserted
61: */
62: public static function loadTableFromYamlFile($table, $yamlFile)
63: {
64: $count = 0;
65:
66: $data = Yaml::read($yamlFile);
67: if ($data) {
68: $count = self::loadTableFromArray($table, $data);
69: }
70:
71: return $count;
72: }
73:
74: /**
75: * truncateTable - empty a database table
76: *
77: * @param string $table name of table to truncate
78: *
79: * @return int number of affected rows
80: */
81: public static function truncateTable($table)
82: {
83: $db = \Xoops::getInstance()->db();
84: $platform = $db->getDatabasePlatform();
85: $sql = $platform->getTruncateTableSQL($db->prefix($table));
86:
87: return $db->exec($sql);
88: }
89:
90: /**
91: * rowCount - get count of rows in a table
92: *
93: * @param string $table name of table to count
94: * @param CriteriaElement $criteria optional criteria
95: *
96: * @return int number of rows
97: */
98: public static function rowCount($table, $criteria = null)
99: {
100: $db = \Xoops::getInstance()->db();
101: $qb = $db->createXoopsQueryBuilder();
102: $qb ->select('COUNT(*)')
103: ->fromPrefix($table, '');
104: if (isset($criteria) && is_subclass_of($criteria, 'Xoops\Core\Kernel\CriteriaElement')) {
105: $qb = $criteria->renderQb($qb, '');
106: }
107: $result = $qb->execute();
108: $count = $result->fetchColumn();
109: return $count;
110: }
111: }
112: