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

[Feature request] Execute raw SQL queries #145

Open
cedx opened this issue Sep 27, 2021 · 4 comments
Open

[Feature request] Execute raw SQL queries #145

cedx opened this issue Sep 27, 2021 · 4 comments

Comments

@cedx
Copy link
Member

cedx commented Sep 27, 2021

Some SQL statements are not yet implemented (cf. DISTINCT), and others will probably never be implemented (i.e. SET foreign_key_checks = 0 or ALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci on MySQL).

So we need a way to execute raw SQL queries to be able to escape from TinkSQL, that is probably expose the ident, run and value methods from the underlying driver.

Currently, I use this kind of horror (with Node.js and MySQL):

import tink.sql.Database;
import tink.sql.DatabaseDefinition;
import tink.sql.drivers.node.MySql.MySqlConnection;
import tink.sql.drivers.node.MySql.MySqlConnectionPool;

class Schema extends Database<SchemaTables> {

	var connection(get, never): MySqlConnection<SchemaTables>;
	function get_connection() {
		final cnx: MySqlConnectionPool<SchemaTables> = cast @:privateAccess __cnx;
		return new MySqlConnection(@:privateAccess cnx.info, @:privateAccess cnx.getNativeConnection(), true);
	}

	public inline function ident(identifier: String) return connection.ident(identifier);
	public inline function run(statement: String) return @:privateAccess connection.run({sql: statement});
	public inline function value(value: Any) return connection.value(value);
}

interface SchemaTables extends DatabaseDefinition {
	@:table("table1") final table1: MyTableData1;
	@:table("table2") final table2: MyTableData2;
}
@serjek
Copy link

serjek commented Sep 27, 2021

I use that:

final QUERY = "#whatever query you have in mind";
 @:privateAccess (cast db.User.cnx:MySqlConnection<Db>).run({sql:QUERY}); //this returns Promise that you can cast to whatever you expect at function declaration

A bit dirty but fits the need. But your approach is more advanced I give you that :)

@sh-dave
Copy link
Contributor

sh-dave commented Nov 25, 2021

Thanks both of you 👍 i just made use of this as well.

(I had to handle unsigned BIGINT's and converted them to strings via CAST(seed as NCHAR) as seed as a dirty hack.)

@andyli
Copy link
Member

andyli commented Mar 2, 2022

I've added Connection#executeSql() which can fulfil some of the use cases. It cannot properly process the query result though because it needs to understand the input query in order to understand what is returned.

@cedx
Copy link
Member Author

cedx commented Feb 16, 2023

We still don't have easy/obvious access to the connection object and the ident/value methods...

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

No branches or pull requests

4 participants