2011. 12. 9. 20:29
mysql procedure
2011. 12. 9. 20:29 in 개발/DB_MYSQL
DROP TABLE IF EXISTS my_bundles;
CREATE TABLE my_bundles(seq int auto_increment, total int, bundle text, PRIMARY KEY (seq));
INSERT INTO my_bundles(total,bundle) VALUES (3,"JAVASCRIPT,ACTIONSCRIPT,HTML"),(1,"C++"),(2,"ALGOL,C#"),(7,"C,PHP,JSP,LISP,BASIC,ADA,PYTHON"),(6,"JAVA,RUBY,PASCAL,COBOL,FORTRAN,PERL"),(5,"DELPHI,PROLOG,SMALLTALK,PERL,COLDFUSION");
SELECT * FROM my_bundles;
DROP TABLE IF EXISTS my_items;
CREATE TABLE my_items(seq int auto_increment, language varchar(32), PRIMARY KEY (seq));
DELIMITER $$
DROP PROCEDURE IF EXISTS tokenizer $$
CREATE PROCEDURE tokenizer(
INOUT input_string varchar(1025), OUT token varchar(1025), IN boundary varchar(16)
) READS SQL DATA
BEGIN
SELECT char_length(boundary) INTO @boundry_length;
SET @idx = LOCATE(boundary,input_string);
IF (@idx = 0) THEN
SET token = input_string;
SET input_string = NULL;
ELSE
SET token = SUBSTR(input_string,1,@idx-1);
SET input_string = SUBSTR(input_string,@idx + @boundry_length);
END IF;
END
$$
DELIMITER ;
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_items $$
CREATE PROCEDURE insert_items(IN my_str varchar(1024)) MODIFIES SQL DATA
BEGIN
SELECT my_str INTO @org_string;
CALL tokenizer(@org_string, @tkn_str, ',');
WHILE (@tkn_str IS NOT NULL) DO
INSERT INTO my_items(language) VALUES (@tkn_str);
CALL tokenizer(@org_string, @tkn_str, ',');
END WHILE;
END
$$
DELIMITER ;
CALL insert_items('a,b,c');
SELECT * FROM my_items;
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_cols_items $$
CREATE PROCEDURE insert_cols_items()
BEGIN
DECLARE ok INT DEFAULT '0';
DECLARE tmp_seq INT DEFAULT '0';
DECLARE tmp_total INT DEFAULT '0';
DECLARE tmp_bundle TEXT DEFAULT '';
DECLARE tot INT DEFAULT '0';
DECLARE cur CURSOR FOR SELECT seq, total, bundle FROM my_bundles;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET ok = 1;
OPEN cur;
REPEAT
FETCH cur INTO tmp_seq, tmp_total, tmp_bundle;
IF NOT ok THEN
CALL insert_items(tmp_bundle);
SET tot = tot + 1;
END IF;
UNTIL ok END REPEAT;
CLOSE cur;
IF tot > 0 THEN
SELECT tot;
ELSE
SELECT 0;
END IF;
END
$$
DELIMITER ;
TRUNCATE my_items;
CALL insert_cols_items();
SELECT * FROM my_items;
CREATE TABLE my_bundles(seq int auto_increment, total int, bundle text, PRIMARY KEY (seq));
INSERT INTO my_bundles(total,bundle) VALUES (3,"JAVASCRIPT,ACTIONSCRIPT,HTML"),(1,"C++"),(2,"ALGOL,C#"),(7,"C,PHP,JSP,LISP,BASIC,ADA,PYTHON"),(6,"JAVA,RUBY,PASCAL,COBOL,FORTRAN,PERL"),(5,"DELPHI,PROLOG,SMALLTALK,PERL,COLDFUSION");
SELECT * FROM my_bundles;
DROP TABLE IF EXISTS my_items;
CREATE TABLE my_items(seq int auto_increment, language varchar(32), PRIMARY KEY (seq));
DELIMITER $$
DROP PROCEDURE IF EXISTS tokenizer $$
CREATE PROCEDURE tokenizer(
INOUT input_string varchar(1025), OUT token varchar(1025), IN boundary varchar(16)
) READS SQL DATA
BEGIN
SELECT char_length(boundary) INTO @boundry_length;
SET @idx = LOCATE(boundary,input_string);
IF (@idx = 0) THEN
SET token = input_string;
SET input_string = NULL;
ELSE
SET token = SUBSTR(input_string,1,@idx-1);
SET input_string = SUBSTR(input_string,@idx + @boundry_length);
END IF;
END
$$
DELIMITER ;
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_items $$
CREATE PROCEDURE insert_items(IN my_str varchar(1024)) MODIFIES SQL DATA
BEGIN
SELECT my_str INTO @org_string;
CALL tokenizer(@org_string, @tkn_str, ',');
WHILE (@tkn_str IS NOT NULL) DO
INSERT INTO my_items(language) VALUES (@tkn_str);
CALL tokenizer(@org_string, @tkn_str, ',');
END WHILE;
END
$$
DELIMITER ;
CALL insert_items('a,b,c');
SELECT * FROM my_items;
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_cols_items $$
CREATE PROCEDURE insert_cols_items()
BEGIN
DECLARE ok INT DEFAULT '0';
DECLARE tmp_seq INT DEFAULT '0';
DECLARE tmp_total INT DEFAULT '0';
DECLARE tmp_bundle TEXT DEFAULT '';
DECLARE tot INT DEFAULT '0';
DECLARE cur CURSOR FOR SELECT seq, total, bundle FROM my_bundles;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET ok = 1;
OPEN cur;
REPEAT
FETCH cur INTO tmp_seq, tmp_total, tmp_bundle;
IF NOT ok THEN
CALL insert_items(tmp_bundle);
SET tot = tot + 1;
END IF;
UNTIL ok END REPEAT;
CLOSE cur;
IF tot > 0 THEN
SELECT tot;
ELSE
SELECT 0;
END IF;
END
$$
DELIMITER ;
TRUNCATE my_items;
CALL insert_cols_items();
SELECT * FROM my_items;
글에서 날씨 정보를 얻어오기 위해서 필요한 국가별 도시 정보를 가져오는 클래스입니다. 간단하긴 한데, 출력 데이터가 validate 되지 않은 JSON이라서 그거 수정하는데 30분 정도 걸렸네요.(미워요 구글~~)
그 부분 메쏘드는 convertJson(한줄짜리 -_-;) 이구요, 나머지는 쉬우니까 패스~~
소스 들어갑니다.
<?php
$iso = 'KR';
$City = new CompanyCity($iso);
print_r($City->get());
class CompanyCity {
const KOURL = 'http://www.google.co.kr/ig/cities?country=';
const ENURL = 'http://www.google.com/ig/cities?country=';
const UTF8 = '&oe=UTF-8';
private $country;
private $city;
private $type = 'array'; // OR object
public function __construct($iso3166)
{
$this->country = $iso3166;
}
private static function convertJson($v)
{
return preg_replace('/(cities|name|lat|lon|code|selected|true)/', "\""."\\1"."\"", $v);
}
public function get()
{
$this->city = file_get_contents(self::KOURL.$this->country.self::UTF8);
return $this->type=='array' ? json_decode(self::convertJson($this->city), true) : json_decode(self::convertJson($this->city));
}
}
?>
입력할 때 ISO-3166-1-alpha-2 에 맞는 코드를 입력해야 출력이 제대로 나옵니다.
[이 게시물은 최고관리자님에 의해 2010-02-11 03:18:53 B16에서 이동 됨]
그 부분 메쏘드는 convertJson(한줄짜리 -_-;) 이구요, 나머지는 쉬우니까 패스~~
소스 들어갑니다.
<?php
$iso = 'KR';
$City = new CompanyCity($iso);
print_r($City->get());
class CompanyCity {
const KOURL = 'http://www.google.co.kr/ig/cities?country=';
const ENURL = 'http://www.google.com/ig/cities?country=';
const UTF8 = '&oe=UTF-8';
private $country;
private $city;
private $type = 'array'; // OR object
public function __construct($iso3166)
{
$this->country = $iso3166;
}
private static function convertJson($v)
{
return preg_replace('/(cities|name|lat|lon|code|selected|true)/', "\""."\\1"."\"", $v);
}
public function get()
{
$this->city = file_get_contents(self::KOURL.$this->country.self::UTF8);
return $this->type=='array' ? json_decode(self::convertJson($this->city), true) : json_decode(self::convertJson($this->city));
}
}
?>
입력할 때 ISO-3166-1-alpha-2 에 맞는 코드를 입력해야 출력이 제대로 나옵니다.
[이 게시물은 최고관리자님에 의해 2010-02-11 03:18:53 B16에서 이동 됨]