Giter Site home page Giter Site logo

Any way to run raw SQL query? about ent HOT 12 CLOSED

ent avatar ent commented on May 6, 2024
Any way to run raw SQL query?

from ent.

Comments (12)

a8m avatar a8m commented on May 6, 2024 16

Hey, closing this issue since I don't think there's an action item here.

If you want to add custom methods to *ent.Client (or any other type), you can either use external templates, or just add them manually to an external.go file under the <project>/ent directory. For example:

func (c *Client) DB() *sql.DB {
  return c.driver.(*entsql.Driver).DB()
}

from ent.

a8m avatar a8m commented on May 6, 2024 11

Hey all! I added a feature flag named sql/execquery to simplify the process for those who need it.

from ent.

giautm avatar giautm commented on May 6, 2024 2

Only one reason for me to access underly *sql.DB is creating sqlhealth.Checker. So, I write custom template that generates health.Checker for gocloud kit.

{{ define "sqlhealth" }}

{{ $pkg := base $.Config.Package }}
{{- with extend $ "Package" $pkg }}
        {{ template "header" . }}
{{- end }}

import (
	"entgo.io/ent/dialect/sql"
	"gocloud.dev/server/health/sqlhealth"
)

func (c *Client) HealthCheck() *sqlhealth.Checker {
	db := c.driver.(*sql.Driver).DB()
	return sqlhealth.New(db)
}
{{ end }}
// appHealthChecks returns a health check for the database. This will signal
// to Kubernetes or other orchestrators that the server should not receive
// traffic until the server is able to connect to its database.
func appHealthChecks(ent *entv1.Client) ([]health.Checker, func()) {
	dbCheck := ent.HealthCheck()
	list := []health.Checker{dbCheck}
	return list, func() {
		dbCheck.Stop()
	}
}

from ent.

manakuro avatar manakuro commented on May 6, 2024 1

If you use debug mode by default and run into the error:

interface conversion: dialect.Driver is *dialect.DebugDriver, not *sql.Driver

Try this one.

func (c *Client) DB() *sql.DB {
	if c.debug {
            return c.driver.(*dialect.DebugDriver).Driver.(*entsql.Driver).DB()
	}
	return c.driver.(*entsql.Driver).DB()
}

from ent.

a8m avatar a8m commented on May 6, 2024

ent.Client doesn't expose an API for interacting directly with the database.
If you need to do this, you can either use the dialect.Driver or sql.DB in your case.

Let me know if you need any help with that.

from ent.

leafduo avatar leafduo commented on May 6, 2024

dialect.Driver or sql.DB are both OK, but that requires passing additional struct around since ent.Client doesn't provide a way to get underlying driver or db.

from ent.

a8m avatar a8m commented on May 6, 2024

ent.Client doesn't provide a way to get underlying driver or db.

Yeah, because we believe it's not required for most of the use cases, and we prefer to stick with the type-safe API. However, predicates can be customized.

Can you share your use case, or what is missing in the client? Thanks.

from ent.

ansarizafar avatar ansarizafar commented on May 6, 2024

It would be great If ent.Client exposes underlying driver or db by default so that we don't have to do it our self every time we generate the code. I want to execute following commands after client is connected to database.

set search_path to 'schema'
set timezone TO 'GMT';

from ent.

a8m avatar a8m commented on May 6, 2024

that we don't have to do it our self every time we generate the code.

How do you run codegen? I'm trying to figure out why do you need to rewrite it after each codegen?

If you delete the ent directory before each codegen, I'll recommend you to use templates. For example:

{{ define "external" }}
package ent

func (c *Client) DB() *sql.DB {
  return c.driver.(*entsql.Driver).DB()
}
{{ end }}

from ent.

ansarizafar avatar ansarizafar commented on May 6, 2024

I mean every time we generate the code for a new project. The workaround is simple but its more convenient to expose DB() method as part of the client api by default specially for PostgreSQL.

from ent.

preslavrachev avatar preslavrachev commented on May 6, 2024

@a8m I am trying the following command:

//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/execquery ./ent/schema

But I don't seem to be finding the expected methods afterwards. Could it be that I am caching the generator somehow?

from ent.

a8m avatar a8m commented on May 6, 2024

Hey @leafduo 👋

Can you please update ent to the latest master and try again? go get entgo.io/ent/cmd/ent@master

from ent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.