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

feat: Generate read-only Queries #3291

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jarri-abidi
Copy link

@jarri-abidi jarri-abidi commented Mar 25, 2024

Solution for #3187

no support for pgx
sorry if the code looks messy at all - open to feedback

if !strings.Contains(qsql, "SELECT") {
continue
}
if strings.Contains(qsql, "UPDATE") || strings.Contains(qsql, "INSERT") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also check for delete as well if queries have

something like

DELETE FROM your_table
WHERE some_condition IN (
    SELECT some_column
    FROM another_table
    WHERE your_condition
);

rq = append(rq, q)
}
return rq
}
Copy link

@K-odesome K-odesome Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do it something like this

func readOnly(queries []Query) []Query {
	var rq []Query
	for _, q := range queries {
		if !q.hasRetType() {
			continue
		}

		qsql := strings.ToUpper(q.SQL)
		if !containsOnlyAllowedKeywords(qsql) {
			continue
		}

		rq = append(rq, q)
	}
	return rq
}

func containsOnlyAllowedKeywords(query string) bool {
	keywords := []string{"INSERT", "UPDATE", "DELETE","CREATE","ALTER"} // and the other keywords
		for _, keyword := range keywords {
                          if strings.Contains(qsql, keyword) {
                             return false
                          }		
	}

	return true
}

So that we can define the list of exluded words

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queries wont/shouldn't have ddl. so we can rule out CREATE and ALTER

@jarri-abidi jarri-abidi changed the title Generate read-only Queries feat: Generate read-only Queries Mar 25, 2024
@jarri-abidi
Copy link
Author

hey @kyleconroy can i get a review on this?

@jarri-abidi
Copy link
Author

@kyleconroy is this good to go?

@jarri-abidi
Copy link
Author

bump @kyleconroy

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

Successfully merging this pull request may close these issues.

None yet

2 participants