<commit_before><?php

include_once "annotations.php";

class ReadWrite {
    const Read = 1;
    const Write = 2;
}

function dao_open() {
    global $db, $all_ops_stmt, $operation_stmt, $code_ref_stmt, $io_stmt, $object_stmt, $io_for_object_stmt, $io_for_parent_io_stmt, $operation_by_name_stmt, $io_for_object_parent_name_stmt;

    $db = new SQlite3("test/test.sq3", SQLITE3_OPEN_READONLY);
    # Get all ops
    $all_ops_stmt = $db->prepare("SELECT operation_id as id, name, parent_id, code_start_id, code_end_id, terminated_time FROM operation ORDER BY operation_id");
    # Get operation for a given id
    $operation_stmt = $db->prepare("SELECT operation_id as id, name, parent_id, code_start_id, code_end_id, terminated_time FROM operation WHERE operation_id = :id");
    # get a reference to code by reference id
    $code_ref_stmt = $db->prepare("SELECT code_id as id, file, line FROM code WHERE code_id = :id");
    # Get list of IOs for given operation (only the top-level ones)
    $io_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id FROM io WHERE operation_id = :id AND parent_id = -1 ORDER BY io_time ASC');
    # Get object of given id
    $object_stmt = $db->prepare('SELECT object_id as id, type, deallocated_time FROM object where object_id = :id');
    # All parent IOs for object
    $io_for_object_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id FROM io where object_id = :id ORDER BY io_time ASC');
    # All IOs for parent io
    $io_for_parent_io_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id, parent_id FROM io WHERE parent_id = :id ORDER BY io_id ASC');
    # Operation of a given name for parent operation
    $operation_by_name_stmt = $db->prepare('SELECT operation_id as id, name, parent_id, code_start_id, code_end_id, terminated_time FROM operation WHERE name = :name AND parent_id = :parent ORDER BY operation_id');
    # All IOs for given object, parent IO and name
    $io_for_object_parent_name_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id FROM io where operation_id = :operation_id AND parent_id = :parent_io AND name = :name AND note=:note ORDER BY io_time ASC');
    check_error();
}

function dao_close() {
    global $db;

    $db->close();
}

function get_code($id)
{
    global $code_ref_stmt;
    $code_ref_stmt->bindParam("id", $id, SQLITE3_INTEGER);
    return $code_ref_stmt->execute()->fetchArray();
}

function annotate($result) {
    $annotations = get_annotations();
    if (array_key_exists($result['name'], $annotations)) {
        $annotation = $annotations[$result['name']];
        $result['sdesc'] = $annotation['sdesc'];
        $result['desc'] = $annotation['desc'];
    } else {
        $result['sdesc'] = $result['name'];
        $result['desc'] = '';
    }
    return $result;
}

function get_operation($id)
{
    global $operation_stmt;

    $operation_stmt->bindParam('id', $id);
    $result = $operation_stmt->execute()->fetchArray();
    return annotate($result);
}

function check_error() {
    global $db;
    if(sqlite_last_error($db)) {
        echo sqlite_error_string(sqlite_last_error($db));
    }
}

