MCPcopy
hub / github.com/dgraph-io/dgraph / parseTables

Function parseTables

dgraph/cmd/migrate/table_info.go:90–202  ·  view source on GitHub ↗
(pool *sql.DB, tableName string, database string)

Source from the content-addressed store, hash-verified

88}
89
90func parseTables(pool *sql.DB, tableName string, database string) (*sqlTable, error) {
91 query := fmt.Sprintf(`select COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.
92COLUMNS where TABLE_NAME = "%s" AND TABLE_SCHEMA="%s" ORDER BY COLUMN_NAME`, tableName, database)
93 columns, err := pool.Query(query)
94 if err != nil {
95 return nil, err
96 }
97 defer columns.Close()
98
99 table := &sqlTable{
100 tableName: tableName,
101 columns: make(map[string]*columnInfo),
102 columnNames: make([]string, 0),
103 isForeignKey: make(map[string]bool),
104 columnDataTypes: make([]dataType, 0),
105 predNames: make([]string, 0),
106 dstTables: make(map[string]interface{}),
107 foreignKeyConstraints: make(map[string]*fkConstraint),
108 }
109
110 for columns.Next() {
111 /*
112 each row represents info about a column, for example
113 +---------------+-----------+
114 | COLUMN_NAME | DATA_TYPE |
115 +---------------+-----------+
116 | p_company | varchar |
117 | p_employee_id | int |
118 | p_fname | varchar |
119 | p_lname | varchar |
120 | title | varchar |
121 +---------------+-----------+
122 */
123 var fieldName, dbType string
124 if err := columns.Scan(&fieldName, &dbType); err != nil {
125 return nil, errors.Wrapf(err, "unable to scan table description result for table %s",
126 tableName)
127 }
128
129 // TODO, should store the column data types into the table info as an array
130 // and the RMI should simply get the data types from the table info
131 table.columns[fieldName] = getColumnInfo(fieldName, dbType)
132 table.columnNames = append(table.columnNames, fieldName)
133 table.columnDataTypes = append(table.columnDataTypes, getDataType(dbType))
134 }
135
136 // query indices
137 indexQuery := fmt.Sprintf(`select INDEX_NAME,COLUMN_NAME from INFORMATION_SCHEMA.`+
138 `STATISTICS where TABLE_NAME = "%s" AND index_schema="%s"`, tableName, database)
139 indices, err := pool.Query(indexQuery)
140 if err != nil {
141 return nil, err
142 }
143 defer indices.Close()
144 for indices.Next() {
145 var indexName, columnName string
146 err := indices.Scan(&indexName, &columnName)
147 if err != nil {

Callers 1

runFunction · 0.85

Calls 5

getColumnInfoFunction · 0.85
getDataTypeFunction · 0.85
CloseMethod · 0.65
QueryMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected