7 Replies Latest reply on Jul 10, 2013 2:49 AM by Mariano Colabraro

    local database in SB1

    Dragos Pop

      Hi,

       

      I am trying to create a local database in RHOEL on SB1 but I get SECURITY_ERR: DOM Exception 18

      Do you think that there are RHOEL settings that can be changed to avoid this problem ?

       

      Below is a small sample page to reproduce the error.

      <!DOCTYPE html>

      <html>

      <head> 

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

      <title>Webshop</title> 

      </head> 

      <body> 

      <script type="text/javascript">

              var db;

              try {

                  if (window.openDatabase)

                      db = openDatabase("Webshop", "1.0", "", 5 * 1024 * 1024);

                  else

                      throw "window.openDatabase is not supported!";

              }

         catch (err) {

                  alert('db open = ' + err.message);

              }

      </script>

      </body> 

      </html>

        • Re: local database in SB1
          Pietro Francesco Maggi

          Hi Dragos,

          try with a smaller databse!

           

          Usually 5000000 it's default maximum websql database value and you're asking for "something more".

           

          The configuration of this quota is inside the config.xml file:

           

          <WebDB>

               <WebStorageDBPath VALUE="file://\Program Files\RhoElements"/>

               <WebSQLDBQuota    VALUE="5000000"/>

               <WebSQLDBPath     VALUE="file://\Program Files\RhoElements"/>

          </WebDB>

           

          Ciao

          Pietro

            • Re: local database in SB1
              Dragos Pop

              You were right Pietro, but there is another strange issue.

               

              I changed a little bit my sample this way and it works:

              <!DOCTYPE html>
              <html>
              <head> 
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
              <title>Webshop</title> 
              </head> 
              <body> 
              <link  href="asl.css" rel="stylesheet" type="text/css" media="screen, projection" /> 
              <script src="asl.js" type="text/javascript" ></script>
              <script type="text/javascript">

                  asl.events.subscribe(asl.events.types.loaded, function () {
                var db;

                try {
                 if (window.openDatabase)
                  db = openDatabase("Webshop", "1.0", "", 4000000);
                 else
                  throw "window.openDatabase is not supported!";
                 alert('OK 3MB', function () { asl.exit(); }););
                }
                catch (err) {
                 alert('db open = ' + err.message, function () { asl.exit(); }););
                }
              });
              </script>
              </body> 
              </html>

               

               

              But when the openDatabase is called from a included javascript file,

              <!DOCTYPE html>

              <html>

              <head> 

              <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

              <title>Webshop</title> 

              </head> 

              <body> 

              <link  href="asl.css" rel="stylesheet" type="text/css" media="screen, projection" /> 

              <script src="asl.js" type="text/javascript" ></script>

              <script src="dbTest.js" type="text/javascript"></script>

              <script type="text/javascript">

                  asl.events.subscribe(asl.events.types.loaded, function () {

                      try {

                          if (window.openDatabase)

                              db.open(4000000);

                          else

                              throw "window.openDatabase is not supported!";

                          alert('OK 4MB', function () { asl.exit(); });

                      }

                  catch (err) {

                          alert('db open = ' + err.message, function () { asl.exit(); });

                      }

                  });

              </script>

              </body> 

              </html>

               

              and the following content of dbTest.js

              var db = {};

              db.open = function (dbSize) {
              if (!dbSize)
                dbSize = 4000000;

              if (window.openDatabase)
                db.local = openDatabase("WebShop", "1.0", "", dbSize);
              else
                throw "window.openDatabase is not supported!";
              }

               

               

              we got again the DOM Exception 18 error.

                • Re: local database in SB1
                  Pietro Francesco Maggi

                  I don't have an SB1 handy to do some testing.

                  Give the issue I'll probably try to download the asl.js library and the stylesheet on a PC and test your code inside Chrome dev tools.

                   

                  trying to "compile this" reading it, don't you miss a "();" or at least a simple ";" at the end of the db.open assignment?

                  I think that you need to have:

                  db.open = function (dbSize) {
                  if (!dbSize)
                    dbSize = 4000000;

                  if (window.openDatabase)
                    db.local = openDatabase("WebShop", "1.0", "", dbSize);
                  else
                    throw "window.openDatabase is not supported!";
                  }();

                    • Re: local database in SB1
                      Dragos Pop

                      I tried with the ";" at the end of the db.open assignment but the result was the same. I also moved the scripts from the body section to the head section of the html file but it continues with the DOM Exception 18 error.

                       

                      <!DOCTYPE html>

                      <html>

                      <head> 

                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

                          <title>Webshop</title> 

                          <link  href="asl.css" rel="stylesheet" type="text/css" media="screen, projection" /> 

                          <script src="asl.js" type="text/javascript" ></script>

                          <script src="dbTest.js" type="text/javascript"></script>

                          <script type="text/javascript">

                              asl.events.subscribe(asl.events.types.loaded, function () {

                                  try {

                                      db.open(4000000);

                                      alert('OK 4MB', function () { asl.exit(); });

                                  }

                            catch (err) {

                                      alert('db open = ' + err.message, function () { asl.exit(); });

                                  }

                              });

                      </script>

                      </head> 

                      <body> 

                      </body> 

                      </html>

                       

                      content of dbTest.js

                      var db = {};

                      db.open = function (dbSize) {

                          if (!dbSize)

                              dbSize = 4000000;

                        if (window.openDatabase)

                              local = openDatabase("WebShop", "1.0", "", dbSize);

                          else

                              throw { message: "window.openDatabase is not supported!" };

                      };

                       

                      I have also registered some situations when the updates of the included js files from the web server are not considered by SB1 (it is still using the previous version until a reboot) but I think I will prepare another thread on the forum on this issue.

                • Re: local database in SB1

                  I'm trying to follow the documentation suggested by SB1 Programmer guide on Web SQL Database and for what I see the implementation is different.

                   

                  interface WindowDatabase {

                    Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback);

                  };

                   

                  When invoked, these methods must run the following steps, with all but the last two steps being run atomically:

                  1. The user agent may raise a SECURITY_ERR exception instead of returning a Database object if the request violates a policy decision (e.g. if the user agent is configured to not allow the page to open databases).
                  2. For the method on the Window object: let origin be the origin of the active document of the browsing context of the Window object on which the method was invoked.For the methods on the WorkerUtils object: let origin be the origin of the scripts in the worker.
                  3. If origin is not a scheme/host/port tuple, then throw a SECURITY_ERR exception and abort these steps.
                  4. If the database version provided is not the empty string, and there is already a database with the given name from the origin origin, but the database has a different version than the version provided, then throw an INVALID_STATE_ERR exception and abort these steps.
                  5. If no database with the given name from the origin origin exists, then create the database and let created be true. If a callback was passed to the method, then set the new database's version to the empty string. Otherwise, set the new database's version to the given database version.Otherwise, if a database with the given name already exists, let created be false.
                  6. For the openDatabase() methods: let result be a newly constructed Database object representing the database with the given database name from the origin origin.For the openDatabaseSync() method: let result be a newly constructed DatabaseSync object representing the database with the given database name from the originorigin.
                  7. If created is false or if no callback was passed to the method, skip this step. Otherwise:For the openDatabase() methods: queue a task to to invoke the callback with result as its only argument.For the openDatabaseSync() method: invoke the callback with result as its only argument. If the callback throws an exception, rethrow that exception and abort these steps.
                  8. Return result.

                  All strings including the empty string are valid database names. Database names must be compared in a case-sensitive manner.

                   

                  The snippet of code they recommend is this following:

                   

                  function prepareDatabase(ready, error) {

                    return openDatabase('documents', '1.0', 'Offline document storage', 5*1024*1024, function (db) {

                      db.changeVersion('', '1.0', function (t) {

                        t.executeSql('CREATE TABLE docids (id, name)');

                      }, error);

                    });

                  }

                   

                  function showDocCount(db, span) {

                    db.readTransaction(function (t) {

                      t.executeSql('SELECT COUNT(*) AS c FROM docids', [], function (t, r) {

                        span.textContent = r.rows[0].c;

                      }, function (t, e) {

                        // couldn't read database

                        span.textContent = '(unknown: ' + e.message + ')';

                      });

                    });

                  }

                   

                  prepareDatabase(function(db) {

                    // got database

                    var span = document.getElementById('doc-count');

                    showDocCount(db, span);

                  }, function (e) {

                    // error getting database

                    alert(e.message);

                  });

                   

                   

                   

                   

                   

                  So it would be interesting to retrieve one of this Error codes if something is not working:

                   

                   

                  UNKNOWN_ERR0The transaction failed for reasons unrelated to the database itself and not covered by any other error code.
                  DATABASE_ERR1The statement failed for database reasons not covered by any other error code.
                  VERSION_ERR2The operation failed because the actual database version was not what it should be. For example, a statement found that the actual database version no longer matched the expected version of the Database or DatabaseSync object, or the Database.changeVersion() orDatabaseSync.changeVersion() methods were passed a version that doesn't match the actual database version.
                  TOO_LARGE_ERR3The statement failed because the data returned from the database was too large. The SQL "LIMIT" modifier might be useful to reduce the size of the result set.
                  QUOTA_ERR4The statement failed because there was not enough remaining storage space, or the storage quota was reached and the user declined to give more space to the database.
                  SYNTAX_ERR5The statement failed because of a syntax error, or the number of arguments did not match the number of ? placeholders in the statement, or the statement tried to use a statement that is not allowed, such as BEGIN, COMMIT, or ROLLBACK, or the statement tried to use a verb that could modify the database but the transaction was read-only.
                  CONSTRAINT_ERR6An INSERT, UPDATE, or REPLACE statement failed due to a constraint failure. For example, because a row was being inserted and the value given for the primary key column duplicated the value of an existing row.
                  TIMEOUT_ERR7A lock for the transaction could not be obtained in a reasonable time.

                  Source:http://www.w3.org/TR/webdatabase/

                  • Re: local database in SB1

                    Look I followed the sample from the documentation and it works for me. Find attached the page and let me know.

                     

                    Source: http://slides.html5rocks.com/#web-sql-db