efficent SQL 스크립트 빌더 아래를 대체한다.
배경입니다.
정말 간단한 프로그램이지만 더 효율적인 방법이 필요합니다.이 코드를 사용하는 이유는 플랫폼과 데이터베이스 테이블에서 '모듈'을 만들 수있는 다른 개발자가 필요하기 때문입니다. 핵심 데이터베이스에 대한 전체 액세스 권한을 차단하면 my $ api-> database-> 액세스 레이어를 사용하여 테이블/데이터에 액세스해야합니다. 내 프로그램의 핵심 부분에 대한 요청을 차단합니다.
다음은 SQL 작성 테이블 스크립트 작성 기능을 사용하는 방법입니다. SqlField 클래스/함수는 전달 된 값의 평면 배열을 반환하며 아무 것도 특별하지 않습니다.
//The structure of the params are..
//$fields[] = SqlField::create($f_name, $f_type, $null, $auto_inc, $is_pk, $is_unique);
$fields[] = SqlField::create('id', 'int(7)', 0, 1, 1, 1);
$sql = $sqlTable->createTable('MyTableName', $fields);
다음은 내가 작성한 현재 기능입니다. 그러나 빠르게 지저분 해지고 완벽하지는 않습니다.
public function createTable($tableName, $fields = array()) {
$sql="CREATE TABLE xmod_".$tableName." (\r\n";
$isinit = true;
foreach($fields as $field) {
$sql .= ($isinit)? "" : ", \r\n" ;
$isinit = false;
$sql .= "".$field[0]." ".$field[1]." ";
if($field[2] == true) {
$sql .= "NOT NULL ";
}
if($field[3] == true) {
$sql .= "auto_increment ";
}
}
$isinit = true;
//Work on the primary keys
foreach($fields as $field) {
if($field[4] == 1) {
$sql .= ($isinit)? "" : ", \r\n" ;
$isinit = false;
$sql .= "PRIMARY KEY (".$field[0].")";
}
}
$isinit = true;
//Work on the unique fields
foreach($fields as $field) {
if($field[5] == 1) {
$sql .= ($isinit)? "" : ", \r\n" ;
$isinit = false;
$sql .= "UNIQUE id (".$field[0].")";
}
}
$sql .= "\r\n)";
return $sql;
}
그래서 사람들이 당신을 위해 코드를 작성하는 장소가 아니다 (코멘트에서 복사) http://codeigniter.com/user_guide/database/forge.html
, 그것은 특정 프로그래밍 질문을 할 수있는 장소입니다. – LeonardChallis
충분합니다. 뭔가 비슷한 것을하는 SQL 클래스가 있습니다. – IEnumerable
직접 시도하지는 않았지만 CodeIgniter는 비슷한 것을 제공합니다. http://codeigniter.com/user_guide/database/forge.html – danneth