Commit f5c793e8 authored by Shelepnev Dmitry's avatar Shelepnev Dmitry
Browse files

Create user defined CONCAT function for sqlite

parent c101fd0f
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -528,9 +528,9 @@ class SearchAuthorsFeed(AuthFeed):
            page = int(page)

        if searchtype == 'm':
            authors = Author.objects.extra(where=["upper(last_name) like %s"], params=["%%%s%%"%searchterms.upper().strip()])
            authors = Author.objects.extra(where=["upper(concat(last_name,' ',first_name)) like %s"], params=["%%%s%%"%searchterms.upper()])
        elif searchtype == 'b':
            authors = Author.objects.extra(where=["upper(last_name) like %s"], params=["%s%%"%searchterms.upper().strip()])            
            authors = Author.objects.extra(where=["upper(concat(last_name,' ',first_name)) like %s"], params=["%s%%"%searchterms.upper()])            
        return {"authors":authors, "searchterms":searchterms, "searchtype":searchtype, "page":page}

    def link(self, obj):
@@ -788,16 +788,16 @@ class AuthorsFeed(AuthFeed):
    def items(self, obj):
        length, chars = obj
        if self.lang_code:
            sql="""select upper(substring(last_name || ' ' || first_name,1,%(length)s)) as id, count(*) as cnt 
            sql="""select upper(substring(concat(last_name,' ',first_name),1,%(length)s)) as id, count(*) as cnt 
                   from opds_catalog_author 
                   where lang_code=%(lang_code)s and upper(last_name || ' ' || first_name) like '%(chars)s%%'
                   group by upper(substring(last_name || ' ' || first_name,1,%(length)s)) 
                   where lang_code=%(lang_code)s and upper(concat(last_name,' ',first_name)) like '%(chars)s%%'
                   group by upper(substring(concat(last_name,' ',first_name),1,%(length)s)) 
                   order by id"""%{'length':length, 'lang_code':self.lang_code, 'chars':chars}
        else:
            sql="""select upper(substring(last_name || ' ' || first_name,1,%(length)s)) as id, count(*) as cnt 
            sql="""select upper(substring(concat(last_name,' ',first_name),1,%(length)s)) as id, count(*) as cnt 
                   from opds_catalog_author 
                   where upper(last_name || ' ' || first_name) like '%(chars)s%%'
                   group by upper(substring(last_name || ' ' || first_name,1,%(length)s)) 
                   where upper(concat(last_name,' ',first_name)) like '%(chars)s%%'
                   group by upper(substring(concat(last_name,' ',first_name),1,%(length)s)) 
                   order by id"""%{'length':length,'chars':chars}
          
        dataset = Author.objects.raw(sql)
+4 −0
Original line number Diff line number Diff line
@@ -52,8 +52,12 @@ def sopds_substring(s,i,l):
    i = i - 1
    return s[i:i+l]

def sopds_concat(s1='',s2='',s3=''):
    return "%s%s%s"%(s1,s2,s3)

@receiver(connection_created)
def extend_sqlite(connection=None, **kwargs):
    if connection.vendor == "sqlite":
        connection.connection.create_function('upper',1,sopds_upper)
        connection.connection.create_function('substring',3,sopds_substring)
        connection.connection.create_function('concat',3,sopds_concat)