Debian Codegraph @

maz-db/example/qml/Main.qml

raw
1 import QtQuick 2.4
2 import QtQuick.Window 2.1
3 //import Ubuntu.Components 1.3
4 import MazDB 1.0
5
6 Window {
7 id: window
8 width: 300
9 height: 300
10 visible: true
11
12 Item {
13 anchors.fill: parent
14 }
15
16 /* You can use a MazDBSettings instance like Settings from qt.labs.Settings
17 *
18 * The database (settings.db) is stored in your applications config directory
19 *
20 * All declared properties will get reloaded on app start.
21 * Readonly properties are not stored in the database so work nice as constants
22 *
23 * The main thing that this provides over qt.labs.Settings is property change notifications
24 * over multiple instances, which is cool!
25 */
26 MazDBSettings {
27 id: settings
28 property alias windowWidth: window.width
29 property alias windowHeight: window.height
30 }
31
32 MazDBSettings {
33 id: dbSettings
34 // You can seperate settings using categories, just like in qt.labs.Settings
35 category: "db-settings"
36 // Readonly properties are not stored so cannot be changed in the underlying DB
37 readonly property string source: Qt.resolvedUrl("/tmp/maztest4.db")
38 property string helloValue: "Heya!! Created via the rolesCallBack"
39 property string worldValue: "Or another callback role: "
40 }
41
42 /* MazDB is the low level api that interfaces with leveldb
43 *
44 * You can perform all leveldb actions put()/putSync()/del()/batch()/readStream()/destroyDB()/repairDB()
45 *
46 * If you have more than one MazDB instance with the same source, key/value changes will
47 * get propogated to all instances. This is the basis on how MazDBListModel functions.
48 *
49 * You can store any QVariant compatible data type in MazDB from primitive types, bytearrays to json objects/arrays or QVariantMap/Hash
50 * even any custom c++ types as long as they are QVariant compatible and support data streams.
51 * Take a look in MazUtils for all supported types.
52 */
53 MazDB {
54 id: db
55
56 source: dbSettings.source
57
58 // This shows a few ways of interacting with the MazDB api
59 Component.onCompleted: {
60 // Add multiple key/values in one batch
61 db.batch()
62 .put("item1","asdf")
63 .put("item1/~", "asdf")
64 .put("item2", "some other value")
65 .put("item1/abc", "What about this?")
66 .put("item2/foo","asdf")
67 .put("item2/~", "asdf")
68 .put("item2/foo/bar", "some other value")
69 .put("item1/abc/def", "What about this?")
70 .write()
71
72 // Put key/value async
73 db.put("async", "async item")
74
75 // Synchronous Put
76 var didPut = db.putSync("sync", "Sync item")
77 if (didPut) {
78 console.log("Synchronous put() success!")
79 // get the value we just put in
80 console.log("sync value: ", db.get("sync"))
81 }
82
83 // If you want to batch write at runtime
84 // take a batch instance and run a loop over your data
85 var batch = db.batch()
86 for (var i = 0; i < 100; ++i) {
87 batch.put("loop" + i, "value " + i)
88 }
89 // Finally write the batch in one go. Pretty nice eh? :-)
90 batch.write()
91
92 // You can also read back every key/value we just put in
93 // via the readStream api.
94 db.readStream(function (key,value){
95 console.log("-- ReadStream --")
96 console.log("Key: ", key)
97 console.log("Value: ", value)
98 console.log("----------------")
99 // return false if you want to end the readStream early. i.e you found what your looking for
100 return true;
101 }, "item2") // You can optionally provide a startkey to slice the stream
102
103 // There's also a keyStream, valueStream and rangeStream
104 db.keyStream(function(key) {
105 console.log("Key: ", key);
106 return true;
107 })
108
109 db.valueStream(function(value){
110 console.log("Val: ", value)
111 return true
112 })
113
114 db.rangeStream(function (key, value) {
115 console.log("RangeKey: ", key)
116 return true;
117 }, "item1", "loop3") // Only stream keys in this range.
118
119 // also let's call the queryStream
120 query.queryStream(function (key, value) {
121 console.log("-- Query Stream --")
122 console.log("Key: ", key)
123 console.log("Value: ", value)
124 console.log("------------------")
125 return true
126 })
127 }
128 }
129
130 MazDBQueryEngine {
131 id: query
132 source: dbSettings.source
133 query: MazDBQuery {
134 // You can define a range to query between
135 // If not defined a full scan is performed.
136 range {
137 start: "item1"
138 end: "loop6"
139 }
140 // Set a limit for the number of results your after.
141 // Note: results will be returned based on the lexicographical ordering
142 // of the keys.
143 limit: 10
144
145 // Define the query to perform
146 definition: [
147 // All queries MUST contain a WHERE clause
148 // which is the first to be evaluated. It doesn't have to be
149 // the first in the list it just has to exist :-)
150 WHERE {
151 // you can define a property to evaluate. Only works with a subset of types.
152 // QRect/QPoint/QSize/QVariantMap/QVariantHash/QJsonObject
153 property: ""
154 // The value to compare
155 value: "asdf"
156 // Eval operator to use ==, !=, <, >, <=, >=
157 matchRule: Query.Equals
158 },
159 // Queries can optionally include an OR clause which is
160 // evaluated if WHERE doesn't match
161 OR {
162 property: ""
163 value: "value 3"
164 matchRule: Query.Equals
165 },
166 // Queries can also have multiple AND clauses which
167 // are evaluated if WHERE or OR find a match.
168 AND {
169 property: ""
170 value: undefined
171 matchRule: Query.LessThan
172 },
173 AND {
174 // You can also provide your own custom comparitor for WHERE, OR, AND if
175 // you want to do something a little different or the default comparitor
176 // doesn't support your data type yet.
177 // The callback function will be passed the value to compare and you
178 // must return a bool result.
179 comparitor: function (value) {
180 console.log("Custom comparator callback! ", value)
181 return true;
182 }
183 }
184 ]
185 }
186 }
187 // MainView {
188 // anchors.fill: parent
189
190 // Page {
191 // id: page
192 // title: "MazDB Example"
193
194
195
196 // MazDBListModel {
197 // id: mazDbListModel
198
199 // /* Uncomment below to filter by a specific Key */
200 //// filter: "item1"
201 // filterPolicy: MazDBListModel.Contains // there's also MazDBListModel.ExactMatch && MazDBListModel.StartsWith
202
203 // // MazDBListModel _doesn't_ take a MazDB instance as it's source
204 // // but rather the db's location for which the listmodel should observe.
205 // // the model will listen for put/del events as well
206 // // as do an initial load of any currently stored data.
207 // source: dbSettings.source
208
209 // /* By default this ListModel only has two rolenames "key" and "value" which
210 // * represent the key/value data stored in the leveldb instance.
211 // * You can extend these by adding customRoles which are appended to the roleNames() method.
212 // * These can then be accessed in your model delegates with model.${customRole}
213 // */
214 // customRoles: ["hello", "world"]
215
216 // /* If you supply custom roles then you need to implement a callback
217 // * that the list model will use to resolve a rolename.
218 // * The callback is passed the requested "role" and the current index's "key" and "value".
219 // * You can return any QVariant that can be handled by your delegate from this callback.
220 // */
221 // rolesCallBack: function (role, key, value) {
222 // console.log("Custom roles callback called!")
223 // if (role === "hello") {
224 // return dbSettings.helloValue + value
225 // } else {
226 // return dbSettings.worldValue + role
227 // }
228 // }
229 // }
230
231 // MazDBSortProxy {
232 // id: proxy
233
234 // // make sure the listmodel is initialized before adding it to the proxy
235 // model: mazDbListModel.source ? mazDbListModel : null
236
237 // // Which property you want to sort on
238 // sortBy: "value"
239
240 // // Sort order. Default is Qt.Ascending
241 // sortOrder: Qt.DescendingOrder
242
243 // /* You can supply a custom callback for the sort handling.
244 // * this will override calling QSortFilterProxyModel::lessThan()
245 // * From here you can sculpt your own ordering recipe. The passed arguments left/right
246 // * is the data behind the sortBy rolename of the two items being compared.
247 // * So using the "value" role and the returned raw variant object
248 // * you can actually come up with some pretty complex rules.
249 // *
250 // * And guess what... no need to dive into c++ for this anymore :-D
251 // */
252 // sortCallBack: function (left, right) {
253 // console.log("Custom sort callback called!")
254 // return left < right
255 // }
256 // }
257
258 // ListView {
259 // anchors.fill: parent
260 // model: proxy
261 // delegate: ListItem {
262 // height: layout.height + divider.height
263 // ListItemLayout {
264 // id: layout
265 // title.text: model.value
266 // // let's call one of the customRoles and trigger the rolesCallBack
267 // subtitle.text: index % 2 ? model.world : model.hello
268 // }
269 // // delete item
270 // onClicked: db.del(model.key)
271 // }
272 // }
273 // }
274 // }
275 }