import bb.cascades 1.0
import bb.cascades.datamanager 1.2
import bb.system 1.0
Page {
id: root
property bool liveUpdate: false;
property string updateRevision: "update revision set revision_id = revision_id + 1"
property string selectRevision: "select revision_id from revision"
property string insertQuery: "insert into artist(revision_id,name,realname,profile,data_quality,primary_image,is_group,master_count,group_master_count) select revision_id,:name,:realname,:profile,:dataquality,:primaryimage,0,0,0 from revision"
titleBar: TitleBar {
kind: TitleBarKind.FreeForm
kindProperties: FreeFormTitleBarKindProperties {
Container {
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
leftPadding: 10
rightPadding: 10
verticalAlignment: VerticalAlignment.Center
Label {
text: "Datamanager Usage"
layoutProperties: StackLayoutProperties {
spaceQuota: 7
}
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.White
}
verticalAlignment: VerticalAlignment.Center
}
}
}
}
Container {
ListView {
id: listView
property alias deleteq: queryExec
property string selectRevision: "select revision_id from revision"
property string deleteQuery: "delete from artist where name = :name"
property variant dq: defaultDataQuery
layout: StackListLayout {
headerMode: ListHeaderMode.Sticky
}
layoutProperties: StackLayoutProperties {
spaceQuota: 1.0
}
horizontalAlignment: HorizontalAlignment.Fill
verticalAlignment: VerticalAlignment.Fill
dataModel: defaultModel
function imageurl(image) {
if ("" == image || !_app.fileExists(image)) {
return _dataDir + "../../native/assets/images/no_image.png";
}
return _dataDir + "images/" + image;
}
listItemComponents: [
ListItemComponent {
StandardListItem {
id: itemRoot
title: ListItemData.name
imageSource: itemRoot.ListItem.view.imageurl(ListItemData.primary_image)
description: ListItemData.realname
function onDeleteExecuted(resultData) {
var revision = resultData[0].revision_id;
console.log("live delete was performed ");
itemRoot.ListItem.view.dq.emitDataChanged(revision);
itemRoot.ListItem.view.deleteq.executed.disconnect(itemRoot.onDeleteExecuted)
}
gestureHandlers: [
LongPressHandler {
onLongPressed: {
itemRoot.ListItem.view.deleteq.queries = [ itemRoot.ListItem.view.selectRevision, itemRoot.ListItem.view.deleteQuery ]
itemRoot.ListItem.view.deleteq.bindValues = { "name" : ListItemData.name }
itemRoot.ListItem.view.deleteq.executed.connect(itemRoot.onDeleteExecuted)
itemRoot.ListItem.view.deleteq.execute()
}
}
]
}
}
]
onDataModelChanged: {
console.log("onDataModelChanged....")
}
onSelectionChanged: {
console.log("onSelectionChanged, selected: " + selected)
}
onActivationChanged: {
console.log("onActivationChanged, active: " + active)
}
}
}
onCreationCompleted: {
defaultModel.load()
}
attachedObjects: [
AsyncDataModel {
id: defaultModel
query: SqlDataQuery {
id: defaultDataQuery
source: _dataDir + "discogs_small.db"
query: "select id, name, realname, data_quality, primary_image, is_group, revision_id from artist order by name"
countQuery: "select count(*) from artist"
onDataChanged: console.log("data changed: revision=" + revision)
onError: console.log("SQL query error: " + code + ", " + message)
}
onLoaded: console.log("initial model data is loaded")
},
AsyncDataModel {
id: withKeyModel
query: SqlDataQuery {
id: kdq
source: _dataDir + "discogs_small.db"
query: "select id, name, realname, data_quality, primary_image, is_group, revision_id from artist order by name"
countQuery: "select count(*) from artist"
keyColumn: "id"
onDataChanged: console.log("data changed: revision=" + revision)
onError: console.log("SQL query error: " + code + ", " + message)
}
onLoaded: console.log("initial model data is loaded")
},
AsyncDataModel {
id: withRevisionModel
query: SqlDataQuery {
id: rdq
source: _dataDir + "discogs_small.db"
query: "select id, name, realname, data_quality, primary_image, is_group, revision_id from artist order by name"
countQuery: "select count(*) from artist"
revisionColumn: "revision_id"
revisionQuery: "SELECT revision_id FROM revision"
onDataChanged: console.log("data changed: revision=" + revision)
onError: console.log("SQL query error: " + code + ", " + message)
}
onLoaded: console.log("initial model data is loaded")
},
AsyncDataModel {
id: withKeyAndRevisionModel
query: SqlDataQuery {
id: krdq
source: _dataDir + "discogs_small.db"
query: "select id, name, realname, data_quality, primary_image, is_group, revision_id from artist order by name"
countQuery: "select count(*) from artist"
keyColumn: "id"
revisionColumn: "revision_id"
revisionQuery: "SELECT revision_id FROM revision"
onDataChanged: console.log("data changed: revision=" + revision)
onError: console.log("SQL query error: " + code + ", " + message)
}
onLoaded: console.log("initial model data is loaded")
},
QueryExec {
//
property string updateContact: "update artist set revision_id = (select revision_id from revision) " +
"where rowid >= :startRow and rowid < (:startRow + 5)"
property int startRow: 0
id: updateQuery
times: 10
interval: 5000
source: _dataDir + "discogs_small.db"
queries: [ updateRevision, updateContact, selectRevision ]
bindValues: { "startRow" : startRow }
onError: console.log("live update error: " + errorType + ", " + errorMessage)
onExecuted: {
var revision = data[0].revision_id;
console.log("live query update was performed: startRow=" + startRow + "; revision=" + revision);
listView.dq.emitDataChanged(revision);
startRow = (startRow + 100) % 1000;
}
},
QueryExec {
id: queryExec
source: _dataDir + "discogs_small.db"
queries: [ ]
onError: console.log("error: " + errorType + ", " + errorMessage)
},
SystemToast {
id: deleteToast
body: qsTr("Long press an item to remove from list")
button.label: qsTr("Ok")
button.enabled: true
}
]
actions: [
ActionItem {
title: "sqlDataQuery+key"
imageSource: "images/query_with_key.png"
onTriggered: {
withKeyModel.load()
listView.dq = kdq
listView.dataModel = withKeyModel
}
},
ActionItem {
title: "sqlDataQuery+rev"
imageSource: "images/query_with_revision.png"
onTriggered: {
withRevisionModel.load()
listView.dq = rdq
listView.dataModel = withRevisionModel
}
},
ActionItem {
title: "sqlDataQuery+key+rev"
imageSource: "images/both.png"
onTriggered: {
withKeyAndRevisionModel.load()
listView.dq = krdq
listView.dataModel = withKeyAndRevisionModel
}
},
ActionItem {
ActionBar.placement: ActionBarPlacement.OnBar
title: "LiveUpdate"
imageSource: "images/ic_edit.png"
onTriggered: {
if(!liveUpdate) {
updateQuery.execute()
liveUpdate = true
} else {
updateQuery.stop()
liveUpdate = false
}
}
},
ActionItem {
ActionBar.placement: ActionBarPlacement.OnBar
title: "delete"
imageSource: "images/ic_delete.png"
onTriggered: {
deleteToast.exec()
}
},
ActionItem {
ActionBar.placement: ActionBarPlacement.OnBar
title: "Reset"
imageSource: "images/list_reset.png"
onTriggered: {
defaultModel.load()
listView.dq = defaultDataQuery
listView.dataModel = defaultModel
}
},
ActionItem {
id: addAction
ActionBar.placement: ActionBarPlacement.OnBar
title: "Add"
imageSource: "images/ic_add.png"
function onAddExecuted(resultData) {
console.log("live record addition ")
var revision = resultData[0].revision_id;
listView.dq.emitDataChanged(revision);
queryExec.executed.disconnect(addAction.onAddExecuted)
}
onTriggered: {
queryExec.queries = [ updateRevision, selectRevision, insertQuery]
queryExec.bindValues = { "name" : "Homer" + Math.random().toString(36).substring(2, 8),
"realname" : "Baercis",
"profile" : "Sr.Attorney at large",
"dataquality" : "Correct",
"primaryimage" : "blah.jpg"}
queryExec.executed.connect(addAction.onAddExecuted)
queryExec.execute()
}
}
]
}