Skip to content
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

How to insert comment or fragment in sql? #244

Open
canuran opened this issue May 31, 2023 · 8 comments
Open

How to insert comment or fragment in sql? #244

canuran opened this issue May 31, 2023 · 8 comments

Comments

@canuran
Copy link

canuran commented May 31, 2023

Such as select name from user /* shard_id=1 */ where name like 'can%', the shard_id comment is to tell the database proxy how to route to the sharded database.

Suggest: SELECT(User.Name).FROM(User).FRAG(FRAG("/* shard_id=1 */").BeforeWhere()).WHERE(...)

@go-jet
Copy link
Owner

go-jet commented Jun 3, 2023

Hi @canuran
Can you use optimizer hints instead?

@canuran
Copy link
Author

canuran commented Jun 7, 2023

Hi @canuran Can you use optimizer hints instead?

It seems impossible, optimizer_hints can only be used for mysql and the format is fixed, not suitable for prompting db proxy.

@houten11
Copy link

What database proxy are you using? Does a comment have to appear at a specific position inside the statement?

@dragondgold
Copy link

Is there any way to do this with the latest version?

@houten11
Copy link

No, it is not possible.

@go-jet
Copy link
Owner

go-jet commented Nov 21, 2024

@dragondgold Is your use case the same as the OP? Comment before WHERE or after FROM? What proxy is this?

@dragondgold
Copy link

@dragondgold Is your use case the same as the OP? Comment before WHERE or after FROM? What proxy is this?

My usecase is actually for tracing. I would like to add comment just before the query with a trace ID

@go-jet
Copy link
Owner

go-jet commented Nov 22, 2024

Workaround would be to modify sql and call qrm manually.

In @dragondgold case:

func QueryWithTrace(ctx context.Context, traceID int, db qrm.Queryable, stmt Statement, dest any) error {
	sql, args := stmt.Sql()

	tracedSQL := fmt.Sprintf("-- Trace: %d", traceID) + sql

	_, err := qrm.Query(ctx, db, tracedSQL, args, dest)

	return err
}

Now instead stmt.Query(db, &dest), the query call would be:

var dest model.Users

err := QueryWithTrace(ctx, 11, db, stmt, &dest)

Similarly in @canuran case:

func QueryWithShardID(ctx context.Context, shardID int, db qrm.Queryable, stmt Statement, dest any) error {
	sql, args := stmt.Sql()

	shardedSQL := sql

	if idx := strings.Index(sql, "WHERE"); idx != -1 {
		shardedSQL = sql[:idx] + fmt.Sprintf("/* shard_id=%d */ ", shardID) + sql[idx:]
	}

	_, err := qrm.Query(ctx, db, shardedSQL, args, dest)

	return err
}

Also note that prepared statement caching should not be used with this modified queries, since new prepared statements will be created and cached for every distinct comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants