Demo entry 5862461

mypythoncode

   

Submitted by anonymous on Aug 04, 2016 at 22:05
Language: JavaScript. Code size: 2.6 kB.

   //  To select the data from a given table id, 
   //  first the table id is requested with the "getElementById" function
   //  and then a query function is build and send to the next function as an argument
   function selectData() {
        var select_tableid = document.getElementById('select_tableid').value;
        if(select_tableid) {
          tableId = select_tableid;
        }
        query('SELECT * FROM ' + tableId);
      }

      // This auxiliary function simply updates the text for the table id.
      function updateInsertSelectFields(){
        document.getElementById('table-id-select').innerHTML = tableId;
        document.getElementById('table-id-insert').innerHTML = tableId;
      }
      

      // This function handles the whole query mojo
      function query(query) {
        var lowerCaseQuery = query.toLowerCase();
        var path = '/fusiontables/v2/query';
        // function to handle the response and displaying it on the website
        var callback = function(element) {
          return function(resp) {
            var output = JSON.stringify(resp);
            document.getElementById(element).innerHTML = output;
          };
        }
        // in this branch any writing request is handled 
        if (lowerCaseQuery.indexOf('select') != 0 &&
          lowerCaseQuery.indexOf('show') != 0 &&
          lowerCaseQuery.indexOf('describe') != 0) {

          // after creating an URI object from the string
          // the next auxiliary function is called  together with a writing request "POST"
          var body = 'sql=' + encodeURIComponent(query);
          runClientRequest({
            path: path,
            body: body,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: 'POST'
          }, callback('insert-data-output'));

        // in this else branch only reading requests "GET" are handled
        } else {
          runClientRequest({
            path: path,
            params: {
              'sql': query
            }
          }, callback('select-data-output'));
        }
        
        updateInsertSelectFields();
      }

      // Finally in this function the query request is send to Fusion Tables API
      // and the response is then handled in the callback function defined previously 
      function runClientRequest(request, callback) {
        var restRequest = gapi.client.request(request);
        restRequest.execute(callback);
      }

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).