Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

YQL native parameters + release 0.3. closes #4

  • Loading branch information...
commit ae55f66380ea5410e18b5df630d50a39e7d9e054 1 parent a286a77
Gabriel Falcão authored
5  ChangeLog
... ...
@@ -0,0 +1,5 @@
  1
+2011-01-03  Gabriel Falcão  <gabriel@nacaolivre.org>
  2
+
  3
+	* jquery-yql now supports @variable for automatic
  4
+	replacement. It's likely #{variable} but with "@" support.
  5
+
63  README.md
Source Rendered
... ...
@@ -1,5 +1,5 @@
1 1
 # jQuery YQL
2  
-> Version 0.2.2
  2
+> Version 0.3.0
3 3
 
4 4
 # What is this ?
5 5
 
@@ -11,7 +11,7 @@ through JSONP, and calls success callback with the results.
11 11
   <script type="text/javascript">
12 12
     $(function(){
13 13
       $.yql(
14  
-        "SELECT * FROM github.repo WHERE id='#{username}' AND repo='#{repository}'",
  14
+        "SELECT * FROM github.repo WHERE id=#{username} AND repo=@repository",
15 15
         {
16 16
           username: "gabrielfalcao",
17 17
           repository: "jquery-yql"
@@ -25,6 +25,65 @@ through JSONP, and calls success callback with the results.
25 25
      });
26 26
   </script>
27 27
 
  28
+## Explanation
  29
+
  30
+jquery-yql automatically handles variable replacement within the query.
  31
+
  32
+So if you use a query that looks like this:
  33
+
  34
+    "SELECT * FROM some_yql_table WHERE some_field=#{foo} AND another_field=#{bar}"
  35
+
  36
+with the object:
  37
+
  38
+    {
  39
+        'foo': 'Just a string',
  40
+        'bar': 42
  41
+    }
  42
+
  43
+You don't need to worry about adding quotes to non-numeric fields, for
  44
+example. Because we already do it for you.
  45
+
  46
+Thus, the query above will become this:
  47
+
  48
+    'SELECT * FROM some_yql_table WHERE some_field="Just a string" AND another_field=42'
  49
+
  50
+See ? jquery-yql automatically identifies if the value must be a string or number.
  51
+
  52
+### "@" variables
  53
+
  54
+Because of the ticket [#4](https://github.com/gabrielfalcao/jquery-yql/issues#issue/4) I've added support to `@variable` too.
  55
+So it's not a big deal, just works in the same way of the `#{variable}`.
  56
+
  57
+But allow me show it:
  58
+
  59
+    "SELECT * FROM some_yql_table WHERE some_field=@foo AND another_field=@bar"
  60
+
  61
+becomes:
  62
+
  63
+    'SELECT * FROM some_yql_table WHERE some_field="Just a string" AND another_field=42'
  64
+
  65
+### Last thing
  66
+
  67
+If you left variables without the respective key-value mapping
  68
+*mapped* in the translation `object`, jquery-yql will not touch it.
  69
+
  70
+
  71
+Once again, I show you:
  72
+
  73
+    "SELECT * FROM person WHERE name=@name AND age=#{years} AND wealth=@value'
  74
+
  75
+with
  76
+
  77
+    {
  78
+        'name': 'Gabriel',
  79
+        'years': '22'
  80
+    }
  81
+
  82
+Then, jquery-yql will resolve to:
  83
+
  84
+    'SELECT * FROM person WHERE name="Gabriel" AND age=22 AND wealth=@value'
  85
+
  86
+
28 87
 # License
29 88
 
30 89
 > Copyright (C) 2010  Gabriel Falcão <gabriel@nacaolivre.org>
12  jquery.yql.js
@@ -7,7 +7,7 @@
7 7
  *
8 8
  * http://github.com/gabrielfalcao/jquery-yql/raw/master/license.txt
9 9
  *
10  
- * Version: 0.2.2
  10
+ * Version: 0.3.0
11 11
  */
12 12
 
13 13
 (function($){
@@ -24,6 +24,16 @@
24 24
                          while (query.search(name) > -1) {
25 25
                              query = query.replace(name, value);
26 26
                          }
  27
+
  28
+                         var name = "@" + key;
  29
+                         var value = $.trim(this);
  30
+                         if (!value.match(/^[0-9]+$/)) {
  31
+                             value = '"' + value + '"';
  32
+                         }
  33
+                         while (query.search(name) > -1) {
  34
+                             query = query.replace(name, value);
  35
+                         }
  36
+
27 37
                      }
28 38
                  );
29 39
                  return query;
18  tests/test.yql.js
@@ -50,7 +50,7 @@ test('It should replace variables in the query string', function () {
50 50
          $.ajax = oldAjax;
51 51
      });
52 52
 
53  
-test('It should replace another variable in the query string', function () {
  53
+test('It should replace another #{variable} in the query string', function () {
54 54
          expect(1);
55 55
 
56 56
          $.ajax = function (params) {
@@ -65,6 +65,22 @@ test('It should replace another variable in the query string', function () {
65 65
 
66 66
          $.ajax = oldAjax;
67 67
      });
  68
+test('It should replace @variables', function () {
  69
+         expect(1);
  70
+
  71
+         $.ajax = function (params) {
  72
+             equal(params.data.q, 'SELECT * FROM woman WHERE hairColor="red" AND skinColor="red" AND age=@age');
  73
+         };
  74
+
  75
+         $.yql(
  76
+             'SELECT * FROM woman WHERE hairColor=@color AND skinColor=@color AND age=@age',
  77
+             {color: 'red'},
  78
+             function (data) { }
  79
+         )
  80
+
  81
+         $.ajax = oldAjax;
  82
+     });
  83
+
68 84
 
69 85
 asyncTest('Querying a yahoo official table (flickr)', function () {
70 86
               expect(3);

0 notes on commit ae55f66

Please sign in to comment.
Something went wrong with that request. Please try again.