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

How to SELECT *? #27

Open
kutchar opened this issue Feb 28, 2015 · 8 comments
Open

How to SELECT *? #27

kutchar opened this issue Feb 28, 2015 · 8 comments

Comments

@kutchar
Copy link
Contributor

kutchar commented Feb 28, 2015

I couldn't figure out how I can run a SELECT *. I would assume that select() (without providing column names) would mean SELECT * by default.

@brendanator
Copy link
Contributor

select statements are parameterised by the types of the columns that are being selected.

This means you can write select(MyTable.col1, MyTable.col2).from(MyTable).fetchAll and result type will be List[(Int, String)] - assuming col1 is an Int and col2 is a String

Writing select().from(MyTable) creates a select with no columns but this can be used with an extractor as hamish as shown - the extractor knows which columns it uses so injects them into the select

If you want to fetch all of the columns for a table then a possible workaround would be to define * in the table definition to contain all the columns

class MyTable(alias: Option[String]) extends Table("mytable", alias) {
  val col1 = column[Int]("col1")
  val col2 = column[String]("col2")

  val * = (col1, col2)
}
object MyTable extends MyTable(None)

You would then be able to write select(MyTable.*).from(MyTable)

I've create a branch select-star with a first stab of changing the select statement to allow this. You can see it in action here: https://github.com/jhc-systems/sqlest/blob/select-star/sqlest/src/test/scala/sqlest/executor/ExecutorSpec.scala#L72. Note this wouldn't work if you wanted to select * from multiple tables

What do you think?

@kutchar
Copy link
Contributor Author

kutchar commented Mar 1, 2015

I see. The only issue with the * approach above is that I won't be able to select all the columns of a table if the table has more than 22 columns. I think a shapeless HList might be a better alternative.

@kutchar
Copy link
Contributor Author

kutchar commented Mar 1, 2015

nm the 22 limit, I just figured out that I can use the following style too
val * = List(col1, col2, col3, ...)

BTW, why is the apply method parameter type scala.collection.immutable.List which is an abstract class, wouldn't it better to use scala.collection.Seq which is a trait?

@brendanator
Copy link
Contributor

You can't actually run a select statement that uses the val * = List(col1, col2, col3, ...)

In fact if you try you will get a compilation error. This is because you can only use the fetch... methods with columns that have a Extractable typeclass implementation available for them:

    def fetchHead[SingleResult](implicit extractable: Extractable.Aux[ResultSet, A, SingleResult]): SingleResult = ...

There is no Extractable typeclass instance for List[AliasedColumn[_]]. The only reason that the apply method you have seen is there is because it is used internally to sqlest. It will be removed when I work out how

Your other suggestion to use an HList is definitely the way to go. However I think it would be dangerous to make the API to sqlest depend on an external library - people using different versions would not be able to use the project . However by implementing the Extractable typeclass for HList you will be able to do this

I'll make some changes to enable doing this and create an example

@kutchar
Copy link
Contributor Author

kutchar commented Mar 2, 2015

That figures, I was getting the compilation error when trying to use the tuple style apply method.

As far as the HList and 22 limit, I gave it some thought and it might not be that useful of a feature. After all Scala case classes can't have more than 22 fields, so you won't be able to use it anyways.

@hrj
Copy link

hrj commented Apr 10, 2015

@kutchar The restriction on 22 fields in case classes was removed in scala 2.11.

@kutchar
Copy link
Contributor Author

kutchar commented Apr 10, 2015

@hrj good to know. Thanks.

@brendanator
Copy link
Contributor

Unfortunately sqlest doesn't currently support case classes with more that 22 columns

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

3 participants