fakeMV

package module
v0.0.0-...-54ca30a Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 3 Imported by: 0

README

This lib helps you create materialized views in your SQLite database.

How to use

import (
	"github.com/mauricedesaxe/fakeMV"
)

func main() {
    // Initialize the database
    db, err := sql.Open("sqlite3", "./test.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    /**
        Create tables and whatever else you need to on the database
    */ 

    // Initialize FakeMV
    fakeMV := &FakeMV{}
    fakeMV.Init(db) // this creates a table "mv_central_store" where we have metadata about all materialized views

    // Create a materialized view
    err = fakeMV.CreateMV(db, "user_income_events", "SELECT id, amount, category, user_id FROM events WHERE category = 'income'")
    if err != nil {
        log.Fatal(err)
    }

    /**
        Change the underlying data
    */ 

    // Refresh the materialized view
    err = fakeMV.RefreshMV(db, "user_income_events")
    if err != nil {
        log.Fatal(err)
    }
}

Parameters kinda suck

Whatever the SQL query you pass to the CreateMV function, it will be executed every time you call the RefreshMV function. So you can't quite pass parameters well unless you bake them in the string.

A simple example: you want a materialized view for the past 7 days of a certain data set. SQLite doesn't have interval syntax so you'd normally have to do something like SELECT id, amount, category, user_id FROM events date >= ?. That won't work. You could limit the amount of rows instead, because the number can be baked into the query: SELECT id, amount, category, user_id FROM events ORDER BY date DESC LIMIT 7.

Take it up with whoever makes SQLite syntax to give you interval and materialized views.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FakeMV

type FakeMV struct{}

func (*FakeMV) CreateMV

func (f *FakeMV) CreateMV(db *sql.DB, query string, mvName string) error

func (*FakeMV) Init

func (f *FakeMV) Init(db *sql.DB) error

creates a table where we store material views => query connections so the user can easily refresh the material view in the future

func (*FakeMV) RefreshMV

func (f *FakeMV) RefreshMV(db *sql.DB, mvName string) error

Jump to

Keyboard shortcuts

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