mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/schema: universl id allocation support
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/9 Reviewed By: alexsn Differential Revision: D16252229 fbshipit-source-id: 795b6556d322e5c1ff5fb826c3b06ba5421ac857
This commit is contained in:
committed by
Facebook Github Bot
parent
ad051e6d72
commit
b5cdb810b8
@@ -72,7 +72,7 @@ func (b *Boring) Unwrap() *Boring {
|
||||
func (b *Boring) String() string {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.WriteString("Boring(")
|
||||
buf.WriteString(fmt.Sprintf("id=%v,", b.ID))
|
||||
buf.WriteString(fmt.Sprintf("id=%v", b.ID))
|
||||
buf.WriteString(")")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"fbc/ent/entc/integration/plugin/ent/boring"
|
||||
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
type BoringQuery struct {
|
||||
config
|
||||
limit *int
|
||||
offset *int
|
||||
order []Order
|
||||
unique []string
|
||||
predicates []ent.Predicate
|
||||
@@ -43,6 +45,12 @@ func (bq *BoringQuery) Limit(limit int) *BoringQuery {
|
||||
return bq
|
||||
}
|
||||
|
||||
// Offset adds an offset step to the query.
|
||||
func (bq *BoringQuery) Offset(offset int) *BoringQuery {
|
||||
bq.offset = &offset
|
||||
return bq
|
||||
}
|
||||
|
||||
// Order adds an order step to the query.
|
||||
func (bq *BoringQuery) Order(o ...Order) *BoringQuery {
|
||||
bq.order = append(bq.order, o...)
|
||||
@@ -330,6 +338,11 @@ func (bq *BoringQuery) sqlQuery() *sql.Selector {
|
||||
for _, p := range bq.order {
|
||||
p.SQL(selector)
|
||||
}
|
||||
if offset := bq.offset; offset != nil {
|
||||
// limit is mandatory for offset clause. We start
|
||||
// with default value, and override it below if needed.
|
||||
selector.Offset(*offset).Limit(math.MaxInt64)
|
||||
}
|
||||
if limit := bq.limit; limit != nil {
|
||||
selector.Limit(*limit)
|
||||
}
|
||||
@@ -399,7 +412,12 @@ func (bq *BoringQuery) gremlinQuery() *dsl.Traversal {
|
||||
p.Gremlin(v)
|
||||
}
|
||||
}
|
||||
if limit := bq.limit; limit != nil {
|
||||
switch limit, offset := bq.limit, bq.offset; {
|
||||
case limit != nil && offset != nil:
|
||||
v.Range(*offset, *offset+*limit)
|
||||
case offset != nil:
|
||||
v.Range(*offset, math.MaxInt64)
|
||||
case limit != nil:
|
||||
v.Limit(*limit)
|
||||
}
|
||||
if unique := bq.unique; len(unique) == 0 {
|
||||
|
||||
20
entc/integration/plugin/ent/context.go
Normal file
20
entc/integration/plugin/ent/context.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// Code generated (@generated) by entc, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
// FromContext returns the Client stored in a context, or nil if there isn't one.
|
||||
func FromContext(ctx context.Context) *Client {
|
||||
c, _ := ctx.Value(contextKey{}).(*Client)
|
||||
return c
|
||||
}
|
||||
|
||||
// NewContext returns a new context with the given Client attached.
|
||||
func NewContext(parent context.Context, c *Client) context.Context {
|
||||
return context.WithValue(parent, contextKey{}, c)
|
||||
}
|
||||
@@ -239,6 +239,14 @@ func IsNotFound(err error) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// MaskNotFound masks nor found error.
|
||||
func MaskNotFound(err error) error {
|
||||
if IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ErrNotSingular returns when trying to fetch a singular entity and more then one was found in the database.
|
||||
type ErrNotSingular struct {
|
||||
label string
|
||||
|
||||
@@ -3,28 +3,27 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"fbc/ent/dialect"
|
||||
"fbc/ent/dialect/sql/schema"
|
||||
"fbc/ent/field"
|
||||
)
|
||||
|
||||
var (
|
||||
nullable = true
|
||||
// BoringsColumns holds the columns for the "borings" table.
|
||||
BoringsColumns = []*schema.Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
}
|
||||
// BoringsTable holds the schema information for the "borings" table.
|
||||
BoringsTable = &schema.Table{
|
||||
Name: "borings",
|
||||
Columns: BoringsColumns,
|
||||
PrimaryKey: []*schema.Column{BoringsColumns[0]},
|
||||
ForeignKeys: []*schema.ForeignKey{},
|
||||
}
|
||||
// Tables holds all the tables in the schema.
|
||||
Tables = []*schema.Table{
|
||||
BoringsTable,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Schema is the API for creating, migrating and dropping a schema.
|
||||
type Schema struct {
|
||||
drv dialect.Driver
|
||||
universalID bool
|
||||
}
|
||||
|
||||
// NewSchema creates a new schema client.
|
||||
func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} }
|
||||
|
||||
// Create creates all schema resources.
|
||||
func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
|
||||
migrate, err := schema.NewMigrate(s.drv, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ent/migrate: %v", err)
|
||||
}
|
||||
return migrate.Create(ctx, Tables...)
|
||||
}
|
||||
|
||||
@@ -3,39 +3,28 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"fbc/ent/dialect"
|
||||
"fbc/ent/dialect/sql/schema"
|
||||
"fbc/ent/field"
|
||||
)
|
||||
|
||||
// SQLDialect wraps the dialect.Driver with additional migration methods.
|
||||
type SQLDriver interface {
|
||||
Create(context.Context, ...*schema.Table) error
|
||||
}
|
||||
|
||||
// Schema is the API for creating, migrating and dropping a schema.
|
||||
type Schema struct {
|
||||
drv SQLDriver
|
||||
}
|
||||
|
||||
// NewSchema creates a new schema client.
|
||||
func NewSchema(drv dialect.Driver) *Schema {
|
||||
s := &Schema{}
|
||||
switch drv.Dialect() {
|
||||
case dialect.MySQL:
|
||||
s.drv = &schema.MySQL{Driver: drv}
|
||||
case dialect.SQLite:
|
||||
s.drv = &schema.SQLite{Driver: drv}
|
||||
var (
|
||||
nullable = true
|
||||
// BoringsColumns holds the columns for the "borings" table.
|
||||
BoringsColumns = []*schema.Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Create creates all schema resources.
|
||||
func (s *Schema) Create(ctx context.Context) error {
|
||||
if s.drv == nil {
|
||||
return fmt.Errorf("ent/migrate: dialect does not support migration")
|
||||
// BoringsTable holds the schema information for the "borings" table.
|
||||
BoringsTable = &schema.Table{
|
||||
Name: "borings",
|
||||
Columns: BoringsColumns,
|
||||
PrimaryKey: []*schema.Column{BoringsColumns[0]},
|
||||
ForeignKeys: []*schema.ForeignKey{},
|
||||
}
|
||||
return s.drv.Create(ctx, Tables...)
|
||||
// Tables holds all the tables in the schema.
|
||||
Tables = []*schema.Table{
|
||||
BoringsTable,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user