4caf0385d2271016798f4a0cd128ee6fc7189add
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', __FUNCTION__
);
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', __FUNCTION__
);
70 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
83 $ret[$row['id']][$cname] = $row[$cname];
84 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
86 $result->closeCursor();
90 function getRacksForRow ($row_id = 0)
94 "select Rack.id, Rack.name, height, Rack.comment, row_id, " .
95 "'yes' as left_is_front, 'yes' as bottom_is_unit1, dict_value as row_name " .
96 "from Rack left join Dictionary on row_id = dict_key natural join Chapter " .
97 "where chapter_name = 'RackRow' and Rack.deleted = 'no' " .
98 (($row_id == 0) ?
"" : "and row_id = ${row_id} ") .
99 "order by row_name, Rack.id";
100 $result = $dbxlink->query ($query);
103 showError ('SQL query failed', __FUNCTION__
);
118 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
119 foreach ($clist as $cname)
120 $ret[$row['id']][$cname] = $row[$cname];
121 $result->closeCursor();
122 usort ($ret, 'sortRacks');
123 $ret = restoreRackIDs ($ret);
127 // This is a popular helper for getting information about
128 // a particular rack and its rackspace at once.
129 function getRackData ($rack_id = 0, $silent = FALSE)
133 if ($silent == FALSE)
134 showError ('Invalid rack_id', __FUNCTION__
);
139 "select Rack.id, Rack.name, row_id, height, Rack.comment, " .
140 "'yes' as left_is_front, 'yes' as bottom_is_unit1, dict_value as row_name from " .
141 "Rack left join Dictionary on Rack.row_id = dict_key natural join Chapter " .
142 "where chapter_name = 'RackRow' and Rack.id='${rack_id}' and Rack.deleted = 'no' limit 1";
143 $result1 = $dbxlink->query ($query);
144 if ($result1 == NULL)
146 if ($silent == FALSE)
147 showError ("SQL query #1 failed", __FUNCTION__
);
150 if (($row = $result1->fetch (PDO
::FETCH_ASSOC
)) == NULL)
152 if ($silent == FALSE)
153 showError ('Query #1 succeded, but returned no data', __FUNCTION__
);
169 foreach ($clist as $cname)
170 $rack[$cname] = $row[$cname];
171 $result1->closeCursor();
173 // start with default rackspace
174 for ($i = $rack['height']; $i > 0; $i--)
175 for ($locidx = 0; $locidx < 3; $locidx++
)
176 $rack[$i][$locidx]['state'] = 'F';
180 "select unit_no, atom, state, object_id " .
181 "from RackSpace where rack_id = ${rack_id} and " .
182 "unit_no between 1 and " . $rack['height'] . " order by unit_no";
183 $result2 = $dbxlink->query ($query);
184 if ($result2 == NULL)
186 if ($silent == FALSE)
187 showError ('SQL query failure #2', __FUNCTION__
);
191 while ($row = $result2->fetch (PDO
::FETCH_ASSOC
))
193 $rack[$row['unit_no']][$loclist[$row['atom']]]['state'] = $row['state'];
194 $rack[$row['unit_no']][$loclist[$row['atom']]]['object_id'] = $row['object_id'];
196 $result2->closeCursor();
200 // This is a popular helper.
201 function getObjectInfo ($object_id = 0)
205 showError ('Invalid object_id', __FUNCTION__
);
210 "select id, name, label, barcode, dict_value as objtype_name, asset_no, dict_key as objtype_id, has_problems, comment from " .
211 "RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter " .
212 "where id = '${object_id}' and deleted = 'no' and chapter_name = 'RackObjectType' limit 1";
213 $result = $dbxlink->query ($query);
216 $ei = $dbxlink->errorInfo();
217 showError ("SQL query failed with error ${ei[1]} (${ei[2]})", __FUNCTION__
);
220 if (($row = $result->fetch (PDO
::FETCH_ASSOC
)) == NULL)
222 showError ('Query succeded, but returned no data', __FUNCTION__
);
227 $ret['id'] = $row['id'];
228 $ret['name'] = $row['name'];
229 $ret['label'] = $row['label'];
230 $ret['barcode'] = $row['barcode'];
231 $ret['objtype_name'] = $row['objtype_name'];
232 $ret['objtype_id'] = $row['objtype_id'];
233 $ret['has_problems'] = $row['has_problems'];
234 $ret['asset_no'] = $row['asset_no'];
235 $ret['dname'] = displayedName ($ret);
236 $ret['comment'] = $row['comment'];
238 $result->closeCursor();
242 function getPortTypes ()
244 return readChapter ('PortType');
247 function getObjectPortsAndLinks ($object_id = 0)
251 showError ('Invalid object_id', __FUNCTION__
);
256 "select Port.id as Port_id, ".
257 "Port.name as Port_name, ".
258 "Port.label as Port_label, ".
259 "Port.l2address as Port_l2address, ".
260 "Port.type as Port_type, ".
261 "Port.reservation_comment as Port_reservation_comment, " .
262 "dict_value as PortType_name, ".
263 "RemotePort.id as RemotePort_id, ".
264 "RemotePort.name as RemotePort_name, ".
265 "RemotePort.object_id as RemotePort_object_id, ".
266 "RackObject.name as RackObject_name ".
270 "Port inner join Dictionary on Port.type = dict_key natural join Chapter".
272 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
274 "left join Port as RemotePort on Link.portb=RemotePort.id or Link.porta=RemotePort.id ".
276 "left join RackObject on RemotePort.object_id=RackObject.id ".
277 "where chapter_name = 'PortType' and Port.object_id=${object_id} ".
278 "and (Port.id != RemotePort.id or RemotePort.id is null) ".
279 "order by Port_name";
280 $result = $dbxlink->query ($query);
289 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
291 $ret[$count]['id'] = $row['Port_id'];
292 $ret[$count]['name'] = $row['Port_name'];
293 $ret[$count]['l2address'] = l2addressFromDatabase ($row['Port_l2address']);
294 $ret[$count]['label'] = $row['Port_label'];
295 $ret[$count]['type_id'] = $row['Port_type'];
296 $ret[$count]['type'] = $row['PortType_name'];
297 $ret[$count]['reservation_comment'] = $row['Port_reservation_comment'];
298 $ret[$count]['remote_id'] = $row['RemotePort_id'];
299 $ret[$count]['remote_name'] = htmlentities ($row['RemotePort_name'], ENT_QUOTES
);
300 $ret[$count]['remote_object_id'] = $row['RemotePort_object_id'];
301 $ret[$count]['remote_object_name'] = $row['RackObject_name'];
302 // Save on displayedName() calls.
303 if (empty ($row['RackObject_name']) and !empty ($row['RemotePort_object_id']))
305 $oi = getObjectInfo ($row['RemotePort_object_id']);
306 $ret[$count]['remote_object_name'] = displayedName ($oi);
311 $result->closeCursor();
315 function commitAddRack ($name, $height, $row_id, $comment)
318 $query = "insert into Rack(row_id, name, height, comment) values('${row_id}', '${name}', '${height}', '${comment}')";
319 $result1 = $dbxlink->query ($query);
320 if ($result1 == NULL)
322 showError ('SQL query failed', __FUNCTION__
);
325 // last_insert_id() is MySQL-specific
326 $query = 'select last_insert_id()';
327 $result2 = $dbxlink->query ($query);
328 if ($result2 == NULL)
330 showError ('Cannot get last ID', __FUNCTION__
);
333 // we always have a row
334 $row = $result2->fetch (PDO
::FETCH_NUM
);
335 $last_insert_id = $row[0];
336 $result2->closeCursor();
337 return recordHistory ('Rack', "id = ${last_insert_id}");
340 function commitAddObject ($new_name, $new_label, $new_barcode, $new_type_id, $new_asset_no)
343 // Maintain UNIQUE INDEX for common names and asset tags.
344 $new_asset_no = empty ($new_asset_no) ?
'NULL' : "'${new_asset_no}'";
345 $new_barcode = empty ($new_barcode) ?
'NULL' : "'${new_barcode}'";
346 $new_name = empty ($new_name) ?
'NULL' : "'${new_name}'";
348 "insert into RackObject(name, label, barcode, objtype_id, asset_no) " .
349 "values(${new_name}, '${new_label}', ${new_barcode}, '${new_type_id}', ${new_asset_no})";
350 $result1 = $dbxlink->query ($query);
351 if ($result1 == NULL)
353 $errorInfo = $dbxlink->errorInfo();
354 showError ("SQL query '${query}' failed: ${errorInfo[2]}", __FUNCTION__
);
357 if ($result1->rowCount() != 1)
359 showError ('Adding new object failed', __FUNCTION__
);
362 $query = 'select last_insert_id()';
363 $result2 = $dbxlink->query ($query);
364 if ($result2 == NULL)
366 $errorInfo = $dbxlink->errorInfo();
367 showError ("SQL query '${query}' failed: ${errorInfo[2]}", __FUNCTION__
);
370 // we always have a row
371 $row = $result2->fetch (PDO
::FETCH_NUM
);
372 $last_insert_id = $row[0];
373 $result2->closeCursor();
374 return recordHistory ('RackObject', "id = ${last_insert_id}");
377 function commitUpdateObject ($object_id = 0, $new_name = '', $new_label = '', $new_barcode = '', $new_type_id = 0, $new_has_problems = 'no', $new_asset_no = '', $new_comment = '')
379 if ($object_id == 0 ||
$new_type_id == 0)
381 showError ('Not all required args are present.', __FUNCTION__
);
385 $new_asset_no = empty ($new_asset_no) ?
'NULL' : "'${new_asset_no}'";
386 $new_barcode = empty ($new_barcode) ?
'NULL' : "'${new_barcode}'";
387 $new_name = empty ($new_name) ?
'NULL' : "'${new_name}'";
388 $query = "update RackObject set name=${new_name}, label='${new_label}', barcode=${new_barcode}, objtype_id='${new_type_id}', " .
389 "has_problems='${new_has_problems}', asset_no=${new_asset_no}, comment='${new_comment}' " .
390 "where id='${object_id}' limit 1";
391 $result = $dbxlink->query ($query);
394 showError ("SQL query '${query}' failed", __FUNCTION__
);
397 if ($result->rowCount() != 1)
399 showError ('Error updating object information', __FUNCTION__
);
402 $result->closeCursor();
403 return recordHistory ('RackObject', "id = ${object_id}");
406 function commitUpdateRack ($rack_id, $new_name, $new_height, $new_row_id, $new_comment)
408 if (empty ($rack_id) ||
empty ($new_name) ||
empty ($new_height))
410 showError ('Not all required args are present.', __FUNCTION__
);
414 $query = "update Rack set name='${new_name}', height='${new_height}', comment='${new_comment}', row_id=${new_row_id} " .
415 "where id='${rack_id}' limit 1";
416 $result1 = $dbxlink->query ($query);
417 if ($result1->rowCount() != 1)
419 showError ('Error updating rack information', __FUNCTION__
);
422 return recordHistory ('Rack', "id = ${rack_id}");
425 // This function accepts rack data returned by getRackData(), validates and applies changes
426 // supplied in $_REQUEST and returns resulting array. Only those changes are examined, which
427 // correspond to current rack ID.
428 // 1st arg is rackdata, 2nd arg is unchecked state, 3rd arg is checked state.
429 // If 4th arg is present, object_id fields will be updated accordingly to the new state.
430 // The function returns the modified rack upon success.
431 function processGridForm (&$rackData, $unchecked_state, $checked_state, $object_id = 0)
433 global $loclist, $dbxlink;
434 $rack_id = $rackData['id'];
435 $rack_name = $rackData['name'];
436 $rackchanged = FALSE;
437 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
439 for ($locidx = 0; $locidx < 3; $locidx++
)
441 if ($rackData[$unit_no][$locidx]['enabled'] != TRUE)
444 $state = $rackData[$unit_no][$locidx]['state'];
445 if (isset ($_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"]) and $_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"] == 'on')
446 $newstate = $checked_state;
448 $newstate = $unchecked_state;
449 if ($state == $newstate)
453 $atom = $loclist[$locidx];
454 // The only changes allowed are those introduced by checkbox grid.
457 !($state == $checked_state && $newstate == $unchecked_state) &&
458 !($state == $unchecked_state && $newstate == $checked_state)
460 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, 'atom ${atom}', cannot change state from '${state}' to '${newstate}'");
461 // Here we avoid using ON DUPLICATE KEY UPDATE by first performing DELETE
462 // anyway and then looking for probable need of INSERT.
464 "delete from RackSpace where rack_id = ${rack_id} and " .
465 "unit_no = ${unit_no} and atom = '${atom}' limit 1";
466 $r = $dbxlink->query ($query);
468 return array ('code' => 500, 'message' => "${rack_name}: SQL DELETE query failed in processGridForm()");
469 if ($newstate != 'F')
472 "insert into RackSpace(rack_id, unit_no, atom, state) " .
473 "values(${rack_id}, ${unit_no}, '${atom}', '${newstate}') ";
474 $r = $dbxlink->query ($query);
476 return array ('code' => 500, 'message' => "${rack_name}: SQL INSERT query failed in processGridForm()");
478 if ($newstate == 'T' and $object_id != 0)
480 // At this point we already have a record in RackSpace.
482 "update RackSpace set object_id=${object_id} " .
483 "where rack_id=${rack_id} and unit_no=${unit_no} and atom='${atom}' limit 1";
484 $r = $dbxlink->query ($query);
485 if ($r->rowCount() == 1)
486 $rackData[$unit_no][$locidx]['object_id'] = $object_id;
488 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, atom '${atom}' failed to set object_id to '${object_id}'");
493 return array ('code' => 200, 'message' => "${rack_name}: All changes were successfully saved.");
495 return array ('code' => 300, 'message' => "${rack_name}: No changes.");
498 // This function builds a list of rack-unit-atom records, which are assigned to
499 // the requested object.
500 function getMoleculeForObject ($object_id = 0)
504 showError ("object_id == 0", __FUNCTION__
);
509 "select rack_id, unit_no, atom from RackSpace " .
510 "where state = 'T' and object_id = ${object_id} order by rack_id, unit_no, atom";
511 $result = $dbxlink->query ($query);
514 showError ("SQL query failed", __FUNCTION__
);
517 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
518 $result->closeCursor();
522 // This function builds a list of rack-unit-atom records for requested molecule.
523 function getMolecule ($mid = 0)
527 showError ("mid == 0", __FUNCTION__
);
532 "select rack_id, unit_no, atom from Atom " .
533 "where molecule_id=${mid}";
534 $result = $dbxlink->query ($query);
537 showError ("SQL query failed", __FUNCTION__
);
540 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
541 $result->closeCursor();
545 // This function creates a new record in Molecule and number of linked
546 // R-U-A records in Atom.
547 function createMolecule ($molData)
550 $query = "insert into Molecule values()";
551 $result1 = $dbxlink->query ($query);
552 if ($result1->rowCount() != 1)
554 showError ('Error inserting into Molecule', __FUNCTION__
);
557 $query = 'select last_insert_id()';
558 $result2 = $dbxlink->query ($query);
559 if ($result2 == NULL)
561 showError ('Cannot get last ID.', __FUNCTION__
);
564 $row = $result2->fetch (PDO
::FETCH_NUM
);
565 $molecule_id = $row[0];
566 $result2->closeCursor();
567 foreach ($molData as $dummy => $rua)
569 $rack_id = $rua['rack_id'];
570 $unit_no = $rua['unit_no'];
571 $atom = $rua['atom'];
573 "insert into Atom(molecule_id, rack_id, unit_no, atom) " .
574 "values (${molecule_id}, ${rack_id}, ${unit_no}, '${atom}')";
575 $result3 = $dbxlink->query ($query);
576 if ($result3 == NULL or $result3->rowCount() != 1)
578 showError ('Error inserting into Atom', __FUNCTION__
);
585 // History logger. This function assumes certain table naming convention and
587 // 1. History table name equals to dictionary table name plus 'History'.
588 // 2. History table must have the same row set (w/o keys) plus one row named
589 // 'ctime' of type 'timestamp'.
590 function recordHistory ($tableName, $whereClause)
592 global $dbxlink, $remote_username;
593 $query = "insert into ${tableName}History select *, current_timestamp(), '${remote_username}' from ${tableName} where ${whereClause}";
594 $result = $dbxlink->query ($query);
595 if ($result == NULL or $result->rowCount() != 1)
597 showError ("SQL query failed for table ${tableName}", __FUNCTION__
);
603 function getRackspaceHistory ()
607 "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 " .
608 "MountOperation as mo inner join RackObject as ro on mo.object_id = ro.id " .
609 "inner join Dictionary on objtype_id = dict_key natural join Chapter " .
610 "where chapter_name = 'RackObjectType' order by ctime desc";
611 $result = $dbxlink->query ($query);
614 showError ('SQL query failed', __FUNCTION__
);
617 $ret = $result->fetchAll(PDO
::FETCH_ASSOC
);
618 $result->closeCursor();
622 // This function is used in renderRackspaceHistory()
623 function getOperationMolecules ($op_id = 0)
627 showError ("Missing argument", __FUNCTION__
);
631 $query = "select old_molecule_id, new_molecule_id from MountOperation where id = ${op_id}";
632 $result = $dbxlink->query ($query);
635 showError ("SQL query failed", __FUNCTION__
);
638 // We expect one row.
639 $row = $result->fetch (PDO
::FETCH_ASSOC
);
642 showError ("SQL query succeded, but returned no results.", __FUNCTION__
);
645 $omid = $row['old_molecule_id'];
646 $nmid = $row['new_molecule_id'];
647 $result->closeCursor();
648 return array ($omid, $nmid);
651 function getResidentRacksData ($object_id = 0)
655 showError ('Invalid object_id', __FUNCTION__
);
658 $query = "select distinct rack_id from RackSpace where object_id = ${object_id} order by rack_id";
660 $result = $dbxlink->query ($query);
663 showError ("SQL query failed", __FUNCTION__
);
666 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
667 $result->closeCursor();
669 foreach ($rows as $row)
671 $rackData = getRackData ($row[0]);
672 if ($rackData == NULL)
674 showError ('getRackData() failed', __FUNCTION__
);
677 $ret[$row[0]] = $rackData;
679 $result->closeCursor();
683 function getObjectGroupInfo ($group_id = 0)
686 'select dict_key as id, dict_value as name, count(id) as count from ' .
687 'Dictionary natural join Chapter left join RackObject on dict_key = objtype_id ' .
688 'where chapter_name = "RackObjectType" ' .
689 (($group_id > 0) ?
"and dict_key = ${group_id} " : '') .
692 $result = $dbxlink->query ($query);
695 showError ('SQL query failed', __FUNCTION__
);
699 $clist = array ('id', 'name', 'count');
700 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
701 foreach ($clist as $dummy => $cname)
702 $ret[$row['id']][$cname] = $row[$cname];
703 $result->closeCursor();
705 return current ($ret);
710 // This function returns objects, which have no rackspace assigned to them.
711 // Additionally it keeps rack_id parameter, so we can silently pre-select
712 // the rack required.
713 function getUnmountedObjects ()
717 'select dict_value as objtype_name, dict_key as objtype_id, name, label, barcode, id, asset_no from ' .
718 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter ' .
719 'left join RackSpace on id = object_id '.
720 'where rack_id is null and chapter_name = "RackObjectType" order by dict_value, name, label, asset_no, barcode';
721 $result = $dbxlink->query ($query);
724 showError ('SQL query failure', __FUNCTION__
);
728 $clist = array ('id', 'name', 'label', 'barcode', 'objtype_name', 'objtype_id', 'asset_no');
729 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
731 foreach ($clist as $dummy => $cname)
732 $ret[$row['id']][$cname] = $row[$cname];
733 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
735 $result->closeCursor();
739 function getProblematicObjects ()
743 'select dict_value as objtype_name, dict_key as objtype_id, name, id, asset_no from ' .
744 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter '.
745 'where has_problems = "yes" and chapter_name = "RackObjectType" order by objtype_name, name';
746 $result = $dbxlink->query ($query);
749 showError ('SQL query failure', __FUNCTION__
);
753 $clist = array ('id', 'name', 'objtype_name', 'objtype_id', 'asset_no');
754 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
756 foreach ($clist as $dummy => $cname)
757 $ret[$row['id']][$cname] = $row[$cname];
758 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
760 $result->closeCursor();
764 function commitAddPort ($object_id = 0, $port_name, $port_type_id, $port_label, $port_l2address)
768 showError ('Invalid object_id', __FUNCTION__
);
771 $port_l2address = l2addressForDatabase ($port_l2address);
772 $result = useInsertBlade
777 'name' => "'${port_name}'",
778 'object_id' => "'${object_id}'",
779 'label' => "'${port_label}'",
780 'type' => "'${port_type_id}'",
781 'l2address' => "${port_l2address}"
787 return 'SQL query failed';
790 function commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_reservation_comment)
793 $port_l2address = l2addressForDatabase ($port_l2address);
795 "update Port set name='$port_name', label='$port_label', " .
796 "reservation_comment = ${port_reservation_comment}, l2address=${port_l2address} " .
797 "where id='$port_id'";
798 $result = $dbxlink->exec ($query);
801 $errorInfo = $dbxlink->errorInfo();
802 // We could update nothing.
803 if ($errorInfo[0] == '00000')
805 return $errorInfo[2];
808 function delObjectPort ($port_id)
810 if (unlinkPort ($port_id) != '')
811 return 'unlinkPort() failed in delObjectPort()';
812 if (useDeleteBlade ('Port', 'id', $port_id) != TRUE)
813 return 'useDeleteBlade() failed in delObjectPort()';
817 function getObjectAddressesAndNames ()
821 "select object_id as object_id, ".
822 "RackObject.name as object_name, ".
823 "IPBonds.name as name, ".
824 "INET_NTOA(ip) as ip ".
825 "from IPBonds join RackObject on id=object_id ";
826 $result = $dbxlink->query ($query);
829 showError ("SQL query failure", __FUNCTION__
);
834 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
836 $ret[$count]['object_id']=$row['object_id'];
837 $ret[$count]['object_name']=$row['object_name'];
838 $ret[$count]['name']=$row['name'];
839 $ret[$count]['ip']=$row['ip'];
842 $result->closeCursor();
847 function getEmptyPortsOfType ($type_id)
851 "select distinct Port.id as Port_id, ".
852 "Port.object_id as Port_object_id, ".
853 "RackObject.name as Object_name, ".
854 "Port.name as Port_name, ".
855 "Port.type as Port_type_id, ".
856 "dict_value as Port_type_name ".
859 " Port inner join Dictionary on Port.type = dict_key natural join Chapter ".
861 " join RackObject on Port.object_id = RackObject.id ".
863 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
864 "inner join PortCompat on Port.type = PortCompat.type2 ".
865 "where chapter_name = 'PortType' and PortCompat.type1 = '$type_id' and Link.porta is NULL ".
866 "and Port.reservation_comment is null order by Object_name, Port_name";
867 $result = $dbxlink->query ($query);
870 showError ("SQL query failure, type_id == ${type_id}", __FUNCTION__
);
875 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
877 $ret[$count]['Port_id']=$row['Port_id'];
878 $ret[$count]['Port_object_id']=$row['Port_object_id'];
879 $ret[$count]['Object_name']=$row['Object_name'];
880 $ret[$count]['Port_name']=$row['Port_name'];
881 $ret[$count]['Port_type_id']=$row['Port_type_id'];
882 $ret[$count]['Port_type_name']=$row['Port_type_name'];
885 $result->closeCursor();
889 function linkPorts ($porta, $portb)
891 if ($porta == $portb)
892 return "Ports can't be the same";
900 $query1 = "insert into Link set porta='${porta}', portb='{$portb}'";
901 $query2 = "update Port set reservation_comment = NULL where id = ${porta} or id = ${portb} limit 2";
902 // FIXME: who cares about the return value?
903 $result = $dbxlink->exec ($query1);
904 $result = $dbxlink->exec ($query2);
909 function unlinkPort ($port)
913 "delete from Link where porta='$port' or portb='$port'";
914 $result = $dbxlink->exec ($query);
919 // FIXME: after falling back to using existing getObjectInfo we don't
920 // need that large query. Shrink it some later.
921 function getObjectAddresses ($object_id = 0)
925 showError ('Invalid object_id', __FUNCTION__
);
931 "IPAddress.name as IPAddress_name, ".
932 "IPAddress.reserved as IPAddress_reserved, ".
933 "IPBonds.name as IPBonds_name, ".
934 "INET_NTOA(IPBonds.ip) as IPBonds_ip, ".
935 "IPBonds.type as IPBonds_type, ".
936 "RemoteBonds.name as RemoteBonds_name, ".
937 "RemoteBonds.type as RemoteBonds_type, ".
938 "RemoteBonds.object_id as RemoteBonds_object_id, ".
939 "RemoteObject.name as RemoteObject_name from IPBonds " .
940 "left join IPBonds as RemoteBonds on IPBonds.ip=RemoteBonds.ip " .
941 "and IPBonds.object_id!=RemoteBonds.object_id " .
942 "left join IPAddress on IPBonds.ip=IPAddress.ip " .
943 "left join RackObject as RemoteObject on RemoteBonds.object_id=RemoteObject.id ".
945 "IPBonds.object_id = ${object_id} ".
946 "order by IPBonds.ip, RemoteObject.name";
947 $result = $dbxlink->query ($query);
950 showError ("SQL query failed", __FUNCTION__
);
959 // We are going to call getObjectInfo() for some rows,
960 // hence the connector must be unloaded from the
962 $rows = $result->fetchAll (PDO
::FETCH_ASSOC
);
963 $result->closeCursor();
964 foreach ($rows as $row)
966 if ($prev_ip != $row['IPBonds_ip'])
970 $prev_ip = $row['IPBonds_ip'];
971 $ret[$count]['address_name'] = $row['IPAddress_name'];
972 $ret[$count]['address_reserved'] = $row['IPAddress_reserved'];
973 $ret[$count]['ip'] = $row['IPBonds_ip'];
974 $ret[$count]['name'] = $row['IPBonds_name'];
975 $ret[$count]['type'] = $row['IPBonds_type'];
976 $ret[$count]['references'] = array();
979 if ($row['RemoteBonds_type'])
981 $ret[$count]['references'][$refcount]['type'] = $row['RemoteBonds_type'];
982 $ret[$count]['references'][$refcount]['name'] = $row['RemoteBonds_name'];
983 $ret[$count]['references'][$refcount]['object_id'] = $row['RemoteBonds_object_id'];
984 if (empty ($row['RemoteBonds_object_id']))
985 $ret[$count]['references'][$refcount]['object_name'] = $row['RemoteObject_name'];
988 $oi = getObjectInfo ($row['RemoteBonds_object_id']);
989 $ret[$count]['references'][$refcount]['object_name'] = displayedName ($oi);
998 function getAddressspaceList ()
1003 "id as IPRanges_id, ".
1004 "INET_NTOA(ip) as IPRanges_ip, ".
1005 "mask as IPRanges_mask, ".
1006 "name as IPRanges_name ".
1009 $result = $dbxlink->query ($query);
1010 if ($result == NULL)
1018 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1020 $ret[$count]['id'] = $row['IPRanges_id'];
1021 $ret[$count]['ip'] = $row['IPRanges_ip'];
1022 $ret[$count]['ip_bin'] = ip2long($row['IPRanges_ip']);
1023 $ret[$count]['name'] = $row['IPRanges_name'];
1024 $ret[$count]['mask'] = $row['IPRanges_mask'];
1025 $ret[$count]['mask_bin'] = binMaskFromDec($row['IPRanges_mask']);
1026 $ret[$count]['mask_bin_inv'] = binInvMaskFromDec($row['IPRanges_mask']);
1030 $result->closeCursor();
1035 function getRangeByIp ($ip = '', $id = 0)
1041 "id, INET_NTOA(ip) as ip, mask, name ".
1046 "id, INET_NTOA(ip) as ip, mask, name ".
1047 "from IPRanges where id='$id'";
1049 $result = $dbxlink->query ($query);
1050 if ($result == NULL)
1057 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1059 $binmask=binMaskFromDec($row['mask']);
1060 if ((ip2long($ip) & $binmask) == ip2long($row['ip']))
1062 $ret['id'] = $row['id'];
1063 $ret['ip'] = $row['ip'];
1064 $ret['ip_bin'] = ip2long($row['ip']);
1065 $ret['name'] = $row['name'];
1066 $ret['mask'] = $row['mask'];
1067 $result->closeCursor();
1072 $result->closeCursor();
1076 function updateRange ($id=0, $name='')
1080 "update IPRanges set name='$name' where id='$id'";
1081 $result = $dbxlink->exec ($query);
1086 // This function is actually used not only to update, but also to create records,
1087 // that's why ON DUPLICATE KEY UPDATE was replaced by DELETE-INSERT pair
1088 // (MySQL 4.0 workaround).
1089 function updateAddress ($ip=0, $name='', $reserved='no')
1091 // DELETE may safely fail.
1092 $r = useDeleteBlade ('IPAddress', 'ip', "INET_ATON('${ip}')", FALSE);
1093 // INSERT may appear not necessary.
1094 if ($name == '' and $reserved == 'no')
1096 if (useInsertBlade ('IPAddress', array ('name' => "'${name}'", 'reserved' => "'${reserved}'", 'ip' => "INET_ATON('${ip}')")))
1099 return 'useInsertBlade() failed in updateAddress()';
1102 // FIXME: This function doesn't wipe relevant records from IPAddress table.
1103 function commitDeleteRange ($id = 0)
1106 return 'Invalid range ID in commitDeleteRange()';
1107 if (useDeleteBlade ('IPRanges', 'id', $id))
1110 return 'SQL query failed in commitDeleteRange';
1113 function updateBond ($ip='', $object_id=0, $name='', $type='')
1118 "update IPBonds set name='$name', type='$type' where ip=INET_ATON('$ip') and object_id='$object_id'";
1119 $result = $dbxlink->exec ($query);
1123 function unbindIpFromObject ($ip='', $object_id=0)
1128 "delete from IPBonds where ip=INET_ATON('$ip') and object_id='$object_id'";
1129 $result = $dbxlink->exec ($query);
1133 // This function returns either all or one user account. Array key is user name.
1134 function getUserAccounts ()
1138 'select user_id, user_name, user_password_hash, user_realname, user_enabled ' .
1139 'from UserAccount order by user_name';
1140 $result = $dbxlink->query ($query);
1141 if ($result == NULL)
1143 showError ('SQL query failed', __FUNCTION__
);
1147 $clist = array ('user_id', 'user_name', 'user_realname', 'user_password_hash', 'user_enabled');
1148 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1149 foreach ($clist as $dummy => $cname)
1150 $ret[$row['user_name']][$cname] = $row[$cname];
1151 $result->closeCursor();
1155 // This function returns permission array for all user accounts. Array key is user name.
1156 function getUserPermissions ()
1160 "select UserPermission.user_id, user_name, page, tab, access from " .
1161 "UserPermission natural left join UserAccount where (user_name is not null) or " .
1162 "(user_name is null and UserPermission.user_id = 0) order by user_name, page, tab";
1163 $result = $dbxlink->query ($query);
1164 if ($result == NULL)
1166 showError ('SQL query failed', __FUNCTION__
);
1170 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1172 if ($row['user_id'] == 0)
1173 $row['user_name'] = '%';
1174 $ret[$row['user_name']][$row['page']][$row['tab']] = $row['access'];
1176 $result->closeCursor();
1180 function searchByl2address ($l2addr)
1183 $l2addr = l2addressForDatabase ($l2addr);
1184 $query = "select object_id, Port.id as port_id from RackObject as ro inner join Port on ro.id = Port.object_id " .
1185 "where l2address = ${l2addr}";
1186 $result = $dbxlink->query ($query);
1187 if ($result == NULL)
1189 showError ('SQL query failed', __FUNCTION__
);
1192 $rows = $result->fetchAll (PDO
::FETCH_ASSOC
);
1193 $result->closeCursor();
1194 if (count ($rows) == 0) // No results.
1196 if (count ($rows) == 1) // Target found.
1198 showError ('More than one results was found. This is probably a broken unique key.', __FUNCTION__
);
1202 // This function returns either port ID or NULL for specified arguments.
1203 function getPortID ($object_id, $port_name)
1206 $query = "select id from Port where object_id=${object_id} and name='${port_name}' limit 2";
1207 $result = $dbxlink->query ($query);
1208 if ($result == NULL)
1210 showError ('SQL query failed', __FUNCTION__
);
1213 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
1214 if (count ($rows) != 1)
1217 $result->closeCursor();
1221 function commitCreateUserAccount ($username, $realname, $password)
1223 return useInsertBlade
1228 'user_name' => "'${username}'",
1229 'user_realname' => "'${realname}'",
1230 'user_password_hash' => "'${password}'"
1235 function commitUpdateUserAccount ($id, $new_username, $new_realname, $new_password)
1239 "update UserAccount set user_name = '${new_username}', user_realname = '${new_realname}', " .
1240 "user_password_hash = '${new_password}' where user_id = ${id} limit 1";
1241 $result = $dbxlink->query ($query);
1242 if ($result == NULL)
1244 showError ('SQL query failed', __FUNCTION__
);
1250 function commitEnableUserAccount ($id, $new_enabled_value)
1254 "update UserAccount set user_enabled = '${new_enabled_value}' " .
1255 "where user_id = ${id} limit 1";
1256 $result = $dbxlink->query ($query);
1257 if ($result == NULL)
1259 showError ('SQL query failed', __FUNCTION__
);
1265 function commitGrantPermission ($userid, $page, $tab, $value)
1267 return useInsertBlade
1272 'user_id' => $userid,
1273 'page' => "'${page}'",
1274 'tab' => "'${tab}'",
1275 'access' => "'${value}'"
1280 function commitRevokePermission ($userid, $page, $tab)
1284 "delete from UserPermission where user_id = '${userid}' and page = '${page}' " .
1285 "and tab = '$tab' limit 1";
1286 $result = $dbxlink->query ($query);
1287 if ($result == NULL)
1289 showError ('SQL query failed', __FUNCTION__
);
1295 // This function returns an array of all port type pairs from PortCompat table.
1296 function getPortCompat ()
1300 "select type1, type2, d1.dict_value as type1name, d2.dict_value as type2name from " .
1301 "PortCompat as pc inner join Dictionary as d1 on pc.type1 = d1.dict_key " .
1302 "inner join Dictionary as d2 on pc.type2 = d2.dict_key " .
1303 "inner join Chapter as c1 on d1.chapter_no = c1.chapter_no " .
1304 "inner join Chapter as c2 on d2.chapter_no = c2.chapter_no " .
1305 "where c1.chapter_name = 'PortType' and c2.chapter_name = 'PortType'";
1306 $result = $dbxlink->query ($query);
1307 if ($result == NULL)
1309 showError ('SQL query failed', __FUNCTION__
);
1312 $ret = $result->fetchAll (PDO
::FETCH_ASSOC
);
1313 $result->closeCursor();
1317 function removePortCompat ($type1 = 0, $type2 = 0)
1320 if ($type1 == 0 or $type2 == 0)
1322 showError ('Invalid arguments', __FUNCTION__
);
1325 $query = "delete from PortCompat where type1 = ${type1} and type2 = ${type2} limit 1";
1326 $result = $dbxlink->query ($query);
1327 if ($result == NULL)
1329 showError ('SQL query failed', __FUNCTION__
);
1335 function addPortCompat ($type1 = 0, $type2 = 0)
1337 if ($type1 <= 0 or $type2 <= 0)
1339 showError ('Invalid arguments', __FUNCTION__
);
1342 return useInsertBlade
1345 array ('type1' => $type1, 'type2' => $type2)
1349 // This function returns the dictionary as an array of trees, each tree
1350 // representing a single chapter. Each element has 'id', 'name', 'sticky'
1351 // and 'word' keys with the latter holding all the words within the chapter.
1356 "select chapter_name, Chapter.chapter_no, dict_key, dict_value, sticky from " .
1357 "Chapter natural left join Dictionary order by chapter_name, dict_value";
1358 $result = $dbxlink->query ($query);
1359 if ($result == NULL)
1361 showError ('SQL query failed', __FUNCTION__
);
1365 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1367 $chapter_no = $row['chapter_no'];
1368 if (!isset ($dict[$chapter_no]))
1370 $dict[$chapter_no]['no'] = $chapter_no;
1371 $dict[$chapter_no]['name'] = $row['chapter_name'];
1372 $dict[$chapter_no]['sticky'] = $row['sticky'] == 'yes' ?
TRUE : FALSE;
1373 $dict[$chapter_no]['word'] = array();
1375 if ($row['dict_key'] != NULL)
1376 $dict[$chapter_no]['word'][$row['dict_key']] = $row['dict_value'];
1378 $result->closeCursor();
1382 function getDictStats ()
1384 $stock_chapters = array (1, 2, 3, 11, 12, 13, 14, 16, 17, 18, 19, 20);
1387 "select Chapter.chapter_no, chapter_name, count(dict_key) as wc from " .
1388 "Chapter natural left join Dictionary group by Chapter.chapter_no";
1389 $result1 = $dbxlink->query ($query);
1390 if ($result1 == NULL)
1392 showError ('SQL query #1 failed', __FUNCTION__
);
1395 $tc = $tw = $uc = $uw = 0;
1396 while ($row = $result1->fetch (PDO
::FETCH_ASSOC
))
1400 if (in_array ($row['chapter_no'], $stock_chapters))
1405 $result1->closeCursor();
1406 $query = "select count(attr_id) as attrc from RackObject as ro left join " .
1407 "AttributeValue as av on ro.id = av.object_id group by ro.id";
1408 $result2 = $dbxlink->query ($query);
1409 if ($result2 == NULL)
1411 showError ('SQL query #2 failed', __FUNCTION__
);
1414 $to = $ta = $so = 0;
1415 while ($row = $result2->fetch (PDO
::FETCH_ASSOC
))
1418 if ($row['attrc'] != 0)
1421 $ta +
= $row['attrc'];
1424 $result2->closeCursor();
1426 $ret['Total chapters in dictionary'] = $tc;
1427 $ret['Total words in dictionary'] = $tw;
1428 $ret['User chapters'] = $uc;
1429 $ret['Words in user chapters'] = $uw;
1430 $ret['Total objects'] = $to;
1431 $ret['Objects with stickers'] = $so;
1432 $ret['Total stickers attached'] = $ta;
1436 function commitUpdateDictionary ($chapter_no = 0, $dict_key = 0, $dict_value = '')
1438 if ($chapter_no <= 0 or $dict_key <= 0 or empty ($dict_value))
1440 showError ('Invalid args', __FUNCTION__
);
1445 "update Dictionary set dict_value = '${dict_value}' where chapter_no=${chapter_no} " .
1446 "and dict_key=${dict_key} limit 1";
1447 $result = $dbxlink->query ($query);
1448 if ($result == NULL)
1450 showError ('SQL query failed', __FUNCTION__
);
1456 function commitSupplementDictionary ($chapter_no = 0, $dict_value = '')
1458 if ($chapter_no <= 0 or empty ($dict_value))
1460 showError ('Invalid args', __FUNCTION__
);
1463 return useInsertBlade
1466 array ('chapter_no' => $chapter_no, 'dict_value' => "'${dict_value}'")
1470 function commitReduceDictionary ($chapter_no = 0, $dict_key = 0)
1472 if ($chapter_no <= 0 or $dict_key <= 0)
1474 showError ('Invalid args', __FUNCTION__
);
1479 "delete from Dictionary where chapter_no=${chapter_no} " .
1480 "and dict_key=${dict_key} limit 1";
1481 $result = $dbxlink->query ($query);
1482 if ($result == NULL)
1484 showError ('SQL query failed', __FUNCTION__
);
1490 function commitAddChapter ($chapter_name = '')
1492 if (empty ($chapter_name))
1494 showError ('Invalid args', __FUNCTION__
);
1497 return useInsertBlade
1500 array ('chapter_name' => "'${chapter_name}'")
1504 function commitUpdateChapter ($chapter_no = 0, $chapter_name = '')
1506 if ($chapter_no <= 0 or empty ($chapter_name))
1508 showError ('Invalid args', __FUNCTION__
);
1513 "update Chapter set chapter_name = '${chapter_name}' where chapter_no = ${chapter_no} " .
1514 "and sticky = 'no' limit 1";
1515 $result = $dbxlink->query ($query);
1516 if ($result == NULL)
1518 showError ('SQL query failed', __FUNCTION__
);
1524 function commitDeleteChapter ($chapter_no = 0)
1526 if ($chapter_no <= 0)
1528 showError ('Invalid args', __FUNCTION__
);
1533 "delete from Chapter where chapter_no = ${chapter_no} and sticky = 'no' limit 1";
1534 $result = $dbxlink->query ($query);
1535 if ($result == NULL)
1537 showError ('SQL query failed', __FUNCTION__
);
1543 // This is a dictionary accessor. We perform link rendering, so the user sees
1544 // nice <select> drop-downs.
1545 function readChapter ($chapter_name = '')
1547 if (empty ($chapter_name))
1549 showError ('invalid argument', __FUNCTION__
);
1554 "select dict_key, dict_value from Dictionary natural join Chapter " .
1555 "where chapter_name = '${chapter_name}'";
1556 $result = $dbxlink->query ($query);
1557 if ($result == NULL)
1559 $errorInfo = $dbxlink->errorInfo();
1560 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed for chapter_no = '${chapter_name}'", __FUNCTION__
);
1564 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1565 $chapter[$row['dict_key']] = parseWikiLink ($row['dict_value'], 'o');
1566 $result->closeCursor();
1567 // SQL ORDER BY had no sense, because we need to sort after link rendering, not before.
1572 function getAttrMap ()
1576 "select a.attr_id, a.attr_type, a.attr_name, am.objtype_id, " .
1577 "d.dict_value as objtype_name, am.chapter_no, c2.chapter_name from " .
1578 "Attribute as a natural left join AttributeMap as am " .
1579 "left join Dictionary as d on am.objtype_id = d.dict_key " .
1580 "left join Chapter as c1 on d.chapter_no = c1.chapter_no " .
1581 "left join Chapter as c2 on am.chapter_no = c2.chapter_no " .
1582 "where c1.chapter_name = 'RackObjectType' or c1.chapter_name is null " .
1583 "order by attr_name";
1584 $result = $dbxlink->query ($query);
1585 if ($result == NULL)
1587 $errorInfo = $dbxlink->errorInfo();
1588 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed", __FUNCTION__
);
1592 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1594 $attr_id = $row['attr_id'];
1595 if (!isset ($ret[$attr_id]))
1597 $ret[$attr_id]['id'] = $attr_id;
1598 $ret[$attr_id]['type'] = $row['attr_type'];
1599 $ret[$attr_id]['name'] = $row['attr_name'];
1600 $ret[$attr_id]['application'] = array();
1602 if ($row['objtype_id'] == '')
1604 $application['objtype_id'] = $row['objtype_id'];
1605 $application['objtype_name'] = $row['objtype_name'];
1606 if ($row['attr_type'] == 'dict')
1608 $application['chapter_no'] = $row['chapter_no'];
1609 $application['chapter_name'] = $row['chapter_name'];
1611 $ret[$attr_id]['application'][] = $application;
1613 $result->closeCursor();
1617 function commitUpdateAttribute ($attr_id = 0, $attr_name = '')
1619 if ($attr_id <= 0 or empty ($attr_name))
1621 showError ('Invalid args', __FUNCTION__
);
1626 "update Attribute set attr_name = '${attr_name}' " .
1627 "where attr_id = ${attr_id} limit 1";
1628 $result = $dbxlink->query ($query);
1629 if ($result == NULL)
1631 showError ("SQL query '${query}' failed", __FUNCTION__
);
1637 function commitAddAttribute ($attr_name = '', $attr_type = '')
1639 if (empty ($attr_name))
1641 showError ('Invalid args', __FUNCTION__
);
1652 showError ('Invalid args', __FUNCTION__
);
1655 return useInsertBlade
1658 array ('attr_name' => "'${attr_name}'", 'attr_type' => "'${attr_type}'")
1662 function commitDeleteAttribute ($attr_id = 0)
1666 showError ('Invalid args', __FUNCTION__
);
1669 return useDeleteBlade ('Attribute', 'attr_id', $attr_id);
1672 // FIXME: don't store garbage in chapter_no for non-dictionary types.
1673 function commitSupplementAttrMap ($attr_id = 0, $objtype_id = 0, $chapter_no = 0)
1675 if ($attr_id <= 0 or $objtype_id <= 0 or $chapter_no <= 0)
1677 showError ('Invalid args', __FUNCTION__
);
1680 return useInsertBlade
1685 'attr_id' => $attr_id,
1686 'objtype_id' => $objtype_id,
1687 'chapter_no' => $chapter_no
1692 function commitReduceAttrMap ($attr_id = 0, $objtype_id)
1694 if ($attr_id <= 0 or $objtype_id <= 0)
1696 showError ('Invalid args', __FUNCTION__
);
1701 "delete from AttributeMap where attr_id=${attr_id} " .
1702 "and objtype_id=${objtype_id} limit 1";
1703 $result = $dbxlink->query ($query);
1704 if ($result == NULL)
1706 showError ('SQL query failed', __FUNCTION__
);
1712 // This function returns all optional attributes for requested object
1713 // as an array of records. NULL is returned on error and empty array
1714 // is returned, if there are no attributes found.
1715 function getAttrValues ($object_id)
1717 if ($object_id <= 0)
1719 showError ('Invalid argument', __FUNCTION__
);
1725 "select A.attr_id, A.attr_name, A.attr_type, C.chapter_name, " .
1726 "AV.uint_value, AV.float_value, AV.string_value, D.dict_value from " .
1727 "RackObject as RO inner join AttributeMap as AM on RO.objtype_id = AM.objtype_id " .
1728 "inner join Attribute as A using (attr_id) " .
1729 "left join AttributeValue as AV on AV.attr_id = AM.attr_id and AV.object_id = RO.id " .
1730 "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_no = D.chapter_no " .
1731 "left join Chapter as C on AM.chapter_no = C.chapter_no " .
1732 "where RO.id = ${object_id} order by A.attr_type";
1733 $result = $dbxlink->query ($query);
1734 if ($result == NULL)
1736 $errorInfo = $dbxlink->errorInfo();
1737 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed", __FUNCTION__
);
1740 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1743 $record['id'] = $row['attr_id'];
1744 $record['name'] = $row['attr_name'];
1745 $record['type'] = $row['attr_type'];
1746 switch ($row['attr_type'])
1752 $record['value'] = parseWikiLink ($row[$row['attr_type'] . '_value'], 'o');
1753 $record['a_value'] = parseWikiLink ($row[$row['attr_type'] . '_value'], 'a');
1754 $record['chapter_name'] = $row['chapter_name'];
1755 $record['key'] = $row['uint_value'];
1758 $record['value'] = NULL;
1761 $ret[$row['attr_id']] = $record;
1763 $result->closeCursor();
1767 function commitResetAttrValue ($object_id = 0, $attr_id = 0)
1769 if ($object_id <= 0 or $attr_id <= 0)
1771 showError ('Invalid arguments', __FUNCTION__
);
1775 $query = "delete from AttributeValue where object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1776 $result = $dbxlink->query ($query);
1777 if ($result == NULL)
1779 showError ('SQL query failed', __FUNCTION__
);
1785 // FIXME: don't share common code with use commitResetAttrValue()
1786 function commitUpdateAttrValue ($object_id = 0, $attr_id = 0, $value = '')
1788 if ($object_id <= 0 or $attr_id <= 0)
1790 showError ('Invalid arguments', __FUNCTION__
);
1794 return commitResetAttrValue ($object_id, $attr_id);
1796 $query1 = "select attr_type from Attribute where attr_id = ${attr_id}";
1797 $result = $dbxlink->query ($query1);
1798 if ($result == NULL)
1800 showError ('SQL query #1 failed', __FUNCTION__
);
1803 $row = $result->fetch (PDO
::FETCH_NUM
);
1806 showError ('SQL query #1 returned no results', __FUNCTION__
);
1809 $attr_type = $row[0];
1810 $result->closeCursor();
1816 $column = $attr_type . '_value';
1819 $column = 'uint_value';
1822 showError ("Unknown attribute type '${attr_type}' met", __FUNCTION__
);
1826 "delete from AttributeValue where " .
1827 "object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1828 $result = $dbxlink->query ($query2);
1829 if ($result == NULL)
1831 showError ('SQL query #2 failed', __FUNCTION__
);
1834 // We know $value isn't empty here.
1836 "insert into AttributeValue set ${column} = '${value}', " .
1837 "object_id = ${object_id}, attr_id = ${attr_id} ";
1838 $result = $dbxlink->query ($query3);
1839 if ($result == NULL)
1841 showError ('SQL query #3 failed', __FUNCTION__
);
1847 function commitUseupPort ($port_id = 0)
1851 showError ("Invalid argument", __FUNCTION__
);
1855 $query = "update Port set reservation_comment = NULL where id = ${port_id} limit 1";
1856 $result = $dbxlink->exec ($query);
1857 if ($result == NULL)
1859 showError ("SQL query failed", __FUNCTION__
);
1866 // This is a swiss-knife blade to insert a record into a table.
1867 // The first argument is table name.
1868 // The second argument is an array of "name" => "value" pairs.
1869 // The function returns either TRUE or FALSE (we expect one row
1871 function useInsertBlade ($tablename, $values)
1874 $namelist = $valuelist = '';
1875 foreach ($values as $name => $value)
1877 $namelist = $namelist . ($namelist == '' ?
"(${name}" : ", ${name}");
1878 $valuelist = $valuelist . ($valuelist == '' ?
"(${value}" : ", ${value}");
1880 $query = "insert into ${tablename} ${namelist}) values ${valuelist})";
1881 $result = $dbxlink->exec ($query);
1887 // This swiss-knife blade deletes one record from the specified table
1888 // using the specified key name and value.
1889 function useDeleteBlade ($tablename, $keyname, $keyvalue, $quotekey = TRUE)
1892 if ($quotekey == TRUE)
1893 $query = "delete from ${tablename} where ${keyname}='$keyvalue' limit 1";
1895 $query = "delete from ${tablename} where ${keyname}=$keyvalue limit 1";
1896 $result = $dbxlink->exec ($query);
1897 if ($result === NULL)
1899 elseif ($result != 1)
1905 function loadConfigCache ()
1908 $query = 'SELECT varname, varvalue, vartype, is_hidden, emptyok, description FROM Config ORDER BY varname';
1909 $result = $dbxlink->query ($query);
1910 if ($result == NULL)
1912 $errorInfo = $dbxlink->errorInfo();
1913 showError ("SQL query '${query}'\nwith message '${errorInfo[2]}'\nfailed", __FUNCTION__
);
1917 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1918 $cache[$row['varname']] = $row;
1919 $result->closeCursor();
1923 // setConfigVar() is expected to perform all necessary filtering
1924 function storeConfigVar ($varname = NULL, $varvalue = NULL)
1927 if (empty ($varname) ||
$varvalue === NULL)
1929 showError ('Invalid arguments', __FUNCTION__
);
1932 $query = "update Config set varvalue='${varvalue}' where varname='${varname}' limit 1";
1933 $result = $dbxlink->query ($query);
1934 if ($result == NULL)
1936 showError ("SQL query '${query}' failed", __FUNCTION__
);
1939 $rc = $result->rowCount();
1940 $result->closeCursor();
1941 if ($rc == 0 or $rc == 1)
1943 showError ("Something went wrong for args '${varname}', '${varvalue}'", __FUNCTION__
);
1947 // Database version detector. Should behave corretly on any
1948 // working dataset a user might have.
1949 function getDatabaseVersion ()
1952 $query = "select varvalue from Config where varname = 'DB_VERSION' and vartype = 'string'";
1953 $result = $dbxlink->query ($query);
1954 if ($result == NULL)
1956 $errorInfo = $dbxlink->errorInfo();
1957 if ($errorInfo[0] == '42S02') // ER_NO_SUCH_TABLE
1959 die ('SQL query #1 failed in getDatabaseVersion() with error ' . $errorInfo[2]);
1961 $rows = $result->fetchAll (PDO
::FETCH_NUM
);
1962 if (count ($rows) != 1 ||
empty ($rows[0][0]))
1964 $result->closeCursor();
1965 die ('Cannot guess database version. Config table is present, but DB_VERSION is missing or invalid. Giving up.');
1968 $result->closeCursor();
1972 // Return an array of virtual services. For each of them list real server pools
1973 // with their load balancers and other stats.
1974 function getSLBSummary ()
1977 $query = 'select vs.id as vsid, vip as vip_bin, inet_ntoa(vip) as vip, vport, proto, ' .
1978 'vs.name, rsp.id as pool_id, rsp.name as pool_name, object_id, count(rs.id) as rscount from ' .
1979 'IPVirtualService as vs inner join IPRSPool as rsp on vs.id = rsp.vs_id ' .
1980 'inner join IPRealServer as rs on rs.rspool_id = rsp.id ' .
1981 'inner join IPLoadBalancer as lb on rsp.id = lb.rspool_id ' .
1982 'group by rsp.id, object_id order by vip_bin, object_id';
1983 $result = $dbxlink->query ($query);
1984 if ($result == NULL)
1986 $errorInfo = $dbxlink->errorInfo();
1987 showError ("SQL query '${query}' failed with message '${errorInfo[2]}'", __FUNCTION__
);
1991 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
1993 $vsid = $row['vsid'];
1994 $object_id = $row['object_id'];
1995 if (!isset ($ret[$vsid]))
1997 $ret[$vsid] = array();
1998 foreach (array ('vip', 'vport', 'proto', 'name') as $cname)
1999 $ret[$vsid][$cname] = $row[$cname];
2000 $ret[$vsid]['lblist'] = array();
2002 $ret[$vsid]['lblist'][$row['object_id']][$row['pool_id']] = array ('size' => $row['rscount'], 'name' => $row['pool_name']);
2004 $result->closeCursor();
2008 // Get the detailed composition of a particular virtual service, namely the list
2009 // of all pools, each shown with the list of objects servicing it. VS/RS configs
2010 // will be returned as well.
2011 function getVServiceInfo ($vsid = 0)
2014 $query = "select inet_ntoa(vip) as vip, vport, proto, name, vsconfig, default_rsconfig, " .
2015 "rs.id as rsid, inet_ntoa(rs.rsip) as rsip, rsport as rsport from " .
2016 "IPVirtualService as vs inner join IPRealServer as rs on vs.vsid = rs.vsid " .
2017 "where vs.vsid = ${vsid} order by rsip, rsport";
2018 $result = $dbxlink->query ($query);
2019 if ($result == NULL)
2021 showError ('SQL query failed', __FUNCTION__
);
2025 while ($row = $result->fetch (PDO
::FETCH_ASSOC
))
2027 if (!isset ($vsinfo['vip']))
2029 foreach (array ('vip', 'vport', 'proto', 'name', 'vsconfig', 'default_rsconfig') as $cname)
2030 $vsinfo[$cname] = $row[$cname];
2031 $vsinfo['rslist'] = array();
2033 $vsinfo['rslist'][] = array ('rsid' => $row['rsid'], 'rsip' => $row['rsip'], 'rsport' => $row['rsport']);
2035 $result->closeCursor();