Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
lucas.parsy
jio
Commits
fbdd12ad
Commit
fbdd12ad
authored
Dec 09, 2015
by
lucas.parsy
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'wsql' into 'lucas_websql'
Some fixes See merge request
!1
parents
23273a36
aaaa7dc8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
148 additions
and
155 deletions
+148
-155
src/jio.storage/websqlstorage.js
src/jio.storage/websqlstorage.js
+110
-105
test/jio.storage/websqlstorage.tests.js
test/jio.storage/websqlstorage.tests.js
+38
-50
No files found.
src/jio.storage/websqlstorage.js
View file @
fbdd12ad
...
@@ -17,62 +17,63 @@
...
@@ -17,62 +17,63 @@
/**
/**
* The JIO Websql Storage extension
* The JIO Websql Storage extension
*
*
* @class
websql
Storage
* @class
WebSQL
Storage
* @constructor
* @constructor
*/
*/
/*jslint unparam: true*/
function
queueSql
(
db
,
query_list
,
argument_list
)
{
function
sqlExec
(
db
,
transac
,
args
)
{
return
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
return
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
/*jslint unparam: true*/
db
.
transaction
(
function
(
tx
)
{
db
.
transaction
(
function
(
tx
)
{
var
len
=
transac
.
length
,
var
len
=
query_list
.
length
,
res
=
[],
res
ult_list
=
[],
i
;
i
;
function
transacSuccess
(
tx
,
results
)
{
function
resolveTransaction
(
tx
,
result
)
{
res
.
push
(
results
);
res
ult_list
.
push
(
result
);
if
(
res
.
length
===
len
)
{
if
(
res
ult_list
.
length
===
len
)
{
resolve
(
res
);
resolve
(
res
ult_list
);
}
}
}
}
function
transacFailure
(
tx
,
error
)
{
function
rejectTransaction
(
tx
,
error
)
{
reject
(
error
);
reject
(
error
);
return
true
;
return
true
;
}
}
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
tx
.
executeSql
(
transac
[
i
],
args
[
i
],
transacSuccess
,
transacFailure
);
tx
.
executeSql
(
query_list
[
i
],
argument_list
[
i
],
resolveTransaction
,
rejectTransaction
);
}
}
},
function
(
tx
,
error
)
{
},
function
(
tx
,
error
)
{
reject
(
error
);
reject
(
error
);
});
});
/*jslint unparam: false*/
});
});
}
}
/*jslint unparam: false*/
function
initDatabase
(
db
)
{
function
initDatabase
(
db
)
{
var
queries
;
var
query_list
=
[
"
CREATE TABLE IF NOT EXISTS document
"
+
queries
=
[
"
CREATE TABLE IF NOT EXISTS documents
"
+
"
(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)
"
,
"
(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)
"
,
"
CREATE TABLE IF NOT EXISTS attachment
"
+
"
CREATE TABLE IF NOT EXISTS attachment
"
+
"
(id VARCHAR, attachment VARCHAR,
"
+
"
(id VARCHAR, attachment VARCHAR,
"
+
"
CONSTRAINT un_id_attachment
UNIQUE (id, attachment))
"
,
"
CONSTRAINT uniq
UNIQUE (id, attachment))
"
,
"
CREATE TABLE IF NOT EXISTS blob
"
+
"
CREATE TABLE IF NOT EXISTS blob
"
+
"
(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)
"
,
"
(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)
"
,
"
CREATE TRIGGER IF NOT EXISTS jIO
remove
"
+
"
CREATE TRIGGER IF NOT EXISTS
remove
"
+
"
BEFORE DELETE ON documents
FOR EACH ROW BEGIN DELETE
"
+
"
BEFORE DELETE ON document
FOR EACH ROW BEGIN DELETE
"
+
"
FROM attachment WHERE id = OLD.id;END;
"
,
"
FROM attachment WHERE id = OLD.id;END;
"
,
"
CREATE TRIGGER IF NOT EXISTS jIO
removeAttachment
"
+
"
CREATE TRIGGER IF NOT EXISTS
removeAttachment
"
+
"
BEFORE DELETE ON attachment FOR EACH ROW
"
+
"
BEFORE DELETE ON attachment FOR EACH ROW
"
+
"
BEGIN DELETE from blob WHERE id = OLD.id
"
+
"
BEGIN DELETE from blob WHERE id = OLD.id
"
+
"
AND attachment = OLD.attachment;END;
"
];
"
AND attachment = OLD.attachment;END;
"
];
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
queries
,
[[],
[],
[],
[],
[]]);
return
queueSql
(
db
,
query_list
,
[[],
[],
[],
[],
[]]);
});
});
}
}
function
websql
Storage
(
spec
)
{
function
WebSQL
Storage
(
spec
)
{
if
(
typeof
spec
.
database
!==
'
string
'
||
!
spec
.
database
)
{
if
(
typeof
spec
.
database
!==
'
string
'
||
!
spec
.
database
)
{
throw
new
TypeError
(
"
database must be a string
"
+
throw
new
TypeError
(
"
database must be a string
"
+
"
which contains more than one character.
"
);
"
which contains more than one character.
"
);
...
@@ -85,41 +86,41 @@
...
@@ -85,41 +86,41 @@
throw
new
TypeError
(
"
blob_len parameter must be a number >= 20
"
);
throw
new
TypeError
(
"
blob_len parameter must be a number >= 20
"
);
}
}
this
.
_blob_length
=
spec
.
blob_length
||
2000000
;
this
.
_blob_length
=
spec
.
blob_length
||
2000000
;
this
.
_init_
ba
se
=
initDatabase
(
this
.
_database
);
this
.
_init_
db_promi
se
=
initDatabase
(
this
.
_database
);
}
}
websql
Storage
.
prototype
.
put
=
function
(
id
,
param
)
{
WebSQL
Storage
.
prototype
.
put
=
function
(
id
,
param
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
,
that
=
this
,
data
S
tring
=
JSON
.
stringify
(
param
);
data
_s
tring
=
JSON
.
stringify
(
param
);
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
[
"
INSERT OR REPLACE INTO
"
+
return
queueSql
(
db
,
[
"
INSERT OR REPLACE INTO
"
+
"
document
s
(id, data) VALUES(?,?)
"
],
"
document(id, data) VALUES(?,?)
"
],
[[
id
,
data
S
tring
]]);
[[
id
,
data
_s
tring
]]);
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
id
;
return
id
;
});
});
};
};
websql
Storage
.
prototype
.
remove
=
function
(
id
)
{
WebSQL
Storage
.
prototype
.
remove
=
function
(
id
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
;
that
=
this
;
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
[
"
DELETE FROM documents
WHERE id = ?
"
],
[[
id
]]);
return
queueSql
(
db
,
[
"
DELETE FROM document
WHERE id = ?
"
],
[[
id
]]);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
_list
)
{
if
(
result
[
0
].
rowsAffected
===
0
)
{
if
(
result
_list
[
0
].
rowsAffected
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
}
return
id
;
return
id
;
...
@@ -127,81 +128,82 @@
...
@@ -127,81 +128,82 @@
};
};
websql
Storage
.
prototype
.
get
=
function
(
id
)
{
WebSQL
Storage
.
prototype
.
get
=
function
(
id
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
;
that
=
this
;
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
[
"
SELECT data FROM documents WHERE id = ?
"
],
[[
id
]]);
return
queueSql
(
db
,
[
"
SELECT data FROM document WHERE id = ?
"
],
[[
id
]]);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
_list
)
{
if
(
result
[
0
].
rows
.
length
===
0
)
{
if
(
result
_list
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
}
return
JSON
.
parse
(
result
[
0
].
rows
[
0
].
data
);
return
JSON
.
parse
(
result
_list
[
0
].
rows
[
0
].
data
);
});
});
};
};
websql
Storage
.
prototype
.
allAttachments
=
function
(
id
)
{
WebSQL
Storage
.
prototype
.
allAttachments
=
function
(
id
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
;
that
=
this
;
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
[
"
SELECT id FROM documents WHERE id = ?
"
],
[[
id
]]);
return
queueSql
(
db
,
[
"
SELECT id FROM document WHERE id = ?
"
,
"
SELECT attachment FROM attachment WHERE id = ?
"
],
[[
id
],
[
id
]]);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
_list
)
{
if
(
result
[
0
].
rows
.
length
===
0
)
{
if
(
result
_list
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
}
return
sqlExec
(
db
,
[
"
SELECT attachment FROM attachment WHERE id = ?
"
],
[[
id
]]);
var
len
=
result_list
[
1
].
rows
.
length
,
})
.
push
(
function
(
result
)
{
var
len
=
result
[
0
].
rows
.
length
,
obj
=
{},
obj
=
{},
i
;
i
;
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
obj
[
result
[
0
].
rows
[
i
].
attachment
]
=
{};
obj
[
result
_list
[
1
].
rows
[
i
].
attachment
]
=
{};
}
}
return
obj
;
return
obj
;
});
});
};
};
function
sendBlobPart
(
blob
,
arg
s
,
index
,
queue
)
{
function
sendBlobPart
(
blob
,
arg
ument_list
,
index
,
queue
)
{
queue
.
push
(
function
()
{
queue
.
push
(
function
()
{
return
jIO
.
util
.
readBlobAsDataURL
(
blob
);
return
jIO
.
util
.
readBlobAsDataURL
(
blob
);
})
})
.
push
(
function
(
strBlob
)
{
.
push
(
function
(
strBlob
)
{
arg
s
[
index
+
3
].
push
(
strBlob
.
currentTarget
.
result
);
arg
ument_list
[
index
+
3
].
push
(
strBlob
.
currentTarget
.
result
);
return
;
return
;
});
});
}
}
websql
Storage
.
prototype
.
putAttachment
=
function
(
id
,
name
,
blob
)
{
WebSQL
Storage
.
prototype
.
putAttachment
=
function
(
id
,
name
,
blob
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
,
that
=
this
,
part
S
ize
=
this
.
_blob_length
;
part
_s
ize
=
this
.
_blob_length
;
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
[
"
SELECT id FROM documents
WHERE id = ?
"
],
[[
id
]]);
return
queueSql
(
db
,
[
"
SELECT id FROM document
WHERE id = ?
"
],
[[
id
]]);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
)
{
var
quer
ies
=
[],
var
quer
y_list
=
[],
arg
s
=
[],
arg
ument_list
=
[],
blob
S
ize
=
blob
.
size
,
blob
_s
ize
=
blob
.
size
,
queue
=
new
RSVP
.
Queue
(),
queue
=
new
RSVP
.
Queue
(),
i
,
i
,
index
;
index
;
...
@@ -209,32 +211,34 @@
...
@@ -209,32 +211,34 @@
if
(
result
[
0
].
rows
.
length
===
0
)
{
if
(
result
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot access subdocument
"
,
404
);
throw
new
jIO
.
util
.
jIOError
(
"
Cannot access subdocument
"
,
404
);
}
}
quer
ies
.
push
(
"
INSERT OR REPLACE INTO attachment(id, attachment)
"
+
quer
y_list
.
push
(
"
INSERT OR REPLACE INTO attachment(id, attachment)
"
+
"
VALUES(?, ?)
"
);
"
VALUES(?, ?)
"
);
arg
s
.
push
([
id
,
name
]);
arg
ument_list
.
push
([
id
,
name
]);
quer
ies
.
push
(
"
DELETE FROM blob WHERE id = ? AND attachment = ?
"
);
quer
y_list
.
push
(
"
DELETE FROM blob WHERE id = ? AND attachment = ?
"
);
arg
s
.
push
([
id
,
name
]);
arg
ument_list
.
push
([
id
,
name
]);
quer
ies
.
push
(
"
INSERT INTO blob(id, attachment, part, blob)
"
+
quer
y_list
.
push
(
"
INSERT INTO blob(id, attachment, part, blob)
"
+
"
VALUES(?, ?, ?, ?)
"
);
"
VALUES(?, ?, ?, ?)
"
);
args
.
push
([
id
,
name
,
-
1
,
blob
.
type
||
"
application/octet-stream
"
]);
argument_list
.
push
([
id
,
name
,
-
1
,
blob
.
type
||
"
application/octet-stream
"
]);
for
(
i
=
0
,
index
=
0
;
i
<
blob
Size
;
i
+=
partS
ize
,
index
+=
1
)
{
for
(
i
=
0
,
index
=
0
;
i
<
blob
_size
;
i
+=
part_s
ize
,
index
+=
1
)
{
quer
ies
.
push
(
"
INSERT INTO blob(id, attachment, part, blob)
"
+
quer
y_list
.
push
(
"
INSERT INTO blob(id, attachment, part, blob)
"
+
"
VALUES(?, ?, ?, ?)
"
);
"
VALUES(?, ?, ?, ?)
"
);
args
.
push
([
id
,
name
,
index
]);
argument_list
.
push
([
id
,
name
,
index
]);
sendBlobPart
(
blob
.
slice
(
i
,
i
+
partSize
),
args
,
index
,
queue
);
sendBlobPart
(
blob
.
slice
(
i
,
i
+
part_size
),
argument_list
,
index
,
queue
);
}
}
queue
.
push
(
function
()
{
queue
.
push
(
function
()
{
return
sqlExec
(
db
,
queries
,
args
);
return
queueSql
(
db
,
query_list
,
argument_list
);
});
});
return
queue
;
return
queue
;
});
});
};
};
websql
Storage
.
prototype
.
getAttachment
=
function
(
id
,
name
,
options
)
{
WebSQL
Storage
.
prototype
.
getAttachment
=
function
(
id
,
name
,
options
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
,
that
=
this
,
part
S
ize
=
this
.
_blob_length
,
part
_s
ize
=
this
.
_blob_length
,
start
,
start
,
end
,
end
,
start_index
,
start_index
,
...
@@ -253,35 +257,36 @@
...
@@ -253,35 +257,36 @@
400
);
400
);
}
}
start_index
=
Math
.
floor
(
start
/
part
S
ize
);
start_index
=
Math
.
floor
(
start
/
part
_s
ize
);
if
(
start
===
0
)
{
start_index
-=
1
;
}
if
(
start
===
0
)
{
start_index
-=
1
;
}
end_index
=
Math
.
floor
(
end
/
part
S
ize
);
end_index
=
Math
.
floor
(
end
/
part
_s
ize
);
if
(
end
%
part
S
ize
===
0
)
{
if
(
end
%
part
_s
ize
===
0
)
{
end_index
-=
1
;
end_index
-=
1
;
}
}
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
var
command
=
"
SELECT part, blob FROM blob WHERE id = ? AND
"
+
var
command
=
"
SELECT part, blob FROM blob WHERE id = ? AND
"
+
"
attachment = ? AND part >= ?
"
,
"
attachment = ? AND part >= ?
"
,
arg
s
=
[
id
,
name
,
start_index
];
arg
ument_list
=
[
id
,
name
,
start_index
];
if
(
end
!==
-
1
)
{
if
(
end
!==
-
1
)
{
command
+=
"
AND part <= ?
"
;
command
+=
"
AND part <= ?
"
;
arg
s
.
push
(
end_index
);
arg
ument_list
.
push
(
end_index
);
}
}
return
sqlExec
(
db
,
[
command
],
[
args
]);
return
queueSql
(
db
,
[
command
],
[
argument_list
]);
})
})
.
push
(
function
(
response
)
{
.
push
(
function
(
response
_list
)
{
var
i
,
var
i
,
blobArray
=
[],
response
,
blob_array
=
[],
blob
,
blob
,
type
;
type
;
response
=
response
[
0
].
rows
;
response
=
response
_list
[
0
].
rows
;
if
(
response
.
length
===
0
)
{
if
(
response
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
}
...
@@ -290,30 +295,30 @@
...
@@ -290,30 +295,30 @@
type
=
response
[
i
].
blob
;
type
=
response
[
i
].
blob
;
start_index
+=
1
;
start_index
+=
1
;
}
else
{
}
else
{
blob
A
rray
.
push
(
jIO
.
util
.
dataURItoBlob
(
response
[
i
].
blob
));
blob
_a
rray
.
push
(
jIO
.
util
.
dataURItoBlob
(
response
[
i
].
blob
));
}
}
}
}
if
((
start
===
0
)
&&
(
options
.
end
===
undefined
))
{
if
((
start
===
0
)
&&
(
options
.
end
===
undefined
))
{
return
new
Blob
(
blob
A
rray
,
{
type
:
type
});
return
new
Blob
(
blob
_a
rray
,
{
type
:
type
});
}
}
blob
=
new
Blob
(
blob
A
rray
,
{});
blob
=
new
Blob
(
blob
_a
rray
,
{});
return
blob
.
slice
(
start
-
(
start_index
*
part
S
ize
),
return
blob
.
slice
(
start
-
(
start_index
*
part
_s
ize
),
end
===
-
1
?
blob
.
size
:
end
===
-
1
?
blob
.
size
:
end
-
(
start_index
*
part
S
ize
),
end
-
(
start_index
*
part
_s
ize
),
"
application/octet-stream
"
);
"
application/octet-stream
"
);
});
});
};
};
websql
Storage
.
prototype
.
removeAttachment
=
function
(
id
,
name
)
{
WebSQL
Storage
.
prototype
.
removeAttachment
=
function
(
id
,
name
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
;
that
=
this
;
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
sqlExec
(
db
,
[
"
DELETE FROM attachment WHERE
"
+
return
queueSql
(
db
,
[
"
DELETE FROM attachment WHERE
"
+
"
id = ? AND attachment = ?
"
],
[[
id
,
name
]]);
"
id = ? AND attachment = ?
"
],
[[
id
,
name
]]);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
)
{
...
@@ -324,26 +329,26 @@
...
@@ -324,26 +329,26 @@
});
});
};
};
websql
Storage
.
prototype
.
hasCapacity
=
function
(
name
)
{
WebSQL
Storage
.
prototype
.
hasCapacity
=
function
(
name
)
{
return
(
name
===
"
list
"
||
(
name
===
"
include
"
));
return
(
name
===
"
list
"
||
(
name
===
"
include
"
));
};
};
websql
Storage
.
prototype
.
buildQuery
=
function
(
options
)
{
WebSQL
Storage
.
prototype
.
buildQuery
=
function
(
options
)
{
var
db
=
this
.
_database
,
var
db
=
this
.
_database
,
that
=
this
,
that
=
this
,
query
=
"
SELECT id
"
;
query
=
"
SELECT id
"
;
return
new
RSVP
.
Queue
()
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
.
push
(
function
()
{
return
that
.
_init_
ba
se
;
return
that
.
_init_
db_promi
se
;
})
})
.
push
(
function
()
{
.
push
(
function
()
{
if
(
options
===
undefined
)
{
options
=
{};
}
if
(
options
===
undefined
)
{
options
=
{};
}
if
(
options
.
include_docs
===
true
)
{
if
(
options
.
include_docs
===
true
)
{
query
+=
"
, data AS doc
"
;
query
+=
"
, data AS doc
"
;
}
}
query
+=
"
FROM document
s ORDER BY id
"
;
query
+=
"
FROM document
"
;
return
sqlExec
(
db
,
[
query
],
[[]]);
return
queueSql
(
db
,
[
query
],
[[]]);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
)
{
var
array
=
[],
var
array
=
[],
...
@@ -361,6 +366,6 @@
...
@@ -361,6 +366,6 @@
});
});
};
};
jIO
.
addStorage
(
'
websql
'
,
websql
Storage
);
jIO
.
addStorage
(
'
websql
'
,
WebSQL
Storage
);
}(
jIO
,
RSVP
,
Blob
,
openDatabase
));
}(
jIO
,
RSVP
,
Blob
,
openDatabase
));
test/jio.storage/websqlstorage.tests.js
View file @
fbdd12ad
...
@@ -29,8 +29,8 @@
...
@@ -29,8 +29,8 @@
db
.
transaction
(
function
(
tx
)
{
db
.
transaction
(
function
(
tx
)
{
/*jslint unparam: true*/
/*jslint unparam: true*/
tx
.
executeSql
(
transac
,
args
,
tx
.
executeSql
(
transac
,
args
,
function
(
tx
,
result
s
)
{
function
(
tx
,
result
)
{
resolve
(
result
s
);
resolve
(
result
);
},
},
function
(
tx
,
error
)
{
function
(
tx
,
error
)
{
reject
(
error
);
reject
(
error
);
...
@@ -41,34 +41,22 @@
...
@@ -41,34 +41,22 @@
}
}
function
spyStorageCreation
(
context
)
{
function
spyStorageCreation
(
context
)
{
var
seven
=
false
;
ok
(
context
.
spy
.
callCount
>=
5
);
if
(
context
.
spy
.
callCount
>=
5
)
{
seven
=
true
;
}
equal
(
seven
,
true
);
/* equal(context.spy.args[0][0],
'SELECT name FROM sqlite_master WHERE type ="table" ' +
'AND name != "__WebKitDatabaseInfoTable__"');
equal(context.spy.args[1][0], 'DELETE FROM documents');
equal(context.spy.args[2][0], 'DELETE FROM metadata');
equal(context.spy.args[3][0], 'DELETE FROM attachment');
equal(context.spy.args[4][0], 'DELETE FROM blob');
*/
equal
(
context
.
spy
.
args
[
0
][
0
],
equal
(
context
.
spy
.
args
[
0
][
0
],
'
CREATE TABLE IF NOT EXISTS document
s
(id VARCHAR PRIMARY
'
+
'
CREATE TABLE IF NOT EXISTS document(id VARCHAR PRIMARY
'
+
'
KEY NOT NULL, data TEXT)
'
);
'
KEY NOT NULL, data TEXT)
'
);
equal
(
context
.
spy
.
args
[
1
][
0
],
equal
(
context
.
spy
.
args
[
1
][
0
],
'
CREATE TABLE IF NOT EXISTS attachment(id VARCHAR, attachment
'
+
'
CREATE TABLE IF NOT EXISTS attachment(id VARCHAR, attachment
'
+
'
VARCHAR, CONSTRAINT un
_id_attachment
UNIQUE (id, attachment))
'
);
'
VARCHAR, CONSTRAINT un
iq
UNIQUE (id, attachment))
'
);
equal
(
context
.
spy
.
args
[
2
][
0
],
equal
(
context
.
spy
.
args
[
2
][
0
],
'
CREATE TABLE IF NOT EXISTS blob(id VARCHAR, attachment
'
+
'
CREATE TABLE IF NOT EXISTS blob(id VARCHAR, attachment
'
+
'
VARCHAR, part INT, blob TEXT)
'
);
'
VARCHAR, part INT, blob TEXT)
'
);
equal
(
context
.
spy
.
args
[
3
][
0
],
equal
(
context
.
spy
.
args
[
3
][
0
],
'
CREATE TRIGGER IF NOT EXISTS
jIO
remove BEFORE DELETE ON
'
+
'
CREATE TRIGGER IF NOT EXISTS remove BEFORE DELETE ON
'
+
'
document
s
FOR EACH ROW BEGIN DELETE FROM attachment
'
+
'
document FOR EACH ROW BEGIN DELETE FROM attachment
'
+
'
WHERE id = OLD.id;END;
'
);
'
WHERE id = OLD.id;END;
'
);
equal
(
context
.
spy
.
args
[
4
][
0
],
equal
(
context
.
spy
.
args
[
4
][
0
],
'
CREATE TRIGGER IF NOT EXISTS
jIO
removeAttachment BEFORE DELETE ON
'
+
'
CREATE TRIGGER IF NOT EXISTS removeAttachment BEFORE DELETE ON
'
+
'
attachment FOR EACH ROW BEGIN DELETE from blob WHERE id = OLD.id
'
+
'
attachment FOR EACH ROW BEGIN DELETE from blob WHERE id = OLD.id
'
+
'
AND attachment = OLD.attachment;END;
'
);
'
AND attachment = OLD.attachment;END;
'
);
}
}
...
@@ -159,7 +147,7 @@
...
@@ -159,7 +147,7 @@
}
}
});
});
test
(
"
can list document
s
"
,
function
()
{
test
(
"
can list document
"
,
function
()
{
var
context
=
this
;
var
context
=
this
;
stop
();
stop
();
...
@@ -184,8 +172,6 @@
...
@@ -184,8 +172,6 @@
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// websqlStorage.buildQuery
// websqlStorage.buildQuery
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
module
(
"
websqlStorage.buildQuery
"
,
{
module
(
"
websqlStorage.buildQuery
"
,
{
setup
:
function
()
{
setup
:
function
()
{
this
.
jio
=
jIO
.
createJIO
({
this
.
jio
=
jIO
.
createJIO
({
...
@@ -212,7 +198,7 @@
...
@@ -212,7 +198,7 @@
return
context
.
jio
.
allDocs
();
return
context
.
jio
.
allDocs
();
})
})
.
then
(
function
()
{
.
then
(
function
()
{
equal
(
context
.
spy
.
args
[
5
][
0
],
'
SELECT id FROM document
s ORDER BY id
'
);
equal
(
context
.
spy
.
args
[
5
][
0
],
'
SELECT id FROM document
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[]);
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
return
;
return
;
...
@@ -242,7 +228,7 @@
...
@@ -242,7 +228,7 @@
})
})
.
then
(
function
()
{
.
then
(
function
()
{
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
SELECT id, data AS doc FROM document
s ORDER BY id
'
);
'
SELECT id, data AS doc FROM document
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[]);
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
return
;
return
;
...
@@ -289,7 +275,7 @@
...
@@ -289,7 +275,7 @@
});
});
});
});
test
(
"
list all document
s
"
,
function
()
{
test
(
"
list all document
"
,
function
()
{
var
context
=
this
;
var
context
=
this
;
stop
();
stop
();
expect
(
1
);
expect
(
1
);
...
@@ -352,13 +338,13 @@
...
@@ -352,13 +338,13 @@
deepEqual
(
result
,
{
deepEqual
(
result
,
{
"
data
"
:
{
"
data
"
:
{
"
rows
"
:
[{
"
rows
"
:
[{
"
id
"
:
"
1
"
,
"
doc
"
:
{
"
title
"
:
"
title1
"
},
"
value
"
:
{}
},
{
"
id
"
:
"
2
"
,
"
id
"
:
"
2
"
,
"
doc
"
:
{
"
title
"
:
"
title2
"
},
"
doc
"
:
{
"
title
"
:
"
title2
"
},
"
value
"
:
{}
"
value
"
:
{}
},
{
"
id
"
:
"
1
"
,
"
doc
"
:
{
"
title
"
:
"
title1
"
},
"
value
"
:
{}
}],
}],
"
total_rows
"
:
2
"
total_rows
"
:
2
}
}
...
@@ -374,7 +360,9 @@
...
@@ -374,7 +360,9 @@
});
});
});
});
/////////////////////////////////////////////////////////////////
// websqlStorage.get
/////////////////////////////////////////////////////////////////
module
(
"
websqlStorage.get
"
,
{
module
(
"
websqlStorage.get
"
,
{
setup
:
function
()
{
setup
:
function
()
{
this
.
jio
=
jIO
.
createJIO
({
this
.
jio
=
jIO
.
createJIO
({
...
@@ -405,10 +393,10 @@
...
@@ -405,10 +393,10 @@
})
})
.
then
(
function
()
{
.
then
(
function
()
{
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
equal
(
context
.
spy
.
args
[
6
][
0
],
'
SELECT data FROM document
s
WHERE id = ?
'
);
'
SELECT data FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
})
})
...
@@ -548,10 +536,10 @@
...
@@ -548,10 +536,10 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
equal
(
context
.
spy
.
args
[
6
][
0
],
'
SELECT id FROM document
s
WHERE id = ?
'
);
'
SELECT id FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
equal
(
context
.
spy
.
args
[
7
][
0
],
equal
(
context
.
spy
.
args
[
7
][
0
],
'
SELECT attachment FROM attachment WHERE id = ?
'
);
'
SELECT attachment FROM attachment WHERE id = ?
'
);
...
@@ -688,7 +676,7 @@
...
@@ -688,7 +676,7 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
})
})
.
then
(
function
()
{
.
then
(
function
()
{
...
@@ -761,9 +749,9 @@
...
@@ -761,9 +749,9 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
'
DELETE FROM document
s
WHERE id = ?
'
);
equal
(
context
.
spy
.
args
[
6
][
0
],
'
DELETE FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
})
})
.
then
(
function
()
{
.
then
(
function
()
{
...
@@ -801,10 +789,10 @@
...
@@ -801,10 +789,10 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
equal
(
context
.
spy
.
args
[
6
][
0
],
'
SELECT id FROM document
s
WHERE id = ?
'
);
'
SELECT id FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
equal
(
context
.
spy
.
args
[
7
][
0
],
equal
(
context
.
spy
.
args
[
7
][
0
],
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
...
@@ -823,7 +811,7 @@
...
@@ -823,7 +811,7 @@
deepEqual
(
context
.
spy
.
args
[
10
][
1
],
deepEqual
(
context
.
spy
.
args
[
10
][
1
],
[
'
foo
'
,
'
attachment1
'
,
0
,
'
data:;base64,YmFy
'
]);
[
'
foo
'
,
'
attachment1
'
,
0
,
'
data:;base64,YmFy
'
]);
equal
(
context
.
spy
.
args
[
11
][
0
],
equal
(
context
.
spy
.
args
[
11
][
0
],
'
SELECT id FROM document
s
WHERE id = ?
'
);
'
SELECT id FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
11
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
11
][
1
],
[
'
foo
'
]);
equal
(
context
.
spy
.
args
[
12
][
0
],
equal
(
context
.
spy
.
args
[
12
][
0
],
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
...
@@ -840,7 +828,7 @@
...
@@ -840,7 +828,7 @@
'
INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)
'
);
'
INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)
'
);
deepEqual
(
context
.
spy
.
args
[
15
][
1
],
deepEqual
(
context
.
spy
.
args
[
15
][
1
],
[
'
foo
'
,
'
attachment2
'
,
0
,
'
data:;base64,YmFyMg==
'
]);
[
'
foo
'
,
'
attachment2
'
,
0
,
'
data:;base64,YmFyMg==
'
]);
equal
(
context
.
spy
.
args
[
16
][
0
],
'
DELETE FROM document
s
WHERE id = ?
'
);
equal
(
context
.
spy
.
args
[
16
][
0
],
'
DELETE FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
16
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
16
][
1
],
[
'
foo
'
]);
})
})
.
then
(
function
()
{
.
then
(
function
()
{
...
@@ -870,7 +858,7 @@
...
@@ -870,7 +858,7 @@
return
context
.
jio
.
put
(
"
foo
"
,
{});
return
context
.
jio
.
put
(
"
foo
"
,
{});
})
})
.
then
(
function
()
{
.
then
(
function
()
{
return
exec
(
"
SELECT id FROM document
s
"
,
[]);
return
exec
(
"
SELECT id FROM document
"
,
[]);
})
})
.
then
(
function
(
selectResult
)
{
.
then
(
function
(
selectResult
)
{
equal
(
selectResult
.
rows
.
length
,
1
,
"
putAttachment done
"
);
equal
(
selectResult
.
rows
.
length
,
1
,
"
putAttachment done
"
);
...
@@ -880,7 +868,7 @@
...
@@ -880,7 +868,7 @@
})
})
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
equal
(
result
,
"
foo
"
);
equal
(
result
,
"
foo
"
);
return
exec
(
"
SELECT id FROM document
s
"
,
[]);
return
exec
(
"
SELECT id FROM document
"
,
[]);
})
})
.
then
(
function
(
selectResult
)
{
.
then
(
function
(
selectResult
)
{
equal
(
selectResult
.
rows
.
length
,
0
,
"
remove done
"
);
equal
(
selectResult
.
rows
.
length
,
0
,
"
remove done
"
);
...
@@ -931,11 +919,11 @@
...
@@ -931,11 +919,11 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
equal
(
context
.
spy
.
args
[
6
][
0
],
'
SELECT id FROM document
s
WHERE id = ?
'
);
'
SELECT id FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
equal
(
context
.
spy
.
args
[
7
][
0
],
equal
(
context
.
spy
.
args
[
7
][
0
],
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
...
@@ -1087,11 +1075,11 @@
...
@@ -1087,11 +1075,11 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
equal
(
context
.
spy
.
args
[
6
][
0
],
'
SELECT id FROM document
s
WHERE id = ?
'
);
'
SELECT id FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
equal
(
context
.
spy
.
args
[
7
][
0
],
equal
(
context
.
spy
.
args
[
7
][
0
],
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
...
@@ -1204,11 +1192,11 @@
...
@@ -1204,11 +1192,11 @@
.
then
(
function
()
{
.
then
(
function
()
{
spyStorageCreation
(
context
);
spyStorageCreation
(
context
);
equal
(
context
.
spy
.
args
[
5
][
0
],
equal
(
context
.
spy
.
args
[
5
][
0
],
'
INSERT OR REPLACE INTO document
s
(id, data) VALUES(?,?)
'
);
'
INSERT OR REPLACE INTO document(id, data) VALUES(?,?)
'
);
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
deepEqual
(
context
.
spy
.
args
[
5
][
1
],
[
'
foo
'
,
'
{"title":"bar"}
'
]);
[
'
foo
'
,
'
{"title":"bar"}
'
]);
equal
(
context
.
spy
.
args
[
6
][
0
],
equal
(
context
.
spy
.
args
[
6
][
0
],
'
SELECT id FROM document
s
WHERE id = ?
'
);
'
SELECT id FROM document WHERE id = ?
'
);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
deepEqual
(
context
.
spy
.
args
[
6
][
1
],
[
'
foo
'
]);
equal
(
context
.
spy
.
args
[
7
][
0
],
equal
(
context
.
spy
.
args
[
7
][
0
],
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
'
INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)
'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment