Просмотр исходного кода

Ticket #270 - Update modules' DB classes.

AntonLV 10 лет назад
Родитель
Сommit
2b09f49ecc

+ 6 - 3
modules/base/notifications/classes/BxBaseModNotificationsDb.php

@@ -130,8 +130,11 @@ class BxBaseModNotificationsDb extends BxDolModuleDb
  				case 'by_group_key_type':
  				case 'by_group_key_type':
  					$aMethod['name'] = 'getAllWithKey';
  					$aMethod['name'] = 'getAllWithKey';
  					$aMethod['params'][1] = 'type';
  					$aMethod['params'][1] = 'type';
+ 					$aMethod['params'][2] = array(
+	                	'group' => $aParams['group']
+	                );
 
 
- 					$sWhereClause = $this->prepare("AND `group`=?", $aParams['group']);
+ 					$sWhereClause = "AND `group`=:group";
  					break;
  					break;
             }
             }
 
 
@@ -146,12 +149,12 @@ class BxBaseModNotificationsDb extends BxDolModuleDb
 
 
         $aSet = array();
         $aSet = array();
         foreach($aParamsSet as $sKey => $sValue)
         foreach($aParamsSet as $sKey => $sValue)
-           $aSet[] = $this->prepare("`" . $sKey . "`=?", $sValue);
+           $aSet[] = "`" . $sKey . "`=:" . $sKey;
 
 
 		if(!isset($aParamsSet['date']))
 		if(!isset($aParamsSet['date']))
 			$aSet[] = "`date`=UNIX_TIMESTAMP()";
 			$aSet[] = "`date`=UNIX_TIMESTAMP()";
 
 
-        if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet)) <= 0)
+        if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet), $aParamsSet) <= 0)
             return 0;
             return 0;
 
 
         return (int)$this->lastId();
         return (int)$this->lastId();

+ 25 - 9
modules/base/profile/classes/BxBaseModProfileDb.php

@@ -27,13 +27,21 @@ class BxBaseModProfileDb extends BxBaseModGeneralDb
 
 
     public function updateContentPictureById($iContentId, $iProfileId, $iPictureId, $sFieldPicture)
     public function updateContentPictureById($iContentId, $iProfileId, $iPictureId, $sFieldPicture)
     {
     {
+    	$aBindings = array(
+    		$sFieldPicture => $iPictureId,
+    		'id' => $iContentId
+    	);
+
         $sWhere = '';
         $sWhere = '';
-        if ($iProfileId)
-            $sWhere = $this->prepare (" AND `author` = ? ", $iProfileId);
+        if ($iProfileId) {
+        	$aBindings['author'] = $iProfileId;
+
+            $sWhere = " AND `author` = :author ";
+        }
 
 
-        $sQuery = $this->prepare ("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = ? WHERE `id` = ?", $iPictureId, $iContentId);
+        $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = :" . $sFieldPicture . " WHERE `id` = :id" . $sWhere;
 
 
-        return $this->res($sQuery . $sWhere);
+        return $this->query($sQuery, $aBindings);
     }
     }
 
 
     public function searchByTerm($sTerm, $iLimit)
     public function searchByTerm($sTerm, $iLimit)
@@ -41,14 +49,22 @@ class BxBaseModProfileDb extends BxBaseModGeneralDb
         if (!$this->_oConfig->CNF['FIELDS_QUICK_SEARCH'])
         if (!$this->_oConfig->CNF['FIELDS_QUICK_SEARCH'])
             return array();
             return array();
 
 
+		$aBindings = array(
+			'type' => $this->_oConfig->getName(),
+			'status' => BX_PROFILE_STATUS_ACTIVE
+		);
+
         $sWhere = '';
         $sWhere = '';
