Пример #1
0
 /**
  * buildMethod
  * @see \lasa\db\builder\DAOMethodBuilder::buildMethod()
  */
 public function buildMethod(\ReflectionMethod $method)
 {
     $params = [];
     foreach ($method->getParameters() as $param) {
         $params[$param->getName()] = $param;
     }
     $scripts = [];
     $query = Query::insert($this->tableName);
     $binds = [];
     foreach ($this->model as $key => $array) {
         if ($key == "id") {
             continue;
         }
         $value_name = array_shift($array);
         $query->column($value_name);
         if (isset($array["serialize"]) && $array["serialize"] == "json") {
             $binds[":" . $value_name] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
         } else {
             $binds[":" . $value_name] = $this->buildBindCode($key, $params);
         }
     }
     $scripts[] = '$query = ' . $query->dump() . ';' . "\n";
     $scripts[] = '$binds = ' . $this->buildBindsCode($binds) . ';' . "\n";
     $scripts[] = '$this->executeUpdateQuery($query, $binds);' . "\n";
     $scripts[] = '$id = $this->lastInsertId();' . "\n";
     $scripts[] = 'return $id;';
     return "\t" . implode("\t", $scripts);
 }
Пример #2
0
 /**
  * buildMethod
  * @see \lasa\db\builder\DAOMethodBuilder::buildMethod()
  */
 public function buildMethod(\ReflectionMethod $method)
 {
     $methodName = $method->getName();
     $params = [];
     foreach ($method->getParameters() as $param) {
         $params[$param->getName()] = $param;
     }
     /* modelClassShortName */
     $namespaceName = $method->getDeclaringClass()->getNamespaceName();
     /* 準備 */
     $annotations = $this->getMethodAnnotations($method);
     /* パラメーター */
     $scripts = [];
     $binds = [];
     $query = Query::select($this->tableName);
     $getByX = null;
     $bind_keys = [];
     $returnColumnName = "count_" . strtolower($methodName);
     if (isset($this->model["id"])) {
         $columnName = $this->model["id"];
         $query->column("count(" . $columnName[0] . ") as " . $returnColumnName);
     } else {
         $query->column("count(*) as " . $returnColumnName);
     }
     if (preg_match('/By([a-zA-Z0-9]*)$/', $methodName, $tmp)) {
         $getByX = lcfirst($tmp[1]);
     }
     if (isset($annotations["where"])) {
         $query->where($annotations["where"]);
         $getByX = null;
         preg_match_all("/:([^\\s]+)/", $annotations["where"], $tmp);
         foreach ($tmp[1] as $key) {
             if (isset($params[$key])) {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
                 continue;
             }
             $bind_keys[$key] = $key;
         }
     }
     foreach ($this->model as $key => $column_array) {
         $value = array_shift($column_array);
         if ($getByX && $getByX == $key) {
             $query->where($value . "=:" . $key);
             if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
                 $binds[":" . $key] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
             } else {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
             }
         }
         if (in_array($key, $bind_keys)) {
             if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
                 $binds[":" . $key] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
             } else {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
             }
         }
     }
     $scripts[] = '$query = ' . $query->dump() . ';' . "\n";
     $scripts[] = '$binds = ' . $this->buildBindsCode($binds) . ';' . "\n";
     //objectの時は1件のみ
     $scripts[] = '$oldLimit = $this->getLimit();';
     $scripts[] = '$this->setLimit(1);';
     $scripts[] = '$oldOffset = $this->getOffset();';
     $scripts[] = '$this->setOffset(0);';
     $scripts[] = '$result=$this->executeQuery($query, $binds);' . "\n";
     $scripts[] = '$this->setLimit($oldLimit);';
     $scripts[] = '$this->setOffset($oldOffset);';
     $scripts[] = 'if(count($result)<1)return 0;' . "\n";
     $scripts[] = 'return (int)$result[0]["' . $returnColumnName . '"];';
     return "\t" . implode("\t", $scripts);
 }
