<filename>api/services/CsmServices.py
from app import db

def getByKeyword(filterKeyword, currentPage):
    count = db.engine.execute("""
        select count(*) from csm
            where (length(%s) = 0 or raw_text like '%%' || %s ||'%%')
    """, filterKeyword, filterKeyword).fetchone()

    total = 0
    results = []
    if count is not None and len(count) > 0:
        total = count[0]
        temps = db.engine.execute("""
            select * from Csm
            where (length(%s) = 0 or raw_text like '%%' || %s ||'%%')
            LIMIT %s OFFSET %s
        """, filterKeyword, filterKeyword, 10, (currentPage-1)*10)
    
        for item in temps:
            results.append(dict(item))

    return results, total

def getCsmSentiment():
    temps = db.engine.execute("""
            select sentiment, count(*) as value 
            from csm 
            group by sentiment
            order by sentiment desc
        """)
    results = []
    for item in temps:
        results.append(dict(item))
    return results

<filename>api/services/CsmServices.py
from app import db

def getByKeyword(filterKeyword, currentPage):
    count = db.engine.execute("""
        select count(*) from csm
            where (length(%s) = 0 or raw_text like '%%' || %s ||'%%')
    """, filterKeyword, filterKeyword).fetchone()

    total = 0
    results = []
    if count is not None and len(count) > 0:
        total = count[0]
        temps = db.engine.execute("""
            select * from Csm
            where (length(%s) = 0 or raw_text like '%%' || %s ||'%%')
            LIMIT %s OFFSET %s
        """, filterKeyword, filterKeyword, 10, (currentPage-1)*10)
    
        for item in temps:
            results.append(dict(item))

    return results, total

def getCsmSentiment():
    temps = db.engine.execute("""
            select sentiment, count(*) as value 
            from csm 
            group by sentiment
            order by sentiment desc
        """)
    results = []
    for item in temps:
        results.append(dict(item))
    return results

select count(*) from csm             where (length(%s) = 0 or raw_text like '%%' || %s ||'%%') select * from Csm             where (length(%s) = 0 or raw_text like '%%' || %s ||'%%')             LIMIT %s OFFSET %s select sentiment, count(*) as value              from csm              group by sentiment             order by sentiment desc