Getting a PublicDB Handle

Before using the sqac API, a connection must be established to the target database. The main interface used by sqac is called PublicDB and we will call this the so-called Handle variable in the sample code.

  // Handle will be the central access-point to the ORM and should be made
  // available in all locations where access to the persistent storage
  // (database) is required.
  var (
    Handle sqac.PublicDB
  )

Once the Handle variable has been declared, an instance must be created via the sqac.Create function. The sqac.Create call requires information regarding the type of database, whether logging should be activated, as well as a connection string for the database. sqac.Create will fail in an absolute way if it encounters a problem. It is therefore reasonably safe to just use the returned Handle variable without additional checking. If sqac.Create does not result in a fatal error, it is safe to proceed.

Samples for each of the supported database systems follow:

Handle to PostgreSQL
  // Create a PublicDB instance for PostGreSQL
  cs := "host=127.0.0.1 user=my_uname dbname=my_dbname sslmode=disable password=my_passwd"
  Handle = sqac.Create("postgres", false, false, cs)
Handle to MSSQL
  // Create a PublicDB instance for MSSQL
  cs := "sqlserver://SA:my_passwd@localhost:1401?database=my_dbname"
  Handle = sqac.Create("mssql", false, false, cs)
Handle to Sqlite
  // Create a PublicDB instance for Sqlite
  cs := "testdb.sqlite"
  Handle = sqac.Create("sqlite", false, false, cs)
Handle to MySQL
  // Create a PublicDB instance for MySQL
  cs := "my_uname:my_passwd@tcp(192.168.1.10:3306)/my_dbname?charset=utf8&parseTime=True&loc=Local"
  Handle = sqac.Create("mysql", false, false, cs)
Handle to SAP Hana
  // Create a PublicDB instance for SAP Hana
  cs := "hdb://my_uname:my_passwd@192.168.111.45:30015"
  Handle = sqac.Create("hdb", false, false, cs)