<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scriptNode &#187; github</title>
	<atom:link href="http://scriptnode.com/tag/github/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 01 Sep 2009 18:46:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JSQL: JavaScript Query Language</title>
		<link>http://scriptnode.com/article/jsql-javascript-query-language/</link>
		<comments>http://scriptnode.com/article/jsql-javascript-query-language/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 06:12:20 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[script-sunday]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=286</guid>
		<description><![CDATA[About JSQL

When I started my new job, I had to make the transition from YUI to jQuery. I&#8217;d seen some jQuery code before (in fact, its terseness and use of $ as both an object and a function were easy to spot), but I wasn&#8217;t familiar with it. I actually thought that the query part [...]]]></description>
			<content:encoded><![CDATA[<h3>About JSQL</h3>
<p>
When I started <a href="http://richter.paletteswap.com/why-i-left-yahoo-for-a-startup/">my new job</a>, I had to make the transition from <a href="http://developer.yahoo.com/yui/">YUI</a> to <a href="http://jquery.com/">jQuery</a>. I&#8217;d seen some jQuery code before (in fact, its terseness and use of <code>$</code> as both an object and a function were easy to spot), but I wasn&#8217;t familiar with it. I actually thought that the <em>query</em> part of jQuery meant there was some kind of <a href="http://mysql.com/">Standard Query Language</a> interface. There isn&#8217;t, but there is a really cool <a href="http://www.css3.info/">CSS3</a> selector instead.
</p>
<p>
Turns out, I was a little disappointed; I wanted to use something query-based. So, I started to throw this script together. I call it <acronym title="JavaScript Query Language">JSQL</acronym>. Below are some quick examples of what it looks like in action:
</p>
<p><script src="/assets/js/jsql/jsql-min.js" type="text/javascript"></script></p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var results = JSQL(&#8217;SELECT * FROM #ft WHERE nodeName = &#8220;LI&#8221;);<br />
</textarea></p>
<p>
JSQL always returns an object with an <code>error</code> property that&#8217;s either <code>Boolean false</code> or a <code>String</code> stating what error occurred (for example, &#8220;Unknown command UPDAT3&#8243;). If it successfully retrieves or operates on elements, it returns a <code>count:Number</code> property with the number of elements and an <code>elements:Array</code> property with those elements.
</p>
<p>
Here are some interactive examples:
</p>
<h3>Example 1: Checkboxes</h3>
<div class="section" id="jsql-example-1">
<div>
<input type="checkbox" value=""/> Option number 1
		</div>
<div>
<input type="checkbox" value=""/> Option number 2
		</div>
<div>
<input type="checkbox" value=""/> Option number 3
		</div>
</p></form>
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-1 SET checked = true WHERE type = &#8220;checkbox&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-1">Set Checked</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-1').onclick = function() {
JSQL('UPDATE #jsql-example-1 SET checked = true WHERE type = "checkbox"');
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-1 SET checked = false WHERE type = &#8220;checkbox&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-1a">Uncheck</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-1a').onclick = function() {
JSQL('UPDATE #jsql-example-1 SET checked = false WHERE type = "checkbox"');
};
</script></p>
<h3>Example 2: Form Validation</h3>
<div class="section" id="jsql-example-2">
<p>
			First name:</p>
<input type="text" value=""/>
<p>
			Last name:</p>
<input type="text" value=""/>
<p>
			Email:</p>
<input type="text" value=""/>
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-2 SET parentNode.className = &#8220;error&#8221; WHERE value = &#8220;&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-2">Highlight empty fields</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-2').onclick = function() {
JSQL('UPDATE #jsql-example-2 SET parentNode.className = "error" WHERE value = ""');
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-2 SET parentNode.className = &#8220;&#8221; WHERE value = &#8220;&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-2a">Reset</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-2a').onclick = function() {
JSQL('UPDATE #jsql-example-2 SET parentNode.className = "" WHERE value = ""');
};
</script></p>
<h3>Example 3: Removing Elements</h3>
<div class="section" id="jsql-example-3">
<strong>Lorem</strong> Ipsum is simply dummy text of the <strong>printing and</strong> typesetting industry. <strong>Lorem</strong> Ipsum has been the <strong>industry&#8217;s</strong> standard dummy text <strong>ever</strong> since the 1500s, when an unknown printer took a galley of type and scrambled it to make a <strong>type specimen</strong> book. It has survived not only <strong>five</strong> centuries, but also the leap into <strong>electronic</strong> typesetting<strong>, remaining essentially unchanged</strong>. It was popularised in the 1960s with the release of <strong>Letraset</strong> sheets containing <strong>Lorem Ipsum</strong> passages, and more recently with <strong>desktop publishing</strong> software like Aldus PageMaker including versions of <strong>Lorem</strong> Ipsum.
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;DELETE FROM #jsql-example-3 WHERE nodeName = &#8220;STRONG&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-3">Remove strong tags</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-3').onclick = function() {
JSQL('DELETE FROM #jsql-example-3 WHERE nodeName = "STRONG"');
};
</script></p>
<p>
In the spirit of jQuery, I also stuck in a quick <code>each</code> method which you can use to iterate through the returned elements immediately instead of setting up your own loop. The context is the current element (like jQuery) and the return value is still the same:</p>
<h3>Example 4: Selecting Elements</h3>
<div class="section" id="jsql-example-4">
Give him that <em>gun</em>. Give him <em>ALL</em> the guns.
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8221;SELECT * FROM #jsql-example-4 WHERE nodeName = &#8216;EM&#8217;&#8221;).each(function() {<br />
	this.style.background = &#8216;#ffff00&#8242;;<br />
});<br />
</textarea></p>
<p>
<button id="jsql-button-4">Select &amp; Operate</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-4').onclick = function() {
JSQL("SELECT * FROM #jsql-example-4 WHERE nodeName = 'EM'").each(function() {
	this.style.background = '#ffff00';
});
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8221;SELECT * FROM #jsql-example-4 WHERE nodeName = &#8216;EM&#8217;&#8221;).each(function() {<br />
	this.style.background = &#8221;;<br />
});<br />
</textarea></p>
<p>
<button id="jsql-button-4a">Undo</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-4a').onclick = function() {
JSQL("SELECT * FROM #jsql-example-4 WHERE nodeName = 'EM'").each(function() {
	this.style.background = '';
});
};
</script></p>
<p>
I started coding this several months ago, and just recently decided to revisit it. I found the code hard to read myself when making some modifications before publishing it here. That typically means it&#8217;s poorly written. And I&#8217;m admittedly lousy at writing parsing code&#8230; so there&#8217;s that. I also doubt it&#8217;s realistic for any real-world websites (other libraries handle those jobs quite well), but I thought it would be fun to play with.
</p>
<p>
Some noticeable features lacking are equivalents to <code>ALTER</code> and <code>INSERT INTO</code>. I&#8217;ve also got it <a href="http://github.com/richtaur/jsql/tree/master">up on GitHub</a>, so feel free to fork it, and maybe add those methods yourself!
</p>
<ul>
<li><a href="/assets/js/jsql/jsql.js">[Download jsql.js]</a> (11k)</li>
<li><a href="/assets/js/jsql/jsql-min.js">[Download jsql-min.js]</a> (3,661 bytes)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/jsql-javascript-query-language/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
