Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLColumn table_name get lost when doing a join operation #69

Open
jonathanBieler opened this issue May 5, 2023 · 0 comments
Open

SQLColumn table_name get lost when doing a join operation #69

jonathanBieler opened this issue May 5, 2023 · 0 comments

Comments

@jonathanBieler
Copy link

jonathanBieler commented May 5, 2023

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

@jonathanBieler jonathanBieler changed the title SQLColumn table_name get lost when doing a Join operation SQLColumn table_name get lost when doing a join operation May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant