Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRUNCATE TABLE not working on DB engines #762

Open
seb-ster opened this issue Oct 5, 2016 · 4 comments
Open

TRUNCATE TABLE not working on DB engines #762

seb-ster opened this issue Oct 5, 2016 · 4 comments

Comments

@seb-ster
Copy link
Contributor

seb-ster commented Oct 5, 2016

TRUNCATE TABLE works on 'in memory' DB. But using Local Storage DB returns error:

A.engines[r.engineid].truncateTable is not a function

Seems Truncating is done in the line where the yy.TruncateTable.prototype.execute method is defined.

For in 'in memory' DBs the line db.tables[tableid].data = []; is called, thus the Table is empty.

For other DB engines the method alasql.engines[db.engineid].truncateTable(this.table.databaseid || databaseid,tableid, this.ifexists, cb); is called.
It seems this method is not defined for any DB engine.

file: alasql.js
version: 0.3.2
on FF49.0.1, W10

@seb-ster
Copy link
Contributor Author

seb-ster commented Oct 5, 2016

A work around is:
DELETE FROM [table name]
but this will not reset AUTOINCREMENT/IDENTITY values on this table

note: AUTOINCREMENT/IDENTITY is not yet working on DB engines as per #462

@seb-ster
Copy link
Contributor Author

seb-ster commented Oct 5, 2016

I wrote and tested a truncateTable method for Local Storage:
This also also resets AUTOINCREMENT/IDENTITY values on this table

/**
   Empty table and reset identities
   @param databaseid {string} AlaSQL database id (not external localStorage)
   @param tableid {string} Table name
   @param ? i dont know what the 3rd argument is
   @param cb {function} Callback
   @return 1 on success
*/
LS.truncateTable = function(databaseid,tableid, unknown, cb){
  var res = 1
  var db = alasql.databases[databaseid];
  var tbl = LS.get(db.lsdbid+'.'+tableid);
  //clear data from table
  tbl.data = [];
  //reset all identities to 1
  for (var field in tbl.identities) {
    tbl.identities[field].value = 1;
  }

  db.tables[tableid] = tbl;

  if(cb)
     res = cb(res);

  return res;
}

@mathiasrw
Copy link
Member

This is great! lets get it into the code.

Would yo like to make a PR and get credits for the contribution, or would you prefer that I just extend the src with your input here?

seb-ster added a commit to seb-ster/alasql that referenced this issue Oct 5, 2016
Added LS.truncateTable method to Local Storage Engine
@seb-ster
Copy link
Contributor Author

seb-ster commented Oct 5, 2016

@mathiasrw
Hi, just did the PR (had not done that before)

seb-ster added a commit to seb-ster/alasql that referenced this issue Oct 8, 2016
This reverts commit a88c77d.

# Conflicts:
#	test/test607.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants