ã¯ããã«
Safari 3.1 ã«ã¯ Client-side database storage ï¼SQL API ã¨ãå¼ã°ãã¦ãã¾ããï¼ã¨ããæ°ããä»æ§ãå®è£
ããã¾ããã
ã¨ããããã§ããã®æ°ãã API ã«ã¤ãã¦è²ã
調ã¹ããã¨ãç°¡åã«ã¾ã¨ãã¦ããã¾ãã
Client-side database storage ã使ãããã©ã¦ã¶
2008 å¹´ 03 æ 27 æ¥ç¾å¨ã§ã¯ã Safari ç³»ã®ãã©ã¦ã¶ã®ã¿ã§ãã
Client-side database storage ã¨ã¯
Selectors API ã¨ã¯ HTML5 ã§å®ç¾©ãããä»æ§ã§ãã詳細ã«é¢ãã¦ã¯こちらãã©ããã
ç°¡åã«èª¬æãã㨠JavaScript å
ã§ãªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ã使ããã¨ãããã¨ã§ãã
ãã£ã¨ç°¡åã«ã¤ã¡ã¼ã¸ããããã«ãå®éã®ã³ã¼ãã示ãã¨ãããªæãã§ãã
var db = openDatabase('mydb', '1.0'); // ãã©ã³ã¶ã¯ã·ã§ã³ã®éå§ db.transaction(function(tx) { // ãã¼ãã«ãä½ã tx.executeSql('CREATE TABLE IF NOT EXIST link (id INTEGER PRIMARY KEY, href TEXT, title TEXT)'); // ãªã³ã¯ãæ½åº var xresult = document.evaluate('//a', document, null, 7, null); for (var i = 0; i < xresult.snapshotLength; i ++) { var elmLink = xresult.snapshotItem(i); // ãã¼ãã«ã«æ¿å ¥ tx.executeSql('INSERT INTO link VALUES(NULL, ?, ?)', [elmLink.href, elmLink.textContent]) } });
ãã®ããã« JavaScript ãã SQL ãå®è¡ãã¦ãã¼ã¿ãã¼ã¹ã使ããã¨ãã§ããã®ã§ãã
ç°¡åã§ããï¼
ãã¼ã¿ãã¼ã¹ã®ä¿åå ´æã¨ä¿åæéã¨å ±æç¯å²
ä¿åå ´æ
ãã¼ã¿ã¯ãã¯ã©ã¤ã¢ã³ããµã¤ãã«ä¿åããã¾ãã
ãµã¼ãã«ãããã¼ã¿ãã¼ã¹ã¨ã¯ä¸åé¢ä¿ãªãã®ã§èª¤è§£ããªãããã«
ä¿åæé
ãã®ãã¼ã¿ãã¼ã¹å
ã®æ
å ±ã¯ãªãã¼ããã¦ããã©ã¦ã¶ãåèµ·åãã¦ã失ããã¾ããã
æ°¸ç¶ Cookie ã®ãããªæãã§ãã
å ±æç¯å²
ãã®ãã¼ã¿ãã¼ã¹ã¯ãåããã¡ã¤ã³å
*1ã§æå¹ã§ãã
ã¤ã¾ãã
- http://www.example.com/hoge/hoge ã¨
- http://www.example.com/hoge/fuga ã¯åããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
- http://www.example.com/fuga/fuga ã¯åããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
- http://www.example.com:80/ ã¯åããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
- http://example.com/hoge/hoge ã¯éããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
- http://sub.example.com/hoge/hoge ã¯éããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
- https://www.example.com/hoge/hoge ã¯éããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
- http://www.example.org/hoge/hoge ã¯éããã¼ã¿ãã¼ã¹ãè¦ã¦ãã¾ãã
ã¨ããæãã«ãªãã¾ãã
ãããããSame origin policyãã¨ãããã¤ã§ããã
é¢æ°ã®å ·ä½çãªä½¿ãæ¹
Client-side database storage ã使ãã«ã¯ä»¥ä¸ã®ä¸ã¤ã®ç¨®é¡ã®é¢æ°ã使ãã¾ãã
- openDatabase
- ãã¼ã¿ãã¼ã¹ãä½ããéã
- transaction, changeVersion
- ãã©ã³ã¶ã¯ã·ã§ã³ãéã
- executeSql
- SQL ãå®è¡ãã
ã§ã¯ãã²ã¨ã¤ã²ã¨ã¤è¦ã¦ããã¾ããã
openDatabase
openDatabase é¢æ°ã¯ãã¼ã¿ãã¼ã¹ãéãé¢æ°ã§ããã¼ã¿ãã¼ã¹ããªãå ´åã¯ãã¼ã¿ãã¼ã¹ãä½ãã¾ããï¼ãã£ã¦ããªãã¦ãéãã£ã¦ãã¨ã§ããï¼
ãã®é¢æ°ã¯ãã°ãã¼ãã«é¢æ°ã¨ãã¦ä½¿ããã¨ãã§ãã¾ãã
// 以ä¸ã®ããã« // 第ä¸å¼æ°ã«ãã¼ã¿ãã¼ã¹ã®ååã // 第äºå¼æ°ã«ãã¼ã¿ãã¼ã¹ã®ãã¼ã¸ã§ã³åï¼èªåã§ãã£ã¦ã«æ±ºãããé©å½ã§ããï¼ // ãä¸ããã¨ããã¼ã¿ãã¼ã¹ãéãã¦ã // Database ãªãã¸ã§ã¯ã // ãè¿ãã¾ã var db = openDatabase('testdb', '1.0'); // æ¢ã« testdb ã¨ãããã¼ã¿ãã¼ã¹ãï¼åããã¡ã¤ã³å ã®ãã¼ã¿ãã¼ã¹ã¨ãã¦ï¼åå¨ãã¦ããå ´åã§ã // ãã¼ã¸ã§ã³ãéãå ´åã¯ä¾å¤ï¼ã¨ã©ã¼ï¼ãæãããã¾ãã db = openDatabase('testdb', '2.0'); // error!
ã¾ãããã¼ã¿ãã¼ã¹ã®å®¹éã¯ãã©ã¦ã¶å´ã§å¶éããã¦ããå¯è½æ§ãããã¾ãï¼å®è£
ä¾åï¼ã
ãã®å ´åã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
// 第ä¸å¼æ°ã«ä½ãããã®ã¡ãã»ã¼ã¸ // 第åå¼æ°ã«ä½¿ããã容éï¼ãã¤ãæ°ï¼ var db = openDatabase('testdb', '1.0', '100MB 使ããã¦ãã ããï¼ï¼', 100000000);
transaction, changeVersion
transaction é¢æ°ã¯ openDatabase é¢æ°ã§åå¾ãã Database ãªãã¸ã§ã¯ãã®é¢æ°ï¼ã¡ã½ããï¼ã§ãã
ãã©ã³ã¶ã¯ã·ã§ã³ãéããã¨ãã§ãã¾ãã
// 第ä¸å¼æ°ã«é¢æ°ã渡ãã¾ãã // ããã¨ã // ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããã // é¢æ°ãå®è¡ãã // é¢æ°ãçµäºããã¨ãã©ã³ã¶ã¯ã·ã§ã³ãçµäºãã¾ãã db.transaction(function(tx) { // é¢æ°ã®ç¬¬ä¸å¼æ°ã« SQLTransaction ãªãã¸ã§ã¯ãã渡ããã // ãã®é¢æ°å ã§ã // SQLTransaction ãªãã¸ã§ã¯ãã使ã£ã¦ãã¼ã¿ãã¼ã¹ãæä½ãã tx.executeSql('INSERT INTO hoge VALUES(NULL, "hoge")'); });
ããã§ã注æããªããã°ãªããªããã¨ãããã¾ãã
ããã¯ã transaction é¢æ°ã«æ¸¡ãããé¢æ°ã¯éåæã«å¼ã³åºãããã¨ãããã¨ã§ãã
ãã¨ãã°ã
var data = 'hoge'; db.transaction(function(tx) { // ãã㧠data 㯠'fuga' ã«ãªã tx.executeSql('INSERT INTO hoge VALUES(NULL, ?)', [data]); }); // ãã㯠transaction ããåã«å®è¡ããã data = 'fuga';
ã¤ã¡ã¼ã¸çã«ã¯ setTimeout ã«æ¸¡ãããé¢æ°ã®æåã¨åãã§ããã
ã§ã¯ãtransaction ãçµãã£ãå¾ã«å¦çããããå ´åã¯ã©ããããããã§ããããã
ãããªã¨ãã¯ã transaction é¢æ°ã«ç¬¬äºå¼æ°ã第ä¸å¼æ°ã¨ãã¦é¢æ°ã渡ãã¾ãã
ããã¨ãã©ã³ã¶ã¯ã·ã§ã³çµäºæã«ãããã®é¢æ°ãå¼ã³åºãã¦ããã¾ãã
- 第äºå¼æ°ã¯ãã¨ã©ã¼ãçºçãã¦ããã¼ã«ããã¯ããå ´åã«å¼ã³åºããã¾ã
- 第ä¸å¼æ°ã¯ãæ£å¸¸ã«ãã©ã³ã¶ã¯ã·ã§ã³ãã³ããããããå ´åã«å¼ã³åºããã¾ãã
以ä¸ã®ãããªæãã§ãã
tx.transaction( function(tx) {/* ...ç¥ */}, function(error) { alert(error.message + 'ã¨ãã訳ã§ãã¼ã«ããã¯ããã') }, function() { alert('æ£å¸¸ã«çµäºããã'); } );
ã¾ãã transaction é¢æ°ã®ç¹æ®çã¨ã㦠changeVersion ã¨ããé¢æ°ãããã¾ãã
ãããã Database ãªãã¸ã§ã¯ãã®é¢æ°ï¼ã¡ã½ããï¼ã§ãã
ãã¼ã¿ãã¼ã¹ã®ãã¼ã¸ã§ã³ãå¤æ´ããããã«ä½¿ãã¾ãã
ãã¼ã¸ã§ã³ã¨ã¯ã openDatabase ã®ç¬¬äºå¼æ°ã§æå®ãããã®ã§ãã
- 第ä¸å¼æ°ã«ãå¤ããã¼ã¸ã§ã³ï¼openDatabase ã®æã«æå®ãããã®ï¼
- 第äºå¼æ°ã«ãæ°ãããã¼ã¸ã§ã³
ãæå®ãã¾ãã
第ä¸å¼æ°ä»¥éã¯ã transaction é¢æ°ã®ç¬¬ä¸å¼æ°ä»¥éã¨åãã§ãã
tx.changeVersion('1.0', '2.0', function(tx) { // ãã¼ãã«ã追å ãããªã©ãã }, function(error) { // ã¨ã©ã¼ãçºçãã }, function() { // æ£å¸¸çµäº } );
ãªãããã¼ã¸ã§ã³å¤æ´é¢æ°ã¨ãã©ã³ã¶ã¯ã·ã§ã³ãé¢ä¿ãããã¨ããã¨ããã¼ã¸ã§ã³ãå¤ããã¨ãã®éã«ãããããã¼ã¿ã®å½¢ãããããããå¿
è¦ãããããã§ãã
ãã¼ã¸ã§ã³ãå¤æ´ãã㨠openDatabase é¢æ°ã®ç¬¬äºå¼æ°ã«ä¸ããã¹ãå¤ãå¤ããã¾ãã
ãã ãåã Safari 3.1 ã§ããã®é¢æ°ã試ããã¨ããããã¼ã¸ã§ã³ãå¤æ´ãããã¼ã¿ãã¼ã¹ããªã¼ãã³ã§ããªããªã£ã¦ãã¾ãã¾ãããã¡ãã£ã¨èª¿ã¹ã¦ãã¾ãï¼ãã°ï¼ï¼
executeSql
executeSql é¢æ°ã¯ transaction é¢æ°ã«æ¸¡ããé¢æ°ã«æ¸¡ããã SQLTransaction ãªãã¸ã§ã¯ãã®é¢æ°ï¼ã¡ã½ããï¼ã§ãã
第ä¸å¼æ°ã« SQL ã渡ããã¨ã§ SQL ãå®è¡ãããã¨ãã§ãã¾ãã
ãããªæãã§ãã
db.transaction(function(tx) { tx.executeSql('CREATE TABLE IF NOT EXIST bbs (id INTEGER PRIMARY, title TEXT, content TEXT)'); });
ãã©ã³ã¶ã¯ã·ã§ã³ã®å¤ã§ã¯å®è¡ã§ãã¾ããã
var globalTx; db.transaction( function(tx) { globalTx = tx; }, function() {}, function() { // ããã¯ããã©ã³ã¶ã¯ã·ã§ã³çµäºå¾ã«å®è¡ããããã // 以ä¸ã¯ãã¨ã©ã¼ globalTx.executeSql('CREATE TABLE IF NOT EXIST bbs (id INTEGER PRIMARY, title TEXT, content TEXT)'); } );
ã¾ãããã¬ã¼ã¹ãã«ãã?ãã使ãã«ã¯ã第äºå¼æ°ã«é åã渡ãã¾ãã
db.transaction(function(tx) { tx.executeSql('INSERT INTO bbs VALUES(NULL, ?, ?)', ['hoge', 'hogehoge']); });
ã¾ããçµæã使ããããªå ´åã¯ã第ä¸å¼æ°ã«é¢æ°ã渡ãã¾ãã
ããããã¨ã SQL å®è¡å¾ã«çµæã表ã SQLResultSet ãªãã¸ã§ã¯ãããã®é¢æ°ã«æ¸¡ããã¾ãã
db.transaction(function(tx) { tx.executeSql('SELECT * FROM bbs', [], function(tx, rs) { // rs ã SQLResultSet ãªãã¸ã§ã¯ãã tx ã¯åãã // rs.rows ã«è¡ã®ãã¼ã¿ãå ¥ã£ã¦ãã for (var i = 0; i < rs.rows.length; i++) { var row = rs.rows.item(i); // row ã¯ã«ã©ã åã§ããã·ã¥ã«ãªã£ã¦ãã var title = row.title; // ã¾ã㯠row['title'] var content = row.content; // row['content'] } }); });
ããã§æ³¨æããªããã°ãªããªãã®ã¯ããã®ã³ã¼ã«ããã¯é¢æ°ã¯éåæã«å¼ã³åºãããã¨ãããã¨ã§ãã
ã¤ã¾ãã以ä¸ã®ãããªãã¨ã¯ã§ãã¾ããã
db.transaction(function(tx) { var bbsid; tx.executeSql('INSERT INTO bbs VALUES(NULL, ?, ?)', ['hoge', 'hogehoge'], function(tx, rs) { bbsid = rs.insertId; // insertId ã«ã¯è¡ã«æ¿å ¥ããã¨ãã«èªåã§æ±ºã¾ã£ã id ãå ¥ã£ã¦ãã }); tx.executeSql('INSERT INTO bbs_user_map VALUES(NULL, ?, ?)', [bbsid, userid]); // ãã®æç¹ã§ bbsid ã¯æ±ºã¾ããªã });
ãªã®ã§ã以ä¸ã®ããã«ããã°å¤§ä¸å¤«ã§ãã
db.transaction(function(tx) { var bbsid; tx.executeSql('INSERT INTO bbs VALUES(NULL, ?, ?)', ['hoge', 'hogehoge'], function(tx, rs) { bbsid = rs.insertId; tx.executeSql('INSERT INTO bbs_user_map VALUES(NULL, ?, ?)', [bbsid, userid]); // ãã®æç¹ã§ bbsid ã¯æ±ºã¾ã£ã¦ãã }); });
ã¾ãã SQL ã§ã¨ã©ã¼ãçºçããå ´åã¯èªåã§ãã¼ã«ããã¯ãããã®ã§ãããã¨ã©ã¼ãè£è¶³ãã¦ãã¼ã«ããã¯ãããªãããã«ããã«ã¯ã第åå¼æ°ã«é¢æ°ã渡ãã¾ãã
db.transaction(function(tx) { tx.executeSql('INSERT INTO bbs VALUES(NULL, ?, ?)', ['hoge', 'hogehoge'], function(tx, rs) { }, function(tx, error) { alert(error.message + 'ã ãã©ãã¼ã«ããã¯ããªã'); } ); });
ãããªæã㧠SQL ãå®è¡ãã¾ãã
便å©ã§ããï¼
Safari ã§ã®å®è£
使ãæ¹ãããã£ãã¨ãã㧠Safari ã®å®è£ ã«ã¤ãã¦å°ãè¦ã¦ã¿ã¾ããã
ãã¼ã¿ãã¼ã¹ã¯ã©ãã«ä¿åãããï¼
Mac ã§ãã確èªãã¦ããªãã®ã§ãããã¼ã¿ãã¼ã¹ã¯ ~/Library/Safari/Databases ãã£ã¬ã¯ããªã«ä»¥ä¸ã®ããã«ä¿åããã¾ããã
Databases |-- Databases.db |-- http_amachang.art-code.org_0 | `-- 0000000000000001.db `-- http_hogehoge.com_0 |-- 0000000000000006.db `-- 0000000000000006.db-journal
ã¨ã³ã¸ã³ã¯ï¼
Sqlite 3 ã®ããã§ããsqlite3 ã³ãã³ãã§ä¸èº«ãè¦ããã¨ãã§ãã¾ããã
Web Inspector ã¨ã®é£æº
ã³ã³ãã³ãã§ãã¼ã¿ãã¼ã¹ããªã¼ãã³ããç¶æ
㧠Web Inspector ãéãã¨ä»¥ä¸ã®ããã«ãã¼ã¿ãã¼ã¹ã調ã¹ããã¨ãã§ãã¾ãã
ãã®ç»é¢ä¸ããããç´æ¥ SQL ãæã¦ãã®ã§ä¾¿å©ã§ãã
Google Gears
Client-side database storage ã¯ä»ã¯ Safari ãããã使ããã¨ãã§ãã¾ãã
ãããã Google Gears ã使ã£ã¦ãã¹ã¦ã®ãã©ã¦ã¶ã«å®è£
ãããã¨ã¯å¯è½ã ã¨æãã¾ããï¼ãã§ã«ããï¼ï¼
ãã®è¾ºããå°ã調ã¹ã¦ã¿ããã§ãã
ã¾ã¨ã
ã¨ãã訳ã§ãå°ãçªã£è¾¼ã㧠Client-side database storage ã¨ãããã®ã調ã¹ã¦ã¿ã¾ããã
Client-side database storage ããªããããããã£ãã§ãã
ã¢ãã¤ã«ãã©ã¦ã¶ä¸ã® JavaScript ã³ã³ãã³ã (iPhone ç¨ã®ã¢ããª) ãã UserScripit ãªã©ããå©ç¨ããã¨ã¨ã¦ã便å©ããã ãªã¨æãã¾ããã
*1:ããã§ã¯åã«ãã¡ã¤ã³ã¨æ¸ããããæ£ç¢ºã«ã¯ãããã³ã«ï¼ã¹ãã¼ã ï¼ããã¹ãåããã¼ãçªå·ãçããç¯å²å ã