
# A more simple set for make queries 

def insert(model, db, dict_values):
    
    final_values={}
    
    for k in model.fields.keys():
        final_values[k]=model.fields[k].check(dict_values.get(k, ''))
    
    del final_values[model.name_field_id]
    
    str_fields="`"+"`, `".join(final_values.keys())+"`"
    
    str_query='insert into {} ({}) VALUES ({})'.format(model.name, str_fields, ", ".join(['%s']*len(final_values)))
    
    success=False
    
    with db.query(str_query, list(final_values.values())) as cursor:
        
        if cursor.rowcount>0:
            
            model.last_id=cursor.lastrowid
            success=True
    
    return success
    

def select(model, db, dict_fields=[], where_sql='', limit='', dict_values=[]):
    
    if len(dict_fields)==0:
        dict_fields=['`'+field+'`' for field in model.fields.keys()]
    
    str_fields=", ".join(dict_fields)
    
    str_query='select {} from {} {} limit 1'.format(str_fields, model.name, where_sql)
    
    arr_result=[]
    
    with db.query(str_query, dict_values) as cursor:
        
        arr_result=cursor.fetchall()
        
    return arr_result
