Lecture 8: Bridging Query and Programming Languages

Advances in Programming Languages - Lecture 8 - Bridging Query and Programming LanguagesToday’s lecture looked further into integrating SQL within general-purpose programming languages, and ways to succinctly capture the structure of SQL queries. Microsoft’s LINQ (Language Integrated Query) is a leading example of this. The lecture illustrated the basics of LINQ, together with background on the .NET framework and the language features added to C# to support LINQ.

There were also some more examples of the pitfalls of working with strings: the story of Bobby Tables, and speculation on the numbering of Windows 10.

Links: Slides for Lecture 8; Bobby Tables; Microsoft on what happened to Windows 9; Was it string-matching?; Code that might cause problems.

The last lecture showed how frameworks like JDBC for accessing databases build SQL queries through string manipulation. This fails to take advantage of the rich language structure of SQL, and thereby misses opportunities for static checking and compile-time optimization. It is also the root cause of security vulnerabilities by SQL injection, as sanitizing user input becomes essential but also difficult.

SQL queries are programs in a structured high-level language, but we treat them as unstructured text.

Microsoft’s LINQ (Language Integrated Query) is part of the .NET framework that aims to bridge this gap between languages. The lecture included some background on .NET, and in particular its approach to supporting development in multiple programming languages. Treating LINQ in particular, there was a direct comparison of query construction in JDBC/ADO.NET (with strings) and in LINQ (with structured data). LINQ exposes queries to static checking, IDE support and compile-time optimization by making them part of the host language.

This language/query integration goes deep: queries become meaningful data structures in the host language, not just raw strings of syntax.

Creating LINQ requires various extensions to the C# language. We briefly looked through them: lambda expressions; free-standing method declarations; structural datatypes; anonymous record types; and type inference. These are new to C#, but based on well-established concepts from other existing languages. Moreover, all are useful in other contexts, and genuinely enrich the programming language. In particular, LINQ uses first-class expressions, which lead into structured reflection and metaprogramming, the topics of the next lecture.

Homework

Read this piece from the Microsoft Developer Network (MSDN) Library:

Friday’s lecture will be about metaprogramming in F#: