Systems Connectivity
This project provides a Go-based library for connecting to and querying multiple database types, including PostgreSQL, MySQL, SQLite, and SQL Server. It features a SystemDatabase
struct to manage database connections and execute queries, with an output format defined by the OutputData
struct for agnostic key-value pair handling.
Table of Contents
Installation
Ensure Go is installed on your system (1.16+ recommended).
Install required dependencies:
go mod tidyImport the library in your Go project:
get "github.com/jvetereaoh/systemsConnectivity/v2"
Usage
Initialize a Database Connection
To connect to a database, provide a context and credentials using the CreateSystemDatabase
function:
Setting and Executing Queries
You can set a query and retrieve results using Select
. The results are returned as a slice of OutputData
, where each row is stored as a key-value map.
Code Structure
SystemDatabase
(insystemDatabase
package): Manages database connections and queries.CreateSystemDatabase(ctx context.Context, credentials map[string]string)
: Establishes a database connection.Close()
: Closes the database connection.SetQuery(query string)
: Sets the SQL query to execute.Select() ([]data.OutputData, error)
: Executes the query and returns the result as a slice ofOutputData
.
OutputData
(indata
package): A flexible struct for storing query results as key-value pairs.NewOutputData()
: InitializesOutputData
.Get(key string)
: Retrieves the value for a given key.Set(key, value string)
: Sets a key-value pair in theOutputData
.
Database Support
Supported databases include:
PostgreSQL (
postgres
): Usesgithub.com/lib/pq
driver.MySQL (
mysql
): Requiresgithub.com/go-sql-driver/mysql
driver.SQLite (
sqlite3
): Usesgithub.com/mattn/go-sqlite3
.