Пример #3
0
 /**
  * 複数パラメーターをUpdateするメソッド
  * byXか、引数にidが無い場合はエラーとなる
  * @param \ReflectionMethod $method
  */
 public function buildParameterUpdateMethod(\ReflectionMethod $method)
 {
     /* 準備 */
     $methodName = $method->getName();
     $annotations = $this->getMethodAnnotations($method);
     $params = [];
     foreach ($method->getParameters() as $param) {
         $params[$param->getName()] = $param;
     }
     $scripts = [];
     $query = Query::update($this->tableName);
     $getByX = null;
     $bind_keys = [];
     $binds = [];
     $columns = [];
     if (preg_match('/By([a-zA-Z0-9]*)$/', $methodName, $tmp)) {
         $getByX = lcfirst($tmp[1]);
     }
     //where句指定
     if (isset($annotations["where"])) {
         $query->where($annotations["where"]);
         $getByX = null;
         preg_match_all("/([^\\s]+)\\s*=\\s*:([^\\s]+)/", $annotations["where"], $tmp);
         foreach ($tmp[2] as $index => $key) {
             $column = $tmp[1][$index];
             $bind_keys[$key] = $column;
         }
     }
     if ($getByX && isset($this->model[$getByX])) {
         $column_array = $this->model[$getByX];
         $value = array_shift($column_array);
         $bind_keys[$getByX] = $column_array;
         $query->where($value . "=:" . $getByX);
     }
     //更新対象のカラムを作る
     foreach ($params as $key => $value) {
         //パラメーター名が不明な場合、where句で指定していればOK
         if (!isset($this->model[$key])) {
             if (isset($bind_keys[$key])) {
                 $value = $key;
                 $binds[":" . $value] = $this->buildBindCode($key, $params);
                 continue;
             }
             continue;
         }
         //対象のカラム情報
         $column_array = $this->model[$key];
         $value = array_shift($column_array);
         if ($key == "id") {
             $query->where("id=:id");
             $binds[":" . $value] = $this->buildBindCode($key, $params);
             continue;
         }
         if (isset($bind_keys[$key])) {
             //where句で指定していた場合はbindとパラメーター名を揃える
             $value = $key;
         } else {
             //更新対象のカラムに指定
             $query->column($value);
         }
         //自動的にbindする
         if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
             $binds[":" . $value] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
         } else {
             $binds[":" . $value] = $this->buildBindCode($key, $params);
         }
     }
     //エラーになるパターン
     if (!$query->columns) {
         $scripts[] = 'throw new \\Exception(__CLASS__ . "#' . $methodName . ' must contain one more columns.");' . "\n";
     }
     if (!$query->wheres) {
         $scripts[] = 'throw new \\Exception(__CLASS__ . "#' . $methodName . ' must contain one more where terms.");' . "\n";
     }
     $scripts[] = '$query = ' . $query->dump() . ';' . "\n";
     $scripts[] = '$binds = ' . $this->buildBindsCode($binds) . ';' . "\n";
     $scripts[] = 'return $this->executeUpdateQuery($query, $binds);';
     return "\t" . implode("\t", $scripts);
 }
Пример #4
0
 /**
  * buildMethod
  * @see \lasa\db\builder\DAOMethodBuilder::buildMethod()
  */
 public function buildMethod(ReflectionMethod $method)
 {
     $methodName = $method->getName();
     $params = [];
     foreach ($method->getParameters() as $param) {
         $params[$param->getName()] = $param;
     }
     /* 準備 */
     $annotations = $this->getMethodAnnotations($method);
     $scripts = [];
     $query = Query::delete($this->tableName);
     $getByX = null;
     $bind_keys = [];
     $binds = [];
     if (preg_match('/By([a-zA-Z0-9]*)$/', $methodName, $tmp)) {
         $getByX = lcfirst($tmp[1]);
     }
     //getByXがない場合は基本的にidを引数とすることにする
     if ($getByX == null) {
         $getByX = "id";
     }
     if (isset($annotations["where"])) {
         $query->where($annotations["where"]);
         $getByX = null;
         preg_match_all("/:([^\\s]+)/", $annotations["where"], $tmp);
         foreach ($tmp[1] as $key) {
             if (isset($params[$key])) {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
                 continue;
             }
             $bind_keys[$key] = $key;
         }
     }
     //ByXの部分がパラメーターにある場合の引数
     if ($getByX && isset($params[$getByX])) {
         $binds[":" . $getByX] = $this->buildBindCode($getByX, $params);
     }
     //Modelが引数の場合のbindsの処理
     foreach ($this->model as $key => $column_array) {
         $value = array_shift($column_array);
         if ($getByX && $getByX == $key) {
             $query->where($value . "=:" . $key);
             if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
                 $binds[":" . $key] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
             } else {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
             }
         }
         if (in_array($key, $bind_keys)) {
             if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
                 $binds[":" . $key] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
             } else {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
             }
         }
     }
     $scripts[] = '$query = ' . $query->dump() . ';' . "\n";
     $scripts[] = '$binds = ' . $this->buildBindsCode($binds) . ';' . "\n";
     $scripts[] = 'return $this->executeUpdateQuery($query, $binds);';
     return "\t" . implode("\t", $scripts);
 }
