Skip to content

SQLColumn table_name get lost when doing a join operation #69

Description

@jonathanBieler

I was trying to do a join query and came across this bug, I'm doing someting like this:

on = SQLOn( SQLColumn("comments.user_id"), SQLColumn("users.id"))
j = Vector{SQLJoin}([
    SQLJoin(Users.User, columns = [SQLColumn("users.name")], [on])
])
q = SQLQuery(where = [SQLWhereExpression("$(SQLColumn(:topic_id)) = ?", topic_id)], order = SQLOrder(:datetime, "DESC"))
DataFrame(Comments.Comment, q, j)

And I get an error telling me "comments.name" doesn't exists, while I asked for "users.name" in my query. The table_name is lost and replaced during the preparation of the query (in prepare_column_name).

The issue is that table_name that was already parsed in SQLColumn constructor is ignored here :

function from_literal_column_name(c::String)::Dict{Symbol,String}

This fixes the issue, but seems a bit hacky. Not sure why we need to parse the column again, feels like that should be the job of the constructor.

@eval SearchLight begin

    function prepare_column_name(column::SearchLight.SQLColumn, m::Type{T})::String where {T<:SearchLight.AbstractModel}
        if column.raw
          column.value |> string
        else
          column_data::Dict{Symbol,Any} = SearchLight.from_literal_column_name(column)
          if ! haskey(column_data, :table_name)
            column_data[:table_name] = SearchLight.table(m)
          end
          if ! haskey(column_data, :alias)
            column_data[:alias] = ""
          end
      
          column_data_to_column_name(column, column_data)
        end
      end

      function from_literal_column_name(column::SearchLight.SQLColumn)
        c = column.value
        result = Dict{Symbol,String}()
        result[:original_string] = c
        result[:table_name] = column.table_name
      
        # has alias?
        if occursin(" AS ", c)
          parts = split(c, " AS ")
          result[:column_name] = parts[1]
          result[:alias] = parts[2]
        else
          result[:column_name] = c
        end
      
        # is fully qualified?
        if occursin(".", result[:column_name])
          parts = split(result[:column_name], ".")
          result[:table_name] = parts[1]
          result[:column_name] = parts[2]
        end
        
        result
      end
end

SearchLightSQLite v2.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions