ionic-team / ng-cordova

OBSOLETE: Please move to Ionic Native https://github.com/ionic-team/ionic-native
https://github.com/ionic-team/ionic-native
MIT License
3.48k stars 1.05k forks source link

$cordovaSQLite.execute() ignores PRAGMA foreign_keys #1166

Open Steffan-Ennis opened 8 years ago

Steffan-Ennis commented 8 years ago

I had a issue when using the $cordovaSqlite service to create my tables. It was ignoring the foreign_keys PRAGMA. I had to run the statements on db.executeSql to get the required results.

Original Code

  if (window.cordova) {
    var db = window.sqlitePlugin.openDatabase({
      name: 'my.db'
    });
    $rootScope.db = db;
    db.executeSql("PRAGMA foreign_keys = ON;", [], function (res) {
      console.log(JSON.stringify(res));
    });

    var keys = Object.keys(SqlLiteQueries.setupQueries);
    keys.forEach(function (key, index) {
      $cordovaSQLite.execute($rootScope.db, SqlLiteQueries.setupQueries[key])
        .then(function (success) {
          console.log(success);
          console.log(success.rows.item(0));
          console.log(key);
        }, function (error) {
          console.log(error);
          console.log(key);
        });
    });
    $rootScope.$broadcast('dbCreation', $rootScope.db);
    GuestType.findGuestTypes();

New Code

      if (window.cordova) {
        var db = window.sqlitePlugin.openDatabase({
          name: 'my.db'
        });
        $rootScope.db = db;
        db.executeSql("PRAGMA foreign_keys = ON;", [], function (res) {
          console.log(JSON.stringify(res));
        });

        var keys = Object.keys(SqlLiteQueries.setupQueries);
        keys.forEach(function (key, index) {
          db.executeSql(SqlLiteQueries.setupQueries[key], [], function (res) {
            console.log(res);
            console.log(res.rows.item(0));
            console.log(key);
          }); //, function (error) {
          //   console.log(error);
          //   console.log(key);
          // });
        });
isaacfi commented 8 years ago

Hi, can you see if this works? I had added this code (executeSql) to ng-cordova.js to not use transaction that causes the problem.

return {
      openDB: function (options, background) {

        if (angular.isObject(options) && !angular.isString(options)) {
          if (typeof background !== 'undefined') {
            options.bgType = background;
          }
          return $window.sqlitePlugin.openDatabase(options);
        }

        return $window.sqlitePlugin.openDatabase({
          name: options,
          bgType: background
        });
      },

     executeSql: function (db, query, binding) {
        var q = $q.defer();
          db.executeSql(query, binding
            , function (tx, result) {
              q.resolve(result);
            },
            function (transaction, error) {
              error.query = query;
              error.binding = binding;
              q.reject(error);
            });
        return q.promise;
      },

      execute: function (db, query, binding) {
        var q = $q.defer();
        db.transaction(function (tx) {
          tx.executeSql(query, binding
            , function (tx, result) {
              q.resolve(result);
            },
            function (transaction, error) {
              error.query = query;
              error.binding = binding;
              q.reject(error);
            });
        });
        return q.promise;
      },