Getting available table from database

I am trying to write an ODBC adapter that will take the driver type and driver source and make a connection to the database. Passing the data source eludes me.
Can someone please help me understand how to archive this.

package odbcstream

import (
	"database/sql"
	"fmt"
	_"github.com/alexbrainman/odbc"
)

// A pointer to an instance of the database
var DBClient *sql.DB

// Initializing variables for data source credentials
var server, user, password, database string

type Table struct {
	name string
}

// Initializes a connection to the database of choice
func InitialiseDBConnection(driverType, dataSourceName string) error{
	dataSourceName = fmt.Sprintf("server=%s;user id=%s;password=%s;database=%s;",
        server, user, password, database)

	db, err := sql.Open(driverType, dataSourceName)
	if err != nil {
		panic(err.Error())
	}

	// Checking if database is connected
	err = db.Ping()
	if err != nil {
		panic(err.Error())
	}

	DBClient = db
	return db.Close()
}

// Getting list of tables in database
func List(t *Table) {
	var tables []Table
	ta, err := DBClient.Query("SELECT * FROM sys.Table") 
		if err != nil {
			panic(err.Error())
		}
	for ta.Next() {
		var table Table
		if err := ta.Scan(&table.name); err != nil {
			fmt.Println("An error scanning for tables", err)
			return
		}
		tables = append(tables, table )
	}
}


package main

import (
	"odbcstream/odbcstream"
	// "fmt"
)

func main() {
       // psql is "driver" type and "france" is datasource
	odbcstream.InitialiseDBConnection("psql",	"france")
	// fmt.Println("Hello Go")
}

I do not know what you are looking for, but I am struggling with similar problem. Some nearly solved. http://94.237.92.101:6060/api

1 Like

Thank you for your effort. I actually want to make a secure connection to any database (i.e: postgresql, MySql, MSSQL, …) through the ODBC adapter interface and retrieve available tables in the source specified as the database source

But why do you want to use ODBC? My experience is that every middle tier complicates rather than simplifies.

1 Like

I thought ODBC could replace the underlining driver type be it postgressql, MSSQL, etc and I just have to specify in the function argument the kind of driver the database is using.
Is there a better way to do it?
Am open to learning please.

My personal view is that you get most out of a RDBMS when you focus on one system, rather than a generic driver. For an example a simple WITH clause has been available in Postgresql for years. In MySQL just recently. The lack of some functionality will limit your ability to produce efficient SQL queries. If you have a generic driver, I guess that you must use the least common ground which limits the query possibilities.

The same view applies on any ORM generic that also limits the ability to write complex SQL queries. The magic creates limitation sooner or later.

I have worked with Postgresql for almost a decade and it has been rock solid and gives you the ability to mix noSQL and SQL against the same database.

And one last thing that I find important. When you need help from a community, I have noticed fewer answers to questions that are using any sort of unknown “middleware”, because the knowledge is limited with a certain combination of code.

1 Like

Thanks a bunch for the knowledge and experience shared. I am actually a newbie to Go and DBMS’s but I really appreciate the clearance and explanation.