Пример #1
0
// getMysqlSchema returns a mysql schema struct pointer
func (db *DB) getMysqlSchema() (*models.Schema, error) {
	schema := models.NewSchema(MySql)
	tableNames := TableNames{}
	err := db.DB.Select(&tableNames, "show tables;")
	if err != nil {
		return nil, err
	}
	for _, name := range tableNames {
		table := &models.Table{}
		rows, err := db.DB.Queryx(fmt.Sprintf("describe %s;", name))
		if err != nil {
			return nil, err
		}
		for rows.Next() {
			tableRow := &models.TableRow{}
			err = rows.StructScan(tableRow)
			if err != nil {
				return nil, err
			}
			table.Rows = append(table.Rows, tableRow)
		}
		table.Name = name
		schema.Tables = append(schema.Tables, table)
	}
	return schema, nil
}
Пример #2
0
// getSqliteSchema returns a schema from sqlite
func (db *DB) getSqliteSchema() (*models.Schema, error) {
	schema := models.NewSchema(SQLite)
	tableNames := TableNames{}
	err := db.DB.Select(&tableNames, "SELECT name FROM sqlite_master WHERE type='table';")
	if err != nil {
		return nil, err
	}
	for _, name := range tableNames {
		table := &models.Table{}
		rows, err := db.DB.Unsafe().Queryx(fmt.Sprintf("PRAGMA table_info(%s);", name))

		if err != nil {
			return nil, err
		}
		for rows.Next() {
			tableRow := &models.SQLiteTableRow{}
			err = rows.StructScan(tableRow)
			if err != nil {
				return nil, err
			}
			table.Rows = append(table.Rows, &models.TableRow{Field: tableRow.Field, Type: tableRow.Type})
		}
		table.Name = name
		schema.Tables = append(schema.Tables, table)
	}
	return schema, nil
}
Пример #3
0
// getPostgresSchema returns a postgres schema struct
func (db *DB) getPostgresSchema() (*models.Schema, error) {
	schema := models.NewSchema(Postgres)
	tableNames := TableNames{}
	err := db.DB.Select(&tableNames, `SELECT
	c.relname as "Name"
	FROM pg_catalog.pg_class c
	LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
	WHERE c.relkind IN ('r','')
	AND n.nspname <> 'pg_catalog'
	AND n.nspname <> 'information_schema'
	AND n.nspname !~ '^pg_toast'
	AND pg_catalog.pg_table_is_visible(c.oid);`)
	if err != nil {
		return nil, err
	}
	for _, name := range tableNames {
		table := &models.Table{}
		rows, err := db.DB.Queryx(fmt.Sprintf(`SELECT a.attname as "Field", format_type(a.atttypid, a.atttypmod) AS "Type"
FROM pg_attribute a
JOIN pg_class b ON (a.attrelid = b.relfilenode)
WHERE b.relname = '%s' and a.attstattarget = -1;`, name))

		if err != nil {
			return nil, err
		}
		for rows.Next() {
			tableRow := &models.TableRow{}
			err = rows.StructScan(tableRow)
			if err != nil {
				return nil, err
			}
			table.Rows = append(table.Rows, tableRow)
		}
		table.Name = name
		schema.Tables = append(schema.Tables, table)
	}
	return schema, nil
}