Fix insert

This commit is contained in:
Lunny Xiao 2020-04-14 15:19:41 +08:00
parent 92c475925d
commit eda3b53418
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 35 additions and 7 deletions

View File

@ -50,8 +50,9 @@ Drivers for Go's sql package which currently support database/sql includes:
* MsSql
- [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
* Oracle
- [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (experiment)
* Oracle (experiment)
- [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8)
- [github.com/godror/godror](https://github.com/godror/godror)
## Installation

View File

@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
@ -867,10 +868,30 @@ func (db *oracle) Filters() []Filter {
type godrorDriver struct {
}
func parseNoProtocol(driverName, dataSourceName string) (*URI, error) {
db := &URI{DBType: schemas.ORACLE}
dsnPattern := regexp.MustCompile(
`^(?P<user>.*)\/(?P<password>.*)@` + // user:password@
`(?P<net>.*)` + // ip:port
`\/(?P<dbname>.*)`) // dbname
matches := dsnPattern.FindStringSubmatch(dataSourceName)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "dbname":
db.DBName = match
}
}
if db.DBName == "" && len(matches) != 0 {
return nil, errors.New("dbname is empty")
}
return db, nil
}
func parseOracle(driverName, dataSourceName string) (*URI, error) {
var connStr = dataSourceName
if !strings.HasPrefix(connStr, "oracle://") {
connStr = fmt.Sprintf("oracle://%s", connStr)
return parseNoProtocol(driverName, dataSourceName)
}
u, err := url.Parse(connStr)

View File

@ -41,7 +41,9 @@ func (statement *Statement) GenInsertSQL(colNames []string, args []interface{})
return nil, err
}
if len(colNames) <= 0 {
var hasInsertColumns = len(colNames) > 0
if !hasInsertColumns && statement.dialect.URI().DBType != schemas.ORACLE {
if statement.dialect.URI().DBType == schemas.MYSQL {
if _, err := buf.WriteString(" VALUES ()"); err != nil {
return nil, err
@ -118,8 +120,10 @@ func (statement *Statement) GenInsertSQL(colNames []string, args []interface{})
// Insert tablename (id) Values(seq_tablename.nextval)
if len(table.AutoIncrement) > 0 && statement.dialect.URI().DBType == schemas.ORACLE {
if _, err := buf.WriteString(","); err != nil {
return nil, err
if hasInsertColumns {
if _, err := buf.WriteString(","); err != nil {
return nil, err
}
}
if _, err := buf.WriteString("seq_" + tableName + ".nextval"); err != nil {
return nil, err

View File

@ -360,10 +360,12 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
}
res, err := session.queryBytes(buf.String(), buf.Args()...)
if err != nil {
return 0, err
}
fmt.Println("=====", id)
defer handleAfterInsertProcessorFunc(bean)
session.cacheInsert(tableName)