Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Oct 2, 2019
1 parent 2c6e9ed commit 630ba69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/tink/sql/Dataset.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tink.sql;
import tink.sql.Expr;
import tink.streams.RealStream;
import tink.sql.Query;
import tink.sql.Info;

using tink.CoreApi;

Expand Down Expand Up @@ -185,7 +186,12 @@ class Dataset<Fields, Result:{}, Db> {
return cnx.execute(toQuery());

public function all():Promise<Array<Result>>
#if php
return (cast cnx: tink.sql.drivers.php.MySQLi.MySQLiConnection<DatabaseInfo>)
.syncResult(cast toQuery());
#else
return stream().collect();
#end

public function first():Promise<Result>
return all()
Expand Down
18 changes: 16 additions & 2 deletions src/tink/sql/drivers/php/MySQLi.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,27 @@ class MySQLiConnection<Db:DatabaseInfo> implements Connection<Db> implements San
public function getFormatter()
return formatter;

public function syncResult<R, T>(query:Query<Db,R>): Outcome<Array<T>, Error> {
return switch query {
case Select(_) | Union(_):
switch cnx.query(formatter.format(query)) {
case false: Failure(new Error(cnx.errno, cnx.error));
case (cast _: ResultSet) => res:
Success([
for (row in res.nestedIterator(formatter.isNested(query)))
row
]);
}
default: throw 'Cannot iterate this query';
}
}

public function execute<Result>(query:Query<Db,Result>):Result {
inline function fetch<T>(): Promise<T> return run(formatter.format(query));
return switch query {
case Select(_) | Union(_):
Stream.promise(fetch().next(function (res:ResultSet)
return Stream.ofIterator(res.nestedIterator(formatter.isNested(query)))
return Stream.ofIterator(cast res.nestedIterator(formatter.isNested(query)))
));
case CreateTable(_, _) | DropTable(_) | AlterTable(_, _):
fetch().next(function(_) return Noise);
Expand All @@ -87,7 +102,6 @@ class MySQLiConnection<Db:DatabaseInfo> implements Connection<Db> implements San
case false: new Error(cnx.errno, cnx.error);
case v: (cast v: T);
}

}

private abstract ResultSet(NativeResultSet) from NativeResultSet {
Expand Down

0 comments on commit 630ba69

Please sign in to comment.