ILIAS  release_7 Revision v7.30-3-g800a261c036
ResourceDBRepository.php
Go to the documentation of this file.
1<?php
2
4
8
15{
16 const TABLE_NAME = 'il_resource';
17 const IDENTIFICATION = 'rid';
18
22 protected $db;
23
24 protected $cache = [];
25
29 public function __construct(\ilDBInterface $db)
30 {
31 $this->db = $db;
32 }
33
34 public function getNamesForLocking() : array
35 {
36 return [self::TABLE_NAME];
37 }
38
42 public function blank(ResourceIdentification $identification) : StorableResource
43 {
44 return new StorableFileResource($identification);
45 }
46
50 public function get(ResourceIdentification $identification) : StorableResource
51 {
52 if (isset($this->cache[$identification->serialize()])) {
53 return $this->cache[$identification->serialize()];
54 }
55 $resource = $this->blank($identification);
56
57 $q = "SELECT " . self::IDENTIFICATION . ", storage_id FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
58 $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
59 $d = $this->db->fetchObject($r);
60
61 $resource->setStorageID($d->storage_id);
62
63 $this->cache[$identification->serialize()] = $resource;
64
65 return $resource;
66 }
67
71 public function has(ResourceIdentification $identification) : bool
72 {
73 if (isset($this->cache[$identification->serialize()])) {
74 return true;
75 }
76 $q = "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
77 $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
78
79 return (bool) $r->numRows() > 0;
80 }
81
85 public function store(StorableResource $resource) : void
86 {
87 $rid = $resource->getIdentification()->serialize();
88 if ($this->has($resource->getIdentification())) {
89 // UPDATE
90 $this->db->update(
91 self::TABLE_NAME,
92 [
93 self::IDENTIFICATION => ['text', $rid],
94 'storage_id' => ['text', $resource->getStorageID()],
95 ],
96 [
97 self::IDENTIFICATION => ['text', $rid],
98 ]
99 );
100 } else {
101 // CREATE
102 $this->db->insert(
103 self::TABLE_NAME,
104 [
105 self::IDENTIFICATION => ['text', $rid],
106 'storage_id' => ['text', $resource->getStorageID()],
107 ]
108 );
109 }
110 $this->cache[$rid] = $resource;
111 }
112
116 public function delete(StorableResource $resource) : void
117 {
118 $rid = $resource->getIdentification()->serialize();
119 $this->db->manipulateF(
120 "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
121 ['text'],
122 [$rid]
123 );
124 unset($this->cache[$rid]);
125 }
126
130 public function getAll() : \Generator
131 {
132 yield from [];
133 }
134
135 public function preload(array $identification_strings) : void
136 {
137 $r = $this->db->query(
138 "SELECT rid, storage_id FROM " . self::TABLE_NAME . " WHERE "
139 . $this->db->in(self::IDENTIFICATION, $identification_strings, false, 'text')
140 );
141 while ($d = $this->db->fetchAssoc($r)) {
142 $this->populateFromArray($d);
143 }
144 }
145
146 public function populateFromArray(array $data) : void
147 {
148 $resource = $this->blank(new ResourceIdentification($data['rid']));
149 $resource->setStorageID($data['storage_id']);
150 $this->cache[$data['rid']] = $resource;
151 }
152
153}
An exception for terminatinating execution or to throw for unit testing.
blank(ResourceIdentification $identification)
@inheritDoc
has(ResourceIdentification $identification)
@inheritDoc
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$data
Definition: storeScorm.php:23