// Constraintes from selected catalog (or all, if empty), matching schemas and names
Constraintes from selected catalog (or all, if empty), matching schemas and names
// Constraintes from selected catalog ( or all if empty ) matching schemas and names
func (s InformationSchema) Constraints(f metadata.Filter) (*metadata.ConstraintSet, error) {
	if !s.hasConstraints {
		return nil, metadata.ErrNotSupported
	}
	columns := []string{
		"t.constraint_catalog",
		"t.table_schema",
		"t.table_name",
		"t.constraint_name",
		"t.constraint_type",
		s.clauses[ConstraintIsDeferrable],
		s.clauses[ConstraintInitiallyDeferred],
		"COALESCE(r.unique_constraint_catalog, '') AS foreign_catalog",
		"COALESCE(r.unique_constraint_schema, '') AS foreign_schema",
		"COALESCE(f.table_name, '') AS foreign_table",
		"COALESCE(r.unique_constraint_name, '') AS foreign_constraint",
		"COALESCE(r.match_option, '') AS match_options",
		"COALESCE(r.update_rule, '') AS update_rule",
		"COALESCE(r.delete_rule, '') AS delete_rule",
		"COALESCE(c.check_clause, '') AS check_clause",
	}
	qstr := "SELECT\n  " + strings.Join(columns, ",\n  ") + `
FROM information_schema.table_constraints t
LEFT JOIN information_schema.referential_constraints r ON t.constraint_catalog = r.constraint_catalog
  AND t.constraint_schema = r.constraint_schema
  AND t.constraint_name = r.constraint_name
  AND t.constraint_type = 'FOREIGN KEY'
LEFT JOIN information_schema.table_constraints f ON r.unique_constraint_catalog = f.constraint_catalog
  AND r.unique_constraint_schema = f.constraint_schema
  AND r.unique_constraint_name = f.constraint_name
  ` + s.clauses[ConstraintJoinCond] + `
LEFT JOIN information_schema.check_constraints c ON t.constraint_catalog = c.constraint_catalog
  AND t.constraint_schema = c.constraint_schema
  AND t.constraint_name = c.constraint_name
`
	conds, vals := s.conditions(1, f, formats{
		catalog:    "t.constraint_catalog LIKE %s",
		schema:     "t.table_schema LIKE %s",
		notSchemas: "t.table_schema NOT IN (%s)",
		parent:     "t.table_name LIKE %s",
		reference:  "f.table_name LIKE %s",
		name:       "t.constraint_name LIKE %s",
	})
	if len(conds) != 0 {
		qstr += " WHERE " + strings.Join(conds, " AND ")
	}
	rows, closeRows, err := s.query(qstr, []string{}, "t.constraint_catalog, t.table_schema, t.table_name, t.constraint_name", vals...)
	if err != nil {
		if err == sql.ErrNoRows {
			return metadata.NewConstraintSet([]metadata.Constraint{}), nil
		}
		return nil, err
	}
	defer closeRows()
	results := []metadata.Constraint{}
	for rows.Next() {
		rec := metadata.Constraint{}
		err = rows.Scan(
			&rec.Catalog,
			&rec.Schema,
			&rec.Table,
			&rec.Name,
			&rec.Type,
			&rec.IsDeferrable,
			&rec.IsInitiallyDeferred,
			&rec.ForeignCatalog,
			&rec.ForeignSchema,
			&rec.ForeignTable,
			&rec.ForeignName,
			&rec.MatchType,
			&rec.UpdateRule,
			&rec.DeleteRule,
			&rec.CheckClause,
		)
		if err != nil {
			return nil, err
		}
		results = append(results, rec)
	}
	if rows.Err() != nil {
		return nil, rows.Err()
	}
	return metadata.NewConstraintSet(results), nil
}
func ( s InformationSchema ) Constraints ( f metadata . Filter ) ( * metadata . ConstraintSet , error ) { if ! s . hasConstraints { return nil , metadata . ErrNotSupported } columns := [ ] string { " " , " " , " " , " " , " " , s . clauses [ ConstraintIsDeferrable ] , s . clauses [ ConstraintInitiallyDeferred ] , " " , " " , " " , " " , " " , " " , " " , " " , } qstr := " \n " + strings . Join ( columns , " \n " ) + `
FROM information_schema.table_constraints t
LEFT JOIN information_schema.referential_constraints r ON t.constraint_catalog = r.constraint_catalog
  AND t.constraint_schema = r.constraint_schema
  AND t.constraint_name = r.constraint_name
  AND t.constraint_type = 'FOREIGN KEY'
LEFT JOIN information_schema.table_constraints f ON r.unique_constraint_catalog = f.constraint_catalog
  AND r.unique_constraint_schema = f.constraint_schema
  AND r.unique_constraint_name = f.constraint_name
  ` + s . clauses [ ConstraintJoinCond ] + `
LEFT JOIN information_schema.check_constraints c ON t.constraint_catalog = c.constraint_catalog
  AND t.constraint_schema = c.constraint_schema
  AND t.constraint_name = c.constraint_name
` conds , vals := s . conditions ( 1 , f , formats { catalog : " " , schema : " " , notSchemas : " " , parent : " " , reference : " " , name : " " , } ) if len ( conds ) != 0 { qstr += " " + strings . Join ( conds , " " ) } rows , closeRows , err := s . query ( qstr , [ ] string { } , " " , vals ... ) if err != nil { if err == sql . ErrNoRows { return metadata . NewConstraintSet ( [ ] metadata . Constraint { } ) , nil } return nil , err } defer closeRows ( ) results := [ ] metadata . Constraint { } for rows . Next ( ) { rec := metadata . Constraint { } err = rows . Scan ( & rec . Catalog , & rec . Schema , & rec . Table , & rec . Name , & rec . Type , & rec . IsDeferrable , & rec . IsInitiallyDeferred , & rec . ForeignCatalog , & rec . ForeignSchema , & rec . ForeignTable , & rec . ForeignName , & rec . MatchType , & rec . UpdateRule , & rec . DeleteRule , & rec . CheckClause , ) if err != nil { return nil , err } results = append ( results , rec ) } if rows . Err ( ) != nil { return nil , rows . Err ( ) } return metadata . NewConstraintSet ( results ) , nil }
Constraintes from selected catalog (or all, if empty), matching schemas and names
Constraintes from selected catalog ( or all if empty ) matching schemas and names