LINQ Benefits over Functional Function Chains

The possibility of adding code trees to support things like C # LINQ was discussed at Kotlin Slack.

There are many applications in C # LINQ, but I want to focus on only one (because others are already supposedly covered by Kotlin syntax): compiling SQL queries to remote databases.

Prerequisites:

  • We have a SQL database data schema expressed somehow in the code, so that static tools (or type systems) could verify the correctness (at least the naming) of the SQL query

  • We need to generate queries as strings

  • We want the syntax close to SQL or Java threads

Question : what expression trees add to the syntax that is so important for this task? How good is the SQL constructor without them?

+4
source share
2 answers

How good can a dsl request be without expression trees?

As JINQ showed that you can get very far by analyzing the bytecode to understand the intent of the developer and thus translate the predicate into SQL.Thus, in principle, expression trees are not necessary to create a great dsl query:

val alices = database.customerStream().where { it.name == "Alice" }

Even without hacking, such as bytecode analysis, you can get a decent dsl request with code generation. Querydsl and JOOQ are great examples. With a bit of Kotlin winding code, you can write

val alices = db.findAll(QCustomer.customer, { it.name.eq("Alice") }) 

How expression trees help build a dsl query

, , . ​​, , , , -.

val alices = database.customerStream().where { it.name == "Alice" }

where expression, SQL . , Kotlin SQL . , linq/jinq, , , POCO/POJO . . , , .

:

+3

JOOQ Querydsl:

ORM DSL DSL . , JOOQ Querydsl, :

  • , , , ( , ), , .
  • : eq db. , equals eq.
  • : , jooqDB.where { it.name.equals("alice") } , .

Jinq

Jinq . , , , . , , Java-, , .

Jinq, , , Jinq , , : AST , , , transcompiler. Jinq ​​ , , : -. java-- JVM, - .

, , , DAL, , , . net, - Linq, Jinq. " " .

:

LINQ-to-SQL LINQ-to-mongo .net. , , : AST SQL ( -), , . ORM , (Microsoft Mongo) .

Kotlin Linq, Kotlin , , MongoDB Hibernate LINQ-to-X Kotlin, , Microsoft IBM, .

Linq Kotlin

, , Kotlin "" inline Linq. Linq-from-Kotlin , LINQ-from-#.

#

someInterface
  .where(customer -> customer.Name.StartsWith("A") && ComplexFunctionCallDoneByMongoDriver(customer))
  .select(customer -> new ResultObject(customer.Name, customer.Age, OtherContext()))

Kotlin :

someInterface
  .filter { it.name startsWith "A" && inlinedComplexFunctionCallDoneOnDB(it) } 
  //inlined methods would have their AST exposed -> can be run on the DB process instead of on the driver.
  .map { ResultObject(name, age, otherContext()) } 
  //uses a reciever type, no need to specify "customer ->"
  //otherContext() might also be inlined

, , , , .

:

, , runtime-code-AST, :

[ ASTM] [] Kotlin

, , , Kotlin Mockito: , , Mocking, Kotlin, java-, Kotlin, .

java-, Kotlin:

  • , . AST, , --, , . .
  • , , . , notifyOfUIElementChange { this.model.displayName } notifyOfUIElementChange("model.displayName"). , - .
    • , ControlsFX , .
  • Kotlin Linq: , Kotlins , . , .

Linq, , , Linq-from-Kotlin , , .

+2

All Articles