Skip to content

Commit

Permalink
Cache type result - skip stream overhead on php when fetching all (ha…
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Dec 28, 2019
1 parent 9f41b98 commit 8b4dd6e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
5 changes: 5 additions & 0 deletions src/tink/sql/Dataset.hx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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.PDO.PDOConnection<DatabaseInfo>)
.syncResult(cast toQuery());
#else
return stream().collect();
#end

public function first():Promise<Result>
return all()
Expand Down
8 changes: 4 additions & 4 deletions src/tink/sql/drivers/node/MySql.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ class MySqlConnection<Db:DatabaseInfo> implements Connection<Db> implements Sani
case Select(_) | Union(_):
Stream.promise(fetch().next(function (res:Array<Any>) {
var iterator = res.iterator();
var parse = parser.queryParser(query, formatter.isNested(query));
return Stream.ofIterator({
hasNext: function() return iterator.hasNext(),
next: function ()
return parser.parseResult(query, iterator.next(), formatter.isNested(query))
next: function () return parse(iterator.next())
});
}));

case CallProcedure(_):
Stream.promise(fetch().next(function (res:Array<Array<Any>>) {
var iterator = res[0].iterator();
var parse = parser.queryParser(query, formatter.isNested(query));
return Stream.ofIterator({
hasNext: function() return iterator.hasNext(),
next: function ()
return parser.parseResult(query, iterator.next(), formatter.isNested(query))
next: function () return parse(iterator.next())
});
}));
case CreateTable(_, _) | DropTable(_) | AlterTable(_, _):
Expand Down
19 changes: 16 additions & 3 deletions src/tink/sql/drivers/php/PDO.hx
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ class PDOConnection<Db:DatabaseInfo> implements Connection<Db> implements Saniti
case Select(_) | Union(_) | CallProcedure(_):
Stream.promise(fetch().next(function (res:PDOStatement) {
var row: Any;
var parse = parser.queryParser(query, formatter.isNested(query));
return Stream.ofIterator({
hasNext: function() {
row = res.fetchObject();
return row != false;
},
next: function () {
return parser.parseResult(query, row, formatter.isNested(query));
}
next: function () return parse(row)
});
}));
case CreateTable(_, _) | DropTable(_) | AlterTable(_, _):
Expand All @@ -105,4 +104,18 @@ class PDOConnection<Db:DatabaseInfo> implements Connection<Db> implements Saniti
try cnx.query(query)
catch (e: PDOException)
new Error(e.getCode(), e.getMessage());

// haxetink/tink_streams#20
public function syncResult<R, T: {}>(query:Query<Db,R>): Outcome<Array<T>, Error> {
return switch query {
case Select(_) | Union(_) | CallProcedure(_):
var parse = parser.queryParser(query, formatter.isNested(query));
try Success([
for (row in cnx.query(formatter.format(query)).fetchAll(PDO.FETCH_OBJ))
parse(row)
]) catch (e: PDOException)
Failure(new Error(e.getCode(), e.getMessage()));
default: throw 'Cannot iterate this query';
}
}
}
47 changes: 24 additions & 23 deletions src/tink/sql/parse/ResultParser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,33 @@ class ResultParser<Db> {
}
}

public function parseResult<Row:{}>(
query:Query<Db, Dynamic>,
row:DynamicAccess<Any>,
public function queryParser<Row:{}>(
query:Query<Db, Dynamic>,
nest:Bool
):Row {
var res: DynamicAccess<Any> = {}
var target = res;
): DynamicAccess<Any> -> Row {
var types = typer.typeQuery(query);
for (field in row.keys()) {
var name = field;
var table = null;
if (nest) {
var parts = field.split(SqlFormatter.FIELD_DELIMITER);
table = parts[0];
name = parts[1];
target =
if (!res.exists(table)) res[table] = {};
else res[table];
}
target[name] = parseValue(row[field],
switch types.get(field) {
case null: None;
case v: v;
return function (row: DynamicAccess<Any>) {
var res: DynamicAccess<Any> = {}
var target = res;
for (field in row.keys()) {
var name = field;
var table = null;
if (nest) {
var parts = field.split(SqlFormatter.FIELD_DELIMITER);
table = parts[0];
name = parts[1];
target =
if (!res.exists(table)) res[table] = {};
else res[table];
}
);
target[name] = parseValue(row[field],
switch types.get(field) {
case null: None;
case v: v;
}
);
}
return cast res;
}
return cast res;
}
}

0 comments on commit 8b4dd6e

Please sign in to comment.