Пример #5
0
 /**
  * buildMethod
  * @see \lasa\db\builder\DAOMethodBuilder::buildMethod()
  */
 public function buildMethod(\ReflectionMethod $method)
 {
     $methodName = $method->getName();
     $params = [];
     foreach ($method->getParameters() as $param) {
         $params[$param->getName()] = $param;
     }
     /* modelClassShortName */
     $namespaceName = $method->getDeclaringClass()->getNamespaceName();
     /* 準備 */
     $annotations = $this->getMethodAnnotations($method);
     $returnType = "array";
     if (isset($annotations["return"])) {
         $annotationReturn = $annotations["return"];
         if (class_exists($annotationReturn) && $annotationReturn == $this->modelClassName) {
             $returnType = "object";
         }
         if (class_exists($namespaceName . "\\" . $annotationReturn) && $namespaceName . "\\" . $annotationReturn == $this->modelClassName) {
             $returnType = "object";
         } else {
             if ($annotationReturn == "list") {
                 $returnType = "list";
             } else {
                 if ($annotationReturn == "[" . $this->modelClassName . "]") {
                     $returnType = "list";
                 } else {
                     if ($annotationReturn == "row") {
                         $returnType = "row";
                     } else {
                         if (strpos($annotationReturn, "column_") === 0) {
                             $returnColumnName = substr($annotationReturn, strlen("column_"));
                         }
                     }
                 }
             }
         }
     }
     /* パラメーター */
     $scripts = [];
     $binds = [];
     $query = Query::select($this->tableName);
     $getByX = null;
     $index = null;
     $bind_keys = [];
     if (isset($annotations["index"])) {
         $index = $annotations["index"];
     }
     if (preg_match('/By([a-zA-Z0-9]*)$/', $methodName, $tmp)) {
         $getByX = lcfirst($tmp[1]);
         //getByIdでreturnを指定していない時は自動補完
         if ($getByX == "id" && !isset($annotations["return"])) {
             $returnType = "object";
         }
     }
     if (isset($annotations["where"])) {
         $query->where($annotations["where"]);
         $getByX = null;
         preg_match_all("/:([^\\s]+)/", $annotations["where"], $tmp);
         foreach ($tmp[1] as $key) {
             if (isset($params[$key])) {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
                 continue;
             }
             $bind_keys[$key] = $key;
         }
     }
     foreach ($this->model as $key => $column_array) {
         $value = array_shift($column_array);
         $query->column($value);
         if ($getByX && $getByX == $key) {
             $query->where($value . "=:" . $key);
             if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
                 $binds[":" . $key] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
             } else {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
             }
         }
         if (in_array($key, $bind_keys)) {
             if (isset($column_array["serialize"]) && $column_array["serialize"] == "json") {
                 $binds[":" . $key] = 'json_encode(' . $this->buildBindCode($key, $params) . ")";
             } else {
                 $binds[":" . $key] = $this->buildBindCode($key, $params);
             }
         }
     }
     if (isset($annotations["order"])) {
         $query->setOrder($annotations["order"]);
     }
     $scripts[] = '$query = ' . $query->dump() . ';' . "\n";
     $scripts[] = '$binds = ' . $this->buildBindsCode($binds) . ';' . "\n";
     //objectの時は1件のみ
     if ($returnType == "object" || $returnType == "column" || $returnType == "row") {
         $scripts[] = '$oldLimit = $this->getLimit();';
         $scripts[] = '$this->setLimit(1);';
         $scripts[] = '$oldOffset = $this->getOffset();';
         $scripts[] = '$this->setOffset(0);';
     }
     $scripts[] = '$result=$this->executeQuery($query, $binds);' . "\n";
     switch ($returnType) {
         /* 一行のパターン */
         case "object":
             $scripts[] = '$this->setLimit($oldLimit);' . "\n";
             $scripts[] = '$this->setOffset($oldOffset);' . "\n";
             $scripts[] = 'if(count($result)<1)return null;' . "\n";
             $scripts[] = '$obj = $this->getObject($result[0]);';
             $scripts[] = 'return $obj;';
             break;
         case "row":
             $scripts[] = '$this->setLimit($oldLimit);';
             $scripts[] = '$this->setOffset($oldOffset);';
             $scripts[] = 'if(count($result)<1)return null;' . "\n";
             $scripts[] = 'return $result[0];';
             break;
         case "column":
             $scripts[] = '$this->setLimit($oldLimit);';
             $scripts[] = '$this->setOffset($oldOffset);';
             $scripts[] = 'if(count($result)<1)return null;' . "\n";
             $scripts[] = '$row = $result[0];';
             $scripts[] = 'return $row["' . $returnColumnName . '"];';
             break;
             /* 複数行のパターン */
         /* 複数行のパターン */
         case "list":
         default:
             $scripts[] = '$array = array();';
             $scripts[] = 'if(is_array($result)){';
             $scripts[] = 'foreach($result as $row){';
             if ($index) {
                 $func = "get" . ucfirst($index);
                 $scripts[] = '$obj = $this->getObject($row);';
                 $scripts[] = '$array[$obj->' . $func . '()] = $obj;';
             } else {
                 $scripts[] = '$array[] = $this->getObject($row);';
             }
             $scripts[] = '}';
             $scripts[] = '}';
             $scripts[] = 'return $array;';
             break;
     }
     return "\t" . implode("\t", $scripts);
 }