/
Improvements to kDBConnection class (iterator, kDBConnectionDebug class)

Improvements to kDBConnection class (iterator, kDBConnectionDebug class)

Imported From: http://groups.google.com/group/in-portal-dev/browse_thread/thread/0ea7299e105bf11a#

In-Portal has nice kDBConnection class, that we use in every day operations
that require database interaction.

Among all it has following most important methods, that you can use:

  • Query - runs sql statement and returns 2-dimensional array with a results
  • GetRow - runs sql statement and returns first row only as 1-dimensional array
  • GetCol - runs sql statement and returns first field from each resulting row
  • GetOne - runs sql statement and returns first column from first row as a string

All of above functions work very well when you don't have thousands rows in database tables you operate.

In case if you have big tables, then idea to create an array of all selected rows inside kDBConnection::Query method (memory allocation #1) and then returning it to calling function (memory allocation #2) isn't a good
idea since you get 2x memory allocation as you would normally get when using code from PHP documentation:

$rs = mysql_query($sql);

while ( $row = mysql_fetch_array($rs) ) {
	...
}
 
mysql_free_result($rs);

To solve this problem I created kDBConnection::GetIterator method, that allocates memory only where you actually iterate the data you get from Query method:

$rows = $this->Conn->GetIterator($sql);
$row_count = count($rows);

foreach ($rows as $index => $row) {
	...
}

I also created kDBConnectionDebug class, which is kDBConnection class descendant with Query & GetIterator methods overridden to collect additional statistics while in debug mode. Before kDBConnectionDebug class Query method always called _debugQuery method in first lines of Query method, which was slowing whole DB querying process a bit.

db_connection_iterator.patch

Related Tasks

Error rendering macro 'jira' : Unable to locate Jira server for this macro. It may be due to Application Link configuration.