 
     ### migrate data where needed ###
 
-    if Zotero.DB.tableExists('betterbibtex."-keys-"')
+    if @tableExists('betterbibtex."-keys-"')
       @debug('attachdatabase: migrating keys')
-      if Zotero.DB.columnsHash('betterbibtex."-keys-"').pinned
+      if @columnsHash('betterbibtex."-keys-"').pinned
         @debug('attachdatabase: migrating old-sty;e keys')
         Zotero.BetterBibTeX.debug('Upgrading betterbibtex.keys')
         Zotero.DB.query('insert into betterbibtex.keys (itemID, citekey, citekeyFormat)

        Zotero.DB.query('insert into betterbibtex.keys (itemID, citekey, citekeyFormat)
                        select itemID, citekey, citekeyFormat from betterbibtex."-keys-"')

    if Zotero.DB.tableExists('betterbibtex."-autoexport-"')
      @debug('attachdatabase: migrating autoexport')
      Zotero.DB.query('insert into betterbibtex.autoexport (
        collection,

  statement.executeStep()
  statement.finalize()

Zotero.BetterBibTeX.parseTable = (name) ->
  name = name.split('.')
  switch name.length
    when 1
      schema = ''
      name = name[0]
    when 2
      schema = name[0] + '.'
      name = name[1]
  name = name.slice(1, -1) if name[0] == '"'
  return {schema: schema, name: name}

Zotero.BetterBibTeX.columnsHash = (name) ->
  table = @parseTable(name)
  statement = Zotero.DB.getStatement("pragma #{table.schema}table_info(\"#{table.name}\")", null, true)

  # Get name column
  for i in [0...statement.columnCount]
    col = i if statement.getColumnName(i).toLowerCase() == 'name'

  columns = {}
  while statement.executeStep()
    columns[Zotero.DB._getTypedValue(statement, col)] = true
  statement.finalize()

  return columns

Zotero.BetterBibTeX.tableExists = (name) ->
  table = @parseTable(name)
  return (Zotero.DB.valueQuery("SELECT count(*) FROM #{table.schema}sqlite_master WHERE type='table' and name=?", [table.name]) != 0)

Zotero.BetterBibTeX.attachDatabase = ->
  db = Zotero.getZoteroDatabase('betterbibtex')
