← Back to team overview

uonedb-qt team mailing list archive

[Merge] lp:~kalikiana/u1db-qt/queryLog into lp:u1db-qt

 

Christian Dywan has proposed merging lp:~kalikiana/u1db-qt/queryLog into lp:u1db-qt.

Commit message:
Query improvements and more advanced example

Requested reviews:
  U1DB Qt developers (uonedb-qt)
Related bugs:
  Bug #1266478 in U1DB Qt/ QML: "Query model doesn't work properly with a multilevel document list"
  https://bugs.launchpad.net/u1db-qt/+bug/1266478
  Bug #1271972 in U1DB Qt/ QML: "Top-level fields in document cannot be indexed"
  https://bugs.launchpad.net/u1db-qt/+bug/1271972
  Bug #1271977 in U1DB Qt/ QML: "Make debugging index/ query easier"
  https://bugs.launchpad.net/u1db-qt/+bug/1271977

For more details, see:
https://code.launchpad.net/~kalikiana/u1db-qt/queryLog/+merge/204327
-- 
https://code.launchpad.net/~kalikiana/u1db-qt/queryLog/+merge/204327
Your team U1DB Qt developers is requested to review the proposed merge of lp:~kalikiana/u1db-qt/queryLog into lp:u1db-qt.
=== renamed directory 'examples/u1db-qt-example-4' => 'examples/bookmarks'
=== renamed file 'examples/u1db-qt-example-4/u1db-qt-example-4.qml' => 'examples/bookmarks/bookmarks.qml'
--- examples/u1db-qt-example-4/u1db-qt-example-4.qml	2013-05-02 18:19:33 +0000
+++ examples/bookmarks/bookmarks.qml	2014-01-31 19:31:39 +0000
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 2013 Canonical, Ltd.
+ * Copyright (C) 2014 Canonical, Ltd.
  *
  * Authors:
- *  Kevin Wright <kevin.wright@xxxxxxxxxxxxx>
+ *  Christian Dywan <christian.dywan@xxxxxxxxxxxxx>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -20,6 +20,8 @@
 import QtQuick 2.0
 import U1db 1.0 as U1db
 import Ubuntu.Components 0.1
