prepare("INSERT INTO $table ($fields) VALUES ($pnames)"); $sth = $pdo->prepare($sql); try { $sth->execute($params); } catch (PDOexception $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); $g_error->Add($sql); $g_error->Add(print_r($params, true)); return false; } return $pdo->LastInsertId(); } function db_exec_update($table, $params, $pk) { // primary key can be a single field name or multiple fields: $pk is an array global $pdo; global $g_error; // remove primary key fields from list of fields to update if (is_array($pk)) { if (count($pk) == 1) { $pk = $pk[0]; $fields = $params; unset($fields[':'.$pk]); $where = "$pk=:$pk"; } else { $pkparams = array_map(fn($value) => ':' . $value, $pk); $fields = array_diff_key($params, array_flip($pkparams)); $where = join(' AND ', array_map(fn($s) => "$s=:$s", $pk)); } } else { $fields = $params; unset($fields[':'.$pk]); $where = "$pk=:$pk"; } $fieldlist = join(', ', array_map(fn($s) => ltrim($s, ':') . '=' . $s, array_keys($fields))); $sql = "UPDATE $table SET $fieldlist WHERE $where"; $sth = $pdo->prepare($sql); try { $sth->execute($params); } catch (PDOexception $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); $g_error->Add($sql); $g_error->Add(print_r($params, true)); return false; } return true; } function db_exec_delete($table, $condition, $params) { // WIP untested! // condition contains the where part of the sql statement // params are values for placeholders in condition // returns the number of deleted records global $pdo; global $g_error; // convert params to array if single value is given if (!is_array($params)) { $params = [$params]; } $sql = "DELETE FROM $table"; if (!empty($condition)) { $sql .= ' WHERE ' . $condition; } $sth = $pdo->prepare($sql); try { $sth->execute($params); } catch (PDOexception $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); $g_error->Add($sql); $g_error->Add(print_r($params, true)); return -1; } return $sth->rowCount(); } function db_get_setting($sno, $userid, $autocreate=false, $default=NULL) { global $pdo; global $g_error; global $g_warning; $sql = "SELECT valint, valstr FROM settings WHERE userid=? AND sno=?"; $sth = $pdo->prepare($sql); try { $sth->execute([$userid, $sno]); } catch (Exception $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); $g_error->Add($sql); $g_error->Add("userid=$userid, sno=$sno", true); return $default; } $row = $sth->fetch(); if ($row === false) { if ($autocreate) { // create empty settings record if it not exists $p[':userid'] = $userid; $p[':sno'] = $sno; db_exec_insert('settings', $p); $g_warning->Add("Added missing settings record with id=$sno"); } return $default; } return $row; } function db_get_ddtext($ddid, $ddval) { // returns single value for dropdown global $pdo; global $g_error; $sql = "SELECT ddtext FROM dropdown WHERE ddid=? AND ddval=?"; $sth = $pdo->prepare($sql); try { $sth->execute([$ddid, $ddval]); } catch(PDOException $e) { $g_error->Add($e->getMessage()); return '#err'; } return $sth->fetchColumn(); } function db_set_revision($revtype, $revid) { global $pdo; global $g_error; $sql = "INSERT INTO revision (revtype, revid, changed) " . "VALUES (?, ?, NOW()) " . "ON DUPLICATE KEY UPDATE changed = NOW()"; try { $sth = $pdo->prepare($sql); $sth->execute([$revtype, $revid]); } catch(PDOException $e) { $g_error->Add($e->getMessage()); } } // ========== OPTION FUNCTIONS ================================================ // Functions to get option lists (key/value) function db_get_options($ddid, $orderby = '', $short = False, $default = NULL) { // returns list for dropdown // if short is set the short value will be returned global $pdo; $list = array(); if ($default != NULL) { $list[0] = $default; } if ($short) { $sql = "SELECT ddval, ddshort"; } else { $sql = "SELECT ddval, ddtext"; } $sql .= " FROM dropdown WHERE ddid=?"; if ($orderby == 'ddtext') { $sql .= ' ORDER BY ddtext'; } elseif ($orderby == 'sort') { $sql .= ' ORDER BY sort'; } else { $sql .= ' ORDER BY ddval'; } $sth = $pdo->prepare($sql); $sth->execute([$ddid]); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $list[$rec[0]] = h($rec[1]); } return $list; } function db_get_opt_vessel() { global $pdo; $list = array(); $sth = $pdo->query("SELECT vid, vesselname, model FROM vessel ORDER BY vid"); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $row) { $list[$row[0]] = h($row[1]); } return $list; } function db_get_opt_storage($vid) { global $pdo; global $g_error; $list = array(); // exclude storage type "tank" from list $sql = "SELECT sid, sname FROM storage WHERE vid=? AND stype<>1 ORDER BY sname"; $sth = $pdo->prepare($sql); try { $sth->execute([$vid]); } catch(PDOException $e) { $g_error->Add($e->getMessage()); } foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $list[$rec[0]] = h($rec[1]); } return $list; } function db_get_opt_box($vid, $exclude=[]) { global $pdo; global $g_error; $list = array(); $sql = "SELECT boxid, label, content " . "FROM box INNER JOIN storage USING (sid) " . "WHERE vid=? " . "ORDER BY label"; $sth = $pdo->prepare($sql); try { $sth->execute([$vid]); } catch(PDOException $e) { $g_error->Add($e->getMessage()); } foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { if (! in_array($rec[0], $exclude)) { $list[$rec[0]] = h($rec[1]) . ($rec[2] ? ' - '. h($rec[2]) : ''); } } return $list; } function db_get_opt_equip($vid, $default=NULL, $exclude=[]) { global $pdo; global $g_error; if (isset($default)) { $list = $default; } else { $list = array(); } $sql = "SELECT eid, ename FROM equipment WHERE vid=? ORDER BY ename"; $sth = $pdo->prepare($sql); try { $sth->execute([$vid]); } catch(PDOException $e) { $g_error->Add($e->getMessage()); } foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { if (! in_array($rec[0], $exclude)) { $list[$rec[0]] = h($rec[1]); } } return $list; } function db_get_opt_manuf($default=NULL) { global $pdo; if (isset($default)) { $list = $default; } else { $list = array(); } $sql = "SELECT compid, compname FROM company WHERE comptype=2 ORDER BY compname"; $sth = $pdo->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $row) { $list[$row[0]] = h($row[1]); } return $list; } function db_get_opt_supp($default=NULL) { global $pdo; if (isset($default)) { $list = $default; } else { $list = array(); } $sql = "SELECT compid, compname FROM company WHERE comptype=1 ORDER BY compname"; $sth = $pdo->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $row) { $list[$row[0]] = h($row[1]); } return $list; } function db_get_opt_user($default=NULL) { global $pdo; if (isset($default)) { $list = $default; } else { $list = array(); } $sql = "SELECT userid, displayname FROM user WHERE userid>0 ORDER BY displayname"; $sth = $pdo->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $row) { $list[$row[0]] = h($row[1]); } return $list; } function db_get_opt_proj($vid, $exclude=[], $default=NULL) { // exclude is a list of project states global $pdo; global $g_error; if (isset($default)) { $list = $default; } else { $list = array(); } $sql = "SELECT projid, projname FROM project WHERE vid=?"; $where = []; foreach ($exclude as $excl) { $where[] = "NOT FIND_IN_SET('$excl',projstate)"; } if (! empty($where)) { $sql .= ' AND '. implode(' AND ', $where); } $sql .= " ORDER BY projname"; $sth = $pdo->prepare($sql); try { $sth->execute([$vid]); } catch(PDOException $e) { $g_error->Add($e->getMessage()); } foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $list[$rec[0]] = h($rec[1]); } return $list; } function db_get_opt_cable($vid, $default=NULL) { global $pdo; if (isset($default)) { $list = $default; } else { $list = array(); } $sql = "SELECT cableid, cablename FROM cable WHERE vid=? ORDER BY cablename"; $sth = $pdo->prepare($sql); $sth->execute([$vid]); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $row) { $list[$row[0]] = h($row[1]); } return $list; } // ========== FILTER FUNCTIONS ================================================ function db_get_filter($user, $sno, $fields = []) { // if fields supplied empting missing field are created global $pdo; global $g_warning; $flt = new stdClass(); $sth = $pdo->prepare("SELECT valstr FROM settings WHERE userid=? AND sno=?"); try { $sth->execute([$user, $sno]); $json = $sth->fetchColumn(); if ($json === false) { // no settings record. create empty one for later updates $sth = $pdo->prepare("INSERT INTO settings (userid, sno, valstr) VALUES (?, ?, '{}')"); $sth->execute([$user, $sno]); } else { $flt = json_decode($json); } // create missing fields foreach ($fields as $f) { if (!property_exists($flt, $f)) { $flt->$f = null; } } } catch (PDOexception $e) { $g_warning->Add('SQL-Error: '. $e->getMessage()); $g_warning->PrintOut(); } return $flt; } function db_clear_filter($sno) { global $user; global $pdo; $sql = "DELETE FROM settings WHERE userid=? AND sno=?"; $sth = $pdo->prepare($sql); try { $sth->execute([$user->id, $sno]); } catch (PDOexception $e) { $g_warning->Add('SQL-Error: '. $e->getMessage()); $g_warning->Add($sql); $g_warning->Add('Setting: ', $sno); return false; } return true; } function db_save_filter($flt, $sno) { global $user; global $pdo; global $g_warning; $sql = "UPDATE settings SET valstr=? WHERE userid=? AND sno=?"; $sth = $pdo->prepare($sql); try { $sth->execute([json_encode($flt), $user->id, $sno]); } catch (PDOexception $e) { $g_warning->Add('SQL-Error: '. $e->getMessage()); $g_warning->Add($sql); $g_warning->Add(print_r($flt, true)); $g_warning->Add('Setting: ', $sno); return false; } return true; } // ========== LOCKING FUNCTIONS =============================================== function db_lock_acquire($table, $key, $userid) { global $pdo; $token = bin2hex(random_bytes(8)); // first try new lock $sql = "INSERT INTO recordlock (lock_table, lock_key, token, userid) " . "VALUES (?, ?, ?, ?)"; try { $sth = $pdo->prepare($sql); $sth->execute([$table, $key, $token, $userid]); return $token; } catch (PDOException $e) { if ($e->errorInfo[1] != 1062) { throw $e; } } // lazy locking: lock exists but is old, so we use it $sql = "UPDATE recordlock " . "SET token=?, userid=?, " . "created_at=CURRENT_TIMESTAMP, last_seen=CURRENT_TIMESTAMP " . "WHERE lock_table=? AND lock_key=? " . "AND last_seenprepare($sql); $sth->execute([$token, $userid, $table, $key]); if ($stmt->rowCount() === 1) { return $token; } return null; } function db_lock_refresh($token) { global $pdo; $sql = "UPDATE recordlock " . "SET last_seen=CURRENT_TIMESTAMP " . "WHERE token=?"; $sth = $pdo->prepare($sql); $sth->execute([$token]); return $sth->rowCount() === 1; } function db_lock_release($token) { global $pdo; $sql = "DELETE FROM recordlock WHERE token=?"; $sth = $pdo->prepare($sql); $sth->execute([$token]); return $sth->rowCount() === 1; }