Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1293 dpurdie 1
###############################################################################
2
# Codestriker: Copyright (c) 2001, 2002 David Sitsky.  All rights reserved.
3
# sits@users.sourceforge.net
4
#
5
# This program is free software; you can redistribute it and modify it under
6
# the terms of the GPL.
7
 
8
# Model object for handling topic file data.
9
 
10
package Codestriker::Model::File;
11
 
12
use strict;
13
use Codestriker::Model::Delta;
14
 
15
# Create the appropriate delta rows for this review.  Note this gets called
16
# from Topic::create(), which controls the transaction commit/rollback.
17
sub create($$$$) {
18
    my ($type, $dbh, $topicid, $deltas_ref) = @_;
19
 
20
    # Create the appropriate prepared statements.
21
    my $insert_file =
22
	$dbh->prepare_cached('INSERT INTO topicfile ' .
23
			     '(topicid, sequence, filename,' .
24
			     ' topicoffset, revision, diff, binaryfile) ' .
25
			     'VALUES (?, ?, ?, ?, ?, ?, ?)');
26
    my $success = defined $insert_file;
27
 
28
    my $insert_delta =
29
	$dbh->prepare_cached('INSERT INTO delta (topicid, file_sequence, ' .
30
			     'delta_sequence, old_linenumber, ' .
31
			     'new_linenumber, deltatext, ' .
32
			     'description, repmatch) ' .
33
			     'VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
34
    $success &&= defined $insert_delta;
35
 
36
    my @deltas = @$deltas_ref;
37
    my $last_filename = "";
38
    my $file_sequence = -1;
39
    my $delta_sequence = -1;
40
    for (my $i = 0; $i <= $#deltas; $i++) {
41
	my $delta = $deltas[$i];
42
	if ($last_filename ne $delta->{filename}) {
43
	    # Create new file entry.
44
	    $success &&= $insert_file->execute($topicid,
45
					       ++$file_sequence,
46
					       $delta->{filename}, -1,
47
					       $delta->{revision}, "",
48
					       $delta->{binary});
49
	    $last_filename = $delta->{filename};
50
	}
51
 
52
	# Add the new delta entry.
53
	$success &&= $insert_delta->execute($topicid, $file_sequence,
54
					    ++$delta_sequence,
55
					    $delta->{old_linenumber},
56
					    $delta->{new_linenumber},
57
					    $delta->{text},
58
					    $delta->{description},
59
					    $delta->{repmatch});
60
    }
61
 
62
    die $dbh->errstr unless $success;
63
}
64
 
65
# Retrieve the details of a file for a specific topicid and filenumber.
66
sub get($$$$$$) {
67
    my ($type, $topicid, $filenumber,
68
	$offset_ref, $revision_ref, $diff_ref) = @_;
69
 
70
    # Obtain a database connection.
71
    my $dbh = Codestriker::DB::DBI->get_connection();
72
 
73
    # Retrieve the file information.
74
    my $select_file =
75
	$dbh->prepare_cached('SELECT topicoffset, revision, diff ' .
76
			     'FROM topicfile ' .
77
			     'WHERE topicid = ? AND sequence = ?');
78
    my $success = defined $select_file;
79
    $success &&= $select_file->execute($topicid, $filenumber);
80
 
81
    if ($success) {
82
	my ($offset, $revision, $diff) = $select_file->fetchrow_array();
83
 
84
	# Store the results in the reference variables and return.
85
	$$offset_ref = $offset;
86
	$$revision_ref = $revision;
87
	$$diff_ref = $diff;
88
	$select_file->finish();
89
    }
90
 
91
    Codestriker::DB::DBI->release_connection($dbh, $success);
92
    die $dbh->errstr unless $success;
93
}
94
 
95
# Retrieve the details of which files, revisions and offsets are present for
96
# a specific topic.
97
sub get_filetable($$$$$$$) {
98
    my ($type, $topicid, $filename_array_ref, $revision_array_ref,
99
	$offset_array_ref, $binary_array_ref, $numchanges_array_ref) = @_;
100
 
101
    # Obtain a database connection.
102
    my $dbh = Codestriker::DB::DBI->get_connection();
103
 
104
    # Setup the appropriate statement and execute it.
105
    my $select_file =
106
	$dbh->prepare_cached('SELECT filename, revision, topicoffset, ' .
107
			     'binaryfile, sequence FROM topicfile ' .
108
			     'WHERE topicid = ? ' .
109
			     'ORDER BY sequence');
110
    my $success = defined $select_file;
111
    $success &&= $select_file->execute($topicid);
112
 
113
    # Store the results in the referenced arrays.
114
    if ($success) {
115
	my @data;
116
        my @sequence;
117
	while (@data = $select_file->fetchrow_array()) {
118
	    push @$filename_array_ref, $data[0];
119
	    push @$revision_array_ref, $data[1];
120
	    push @$offset_array_ref, $data[2];
121
	    push @$binary_array_ref, $data[3];
122
	    push @sequence, $data[4];
123
	}
124
	$select_file->finish();
125
 
126
	# This has to be called outside the loop above, as SQL Server
127
	# doesn't allow nested selects... gggrrrr.
128
	foreach my $file_id (@sequence) {
129
	    # Now get the number of lines affected in this file
130
	    my $numchanges = Codestriker::Model::Delta->get_delta_size($topicid, $file_id);
131
 
132
	    push @$numchanges_array_ref, $numchanges;
133
	}
134
    }
135
 
136
    Codestriker::DB::DBI->release_connection($dbh, $success);
137
    die $dbh->errstr unless $success;
138
}
139
 
140
 
141
1;