/** * @covers \Cougar\Util\Arrays::renameKeys * @depends testRenameKeys */ public function testRenameKeysPerformance() { // Create an array with 50,000 elements $n = 50000; $array = array(); for ($i = 0; $i < $n; $i++) { $array[] = array("record_id" => 1, "first_name" => "Peter", "LAST_NAME" => "Stevens", "age" => 45); } // Define the key map $key_map = array("record_id" => "id", "first_name" => "firstName", "LAST_NAME" => "lastName", "age" => "age"); // Start the timer $start_time = microtime(true); // Rename the keys $new_data = Arrays::renameKeys($array, $key_map); // Stop the timer $stop_time = microtime(true); // Make sure the rename took less than one second $this->assertLessThan(1, $stop_time - $start_time); }
/** * Returns a list of records with the given values * * @history * 2013.09.30: * (AT) Initial release * 2013.10.25: * (AT) Improve thrown exceptions * 2013.11.25: * (AT) Add support for _limit and _offset query parameters * (AT) Set default limit to 10,000 rows * 2014.02.27: * (AT) Fix bad logic when setting limit and offset on OCI * 2014.03.04: * (AT) Split function into a query-generating function and a query * execution part to make it easier to extend query functionality * 2014.03.05: * (AT) Handle queryUnique flag * 2014.03.06: * (AT) Rename array keys when using the OCI driver * 2014.03.18: * (AT) Make sure the method is still persistent * * @version 2014.03.18 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param array $parameters * List of query parameters * @param string $class_name * Use array to return list as an array, or class name to return objects * @param array $ctorargs * Constructor arguments if returning objects * @return array Record list * @throws \Cougar\Exceptions\Exception; * @throws \Cougar\Exceptions\AccessDeniedException; */ public function query(array $parameters = array(), $class_name = "array", array $ctorargs = array()) { # Make sure we are still persistent if (!$this->__persistent) { throw new Exception("Querying is no longer allowed on this model"); } # See if querying is allowed if (!$this->__allowQuery) { throw new AccessDeniedException("This model does not support querying"); } # Set the view (if it hasn't been changed) if ($this->__currentView == "__default__" && $this->__queryView !== "__default__") { $this->__setView($this->__queryView); } # Extract the columns and aliases for the columns we can query $query_aliases = array_intersect($this->__alias, $this->__queryProperties); $columns = array(); $key_map = array(); foreach ($this->__queryProperties as $property) { if ($this->__visible[$property]) { if ($this->__exportAlias[$property] == $this->__columnMap[$property]) { $columns[$property] = $this->__columnMap[$property]; $key_map[$this->__columnMap[$property]] = $this->__columnMap[$property]; } else { $columns[$property] = $this->__columnMap[$property] . " AS " . $this->__exportAlias[$property]; $key_map[$this->__exportAlias[$property]] = $this->__exportAlias[$property]; } } } # Recursively iterate through the query parameters $this->iterateQueryParameters($parameters, $query_aliases, $columns, $key_map); # Prepare the array that will hold the parameter values $values = array(); # Set the default limit to 10,000 rows $limit = 10000; $offset = 0; $used_parameters = array(); # Prepare the query and execute the statement if ($this->__queryUnique) { $query = "SELECT DISTINCT "; } else { $query = "SELECT "; } $query .= implode(", ", $columns) . " FROM " . $this->__table . " " . implode(" ", $this->__joins); $where_clause = QueryParameter::toSql($parameters, $this->__columnMap, $query_aliases, $this->__caseInsensitive, $values, $used_parameters, $limit, $offset); if ($where_clause) { $query .= " WHERE " . $where_clause; } # Set the limit and offset $limit = (int) $limit; $offset = (int) $offset; if ($this->__pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == "oci") { if ($where_clause) { $query .= " AND "; } else { $query .= " WHERE "; } $query .= "ROWNUM > " . $offset . " AND ROWNUM <= " . ($offset + $limit); } else { $query .= " LIMIT " . $limit . " OFFSET " . $offset; } # Execute the query $results = $this->executeQuery($query, $values, $class_name, $ctorargs); # Oracle will turn all column names as uppercase. This will rename them # if we are returning an array and are using OCI if ($class_name == "array" && count($results) > 0 && $this->__pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == "oci") { # Change the keys in the key map to uppercase $key_map = array_change_key_case($key_map, CASE_UPPER); # Rename the keys $results = Arrays::renameKeys($results, $key_map); } # Return the result return $results; }