DropTables

The DropTables method is used to drop one or more tables in the target database. DropTables accepts one or more go struct definitions that have been annotated with “sqac:” tags and uses the provided information to drop tables in the database. Note that the DropTables method will not attempt to drop a table that does not exist in the target database. In such a case, no error is reported as the database is already in the desired state.

Errors encountered during table creation are returned to the caller immediately. This may result in the incomplete processing of the tables contained in parameter i …interface{}.

  type Depot struct {
    DepotNum   int       `db:"depot_num" sqac:"primary_key:inc"`
    CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
    Country    string    `db:"country" sqac:"nullable:false;default:CA"`
    Province   string    `db:"province" sqac:"nullable:false;default:AB"`
    Region     string    `db:"region" sqac:"nullable:false;default:YYC"`
  }

The struct shown above can be used by sqac to refer to a table named “depot” in the target database. In order to call the PublicDB.DropTables() method, an instance of PublicDB must be created and connected to the database.

DropTables Example

A small sample program illustrating the dropping of table “depot” follows:

package main

import (
  "log"

  "github.com/1414C/sqac"
  _ "github.com/SAP/go-hdb/driver"
  _ "github.com/denisenkom/go-mssqldb"
  _ "github.com/go-sql-driver/mysql"
  _ "github.com/lib/pq"
  _ "github.com/mattn/go-sqlite3"
)

func main() {

  var (
    Handle sqac.PublicDB
  )

  // Struct related to database table "depot"
  type Depot struct {
    DepotNum   int       `db:"depot_num" sqac:"primary_key:inc"`
    CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
    Country    string    `db:"country" sqac:"nullable:false;default:CA"`
    Province   string    `db:"province" sqac:"nullable:false;default:AB"`
    Region     string    `db:"region" sqac:"nullable:false;default:YYC"`
    Population int       `db:"population" sqac:"nullable:false;default:0;index:non-unique"`
  }

  // Create a PublicDB instance to connect to the test sqlite db
  Handle = sqac.Create("sqlite", false, false, "testdb.sqlite")

  // Drop table "depot" in the target database
  err := Handle.DropTables(Depot{})
  if err != nil {
    log.Errorf("%s", err.Error())
  }

  // Close the connection
  Handle.Close()
}