Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Various fixes related to PostgreSQL. #1131

Merged
merged 3 commits into from

4 participants

@gpongelli

Various fixes reported from this pull request that correct errors coming with PostgreSQL database.
Added insertObject's test.

Eng. Gabriele Pongelli.

libraries/joomla/database/driver.php
@@ -832,12 +833,15 @@ public function insertObject($table, &$object, $key = null)
// Prepare and sanitize the fields and values for the database query.
$fields[] = $this->quoteName($k);
- $values[] = $this->quote($v);
+ $values[] = is_numeric($v) ? $v : $this->quote($v);

I'm not sure this change is wise. I seem to remember some databases require quoting of numeric values if the column is a string type (DB2 maybe?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@LouisLandry LouisLandry merged commit d8e0299 into from
@mbabker

This change broke extension installation in the CMS. There is no $key variable defined which causes a "Undefined variable: key in libraries/joomla/installer/adapters/component.php on line 483" message.

I know, I'm waiting other fixes and improvement to add them here, including this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
11 libraries/joomla/database/driver.php
@@ -812,9 +812,6 @@ public function insertObject($table, &$object, $key = null)
$fields = array();
$values = array();
- // Create the base insert statement.
- $statement = 'INSERT INTO ' . $this->quoteName($table) . ' (%s) VALUES (%s)';
-
// Iterate over the object variables to build the query fields and values.
foreach (get_object_vars($object) as $k => $v)
{
@@ -835,8 +832,14 @@ public function insertObject($table, &$object, $key = null)
$values[] = $this->quote($v);
}
+ // Create the base insert statement.
+ $query = $this->getQuery(true);
+ $query->insert($this->quoteName($table))
+ ->columns($fields)
+ ->values(implode(',', $values));
+
// Set the query and execute the insert.
- $this->setQuery(sprintf($statement, implode(',', $fields), implode(',', $values)));
+ $this->setQuery($query);
if (!$this->execute())
{
return false;
View
47 libraries/joomla/database/driver/postgresql.php
@@ -242,7 +242,7 @@ public function getNumRows( $cur = null )
{
$this->connect();
- return pg_num_rows($cur ? $cur : $this->cursor);
+ return pg_num_rows((int) $cur ? $cur : $this->cursor);
}
/**
@@ -889,10 +889,6 @@ public function insertObject($table, &$object, $key = null)
$fields = array();
$values = array();
- // Create the base insert statement.
- $query = $this->getQuery(true);
- $query->insert($this->quoteName($table));
-
// Iterate over the object variables to build the query fields and values.
foreach (get_object_vars($object) as $k => $v)
{
@@ -913,24 +909,41 @@ public function insertObject($table, &$object, $key = null)
$values[] = is_numeric($v) ? $v : $this->quote($v);
}
- $query->columns($fields);
- $query->values(implode(',', $values));
+ // Create the base insert statement.
+ $query = $this->getQuery(true);
+
+ $query->insert($this->quoteName($table))
+ ->columns($fields)
+ ->values(implode(',', $values));
- // Set the query and execute the insert.
- $this->setQuery($query);
- if (!$this->execute())
+ $retVal = false;
+
+ if ($key)
{
- return false;
- }
+ $query->returning($key);
+
+ // Set the query and execute the insert.
+ $this->setQuery($query);
- // Update the primary key if it exists.
- $id = $this->insertid();
- if ($key && $id)
+ $id = $this->loadResult();
+ if ($id)
+ {
+ $object->$key = $id;
+ $retVal = true;
+ }
+ }
+ else
{
- $object->$key = $id;
+ // Set the query and execute the insert.
+ $this->setQuery($query);
+
+ if ($this->execute())
+ {
+ $retVal = true;
+ }
}
- return true;
+ return $retVal;
}
/**
View
10 libraries/joomla/installer/adapters/component.php
@@ -480,11 +480,11 @@ public function install()
return false;
}
- $eid = $db->insertid();
+ $eid = $row->$key;
// Clobber any possible pending updates
$update = JTable::getInstance('update');
- $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => '', 'folder' => ''));
+ $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => 1, 'folder' => ''));
if ($uid)
{
@@ -877,7 +877,7 @@ public function update()
// Clobber any possible pending updates
$update = JTable::getInstance('update');
- $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => '', 'folder' => ''));
+ $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => 1, 'folder' => ''));
if ($uid)
{
@@ -1135,7 +1135,7 @@ public function uninstall($id)
// Clobber any possible pending updates
$update = JTable::getInstance('update');
- $uid = $update->find(array('element' => $row->element, 'type' => 'component', 'client_id' => '', 'folder' => ''));
+ $uid = $update->find(array('element' => $row->element, 'type' => 'component', 'client_id' => 1, 'folder' => ''));
if ($uid)
{
@@ -1724,7 +1724,7 @@ public function discover_install()
// Clobber any possible pending updates
$update = JTable::getInstance('update');
- $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => '', 'folder' => ''));
+ $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => 1, 'folder' => ''));
if ($uid)
{
View
3  libraries/joomla/installer/adapters/file.php
@@ -273,9 +273,6 @@ public function install()
return false;
}
- // Set the insert id
- $row->set('extension_id', $db->insertid());
-
// Since we have created a module item, we add it to the installation step stack
// so that if we have to rollback the changes we can undo it.
$this->parent->pushStep(array('type' => 'extension', 'extension_id' => $row->extension_id));
View
3  libraries/joomla/installer/adapters/module.php
@@ -457,9 +457,6 @@ public function install()
return false;
}
- // Set the insert id
- $row->extension_id = $db->insertid();
-
// Since we have created a module item, we add it to the installation step stack
// so that if we have to rollback the changes we can undo it.
$this->parent->pushStep(array('type' => 'extension', 'extension_id' => $row->extension_id));
View
38 libraries/joomla/installer/adapters/template.php
@@ -282,25 +282,28 @@ public function install()
if ($this->route == 'install')
{
- // Insert record in #__template_styles
- $query = $db->getQuery(true);
- $query->insert($db->quoteName('#__template_styles'));
$debug = $lang->setDebug(false);
+
$columns = array($db->quoteName('template'),
$db->quoteName('client_id'),
$db->quoteName('home'),
$db->quoteName('title'),
$db->quoteName('params')
);
- $query->columns($columns);
- $query->values(
- $db->Quote($row->element)
- . ',' . $db->Quote($clientId)
- . ',' . $db->Quote(0)
- . ',' . $db->Quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', JText::_($this->get('name'))))
- . ',' . $db->Quote($row->params)
- );
+
+ $values = array(
+ $db->Quote($row->element), $clientId, $db->Quote(0),
+ $db->Quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', JText::_($this->get('name')))),
+ $db->Quote($row->params) );
+
$lang->setDebug($debug);
+
+ // Insert record in #__template_styles
+ $query = $db->getQuery(true);
+ $query->insert($db->quoteName('#__template_styles'))
+ ->columns($columns)
+ ->values(implode(',', $values));
+
$db->setQuery($query);
// There is a chance this could fail but we don't care...
@@ -366,7 +369,7 @@ public function uninstall($id)
// Deny remove default template
$db = $this->parent->getDbo();
- $query = 'SELECT COUNT(*) FROM #__template_styles' . ' WHERE home = 1 AND template = ' . $db->Quote($name);
+ $query = "SELECT COUNT(*) FROM #__template_styles WHERE home = '1' AND template = " . $db->Quote($name);
$db->setQuery($query);
if ($db->loadResult() != 0)
@@ -418,13 +421,16 @@ public function uninstall($id)
}
// Set menu that assigned to the template back to default template
- $query = 'UPDATE #__menu INNER JOIN #__template_styles' . ' ON #__template_styles.id = #__menu.template_style_id'
- . ' SET #__menu.template_style_id = 0' . ' WHERE #__template_styles.template = ' . $db->Quote(strtolower($name))
- . ' AND #__template_styles.client_id = ' . $db->Quote($clientId);
+ $query = 'UPDATE #__menu'
+ . ' SET template_style_id = 0'
+ . ' WHERE template_style_id in ('
+ . ' SELECT s.id FROM #__template_styles s'
+ . ' WHERE s.template = ' . $db->Quote(strtolower($name)) . ' AND s.client_id = ' . $clientId . ')';
+
$db->setQuery($query);
$db->execute();
- $query = 'DELETE FROM #__template_styles' . ' WHERE template = ' . $db->Quote($name) . ' AND client_id = ' . $db->Quote($clientId);
+ $query = 'DELETE FROM #__template_styles WHERE template = ' . $db->Quote($name) . ' AND client_id = ' . $clientId;
$db->setQuery($query);
$db->execute();
View
14 libraries/joomla/table/user.php
@@ -162,6 +162,12 @@ public function bind($array, $ignore = '')
*/
public function check()
{
+ // Set user id to null istead of 0, if needed
+ if ($this->id === 0)
+ {
+ $this->id = null;
+ }
+
// Validate user information
if (trim($this->name) == '')
{
@@ -188,11 +194,17 @@ public function check()
}
// Set the registration timestamp
- if ($this->registerDate == null || $this->registerDate == $this->_db->getNullDate())
+ if (empty($this->registerDate) || $this->registerDate == $this->_db->getNullDate())
{
$this->registerDate = JFactory::getDate()->toSql();
}
+ // Set the lastvisitDate timestamp
+ if (empty($this->lastvisitDate))
+ {
+ $this->lastvisitDate = $this->_db->getNullDate();
+ }
+
// Check for existing username
$query = $this->_db->getQuery(true);
$query->select($this->_db->quoteName('id'));
View
37 tests/suites/database/driver/postgresql/JDatabasePostgresqlTest.php
@@ -547,14 +547,43 @@ public function testInsertid()
/**
* Test insertObject function
*
- * @todo Implement testInsertObject().
- *
* @return void
+ *
+ * @since 12.1
*/
public function testInsertObject()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
+ self::$driver->setQuery('TRUNCATE TABLE "jos_dbtest" RESTART IDENTITY');
+ $result = self::$driver->execute();
+
+ $tst = new JObject;
+ $tst->title = "PostgreSQL test insertObject";
+ $tst->start_date = '2012-04-07 15:00:00';
+ $tst->description = "Test insertObject";
+
+ // Insert object without retrieving key
+ $ret = self::$driver->insertObject('#__dbtest', $tst);
+
+ $checkQuery = self::$driver->getQuery(true);
+ $checkQuery->select('COUNT(*)')
+ ->from('#__dbtest')
+ ->where('start_date = \'2012-04-07 15:00:00\'', 'AND')
+ ->where('description = \'Test insertObject\'')
+ ->where('title = \'PostgreSQL test insertObject\'');
+ self::$driver->setQuery($checkQuery);
+
+ $this->assertThat(self::$driver->loadResult(), $this->equalTo(1), __LINE__);
+ $this->assertThat($ret, $this->equalTo(true), __LINE__);
+
+ // Insert object retrieving the key
+ $tstK = new JObject;
+ $tstK->title = "PostgreSQL test insertObject with key";
+ $tstK->start_date = '2012-04-07 15:00:00';
+ $tstK->description = "Test insertObject with key";
+ $retK = self::$driver->insertObject('#__dbtest', $tstK, 'id');
+
+ $this->assertThat($tstK->id, $this->equalTo(2), __LINE__);
+ $this->assertThat($retK, $this->equalTo(true), __LINE__);
}
/**
Something went wrong with that request. Please try again.