-        foreach ($this->_oConfig->CNF['FIELDS_QUICK_SEARCH'] as $sField)
-            $sWhere .= $this->prepare(" OR `c`.`$sField` LIKE ? ", '%' . $sTerm . '%');
+        foreach ($this->_oConfig->CNF['FIELDS_QUICK_SEARCH'] as $sField) {
+        	$aBindings[$sField] = '%' . $sTerm . '%';
+
+            $sWhere .= " OR `c`.`$sField` LIKE :" . $sField;
+        }
 
 
-        $sOrderBy = $this->prepare(" ORDER BY `added` DESC LIMIT ?", (int)$iLimit);
+        $sOrderBy = $this->prepareAsString(" ORDER BY `added` DESC LIMIT ?", (int)$iLimit);
 
 
-        $sQuery = $this->prepare("SELECT `c`.`id` AS `content_id`, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = ?) WHERE `p`.`status` = ?", $this->_oConfig->getName(), BX_PROFILE_STATUS_ACTIVE);
-        return $this->getAll($sQuery . " AND (0 $sWhere) " . $sOrderBy);
+        $sQuery = "SELECT `c`.`id` AS `content_id`, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :type) WHERE `p`.`status` = :status AND (0 $sWhere)" . $sOrderBy;
+        return $this->getAll($sQuery, $aBindings);
     }
     }
 }
 }
 
 

+ 14 - 6
modules/boonex/albums/classes/BxAlbumsDb.php

@@ -30,15 +30,23 @@ class BxAlbumsDb extends BxBaseModTextDb
 
 
     public function deassociateFileWithContent($iContentId, $iFileId)
     public function deassociateFileWithContent($iContentId, $iFileId)
     {
     {
+    	$aBindings = array();
+
         $sWhere = '';
         $sWhere = '';
-        if ($iContentId)
-            $sWhere .= $this->prepare (" AND `content_id` = ? ", $iContentId);
+        if ($iContentId) {
+        	$aBindings['content_id'] = $iContentId;
+
+            $sWhere .= " AND `content_id` = :content_id";
+        }
+
+        if ($iFileId) {
+        	$aBindings['file_id'] = $iFileId;
 
 
-        if ($iFileId)
-            $sWhere .= $this->prepare (" AND `file_id` = ? ", $iFileId);
+            $sWhere .= " AND `file_id` = :file_id";
+        }
 
 
-        $sQuery = "DELETE FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE 1 ";
-        return $this->query($sQuery . $sWhere);
+        $sQuery = "DELETE FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE 1 " . $sWhere;
+        return $this->query($sQuery, $aBindings);
     }
     }
 
 
     public function getFileTitle($iFileId)
     public function getFileTitle($iFileId)

+ 11 - 4
modules/boonex/convos/classes/BxCnvDb.php

@@ -73,12 +73,19 @@ class BxCnvDb extends BxBaseModTextDb
             return true;
             return true;
 
 
         // delete convo
         // delete convo
+        $aBindings = array(
+        	'conv_id' => $iConversationId
+        );
+
         $sWhere = '';
         $sWhere = '';
-        if ($iProfileId)
-            $sWhere = $this->prepare(" AND `collaborator` = ?", $iProfileId);
+        if ($iProfileId) {
+        	$aBindings['collaborator'] = $iProfileId;
+
+            $sWhere = " AND `collaborator` = :collaborator";
+        }
 
 
-        $sQuery = $this->prepare("DELETE FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ?", $iConversationId);
-        if (!$this->query($sQuery . $sWhere))
+        $sQuery = "DELETE FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = :conv_id" . $sWhere;
+        if (!$this->query($sQuery, $aBindings))
             return false;
             return false;
 
 
         // delete whole conversation if there is no refencences to the conversation in conv2folder table
         // delete whole conversation if there is no refencences to the conversation in conv2folder table

+ 20 - 11
modules/boonex/invites/classes/BxInvDb.php

