-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: [#358] Add facades.DB, to provider an original sql flow * optimize * update go.mod * address comments
- Loading branch information
Showing
26 changed files
with
667 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
package database | ||
|
||
type Config struct { | ||
Connection string | ||
Database string | ||
Driver string | ||
Host string | ||
Password string | ||
Port int | ||
Prefix string | ||
Schema string | ||
Username string | ||
Version string | ||
Connection string | ||
DSN string | ||
Database string | ||
Driver string | ||
Host string | ||
Password string | ||
Port int | ||
Prefix string | ||
Schema string | ||
Username string | ||
Version string | ||
PlaceholderFormat PlaceholderFormat | ||
} | ||
|
||
type PlaceholderFormat interface { | ||
ReplacePlaceholders(sql string) (string, error) | ||
} |
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,10 @@ | ||
package db | ||
|
||
type DB interface { | ||
Table(name string) Query | ||
} | ||
|
||
type Query interface { | ||
Where(query any, args ...any) Query | ||
Get(dest any) error | ||
} |
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,12 @@ | ||
package db | ||
|
||
type Conditions struct { | ||
table string | ||
where []Where | ||
} | ||
|
||
type Where struct { | ||
query any | ||
args []any | ||
// or bool | ||
} |
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,45 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jmoiron/sqlx" | ||
|
||
"github.com/goravel/framework/contracts/config" | ||
"github.com/goravel/framework/contracts/database" | ||
"github.com/goravel/framework/contracts/database/db" | ||
contractsdriver "github.com/goravel/framework/contracts/database/driver" | ||
"github.com/goravel/framework/errors" | ||
) | ||
|
||
type DB struct { | ||
config database.Config | ||
instance *sqlx.DB | ||
} | ||
|
||
func NewDB(config database.Config, instance *sqlx.DB) db.DB { | ||
return &DB{config: config, instance: instance} | ||
} | ||
|
||
func BuildDB(config config.Config, connection string) (db.DB, error) { | ||
driverCallback, exist := config.Get(fmt.Sprintf("database.connections.%s.via", connection)).(func() (contractsdriver.Driver, error)) | ||
if !exist { | ||
return nil, errors.DatabaseConfigNotFound | ||
} | ||
|
||
driver, err := driverCallback() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
instance, err := driver.DB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewDB(driver.Config(), sqlx.NewDb(instance, driver.Config().Driver)), nil | ||
} | ||
|
||
func (r *DB) Table(name string) db.Query { | ||
return NewQuery(r.config, r.instance, name) | ||
} |
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,67 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/jmoiron/sqlx" | ||
|
||
"github.com/goravel/framework/contracts/database" | ||
"github.com/goravel/framework/contracts/database/db" | ||
"github.com/goravel/framework/errors" | ||
) | ||
|
||
type Query struct { | ||
conditions Conditions | ||
config database.Config | ||
instance *sqlx.DB | ||
} | ||
|
||
func NewQuery(config database.Config, instance *sqlx.DB, table string) *Query { | ||
return &Query{ | ||
conditions: Conditions{ | ||
table: table, | ||
}, | ||
config: config, | ||
instance: instance, | ||
} | ||
} | ||
|
||
func (r *Query) Where(query any, args ...any) db.Query { | ||
r.conditions.where = append(r.conditions.where, Where{ | ||
query: query, | ||
args: args, | ||
}) | ||
|
||
return r | ||
} | ||
|
||
func (r *Query) Get(dest any) error { | ||
sql, args, err := r.buildSelect() | ||
// TODO: use logger instead of println | ||
fmt.Println(sql, args, err) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return r.instance.Select(dest, sql, args...) | ||
} | ||
|
||
func (r *Query) buildSelect() (sql string, args []any, err error) { | ||
if r.conditions.table == "" { | ||
return "", nil, errors.DatabaseTableIsRequired | ||
} | ||
|
||
builder := sq.Select("*") | ||
if r.config.PlaceholderFormat != nil { | ||
builder = builder.PlaceholderFormat(r.config.PlaceholderFormat) | ||
} | ||
|
||
builder = builder.From(r.conditions.table) | ||
|
||
for _, where := range r.conditions.where { | ||
builder = builder.Where(where.query, where.args...) | ||
} | ||
|
||
return builder.ToSql() | ||
} |
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
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
Oops, something went wrong.