diff --git a/src/tink/sql/Dataset.hx b/src/tink/sql/Dataset.hx index f22898e..5ae46c4 100755 --- a/src/tink/sql/Dataset.hx +++ b/src/tink/sql/Dataset.hx @@ -199,7 +199,12 @@ class Dataset { return cnx.execute(toQuery()); public function all():Promise> + #if php + return (cast cnx: tink.sql.drivers.php.PDO.PDOConnection) + .syncResult(cast toQuery()); + #else return stream().collect(); + #end public function first():Promise return all() diff --git a/src/tink/sql/drivers/node/MySql.hx b/src/tink/sql/drivers/node/MySql.hx index fe770bf..fa056ca 100755 --- a/src/tink/sql/drivers/node/MySql.hx +++ b/src/tink/sql/drivers/node/MySql.hx @@ -76,20 +76,20 @@ class MySqlConnection implements Connection implements Sani case Select(_) | Union(_): Stream.promise(fetch().next(function (res:Array) { 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>) { 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(_, _): diff --git a/src/tink/sql/drivers/php/PDO.hx b/src/tink/sql/drivers/php/PDO.hx index 52cf42b..0bce699 100644 --- a/src/tink/sql/drivers/php/PDO.hx +++ b/src/tink/sql/drivers/php/PDO.hx @@ -73,14 +73,13 @@ class PDOConnection implements Connection 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(_, _): @@ -105,4 +104,18 @@ class PDOConnection implements Connection implements Saniti try cnx.query(query) catch (e: PDOException) new Error(e.getCode(), e.getMessage()); + + // haxetink/tink_streams#20 + public function syncResult(query:Query): Outcome, 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'; + } + } } diff --git a/src/tink/sql/parse/ResultParser.hx b/src/tink/sql/parse/ResultParser.hx index 524125e..ba0ea6e 100644 --- a/src/tink/sql/parse/ResultParser.hx +++ b/src/tink/sql/parse/ResultParser.hx @@ -83,32 +83,33 @@ class ResultParser { } } - public function parseResult( - query:Query, - row:DynamicAccess, + public function queryParser( + query:Query, nest:Bool - ):Row { - var res: DynamicAccess = {} - var target = res; + ): DynamicAccess -> 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) { + var res: DynamicAccess = {} + 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; } } \ No newline at end of file