Skip to content

Commit

Permalink
add support for SQL Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Prinetti authored and James Cooper committed May 16, 2014
1 parent 108d32d commit 354af19
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 21 deletions.
107 changes: 107 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Dialect interface {
// string to append to primary key column definitions
AutoIncrStr() string

// string to bind autoincrement columns to. Empty string will
// remove reference to those columns in the INSERT statement.
AutoIncrBindValue() string

AutoIncrInsertSuffix(col *ColumnMap) string
Expand Down Expand Up @@ -388,3 +390,108 @@ func (d MySQLDialect) QuotedTableForQuery(schema string, table string) string {

return schema + "." + d.QuoteField(table)
}

///////////////////////////////////////////////////////
// Sql Server //
////////////////

// Implementation of Dialect for Microsoft SQL Server databases.
// Tested on SQL Server 2008.
// Presently, it doesn't work with CreateTablesIfNotExists().

type SqlServerDialect struct {
suffix string
}

func (m SqlServerDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string {
switch val.Kind() {
case reflect.Ptr:
return m.ToSqlType(val.Elem(), maxsize, isAutoIncr)
case reflect.Bool:
return "bit"
case reflect.Int8:
return "tinyint"
case reflect.Uint8:
return "smallint"
case reflect.Int16:
return "smallint"
case reflect.Uint16:
return "int"
case reflect.Int, reflect.Int32:
return "int"
case reflect.Uint, reflect.Uint32:
return "bigint"
case reflect.Int64:
return "bigint"
case reflect.Uint64:
return "bigint"
case reflect.Float32:
return "real"
case reflect.Float64:
return "float(53)"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "varbinary"
}
}

switch val.Name() {
case "NullInt64":
return "bigint"
case "NullFloat64":
return "float(53)"
case "NullBool":
return "tinyint"
case "Time":
return "datetime"
}

if maxsize < 1 {
maxsize = 255
}
return fmt.Sprintf("varchar(%d)", maxsize)
}

// Returns auto_increment
func (m SqlServerDialect) AutoIncrStr() string {
return "identity(0,1)"
}

// Empty string removes autoincrement columns from the INSERT statements.
func (m SqlServerDialect) AutoIncrBindValue() string {
return ""
}

func (m SqlServerDialect) AutoIncrInsertSuffix(col *ColumnMap) string {
return ""
}

// Returns suffix
func (d SqlServerDialect) CreateTableSuffix() string {

return d.suffix
}

func (m SqlServerDialect) TruncateClause() string {
return "delete from"
}

// Returns "?"
func (m SqlServerDialect) BindVar(i int) string {
return "?"
}

func (m SqlServerDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(exec, insertSql, params...)
}

func (d SqlServerDialect) QuoteField(f string) string {
return f
}

func (d SqlServerDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return table
}
return schema + "." + table
}
45 changes: 24 additions & 21 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,31 +304,34 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
first := true
for y := range t.columns {
col := t.columns[y]
if !(col.isAutoIncr && t.dbmap.Dialect.AutoIncrBindValue() == "") {
if !col.Transient {
if !first {
s.WriteString(",")
s2.WriteString(",")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))

if !col.Transient {
if !first {
s.WriteString(",")
s2.WriteString(",")
}
s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName))

if col.isAutoIncr {
s2.WriteString(t.dbmap.Dialect.AutoIncrBindValue())
plan.autoIncrIdx = y
plan.autoIncrFieldName = col.fieldName
} else {
s2.WriteString(t.dbmap.Dialect.BindVar(x))
if col == t.version {
plan.versField = col.fieldName
plan.argFields = append(plan.argFields, versFieldConst)
if col.isAutoIncr {
s2.WriteString(t.dbmap.Dialect.AutoIncrBindValue())
plan.autoIncrIdx = y
plan.autoIncrFieldName = col.fieldName
} else {
plan.argFields = append(plan.argFields, col.fieldName)
s2.WriteString(t.dbmap.Dialect.BindVar(x))
if col == t.version {
plan.versField = col.fieldName
plan.argFields = append(plan.argFields, versFieldConst)
} else {
plan.argFields = append(plan.argFields, col.fieldName)
}

x++
}

x++
first = false
}

first = false
} else {
plan.autoIncrIdx = y
plan.autoIncrFieldName = col.fieldName
}
}
s.WriteString(") values (")
Expand Down

0 comments on commit 354af19

Please sign in to comment.