Skip to content

Commit f8c5f67

Browse files
authored
Merge pull request #75 from hymkor/push-vrtuwykuwyox
Support `DESC {OWNER}.{TABLE}` in MS SQL Server
2 parents c07dd42 + d1fc0bf commit f8c5f67

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Changelog (English)
1010
- Improved datetime handling in SELECT and EDIT operations. Columns without timezone information no longer display redundant timezone offsets, and fields without certain precision (such as seconds) no longer show unnecessary components. Datetime comparison and update matching are now performed using string representations appropriate for each column type. (#55, #57, #58, #65, #66, #69)
1111
- Improved readability of datetime values shown in bind variable output during edit operations. (#60)
1212
- Added support for Oracle TIMESTAMP WITH TIME ZONE / TIMESTAMP WITH LOCAL TIME ZONE and SQL Server DATETIMEOFFSET columns in edit mode. (#63, #66)
13-
- In MySQL, PostgreSQL and Oracle, `desc` supports `desc {schema}.{table}` syntax now (#68, #73, #74)
13+
- In MySQL, PostgreSQL, Oracle and MS SQL Server, `desc` supports `desc {schema}.{table}` syntax now (#68, #73, #74, #75)
1414
- Since the `edit` command now handles date/time values primarily as strings, SQL-Bless no longer needs to force MySQL drivers to return `time.Time` values. As a result, SQL-Bless no longer appends `&parseTime=true&loc=Local` to MySQL DSNs. (#71)
1515
- PostgreSQL: Improved table selection for the edit command. All accessible tables are now available as candidates, including those in `pg_catalog` and `information_schema`. System tables are listed after user tables, and schema-qualified names (schema.table) are displayed to avoid ambiguity. (#72)
1616
- Improve output of `desc` on PostgreSQL (#73)

CHANGELOG_ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Changelog (Japanese)
1010
- SELECT / EDIT における日時表示を改善。タイムゾーン情報を持たないカラムでは不要なタイムゾーン表示を行わないようにし、精度を持たない項目(例: 秒)も表示しないようにした。日時比較や更新時の照合も、各カラム型に応じた文字列形式で行うよう改善した。(#55, #57, #58, #65, #66, #69)
1111
- edit 文のバインド変数出力における日時表示の読み易さを改善した。 (#60)
1212
- edit 文で Oracle TIMESTAMP WITH TIME ZONE / TIMESTAMP WITH LOCAL TIME ZONE および SQL Server DATETIMEOFFSET 型をサポートした。 (#63, #66)
13-
- MySQL, PostgreSQL, Oracle の desc 文で `desc {スキーマ名}.{テーブル名}` をサポート (#68, #73, #74)
13+
- MySQL, PostgreSQL, Oracle, MS SQL Server の desc 文で `desc {スキーマ名}.{テーブル名}` をサポート (#68, #73, #74, #75)
1414
- `edit` 文で日時をあくまで文字列ベースで扱うようにした結果、time.Time の使用を MySQL ドライバーパッケージに指定する必要がなくなったため、`&parseTime=True&loc=Local` を DSN 文字列に追記するのをやめた (#71)
1515
- PostgreSQL で edit コマンドでのテーブル選択を改善した。`pg_catalog``information_schema` も含め、アクセスできる全テーブルを候補にできるようにした。システムテーブルはユーザテーブルの後でリストするようにし、あいまいにならないようにスキーマ修飾子(schema.table)も表示するようにした (#72)
1616
- PostgreSQL の desc コマンドの出力を改善 (#73)

dialect/sqlserver/columns.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
select c.column_id as "ID",
2+
c.name as "NAME",
3+
case c.is_nullable
4+
when 1 then 'NULL'
5+
else 'NOT NULL'
6+
end as "NULL?",
7+
case
8+
when t.name in ('char','varchar','nchar','nvarchar',
9+
'binary','varbinary')
10+
and c.max_length > 0
11+
then t.name + '(' +
12+
convert(varchar,
13+
case
14+
when t.name in ('nchar','nvarchar')
15+
then c.max_length/2
16+
else c.max_length
17+
end
18+
) + ')'
19+
else t.name
20+
end as "TYPE"
21+
from sys.columns c,
22+
sys.objects o,
23+
sys.types t,
24+
sys.schemas s
25+
where c.object_id = o.object_id
26+
and o.name = case
27+
when @p1 like '%.%' then parsename(@p1,1)
28+
else @p1
29+
end
30+
and o.schema_id = s.schema_id
31+
and s.name = case
32+
when @p1 like '%.%' then parsename(@p1,2)
33+
else schema_name()
34+
end
35+
and c.user_type_id = t.user_type_id
36+
order by c.column_id

dialect/sqlserver/main.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqlserver
22

33
import (
4+
_ "embed"
45
"time"
56

67
_ "github.com/microsoft/go-mssqldb"
@@ -10,6 +11,9 @@ import (
1011
"github.com/hymkor/sqlbless/dialect"
1112
)
1213

14+
//go:embed columns.sql
15+
var columnSql string
16+
1317
var typeSpec = map[string][2]string{
1418
"DATE": {
1519
"CAST(@v%d AS DATE)",
@@ -61,27 +65,8 @@ func formatValue(typeName string, value any) (string, bool) {
6165
}
6266

6367
var sqlServerSpec = &dialect.Entry{
64-
Usage: "sqlbless sqlserver://@<HOSTNAME>?database=<DBNAME>",
65-
SQLForColumns: `
66-
select c.column_id as "ID",
67-
c.name as "NAME",
68-
case
69-
when c.max_length > 0 then
70-
t.name + '(' + convert(varchar,c.max_length) + ')'
71-
else
72-
t.name
73-
end as "TYPE",
74-
case c.is_nullable
75-
when 1 then 'NULL'
76-
else 'NOT NULL'
77-
end as "NULL?"
78-
from sys.columns c,
79-
sys.objects o,
80-
sys.types t
81-
where c.object_id = o.object_id
82-
and o.name = @p1
83-
and c.user_type_id = t.user_type_id
84-
order by c.column_id`,
68+
Usage: "sqlbless sqlserver://@<HOSTNAME>?database=<DBNAME>",
69+
SQLForColumns: columnSql,
8570
SQLForTables: `select * from sys.tables`,
8671
TypeConverterFor: typeNameToConv,
8772
PlaceHolder: &dialect.PlaceHolderName{Mark: "@", Prefix: "v"},

0 commit comments

Comments
 (0)