@@ -31,25 +31,29 @@ class BxInvDb extends BxDolModuleDb
 
 
 	public function getInvites($aParams, $bReturnCount = false)
 	public function getInvites($aParams, $bReturnCount = false)
     {
     {
-    	$sMethod = 'getAll';
+    	$aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
 
 
         $sSelectClause = "`{$this->_sTableInvites}`.*";
         $sSelectClause = "`{$this->_sTableInvites}`.*";
 
 
         switch($aParams['type']) {
         switch($aParams['type']) {
             case 'count_by_account':
             case 'count_by_account':
-                $sMethod = 'getOne';
+            	$aMethod['name'] = 'getOne';
+                $aMethod['params'][1] = array(
+                	'account_id' => $aParams['value']
+                );
+
                 $sSelectClause = "COUNT(`{$this->_sTableInvites}`.`id`) AS `count`";
                 $sSelectClause = "COUNT(`{$this->_sTableInvites}`.`id`) AS `count`";
-                $sWhereClause = $this->prepare("AND `{$this->_sTableInvites}`.`account_id`=? ", $aParams['value']);
+                $sWhereClause = "AND `{$this->_sTableInvites}`.`account_id`=:account_id ";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
         }
         }
 
 
-        $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . " " . $sSelectClause . "
+        $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . " " . $sSelectClause . "
             FROM `{$this->_sTableInvites}` " . $sJoinClause . "
             FROM `{$this->_sTableInvites}` " . $sJoinClause . "
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
 
 
-        $aEntries = $this->$sMethod($sSql);
+        $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);;
         if(!$bReturnCount)
         if(!$bReturnCount)
         	return $aEntries;
         	return $aEntries;
 
 
@@ -64,30 +68,35 @@ class BxInvDb extends BxDolModuleDb
 
 
 	public function getRequests($aParams, $bReturnCount = false)
 	public function getRequests($aParams, $bReturnCount = false)
     {
     {
-    	$sMethod = 'getAll';
+    	$aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
 
 
         $sSelectClause = "`{$this->_sTableRequests}`.*";
         $sSelectClause = "`{$this->_sTableRequests}`.*";
 
 
         switch($aParams['type']) {
         switch($aParams['type']) {
         	case 'by_id':
         	case 'by_id':
-        		$sMethod = 'getRow';
-        		$sWhereClause = $this->prepare("AND `{$this->_sTableRequests}`.`id`=? ", $aParams['value']);
+        		$aMethod['name'] = 'getRow';
+                $aMethod['params'][1] = array(
+                	'id' => $aParams['value']
+                );
+
+        		$sWhereClause = "AND `{$this->_sTableRequests}`.`id`=:id ";
         		$sLimitClause = "LIMIT 1";
         		$sLimitClause = "LIMIT 1";
         		break;
         		break;
 
 
             case 'count_all':
             case 'count_all':
-                $sMethod = 'getOne';
+            	$aMethod['name'] = 'getOne';
+
                 $sSelectClause = "COUNT(`{$this->_sTableRequests}`.`id`) AS `count`";
                 $sSelectClause = "COUNT(`{$this->_sTableRequests}`.`id`) AS `count`";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
         }
         }
 
 
-        $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . " " . $sSelectClause . "
+        $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . " " . $sSelectClause . "
             FROM `{$this->_sTableRequests}` " . $sJoinClause . "
             FROM `{$this->_sTableRequests}` " . $sJoinClause . "
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
 
 
-        $aEntries = $this->$sMethod($sSql);
+        $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
         if(!$bReturnCount)
         if(!$bReturnCount)
         	return $aEntries;
         	return $aEntries;
 
 

+ 55 - 37
modules/boonex/sites/classes/BxSitesDb.php

@@ -51,32 +51,56 @@ class BxSitesDb extends BxDolModuleDb
 
 
     function getAccount($aParams)
     function getAccount($aParams)
     {
     {
-        $sMethod = 'getRow';
+        $aMethod = array('name' => 'getRow', 'params' => array(0 => 'query'));
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = '';
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = '';
+
         switch($aParams['type']) {
         switch($aParams['type']) {
             case 'id':
             case 'id':
-                $sWhereClause = $this->prepare("AND `ta`.`id`=?", $aParams['value']);
+            	$aMethod['params'][1] = array(
+                	'id' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `ta`.`id`=:id";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
             case 'owner_id':
             case 'owner_id':
-                $sWhereClause = $this->prepare("AND `ta`.`owner_id`=?", $aParams['value']);
+            	$aMethod['params'][1] = array(
+                	'owner_id' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `ta`.`owner_id`=:owner_id";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
+
             case 'token':
             case 'token':
-                $sWhereClause = $this->prepare("AND `tpd`.`token`=?", $aParams['value']);
+            	$aMethod['params'][1] = array(
+                	'token' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `tpd`.`token`=:token";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
+
             case 'profile_id':
             case 'profile_id':
-                $sWhereClause = $this->prepare("AND `tpd`.`profile_id`=?", $aParams['value']);
+            	$aMethod['params'][1] = array(
+                	'profile_id' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `tpd`.`profile_id`=:profile_id";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
+
             case 'profile_sid':
             case 'profile_sid':
-                $sWhereClause = $this->prepare("AND `tpd`.`profile_sid`=?", $aParams['value']);
+            	$aMethod['params'][1] = array(
+                	'profile_sid' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `tpd`.`profile_sid`=:profile_sid";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
         }
         }
 
 
-        $sSql = "SELECT
+        $aMethod['params'][0] = "SELECT
                 `ta`.`id`,
                 `ta`.`id`,
                 `ta`.`owner_id`,
                 `ta`.`owner_id`,
                 `to`.`trials` AS `owner_trials`,
                 `to`.`trials` AS `owner_trials`,
@@ -96,27 +120,37 @@ class BxSitesDb extends BxDolModuleDb
             LEFT JOIN `" . $this->_sPrefix . "payment_details` AS `tpd` ON `ta`.`id`=`tpd`.`account_id` " . $sJoinClause . "
             LEFT JOIN `" . $this->_sPrefix . "payment_details` AS `tpd` ON `ta`.`id`=`tpd`.`account_id` " . $sJoinClause . "
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
 
 
-        return $this->$sMethod($sSql);
+        return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
     }
     }
 
 
     function getPaymentHistory($aParams)
     function getPaymentHistory($aParams)
     {
     {
-        $sMethod = 'getAll';
+        $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = '';
         $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = '';
+
         switch($aParams['type']) {
         switch($aParams['type']) {
             case 'account_id':
             case 'account_id':
-                $sWhereClause = $this->prepare("AND `tph`.`account_id`=?", $aParams['value']);
+            	$aMethod['params'][1] = array(
+                	'account_id' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `tph`.`account_id`=:account_id";
                 $sOrderClause = "ORDER BY `tph`.`when` DESC";
                 $sOrderClause = "ORDER BY `tph`.`when` DESC";
                 break;
                 break;
+
             case 'account_id_last':
             case 'account_id_last':
-                $sMethod = "getRow";
-                $sWhereClause = $this->prepare("AND `tph`.`account_id`=?", $aParams['value']);
+                $aMethod['name'] = 'getRow';
+                $aMethod['params'][1] = array(
+                	'account_id' => $aParams['value']
+                );
+
+                $sWhereClause = "AND `tph`.`account_id`=:account_id";
                 $sOrderClause = "ORDER BY `tph`.`when` DESC";
                 $sOrderClause = "ORDER BY `tph`.`when` DESC";
                 $sLimitClause = "LIMIT 1";
                 $sLimitClause = "LIMIT 1";
                 break;
                 break;
         }
         }
 
 
-        $sSql = "SELECT
+        $aMethod['params'][0] = "SELECT
                 `tph`.`id` AS `id`,
                 `tph`.`id` AS `id`,
                 `tph`.`account_id` AS `account_id`,
                 `tph`.`account_id` AS `account_id`,
                 `tph`.`type` AS `type`,
                 `tph`.`type` AS `type`,
@@ -127,7 +161,7 @@ class BxSitesDb extends BxDolModuleDb
             FROM `" . $this->_sPrefix . "payment_history` AS `tph` " . $sJoinClause . "
             FROM `" . $this->_sPrefix . "payment_history` AS `tph` " . $sJoinClause . "
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
             WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
 
 
-        return $this->$sMethod($sSql);
+        return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
     }
     }
 
 
     function insertPaymentDetails($aValues)
     function insertPaymentDetails($aValues)
@@ -164,19 +198,14 @@ class BxSitesDb extends BxDolModuleDb
     {
     {
         $sWhere = '';
         $sWhere = '';
         foreach($aWhere as $sKey => $sValue)
         foreach($aWhere as $sKey => $sValue)
-            $sWhere .= $this->prepare(" AND `" . $sKey . "`=?", $sValue);
+            $sWhere .= " AND `" . $sKey . "`=:" . $sKey;
 
 
-        return (int)$this->query("SELECT `id` FROM `" . $this->_sPrefix . $sTable. "` WHERE 1" . $sWhere) > 0;
+        return (int)$this->query("SELECT `id` FROM `" . $this->_sPrefix . $sTable. "` WHERE 1" . $sWhere, $aWhere) > 0;
     }
     }
 
 
     protected function insert($sTable, $aValues)
     protected function insert($sTable, $aValues)
     {
     {
-        $sSql = "";
-        foreach($aValues as $sKey => $sValue)
-           $sSql .= $this->prepare("`" . $sKey . "`=?, ", $sValue);
-        $sSql = "INSERT INTO `" . $this->_sPrefix . $sTable. "` SET " . substr($sSql, 0, -2);
-
-        if((int)$this->query($sSql) > 0)
+        if((int)$this->query("INSERT INTO `" . $this->_sPrefix . $sTable. "` SET " . $this->arrayToSQL($aValues)) > 0)
             return (int)$this->lastId();
             return (int)$this->lastId();
 
 
         return 0;
         return 0;
@@ -184,25 +213,14 @@ class BxSitesDb extends BxDolModuleDb
 
 
     protected function update($sTable, $aUpdate, $aWhere)
     protected function update($sTable, $aUpdate, $aWhere)
     {
     {
-        $sUpdate = $sWhere = "";
-
-        foreach($aUpdate as $sKey => $sValue)
-            $sUpdate .= $this->prepare("`" . $sKey . "`=?, ", $sValue);
-        $sUpdate = substr($sUpdate, 0, -2);
-
-        foreach($aWhere as $sKey => $sValue)
-            $sWhere .= $this->prepare(" AND `" . $sKey . "`=?", $sValue);
-
-        return (int)$this->query("UPDATE `" . $this->_sPrefix . $sTable . "` SET " . $sUpdate . " WHERE 1" . $sWhere) > 0;
+        $sWhere = $this->arrayToSQL($aWhere, ' AND ');
+        return (int)$this->query("UPDATE `" . $this->_sPrefix . $sTable . "` SET " . $this->arrayToSQL($aUpdate) . " WHERE 1" . (!empty($sWhere) ? " AND " . $sWhere : "")) > 0;
     }
     }
 
 
     protected function delete($sTable, $aWhere)
     protected function delete($sTable, $aWhere)
     {
     {
-        $sWhere = "";
-        foreach($aWhere as $sKey => $sValue)
-            $sWhere .= $this->prepare(" AND `" . $sKey . "`=?", $sValue);
-
-        return (int)$this->query("DELETE FROM `" . $this->_sPrefix . $sTable . "` WHERE 1" . $sWhere) > 0;
+     	$sWhere = $this->arrayToSQL($aWhere, ' AND ');
+        return (int)$this->query("DELETE FROM `" . $this->_sPrefix . $sTable . "` WHERE 1" . (!empty($sWhere) ? " AND " . $sWhere : "")) > 0;
     }
     }
 }
 }
 
 

+ 1 - 1
modules/boonex/timeline/classes/BxTimelineDb.php

@@ -185,7 +185,7 @@ class BxTimelineDb extends BxBaseModNotificationsDb
 
 
         $sLimitAddon = '';
         $sLimitAddon = '';
         if($iOffset != 0)
         if($iOffset != 0)
-            $sLimitAddon = $this->prepare(" OFFSET ?", $iOffset);
+            $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
 
 
         $sQuery = $this->prepare("SELECT
         $sQuery = $this->prepare("SELECT
                  `tme`.`media_id` AS `id`
                  `tme`.`media_id` AS `id`