Merge lp:~kevin-wright-1/u1db-qt/kevin-wright-1-addCreateIndexList into lp:u1db-qt

Proposed by Kevin Wright
Status: Merged
Approved by: Cris Dywan
Approved revision: 15
Merge reported by: Cris Dywan
Merged at revision: not available
Proposed branch: lp:~kevin-wright-1/u1db-qt/kevin-wright-1-addCreateIndexList
Merge into: lp:u1db-qt
Diff against target: 140 lines (+119/-0)
2 files modified
database.cpp (+117/-0)
database.h (+2/-0)
To merge this branch: bzr merge lp:~kevin-wright-1/u1db-qt/kevin-wright-1-addCreateIndexList
Reviewer Review Type Date Requested Status
Cris Dywan Approve
Review via email: mp+150559@code.launchpad.net

Description of the change

Added the function int Database::createIndexList(QString index_name, QStringList expressions), which corresponds to the u1db.c function: int u1db_create_index_list(u1database *db, const char *index_name, int n_expressions, const char **expressions). It is currently marked INCOMPLETE.

To post a comment you must log in.
14. By Kevin Wright

Added the function int Database::createIndex(u1database *db, const char *index_name, QStringList expressions). It is currently marked INCOMPLETE.

15. By Kevin Wright

Modified comment: int Database::createIndex(QString index_name, QStringList expressions), which corresponds to the u1db.c function: int u1db_create_index(u1database *db, const char *index_name, int n_expressions, ...). It is being marked as INCOMPLETE

Revision history for this message
Cris Dywan (kalikiana) wrote :

We might want to replace the return values with setError() later. Also for consistency it'd be nice to use placeholders (:foo instead of ?) everywhere. For now, this looks great!

review: Approve
Revision history for this message
Cris Dywan (kalikiana) wrote :

Note that I'll rename createIndex(List) to putIndex(List) because it conflicts with QAbstractListModel methods.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'database.cpp'
2--- database.cpp 2013-02-25 12:49:54 +0000
3+++ database.cpp 2013-02-26 13:57:20 +0000
4@@ -259,6 +259,123 @@
5 return m_path;
6 }
7
8+/* int Database::createIndexList(QString index_name, QStringList expressions)
9+
10+corresponds to the u1db.c function:
11+
12+int u1db_create_index_list(u1database *db, const char *index_name, int n_expressions, const char **expressions)
13+
14+It is currently marked INCOMPLETE */
15+
16+
17+/* Tests to create for createIndexList: does it return a value, what is the value it returns, is the value returned an acceptable value, what is the expected result for an index_name that we know is unique as well as one we know is not unique, is the database NULL or not, is the expressions list empty or not, is the index_name empty or not
18+*/
19+
20+int Database::createIndexList(QString index_name, QStringList expressions)
21+{
22+ int U1DB_OK, U1DB_INVALID_PARAMETER, U1DB_DUPLICATE_INDEX_NAME, SQLITE_OK = 0; // These are here temporarily
23+ int status = U1DB_OK;
24+ int i = 0;
25+
26+ if (m_db.isOpen() == false || index_name.isEmpty() == false || expressions.isEmpty() == false) {
27+ return U1DB_INVALID_PARAMETER; // is this really the correct return value for all three of the conditions above?
28+ }
29+
30+ for (i = 0; i < expressions.count(); ++i) {
31+ if (expressions[i].isEmpty() == true || expressions[i].isNull() == true) {
32+ return U1DB_INVALID_PARAMETER;
33+ }
34+ }
35+
36+ //status = u1db__find_unique_expressions(db, n_expressions, expressions, &n_unique, &unique_expressions); // needs to be modified
37+
38+ if (status != U1DB_OK) {
39+ return status;
40+ }
41+
42+ //QString statement = "SELECT field FROM index_definitions WHERE name = ".index_name." ORDER BY offset DESC";
43+ QSqlQuery query;
44+ query.prepare("SELECT field FROM index_definitions WHERE name = ? ORDER BY offset DESC");
45+ query.addBindValue(index_name);
46+
47+ QStringList results;
48+
49+ //QSqlQuery query(statement);
50+
51+ // Add a check to ensure our query was OK -- what return value do we need here if there was an error rather than simply an empty set of results?
52+
53+ while (query.next()) {
54+ results.append(query.value(0).toString());
55+ }
56+
57+ for (i = 0; i < expressions.count(); i++) {
58+ if (results.contains(expressions.at(i))==true) {
59+ return U1DB_DUPLICATE_INDEX_NAME;
60+ }
61+ }
62+
63+ if (results.count() > 0) {
64+ status = SQLITE_OK;
65+ return status;
66+ }
67+ else{
68+
69+
70+ // Should probably use a prepared statement here ... or some alternative
71+
72+ for (i = 0; i < expressions.count(); ++i) {
73+
74+ query.prepare("INSERT INTO index_definitions VALUES (?, ?, ?)");
75+ query.addBindValue(index_name);
76+ query.addBindValue(i);
77+ query.addBindValue(expressions.at(i));
78+
79+ if(query.exec()==false){
80+ // Actually in the orginal C function the check was to see if the return value was SQLITE_CONSTRAINT and not only whether the exec generally failed
81+ status = U1DB_DUPLICATE_INDEX_NAME;
82+ // Is it safe to assume that all insert statements in this loop need to be rolled back?
83+ return status;
84+ }
85+
86+ }
87+
88+ //status = u1db__index_all_docs(db, n_unique, unique_expressions); // Needs to be modified
89+
90+ return status;
91+
92+ }
93+
94+
95+
96+}
97+
98+
99+/* int Database::createIndex(QString index_name, QStringList expressions)
100+
101+corresponds to the u1db.c function:
102+
103+int u1db_create_index(u1database *db, const char *index_name, int n_expressions, ...)
104+
105+It is being marked as INCOMPLETE */
106+
107+int Database::createIndex(QString index_name, QStringList expressions)
108+{
109+ int U1DB_OK, U1DB_NOMEM;
110+ int i;
111+
112+ int status = U1DB_OK;
113+
114+ if(expressions.isEmpty() == true) {
115+ status = U1DB_NOMEM;
116+ }
117+ else
118+ {
119+ status = createIndexList(index_name, expressions);
120+ return status;
121+ }
122+
123+}
124+
125 QT_END_NAMESPACE_U1DB
126
127 #include "moc_database.cpp"
128
129=== modified file 'database.h'
130--- database.h 2013-02-25 12:49:54 +0000
131+++ database.h 2013-02-26 13:57:20 +0000
132@@ -47,6 +47,8 @@
133 Q_INVOKABLE int putDoc(QVariant newDoc, const QString& docID=QString());
134 Q_INVOKABLE QList<QVariant> listDocs();
135 Q_INVOKABLE QString lastError();
136+ Q_INVOKABLE int createIndexList(QString index_name, QStringList expressions);
137+ Q_INVOKABLE int createIndex(QString index_name, QStringList expressions);
138 Q_SIGNALS:
139 void pathChanged(const QString& path);
140 void errorChanged(const QString& error);

Subscribers

People subscribed via source and target branches

to all changes: