more work on Friendica db driver for b8

This commit is contained in:
friendica 2012-02-01 20:37:05 -08:00
parent 94e396e6c4
commit f57fa90228
2 changed files with 39 additions and 46 deletions

View file

@ -318,7 +318,7 @@ abstract class b8_storage_base
if($count_ham !== 0 or $count_spam !== 0) if($count_ham !== 0 or $count_spam !== 0)
$this->_update($token, "$count_ham $count_spam " . $this->b8_config['today'], $uid); $this->_update($token, "$count_ham $count_spam " . $this->b8_config['today'], $uid);
else else
$this->_del($token); $this->_del($token, $uid);
} }
@ -387,7 +387,7 @@ abstract class b8_storage_base
} }
# We're done and can commit all changes to the database now # We're done and can commit all changes to the database now
$this->_commit(); $this->_commit($uid);
} }

View file

@ -29,7 +29,7 @@
* @author Tobias Leupold * @author Tobias Leupold
*/ */
class b8_storage_mysql extends b8_storage_base class b8_storage_frndc extends b8_storage_base
{ {
public $config = array( public $config = array(
@ -50,6 +50,7 @@ class b8_storage_mysql extends b8_storage_base
private $_deletes = array(); private $_deletes = array();
private $_puts = array(); private $_puts = array();
private $_updates = array(); private $_updates = array();
private $uid = 0;
const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL'; const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL';
const DATABASE_CONNECTION_ERROR = 'DATABASE_CONNECTION_ERROR'; const DATABASE_CONNECTION_ERROR = 'DATABASE_CONNECTION_ERROR';
@ -146,6 +147,8 @@ class b8_storage_mysql extends b8_storage_base
public function connect() public function connect()
{ {
return TRUE;
# Are we already connected? # Are we already connected?
if($this->connected === TRUE) if($this->connected === TRUE)
return TRUE; return TRUE;
@ -207,7 +210,7 @@ class b8_storage_mysql extends b8_storage_base
* @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data. * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
*/ */
protected function _get_query($tokens) protected function _get_query($tokens, $uid)
{ {
# Construct the query ... # Construct the query ...
@ -217,7 +220,7 @@ class b8_storage_mysql extends b8_storage_base
$where = array(); $where = array();
foreach ($tokens as $token) { foreach ($tokens as $token) {
$token = mysql_real_escape_string($token, $this->_connection); $token = dbesc($token);
array_push($where, $token); array_push($where, $token);
} }
@ -225,26 +228,18 @@ class b8_storage_mysql extends b8_storage_base
} }
else { else {
$token = mysql_real_escape_string($token, $this->_connection); $token = dbesc($token);
$where = 'token = "' . $token . '"'; $where = 'token = "' . $token . '"';
} }
# ... and fetch the data # ... and fetch the data
$result = mysql_query(' $result = q('
SELECT token, count SELECT token, count
FROM ' . $this->config['table_name'] . ' FROM ' . $this->config['table_name'] . '
WHERE ' . $where . '; WHERE ' . $where . ' AND uid = ' . $uid );
', $this->_connection);
$data = array(); return $result;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$data[$row['token']] = $row['count'];
mysql_free_result($result);
return $data;
} }
@ -257,10 +252,11 @@ class b8_storage_mysql extends b8_storage_base
* @return void * @return void
*/ */
protected function _put($token, $count) { protected function _put($token, $count, $uid) {
$token = mysql_real_escape_string($token, $this->_connection); $token = dbesc($token);
$count = mysql_real_escape_string($count, $this->_connection);; $count = dbesc($count);
array_push($this->_puts, '("' . $token . '", "' . $count . '")'); $uid = dbesc($uid);
array_push($this->_puts, '("' . $token . '", "' . $count . '", '"' . $uid .'")');
} }
/** /**
@ -272,11 +268,12 @@ class b8_storage_mysql extends b8_storage_base
* @return void * @return void
*/ */
protected function _update($token, $count) protected function _update($token, $count, $uid)
{ {
$token = mysql_real_escape_string($token, $this->_connection); $token = dbesc($token);
$count = mysql_real_escape_string($count, $this->_connection); $count = dbesc($count);
array_push($this->_updates, '("' . $token . '", "' . $count . '")'); $uid = dbesc($uid);
array_push($this->_puts, '("' . $token . '", "' . $count . '", '"' . $uid .'")');
} }
/** /**
@ -287,9 +284,11 @@ class b8_storage_mysql extends b8_storage_base
* @return void * @return void
*/ */
protected function _del($token) protected function _del($token, $uid)
{ {
$token = mysql_real_escape_string($token, $this->_connection); $token = dbesc($token);
$uid = dbesc($uid);
$this->uid = $uid;
array_push($this->_deletes, $token); array_push($this->_deletes, $token);
} }
@ -300,18 +299,14 @@ class b8_storage_mysql extends b8_storage_base
* @return void * @return void
*/ */
protected function _commit() protected function _commit($uid)
{ {
if(count($this->_deletes) > 0) { if(count($this->_deletes) > 0) {
$result = mysql_query(' $result = q('
DELETE FROM ' . $this->config['table_name'] . ' DELETE FROM ' . $this->config['table_name'] . '
WHERE token IN ("' . implode('", "', $this->_deletes) . '"); WHERE token IN ("' . implode('", "', $this->_deletes) . '") AND uid = ' . $this->uid);
', $this->_connection);
if(is_resource($result) === TRUE)
mysql_free_result($result);
$this->_deletes = array(); $this->_deletes = array();
@ -319,12 +314,9 @@ class b8_storage_mysql extends b8_storage_base
if(count($this->_puts) > 0) { if(count($this->_puts) > 0) {
$result = mysql_query(' $result = q('
INSERT INTO ' . $this->config['table_name'] . '(token, count) INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
VALUES ' . implode(', ', $this->_puts) . ';', $this->_connection); VALUES ' . implode(', ', $this->_puts));
if(is_resource($result) === TRUE)
mysql_free_result($result);
$this->_puts = array(); $this->_puts = array();
@ -332,13 +324,14 @@ class b8_storage_mysql extends b8_storage_base
if(count($this->_updates) > 0) { if(count($this->_updates) > 0) {
$result = mysql_query(' // this still needs work
INSERT INTO ' . $this->config['table_name'] . '(token, count) $result = q("select * from " . $this->config['table_name'] . ' where token = ';
VALUES ' . implode(', ', $this->_updates) . '
ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
if(is_resource($result) === TRUE)
mysql_free_result($result); $result = q('
INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
VALUES ' . implode(', ', $this->_updates) . ', ' . $uid . '
ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
$this->_updates = array(); $this->_updates = array();