summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/ImportRun.php
blob: d3bdb7c37dbf2b25b64954b5c6ca3a88c9b1a81c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

namespace Icinga\Module\Director\Objects;

use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;

class ImportRun extends DbObject
{
    protected $table = 'import_run';

    protected $keyName = 'id';

    protected $autoincKeyName = 'id';

    /** @var ImportSource */
    protected $importSource = null;

    protected $defaultProperties = [
        'id'              => null,
        'source_id'       => null,
        'rowset_checksum' => null,
        'start_time'      => null,
        'end_time'        => null,
        // TODO: Check whether succeeded could be dropped
        'succeeded'       => null,
    ];

    protected $binaryProperties = [
        'rowset_checksum',
    ];

    public function prepareImportedObjectQuery($columns = array('object_name'))
    {
        return $this->getDb()->select()->from(
            array('r' => 'imported_row'),
            $columns
        )->joinLeft(
            array('rsr' => 'imported_rowset_row'),
            'rsr.row_checksum = r.checksum',
            array()
        )->where(
            'rsr.rowset_checksum = ?',
            $this->getConnection()->quoteBinary($this->rowset_checksum)
        );
    }

    public function listColumnNames()
    {
        $db = $this->getDb();

        $query = $db->select()->distinct()->from(
            array('p' => 'imported_property'),
            'property_name'
        )->join(
            array('rp' => 'imported_row_property'),
            'rp.property_checksum = p.checksum',
            array()
        )->join(
            array('rsr' => 'imported_rowset_row'),
            'rsr.row_checksum = rp.row_checksum',
            array()
        )->where('rsr.rowset_checksum = ?', $this->getConnection()->quoteBinary($this->rowset_checksum));

        return $db->fetchCol($query);
    }

    public function fetchRows($columns, $filter = null, $keys = null)
    {
        $db = $this->getDb();
        /** @var Db $connection */
        $connection = $this->getConnection();
        $binchecksum = $this->rowset_checksum;

        $query = $db->select()->from(
            array('rsr' => 'imported_rowset_row'),
            array(
                'object_name'    => 'r.object_name',
                'property_name'  => 'p.property_name',
                'property_value' => 'p.property_value',
                'format'         => 'p.format'
            )
        )->join(
            array('r' => 'imported_row'),
            'rsr.row_checksum = r.checksum',
            array()
        )->join(
            array('rp' => 'imported_row_property'),
            'r.checksum = rp.row_checksum',
            array()
        )->join(
            array('p' => 'imported_property'),
            'p.checksum = rp.property_checksum',
            array()
        )->order('r.object_name');
        if ($connection->isMysql()) {
            $query->where('rsr.rowset_checksum = :checksum')->bind([
                'checksum' => $binchecksum
            ]);
        } else {
            $query->where(
                'rsr.rowset_checksum = ?',
                $connection->quoteBinary($binchecksum)
            );
        }

        if ($columns === null) {
            $columns = $this->listColumnNames();
        } else {
            $query->where('p.property_name IN (?)', $columns);
        }

        $result = array();
        $empty = (object) array();
        foreach ($columns as $k => $v) {
            $empty->$k = null;
        }

        if ($keys !== null) {
            $query->where('r.object_name IN (?)', $keys);
        }

        foreach ($db->fetchAll($query) as $row) {
            if (! array_key_exists($row->object_name, $result)) {
                $result[$row->object_name] = clone($empty);
            }

            if ($row->format === 'json') {
                $result[$row->object_name]->{$row->property_name} = json_decode($row->property_value);
            } else {
                $result[$row->object_name]->{$row->property_name} = $row->property_value;
            }
        }

        if ($filter) {
            $filtered = array();
            foreach ($result as $key => $row) {
                if ($filter->matches($row)) {
                    $filtered[$key] = $row;
                }
            }

            return $filtered;
        }

        return $result;
    }

    public function importSource()
    {
        if ($this->importSource === null) {
            $this->importSource = ImportSource::loadWithAutoIncId(
                (int) $this->get('source_id'),
                $this->connection
            );
        }
        return $this->importSource;
    }
}