Skip to content

Commit

Permalink
lua fix dt.collection not working
Browse files Browse the repository at this point in the history
  • Loading branch information
boucman committed May 8, 2016
1 parent 85827aa commit b049621
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
22 changes: 22 additions & 0 deletions src/common/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,28 @@ GList *dt_collection_get_all(const dt_collection_t *collection, int limit)
return list;
}

int dt_collection_get_nth(const dt_collection_t *collection, int nth)
{
if(nth < 0 || nth >= dt_collection_get_count(collection))
return -1;
const gchar *query = dt_collection_get_query(collection);
sqlite3_stmt *stmt = NULL;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, nth);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, 1);

int result = -1;
if(sqlite3_step(stmt) == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}

sqlite3_finalize(stmt);

return result;

}

GList *dt_collection_get_selected(const dt_collection_t *collection, int limit)
{
GList *list = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/common/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ gchar *dt_collection_get_sort_query(const dt_collection_t *collection);

/** get the count of query */
uint32_t dt_collection_get_count(const dt_collection_t *collection);

/** get the nth image in the query */
int dt_collection_get_nth(const dt_collection_t *collection, int nth);
/** get all image ids order as current selection. no more than limit many images are returned, <0 ==
* unlimited */
GList *dt_collection_get_all(const dt_collection_t *collection, int limit);
Expand Down
38 changes: 3 additions & 35 deletions src/lua/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,44 +197,12 @@ static int collection_numindex(lua_State *L)
{
return luaL_error(L, "incorrect index in database");
}

gchar *query = NULL;
gchar *sq = NULL;

/* get collection order */
if((darktable.collection->params.query_flags & COLLECTION_QUERY_USE_SORT))
sq = dt_collection_get_sort_query(darktable.collection);


sqlite3_stmt *stmt = NULL;

/* build the query string */
query = dt_util_dstrcat(query, "select distinct id from images ");

if(darktable.collection->params.sort == DT_COLLECTION_SORT_COLOR
&& (darktable.collection->params.query_flags & COLLECTION_QUERY_USE_SORT))
query = dt_util_dstrcat(query, "as a left outer join color_labels as b on a.id = b.imgid ");

query = dt_util_dstrcat(query, "%s limit -1 offset ?1", sq);

DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, index -1);

if(sqlite3_step(stmt) == SQLITE_ROW)
{
int imgid = sqlite3_column_int(stmt, 0);
luaA_push(L, dt_lua_image_t, &imgid);
sqlite3_finalize(stmt);
}
else
int imgid = dt_collection_get_nth(darktable.collection,index-1);
if (imgid <1)
{
g_free(sq);
g_free(query);
sqlite3_finalize(stmt);
return luaL_error(L, "incorrect index in database");
}
g_free(sq);
g_free(query);
luaA_push(L, dt_lua_image_t, &imgid);
return 1;

}
Expand Down

0 comments on commit b049621

Please sign in to comment.