Demo entry 5862493

mypythoncode

   

Submitted by anonymous on Aug 07, 2016 at 14:14
Language: JavaScript. Code size: 3.0 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;
        console.log("select_tableid %s",select_tableid);
        if(select_tableid) {
          tableId = select_tableid;
        }
        query('SELECT * FROM ' + tableId);
      }
            
        // the execute function of
        // gapi.client.request().execute(callback) requires 
        // a function with a single argument
        // in the function the responce is handeled
        function callback(element) {
          console.log("c_element %s",element);
          return function(resp) {
            var output = JSON.stringify(resp);
            document.getElementById(element).innerHTML = output;
            console.log('resp["tableId"] ',resp['tableId'] );
            // distinguish if we have a new table id or an existing one
            resp_tableid = resp['tableId']
            if(resp_tableid) {
              tableId = resp['tableId'];
            }
            updateInnerHTMLFields();
          };
        }

      // This function handles the whole query mojo
      function query(query) {
        var lowerCaseQuery = query.toLowerCase();
        var path = '/fusiontables/v2/query';
        var body = 'sql=' + encodeURIComponent(query);
        // in this branch any reading and writing requests are handled
        if (lowerCaseQuery.indexOf('select') == 0) {
          // after creating an URI object from the string
          // only reading requests "GET" are handled
          runClientRequest({
            path: path,
            params: {
              'sql': query
            }
          }, callback('select-data-output'));
          
        } else {
          // the next auxiliary function is called  together with a writing request "POST"
          runClientRequest({
            path: path,
            body: body,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: 'POST'
          }, callback('insert-data-output'));
          
        }
                
      } // query()

      // 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(args, callback) {
        gapi.client.request(args).execute(callback);
      }
      
      // This auxiliary function simply updates the text for the table id.
      function updateInnerHTMLFields(){
        document.getElementById('table-id-select').innerHTML = tableId;
        document.getElementById('table-id-insert').innerHTML = tableId;
      }

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).