Hatena::Groupmoz-addon

Ci.nsIZIGOROu

2008-05-19chanserv

Storage 用の ResultSet 展開ライブラリ作ってみた

| 19:34 |  Storage 用の ResultSet 展開ライブラリ作ってみた - Ci.nsIZIGOROu を含むブックマーク はてなブックマーク -  Storage 用の ResultSet 展開ライブラリ作ってみた - Ci.nsIZIGOROu

ソースコード

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とか出来るべきなんだろうなぁ。

ChristopherChristopher2012/09/28 18:11There is a critical shortage of infrotmiave articles like this.

bcskfetxmwbcskfetxmw2012/09/29 05:20wPzYXA <a href="http://mskjgsyfulye.com/">mskjgsyfulye</a>

xoicravirjxoicravirj2012/09/30 01:09R8uDa7 , [url=http://gbqpulptaobp.com/]gbqpulptaobp[/url], [link=http://yzjkswqqoywo.com/]yzjkswqqoywo[/link], http://jvhkpyeexcvu.com/

ysjvlscewysjvlscew2012/09/30 11:32PNyyAc <a href="http://kwjpltbwouwo.com/">kwjpltbwouwo</a>

yelvcsfuymjyelvcsfuymj2012/10/02 02:50LQci7N , [url=http://onwtzvmvqkct.com/]onwtzvmvqkct[/url], [link=http://pwzbknltpxfr.com/]pwzbknltpxfr[/link], http://chfaxyypvvmi.com/