/** * Get Records from DB in reponse to GET * @global class $api * @param class $conn * @param class $dbTable * @param string $RecordKey * @param string $resServer * @param string $api_key * @return array */ function getRecord($conn, $dbTable, $RecordKey = '', $resServer = '', $api_key = 'public') { global $api; // Setup Where Clause $where = ''; if (strlen($RecordKey) > 0) { $where = 'WHERE ' . $dbTable->getPKField() . ' = ' . $RecordKey; } // var_dump($where); // Set Table Name $tblName = $conn->getSchema(false) . '.' . $dbTable->getName(); $resData = array(); try { switch ($conn->getType()) { case 0: // SQLite $tblName = $dbTable->getName(); $_db = new PDO("sqlite:" . $conn->getServer(false)); $_sql = "SELECT * FROM {$tblName} {$where}"; break; case 1: // MySQL $_db = new PDO("mysql:host=" . $conn->getServer(false) . ";dbname=" . $conn->getSchema(false), $conn->getUser(false), $conn->getPassword(false)); $_sql = "select * from {$tblName} {$where}"; break; case 2: // PostgreSQL $_db = new PDO("pgsql:host=" . $conn->getServer(false) . ";dbname=" . $conn->getSchema(false), $conn->getUser(false), $conn->getPassword(false)); $_sql = "select * from {$tblName} {$where}"; break; case 3: // Oracle $_db = new PDO("oci:dbname=" . $conn->getServer(false), $conn->getUser(false), $password); $_sql = "select * from {$tblName} {$where}"; break; case 4: // Microsoft SQL $_db = new PDO("sqlsrv:Server=" . $conn->getServer(false) . ";Database=" . $conn->getSchema(false), $conn->getUser(false), $conn->getPassword(false)); $_sql = "select * from {$tblName} {$where}"; break; } $_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Get Response // var_dump($_sql); $dbRes = $_db->query($_sql); $iRec = 0; foreach ($dbRes as $row) { // Get Column Data $recID = ''; $rec = array(); foreach (range(0, $dbRes->columnCount() - 1) as $column_index) { $field = $dbRes->getColumnMeta($column_index); $rowValue = $row[$column_index]; $rec[] = array("description" => $field['name'], "value" => $rowValue, "type" => $field['native_type']); // Check if this is the PK Field if (strtolower($field['name']) == strtolower($dbTable->getPKField())) { $recID = $row[$column_index]; } } // Build Link URL $link = $resServer . "/" . $recID; if (strlen($RecordKey) > 0) { $link = $resServer; } // Add To Result $resData[] = array("type" => "record", "fields" => $rec, "field_count" => count($rec), "link" => $link, "id" => $recID); // Check for Max Records if (strtolower($api_key) == 'public') { $iRec++; if ($iRec >= $api->getMaxRecords()) { break; } } } } catch (PDOException $ex) { showMessage($_sql); // Print PDOException message $res = $ex->getMessage(); showMessage($res); } return $resData; }