Browse Source

继续修订一些MySQL操作代码的语法

王刚 3 năm trước cách đây
mục cha
commit
7fbf471fc6
1 tập tin đã thay đổi với 127 bổ sung69 xóa
  1. 127 69
      Gameserver/Amfphp/util/dao.php

+ 127 - 69
Gameserver/Amfphp/util/dao.php

@@ -199,10 +199,12 @@ class dao {
      * @return object|bool
      */
     private function connectByPDO($params) {
-        if (!isset($params->driver))
+        if (!isset($params->driver)) {
             self::triggerError('no pdo driver defined, it should be mysql or sqlite', __FILE__, __LINE__, $exit = true);
-        if (!isset($params->user))
+        }
+        if (!isset($params->user)) {
             return false;
+        }
         if ($params->driver == 'mysql') {
             $dsn = "mysql:host={$params->host}; port={$params->port}; dbname={$params->name}";
         }
@@ -211,19 +213,24 @@ class dao {
             $dbh->exec("SET NAMES {$params->encoding}");
 
             /* If run on linux, set emulatePrepare and bufferQuery to true. */
-            if (!isset($params->emulatePrepare) and PHP_OS == 'Linux')
+            if (!isset($params->emulatePrepare) and PHP_OS == 'Linux') {
                 $params->emulatePrepare = true;
-            if (!isset($params->bufferQuery) and PHP_OS == 'Linux')
+            }
+            if (!isset($params->bufferQuery) and PHP_OS == 'Linux') {
                 $params->bufferQuery = true;
+            }
 
             $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
             $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-            if (isset($params->strictMode) and $params->strictMode == false)
+            if (isset($params->strictMode) and $params->strictMode == false) {
                 $dbh->exec("SET @@sql_mode= ''");
-            if (isset($params->emulatePrepare))
+            }
+            if (isset($params->emulatePrepare)) {
                 $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, $params->emulatePrepare);
-            if (isset($params->bufferQuery))
+            }
+            if (isset($params->bufferQuery)) {
                 $dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $params->bufferQuery);
+            }
 
             return $dbh;
         } catch (PDOException $exception) {
@@ -333,10 +340,12 @@ class dao {
         $subLength = strlen($sql);
         $orderPOS = strripos($sql, DAO::ORDERBY);
         $limitPOS = strripos($sql, DAO::LIMIT);
-        if ($limitPOS)
+        if ($limitPOS) {
             $subLength = $limitPOS;
-        if ($orderPOS)
+        }
+        if ($orderPOS) {
             $subLength = $orderPOS;
+        }
         $sql = substr($sql, 0, $subLength);
         self::$querys[] = $sql;
 
@@ -421,8 +430,9 @@ class dao {
      */
     public function from($table) {
         $this->setTable($table);
-        if ($this->mode == 'raw')
+        if ($this->mode == 'raw') {
             $this->sqlobj->from($table);
+        }
         return $this;
     }
 
@@ -446,8 +456,9 @@ class dao {
      * @return object the dao object self.
      */
     public function alias($alias) {
-        if (empty($this->alias))
+        if (empty($this->alias)) {
             $this->setAlias($alias);
+        }
         $this->sqlobj->alias($alias);
         return $this;
     }
@@ -460,8 +471,9 @@ class dao {
      * @return object the dao object self.
      */
     public function data($data) {
-        if (!is_object($data))
+        if (!is_object($data)) {
             $data = (object) $data;
+        }
         $this->sqlobj->data($data);
         return $this;
     }
@@ -499,10 +511,12 @@ class dao {
 
         /* If the mode is magic, process the $fields and $table. */
         if ($this->mode == 'magic') {
-            if ($this->fields == '')
+            if ($this->fields == '') {
                 $this->fields = '*';
-            if ($this->table == '')
+            }
+            if ($this->table == '') {
                 $this->triggerError('Must set the table name', __FILE__, __LINE__, $exit = true);
+            }
             $sql = sprintf($this->sqlobj->get(), $this->fields, $this->table);
         }
 
@@ -522,11 +536,10 @@ class dao {
      */
     private function triggerError($message, $file, $line, $exit = false) {
         /* Set the error info. */
-        $log = "ERROR: " . CommUtil::str2UTF8($message) . " in "
-                . CommUtil::str2UTF8($file) . " on line $line";
-        if (isset($_SERVER['SCRIPT_URI']))
+        $log = "ERROR: " . CommUtil::str2UTF8($message) . " in " . CommUtil::str2UTF8($file) . " on line $line";
+        if (isset($_SERVER['SCRIPT_URI'])) {
             $log .= ", request: $_SERVER[SCRIPT_URI]";
-        ;
+        }
         $trace = debug_backtrace();
         extract($trace[0]);                        # function
         extract($trace[1]);                        # line
@@ -607,8 +620,9 @@ class dao {
      * @return object the dao object self.
      */
     public function page($pager) {
-        if (!is_object($pager))
+        if (!is_object($pager)) {
             return $this;
+        }
 
         /* If the record total is 0, compute it. */
         if ($pager->recTotal == 0) {
@@ -623,10 +637,12 @@ class dao {
             $subLength = strlen($sql);
             $orderPOS = strripos($sql, 'order ');
             $limitPOS = strripos($sql, 'limit');
-            if ($limitPOS)
+            if ($limitPOS) {
                 $subLength = $limitPOS;
-            if ($orderPOS)
+            }
+            if ($orderPOS) {
                 $subLength = $orderPOS;
+            }
             $sql = substr($sql, 0, $subLength);
             self::$querys[] = $sql;
 
@@ -662,8 +678,9 @@ class dao {
      * @return int the modified or deleted records.
      */
     public function exec($sql = '') {
-        if (!empty(dao::$errors))
+        if (!empty(dao::$errors)) {
             return new PDOStatement();   // If any error, return an empty statement object to make sure the remain method to execute.
+        }
         if ($sql) {
             if (is_null($this->sqlobj)) {
                 $this->sqlobj = sql::factory();
@@ -691,12 +708,15 @@ class dao {
      * @return object|mixed
      */
     public function fetch($field = '') {
-        if (empty($field))
+        if (empty($field)) {
             return $this->query()->fetch();
+        }
         $this->setFields($field);
         $result = $this->query()->fetch(PDO::FETCH_OBJ);
-        if ($result)
+        if ($result) {
             return $result->$field;
+        }
+        return false;
     }
 
     /**
@@ -710,11 +730,13 @@ class dao {
 
         $stmt = $this->query();
 
-        if (empty($keyField))
+        if (empty($keyField)) {
             return $stmt->fetchAll();
+        }
         $rows = array();
-        while ($row = $stmt->fetch())
+        while ($row = $stmt->fetch()) {
             $rows[$row->$keyField] = $row;
+        }
         return $rows;
     }
 
@@ -751,8 +773,9 @@ class dao {
         $stmt = $this->query();
         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
             if (!$ready) {
-                if (empty($keyField))
+                if (empty($keyField)) {
                     $keyField = key($row);
+                }
                 if (empty($valueField)) {
                     end($row);
                     $valueField = key($row);
@@ -812,8 +835,9 @@ class dao {
             while ($row = $stmt->fetch()) {
                 $key ? $rows[$row->$key] = $row : $rows[] = $row;
                 $i++;
-                if ($i == $max)
+                if ($i == $max) {
                     break;
+                }
             }
             return $rows;
         }
@@ -839,9 +863,9 @@ class dao {
      */
     public function check($fieldName, $funcName) {
         /* If no this field in the data, reuturn. */
-        if (!isset($this->sqlobj->data->$fieldName))
+        if (!isset($this->sqlobj->data->$fieldName)) {
             return $this;
-
+        }
         /* Set the field label and value. */
         global $lang, $config, $app;
         $table = strtolower(str_replace(array($config->db->prefix, '`'), '', $this->table));
@@ -852,12 +876,14 @@ class dao {
         if ($funcName == 'unique') {
             $args = func_get_args();
             $sql = "SELECT COUNT(*) AS count FROM $this->table WHERE `$fieldName` = " . $this->sqlobj->quote($value);
-            if (isset($args[2]))
+            if (isset($args[2])) {
                 $sql .= ' AND ' . $args[2];
+            }
             try {
                 $row = $this->dbh->query($sql)->fetch();
-                if ($row->count != 0)
+                if ($row->count != 0) {
                     $this->logError($funcName, $fieldName, $fieldLabel, array($value));
+                }
             } catch (PDOException $e) {
                 $this->sqlError($e);
             }
@@ -889,8 +915,9 @@ class dao {
      * @return object the dao object self.
      */
     public function checkIF($condition, $fieldName, $funcName) {
-        if (!$condition)
+        if (!$condition) {
             return $this;
+        }
         $funcArgs = func_get_args();
         for ($i = 0; $i < VALIDATER::MAX_ARGS; $i++) {
             ${"arg$i"} = isset($funcArgs[$i + 3]) ? $funcArgs[$i + 3] : null;
@@ -913,8 +940,9 @@ class dao {
         for ($i = 0; $i < VALIDATER::MAX_ARGS; $i++) {
             ${"arg$i"} = isset($funcArgs[$i + 2]) ? $funcArgs[$i + 2] : null;
         }
-        foreach ($fields as $fieldName)
+        foreach ($fields as $fieldName) {
             $this->check($fieldName, $funcName, $arg0, $arg1, $arg2);
+        }
         return $this;
     }
 
@@ -928,15 +956,17 @@ class dao {
      * @return object the dao object self.
      */
     public function batchCheckIF($condition, $fields, $funcName) {
-        if (!$condition)
+        if (!$condition) {
             return $this;
+        }
         $fields = explode(',', str_replace(' ', '', $fields));
         $funcArgs = func_get_args();
         for ($i = 0; $i < VALIDATER::MAX_ARGS; $i++) {
             ${"arg$i"} = isset($funcArgs[$i + 2]) ? $funcArgs[$i + 2] : null;
         }
-        foreach ($fields as $fieldName)
+        foreach ($fields as $fieldName) {
             $this->check($fieldName, $funcName, $arg0, $arg1, $arg2);
+        }
         return $this;
     }
 
@@ -952,15 +982,19 @@ class dao {
         $skipFields = ",$skipFields,";
 
         foreach ($fields as $fieldName => $validater) {
-            if (strpos($skipFields, $fieldName) !== false)
+            if (strpos($skipFields, $fieldName) !== false) {
                 continue; // skip it.
-            if (!isset($this->sqlobj->data->$fieldName))
+            }
+            if (!isset($this->sqlobj->data->$fieldName)) {
                 continue;
-            if ($validater['rule'] == 'skip')
+            }
+            if ($validater['rule'] == 'skip') {
                 continue;
+            }
             $options = array();
-            if (isset($validater['options']))
+            if (isset($validater['options'])) {
                 $options = array_values($validater['options']);
+            }
             for ($i = 0; $i < VALIDATER::MAX_ARGS; $i++) {
                 ${"arg$i"} = isset($options[$i]) ? $options[$i] : null;
             }
@@ -990,7 +1024,7 @@ class dao {
             return false;
         }
         // Result is either boolean FALSE (no table found) or PDOStatement Object (table found) 
-        return $ret !== FALSE;      # false代表表不存在
+        return $ret !== FALSE;                                                  # false代表表不存在
     }
 
     public function Ping() {
@@ -1025,16 +1059,18 @@ class dao {
         if (!is_array($error)) {
             foreach ($replaces as $replace) {
                 $pos = strpos($error, '%s');
-                if ($pos === false)
+                if ($pos === false) {
                     break;
+                }
                 $error = substr($error, 0, $pos) . $replace . substr($error, $pos + 2);
             }
-        }
-        /* If the error define is an array, select the one which %s counts match the $replaces.  */ else {
+        } else {/* If the error define is an array, select the one which %s counts match the $replaces.  */
             /* Remove the empty items. */
-            foreach ($replaces as $key => $value)
-                if (is_null($value))
+            foreach ($replaces as $key => $value) {
+                if (is_null($value)) {
                     unset($replaces[$key]);
+                }
+            }
             $replacesCount = count($replaces);
             foreach ($error as $errorString) {
                 if (substr_count($errorString, '%s') == $replacesCount) {
@@ -1064,11 +1100,11 @@ class dao {
      */
     public static function getError($join = false) {
         $errors = dao::$errors;
-        dao::$errors = array();     // Must clear it.
+        dao::$errors = array();                                                 # Must clear it.
 
-        if (!$join)
+        if (!$join) {
             return $errors;
-
+        }
         if (is_array($errors)) {
             $message = '';
             foreach ($errors as $item) {
@@ -1453,8 +1489,9 @@ class sql {
      * @return object the sql object.
      */
     public function where($arg1, $arg2 = null, $arg3 = null) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         if ($arg3 !== null) {
             $value = $this->quote($arg3);
             $condition = "`$arg1` $arg2 " . $this->quote($arg3);
@@ -1474,8 +1511,9 @@ class sql {
      * @return object the sql object.
      */
     public function andWhere($condition) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " AND $condition ";
         return $this;
     }
@@ -1488,8 +1526,9 @@ class sql {
      * @return object the sql object.
      */
     public function orWhere($condition) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " OR $condition ";
         return $this;
     }
@@ -1502,8 +1541,9 @@ class sql {
      * @return object the sql object.
      */
     public function eq($value) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " = " . $this->quote($value);
         return $this;
     }
@@ -1516,8 +1556,9 @@ class sql {
      * @return void the sql object.
      */
     public function ne($value) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " != " . $this->quote($value);
         return $this;
     }
@@ -1530,8 +1571,9 @@ class sql {
      * @return object the sql object.
      */
     public function gt($value) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " > " . $this->quote($value);
         return $this;
     }
@@ -1544,8 +1586,9 @@ class sql {
      * @return object the sql object.
      */
     public function ge($value) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " >= " . $this->quote($value);
         return $this;
     }
@@ -1558,8 +1601,9 @@ class sql {
      * @return object the sql object.
      */
     public function lt($value) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " < " . $this->quote($value);
 
         return $this;
@@ -1589,8 +1633,9 @@ class sql {
      * @return object the sql object.
      */
     public function between($min, $max) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $min = $this->quote($min);
         $max = $this->quote($max);
         $this->sql .= " BETWEEN $min AND $max ";
@@ -1606,8 +1651,9 @@ class sql {
      */
     public function in($ids) {
 //        var_dump($ids);
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= self::dbIN($ids);
         return $this;
     }
@@ -1621,8 +1667,9 @@ class sql {
      * @return  string  the string like IN('a', 'b').
      */
     private static function dbIN($ids) {
-        if (is_array($ids))
+        if (is_array($ids)) {
             return "IN ('" . join("','", $ids) . "')";
+        }
         return "IN ('" . str_replace(',', "','", str_replace(' ', '', $ids)) . "')";
     }
 
@@ -1634,8 +1681,9 @@ class sql {
      * @return object the sql object.
      */
     public function notin($ids) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= ' NOT ' . self::dbIN($ids);
         return $this;
     }
@@ -1648,8 +1696,9 @@ class sql {
      * @return object the sql object.
      */
     public function like($string) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= " LIKE " . $this->quote($string);
         return $this;
     }
@@ -1662,8 +1711,9 @@ class sql {
      * @return object the sql object.
      */
     public function notLike($string) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= "NOT LIKE " . $this->quote($string);
         return $this;
     }
@@ -1677,8 +1727,9 @@ class sql {
      * @return object the sql object.
      */
     public function findInSet($str, $strList) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
         $this->sql .= "FIND_IN_SET(" . $str . "," . $strList . ")";
     }
 
@@ -1690,8 +1741,9 @@ class sql {
      * @return object the sql object.
      */
     public function orderBy($order) {
-        if ($this->inCondition and!$this->conditionIsTrue)
+        if ($this->inCondition and!$this->conditionIsTrue) {
             return $this;
+        }
 
         $order = str_replace(array('|', '', '_'), ' ', $order);
 
@@ -1706,13 +1758,17 @@ class sql {
             $orderParse = explode(' ', trim($order));
             foreach ($orderParse as $key => $value) {
                 $value = trim($value);
-                if (empty($value) or strtolower($value) == 'desc' or strtolower($value) == 'asc')
+                if (empty($value) #
+                        or strtolower($value) == 'desc' #
+                        or strtolower($value) == 'asc') {
                     continue;
+                }
                 $field = trim($value, '`');
 
                 /* such as t1.id field. */
-                if (strpos($value, '.') !== false)
+                if (strpos($value, '.') !== false) {
                     list($table, $field) = explode('.', $field);
+                }
                 $field = "`$field`";
 
                 $orderParse[$key] = isset($table) ? $table . '.' . $field : $field;
@@ -1734,8 +1790,9 @@ class sql {
      * @return object the sql object.
      */
     public function limit($limit) {
-        if (empty($limit))
+        if (empty($limit)) {
             return $this;
+        }
         stripos($limit, 'limit') !== false ? $this->sql .= " $limit " : $this->sql .= ' ' . DAO::LIMIT . " $limit ";
         return $this;
     }
@@ -1782,8 +1839,9 @@ class sql {
      * @return mixed
      */
     public function quote($value) {
-        if ($this->magicQuote)
+        if ($this->magicQuote) {
             $value = stripslashes($value);
+        }
         return $this->dbh->quote($value);
     }