MCPcopy Create free account
hub / github.com/araddon/qlbridge / Setup

Method Setup

datasource/sqlite/source.go:62–113  ·  view source on GitHub ↗

Setup this source with schema from parent.

(s *schema.Schema)

Source from the content-addressed store, hash-verified

60
61// Setup this source with schema from parent.
62func (m *Source) Setup(s *schema.Schema) error {
63 m.mu.Lock()
64 defer m.mu.Unlock()
65
66 u.Debugf("got new sqlite schema %s", s.Name)
67 m.schema = s
68 if m.db != nil {
69 return nil
70 }
71
72 m.file = s.Conf.Settings.String("file")
73 if m.file == "" {
74 m.file = fmt.Sprintf("/tmp/%s.sql.db", s.Name)
75 u.Warnf("using tmp? %q", m.file)
76 }
77
78 // It will be created if it doesn't exist.
79 // "./source.enriched.db"
80 db, err := sql.Open("sqlite3", m.file)
81 if err != nil {
82 u.Errorf("could not open %q err=%v", m.file, err)
83 return err
84 }
85 err = db.Ping()
86 if err != nil {
87 u.Errorf("could not ping %q err=%v", m.file, err)
88 return err
89 }
90 m.db = db
91
92 // SELECT * FROM dbname.sqlite_master WHERE type='table';
93 rows, err := db.Query("SELECT tbl_name, sql FROM sqlite_master WHERE type='table';")
94 if err != nil {
95 u.Errorf("could not open master err=%v", err)
96 return err
97 }
98 var name, sql string
99 for rows.Next() {
100 rows.Scan(&name, &sql)
101 name = strings.ToLower(name)
102 t := tableFromSQL(name, sql)
103 m.tables[name] = t
104 m.tableList = append(m.tableList, name)
105 }
106 rows.Close()
107
108 // if err := datasource.IntrospectTable(m.tbl, m.CreateIterator()); err != nil {
109 // u.Errorf("Could not introspect schema %v", err)
110 // }
111
112 return nil
113}
114
115// Init the source
116func (m *Source) Init() {}

Callers

nothing calls this directly

Calls 8

tableFromSQLFunction · 0.85
ErrorfMethod · 0.80
StringMethod · 0.65
OpenMethod · 0.65
NextMethod · 0.65
CloseMethod · 0.65
QueryMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected