2016-02-24 36 views
0

我试图在CKAN(ckan.org)中使用datastore_api并想要删除ID高于X的所有记录 - 我的目标是简单地删除所有记录删除表格。删除CKAN中ID高于X的所有记录

文档: http://docs.ckan.org/en/ckan-2.5.1/maintaining/datastore.html?highlight=filter#ckanext.datastore.logic.action.datastore_delete

运行API与

'filters': {'_id': '0'} 

的工作,但设置

{'_id': '>0'} 

{'_id': '*'} fails. 

如何在不删除表的情况下清空数据存储中的表?

在CKAN生成的SQL是:

...9b89-7ab1c36c8e00" WHERE ("_id" = '*') ... 

所以很有意义失败的原因,但我不知道如何使用通配符两种或API调用“高于”。

这似乎是代码:

def delete_data(context, data_dict): 
    validate(context, data_dict) 
    fields_types = _get_fields_types(context, data_dict) 

    query_dict = { 
     'where': [] 
    } 

    for plugin in p.PluginImplementations(interfaces.IDatastore): 
     query_dict = plugin.datastore_delete(context, data_dict, 
              fields_types, query_dict) 

    where_clause, where_values = _where(query_dict['where']) 
    sql_string = u'DELETE FROM "{0}" {1}'.format(
     data_dict['resource_id'], 
     where_clause 
    ) 

    _execute_single_statement(context, sql_string, where_values) 

... 

def _where(where_clauses_and_values): 
    '''Return a SQL WHERE clause from list with clauses and values 

    :param where_clauses_and_values: list of tuples with format 
     (where_clause, param1, ...) 
    :type where_clauses_and_values: list of tuples 

    :returns: SQL WHERE string with placeholders for the parameters, and list 
     of parameters 
    :rtype: string 
    ''' 
    where_clauses = [] 
    values = [] 

    for clause_and_values in where_clauses_and_values: 
     where_clauses.append('(' + clause_and_values[0] + ')') 
     values += clause_and_values[1:] 

    where_clause = u' AND '.join(where_clauses) 
    if where_clause: 
     where_clause = u'WHERE ' + where_clause 

    return where_clause, values 
+0

您是否阅读过相关的代码? –

+1

添加评论有问题... –

回答

1

注:有,因为这个问题找到了一个错误。似乎没有办法从datastore中删除数据。解决此问题的请求已发送至https://github.com/ckan/ckan/pull/2885

你问题的标题问如何删除ID高于X的所有记录,但在你的问题中你说你的目标是“简单地删除所有记录而不删除表”。这是我在这里回答的。

根据在http://docs.ckan.org/en/ckan-2.5.1/maintaining/datastore.html?#ckanext.datastore.logic.action.datastore_delete的文档:

ckanext.datastore.logic.action.datastore_delete(上下文,data_dict) 删除一个表或一组从数据存储区的记录。

参数:

  • RESOURCE_ID(串) - 资源ID,该数据将从被删除。 (可选)
  • force(bool(可选,默认:False)) - 设置为True编辑只读资源
  • 过滤器(字典) - 删除前应用的过滤器(例如{“name”:“fred “})。 如果缺少删除整个表格和所有依赖视图。 (可选)

重点是我的。

如果您未通过filters属性,CKAN将删除表本身。如果您通过过滤器{},它会删除所有表格的内容。

+0

解析'filters':{}实际上删除了表格,而不仅仅是行。 –

+0

这条线: https://github.com/ckan/ckan/blob/f74575024a472085db2c8238d8f0ff71d888ffaf/ckanext/datastore/db.py#L1169 是去除data_dict过滤器参数。这意味着,当db.delete终于这里称为: https://github.com/ckan/ckan/blob/f74575024a472085db2c8238d8f0ff71d888ffaf/ckanext/datastore/db.py#L1169 ...数据库将永远是删除。 这是一个错误,我将发送一个PR来纠正这个问题。 – TkTech

+0

@TkTech通过https://github.com/ckan/ckan/pull/2885发送拉取请求。 –