/
ougi
/
FromPostgresToClick
Обзор
Документация
Войти
/
ougi
/
FromPostgresToClick
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
pkg/jsonToSql/jsontosql.go
143 строки
3 KB
Denis V Zhilin
Маломальски готовый SqlBuilder + readme
18 окт 2024, 13:19
18 окт 2024, 13:19
5787df2
Код
Авторство
О чём код?
package jsontosql import ( "FromSqlToClick/pkg/json" ) type Sql struct { ShortNameTables map[string]string ShortNameColumns map[string]string Select string Join string Where string // limit int пока придержим } // func (sql *Sql) SetLimit(limit int) { // sql.limit = limit // } func (sql *Sql) SetShortNameColumns(shemaSql *json.SqlShema) (columns []string) { tables := shemaSql.Tables sql.ShortNameColumns = make(map[string]string) for _, table := range tables { for _, column := range table.Columns { if column.Short_name != "" { sql.ShortNameColumns[column.Name] = column.Short_name } columns = append(columns, column.Name) } } return columns } func (sql *Sql) SetShortNameTables(shemaSql *json.SqlShema) { tables := shemaSql.Tables sql.ShortNameTables = make(map[string]string) for _, table := range tables { if table.Short_name != "" { sql.ShortNameTables[table.Name] = table.Short_name } } } func (sql *Sql) CreateSelect(columns []string) { selectSql := "SELECT\n" for _, column := range columns { selectSql += "\t" + column if shortName, ok := sql.ShortNameColumns[column]; ok { selectSql += " AS " + shortName } selectSql += "\n" } sql.Select = selectSql } func (sql *Sql) getName(table, column string) string { name := "" if table != "" { if shortNameTable, ok := sql.ShortNameTables[table]; ok { name += shortNameTable } else { name += table } name += "." } if shortNameColumn, ok := sql.ShortNameColumns[column]; ok { name += shortNameColumn } else { name += column } return name } func (sql *Sql) CreateJoin(shemaSql *json.SqlShema) { joins := shemaSql.Joins joinSql := "" for _, join := range joins { if join.Join_type == "main" { joinSql += "FROM " + join.Left_name if shortName, ok := sql.ShortNameTables[join.Left_name]; ok { joinSql += " AS " + shortName } } else { joinSql += join.Join_type + " JOIN " + join.Right_name rightShortName, ok := sql.ShortNameTables[join.Right_name] if ok { joinSql += " AS " + rightShortName + " ON " + rightShortName + "." + join.Right_column + "=" } else { joinSql += " ON " + join.Right_name + "." + join.Right_column + "=" } leftShortName, ok := sql.ShortNameTables[join.Left_name] // joinSql += if ok { joinSql += leftShortName } else { joinSql += join.Left_name } joinSql += "." + join.Left_column } joinSql += "\n" } sql.Join = joinSql } func (sql *Sql) CreateWhere(shemaSQL *json.SqlShema) { wheres := shemaSQL.Where whereSQL := "WHERE " if len(wheres) == 0 { return } else { flagHead := 0 for _, where := range wheres { if flagHead == 0 { flagHead += 1 } else { whereSQL += "AND" } name := sql.getName(where.NameColumn, where.Name) whereSQL += name + " " + where.Condition + " " + where.Value if len(whereSQL) > 40 { whereSQL += "\n\t" } } } sql.Where = whereSQL } func CreateSQL(shemaSQL *json.SqlShema) string { var sql Sql // sql.SetLimit(1000) columns := sql.SetShortNameColumns(shemaSQL) sql.SetShortNameTables(shemaSQL) sql.CreateSelect(columns) sql.CreateJoin(shemaSQL) sql.CreateWhere(shemaSQL) requests := sql.Select + sql.Join + sql.Where return requests }