-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Parashuram
committed
Mar 30, 2011
1 parent
4fc0fd7
commit 77c7d75
Showing
4 changed files
with
387 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>linq4idb</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
</buildSpec> | ||
<natures> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,111 @@ | ||
"My GitHub Page" | ||
<html> | ||
<head> | ||
<title>Linq4DB Test</title> | ||
<style> | ||
li { | ||
padding: 0.2em 1em; | ||
} | ||
|
||
#console { | ||
clear:both; | ||
margin: 1em 0 1em 0; | ||
padding: 0.5em; | ||
height : 50%; | ||
overflow-y : scroll; | ||
border: SOLID 1px GRAY; | ||
} | ||
|
||
#console div { | ||
padding: 0.2em; | ||
font-size: 0.8em; | ||
color: #333333; | ||
} | ||
|
||
#code{ | ||
font-family : courier; | ||
font-size : 1.5em; | ||
margin : 1em; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<ol id = "examples"> | ||
</ol> | ||
<div id = "code"> | ||
{{ LINQ Code will be show here }} | ||
</div> | ||
<a href = "javascript:clear()" style = "clear:both; display: block; ">Clear Console</a> | ||
<div id = "console"> | ||
</div> | ||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"> | ||
</script> | ||
<script src= "linq4idb.js" type="text/javascript"> | ||
</script> | ||
<script charset="utf-8"> | ||
function clear(){ | ||
$("#console").empty(); | ||
} | ||
|
||
function data(){ | ||
return { | ||
"bookName": "bookName-" + parseInt(Math.random() * 100), | ||
"price": parseInt(Math.random() * 1000), | ||
"checkedOut": new Date() | ||
} | ||
}; | ||
|
||
function showRecords(record){ | ||
$("#console").append($("<div>").html(JSON.stringify(record))); | ||
//console.info.apply(this, arguments); | ||
} | ||
|
||
var examples = { | ||
"To ObjectStore, Add Data": function(){ | ||
linq4idb().to("BookList")["in"]("LibraryDB").add(data()).done(showRecords); | ||
}, | ||
"From ObjectStore Select and print each": function(){ | ||
linq4idb().from("BookList")["in"]("LibraryDB").select().forEach(showRecords); | ||
}, | ||
"From ObjectStore, Order By Price and Print All": function(){ | ||
linq4idb().from("BookList")["in"]("LibraryDB").orderby("price").select().forEach(showRecords); | ||
}, | ||
"From ObjectStore, Order By Price REVERSED and Print All": function(){ | ||
linq4idb().from("BookList")["in"]("LibraryDB").orderby("price", 1).select().forEach(showRecords); | ||
}, | ||
|
||
"From ObjectStore, Where property Equals": function(){ | ||
linq4idb().from("BookList")["in"]("LibraryDB").where("price", { | ||
"equals": 450 | ||
}).select().forEach(showRecords) | ||
}, | ||
"From ObjectStore, Where property in Range": function(){ | ||
linq4idb().from("BookList")["in"]("LibraryDB").where("price", { | ||
"range": [200, 450] | ||
}).select().forEach(showRecords) | ||
}, | ||
|
||
"From ObjectStore, Where property in Range and Ordered": function(){ | ||
linq4idb().from("BookList")["in"]("LibraryDB").where("price", { | ||
"range": [200, 450] | ||
}).orderByDesc().select().forEach(showRecords) | ||
}, | ||
|
||
|
||
"Remove ObjectStore in Database": function(){ | ||
linq4idb().remove("BookList")["in"]("LibraryDB").done(showRecords); | ||
} | ||
} | ||
|
||
for (example in examples) { | ||
$("#examples").append($("<li><a class = 'command' href = 'javascript:examples[\"" + example + "\"]()'>" + example + "</a></li>")); | ||
} | ||
|
||
$(".command").live("click", function(){ | ||
var code = examples[$(this).html()].toString(); | ||
var start = "function (){"; | ||
code = code.substring(code.indexOf(start) + start.length + 2, code.length - 1); | ||
$("#code").html(code); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB; | ||
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange; | ||
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; | ||
|
||
|
||
var linq4idb = function(config){ | ||
var dbPromise = function(dbName){ | ||
return $.Deferred(function(dfd){ | ||
//console.debug("Opening Database", dbName); | ||
var req = window.indexedDB.open(dbName); | ||
req.onsuccess = function(){ | ||
//console.debug("Database opened", req.result); | ||
dfd.resolve(req.result); | ||
}; | ||
req.onerror = function(e){ | ||
//console.debug("Error while opening database", e); | ||
dfd.reject(e); | ||
}; | ||
}).promise(); | ||
}; | ||
|
||
var versionChangePromise = function(db){ | ||
return $.Deferred(function(dfd){ | ||
var req = db.setVersion(isNaN(parseInt(db.version, 10)) ? 0 : parseInt(db.version, 10) + 1); | ||
//console.debug("Starting to Changing Version", req); | ||
req.onsuccess = function(){ | ||
//console.debug("Database version changed to ", req.result); | ||
req.result.oncomplete = function(){ | ||
req.result.db.close(); | ||
} | ||
dfd.resolve(req.result); | ||
}; | ||
req.onblocked = function(e){ | ||
//console.debug("Transaction is blocked", e); | ||
}; | ||
req.onerror = function(e){ | ||
//console.debug("Transaction error", e); | ||
dfd.reject(e); | ||
}; | ||
}).promise(); | ||
} | ||
|
||
var objectStorePromise = function(objectStoreName, dbName, transactionType){ | ||
return $.Deferred(function(dfd){ | ||
$.when(dbPromise(dbName)).then(function(db){ | ||
try { | ||
//console.debug("Starting to create transaction"); | ||
var transaction = db.transaction([objectStoreName], transactionType || IDBTransaction.READ_WRITE); | ||
transaction.oncomplete = function(){ | ||
//console.debug("Transaction Compelte", transaction); | ||
db.close(); | ||
} | ||
dfd.resolve(transaction.objectStore(objectStoreName)); | ||
} | ||
catch (e) { | ||
//console.debug(objectStoreName, "object store not found, so creating it", transactionType); | ||
$.when(versionChangePromise(db)).then(function(transaction){ | ||
transaction.db.createObjectStore(objectStoreName, { | ||
"autoIncrement": true, | ||
"keyPath": "id" | ||
}, true); | ||
dfd.resolve(transaction.objectStore(objectStoreName)); | ||
}); | ||
} | ||
}); | ||
}).promise(); | ||
}; | ||
|
||
var indexPromise = function(indexName, objectStorePromise){ | ||
return $.Deferred(function(dfd){ | ||
$.when(objectStorePromise).then(function(objectStore){ | ||
//console.debug("Index on ", indexName, objectStorePromise); | ||
try { | ||
dfd.resolve(objectStore.index(indexName + "-index")); | ||
} | ||
catch (e) { | ||
var name = objectStore.transaction.db.name; | ||
objectStore.transaction.abort(); | ||
//console.debug(indexName, "not found, so creating one"); | ||
$.when(versionChangePromise(objectStore.transaction.db)).then(function(transaction){ | ||
//console.debug("Database Version Changed, so now creating the index"); | ||
var index = transaction.objectStore(objectStore.name).createIndex(indexName + "-index", indexName); | ||
transaction.oncomplete = function(){ | ||
transaction.db.close(); | ||
} | ||
//console.debug("Index created", index); | ||
dfd.resolve(index); | ||
}); | ||
} | ||
}); | ||
}).promise(); | ||
} | ||
|
||
var cursorPromise = function(sourcePromise, range, direction){ | ||
return $.Deferred(function(dfd){ | ||
//console.debug("Opening cursor on ", sourcePromise, range, direction); | ||
$.when(sourcePromise).then(function(source){ | ||
var req = source.openCursor(range, direction); | ||
req.onsuccess = function(){ | ||
dfd.resolve(req); | ||
}; | ||
req.onerror = function(e){ | ||
dfd.reject(e); | ||
} | ||
}); | ||
}).promise(); | ||
} | ||
|
||
var selectObject = function(cursorPromise, projection){ | ||
var iterator = function(callback){ | ||
$.when(cursorPromise).then(function(cursorRequest){ | ||
function iterator(){ | ||
if (cursorRequest.result) { | ||
callback(cursorRequest.result.value, cursorRequest.result.key); | ||
cursorRequest.result["continue"](); | ||
} | ||
} | ||
cursorRequest.onsuccess = iterator; | ||
iterator(); | ||
}); | ||
} | ||
return { | ||
"forEach": iterator, | ||
"getAll": function(callback){ | ||
var result = []; | ||
iterator(function(e){ | ||
result.push[e]; | ||
}); | ||
return result; | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
"to": function(objectStoreName){ | ||
return { | ||
"in": function(dbName){ | ||
return { | ||
"add": function(data){ | ||
return $.Deferred(function(dfd){ | ||
$.when(objectStorePromise(objectStoreName, dbName)).then(function(objectStore){ | ||
var req = objectStore.add(data); | ||
req.onsuccess = function(){ | ||
dfd.resolve(req.result, data); | ||
} | ||
}); | ||
}).promise(); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
|
||
"from": function(objectStoreName){ | ||
return { | ||
"in": function(dbName){ | ||
return { | ||
"orderby": function(sortProperty, direction){ | ||
return { | ||
"select": function(){ | ||
return selectObject(cursorPromise(indexPromise(sortProperty, objectStorePromise(objectStoreName, dbName, IDBTransaction.READ)), null, direction)); | ||
} | ||
} | ||
}, | ||
"where": function(property, clause){ | ||
if (clause.equals) { | ||
var range = new IDBKeyRange.bound(clause.equals, clause.equals, false, false); | ||
} | ||
else | ||
if (clause.range) { | ||
range = new IDBKeyRange.bound(clause.range[0], clause.range[1], clause.range[2], clause.range[3]); | ||
} | ||
return { | ||
"select": function(){ | ||
return selectObject(cursorPromise(indexPromise(property, objectStorePromise(objectStoreName, dbName, IDBTransaction.READ)), range)); | ||
}, | ||
"orderByDesc": function(){ | ||
return { | ||
"select": function(){ | ||
return selectObject(cursorPromise(indexPromise(property, objectStorePromise(objectStoreName, dbName, IDBTransaction.READ)), range)); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"select": function(){ | ||
return selectObject(cursorPromise(objectStorePromise(objectStoreName, dbName, IDBTransaction.READ))); | ||
} | ||
}; | ||
} | ||
} | ||
}, | ||
|
||
"remove": function(objectStoreName){ | ||
return { | ||
"in": function(dbName){ | ||
return $.Deferred(function(dfd){ | ||
$.when(dbPromise(dbName)).then(function(db){ | ||
$.when(versionChangePromise(db)).then(function(transaction){ | ||
transaction.db.deleteObjectStore(objectStoreName); | ||
//console.debug("Removing", objectStoreName); | ||
dfd.resolve(transaction.db); | ||
}); | ||
}) | ||
}).promise(); | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<script> | ||
|
||
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB; | ||
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange; | ||
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; | ||
|
||
function print(){ | ||
console.log("Print Started"); | ||
var req = window.indexedDB.open("LibraryDB"); | ||
req.onsuccess = function(){ | ||
var transaction = req.result.transaction(["BookList"], 1, 1); | ||
transaction.oncomplete = function(){ | ||
console.log("Print Transaction complete"); | ||
req.result.close(); | ||
} | ||
var objectStore = transaction.objectStore("BookList"); | ||
var creq = objectStore.openCursor(); | ||
function iterate(){ | ||
if (creq.result) { | ||
console.log(creq.result.value); | ||
creq.result["continue"](); | ||
} | ||
} | ||
|
||
creq.onsuccess = iterate; | ||
}; | ||
req.onerror = function(){ | ||
console.log("Print Transacton Error"); | ||
} | ||
req.onblocked = function(){ | ||
console.log("Print Transaction Blocked"); | ||
} | ||
} | ||
|
||
|
||
function version(){ | ||
console.log("Version Change started"); | ||
var req = window.indexedDB.open("LibraryDB"); | ||
req.onsuccess = function(){ | ||
var db = req.result; | ||
var sreq = db.setVersion(parseInt(Math.random() * 1000)); | ||
sreq.onsuccess = function(){ | ||
console.log("Version changed"); | ||
} | ||
sreq.onerror = function(){ | ||
console.log("Error Version change"); | ||
} | ||
sreq.onblocked = function(e){ | ||
console.log("Transaction is blocked", e); | ||
}; | ||
}; | ||
} | ||
</script> | ||
<h1><a href = "javascript:version()">Version</a></h1> | ||
<h1><a href = "javascript:print()">Print</a></h1> |