+import Ubuntu.Components.ListItems 0.1 as ListItem
+import Ubuntu.Components.Popups 0.1
 
 /*!
 
@@ -33,452 +35,131 @@
   */
 
 MainView {
-        width: units.gu(45)
-        height: units.gu(80)
-
-        /*!
-
-            A Database is very simple to create. It only needs an id and a path where the file will be created. A Database is a model, which can be used by elements, such as the ListView further in this example.
-
-            U1db.Database {
-                id: aDatabase
-                path: "aDatabase4"
-            }
-
-        */
-
-        U1db.Database {
-            id: aDatabase
-            path: "aDatabase4"
-        }
-
-        /*!
-
-            A Document can be declared at runtime. It requires at the very least a unique 'docId', but that alone won't do anything special. The snipet below snippet demonstrates the basic requirements.
-
-            In addition to this, this example displays text from the database for a specific docId and id key in a text area called 'documentContent. To update the text area at startup with either the default value or a value from the database the onCompleted function is utilized, which is also demonstrated below.
-
-            U1db.Document {
-                id: aDocument
-                database: aDatabase
-                docId: 'helloworld'
-                create: true
-                defaults: { "helloworld":"Hello World" }
-
-                Component.onCompleted: {
-                    documentContent.text = aDocument.contents.helloworld
-                }
-
-            }
-
-        */
-
-
-       U1db.Document {
-            id: aDocument
-            database: aDatabase
-            docId: 'helloworld'
-            create: true
-            defaults: { "helloworld":"Hello World" }
-
-            Component.onCompleted: {
-                documentContent.text = aDocument.contents.helloworld
-            }
-
-        }
-
-       /*!
-
-         It should be possible to use a document without a database, as demonstrated in this snippet. Additionally this document will use the concept of sub-keys, as exemplified by the "bookmarks" id key + contents. This example will attempt to use the bookmark document to store docId values from the database, which will be displayed in a ListView on the second tab of the application. The user will be able to select a value from the ListView and the first tab will be modified accordingly.
-
-       U1db.Document {
-            id: aBookmarkDocument
-            docId: 'bookmarks'
-            create: true
-            defaults: { "bookmarks": [{}] }
-       }
-
-
-         */
-
-
-       U1db.Document {
-            id: aBookmarkDocument
-            docId: 'bookmarks'
-            create: true
-            defaults: { "bookmarks": [{}] }
-       }
-
-
-       function switchToPreviousDocument(documentObject){
-
-          aDocument.docId = getPreviousDocumentId(documentObject)
-
-          }
-
-       function switchToNextDocument(){
-
-          aDocument.docId = getNextDocumentId(aDocument)
-
-        }
-
-       function getPreviousDocumentId(documentObject){
-
-           if(typeof documentObject!='undefined'){
-
-               /*!
-
-                 The listDocs method retrieves all the docId values from the current database. In this demonstration the values are put into an array, which is then checked to locate the docId for the current and previous documents within the database.
-
-               var documentIds = {}
-
-               documentIds = documentObject.database.listDocs()
-
-               for(var i = 0; i < documentIds.length; i++){
-
-                   if(documentIds[i]===documentObject.docId && i > 0){
-                       return documentIds[i-1]
-                   }
-                   else if(documentIds[i]===documentObject.docId && i==0){
-                       return documentIds[documentIds.length-1]
-                   }
-
-               }
-
-                 */
-
-               var documentIds = {}
-
-               documentIds = documentObject.database.listDocs()
-
-               for(var i = 0; i < documentIds.length; i++){
-
-                   if(documentIds[i]===documentObject.docId && i > 0){
-                       return documentIds[i-1]
-                   }
-                   else if(documentIds[i]===documentObject.docId && i==0){
-                       return documentIds[documentIds.length-1]
-                   }
-
-               }
-
-               return documentIds[0]
-
-           }
-
-           else{
-
-               print("Error!")
-
-               return ''
-           }
-
-       }
-
-       function getNextDocumentId(documentObject){
-
-           if(typeof documentObject!='undefined'){
-
-               var documentIds = documentObject.database.listDocs()
-
-               for(var i = 0; i < documentIds.length; i++){
-
-                   if(documentIds[i]===documentObject.docId && i < (documentIds.length-1)){
-                       return documentIds[i+1]
-                   }
-                   else if(documentIds[i]===documentObject.docId && i==(documentIds.length-1)){
-                       return documentIds[0]
-                   }
-
-               }
-
-               return documentIds[0]
-
-           }
-
-           else{
-
-               print("Error!")
-
-               return ''
-           }
-
-       }
-
-        function getCurrentDocumentKey(contentsObject){
-
-            if(typeof contentsObject!='undefined'){
-
-                var keys = Object.keys(contentsObject);
-
-                return keys[0]
-
-            }
-
-            else{
-
-                return ''
-            }
-
-        }
-
-        function updateContentWindow(documentText, addressBarText) {
-
-            // Somewhere below need to check for things like invalid docId
-
-            if(documentText!==addressBarText) {
-
-                /*!
-
-                These steps demonstrate the creation of a temporary document, based on a copy of the global document. This will then be used to determine if there is already a document in the database with the same docId as the address bar, and additionally with a key id with the same name.
-
-                var tempDocument = {}
-                var tempFieldName = addressBarText;
-                var tempContents = {};
-
-                tempDocument = aDocument
-                tempDocument.docId = addressBarText;
-
-                tempContents = tempDocument.contents
-
-                NOTE: For simplicity sake this example sometimes uses the same value for both the docId and the key id, as seen here. Real life implimentations can and will differ, and this will be demonstrated elsewhere in the example code.
-
-                */
-
-                var tempDocument = {}
-                var tempFieldName = addressBarText;
-                var tempContents = {};
-
-                tempDocument = aDocument
-                tempDocument.docId = addressBarText;
-
-                tempContents = tempDocument.contents
-
-                if(typeof tempContents !='undefined' && typeof tempContents[tempFieldName]!='undefined') {
-
-                    aDocument = tempDocument
-                    documentContent.text = tempContents[tempFieldName]
-
-                }
-                else {
-
-                    /*!
-
-                    Here the contents of the temporary document are modified, which then replaces the global document.
-
-                    documentContent.text = 'More Hello World...';
-
-                    tempContents = {}
-                    tempContents[tempFieldName] = documentContent.text
-                    tempDocument.contents = tempContents
-                    aDocument = tempDocument
-
-                    */
-
-                    documentContent.text = 'More Hello World...';
-
-                    tempContents = {}
-                    tempContents[tempFieldName] = documentContent.text
-                    tempDocument.contents = tempContents
-                    aDocument = tempDocument
-
-                }
-
-            }
-            else {
-
-                /*!
-
-                In this instance the current document's content is updated from the text view. The unique key and docId are not modified because the database already contains a record with those properties.
-
-                tempContents = {}
-                tempFieldName = getCurrentDocumentKey(aDocument.contents)
-                tempContents[tempFieldName] = documentContent.text
-                aDocument.contents = tempContents
-
-                */
-
-                tempContents = {}
-                tempFieldName = getCurrentDocumentKey(aDocument.contents)
-                tempContents[tempFieldName] = documentContent.text
-                aDocument.contents = tempContents
-
-            }
-
-        }
-
-        Tabs {
-            id: tabs
-
-            Tab {
-                title: i18n.tr("Hello U1Db!")
-
-                page: Page {
-
-                    id: helloPage
-
-                    /*! Here a rectangle is defined that represents the lower portion of our application. It will contain all the main parts of the application.
-
-                    Rectangle {
-
-                         width: units.gu(45)
-                         height: units.gu(70)
-                         anchors.bottom: parent.bottom
-
-                         color: "#00FFFFFF"
-
-                         // The remainder of the main part of the application goes here ...
-
-                         }
-
-                     */
-
-                    Rectangle {
-
-                         width: units.gu(45)
-                         height: units.gu(70)
-                         anchors.fill: parent
-
-                         color: "#00FFFFFF"
-
-                         Rectangle {
-
-                            width: units.gu(45)
-                            height: units.gu(60)
-                            anchors.bottom: parent.bottom
-
-                            /*!
-
-                            The following TextArea is for displaying contents for the current state of the global document, as defined by the key / name in the address bar.
-
-                            TextArea{
-
-                                id: documentContent
-
-                                selectByMouse : false
-
-                                x: units.gu(1)
-                                y: units.gu(1)
-                                width: units.gu(43)
-                                height: units.gu(58)
-                                color: "#000000"
-
-                            }
-
-                             */
-
-                            TextArea {
-
-                                id: documentContent
-
-                                selectByMouse : false
-
-                                x: units.gu(1)
-                                y: units.gu(1)
-                                width: units.gu(43)
-                                height: units.gu(58)
-                                color: "#000000"
-
-                            }
-
-                         }
-
-                         // This rectangle contains the navigation controls
-
-                         Rectangle {
-
-                              width: units.gu(43)
-                              height: units.gu(5)
-                              anchors.top: addressBarArea.bottom
-                              x: units.gu(1.5)
-                              color: "#00FFFFFF"
-
-                              Row {
-
-                                 width: units.gu(43)
-                                 height: units.gu(5)
-                                 anchors.verticalCenter: parent.verticalCenter
-                                 spacing: units.gu(2)
-
-                                 Button {
-                                 text: "<"
-                                 onClicked: updateContentWindow(switchToPreviousDocument(aDocument), addressBar.text)
-                                 }
-                                 Button {
-                                 text: "Home"
-                                 onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),'helloworld')
-                                                                 }
-                                 Button {
-                                 text: "Save"
-                                 onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
-                                 }
-                                 Button {
-                                 text: ">"
-                                 onClicked: updateContentWindow(switchToNextDocument(aDocument), addressBar.text)
-                                 }
-
-                              }
-
-                          }
-
-                          Rectangle {
-
-                            id: addressBarArea
-
-                            width: units.gu(45)
-                            height: units.gu(5)
-                            anchors.top: parent.top
-
-                            TextField {
-
-                                    id: addressBar
-
-                                    width: units.gu(43)
-                                    anchors.verticalCenter: parent.verticalCenter
-                                    x: units.gu(1)
-
-                                    hasClearButton: false
-
-                                    /*!
-
-                                        There is an object within in the 'aDocument' model defined earlier called 'contents', which contains a key called 'hello', which represents a search string.  In this example the key will represent the name of a document in the database, which will be displayed in the address bar. Displaying the key is demonstrated here:
-
-                                    text: displayKey(aDocument.contents)
-
-                                    function displayKey(documentObject){
-
-                                        var keys = Object.keys(documentObject);
-
-                                        return keys[0]
-
-                                    }
-
-                                    */
-
-                                    text: getCurrentDocumentKey(aDocument.contents)
-
-
-                                    onAccepted: {
-
-                                        onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
-
-                                    }
-                                }
-                        }
-                    }
-                }
-            }
-
-            Tab {
-                title: i18n.tr("Bookmarks")
-                page: Page {
-                    id: bookmarkPage
-
-                    Label {
-                        text: aDocument.contents.helloworld
-                    }
-
-                }
-            }
-        }
+    id: root
+    applicationName: "com.ubuntu.developer.foobar.bookmarks"
+
+    width: units.gu(45)
+    height: units.gu(80)
+
+    /*
+        Bookmarks database
+    */
+
+    U1db.Database {
+        id: db
+        // path: "bookmarks.db"
+    }
+
+    U1db.Document {
+        database: db
+        docId: 'defaultsDuckDuckGo'
+        create: true
+        defaults: { "uri": "https://www.duckduckgo.com";, visited: false, "meta": { "title": "Search DuckDuckGo", visits: 0, tags: [ 'search', 'engine' ] } }
+    }
+
+    U1db.Document {
+        database: db
+        docId: 'defaultsUbuntu'
+        create: true
+        defaults: { "uri": "http://www.ubuntu.com";, visited: true, "meta": { "title": "The world's most popular free OS", visits: 1001 } }
+    }
+
+    U1db.Query {
+        id: allBookmarks
+        index: U1db.Index {
+            database: db
+        }
+    }
+
+    /*
+        UI: details
+    */
+    Component {
+        id: detailsPopCom
+        Popover {
+            id: detailsPop
+            Column {
+                anchors.centerIn: parent
+                spacing: units.gu(1)
+                Label {
+                    text: i18n.tr('JSON')
+                }
+                TextField {
+                    text: bookmarksList.detailsDocId
+                }
+                TextArea {
+                    text: bookmarksList.detailsContents.replace(',',',\n')+'\n\n'
+                    readOnly: true
+                    autoSize: true
+                }
+                Button {
+                    text: i18n.tr('Delete')
+                    onClicked: {
+                        PopupUtils.close(detailsPop)
+                        db.putDoc('', bookmarksList.detailsDocId)
+                    }
+                }
+            }
+        }
+    }
+
+    /*
+        UI: list view, filters
+    */
+
+    Page {
+        id: page
+        title: i18n.tr("Bookmarks")
+
+        Item {
+            id: container
+            anchors.margins: units.gu(1)
+            anchors.fill: parent
+ 
+            ListView {
+                id: bookmarksList
+                anchors.fill: parent
+                model: allBookmarks
+                property string detailsDocId: ""
+                property string detailsContents: ""
+                delegate: ListItem.Subtitled {
+                    text: contents.title || '[title:%1]'.arg(docId)
+                    subText: contents.uri || '[uri:%1]'.arg(docId)
+                    // iconSource: contents.uri + "/favicon.ico"
+                    fallbackIconName: "favorite-unselected,text-html"
+                    iconFrame: false
+                    onClicked: {
+                        bookmarksList.detailsDocId = docId
+                        bookmarksList.detailsContents = JSON.stringify(contents)
+                        PopupUtils.open(detailsPopCom, bookmarksList)
+                    }
+                }
+            }
+
+            OptionSelector {
+                id: filterSelector
+                StateSaver.properties: 'selectedIndex'
+                anchors.bottom: parent.bottom
+                text: i18n.tr('N/A')
+                expanded: true
+                model: ListModel {
+                    ListElement { label: 'Newly Added'; expression: "[ 'meta.visits' ]"; query: "[ { 'visits': 0 } ]" }
+                    ListElement { label: 'Ubuntu'; expression: "[ 'uri' ]"; query: "[ 'http://www.ubuntu*' ]" }
+                    ListElement { label: 'Search'; expression: "[ 'meta.title' ]"; query: "[ 'Search*' ]" }
+                    ListElement { label: 'Engine'; expression: "[ 'meta.tags' ]"; query: "[ 'engine' ]" }
+                    ListElement { label: 'All'; expression: "[ 'meta.visits', 'meta.title' ]"; query: "[ '*', '*' ]" }
+                }
+                delegate: OptionSelectorDelegate {
+                    text: i18n.tr(label)
+                }
+                selectedIndex: model.count - 1
+                onSelectedIndexChanged: {
+                    var d = model.get(selectedIndex)
+                    text = '%1 - %2'.arg(d.expression).arg(d.query)
+                    allBookmarks.index.expression = eval(d.expression)
+                    allBookmarks.query = eval(d.query)
+                }
+            }
+        }
+    }
 }

