-
-
Save des1roer/8cd9dc7c864b3b71dbf4219dcb7a2316 to your computer and use it in GitHub Desktop.
array_agg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"log" | |
"os" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func main() { | |
// Подключение к MySQL | |
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/information_schema") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
// Запрос, который возвращает готовый JSON | |
query := ` | |
SELECT | |
JSON_ARRAYAGG( | |
JSON_OBJECT( | |
'table_schema', TABLE_SCHEMA, | |
'table_name', TABLE_NAME, | |
'column_name', COLUMN_NAME, | |
'data_type', DATA_TYPE, | |
'column_type', COLUMN_TYPE, | |
'is_nullable', IS_NULLABLE, | |
'column_key', COLUMN_KEY, | |
'column_default', COLUMN_DEFAULT, | |
'extra', EXTRA, | |
'column_comment', COLUMN_COMMENT, | |
'ordinal_position', ORDINAL_POSITION | |
) | |
) AS json_result | |
FROM COLUMNS | |
WHERE TABLE_SCHEMA = 'your_database_name' | |
` | |
var jsonData string | |
// Выполняем запрос и сканируем результат в строку | |
err = db.QueryRow(query).Scan(&jsonData) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Сохраняем JSON в файл | |
err = os.WriteFile("columns_info.json", []byte(jsonData), 0644) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println("Данные сохранены в columns_info.json") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment