From 294c65a9bf9950e73ead97ab79e4596bdb97100a Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 9 Jun 2017 15:45:33 +0200 Subject: [PATCH] Added controller UDF to read UDFs definitions UDFs update&insert second version --- application/controllers/api/v1/system/UDF.php | 56 +++++++++++ application/core/DB_Model.php | 97 +++++++++---------- 2 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 application/controllers/api/v1/system/UDF.php diff --git a/application/controllers/api/v1/system/UDF.php b/application/controllers/api/v1/system/UDF.php new file mode 100644 index 000000000..d9bf96422 --- /dev/null +++ b/application/controllers/api/v1/system/UDF.php @@ -0,0 +1,56 @@ +load->model('system/UDF_model', 'UDFModel'); + } + + /** + * @return void + */ + public function getUDF() + { + $schema = $this->get('schema'); + $table = $this->get('table'); + + if (isset($schema) || isset($table)) + { + $result = $this->UDFModel->loadWhere( + array( + 'schema' => $schema, + 'table' => $table + ) + ); + + $this->response($result, REST_Controller::HTTP_OK); + } + else + { + $result = $this->UDFModel->load(); + + $this->response($result, REST_Controller::HTTP_OK); + } + } +} \ No newline at end of file diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 692d87065..9c78d4ce0 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -97,7 +97,7 @@ class DB_Model extends FHC_Model // Checks rights if ($isEntitled = $this->_isEntitled(PermissionLib::REPLACE_RIGHT)) return $isEntitled; - + // DB-REPLACE if ($this->db->replace($this->dbTable, $data)) return success($this->db->insert_id()); @@ -105,51 +105,6 @@ class DB_Model extends FHC_Model return error($this->db->error(), FHC_DB_ERROR); } - /** - * Manage UDFs on update - */ - private function _manageUDFUpdate($id, &$data) - { - // Checks if this table has udf - if ($this->_hasUDF()) - { - $dbVersionArray = explode('.', $this->db->version()); - if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 5) - { - $this->addSelect(DB_Model::UDF_FIELD_NAME); - $result = $this->load($id); - if (hasData($result)) - { - // Get udf values from $data & clean udf values from $data - // Must be performed here because the load method populates UDFs too - $this->_popUDF($data); - - $jsonb = (array)$result->retval[0]; - if (count($jsonb) > 0) - { - foreach($this->UDFs as $key => $val) - { - if (isset($jsonb[$key])) - { - $jsonb[$key] = $val; - } - } - - $jsonEncodedUDFs = json_encode($jsonb); - if ($jsonEncodedUDFs !== false) - { - $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; - } - } - } - } - else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 5) - { - // TODO - } - } - } - /** * Update Data in DB-Table * @@ -171,11 +126,6 @@ class DB_Model extends FHC_Model // UDFs $this->_manageUDFUpdate($id, $data); - var_dump($this->UDFs); - var_dump($data); - - exit; - // DB-UPDATE // Check for composite Primary Key if (is_array($id)) @@ -864,6 +814,51 @@ class DB_Model extends FHC_Model } } + /** + * Manage UDFs on update + */ + private function _manageUDFUpdate($id, &$data) + { + // Checks if this table has udf + if ($this->_hasUDF()) + { + $dbVersionArray = explode('.', $this->db->version()); + if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 4) // If postgresql version is <= 9.4 + { + $this->addSelect(DB_Model::UDF_FIELD_NAME); + $result = $this->load($id); + if (hasData($result)) + { + // Get udf values from $data & clean udf values from $data + // Must be performed here because the load method populates UDFs too + $this->_popUDF($data); + + $jsonb = (array)$result->retval[0]; // convert the result to an array + if (count($jsonb) > 0) // if UDFs are present + { + foreach($this->UDFs as $key => $val) + { + if (isset($jsonb[$key])) + { + $jsonb[$key] = $val; + } + } + + $jsonEncodedUDFs = json_encode($jsonb); // encode to json + if ($jsonEncodedUDFs !== false) + { + $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + } + } + } + } + else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 4) // If postgresql version is > 9.4 + { + // TODO: use jsonb_set + } + } + } + /** * Checks if the caller is entitled to perform this operation with this right */