Thursday, June 17, 2010

JSOND

In my last post, I introduced the Rdb.js library, which allows making requests of an Rdbhost database, provided that the Rdbhost server has been aliased to a subdomain of your domain.

The cross-site data retrieval relies on the data page containing a little executable javascript, that, when loaded into an iframe, changes the iframe 's document.domain to a right-hand subset of the domain that then matches the main frame, similarly treated.

The Rdb.js library handles all this for you, but I describe the method here in case anybody wants to create an alternate library, or modify the library.

The request goes to the server using the client's domain, and with the 'format' parameter set to 'jsond'. The data returned by the server will be formatted similar to:

<html>
<head>
  <script type="text/javascript" >
    window.document.domain=
    "window.document.domain.split('.').slice(-2).join('.');"
  </script></head>
<body>
  <script type="text/plain">
    { ...json here... }
  </script>
</body>
</html>

Lines 4 and 5 execute when the data is loaded into the iframe, changing the domain. It removes all but the rightmost two dot-delimited segments. For example, 'rdbhost.example.com' becomes 'example.com'.

The calling frame also executes the document.domain manipulation so the two have the same domain value. The caller can then load the data from the body script container, and convert it using a JSON parser.

The container is a script with type 'text/plain', as the browsers will leave the contents of such a container uninterpreted, a raw string. The body of the script tag is escaped by replacing all instances of '</' with '<\/'. The JSON parser, or Javascript's eval, will interpret the two identically, so no unescaping is required.

Wednesday, June 16, 2010

Rdb.js


Cross-server data transfer is one of the more problematic areas of javascript in-browser programming today.

The obstacles are designed into the browser for good pragmatic security concerns, but sometimes you have legitimate need to retrieve data from multiple servers in one page.

Using Rdbhost databases from javascript applications is one such need, and we provide a library to make access easy and reliable. The method we recommend, the method our library supports, involves some domain name server (DNS) manipulation.

I assume you are hosting under your own domain name, and that your domain registrar provides a way to configure subdomains. Just create a subdomain of your domain that points at our server. The IP address of the Rdbhost server is on your profile page.

For example:


With that done, wait a few hours for the change to propagate across the internet.

Include the javascript module 'rdb.js' in your page, by reference, and create a javascript inline sequence to initialize an SQLEngine object. Here is a code sample, and line-by-line explanation follows


01 var uid = 'r0000000002';
02 var authcode = '2398473219847219834';
03 var rdb = new SQLEngine(uid,authcode,'rdbhost');
04 var query = 'SELECT * FROM css_data';
05 var res = rdb.query(  {'callback' : success,
06                        'q' : query } );
07 function success(json) {
08   // do something useful with data
09   for (var i=0; i<json.records.rows.length; i++) {
10     var val = json.records.rows[i];
11     alert('engine: '+val[0]);
12   }
13 }
Lines 01 and 02 just store the necessary user id and authcode, copied from the account_manager page.

Line 03 creates the SQLEngine object, which will handle making queries against the database.

Lines 04-06 query the database; the function success is called with a data structure containing the retrieved records.

Lines 07-13 implement the  callback success, which simply loops over the list of rows, displaying an alert box with the value of the first field in each record.


Testing
The above script can be pasted into a script element in an html page, after a script element loading the rdb.js library, and (with a valid uid/authcode combo) will function.  It does not need any supporting html, beyond the script container.

The jquery plugin DataTables works very well with Rdb.Js.  See examples here:

JSON Example 1
JSON Example 2


The javascript database client code is inline in the source files, so view-source will be informative.


API
The SQLEngine object has two methods intended for client use.  The query method, demonstrated above,
takes one object as a parameter, and expects at least two attributes in that object.  See the documentation
page on site for more specifics.


The other method is queryByForm, which takes three parameters:  the first is a string with the id of
the source form, the second is the success callback function, and the last is an optional error callback.  This method is useful if you wish to send file-sourced data to the server; you can put file-fields in your form, and after the user has selected files, the form can be submitted using the queryByForm method.  The form must have a field (hidden or otherwise) q for the query, and optionally format, arg### and argtype### fields.


Rdb.js documentation
  

Wednesday, June 9, 2010

PostgreSQL upgrade

The Postgresql server behind Rdbhost has been upgraded to the latest 8.3 maintenance release, version 8.3.11.

Tuesday, June 8, 2010

YASOP


Our Stackoverflow data dump hosting account has been updated to include the latest data.

Take a look at:


It now shows off a new custom look, designed for it by Branko Vukelic.

Each account can now host its own css file, which is loaded by the admin script in lieu of the default.

See this post on skinning.

Monday, June 7, 2010

Screencast is now shorter

Rdbhost's introductory (and so far, only) screencast has been slimmed down, from 8 minutes to 4 ½.


http://www.rdbhost.com/screencasts/introscreencast.html


Hopefully, more folks will watch, and more will finish.