<commit_before>package tink.sql;


import tink.sql.Expr;
import tink.streams.RealStream;
import tink.sql.Table;

using tink.CoreApi;

class Dataset<Fields, Filter, Result:{}, Db> { 
  
  public var fields(default, null):Fields;
  
  var cnx:Connection<Db>;
  var target:Target<Result, Db>;
  var toCondition:Filter->Condition;
  var condition:Null<Condition>;
  
  function new(fields, cnx, target, toCondition, ?condition) { 
    this.fields = fields;
    this.cnx = cnx;
    this.target = target;
    this.toCondition = toCondition;
    this.condition = condition;
  }
  
  macro public function where(ethis, filter) {
    filter = tink.sql.macros.Filters.makeFilter(ethis, filter);
    return macro @:pos(ethis.pos) @:privateAccess $ethis._where(@:noPrivateAccess $filter);
  }
  
  function _where(filter:Filter):Dataset<Fields, Filter, Result, Db>
    return new Dataset(fields, cnx, target, toCondition, condition && toCondition(filter));
  
  public function stream(?limit:Limit, ?orderBy:OrderBy<Result>):RealStream<Result>
    return cnx.selectAll(target, condition, limit, orderBy);
    
  //TODO: add order
  public function first(?orderBy:OrderBy<Result>):Promise<Result> 
    return all({limit:1, offset:0}, orderBy)
      .next(function (r:Array<Result>) return switch r {
        case []: Failure(new Error(NotFound, 'The requested item was not found'));
        case v: Success(v[0]);
      });
    
  public function all(?limit:Limit, ?orderBy:OrderBy<Result>):Promise<Array<Result>>
    return stream(limit, orderBy).collect();
  
  @:noCompletion 
  static public function get<Fields, Filter, Result:{}, Db>(v:Dataset<Fields, Filter, Result, Db>) {
    return v;
  }
    
  macro public function leftJoin(ethis, ethat)
    return tink.sql.macros.Joins.perform(Left, ethis, ethat);<commit_msg>Use a function to create the order-by clause<commit_after>package tink.sql;


import tink.sql.Expr;
import tink.streams.RealStream;
import tink.sql.Table;

using tink.CoreApi;

class Dataset<Fields, Filter, Result:{}, Db> { 
  
  public var fields(default, null):Fields;
  
  var cnx:Connection<Db>;
  var target:Target<Result, Db>;
  var toCondition:Filter->Condition;
  var condition:Null<Condition>;
  
  function new(fields, cnx, target, toCondition, ?condition) { 
    this.fields = fields;
    this.cnx = cnx;
    this.target = target;
    this.toCondition = toCondition;
    this.condition = condition;
  }
  
  macro public function where(ethis, filter) {
    filter = tink.sql.macros.Filters.makeFilter(ethis, filter);
    return macro @:pos(ethis.pos) @:privateAccess $ethis._where(@:noPrivateAccess $filter);
  }
  
  function _where(filter:Filter):Dataset<Fields, Filter, Result, Db>
    return new Dataset(fields, cnx, target, toCondition, condition && toCondition(filter));
  
  public function stream(?limit:Limit, ?orderBy:Fields->OrderBy<Result>):RealStream<Result>
    return cnx.selectAll(target, condition, limit, orderBy == null ? null : orderBy(fields));
    
  //TODO: add order
  public function first(?orderBy:Fields->OrderBy<Result>):Promise<Result> 
    return all({limit:1, offset:0}, orderBy)
      .next(function (r:Array<Result>) return switch r {
        case []: Failure(new Error(NotFound, 'The requested item was not found'));
        case v: Success(v[0]);
      });
    
  public function all(?limit:Limit, ?orderBy:Fields->OrderBy<Result>):Promise<Array<Result>>
    return stream(limit, orderBy).collect();
  
  @:noCompletion 
  static public function get<Fields, Filter, Result:{}, Db>(v:Dataset<Fields, Filter, Result, Db>) {
    return v;
  }
    
  macro public function leftJoin(ethis, ethat)
    return tink.sql.macros.Joins.perform(Left, ethis, ethat);
{'fraction_non_alphanumeric': 0.1334231805929919, 'fraction_numerical': 0.0016172506738544475, 'mean_word_length': 4.301428571428572, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4788507', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1180, 'n_words': 281}