# Return a list of operations of a given name for given parent
# Example syntax:
# op1(*)/op2(0)/op3(1)
# ... will enumerate all operations named op1, then take 1-st sub-operation named op2 of each, then 2-nd sub-operation named op3 of that
# .. all results are returned
function get_operations_by_name($path, $parent_id=-1) {
    global $operation_by_name_stmt;
    $names = preg_split('/\//', $path, 2);
    $current = $names[0];
    $remaining = $names[1];
    preg_match('/^(?P<name>.*)\((?P<count>.*)\)$/', $current, $matches);
    $name = $matches['name'];
    $count = $matches['count'];<commit_msg>Switching to PDO, work in progress.<commit_after><?php

include_once "annotations.php";

class ReadWrite {
    const Read = 1;
    const Write = 2;
}

function dao_open() {
    global $db, $all_ops_stmt, $operation_stmt, $code_ref_stmt, $io_stmt, $object_stmt, $io_for_object_stmt, $io_for_parent_io_stmt, $operation_by_name_stmt, $io_for_object_parent_name_stmt;

    $db = new PDO("sqlite:test.sq3");
    # Get all ops
    $all_ops_stmt = $db->prepare("SELECT operation_id as id, name, parent_id, code_start_id, code_end_id, terminated_time FROM operation ORDER BY operation_id");
    # Get operation for a given id
    $operation_stmt = $db->prepare("SELECT operation_id as id, name, parent_id, code_start_id, code_end_id, terminated_time FROM operation WHERE operation_id = :id");
    # get a reference to code by reference id
    $code_ref_stmt = $db->prepare("SELECT code_id as id, file, line FROM code WHERE code_id = :id");
    # Get list of IOs for given operation (only the top-level ones)
    $io_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id FROM io WHERE operation_id = :id AND parent_id = -1 ORDER BY io_time ASC');
    # Get object of given id
    $object_stmt = $db->prepare('SELECT object_id as id, type, deallocated_time FROM object where object_id = :id');
    # All parent IOs for object
    $io_for_object_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id FROM io where object_id = :id ORDER BY io_time ASC');
    # All IOs for parent io
    $io_for_parent_io_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id, parent_id FROM io WHERE parent_id = :id ORDER BY io_id ASC');
    # Operation of a given name for parent operation
    $operation_by_name_stmt = $db->prepare('SELECT operation_id as id, name, parent_id, code_start_id, code_end_id, terminated_time FROM operation WHERE name = :name AND parent_id = :parent ORDER BY operation_id');
    # All IOs for given object, parent IO and name
    $io_for_object_parent_name_stmt = $db->prepare('SELECT io_id as id, io_time, object_id, operation_id, name, value, readwrite, note, code_id FROM io where operation_id = :operation_id AND parent_id = :parent_io AND name = :name AND note=:note ORDER BY io_time ASC');
    check_error();
}

function dao_close() {
    global $db;

    $db->close();
}

function get_code($id)
{
    global $code_ref_stmt;
    $code_ref_stmt->bindParam("id", $id, PDO::PARAM_INT);
    return $code_ref_stmt->execute()->fetchArray();
}

function annotate($result) {
    $annotations = get_annotations();
    if (array_key_exists($result['name'], $annotations)) {
        $annotation = $annotations[$result['name']];
        $result['sdesc'] = $annotation['sdesc'];
        $result['desc'] = $annotation['desc'];
    } else {
        $result['sdesc'] = $result['name'];
        $result['desc'] = '';
    }
    return $result;
}

function get_operation($id)
{
    global $operation_stmt;

    $operation_stmt->bindParam('id', $id);
    $result = $operation_stmt->execute()->fetchArray();
    return annotate($result);
}

function check_error() {
    global $db;
    if($db->errorCode()) {
        $errorInfo = $db->errorInfo();
        echo $errorInfo[2];
    }
}

# Return a list of operations of a given name for given parent
# Example syntax:
# op1(*)/op2(0)/op3(1)
# ... will enumerate all operations named op1, then take 1-st sub-operation named op2 of each, then 2-nd sub-operation named op3 of that
# .. all results are returned
function get_operations_by_name($path, $parent_id=-1) {
    global $operation_by_name_stmt;
    $names = preg_split('/\//', $path, 2);
    $current = $names[0];
    $remaining = $names[1];
    preg_match('/^(?P<name>.*)\((?P<count>.*)\)$/', $current, $matches);
    $name = $matches['name'];
    $count = $matches['count'];
{'fraction_non_alphanumeric': 0.13246292305742172, 'fraction_numerical': 0.005070351121815185, 'mean_word_length': 4.281124497991968, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, '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': '5036068', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2583, 'n_words': 753}