On 13.11.2013. 9:26, Jacob Carlborg wrote: > On 2013-11-12 17:14, John Colvin wrote: > >> oh, I see. Would AST macros really be enough to make this work in D? >> "Arbitrary code" is a huge feature space in D, including much that >> doesn't map well to anything outside of a relatively low-level language, >> let alone SQL. >> I can see it quickly becoming a nightmare that would be worse than just >> issuing the predicate as an sql string or some generic equivalent. > > Person.where(e => e.name == "John") > > I'm thinking that we only need to convert the part that is prefixed > with, in this example, "e". Any other code should be executed in the > context of the caller. It should be possible to do this as well: > > auto foo = "John"; > auto result = Person.where(e => e.name == foo); > > Which will result in the same SQL query. > > I'm using a pluign to Ruby on Rails that does something similar but by > overloading operators. The problem with this approach, in Ruby, is that > you cannot overload operators like || and &&, so instead they overload | > and & resulting in new problems like operator precedence. Example: > > Person.where{ |e| (e.name == "John") & (e.address == "Main street") } >
What about something like this? class Person { macro where (Context context, Statement statement) { // ... } } auto foo = "John"; auto result = Person.where(e => e.name == foo); // is replaced by auto foo = "John"; auto result = Person.query("select * from person where person.name = " ~ sqlQuote(foo) ~ ";");