dsn = $dsn; } /** * 生成ディレクトリを返します * @return 生成ディレクトリ名 */ function getDirectory() { $dir = $this->dir; if(substr($dir, strlen($dir)-1, 1) != $this->sep) $dir .= $this->sep; return $dir; } /** * 生成ディレクトリを設定します * @param $dir 生成ディレクトリ */ function setDirectory($dir) { $this->dir = $dir; } /** * ディレクトリセパレータを返します * @return ディレクトリセパレータ */ function getSeparator() { return $this->sep; } /** * ディレクトリセパレータを設定します * @param $sep ディレクトリセパレータ */ function setSeparator($sep) { $this->sep = $sep; } /** * クラスファイルの生成 * @return 失敗時にfalse、それ以外でtrue */ function generate() { $conn = &ADONewConnection($this->dsn); $date = date("Y/m/d"); $dsn = $this->dsn; $fp = fopen($this->dir . $this->sep . "CommonDAO.php", "w"); if(!$fp) return false; $body = <<< PHP_END connect("{$dsn}"); } } PHP_END; if(fwrite($fp, $body) === FALSE) return false; fclose($fp); $tables = $conn->MetaTables("TABLES"); foreach($tables as $table) { $columns = $conn->MetaColumnNames($table, true); $pkeys = $conn->MetaPrimaryKeys($table); $class_name = ucfirst($table); $dirpath = $this->dir . $this->sep . $class_name; if(!file_exists($dirpath)) mkdir($dirpath); $dirpath .= $this->sep; // DTOクラスの生成 $dto_class = $class_name; $dto_path = $dirpath . $dto_class . ".php"; $fp = fopen($dto_path, "w"); if(!$fp) return false; $body = <<< PHP_END get(\"$column\");\n"; $body .= "\t}\n"; $body .= "\tfunction set$method(\$$column)\n"; $body .= "\t{\n"; $body .= "\t\t\$this->set(\"$column\", \$$column);\n"; $body .= "\t}\n"; } $body .= "}\n"; if(fwrite($fp, $body) === FALSE) return false; fclose($fp); // Collectionクラスの生成 $collection_class = $class_name . "Collection"; $collection_path = $dirpath . $collection_class . ".php"; $fp = fopen($collection_path, "w"); if(!$fp) return false; $body = <<< PHP_END $pkey) { $pkey = strtolower($pkey); if($index > 0) $body .= ", "; $body .= "$" . $pkey; } $body .= ")\n"; $body .= "\t{\n"; $body .= "\t\treturn \$this->queryOne(\"select * from $table where "; foreach($pkeys as $index => $pkey) { $pkey = strtolower($pkey); if($index > 0) $body .= " AND "; $body .= "$pkey = ?"; } $body .= "\", array("; foreach($pkeys as $index => $pkey) { $pkey = strtolower($pkey); if($index > 0) $body .= ", "; $body .= "$" . $pkey; } $body .= "));\n"; $body .= "\t}\n"; } $body .= "\tfunction &findAll()\n"; $body .= "\t{\n"; $body .= "\t\treturn \$this->query(\"select * from $table\");\n"; $body .= "\t}\n"; $body .= "\tfunction insert(\$".$table.")\n"; $body .= "\t{\n"; $body .= "\t\treturn \$this->doInsert(\"$table\", \$".$table."->get());\n"; $body .= "\t}\n"; if($pkeys) { $body .= "\tfunction update(\$$table)\n"; $body .= "\t{\n"; foreach($pkeys as $index => $pkey) { $pkey = strtolower($pkey); $body .= "\t\t\$" . $pkey . " = \$this->qstr(\$" . $table . "->get(\"$pkey\"));\n"; } $body .= "\t\treturn \$this->doUpdate(\"$table\", \$".$table."->get(), \""; foreach($pkeys as $index => $pkey) { $pkey = strtolower($pkey); if($index > 0) $body .= " AND "; $body .= "$pkey = \$".$pkey; } $body .= "\");\n"; $body .= "\t}\n"; } $body .= "\tfunction &createCollection(\$result)\n"; $body .= "\t{\n"; $body .= "\t\t\$collection = new " . $collection_class . "(\$result);\n"; $body .= "\t\treturn \$collection;\n"; $body .= "\t}\n"; $body .= "}\n"; if(fwrite($fp, $body) === FALSE) return false; fclose($fp); } return true; } }