-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support Oracle dialect * feat: update README.md
- Loading branch information
Showing
7 changed files
with
161 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package oracledialect | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/hex" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect" | ||
"github.com/uptrace/bun/dialect/feature" | ||
"github.com/uptrace/bun/dialect/sqltype" | ||
"github.com/uptrace/bun/schema" | ||
) | ||
|
||
func init() { | ||
if Version() != bun.Version() { | ||
panic(fmt.Errorf("oracledialect and Bun must have the same version: v%s != v%s", | ||
Version(), bun.Version())) | ||
} | ||
} | ||
|
||
type Dialect struct { | ||
schema.BaseDialect | ||
|
||
tables *schema.Tables | ||
features feature.Feature | ||
} | ||
|
||
func New() *Dialect { | ||
d := new(Dialect) | ||
d.tables = schema.NewTables(d) | ||
d.features = feature.CTE | | ||
feature.WithValues | | ||
feature.Returning | | ||
//feature.InsertReturning | // TODO | ||
//feature.Output | // TODO | ||
feature.InsertOnConflict | | ||
//feature.TableNotExists | | ||
feature.SelectExists | | ||
feature.AutoIncrement | | ||
feature.CompositeIn | ||
return d | ||
} | ||
|
||
func (d *Dialect) Init(*sql.DB) {} | ||
|
||
func (d *Dialect) Name() dialect.Name { | ||
return dialect.Oracle | ||
} | ||
|
||
func (d *Dialect) Features() feature.Feature { | ||
return d.features | ||
} | ||
|
||
func (d *Dialect) Tables() *schema.Tables { | ||
return d.tables | ||
} | ||
|
||
func (d *Dialect) OnTable(table *schema.Table) { | ||
for _, field := range table.FieldMap { | ||
d.onField(field) | ||
} | ||
} | ||
|
||
func (d *Dialect) onField(field *schema.Field) { | ||
field.DiscoveredSQLType = fieldSQLType(field) | ||
} | ||
|
||
func (d *Dialect) IdentQuote() byte { | ||
return '"' | ||
} | ||
|
||
func (*Dialect) AppendBytes(b, bs []byte) []byte { | ||
if bs == nil { | ||
return dialect.AppendNull(b) | ||
} | ||
|
||
b = append(b, "0x"...) | ||
|
||
s := len(b) | ||
b = append(b, make([]byte, hex.EncodedLen(len(bs)))...) | ||
hex.Encode(b[s:], bs) | ||
|
||
return b | ||
} | ||
|
||
func (d *Dialect) DefaultVarcharLen() int { | ||
return 255 | ||
} | ||
|
||
func (d *Dialect) AppendSequence(b []byte, table *schema.Table, field *schema.Field) []byte { | ||
return append(b, " GENERATED BY DEFAULT AS IDENTITY"...) | ||
} | ||
|
||
func fieldSQLType(field *schema.Field) string { | ||
switch field.DiscoveredSQLType { | ||
case sqltype.SmallInt, sqltype.BigInt: | ||
// INTEGER PRIMARY KEY is an alias for the ROWID. | ||
// It is safe to convert all ints to INTEGER, because SQLite types don't have size. | ||
return sqltype.Integer | ||
case sqltype.Boolean: | ||
return "number(1,0)" | ||
default: | ||
return field.DiscoveredSQLType | ||
} | ||
} | ||
|
||
func (*Dialect) AppendTime(b []byte, tm time.Time) []byte { | ||
if tm.IsZero() { | ||
b = append(b, "NULL"...) | ||
return b | ||
} | ||
b = append(b, "TO_TIMESTAMP('"...) | ||
b = tm.AppendFormat(b, "2006-01-02 15:04:05.999999") | ||
b = append(b, "', 'YYYY-MM-DD HH24:MI:SS.FF')"...) | ||
return b | ||
} | ||
|
||
func (*Dialect) AppendBool(b []byte, v bool) []byte { | ||
if v { | ||
return append(b, '1') | ||
} | ||
|
||
return append(b, '0') | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package oracledialect | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/uptrace/bun/schema" | ||
) | ||
|
||
func scanner(typ reflect.Type) schema.ScannerFunc { | ||
return schema.Scanner(typ) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package oracledialect | ||
|
||
// Version is the current release version. | ||
func Version() string { | ||
return "1.2.1" | ||
} |
This file contains 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
This file contains 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