-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Oracle backend support #45
Open
stevefan1999-personal
wants to merge
16
commits into
k3s-io:master
Choose a base branch
from
stevefan1999-personal:patch-oracle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
eb469f7
add oracle backend support & explanations
stevefan1999-personal 2e46f80
do not use encrypted password
stevefan1999-personal c474809
use a template instead to generate the procedure statement
stevefan1999-personal 180d6a2
use a more generic form of SQL for listSQL
stevefan1999-personal e15d5c9
add translate limit suffix hack
stevefan1999-personal 1be56c9
support insert into values(...) returing (...) into (...)
stevefan1999-personal 9b9ad43
add the missing limit translator
stevefan1999-personal 9948e71
fix type "mismatch" for deleted field, boolean vs number (damn you or…
stevefan1999-personal 1cba66b
oracle does not use question mark for prepared statement too
stevefan1999-personal fd38adf
number in oracle db is floating point instead of integer
stevefan1999-personal a649b3a
unwrap the error from xerror instead to get true ORA Error
stevefan1999-personal 6b3e0fa
add xerror
stevefan1999-personal bf38a77
serialize the query as it's dangerous and makes context messy
stevefan1999-personal c00f486
progressively replace custom formatted log message to use WithField
stevefan1999-personal 0838cf0
set back on using by default on null to prevent insertion error with …
stevefan1999-personal bdb0f38
Merge branch 'master' into patch-oracle
stevefan1999-personal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,102 @@ | ||
package oracle | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/godror/godror" | ||
|
||
"github.com/rancher/kine/pkg/drivers/generic" | ||
"github.com/rancher/kine/pkg/logstructured" | ||
"github.com/rancher/kine/pkg/logstructured/sqllog" | ||
"github.com/rancher/kine/pkg/server" | ||
) | ||
|
||
const ( | ||
defaultHostDSN = "system@localhost" | ||
) | ||
|
||
var ( | ||
schema = []string{` | ||
CREATE TABLE kine ( | ||
id NUMBER GENERATED ALWAYS as IDENTITY, | ||
name VARCHAR(630), | ||
created INTEGER, | ||
deleted INTEGER, | ||
create_revision INTEGER, | ||
prev_revision INTEGER, | ||
lease INTEGER, | ||
value BLOB, | ||
old_value BLOB, | ||
CONSTRAINT kine_pk PRIMARY KEY (id) | ||
)`, | ||
`CREATE INDEX kine_name_index ON kine (name)`, | ||
`CREATE INDEX kine_name_id_index ON kine (name,id)`, | ||
`CREATE UNIQUE INDEX kine_name_prev_revision_uindex ON kine (name, prev_revision)`, | ||
} | ||
procedureTemplate = []string{ | ||
"declare", | ||
"begin", | ||
"%s", | ||
"exception when others then", | ||
"if SQLCODE = -955 then null; else raise; end if;", | ||
"end;", | ||
} | ||
) | ||
|
||
func New(ctx context.Context, dataSourceName string) (server.Backend, error) { | ||
parsedDSN, err := prepareDSN(dataSourceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dialect, err := generic.Open(ctx, "godror", parsedDSN, "?", false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dialect.LastInsertID = true | ||
dialect.TranslateErr = func(err error) error { | ||
// ORA-00001: unique constraint violated | ||
if err, ok := err.(*godror.OraErr); ok && err.Code() == 1 { | ||
return server.ErrKeyExists | ||
} | ||
return err | ||
} | ||
if err := setup(dialect.DB); err != nil { | ||
return nil, err | ||
} | ||
|
||
dialect.Migrate(context.Background()) | ||
return logstructured.New(sqllog.New(dialect)), nil | ||
} | ||
|
||
func setup(db *sql.DB) error { | ||
var str strings.Builder | ||
|
||
for _, cmd := range procedureTemplate { | ||
if cmd != "%s" { | ||
str.WriteString(cmd + "\n") | ||
} else { | ||
for _, stmt := range schema { | ||
str.WriteString(fmt.Sprintf("execute immediate '%s';\n", stmt)) | ||
} | ||
} | ||
} | ||
_, err := db.Exec(str.String()) | ||
return err | ||
} | ||
|
||
func prepareDSN(dataSourceName string) (string, error) { | ||
if len(dataSourceName) == 0 { | ||
dataSourceName = defaultHostDSN | ||
} | ||
config, err := godror.ParseConnString(dataSourceName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
parsedDSN := config.StringWithPassword() | ||
return parsedDSN, nil | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am only lightly versed in oracle's SQL dialect, but why is it necessary to wrap all the queries in a templated procedure wrapper? Is that the only way to keep errors from messing up the connection state or something?