1 2/** 3 * IndexedDB ê´ë¦¬ í´ëì¤ 4 * * 5 * @param {string} dbName ë°ì´í°ë² ì´ì¤ëª 6 * @param {string} version ë²ì 7 * @param {string} tableName í ì´ë¸ ëª 8 * @param {function} [openCallback] 9 * @param {Object} [initParams 10 * @constructor 11 */ 12function IDB(dbName, version, tableName, openCallback, initParams) { 13 // // IndexedDb ê°ì²´ 14 this.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; 15 16 this.db = null; 17 18 try { 19 if(!this.indexedDB) { 20 throw { 21 'code' : -1, 22 'message' : 'í´ë¹ ë¸ë¼ì°ì ììë IndexedDB를 ì§ìíì§ ììµëë¤.' 23 } 24 } 25 26 // ë°ì´í° ë² ì´ì¤ë¥¼ ì´ì´ ì¤ë¤. 27 this.open(dbName, version, tableName, openCallback, initParams); 28 } catch(e) { 29 30 } 31} 32 33/** 34 * ë°ì´í° ë² ì´ì¤ë¥¼ ìì(ì´ê¸°)íë¤. 35 * 기본ì ì¼ë¡ Autoincrement 를 ì¬ì©íë¤. 36 * 37 * @param {string} dbName ë°ì´í° ë² ì´ì¤ ëª 38 * @param {number} version ë²ì (integer í) 39 * @param {string} tableName 40 * @param {function} openCallback 41 * @param {Object} initParams 42 */ 43IDB.prototype.open = function (dbName, version, tableName, openCallback, initParams) { 44 this.dbName = dbName; 45 this.tableName = tableName; 46 this.version = version; 47 if(typeof initParams != 'object') initParams = {}; 48 49 var request = this.indexedDB.open(this.dbName , this.version); 50 request.onerror = function(event) { 51 throw { 52 'code' : event.target.errorCode, 53 'message' : 'ë°ì´í°ë² ì´ì¤ë¥¼ ì´ì§ 못íìµëë¤.' 54 }; 55 }; 56 57 request.onsuccess = function(event) { 58 this.db = event.target.result; 59 60 if(typeof openCallback == 'function') { 61 openCallback(); 62 } 63 }.bind(this); 64 65 request.onupgradeneeded = function(event) { 66 this.db = request.result; 67 68 var objectStore = this.db.createObjectStore( 69 this.tableName, 70 event.hasOwnProperty('params') ? event.params : null 71 ); 72 73 if(event.hasOwnProperty('indices') && event.indices.length > 0) { 74 for(var i in event.indices) { 75 if(event.indices.hasOwnProperty(i)) { 76 objectStore.createIndex( 77 event.indices[i].indexName, 78 event.indices[i].keyPath, 79 event.indices[i].params 80 ); 81 } 82 } 83 } 84 }.bind(this, initParams); 85}; 86 87/** 88 * ë°ì´í° ì½ì ì í¸ì¶í ì½ë°± í¨ì 89 * @type {null} 90 */ 91IDB.prototype.addCallback = null; 92 93/** 94 * ë°ì´í° ë¶ë¬ ì¤ê¸°ì í¸ì¶í ì½ë°± 95 * @type {null} 96 */ 97IDB.prototype.getCallback = null; 98 99/** 100 * ë°ì´í° ë² ì´ì¤ë¥¼ ìì íë¤. 101 * @param dbName 102 */ 103IDB.prototype.deleteDatabase = function(dbName) { 104 window.indexedDB.deleteDatabase(dbName); 105}; 106 107/** 108 * í¸ëìì ì ìì±íë¤. 109 */ 110IDB.prototype.setTransaction = function(tableName, mode) { 111 try{ 112 var transaction = this.db.transaction(tableName, mode); 113 } catch(e) { 114 } 115 116 /* 117 if(typeof this.tnxComplete == 'function') { 118 transaction.oncomplete = this.tnxComplete; 119 } 120 121 if(typeof this.tnxError == 'function') { 122 transaction.oncomplete = this.tnxError; 123 } 124 */ 125 126 return transaction; 127}; 128 129/** 130 * Object store 를 ê°ì ¸ì¨ë¤. 131 * 132 * @param transaction 133 * @param tableName 134 * @returns {IDBObjectStore|*} 135 */ 136IDB.prototype.objectStore = function(transaction, tableName) { 137 var objectStore = transaction.objectStore(tableName); 138 objectStore.onsuccess = function(event) { 139 140 }; 141 142 return objectStore; 143}; 144 145/** 146 * DBì ë°ì´í° ë£ê¸° 147 * 148 * @param data ì½ì í ë°ì´í° 149 * @param callback ì½ë°± (ìµì ) 150 * @param tableName í ì´ë¸ëª (ìµì ) 151 */ 152IDB.prototype.add = function(data, callback , tableName) { 153 if(tableName) { 154 this.tableName = tableName; 155 } 156 157 var transaction; 158 try { 159 transaction = this.setTransaction(this.tableName, "readwrite"); 160 } catch(e) { 161 } 162 163 var objectStore = this.objectStore(transaction, this.tableName); 164 165 var request = objectStore.add(data); 166 167 request.onsuccess = function(event) { 168 if(typeof callback == 'function') { 169 callback(event.target.result); 170 } 171 }; 172}; 173 174 175/** 176 * ë°ì´í° ë² ì´ì¤ìì ê°ì ê°ì§ê³ ì¨ë¤. 177 * 178 * @param key ê°ì ¸ì¬ í¤ 179 * @param callback ì½ë°± í¨ì (ìµì ) 180 * @param tableName í ì´ë¸ëª (ìµì ) 181 */ 182IDB.prototype.get = function(key, callback, tableName) { 183 if(tableName) { 184 this.tableName = tableName; 185 } 186 187 var transaction = this.setTransaction(this.tableName, "readwrite"); 188 189 var objectStore = this.objectStore(transaction, this.tableName); 190 var request = objectStore.get(key); 191 192 request.onsuccess = function(event) { 193 if(typeof callback == 'function') { 194 callback(event.target.result); 195 } 196 }; 197 198 request.onerror = function(event) { 199 }; 200}; 201 202/** 203 * ë°ì´í° ë² ì´ì¤ìì ê°ì ìì íë¤. 204 * @param {string} key 205 * @param {function} callback 206 * @param {string} tableName 207 */ 208IDB.prototype.del = function(key, callback, tableName) { 209 if(tableName) { 210 this.tableName = tableName; 211 } 212 var transaction = this.setTransaction(this.tableName, "readwrite"); 213 214 var objectStore = this.objectStore(transaction, this.tableName); 215 216 var request = objectStore.delete(key); 217 218 request.onsuccess = function(event) { 219 if(typeof callback == 'function') { 220 callback(key); 221 } 222 }; 223 224 request.onerror = function(event) { 225 }; 226}; 227 228/** 229 * í ì´ë¸ì ë¹ìì¤ë¤. 230 * 231 * @param tableName 232 */ 233IDB.prototype.clear = function(tableName) { 234 if(tableName) { 235 this.tableName = tableName; 236 } 237 var transaction = this.setTransaction(this.tableName, "readwrite"); 238 239 var objectStore = this.objectStore(transaction, this.tableName); 240 var request = objectStore.clear(); 241 request.onsuccess = function(event) { 242 }; 243 244 request.onerror = function(event) { 245 }; 246}; 247 248IDB.prototype.search = function(search, callback, tableName) { 249 if(tableName) { 250 this.tableName = tableName; 251 } 252 253 var transaction = this.setTransaction(this.tableName, "readwrite"); 254 255 var objectStore = this.objectStore(transaction, this.tableName); 256 257 var keyRange = window.IDBKeyRange.only(search); 258
259 var request = objectStore.index('keyword').openCursor(keyRange); 260 261 var result = {}; 262 263 request.onsuccess = function(event) { 264 var cursor = event.target.result; 265 if(cursor) { 266 result[cursor.key] = cursor.value; 267 cursor.continue(); 268 } else { 269 if(typeof callback == 'function') { 270 callback(result); 271 } 272 } 273 }; 274}; 275 276/** 277 * 모ë ë°ì´í°ë¥¼ ê°ì§ê³ ì¨ë¤. 278 * 279 * @param callback ë°ì´í°ë¥¼ ê°ì§ê³ ì¨ í ì¤íí ì½ë°± 280 * @param tableName í ì´ë¸ ëª 281 */ 282IDB.prototype.getAll = function(callback, tableName) { 283 if(tableName) { 284 this.tableName = tableName; 285 } 286 287 var transaction = this.setTransaction(this.tableName, "readwrite"); 288 289 var objectStore = this.objectStore(transaction, this.tableName); 290 291 var request = objectStore.openCursor(); 292 293 var result = []; 294 295 request.onsuccess = function(event) { 296 var data, x, cursor = event.target.result; 297 if(cursor) { 298 // result[cursor.key] = cursor.value; 299 var data = { 300 'key' : cursor.key 301 }; 302 for(x in cursor.value) { 303 data[x] = cursor.value[x]; 304 } 305 result.push(data); 306 cursor.continue(); 307 } else { 308 if(typeof callback == 'function') { 309 callback(result); 310 } 311 } 312 }; 313};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.