Fix insert
This commit is contained in:
parent
92c475925d
commit
eda3b53418
|
@ -50,8 +50,9 @@ Drivers for Go's sql package which currently support database/sql includes:
|
||||||
* MsSql
|
* MsSql
|
||||||
- [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
- [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
||||||
|
|
||||||
* Oracle
|
* Oracle (experiment)
|
||||||
- [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (experiment)
|
- [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8)
|
||||||
|
- [github.com/godror/godror](https://github.com/godror/godror)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -867,10 +868,30 @@ func (db *oracle) Filters() []Filter {
|
||||||
type godrorDriver struct {
|
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) {
|
func parseOracle(driverName, dataSourceName string) (*URI, error) {
|
||||||
var connStr = dataSourceName
|
var connStr = dataSourceName
|
||||||
if !strings.HasPrefix(connStr, "oracle://") {
|
if !strings.HasPrefix(connStr, "oracle://") {
|
||||||
connStr = fmt.Sprintf("oracle://%s", connStr)
|
return parseNoProtocol(driverName, dataSourceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := url.Parse(connStr)
|
u, err := url.Parse(connStr)
|
||||||
|
|
|
@ -41,7 +41,9 @@ func (statement *Statement) GenInsertSQL(colNames []string, args []interface{})
|
||||||
return nil, err
|
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 statement.dialect.URI().DBType == schemas.MYSQL {
|
||||||
if _, err := buf.WriteString(" VALUES ()"); err != nil {
|
if _, err := buf.WriteString(" VALUES ()"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -118,8 +120,10 @@ func (statement *Statement) GenInsertSQL(colNames []string, args []interface{})
|
||||||
|
|
||||||
// Insert tablename (id) Values(seq_tablename.nextval)
|
// Insert tablename (id) Values(seq_tablename.nextval)
|
||||||
if len(table.AutoIncrement) > 0 && statement.dialect.URI().DBType == schemas.ORACLE {
|
if len(table.AutoIncrement) > 0 && statement.dialect.URI().DBType == schemas.ORACLE {
|
||||||
if _, err := buf.WriteString(","); err != nil {
|
if hasInsertColumns {
|
||||||
return nil, err
|
if _, err := buf.WriteString(","); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if _, err := buf.WriteString("seq_" + tableName + ".nextval"); err != nil {
|
if _, err := buf.WriteString("seq_" + tableName + ".nextval"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -360,10 +360,12 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := session.queryBytes(buf.String(), buf.Args()...)
|
res, err := session.queryBytes(buf.String(), buf.Args()...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("=====", id)
|
||||||
|
|
||||||
defer handleAfterInsertProcessorFunc(bean)
|
defer handleAfterInsertProcessorFunc(bean)
|
||||||
|
|
||||||
session.cacheInsert(tableName)
|
session.cacheInsert(tableName)
|
||||||
|
|
Loading…
Reference in New Issue