=== removed file 'examples/u1db-qt-example-4/u1db-qt-example-4.qdoc'
--- examples/u1db-qt-example-4/u1db-qt-example-4.qdoc	2013-04-23 11:55:20 +0000
+++ examples/u1db-qt-example-4/u1db-qt-example-4.qdoc	1970-01-01 00:00:00 +0000
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2013 Canonical, Ltd.
- *
- * Authors:
- *  Kevin Wright <kevin.wright@xxxxxxxxxxxxx>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*!
-
-\page  u1db-qt-tutorial-4.html
-
-*/

=== modified file 'src/index.cpp'
--- src/index.cpp	2013-08-08 10:31:16 +0000
+++ src/index.cpp	2014-01-31 19:31:39 +0000
@@ -216,7 +216,7 @@
 
         i.next();
 
-        if(fieldsList.count()>0){
+        if(!original_field.isEmpty()){
             current_field = original_field + "." + i.key();
         }
         else{
@@ -235,7 +235,7 @@
         {
             fieldsList = getFieldsFromList(docId, fieldsList, value.toList(),current_field);
         }
-        else
+
         {
             if(m_expression.contains(current_field)==true){
                 results_map.insert(i.key(),value);
@@ -278,6 +278,10 @@
         {
             fieldsList = getFieldsFromList(docId, fieldsList, value.toList(),current_field);
         }
+        else if(value.userType()==10) // QString
+        {
+            fieldsList.append(current_field);
+        }
         else
         {
 

=== modified file 'src/query.cpp'
--- src/query.cpp	2013-08-27 10:49:26 +0000
+++ src/query.cpp	2014-01-31 19:31:39 +0000
@@ -181,7 +181,6 @@
 
     bool match = false;
 
-    QString value_string = value.toString();
     QVariant query = getQuery();
     // * is the default if query is empty
     if (!query.isValid())
@@ -191,16 +190,16 @@
     if(typeName == "QString")
     {
         QString query_string = query.toString();
-        match = queryString(query_string, value_string);
+        match = queryString(query_string, value);
     }
     else if(typeName == "int")
     {
         QString query_string = query.toString();
-        match = queryString(query_string, value_string);
+        match = queryString(query_string, value);
     }
     else if(typeName == "QVariantList")
     {
-        match = iterateQueryList(query, field, value_string);
+        match = iterateQueryList(query, field, value);
     }
     else
     {
@@ -216,7 +215,7 @@
     \internal
     Loop through the query assuming it's a list.
  */
-bool Query::iterateQueryList(QVariant query, QString field, QString value)
+bool Query::iterateQueryList(QVariant query, QString field, QVariant value)
 {
 
     bool match = false;
@@ -232,7 +231,7 @@
 
         if(typeName == "QVariantMap")
         {
-            match = queryMap(j_value.toMap(), value, field);
+            match = queryMap(j_value.toMap(), value.toString(), field);
 
             if(match == true){
                 break;
@@ -263,9 +262,18 @@
     \internal
     Handle different types of string values including wildcards.
  */
-bool Query::queryString(QString query, QString value)
+bool Query::queryString(QString query, QVariant value)
 {
 
+    QString typeName = value.typeName();
+    if (typeName == "QVariantList") {
+        Q_FOREACH (QVariant value_string, value.toList()) {
+            if (queryString(query, value_string.toString()))
+                return true;
+        }
+        return false;
+    }
+
     bool match = false;
 
         if(query == "*"){
@@ -277,7 +285,7 @@
         else if(query.contains("*")){
             QStringList k_string_list = query.split("*");
             QString k_string = k_string_list[0];
-            match = value.startsWith(k_string,Qt::CaseSensitive);
+            match = value.toString().startsWith(k_string,Qt::CaseSensitive);
 
             return match;
 

=== modified file 'src/query.h'
--- src/query.h	2013-06-19 11:35:10 +0000
+++ src/query.h	2014-01-31 19:31:39 +0000
@@ -69,8 +69,8 @@
     void onDataInvalidated();
 
     void generateQueryResults();
-    bool iterateQueryList(QVariant query, QString field, QString value);
-    bool queryString(QString query, QString value);
+    bool iterateQueryList(QVariant query, QString field, QVariant value);
+    bool queryString(QString query, QVariant value);
     bool queryMap(QVariantMap map, QString value, QString field);
     bool queryField(QString field, QVariant value);
 };


Follow ups