4 * This file is a library of database access functions for RackTables.
8 function escapeString ($value)
11 return substr ($dbxlink->quote (htmlentities ($value)), 1, -1);
14 // This function returns detailed information about either all or one
15 // rack row depending on its argument.
16 function getRackRowInfo ($rackrow_id = 0)
20 "select dict_key, dict_value, count(Rack.id) as count, " .
21 "if(isnull(sum(Rack.height)),0,sum(Rack.height)) as sum " .
22 "from Chapter natural join Dictionary left join Rack on Rack.row_id = dict_key " .
23 "where chapter_name = 'RackRow' " .
24 ($rackrow_id > 0 ?
"and dict_key = ${rackrow_id} " : '') .
25 "group by dict_key order by dict_value";
26 $result = $dbxlink->query ($query);
29 showError ('SQL query failed in getRackRowInfo()');
33 $clist = array ('dict_key', 'dict_value', 'count', 'sum');
34 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
35 foreach ($clist as $dummy => $cname)
36 $ret[$row['dict_key']][$cname] = $row[$cname];
37 $result->closeCursor();
39 return current ($ret);
44 // This function returns id->name map for all object types. The map is used
45 // to build <select> input for objects.
46 function getObjectTypeList ()
48 return readChapter ('RackObjectType');
51 function getObjectList ($type_id = 0)
55 "select distinct RackObject.id as id , RackObject.name as name, dict_value as objtype_name, " .
56 "RackObject.label as label, RackObject.barcode as barcode, " .
57 "dict_key as objtype_id, asset_no, rack_id, Rack.name as Rack_name from " .
58 "((RackObject inner join Dictionary on objtype_id=dict_key natural join Chapter) " .
59 "left join RackSpace on RackObject.id = object_id) " .
60 "left join Rack on rack_id = Rack.id " .
61 "where objtype_id = '${type_id}' and RackObject.deleted = 'no' " .
62 "and chapter_name = 'RackObjectType' order by name";
63 $result = $dbxlink->query ($query);
66 showError ('SQL query failed in getObjectList()');
70 $clist = array ('id', 'name', 'label', 'barcode', 'objtype_name', 'objtype_id', 'asset_no', 'rack_id', 'Rack_name');
71 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
73 foreach ($clist as $dummy => $cname)
74 $ret[$row['id']][$cname] = $row[$cname];
75 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
77 $result->closeCursor();
81 function getRacksForRow ($row_id = 0)
85 "select Rack.id, Rack.name, height, Rack.comment, row_id, dict_value as row_name " .
86 "from Rack left join Dictionary on row_id = dict_key natural join Chapter " .
87 "where chapter_name = 'RackRow' and Rack.deleted = 'no' " .
88 (($row_id == 0) ?
"" : "and row_id = ${row_id} ") .
89 "order by row_name, Rack.id";
90 $result = $dbxlink->query ($query);
93 showError ('SQL query failed in getRacksForRow()');
97 $clist = array ('id', 'name', 'height', 'comment', 'row_id', 'row_name');
98 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
99 foreach ($clist as $dummy => $cname)
100 $ret[$row['id']][$cname] = $row[$cname];
101 $result->closeCursor();
102 usort ($ret, 'sortRacks');
103 $ret = restoreRackIDs ($ret);
107 // This is a popular helper for getting information about
108 // a particular rack and its rackspace at once.
109 function getRackData ($rack_id = 0, $silent = FALSE)
113 if ($silent == FALSE)
114 showError ('Invalid rack_id in getRackData()');
119 "select Rack.id, Rack.name, row_id, height, Rack.comment, dict_value as row_name from " .
120 "Rack left join Dictionary on Rack.row_id = dict_key natural join Chapter " .
121 "where chapter_name = 'RackRow' and Rack.id='${rack_id}' and Rack.deleted = 'no' limit 1";
122 $result1 = $dbxlink->query ($query);
123 if ($result1 == NULL)
125 if ($silent == FALSE)
126 showError ("SQL query #1 failed in getRackData()");
129 if (($row = $result1->fetch (PDO
::FETCH_ASSOC
)) == NULL)
131 if ($silent == FALSE)
132 showError ('Query #1 succeded, but returned no data in getRackData()');
137 $rack['id'] = $row['id'];
138 $rack['name'] = $row['name'];
139 $rack['height'] = $row['height'];
140 $rack['comment'] = $row['comment'];
141 $rack['row_id'] = $row['row_id'];
142 $rack['row_name'] = $row['row_name'];
143 $result1->closeCursor();
145 // start with default rackspace
146 for ($i = $rack['height']; $i > 0; $i--)
147 for ($locidx = 0; $locidx < 3; $locidx++
)
148 $rack[$i][$locidx]['state'] = 'F';
152 "select unit_no, atom, state, object_id " .
153 "from RackSpace where rack_id = ${rack_id} and " .
154 "unit_no between 1 and " . $rack['height'] . " order by unit_no";
155 $result2 = $dbxlink->query ($query);
156 if ($result2 == NULL)
158 if ($silent == FALSE)
159 showError ('SQL query failure #2 in getRackData()');
163 while ($row = $result2->fetch (PDO
::FETCH_ASSOC
))
165 $rack[$row['unit_no']][$loclist[$row['atom']]]['state'] = $row['state'];
166 $rack[$row['unit_no']][$loclist[$row['atom']]]['object_id'] = $row['object_id'];
168 $result2->closeCursor();
172 // This is a popular helper.
173 function getObjectInfo ($object_id = 0)
177 showError ('Invalid object_id in getObjectInfo()');
182 "select id, name, label, barcode, dict_value as objtype_name, asset_no, dict_key as objtype_id, has_problems, comment from " .
183 "RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter " .
184 "where id = '${object_id}' and deleted = 'no' and chapter_name = 'RackObjectType' limit 1";
185 $result = $dbxlink->query ($query);
188 $ei = $dbxlink->errorInfo();
189 showError ("SQL query failed in getObjectInfo (${object_id}) with error ${ei[1]} (${ei[2]})");
192 if (($row = $result->fetch (PDO
::FETCH_ASSOC
)) == NULL)
194 showError ('Query succeded, but returned no data in getObjectInfo()');
199 $ret['id'] = $row['id'];
200 $ret['name'] = $row['name'];
201 $ret['label'] = $row['label'];
202 $ret['barcode'] = $row['barcode'];
203 $ret['objtype_name'] = $row['objtype_name'];
204 $ret['objtype_id'] = $row['objtype_id'];
205 $ret['has_problems'] = $row['has_problems'];
206 $ret['asset_no'] = $row['asset_no'];
207 $ret['dname'] = displayedName ($ret);
208 $ret['comment'] = $row['comment'];
210 $result->closeCursor();
214 function getPortTypes ()
216 $chapter = readChapter ('PortType');
218 foreach ($chapter as $entry)
219 $ret[$entry['dict_key']] = $entry['dict_value'];
223 function getObjectPortsAndLinks ($object_id = 0)
227 showError ('Invalid object_id in getObjectPorts()');
232 "select Port.id as Port_id, ".
233 "Port.name as Port_name, ".
234 "Port.label as Port_label, ".
235 "Port.l2address as Port_l2address, ".
236 "Port.type as Port_type, ".
237 "Port.reservation_comment as Port_reservation_comment, " .
238 "dict_value as PortType_name, ".
239 "RemotePort.id as RemotePort_id, ".
240 "RemotePort.name as RemotePort_name, ".
241 "RemotePort.object_id as RemotePort_object_id, ".
242 "RackObject.name as RackObject_name ".
246 "Port inner join Dictionary on Port.type = dict_key natural join Chapter".
248 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
250 "left join Port as RemotePort on Link.portb=RemotePort.id or Link.porta=RemotePort.id ".
252 "left join RackObject on RemotePort.object_id=RackObject.id ".
253 "where chapter_name = 'PortType' and Port.object_id=${object_id} ".
254 "and (Port.id != RemotePort.id or RemotePort.id is null) ".
255 "order by Port_name";
256 $result = $dbxlink->query ($query);
265 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
267 $ret[$count]['id'] = $row['Port_id'];
268 $ret[$count]['name'] = $row['Port_name'];
269 $ret[$count]['l2address'] = l2addressFromDatabase ($row['Port_l2address']);
270 $ret[$count]['label'] = $row['Port_label'];
271 $ret[$count]['type_id'] = $row['Port_type'];
272 $ret[$count]['type'] = $row['PortType_name'];
273 $ret[$count]['reservation_comment'] = $row['Port_reservation_comment'];
274 $ret[$count]['remote_id'] = $row['RemotePort_id'];
275 $ret[$count]['remote_name'] = htmlentities ($row['RemotePort_name'], ENT_QUOTES
);
276 $ret[$count]['remote_object_id'] = $row['RemotePort_object_id'];
277 $ret[$count]['remote_object_name'] = $row['RackObject_name'];
281 $result->closeCursor();
285 function commitAddRack ($name, $height, $row_id, $comment)
288 $query = "insert into Rack(row_id, name, height, comment) values('${row_id}', '${name}', '${height}', '${comment}')";
289 $result1 = $dbxlink->query ($query);
290 if ($result1 == NULL)
292 showError ('SQL query failed in commitAddRack()');
295 // last_insert_id() is MySQL-specific
296 $query = 'select last_insert_id()';
297 $result2 = $dbxlink->query ($query);
298 if ($result2 == NULL)
300 showError ('Cannot get last ID in commitAddRack()');
303 // we always have a row
304 $row = $result2->fetch (PDO
::FETCH_NUM
);
305 $last_insert_id = $row[0];
306 $result2->closeCursor();
307 return recordHistory ('Rack', "id = ${last_insert_id}");
310 function commitAddObject ($new_name, $new_label, $new_barcode, $new_type_id, $new_asset_no)
313 // Maintain UNIQUE INDEX for common names and asset tags.
314 $new_asset_no = empty ($new_asset_no) ?
'NULL' : "'${new_asset_no}'";
315 $new_barcode = empty ($new_barcode) ?
'NULL' : "'${new_barcode}'";
316 $new_name = empty ($new_name) ?
'NULL' : "'${new_name}'";
318 "insert into RackObject(name, label, barcode, objtype_id, asset_no) " .
319 "values(${new_name}, '${new_label}', ${new_barcode}, '${new_type_id}', ${new_asset_no})";
320 $result1 = $dbxlink->query ($query);
321 if ($result1 == NULL)
323 $errorInfo = $dbxlink->errorInfo();
324 showError ("SQL query '${query}' failed in commitAddObject(): " . $errorInfo[2]);
327 if ($result1->rowCount() != 1)
329 showError ('Adding new object failed in commitAddObject()');
332 $query = 'select last_insert_id()';
333 $result2 = $dbxlink->query ($query);
334 if ($result2 == NULL)
336 $errorInfo = $dbxlink->errorInfo();
337 showError ("SQL query '${query}' failed in commitAddObject(): " . $errorInfo[2]);
340 // we always have a row
341 $row = $result2->fetch (PDO
::FETCH_NUM
);
342 $last_insert_id = $row[0];
343 $result2->closeCursor();
344 return recordHistory ('RackObject', "id = ${last_insert_id}");
347 function commitUpdateObject ($object_id = 0, $new_name = '', $new_label = '', $new_barcode = '', $new_type_id = 0, $new_has_problems = 'no', $new_asset_no = '', $new_comment = '')
349 if ($object_id == 0 ||
$new_type_id == 0)
351 showError ('Not all required args to commitUpdateObject() are present.');
355 $new_asset_no = empty ($new_asset_no) ?
'NULL' : "'${new_asset_no}'";
356 $new_barcode = empty ($new_barcode) ?
'NULL' : "'${new_barcode}'";
357 $new_name = empty ($new_name) ?
'NULL' : "'${new_name}'";
358 $query = "update RackObject set name=${new_name}, label='${new_label}', barcode=${new_barcode}, objtype_id='${new_type_id}', " .
359 "has_problems='${new_has_problems}', asset_no=${new_asset_no}, comment='${new_comment}' " .
360 "where id='${object_id}' limit 1";
361 $result = $dbxlink->query ($query);
364 showError ("SQL query '${query}' failed in commitUpdateObject");
367 if ($result->rowCount() != 1)
369 showError ('Error updating object information in commitUpdateObject()');
372 $result->closeCursor();
373 return recordHistory ('RackObject', "id = ${object_id}");
376 function commitUpdateRack ($rack_id, $new_name, $new_height, $new_row_id, $new_comment)
378 if (empty ($rack_id) ||
empty ($new_name) ||
empty ($new_height))
380 showError ('Not all required args to commitUpdateRack() are present.');
384 $query = "update Rack set name='${new_name}', height='${new_height}', comment='${new_comment}', row_id=${new_row_id} " .
385 "where id='${rack_id}' limit 1";
386 $result1 = $dbxlink->query ($query);
387 if ($result1->rowCount() != 1)
389 showError ('Error updating rack information in commitUpdateRack()');
392 return recordHistory ('Rack', "id = ${rack_id}");
395 // This function accepts rack data returned by getRackData(), validates and applies changes
396 // supplied in $_REQUEST and returns resulting array. Only those changes are examined, which
397 // correspond to current rack ID.
398 // 1st arg is rackdata, 2nd arg is unchecked state, 3rd arg is checked state.
399 // If 4th arg is present, object_id fields will be updated accordingly to the new state.
400 // The function returns the modified rack upon success.
401 function processGridForm (&$rackData, $unchecked_state, $checked_state, $object_id = 0)
403 global $loclist, $dbxlink;
404 $rack_id = $rackData['id'];
405 $rack_name = $rackData['name'];
406 $rackchanged = FALSE;
407 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
409 for ($locidx = 0; $locidx < 3; $locidx++
)
411 if ($rackData[$unit_no][$locidx]['enabled'] != TRUE)
414 $state = $rackData[$unit_no][$locidx]['state'];
415 if (isset ($_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"]) and $_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"] == 'on')
416 $newstate = $checked_state;
418 $newstate = $unchecked_state;
419 if ($state == $newstate)
423 $atom = $loclist[$locidx];
424 // The only changes allowed are those introduced by checkbox grid.
427 !($state == $checked_state && $newstate == $unchecked_state) &&
428 !($state == $unchecked_state && $newstate == $checked_state)
430 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, 'atom ${atom}', cannot change state from '${state}' to '${newstate}'");
431 // Here we avoid using ON DUPLICATE KEY UPDATE by first performing DELETE
432 // anyway and then looking for probable need of INSERT.
434 "delete from RackSpace where rack_id = ${rack_id} and " .
435 "unit_no = ${unit_no} and atom = '${atom}' limit 1";
436 $r = $dbxlink->query ($query);
438 return array ('code' => 500, 'message' => "${rack_name}: SQL DELETE query failed in processGridForm()");
439 if ($newstate != 'F')
442 "insert into RackSpace(rack_id, unit_no, atom, state) " .
443 "values(${rack_id}, ${unit_no}, '${atom}', '${newstate}') ";
444 $r = $dbxlink->query ($query);
446 return array ('code' => 500, 'message' => "${rack_name}: SQL INSERT query failed in processGridForm()");
448 if ($newstate == 'T' and $object_id != 0)
450 // At this point we already have a record in RackSpace.
452 "update RackSpace set object_id=${object_id} " .
453 "where rack_id=${rack_id} and unit_no=${unit_no} and atom='${atom}' limit 1";
454 $r = $dbxlink->query ($query);
455 if ($r->rowCount() == 1)
456 $rackData[$unit_no][$locidx]['object_id'] = $object_id;
458 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, atom '${atom}' failed to set object_id to '${object_id}'");
463 return array ('code' => 200, 'message' => "${rack_name}: All changes were successfully saved.");
465 return array ('code' => 300, 'message' => "${rack_name}: No changes.");
468 // This function builds a list of rack-unit-atom records, which are assigned to
469 // the requested object.
470 function getMoleculeForObject ($object_id = 0)
474 showError ("object_id == 0 in getMoleculeForObject()");
479 "select rack_id, unit_no, atom from RackSpace " .
480 "where state = 'T' and object_id = ${object_id} order by rack_id, unit_no, atom";
481 $result = $dbxlink->query ($query);
484 showError ("SQL query failed in getMoleculeForObject()");
487 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
488 $result->closeCursor();
492 // This function builds a list of rack-unit-atom records for requested molecule.
493 function getMolecule ($mid = 0)
497 showError ("mid == 0 in getMolecule()");
502 "select rack_id, unit_no, atom from Atom " .
503 "where molecule_id=${mid}";
504 $result = $dbxlink->query ($query);
507 showError ("SQL query failed in getMolecule()");
510 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
511 $result->closeCursor();
515 // This function creates a new record in Molecule and number of linked
516 // R-U-A records in Atom.
517 function createMolecule ($molData)
520 $query = "insert into Molecule values()";
521 $result1 = $dbxlink->query ($query);
522 if ($result1->rowCount() != 1)
524 showError ('Error inserting into Molecule in createMolecule()');
527 $query = 'select last_insert_id()';
528 $result2 = $dbxlink->query ($query);
529 if ($result2 == NULL)
531 showError ('Cannot get last ID in createMolecule().');
534 $row = $result2->fetch (PDO
::FETCH_NUM
);
535 $molecule_id = $row[0];
536 $result2->closeCursor();
537 foreach ($molData as $dummy => $rua)
539 $rack_id = $rua['rack_id'];
540 $unit_no = $rua['unit_no'];
541 $atom = $rua['atom'];
543 "insert into Atom(molecule_id, rack_id, unit_no, atom) " .
544 "values (${molecule_id}, ${rack_id}, ${unit_no}, '${atom}')";
545 $result3 = $dbxlink->query ($query);
546 if ($result3 == NULL or $result3->rowCount() != 1)
548 showError ('Error inserting into Atom in createMolecule()');
555 // History logger. This function assumes certain table naming convention and
557 // 1. History table name equals to dictionary table name plus 'History'.
558 // 2. History table must have the same row set (w/o keys) plus one row named
559 // 'ctime' of type 'timestamp'.
560 function recordHistory ($tableName, $whereClause)
562 global $dbxlink, $remote_username;
563 $query = "insert into ${tableName}History select *, current_timestamp(), '${remote_username}' from ${tableName} where ${whereClause}";
564 $result = $dbxlink->query ($query);
565 if ($result == NULL or $result->rowCount() != 1)
567 showError ("SQL query failed in recordHistory() for table ${tableName}");
573 function getRackspaceHistory ()
577 "select mo.id as mo_id, ro.id as ro_id, ro.name, mo.ctime, mo.comment, dict_value as objtype_name, user_name from " .
578 "MountOperation as mo inner join RackObject as ro on mo.object_id = ro.id " .
579 "inner join Dictionary on objtype_id = dict_key natural join Chapter " .
580 "where chapter_name = 'RackObjectType' order by ctime desc";
581 $result = $dbxlink->query ($query);
584 showError ('SQL query failed in getRackspaceHistory()');
587 $ret = $result->fetchAll(PDO
::FETCH_ASSOC
);
588 $result->closeCursor();
592 // This function is used in renderRackspaceHistory()
593 function getOperationMolecules ($op_id = 0)
597 showError ("Missing argument to getOperationMolecules()");
601 $query = "select old_molecule_id, new_molecule_id from MountOperation where id = ${op_id}";
602 $result = $dbxlink->query ($query);
605 showError ("SQL query failed in getOperationMolecules()");
608 // We expect one row.
609 $row = $result->fetch (PDO
::FETCH_ASSOC
);
612 showError ("SQL query succeded, but returned no results in getOperationMolecules().");
615 $omid = $row['old_molecule_id'];
616 $nmid = $row['new_molecule_id'];
617 $result->closeCursor();
618 return array ($omid, $nmid);
621 function getResidentRacksData ($object_id = 0)
625 showError ('Invalid object_id in getResidentRacksData()');
628 $query = "select distinct rack_id from RackSpace where object_id = ${object_id} order by rack_id";
630 $result = $dbxlink->query ($query);
633 showError ("SQL query failed in getResidentRacksData()");
636 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
637 $result->closeCursor();
639 foreach ($rows as $row)
641 $rackData = getRackData ($row[0]);
642 if ($rackData == NULL)
644 showError ('getRackData() failed in getResidentRacksData()');
647 $ret[$row[0]] = $rackData;
649 $result->closeCursor();
653 function getObjectGroupInfo ($group_id = 0)
656 'select dict_key as id, dict_value as name, count(id) as count from ' .
657 'Dictionary natural join Chapter left join RackObject on dict_key = objtype_id ' .
658 'where chapter_name = "RackObjectType" ' .
659 (($group_id > 0) ?
"and dict_key = ${group_id} " : '') .
662 $result = $dbxlink->query ($query);
665 showError ('SQL query failed in getObjectGroupSummary');
669 $clist = array ('id', 'name', 'count');
670 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
671 foreach ($clist as $dummy => $cname)
672 $ret[$row['id']][$cname] = $row[$cname];
673 $result->closeCursor();
675 return current ($ret);
680 // This function returns objects, which have no rackspace assigned to them.
681 // Additionally it keeps rack_id parameter, so we can silently pre-select
682 // the rack required.
683 function getUnmountedObjects ()
687 'select dict_value as objtype_name, dict_key as objtype_id, name, label, barcode, id, asset_no from ' .
688 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter ' .
689 'left join RackSpace on id = object_id '.
690 'where rack_id is null and chapter_name = "RackObjectType" order by dict_value, name, label, asset_no, barcode';
691 $result = $dbxlink->query ($query);
694 showError ('SQL query failure in getUnmountedObjects()');
698 $clist = array ('id', 'name', 'label', 'barcode', 'objtype_name', 'objtype_id', 'asset_no');
699 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
701 foreach ($clist as $dummy => $cname)
702 $ret[$row['id']][$cname] = $row[$cname];
703 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
705 $result->closeCursor();
709 function getProblematicObjects ()
713 'select dict_value as objtype_name, dict_key as objtype_id, name, id, asset_no from ' .
714 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter '.
715 'where has_problems = "yes" and chapter_name = "RackObjectType" order by objtype_name, name';
716 $result = $dbxlink->query ($query);
719 showError ('SQL query failure in getProblematicObjects()');
723 $clist = array ('id', 'name', 'objtype_name', 'objtype_id', 'asset_no');
724 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
726 foreach ($clist as $dummy => $cname)
727 $ret[$row['id']][$cname] = $row[$cname];
728 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
730 $result->closeCursor();
734 function commitAddPort ($object_id = 0, $port_name, $port_type_id, $port_label, $port_l2address)
738 showError ('Invalid object_id in commitAddPort()');
741 $port_l2address = l2addressForDatabase ($port_l2address);
742 $result = useInsertBlade
747 'name' => "'${port_name}'",
748 'object_id' => "'${object_id}'",
749 'label' => "'${port_label}'",
750 'type' => "'${port_type_id}'",
751 'l2address' => "${port_l2address}"
757 return 'SQL query failed';
760 function commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_reservation_comment)
763 $port_l2address = l2addressForDatabase ($port_l2address);
765 "update Port set name='$port_name', label='$port_label', " .
766 "reservation_comment = ${port_reservation_comment}, l2address=${port_l2address} " .
767 "where id='$port_id'";
768 $result = $dbxlink->exec ($query);
771 $errorInfo = $dbxlink->errorInfo();
772 // We could update nothing.
773 if ($errorInfo[0] == '00000')
775 return $errorInfo[2];
778 function delObjectPort ($port_id)
780 if (unlinkPort ($port_id) != '')
781 return 'unlinkPort() failed in delObjectPort()';
782 if (useDeleteBlade ('Port', 'id', $port_id) != TRUE)
783 return 'useDeleteBlade() failed in delObjectPort()';
787 function getObjectAddressesAndNames ()
791 "select object_id as object_id, ".
792 "RackObject.name as object_name, ".
793 "IPBonds.name as name, ".
794 "INET_NTOA(ip) as ip ".
795 "from IPBonds join RackObject on id=object_id ";
796 $result = $dbxlink->query ($query);
799 showError ("SQL query failure in getObjectAddressesAndNames()");
804 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
806 $ret[$count]['object_id']=$row['object_id'];
807 $ret[$count]['object_name']=$row['object_name'];
808 $ret[$count]['name']=$row['name'];
809 $ret[$count]['ip']=$row['ip'];
812 $result->closeCursor();
817 function getEmptyPortsOfType ($type_id)
821 "select distinct Port.id as Port_id, ".
822 "Port.object_id as Port_object_id, ".
823 "RackObject.name as Object_name, ".
824 "Port.name as Port_name, ".
825 "Port.type as Port_type_id, ".
826 "dict_value as Port_type_name ".
829 " Port inner join Dictionary on Port.type = dict_key natural join Chapter ".
831 " join RackObject on Port.object_id = RackObject.id ".
833 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
834 "inner join PortCompat on Port.type = PortCompat.type2 ".
835 "where chapter_name = 'PortType' and PortCompat.type1 = '$type_id' and Link.porta is NULL ".
836 "and Port.reservation_comment is null order by Object_name, Port_name";
837 $result = $dbxlink->query ($query);
840 showError ("SQL query failure in getEmptyPortsOfType($type_id)");
845 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
847 $ret[$count]['Port_id']=$row['Port_id'];
848 $ret[$count]['Port_object_id']=$row['Port_object_id'];
849 $ret[$count]['Object_name']=$row['Object_name'];
850 $ret[$count]['Port_name']=$row['Port_name'];
851 $ret[$count]['Port_type_id']=$row['Port_type_id'];
852 $ret[$count]['Port_type_name']=$row['Port_type_name'];
855 $result->closeCursor();
859 function linkPorts ($porta, $portb)
861 if ($porta == $portb)
862 return "Ports can't be the same";
870 $query1 = "insert into Link set porta='${porta}', portb='{$portb}'";
871 $query2 = "update Port set reservation_comment = NULL where id = ${porta} or id = ${portb} limit 2";
872 // FIXME: who cares about the return value?
873 $result = $dbxlink->exec ($query1);
874 $result = $dbxlink->exec ($query2);
879 function unlinkPort ($port)
883 "delete from Link where porta='$port' or portb='$port'";
884 $result = $dbxlink->exec ($query);
889 // FIXME: after falling back to using existing getObjectInfo we don't
890 // need that large query. Shrink it some later.
891 function getObjectAddresses ($object_id = 0)
895 showError ('Invalid object_id in getObjectAddresses()');
901 "IPAddress.name as IPAddress_name, ".
902 "IPAddress.reserved as IPAddress_reserved, ".
903 "IPBonds.name as IPBonds_name, ".
904 "INET_NTOA(IPBonds.ip) as IPBonds_ip, ".
905 "IPBonds.type as IPBonds_type, ".
906 "RemoteBonds.name as RemoteBonds_name, ".
907 "RemoteBonds.type as RemoteBonds_type, ".
908 "RemoteBonds.object_id as RemoteBonds_object_id, ".
909 "RemoteObject.name as RemoteObject_name from IPBonds " .
910 "left join IPBonds as RemoteBonds on IPBonds.ip=RemoteBonds.ip " .
911 "and IPBonds.object_id!=RemoteBonds.object_id " .
912 "left join IPAddress on IPBonds.ip=IPAddress.ip " .
913 "left join RackObject as RemoteObject on RemoteBonds.object_id=RemoteObject.id ".
915 "IPBonds.object_id = ${object_id} ".
916 "order by IPBonds.ip, RemoteObject.name";
917 $result = $dbxlink->query ($query);
920 showError ("SQL query failed in getObjectAddresses()");
929 // We are going to call getObjectInfo() for some rows,
930 // hence the connector must be unloaded from the
932 $rows = $result->fetchAll (PDO
::FETCH_ASSOC
);
933 $result->closeCursor();
934 foreach ($rows as $row)
936 if ($prev_ip != $row['IPBonds_ip'])
940 $prev_ip = $row['IPBonds_ip'];
941 $ret[$count]['address_name'] = $row['IPAddress_name'];
942 $ret[$count]['address_reserved'] = $row['IPAddress_reserved'];
943 $ret[$count]['ip'] = $row['IPBonds_ip'];
944 $ret[$count]['name'] = $row['IPBonds_name'];
945 $ret[$count]['type'] = $row['IPBonds_type'];
946 $ret[$count]['references'] = array();
949 if ($row['RemoteBonds_type'])
951 $ret[$count]['references'][$refcount]['type'] = $row['RemoteBonds_type'];
952 $ret[$count]['references'][$refcount]['name'] = $row['RemoteBonds_name'];
953 $ret[$count]['references'][$refcount]['object_id'] = $row['RemoteBonds_object_id'];
954 if (empty ($row['RemoteBonds_object_id']))
955 $ret[$count]['references'][$refcount]['object_name'] = $row['RemoteObject_name'];
958 $oi = getObjectInfo ($row['RemoteBonds_object_id']);
959 $ret[$count]['references'][$refcount]['object_name'] = displayedName ($oi);
968 function getAddressspaceList ()
973 "id as IPRanges_id, ".
974 "INET_NTOA(ip) as IPRanges_ip, ".
975 "mask as IPRanges_mask, ".
976 "name as IPRanges_name ".
979 $result = $dbxlink->query ($query);
988 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
990 $ret[$count]['id'] = $row['IPRanges_id'];
991 $ret[$count]['ip'] = $row['IPRanges_ip'];
992 $ret[$count]['ip_bin'] = ip2long($row['IPRanges_ip']);
993 $ret[$count]['name'] = $row['IPRanges_name'];
994 $ret[$count]['mask'] = $row['IPRanges_mask'];
995 $ret[$count]['mask_bin'] = binMaskFromDec($row['IPRanges_mask']);
996 $ret[$count]['mask_bin_inv'] = binInvMaskFromDec($row['IPRanges_mask']);
1000 $result->closeCursor();
1005 function getRangeByIp ($ip = '', $id = 0)
1011 "id, INET_NTOA(ip) as ip, mask, name ".
1016 "id, INET_NTOA(ip) as ip, mask, name ".
1017 "from IPRanges where id='$id'";
1019 $result = $dbxlink->query ($query);
1020 if ($result == NULL)
1027 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1029 $binmask=binMaskFromDec($row['mask']);
1030 if ((ip2long($ip) & $binmask) == ip2long($row['ip']))
1032 $ret['id'] = $row['id'];
1033 $ret['ip'] = $row['ip'];
1034 $ret['ip_bin'] = ip2long($row['ip']);
1035 $ret['name'] = $row['name'];
1036 $ret['mask'] = $row['mask'];
1037 $result->closeCursor();
1042 $result->closeCursor();
1046 function updateRange ($id=0, $name='')
1050 "update IPRanges set name='$name' where id='$id'";
1051 $result = $dbxlink->exec ($query);
1056 // This function is actually used not only to update, but also to create records,
1057 // that's why ON DUPLICATE KEY UPDATE was replaced by DELETE-INSERT pair
1058 // (MySQL 4.0 workaround).
1059 function updateAddress ($ip=0, $name='', $reserved='no')
1061 // DELETE may safely fail.
1062 $r = useDeleteBlade ('IPAddress', 'ip', "INET_ATON('${ip}')", FALSE);
1063 // INSERT may appear not necessary.
1064 if ($name == '' and $reserved == 'no')
1066 if (useInsertBlade ('IPAddress', array ('name' => "'${name}'", 'reserved' => "'${reserved}'", 'ip' => "INET_ATON('${ip}')")))
1069 return 'useInsertBlade() failed in updateAddress()';
1072 // FIXME: This function doesn't wipe relevant records from IPAddress table.
1073 function commitDeleteRange ($id = 0)
1076 return 'Invalid range ID in commitDeleteRange()';
1077 if (useDeleteBlade ('IPRanges', 'id', $id))
1080 return 'SQL query failed in commitDeleteRange';
1083 function updateBond ($ip='', $object_id=0, $name='', $type='')
1088 "update IPBonds set name='$name', type='$type' where ip=INET_ATON('$ip') and object_id='$object_id'";
1089 $result = $dbxlink->exec ($query);
1093 function unbindIpFromObject ($ip='', $object_id=0)
1098 "delete from IPBonds where ip=INET_ATON('$ip') and object_id='$object_id'";
1099 $result = $dbxlink->exec ($query);
1103 // This function returns either all or one user account. Array key is user name.
1104 function getUserAccounts ()
1108 'select user_id, user_name, user_password_hash, user_realname, user_enabled ' .
1109 'from UserAccount order by user_id';
1110 $result = $dbxlink->query ($query);
1111 if ($result == NULL)
1113 showError ('SQL query failed in getUserAccounts()');
1117 $clist = array ('user_id', 'user_name', 'user_realname', 'user_password_hash', 'user_enabled');
1118 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1119 foreach ($clist as $dummy => $cname)
1120 $ret[$row['user_name']][$cname] = $row[$cname];
1121 $result->closeCursor();
1125 // This function returns permission array for all user accounts. Array key is user name.
1126 function getUserPermissions ()
1130 "select UserPermission.user_id, user_name, page, tab, access from " .
1131 "UserPermission natural left join UserAccount where (user_name is not null) or " .
1132 "(user_name is null and UserPermission.user_id = 0) order by user_id, page, tab";
1133 $result = $dbxlink->query ($query);
1134 if ($result == NULL)
1136 showError ('SQL query failed in getUserPermissions()');
1140 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1142 if ($row['user_id'] == 0)
1143 $row['user_name'] = '%';
1144 $ret[$row['user_name']][$row['page']][$row['tab']] = $row['access'];
1146 $result->closeCursor();
1150 function searchByl2address ($l2addr)
1153 $l2addr = l2addressForDatabase ($l2addr);
1154 $query = "select object_id, Port.id as port_id from RackObject as ro inner join Port on ro.id = Port.object_id " .
1155 "where l2address = ${l2addr}";
1156 $result = $dbxlink->query ($query);
1157 if ($result == NULL)
1159 showError ('SQL query failed in objectIDbyl2address()');
1162 $rows = $result->fetchAll (PDO
::FETCH_ASSOC
);
1163 $result->closeCursor();
1164 if (count ($rows) == 0) // No results.
1166 if (count ($rows) == 1) // Target found.
1168 showError ('More than one results found in objectIDbyl2address(). This is probably a broken unique key.');
1172 // This function returns either port ID or NULL for specified arguments.
1173 function getPortID ($object_id, $port_name)
1176 $query = "select id from Port where object_id=${object_id} and name='${port_name}' limit 2";
1177 $result = $dbxlink->query ($query);
1178 if ($result == NULL)
1180 showError ('SQL query failed in getPortID()');
1183 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
1184 if (count ($rows) != 1)
1187 $result->closeCursor();
1191 function commitCreateUserAccount ($username, $realname, $password)
1193 return useInsertBlade
1198 'user_name' => "'${username}'",
1199 'user_realname' => "'${realname}'",
1200 'user_password_hash' => "'${password}'"
1205 function commitUpdateUserAccount ($id, $new_username, $new_realname, $new_password)
1209 "update UserAccount set user_name = '${new_username}', user_realname = '${new_realname}', " .
1210 "user_password_hash = '${new_password}' where user_id = ${id} limit 1";
1211 $result = $dbxlink->query ($query);
1212 if ($result == NULL)
1214 showError ('SQL query failed in commitUpdateUserAccount()');
1220 function commitEnableUserAccount ($id, $new_enabled_value)
1224 "update UserAccount set user_enabled = '${new_enabled_value}' " .
1225 "where user_id = ${id} limit 1";
1226 $result = $dbxlink->query ($query);
1227 if ($result == NULL)
1229 showError ('SQL query failed in commitEnableUserAccount()');
1235 function commitGrantPermission ($userid, $page, $tab, $value)
1237 return useInsertBlade
1242 'user_id' => $userid,
1243 'page' => "'${page}'",
1244 'tab' => "'${tab}'",
1245 'access' => "'${value}'"
1250 function commitRevokePermission ($userid, $page, $tab)
1254 "delete from UserPermission where user_id = '${userid}' and page = '${page}' " .
1255 "and tab = '$tab' limit 1";
1256 $result = $dbxlink->query ($query);
1257 if ($result == NULL)
1259 showError ('SQL query failed in commitRevokePermission()');
1265 // This function returns an array of all port type pairs from PortCompat table.
1266 function getPortCompat ()
1270 "select type1, type2, d1.dict_value as type1name, d2.dict_value as type2name from " .
1271 "PortCompat as pc inner join Dictionary as d1 on pc.type1 = d1.dict_key " .
1272 "inner join Dictionary as d2 on pc.type2 = d2.dict_key " .
1273 "inner join Chapter as c1 on d1.chapter_no = c1.chapter_no " .
1274 "inner join Chapter as c2 on d2.chapter_no = c2.chapter_no " .
1275 "where c1.chapter_name = 'PortType' and c2.chapter_name = 'PortType'";
1276 $result = $dbxlink->query ($query);
1277 if ($result == NULL)
1279 showError ('SQL query failed in getPortCompat()');
1282 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
1283 $result->closeCursor();
1287 function removePortCompat ($type1 = 0, $type2 = 0)
1290 if ($type1 == 0 or $type2 == 0)
1292 showError ('Invalid arguments to removePortCompat');
1295 $query = "delete from PortCompat where type1 = ${type1} and type2 = ${type2} limit 1";
1296 $result = $dbxlink->query ($query);
1297 if ($result == NULL)
1299 showError ('SQL query failed in removePortCompat()');
1305 function addPortCompat ($type1 = 0, $type2 = 0)
1307 if ($type1 <= 0 or $type2 <= 0)
1309 showError ('Invalid arguments to addPortCompat');
1312 return useInsertBlade
1315 array ('type1' => $type1, 'type2' => $type2)
1319 // This function returns the dictionary as an array of trees, each tree
1320 // representing a single chapter. Each element has 'id', 'name', 'sticky'
1321 // and 'word' keys with the latter holding all the words within the chapter.
1326 "select chapter_name, Chapter.chapter_no, dict_key, dict_value, sticky from " .
1327 "Chapter natural left join Dictionary order by chapter_name, dict_value";
1328 $result = $dbxlink->query ($query);
1329 if ($result == NULL)
1331 showError ('SQL query failed in getDict()');
1335 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1337 $chapter_no = $row['chapter_no'];
1338 if (!isset ($dict[$chapter_no]))
1340 $dict[$chapter_no]['no'] = $chapter_no;
1341 $dict[$chapter_no]['name'] = $row['chapter_name'];
1342 $dict[$chapter_no]['sticky'] = $row['sticky'] == 'yes' ?
TRUE : FALSE;
1343 $dict[$chapter_no]['word'] = array();
1345 if ($row['dict_key'] != NULL)
1346 $dict[$chapter_no]['word'][$row['dict_key']] = $row['dict_value'];
1348 $result->closeCursor();
1352 function getDictStats ()
1354 $stock_chapters = array (1, 2, 3, 11, 12, 13, 14, 16, 17, 18, 19, 20);
1357 "select Chapter.chapter_no, chapter_name, count(dict_key) as wc from " .
1358 "Chapter natural left join Dictionary group by Chapter.chapter_no";
1359 $result1 = $dbxlink->query ($query);
1360 if ($result1 == NULL)
1362 showError ('SQL query #1 failed in getDictStats()');
1365 $tc = $tw = $uc = $uw = 0;
1366 while ($row = $result1->fetch (PDO
::FETCH_ASSOC
))
1370 if (in_array ($row['chapter_no'], $stock_chapters))
1375 $result1->closeCursor();
1376 $query = "select count(attr_id) as attrc from RackObject as ro left join " .
1377 "AttributeValue as av on ro.id = av.object_id group by ro.id";
1378 $result2 = $dbxlink->query ($query);
1379 if ($result2 == NULL)
1381 showError ('SQL query #2 failed in getDictStats()');
1384 $to = $ta = $so = 0;
1385 while ($row = $result2->fetch (PDO
::FETCH_ASSOC
))
1388 if ($row['attrc'] != 0)
1391 $ta +
= $row['attrc'];
1394 $result2->closeCursor();
1396 $ret['Total chapters in dictionary'] = $tc;
1397 $ret['Total words in dictionary'] = $tw;
1398 $ret['User chapters'] = $uc;
1399 $ret['Words in user chapters'] = $uw;
1400 $ret['Total objects'] = $to;
1401 $ret['Objects with stickers'] = $so;
1402 $ret['Total stickers attached'] = $ta;
1406 function commitUpdateDictionary ($chapter_no = 0, $dict_key = 0, $dict_value = '')
1408 if ($chapter_no <= 0 or $dict_key <= 0 or empty ($dict_value))
1410 showError ('Invalid args to commitUpdateDictionary()');
1415 "update Dictionary set dict_value = '${dict_value}' where chapter_no=${chapter_no} " .
1416 "and dict_key=${dict_key} limit 1";
1417 $result = $dbxlink->query ($query);
1418 if ($result == NULL)
1420 showError ('SQL query failed in commitUpdateDictionary()');
1426 function commitSupplementDictionary ($chapter_no = 0, $dict_value = '')
1428 if ($chapter_no <= 0 or empty ($dict_value))
1430 showError ('Invalid args to commitSupplementDictionary()');
1433 return useInsertBlade
1436 array ('chapter_no' => $chapter_no, 'dict_value' => "'${dict_value}'")
1440 function commitReduceDictionary ($chapter_no = 0, $dict_key = 0)
1442 if ($chapter_no <= 0 or $dict_key <= 0)
1444 showError ('Invalid args to commitReduceDictionary()');
1449 "delete from Dictionary where chapter_no=${chapter_no} " .
1450 "and dict_key=${dict_key} limit 1";
1451 $result = $dbxlink->query ($query);
1452 if ($result == NULL)
1454 showError ('SQL query failed in commitReduceDictionary()');
1460 function commitAddChapter ($chapter_name = '')
1462 if (empty ($chapter_name))
1464 showError ('Invalid args to commitAddChapter()');
1467 return useInsertBlade
1470 array ('chapter_name' => "'${chapter_name}'")
1474 function commitUpdateChapter ($chapter_no = 0, $chapter_name = '')
1476 if ($chapter_no <= 0 or empty ($chapter_name))
1478 showError ('Invalid args to commitUpdateChapter()');
1483 "update Chapter set chapter_name = '${chapter_name}' where chapter_no = ${chapter_no} " .
1484 "and sticky = 'no' limit 1";
1485 $result = $dbxlink->query ($query);
1486 if ($result == NULL)
1488 showError ('SQL query failed in commitUpdateChapter()');
1494 function commitDeleteChapter ($chapter_no = 0)
1496 if ($chapter_no <= 0)
1498 showError ('Invalid args to commitDeleteChapter()');
1503 "delete from Chapter where chapter_no = ${chapter_no} and sticky = 'no' limit 1";
1504 $result = $dbxlink->query ($query);
1505 if ($result == NULL)
1507 showError ('SQL query failed in commitDeleteChapter()');
1513 // This is a dictionary accessor.
1514 function readChapter ($chapter_name = '')
1516 if (empty ($chapter_name))
1518 showError ('invalid argument to readChapter()');
1523 "select dict_key, dict_value from Dictionary natural join Chapter " .
1524 "where chapter_name = '${chapter_name}' order by dict_value";
1525 $result = $dbxlink->query ($query);
1526 if ($result == NULL)
1528 $errorInfo = $dbxlink->errorInfo();
1529 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed in readChapter('${chapter_name}')");
1533 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1535 $result->closeCursor();
1539 function getAttrMap ()
1543 "select a.attr_id, a.attr_type, a.attr_name, am.objtype_id, " .
1544 "d.dict_value as objtype_name, am.chapter_no, c2.chapter_name from " .
1545 "Attribute as a natural left join AttributeMap as am " .
1546 "left join Dictionary as d on am.objtype_id = d.dict_key " .
1547 "left join Chapter as c1 on d.chapter_no = c1.chapter_no " .
1548 "left join Chapter as c2 on am.chapter_no = c2.chapter_no " .
1549 "where c1.chapter_name = 'RackObjectType' or c1.chapter_name is null " .
1550 "order by attr_name";
1551 $result = $dbxlink->query ($query);
1552 if ($result == NULL)
1554 $errorInfo = $dbxlink->errorInfo();
1555 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed in getAttrMap()");
1559 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1561 $attr_id = $row['attr_id'];
1562 if (!isset ($ret[$attr_id]))
1564 $ret[$attr_id]['id'] = $attr_id;
1565 $ret[$attr_id]['type'] = $row['attr_type'];
1566 $ret[$attr_id]['name'] = $row['attr_name'];
1567 $ret[$attr_id]['application'] = array();
1569 if ($row['objtype_id'] == '')
1571 $application['objtype_id'] = $row['objtype_id'];
1572 $application['objtype_name'] = $row['objtype_name'];
1573 if ($row['attr_type'] == 'dict')
1575 $application['chapter_no'] = $row['chapter_no'];
1576 $application['chapter_name'] = $row['chapter_name'];
1578 $ret[$attr_id]['application'][] = $application;
1580 $result->closeCursor();
1584 function commitUpdateAttribute ($attr_id = 0, $attr_name = '')
1586 if ($attr_id <= 0 or empty ($attr_name))
1588 showError ('Invalid args to commitUpdateAttribute()');
1593 "update Attribute set attr_name = '${attr_name}' " .
1594 "where attr_id = ${attr_id} limit 1";
1595 $result = $dbxlink->query ($query);
1596 if ($result == NULL)
1598 showError ("SQL query '${query}' failed in commitUpdateAttribute()");
1604 function commitAddAttribute ($attr_name = '', $attr_type = '')
1606 if (empty ($attr_name))
1608 showError ('Invalid args to commitAddAttribute()');
1619 showError ('Invalid args to commitAddAttribute()');
1622 return useInsertBlade
1625 array ('attr_name' => "'${attr_name}'", 'attr_type' => "'${attr_type}'")
1629 function commitDeleteAttribute ($attr_id = 0)
1633 showError ('Invalid args to commitDeleteAttribute()');
1636 return useDeleteBlade ('Attribute', 'attr_id', $attr_id);
1639 // FIXME: don't store garbage in chapter_no for non-dictionary types.
1640 function commitSupplementAttrMap ($attr_id = 0, $objtype_id = 0, $chapter_no = 0)
1642 if ($attr_id <= 0 or $objtype_id <= 0 or $chapter_no <= 0)
1644 showError ('Invalid args to commitSupplementAttrMap()');
1647 return useInsertBlade
1652 'attr_id' => $attr_id,
1653 'objtype_id' => $objtype_id,
1654 'chapter_no' => $chapter_no
1659 function commitReduceAttrMap ($attr_id = 0, $objtype_id)
1661 if ($attr_id <= 0 or $objtype_id <= 0)
1663 showError ('Invalid args to commitReduceAttrMap()');
1668 "delete from AttributeMap where attr_id=${attr_id} " .
1669 "and objtype_id=${objtype_id} limit 1";
1670 $result = $dbxlink->query ($query);
1671 if ($result == NULL)
1673 showError ('SQL query failed in commitReduceAttrMap()');
1679 // This function returns all optional attributes for requested object
1680 // as an array of records. NULL is returned on error and empty array
1681 // is returned, if there are no attributes found.
1682 function getAttrValues ($object_id)
1684 if ($object_id <= 0)
1686 showError ('Invalid argument to getAttrValues()');
1692 "select A.attr_id, A.attr_name, A.attr_type, C.chapter_name, " .
1693 "AV.uint_value, AV.float_value, AV.string_value, D.dict_value from " .
1694 "RackObject as RO inner join AttributeMap as AM on RO.objtype_id = AM.objtype_id " .
1695 "inner join Attribute as A using (attr_id) " .
1696 "left join AttributeValue as AV on AV.attr_id = AM.attr_id and AV.object_id = RO.id " .
1697 "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_no = D.chapter_no " .
1698 "left join Chapter as C on AM.chapter_no = C.chapter_no " .
1699 "where RO.id = ${object_id} order by A.attr_type";
1700 $result = $dbxlink->query ($query);
1701 if ($result == NULL)
1703 $errorInfo = $dbxlink->errorInfo();
1704 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed in getAttrValues()");
1707 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1710 $record['id'] = $row['attr_id'];
1711 $record['name'] = $row['attr_name'];
1712 $record['type'] = $row['attr_type'];
1713 switch ($row['attr_type'])
1719 $record['value'] = $row[$row['attr_type'] . '_value'];
1720 $record['chapter_name'] = $row['chapter_name'];
1721 $record['key'] = $row['uint_value'];
1724 $record['value'] = NULL;
1727 $ret[$row['attr_id']] = $record;
1729 $result->closeCursor();
1733 function commitResetAttrValue ($object_id = 0, $attr_id = 0)
1735 if ($object_id <= 0 or $attr_id <= 0)
1737 showError ('Invalid arguments to commitResetAttrValue()');
1741 $query = "delete from AttributeValue where object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1742 $result = $dbxlink->query ($query);
1743 if ($result == NULL)
1745 showError ('SQL query failed in commitResetAttrValue()');
1751 // FIXME: don't share common code with use commitResetAttrValue()
1752 function commitUpdateAttrValue ($object_id = 0, $attr_id = 0, $value = '')
1754 if ($object_id <= 0 or $attr_id <= 0)
1756 showError ('Invalid arguments to commitUpdateAttrValue()');
1760 return commitResetAttrValue ($object_id, $attr_id);
1762 $query1 = "select attr_type from Attribute where attr_id = ${attr_id}";
1763 $result = $dbxlink->query ($query1);
1764 if ($result == NULL)
1766 showError ('SQL query #1 failed in commitUpdateAttrValue()');
1769 $row = $result->fetch (PDO
::FETCH_NUM
);
1772 showError ('SQL query #1 returned no results in commitUpdateAttrValue()');
1775 $attr_type = $row[0];
1776 $result->closeCursor();
1782 $column = $attr_type . '_value';
1785 $column = 'uint_value';
1788 showError ("Unknown attribute type '${attr_type}' met in commitUpdateAttrValue()");
1792 "delete from AttributeValue where " .
1793 "object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1794 $result = $dbxlink->query ($query2);
1795 if ($result == NULL)
1797 showError ('SQL query #2 failed in commitUpdateAttrValue()');
1800 // We know $value isn't empty here.
1802 "insert into AttributeValue set ${column} = '${value}', " .
1803 "object_id = ${object_id}, attr_id = ${attr_id} ";
1804 $result = $dbxlink->query ($query3);
1805 if ($result == NULL)
1807 showError ('SQL query #3 failed in commitUpdateAttrValue()');
1813 function commitUseupPort ($port_id = 0)
1817 showError ("Invalid argument to commitUseupPort()");
1821 $query = "update Port set reservation_comment = NULL where id = ${port_id} limit 1";
1822 $result = $dbxlink->exec ($query);
1823 if ($result == NULL)
1825 showError ("SQL query failed in commitUseupPort()");
1832 // This is a swiss-knife blade to insert a record into a table.
1833 // The first argument is table name.
1834 // The second argument is an array of "name" => "value" pairs.
1835 // The function returns either TRUE or FALSE (we expect one row
1837 function useInsertBlade ($tablename, $values)
1840 $namelist = $valuelist = '';
1841 foreach ($values as $name => $value)
1843 $namelist = $namelist . ($namelist == '' ?
"(${name}" : ", ${name}");
1844 $valuelist = $valuelist . ($valuelist == '' ?
"(${value}" : ", ${value}");
1846 $query = "insert into ${tablename} ${namelist}) values ${valuelist})";
1847 $result = $dbxlink->exec ($query);
1853 // This swiss-knife blade deletes one record from the specified table
1854 // using the specified key name and value.
1855 function useDeleteBlade ($tablename, $keyname, $keyvalue, $quotekey = TRUE)
1858 if ($quotekey == TRUE)
1859 $query = "delete from ${tablename} where ${keyname}='$keyvalue' limit 1";
1861 $query = "delete from ${tablename} where ${keyname}=$keyvalue limit 1";
1862 $result = $dbxlink->exec ($query);
1863 if ($result === NULL)
1865 elseif ($result != 1)
1871 function loadConfigCache ()
1874 $query = 'SELECT varname, varvalue, vartype, is_hidden, emptyok, description FROM Config ORDER BY varname';
1875 $result = $dbxlink->query ($query);
1876 if ($result == NULL)
1878 $errorInfo = $dbxlink->errorInfo();
1879 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed in loadConfigCache()");
1883 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1884 $cache[$row['varname']] = $row;
1885 $result->closeCursor();
1889 // setConfigVar() is expected to perform all necessary filtering
1890 function storeConfigVar ($varname = NULL, $varvalue = NULL)
1893 if (empty ($varname) ||
$varvalue === NULL)
1895 showError ('Invalid arguments to storeConfigVar()');
1898 $query = "update Config set varvalue='${varvalue}' where varname='${varname}' limit 1";
1899 $result = $dbxlink->query ($query);
1900 if ($result == NULL)
1902 showError ("SQL query '${query}' failed in storeConfigVar()");
1905 $rc = $result->rowCount();
1906 $result->closeCursor();
1907 if ($rc == 0 or $rc == 1)
1909 showError ("Something went wrong in storeConfigVar('${varname}', '${varvalue}')");
1913 // Database version detector. Should behave corretly on any
1914 // working dataset a user might have.
1915 function getDatabaseVersion ()
1918 $query = "select varvalue from Config where varname = 'DB_VERSION' and vartype = 'string'";
1919 $result = $dbxlink->query ($query);
1920 if ($result == NULL)
1922 $errorInfo = $dbxlink->errorInfo();
1923 if ($errorInfo[0] == '42S02') // ER_NO_SUCH_TABLE
1925 die ('SQL query #1 failed in getDatabaseVersion() with error ' . $errorInfo[2]);
1927 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
1928 if (count ($rows) != 1 ||
empty ($rows[0][0]))
1930 $result->closeCursor();
1931 die ('Cannot guess database version. Config table is present, but DB_VERSION is missing or invalid. Giving up.');
1934 $result->closeCursor();