MCPcopy Index your code
hub / github.com/bytebase/bytebase / getTables

Function getTables

backend/plugin/db/sqlite/sync.go:100–166  ·  view source on GitHub ↗

getTables gets all tables of a database.

(txn *sql.Tx)

Source from the content-addressed store, hash-verified

98
99// getTables gets all tables of a database.
100func getTables(txn *sql.Tx) ([]*storepb.TableMetadata, error) {
101 indexMap, err := getIndices(txn)
102 if err != nil {
103 return nil, errors.Wrapf(err, "failed to get indices")
104 }
105
106 var tableNames []string
107 query := `
108 SELECT
109 name
110 FROM sqlite_schema
111 WHERE type ='table' AND name NOT LIKE 'sqlite_%'
112 ORDER BY name;`
113 rows, err := txn.Query(query)
114 if err != nil {
115 return nil, err
116 }
117 defer rows.Close()
118 for rows.Next() {
119 var name string
120 if err := rows.Scan(&name); err != nil {
121 return nil, err
122 }
123 tableNames = append(tableNames, name)
124 }
125 if err := rows.Err(); err != nil {
126 return nil, util.FormatErrorWithQuery(err, query)
127 }
128
129 var tables []*storepb.TableMetadata
130 for _, name := range tableNames {
131 table := &storepb.TableMetadata{
132 Name: name,
133 }
134 if err := func() error {
135 // Get columns: cid, name, type, notnull, dflt_value, pk.
136 query := fmt.Sprintf("pragma table_info(%s);", name)
137 rows, err := txn.Query(query)
138 if err != nil {
139 return err
140 }
141 defer rows.Close()
142 for rows.Next() {
143 column := &storepb.ColumnMetadata{}
144 var notNull, unusedPk bool
145 var defaultStr sql.NullString
146 if err := rows.Scan(&column.Position, &column.Name, &column.Type, &notNull, &defaultStr, &unusedPk); err != nil {
147 return err
148 }
149 column.Nullable = !notNull
150 if defaultStr.Valid {
151 // TODO: use correct default type
152 column.Default = defaultStr.String
153 }
154
155 table.Columns = append(table.Columns, column)
156 table.Indexes = indexMap[table.Name]
157 }

Callers 1

SyncDBSchemaMethod · 0.70

Calls 6

FormatErrorWithQueryFunction · 0.92
getIndicesFunction · 0.85
ScanMethod · 0.80
QueryMethod · 0.65
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected