rsqldrv

package
v0.36.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package rsqldrv registers a database/sql/driver.Driver implementation for ROOT files.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connector

func Connector(file *riofs.File) driver.Connector

Connector returns a database/sql/driver.Connector from a ROOT file.

Connector can be used to open a database/sql.DB from an already open ROOT file.

func Create

func Create(name string) (*sql.DB, error)

Create is a ROOT/SQL-driver helper function for sql.Open.

It creates a new ROOT file, connected via the ROOT/SQL driver.

func Open

func Open(name string) (*sql.DB, error)

Open is a ROOT/SQL-driver helper function for sql.Open.

It opens a database connection to the ROOT/SQL driver.

Example
package main

import (
	"database/sql"
	"fmt"
	"log"
)

func main() {
	db, err := sql.Open("root", "../../testdata/simple.root")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM tree")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	type data struct {
		i32 int32
		f32 float32
		str string
	}

	n := 0
	for rows.Next() {
		var v data
		err := rows.Scan(&v.i32, &v.f32, &v.str)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("row[%d]: (%v, %v, %q)\n", n, v.i32, v.f32, v.str)
		n++
	}

}
Output:

row[0]: (1, 1.1, "uno")
row[1]: (2, 2.2, "dos")
row[2]: (3, 3.3, "tres")
row[3]: (4, 4.4, "quatro")
Example (Connector)
package main

import (
	"database/sql"
	"fmt"
	"log"

	"go-hep.org/x/hep/groot"
	"go-hep.org/x/hep/groot/rsql/rsqldrv"
	"go-hep.org/x/hep/groot/rtree"
)

func main() {
	f, err := groot.Open("../../testdata/simple.root")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	o, err := f.Get("tree")
	if err != nil {
		log.Fatal(err)
	}

	tree := o.(rtree.Tree)

	db := sql.OpenDB(rsqldrv.Connector(rtree.FileOf(tree)))
	defer db.Close()

	rows, err := db.Query("SELECT * FROM tree")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	type data struct {
		i32 int32
		f32 float32
		str string
	}

	n := 0
	for rows.Next() {
		var v data
		err = rows.Scan(&v.i32, &v.f32, &v.str)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("row[%d]: (%v, %v, %q)\n", n, v.i32, v.f32, v.str)
		n++
	}

}
Output:

row[0]: (1, 1.1, "uno")
row[1]: (2, 2.2, "dos")
row[2]: (3, 3.3, "tres")
row[3]: (4, 4.4, "quatro")
Example (Tuple)
package main

import (
	"database/sql"
	"fmt"
	"log"
)

func main() {
	db, err := sql.Open("root", "../../testdata/simple.root")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT (one, two, three) FROM tree")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	type data struct {
		i32 int32
		f32 float32
		str string
	}

	n := 0
	for rows.Next() {
		var v data
		err := rows.Scan(&v.i32, &v.f32, &v.str)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("row[%d]: (%v, %v, %q)\n", n, v.i32, v.f32, v.str)
		n++
	}

}
Output:

row[0]: (1, 1.1, "uno")
row[1]: (2, 2.2, "dos")
row[2]: (3, 3.3, "tres")
row[3]: (4, 4.4, "quatro")
Example (WhereStmt)
package main

import (
	"database/sql"
	"fmt"
	"log"
)

func main() {
	db, err := sql.Open("root", "../../testdata/simple.root")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT (one, two, three) FROM tree WHERE (one>2 && two < 5)")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	type data struct {
		i32 int32
		f32 float32
		str string
	}

	n := 0
	for rows.Next() {
		var v data
		err := rows.Scan(&v.i32, &v.f32, &v.str)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("row[%d]: (%v, %v, %q)\n", n, v.i32, v.f32, v.str)
		n++
	}

}
Output:

row[0]: (3, 3.3, "tres")
row[1]: (4, 4.4, "quatro")

func OpenDB

func OpenDB(file *riofs.File) *sql.DB

OpenDB opens a database/sql.DB from an already open ROOT file.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL