15

During my routine work, i happened to write the chained javascript function which is something like LINQ expression to query the JSON result.

var Result = from(obj1).as("x").where("x.id=5").groupby("x.status").having(count("x.status") > 5).select("x.status");

It works perfectly and give the expected result.

I was wondering this looks awesome if the code is written like this (in a more readable way)

var Result = from obj1 as x where x.status
groupby x.status having count(x.status)  > 5
select x.status;

is there a way to achieve this??

Cheers

Ramesh Vel

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
RameshVel
  • 64,778
  • 30
  • 169
  • 213

8 Answers8

20

No. JavaScript doesn't support this.

But this looks quite good too:

var Result =  from(obj1)
             .as("x")
             .where("x.id=5")
             .groupby("x.status")
             .having(count("x.status") > 5)
             .select("x.status");
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
6

Most people insist on trying to metaprogram from inside their favorite language. That doesn't work if the language doesn't support metaprogramming well; other answers have observed that JavaScript does not.

A way around this is to do metaprogramming from outside the language, using program transformation tools. Such tools can parse source code, and carry out arbitrary transformations on it (that's what metaprogramming does anyway) and then spit the revised program.

If you have a general purpose program transformation system, that can parse arbitrary languages, you can then do metaprogramming on/with whatever language you like. See our DMS Software Reengineering Toolkit for such a tool, that has robust front ends for C, C++, Java, C#, COBOL, PHP, and ECMAScript and a number of other programming langauges, and has been used for metaprogramming on all of these.

In your case, you want to extend the JavaScript grammar with new syntax for SQL queries, and then transform them to plain JavaScript. (This is a lot like Intentional Programming) DMS will easily let you build a JavaScript dialect with additional rules, and then you can use its program transformation capabilities to produce the equivalent standard Javascript.

Having said, that, I'm not a great fan of "custom syntax for every programmer on the planet" which is where Intentional Programming leads IMHO.

This is a good thing to do if there is a large community of users that would find this valuable. This idea may or may not be one of them; part of the problem is you don't get to find out without doing the experiment, and it might fail to gain enough social traction to matter.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
5

although not quite what you wanted, it is possible to write parsers in javascript, and just parse the query (stored as strings) and then execute it. e.g.,using libraries like http://jscc.jmksf.com/ (no doubt there are others out there) it shouldnt be too hard to implement.

but what you have in the question looks great already, i m not sure why you'd want it to look the way you suggested.

Chii
  • 14,540
  • 3
  • 37
  • 44
  • thanks for the reply Chii.. Actually i was just experimenting the different possibilities. i am with c# background, recentlty started in javascript and just wanted to explore the JS features... thats the reason.. And thanks for the link.. I have already look around the JS parsers like the one you have mentioned.. and i found http://www.codeplex.com/jsinq is useful.. thanks again.. – RameshVel Sep 01 '09 at 10:52
5

Considering that this question is asked some years ago, I will try to add more to it based on the current technologies.

As of ECMAScript 6, metaprogramming is now supported in a sense via Symbol, Reflect and Proxy objects.

By searching on the web, I found a series of very interesting articles on the subject, written by Keith Kirkel:

Metaprogramming in ES6: Symbols and why they're awesome

In short, Symbols are new primitives that can be added inside an object (without practically being properties) and are very handy for passing metaprogramming properties to it among others. Symbols are all about changing the behavior of existing classes by modifying them (Reflection within implementation).

Metaprogramming in ES6: Part 2 - Reflect

In short, Reflect is effectively a collection of all of those “internal methods” that were available exclusively through the JavaScript engine internals, now exposed in one single, handy object. Its usage is analogous to the Reflection capabilities of Java and C#. They are used to discover very low level information about your code (Reflection through introspection).

Metaprogramming in ES6: Part 3 - Proxies

In short, Proxies are handler objects, responsible for wrapping objects and intercepting their behaviors through traps (Reflection through intercession).

Of course, these objects provide specific metaprogramming capabilities, much more restrictive compared to metaprogramming languages, but still can provide handy ways of basic metaprogramming, mainly through Reflection practices, in fact.

In the end, it is worth mentioning that there is some worth-noticing ongoing research work on staged metaprogramming in JavaScript.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
3

Well, in your code sample:

var Result = from(obj1)
            .as("x")
            .where("x.id=5")
            .groupby("x.status")
            .having(count("x.status") > 5)
            .select("x.status");

The only problem I see (other than select used as an identifier) is that you embed a predicate as a function argument. You'd have to make it a function instead:

            .having(function(x){ return x.status > 5; })

JavaScript has closures and dynamic typing, so you can do some really nifty and elegant things in it. Just letting you know.

Joey Adams
  • 41,996
  • 18
  • 86
  • 115
2

In pure JS no you can not. But with right preprocessor it is possible.

You can do something similar with sweet.js macros or (God forgive me) GPP.

mixture
  • 157
  • 2
  • 12
1

Wat you want is to change the javascript parser into an SQL parser. It wasn't created to do that, the javascript syntax doesn't allow you to.

What you have is 90% like SQL (it maps straight onto it), and a 100% valid javascript, which is a great achievement. My answer to the question in the title is: YES, metaprogramming is possible, but NO it won't give you an SQL parser, since it's bound to use javascript grammar.

xtofl
  • 40,723
  • 12
  • 105
  • 192
0

Maybe you want something like JSONPath if you've got JSON data. I found this at http://www.json.org/. Lots of other tools linked to from there if it's not exactly what you need.

(this is being worked on as well: http://docs.dojocampus.org/dojox/json/query)

Glenn
  • 5,334
  • 4
  • 28
  • 31