2008-05-19chanserv
Storage 用の ResultSet 展開ライブラリ作ってみた
Mozilla, Storage, JavaScript | |
ソースコード
var StorageResultSetExtractor = function(stmt) { this.stmt = stmt; }; StorageResultSetExtractor.prototype = { rowInfoCache: null, rowData: null, getRowInfo: function() { if (this.rowInfoCache) { return this.rowInfoCache; } var rowInfo = []; for (var idx = 0, colCnt = this.stmt.columnCount; idx < colCnt; idx++) { rowInfo.push({ name: this.stmt.getColumnName(idx), type: this.stmt.getTypeOfIndex(idx) }); } this.rowInfoCache = rowInfo; return this.rowInfoCache; }, next: function() { if (!this.stmt.executeStep()) return false; var rowInfo = this.getRowInfo(); var rowData = {}; for (var cidx = 0, clen = rowInfo.length; cidx < clen; cidx++) { switch (rowInfo[cidx].type) { case this.stmt.VALUE_TYPE_NULL: rowData[rowInfo[cidx].name] = null; break; case this.stmt.VALUE_TYPE_INTEGER: rowData[rowInfo[cidx].name] = this.stmt.getInt32(cidx); break; case this.stmt.VALUE_TYPE_FLOAT: rowData[rowInfo[cidx].name] = this.stmt.getDouble(cidx); break; case this.stmt.VALUE_TYPE_TEXT: rowData[rowInfo[cidx].name] = this.stmt.getUTF8String(cidx); break; case this.stmt.VALUE_TYPE_BLOB: var dataSize = { aDataSize: null }; var data = { aData: null }; this.stmt.getBlob(cidx, dataSize, data); rowData[rowInfo[cidx].name] = data.aData; break; default: ; } } this.rowData = rowData; return true; }, item: function() { return this.rowData; } };
使い方
var storage = Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService); var conn = storage.openDatabase(aDBFile); var stmt = conn.createStatement("SELECT * FROM foo;"); var extractor = new StorageResultSetExtractor(stmt); var result = []; while (extractor.next()) { result.push(extractor.item()); } result[0].colname; // これで値取れる
んー、まぁ今はここまで。本当はinflate/deflateとか出来るべきなんだろうなぁ。
ファイル関連の関数追加した
MozRepl | |
function file(path) { var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); file.initWithPath(path); return file; } function url(aURL) { var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); var url = ios.newURI(aURL, null, null); return url; } function slurp(path) { var file = this.file(path); var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); var sstream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream); fstream.init(file, -1, 0, 0); sstream.init(fstream); var data = ""; var str = sstream.read(4096); while (str.length > 0) { data += str; str = sstream.read(4096); } sstream.close(); fstream.close(); return data; }
あとresourceに簡単に追加出来る関数はこんなん、
function createResource(alias, filepath) { var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); var resProtocol = ioService.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler); var aliasFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); aliasFile.initWithPath(filepath); resProtocol.setSubstitution(alias, ioService.newFileURI(aliasFile)); }
コメントを書く
Christopher2012/09/28 18:11There is a critical shortage of infrotmiave articles like this.
bcskfetxmw2012/09/29 05:20wPzYXA <a href="http://mskjgsyfulye.com/">mskjgsyfulye</a>
xoicravirj2012/09/30 01:09R8uDa7 , [url=http://gbqpulptaobp.com/]gbqpulptaobp[/url], [link=http://yzjkswqqoywo.com/]yzjkswqqoywo[/link], http://jvhkpyeexcvu.com/
ysjvlscew2012/09/30 11:32PNyyAc <a href="http://kwjpltbwouwo.com/">kwjpltbwouwo</a>
yelvcsfuymj2012/10/02 02:50LQci7N , [url=http://onwtzvmvqkct.com/]onwtzvmvqkct[/url], [link=http://pwzbknltpxfr.com/]pwzbknltpxfr[/link], http://chfaxyypvvmi.com/