Skip to content

Commit

Permalink
Fixed #101 DB Queries. Make use of queryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Stocker committed Dec 17, 2021
1 parent f81f142 commit 7a9736e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 82 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SensorLogger

### 0.1.4nc UNRELEASED
* Fixed #101 DB Queries. Make use of queryBuilder.
* Fixed #103 Migration Version000010Date20190206183552. Drop index before setting new. Thanx @stefan123t for the hint
* Fixed #103 Devices::getDevices contributed by @stefan123t
* Added #101 PHP 8 and #100 NC23 Support
Expand Down
87 changes: 52 additions & 35 deletions controller/apisensorloggercontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,14 @@ protected function requestResponse($success, $code = null, $message = null, $dat
*/
protected function insertDeviceType($deviceType)
{
$sql = 'INSERT INTO `*PREFIX*sensorlogger_device_types` (`user_id`,`device_type_name`) VALUES(?,?)';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $this->userSession->getUser()->getUID());
$stmt->bindParam(2, $deviceType);
if ($stmt->execute()) {
return (int)$this->db->lastInsertId();
$userId = $this->userSession->getUser()->getUID();
$queryBuilder = $this->db->getQueryBuilder();
$queryBuilder->insert('sensorlogger_device_types')
->setValue('device_type_name', $queryBuilder->createNamedParameter($deviceType))
->setValue('user_id', $queryBuilder->createNamedParameter($userId));

if ($queryBuilder->execute()) {
return (int)$queryBuilder->getLastInsertId();
}
return false;
}
Expand All @@ -368,12 +370,14 @@ protected function insertDeviceType($deviceType)
*/
protected function insertDeviceGroup($string)
{
$sql = 'INSERT INTO `*PREFIX*sensorlogger_device_groups` (`user_id`,`device_group_name`) VALUES(?,?)';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $this->userSession->getUser()->getUID());
$stmt->bindParam(2, $string);
if ($stmt->execute()) {
return (int)$this->db->lastInsertId();
$userId = $this->userSession->getUser()->getUID();
$queryBuilder = $this->db->getQueryBuilder();
$queryBuilder->insert('sensorlogger_device_groups')
->setValue('device_group_name', $queryBuilder->createNamedParameter($string))
->setValue('user_id', $queryBuilder->createNamedParameter($userId));

if ($queryBuilder->execute()) {
return (int)$queryBuilder->getLastInsertId();
}
return false;
}
Expand All @@ -384,8 +388,8 @@ protected function insertDeviceGroup($string)
*/
protected function insertDataTypes($array)
{
$sql = 'INSERT INTO `*PREFIX*sensorlogger_data_types` (`user_id`,`description`,`type`,`short`) VALUES(?,?,?,?)';
$stmt = $this->db->prepare($sql);
// $sql = 'INSERT INTO `*PREFIX*sensorlogger_data_types` (`user_id`,`description`,`type`,`short`) VALUES(?,?,?,?)';
// $stmt = $this->db->prepare($sql);

if (isset($array['description'])) {
$description = $array['description'] ?: '';
Expand All @@ -405,12 +409,16 @@ protected function insertDataTypes($array)
$unit = '';
}

$stmt->bindParam(1, $this->userSession->getUser()->getUID());
$stmt->bindParam(2, $description);
$stmt->bindParam(3, $type);
$stmt->bindParam(4, $unit);
if ($stmt->execute()) {
return (int)$this->db->lastInsertId();
$userId = $this->userSession->getUser()->getUID();
$queryBuilder = $this->db->getQueryBuilder();
$queryBuilder->insert('sensorlogger_data_types')
->setValue('user_id', $queryBuilder->createNamedParameter($userId))
->setValue('description', $queryBuilder->createNamedParameter($description))
->setValue('type', $queryBuilder->createNamedParameter($type))
->setValue('short', $queryBuilder->createNamedParameter($unit));

if ($queryBuilder->execute()) {
return (int)$queryBuilder->getLastInsertId();
}
return false;
}
Expand Down Expand Up @@ -461,12 +469,21 @@ protected function checkRequestParams($params)
*/
protected function insertDeviceDataTypes($deviceId, $dataTypeId)
{
$sql = 'INSERT INTO `*PREFIX*sensorlogger_device_data_types` (`user_id`,`device_id`,`data_type_id`) VALUES(?,?,?)';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $this->userSession->getUser()->getUID());
$stmt->bindParam(2, $deviceId);
$stmt->bindParam(3, $dataTypeId);
$stmt->execute();
// $sql = 'INSERT INTO `*PREFIX*sensorlogger_device_data_types` (`user_id`,`device_id`,`data_type_id`) VALUES(?,?,?)';
// $stmt = $this->db->prepare($sql);
// $stmt->bindParam(1, $this->userSession->getUser()->getUID());
// $stmt->bindParam(2, $deviceId);
// $stmt->bindParam(3, $dataTypeId);
// $stmt->execute();

$userId = $this->userSession->getUser()->getUID();
$queryBuilder = $this->db->getQueryBuilder();
$queryBuilder->insert('sensorlogger_device_data_types')
->setValue('device_id', $queryBuilder->createNamedParameter($deviceId))
->setValue('data_type_id', $queryBuilder->createNamedParameter($dataTypeId))
->setValue('user_id', $queryBuilder->createNamedParameter($userId));

$queryBuilder->execute();
}

/**
Expand Down Expand Up @@ -520,9 +537,6 @@ protected function checkRegisteredDataTypes($params)
*/
protected function insertDevice($array)
{
$sql = 'INSERT INTO `*PREFIX*sensorlogger_devices` (`uuid`,`name`,`type_id`,`user_id`) VALUES(?,?,?,?)';
$stmt = $this->db->prepare($sql);

if (isset($array['deviceId'])) {
if (!isset($array['deviceName'])) {
$array['deviceName'] = 'Default device';
Expand All @@ -532,13 +546,16 @@ protected function insertDevice($array)
$array['deviceTypeId'] = 0;
}

$stmt->bindParam(1, $array['deviceId']);
$stmt->bindParam(2, $array['deviceName']);
$stmt->bindParam(3, $array['deviceTypeId']);
$stmt->bindParam(4, $this->userSession->getUser()->getUID());
$userId = $this->userSession->getUser()->getUID();
$queryBuilder = $this->db->getQueryBuilder();
$queryBuilder->insert('sensorlogger_devices')
->setValue('uuid', $queryBuilder->createNamedParameter($array['deviceId']))
->setValue('name', $queryBuilder->createNamedParameter($array['deviceName']))
->setValue('type_id', $queryBuilder->createNamedParameter($array['deviceTypeId']))
->setValue('user_id', $queryBuilder->createNamedParameter($userId));

if ($stmt->execute()) {
return (int)$this->db->lastInsertId();
if ($queryBuilder->execute()) {
return (int)$queryBuilder->getLastInsertId();
}
} else {
return 'Missing device ID';
Expand Down
50 changes: 27 additions & 23 deletions lib/DeviceGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ public static function isDeletable($userId, $id, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->select('id')
->from('sensorlogger_devices')
->where('user_id = :userId')
->andWhere('( group_id = :gid')
->orWhere('group_parent_id = :gpid )')
->setParameter(':userId', $userId)
->setParameter(':gid', $id)
->setParameter(':gpid', $id);
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('group_id', $query->createNamedParameter($id)))
->orWhere($query->expr()->eq('group_parent_id',$query->createNamedParameter($id)));
$result = $query->execute();
if ($result)
{
Expand Down Expand Up @@ -157,27 +154,34 @@ public static function deleteDeviceGroupByName($userId, $deviceGroupName, IDBCon
* @return int
*/
public static function insertSensorGroup($userId, $deviceGroupName, IDBConnection $db) {
// immer zuerst die Existenz pruefen
$devGroup = DeviceGroups::getDeviceGroupByName($userId, $deviceGroupName, $db);
if (is_numeric($devGroup['id']) && $devGroup['id'] > 0)
return (int)$devGroup['id'];

if(is_array($devGroup)) {
if (is_numeric($devGroup['id']) && $devGroup['id'] > 0) {
return (int)$devGroup['id'];
}
}
$lastId = 0;

//SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
//START TRANSACTION;

$sql = 'INSERT INTO `*PREFIX*sensorlogger_device_groups` (`user_id`,`device_group_name`) VALUES(?,?)';
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $userId);
$stmt->bindParam(2, $deviceGroupName);
if($stmt->execute()){
$lastId = (int)$db->lastInsertId();
}

// COMMIT;

return $lastId;
// $sql = 'INSERT INTO `*PREFIX*sensorlogger_device_groups` (`user_id`,`device_group_name`) VALUES(?,?)';
// $stmt = $db->prepare($sql);
// $stmt->bindParam(1, $userId);
// $stmt->bindParam(2, $deviceGroupName);

//$userId = $this->userSession->getUser()->getUID();
$queryBuilder = $db->getQueryBuilder();
$queryBuilder->insert('sensorlogger_device_groups')
->setValue('device_group_name', $queryBuilder->createNamedParameter($deviceGroupName))
->setValue('user_id', $queryBuilder->createNamedParameter($userId));
if ($queryBuilder->execute()) {
return (int)$queryBuilder->getLastInsertId();
}

// if($stmt->execute()){
// $lastId = (int)$db->lastInsertId();
// }

// return $lastId;
}

}
32 changes: 14 additions & 18 deletions lib/DeviceTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class DeviceTypes {
*/
public static function getDeviceTypes($userId, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->select(array('sdt.user_id','sdt.id','sdt.device_type_name'))
$query->select(['sdt.user_id','sdt.id','sdt.device_type_name'])
->from('sensorlogger_device_types','sdt')
->leftJoin('sdt', 'sensorlogger_devices', 'sd', 'sdt.id = sd.type_id')
->where('sdt.user_id = :userId')
->leftJoin('sdt', 'sensorlogger_devices', 'sd', $query->expr()->eq('sdt.id', 'sd.type_id'))
->where($query->expr()->eq('sdt.user_id',$query->createNamedParameter($userId)))
->groupBy('sdt.id')
->orderBy('sdt.id', 'DESC')
->setParameter(':userId', $userId);
->orderBy('sdt.id', 'DESC');
$query->setMaxResults(100);


$result = $query->execute();
$data = $result->fetchAll();
return $data;
Expand All @@ -39,12 +40,10 @@ public static function getDeviceTypes($userId, IDBConnection $db) {
*/
public static function getDeviceType($userId, $deviceTypeId, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->select(array('id', 'user_id', 'device_type_name'))
$query->select(['id', 'user_id', 'device_type_name'])
->from('sensorlogger_device_types')
->where('user_id = :userId')
->andWhere('id = :deviceTypeId')
->setParameter(':userId', $userId)
->setParameter(':deviceTypeId', $deviceTypeId);
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('id', $deviceTypeId));
$result = $query->execute();
if ($result)
{
Expand Down Expand Up @@ -136,10 +135,8 @@ public static function isDeletable($userId, $id, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->select('id')
->from('sensorlogger_devices')
->where('user_id = :userId')
->andWhere('type_id = :id')
->setParameter(':userId', $userId)
->setParameter(':id', $id);
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('type_id', $query->createNamedParameter($id)));
$result = $query->execute();
if ($result)
{
Expand All @@ -163,10 +160,9 @@ public static function deleteDeviceType($userId, $typeId, IDBConnection $db) {

$query = $db->getQueryBuilder();
$query->delete('sensorlogger_device_types')
->where('user_id = :userId')
->andWhere('id = :typeId')
->setParameter(':userId', $userId)
->setParameter(':typeId', $typeId);
->from('sensorlogger_devices')
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('id', $query->createNamedParameter($typeId)));
return $query->execute();
}
}
15 changes: 13 additions & 2 deletions lib/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public function __construct(IDBConnection $db) {
*/
public static function getDevices($userId, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->selectAlias('sd.user_id','user_id')
$query->addSelect('sd.uuid')
->addSelect('sd.name')
->addSelect('sd.group_id')
->addSelect('sd.group_parent_id')
->selectAlias('sd.user_id','user_id')
->selectAlias('sd.id','id')
->selectAlias('sdt.device_type_name','device_type_name')
->selectAlias('sdg0.device_group_name','device_group_name')
Expand Down Expand Up @@ -120,7 +124,12 @@ public static function getDeviceByUuid($userId, $uuid, IDBConnection $db) {
*/
public static function getDeviceDetails($userId, $id, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->select('*')
$query->addSelect('sd.uuid')
->addSelect('sd.name')
->addSelect('sd.user_id')
->addSelect('sd.type_id')
->addSelect('sd.group_id')
->addSelect('sd.group_parent_id')
->selectAlias('sdg0.device_group_name','device_group_name')
->selectAlias('sdg1.device_group_name','device_group_parent_name')
->from('sensorlogger_devices','sd')
Expand All @@ -129,9 +138,11 @@ public static function getDeviceDetails($userId, $id, IDBConnection $db) {
->leftJoin('sd', 'sensorlogger_device_groups', 'sdg1', 'sdg1.id = sd.group_parent_id')
->where('sd.id = "'.$id.'" ')
->andWhere('sd.user_id = "'.$userId.'"');

$result = $query->execute();
$data = $result->fetch();
$device = Device::fromRow($data);

return $device;
}

Expand Down
5 changes: 1 addition & 4 deletions lib/SensorLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ public static function deleteLogById($id, IDBConnection $db) {
public static function deleteLogsByUuid($uuid, IDBConnection $db) {
$query = $db->getQueryBuilder();
$query->delete('sensorlogger_logs')
->where('device_uuid = :uuid')
->setParameters([
':uuid' => $uuid
]);
->where($query->expr()->eq('device_uuid', $query->createNamedParameter($uuid)));
return $query->execute();
}
}

0 comments on commit 7a9736e

Please sign in to comment.