Generates SQL where clause. Returns (sql, values).
    Criteria is a dictionary of {field: value}.

    >>> sqlwhere()
    ('', [])
    >>> sqlwhere({'id': 5})
    ('id=%s', [5])
    >>> sqlwhere({'id': 3, 'name': 'toto'})
    ('id=%s and name=%s', [3, 'toto'])
    >>> sqlwhere({'id': 3, 'name': 'toto', 'createdon': '2013-12-02'})
    ('createdon=%s and id=%s and name=%s', ['2013-12-02', 3, 'toto'])

def sqlwhere(criteria=None):
    """Generates SQL where clause. Returns (sql, values).
    Criteria is a dictionary of {field: value}.

    >>> sqlwhere()
    ('', [])
    >>> sqlwhere({'id': 5})
    ('id=%s', [5])
    >>> sqlwhere({'id': 3, 'name': 'toto'})
    ('id=%s and name=%s', [3, 'toto'])
    >>> sqlwhere({'id': 3, 'name': 'toto', 'createdon': '2013-12-02'})
    ('createdon=%s and id=%s and name=%s', ['2013-12-02', 3, 'toto'])
    """
    if not criteria:
        return ('', [])
    fields = sorted(criteria.keys())
    validate_names(fields)
    values = [criteria[field] for field in fields]
    parts = [field + '=%s' for field in fields]
    sql = ' and '.join(parts)
    return (sql, values)