📄 comment.pm
字号:
################################################################################ Codestriker: Copyright (c) 2001, 2002 David Sitsky. All rights reserved.# sits@users.sourceforge.net## This program is free software; you can redistribute it and modify it under# the terms of the GPL.# Model object for handling comment data.package Codestriker::Model::Comment;use strict;use Encode qw(decode_utf8);use Codestriker::DB::DBI;sub new { my $class = shift; my $self = {}; $self->{id} = 0; $self->{topicid} = 0; $self->{fileline} = 0; $self->{filenumber} = 0; $self->{filenew} = 0; $self->{author} = ''; $self->{data} = ''; $self->{date} = Codestriker->get_timestamp(time); $self->{version} = 0; $self->{db_creation_ts} = ""; $self->{db_modified_ts} = ""; $self->{creation_ts} = ""; $self->{modified_ts} = ""; $self->{metrics} = undef; bless $self, $class; return $self;}# Create a new comment with all of the specified properties. Ensure that the# associated commentstate record is created/updated.sub create { my ($self, $topicid, $fileline, $filenumber, $filenew, $author, $data, $metrics) = @_; my $timestamp = Codestriker->get_timestamp(time); # Obtain a database connection. my $dbh = Codestriker::DB::DBI->get_connection(); # Check if a comment has been made against this line before. my $select_commentstate = $dbh->prepare_cached('SELECT version, id ' . 'FROM commentstate ' . 'WHERE topicid = ? AND fileline = ? AND '. 'filenumber = ? AND filenew = ?'); my $success = defined $select_commentstate; $success &&= $select_commentstate->execute($topicid, $fileline, $filenumber, $filenew); my $commentstateid = 0; my $version = 0; my $creation_ts = ""; if ($success) { ($version, $commentstateid) = $select_commentstate->fetchrow_array(); $success &&= $select_commentstate->finish(); if (! defined $version) { # A comment has not been made on this particular line yet, # create the commentstate row now. Note the old column of # state has its value set to -100 so the data migration code # in checksetup.pl knows this is a new row that can be # ignored. $creation_ts = $timestamp; my $insert = $dbh->prepare_cached('INSERT INTO commentstate ' . '(topicid, fileline, ' . 'filenumber, filenew, ' . 'state, version, creation_ts, ' . 'modified_ts) VALUES ' . '(?, ?, ?, ?, ?, ?, ?, ?)'); $success &&= defined $insert; $success &&= $insert->execute($topicid, $fileline, $filenumber, $filenew, -100, 0, $creation_ts, $creation_ts); $success &&= $insert->finish(); } else { # Update the commentstate record. my $update = $dbh->prepare_cached('UPDATE commentstate SET ' . 'version = ?, ' . 'modified_ts = ? ' . 'WHERE topicid = ? AND ' . 'fileline = ? AND ' . 'filenumber = ? AND ' . 'filenew = ?'); $success &&= defined $update; $success &&= $update->execute(++$version, $timestamp, $topicid, $fileline, $filenumber, $filenew); $success &&= $update->finish(); } # Determine the commentstateid that may have been just created. $success &&= $select_commentstate->execute($topicid, $fileline, $filenumber, $filenew); if ($success) { ($version, $commentstateid) = $select_commentstate->fetchrow_array(); } $success &&= $select_commentstate->finish(); # Create the comment record. my $insert_comment = $dbh->prepare_cached('INSERT INTO commentdata ' . '(commentstateid, '. 'commentfield, author, creation_ts) ' . 'VALUES (?, ?, ?, ?)'); my $success = defined $insert_comment; # Create the comment row. $success &&= $insert_comment->execute($commentstateid, $data, $author, $timestamp); $success &&= $insert_comment->finish(); # Now handle any commentmetric rows. update_comment_metrics($commentstateid, $metrics, $dbh); } $self->{id} = $commentstateid; $self->{topicid} = $topicid; $self->{fileline} = $fileline; $self->{filenumber} = $filenumber; $self->{filenew} = $filenew; $self->{author} = $author; $self->{data} = $data; $self->{date} = $timestamp; $self->{version} = $version; $self->{db_creation_ts} = $creation_ts; $self->{creation_ts} = Codestriker->format_timestamp($creation_ts); $self->{db_modified_ts} = $timestamp; $self->{modified_ts} = Codestriker->format_timestamp($timestamp); # Update the metrics into the object as a hash. foreach my $metric (@{ $metrics }) { $self->{metrics}->{$metric->{name}} = $metric->{value}; } # Get the filename, for the new comment. my $get_filename = $dbh->prepare_cached('SELECT filename ' . 'FROM topicfile ' . 'WHERE topicid = ? AND ' . 'sequence = ?'); $success &&= defined $get_filename; $success &&= $get_filename->execute($topicid, $filenumber); ( $self->{filename} ) = $get_filename->fetchrow_array(); $select_commentstate->finish(); $get_filename->finish(); Codestriker::DB::DBI->release_connection($dbh, $success); die $dbh->errstr if !$success;}# Update the comment metrics for a specific commentstate. Note the rows for# a specific metric may or may not already exist.sub update_comment_metrics { my ($commentstateid, $metrics, $dbh) = @_; # Now create any necessary commentmetric rows. Note its possible this # may refer to existing data which needs to be updated, or could be # new metric data. eval { if (defined $metrics) { foreach my $metric (@{ $metrics }) { # Check if a value for this metric name has been created # already. my $select_metric = $dbh->prepare_cached('SELECT COUNT(id) ' . 'FROM commentstatemetric ' . 'WHERE id = ? AND name = ?'); $select_metric->execute($commentstateid, $metric->{name}); my $count; ($count) = $select_metric->fetchrow_array(); $select_metric->finish(); if ($count == 0) { # Need to create a new row for this metric. my $insert_metric = $dbh->prepare_cached('INSERT INTO commentstatemetric '. '(id, name, value) VALUES ' . '(?, ?, ?)'); $insert_metric->execute($commentstateid, $metric->{name}, $metric->{value}); $insert_metric->finish(); } else { # Need to update this row for this metric. my $update_metric = $dbh->prepare_cached('UPDATE commentstatemetric ' . 'SET value = ? ' . 'WHERE id = ? AND name = ?'); $update_metric->execute($metric->{value}, $commentstateid, $metric->{name}); $update_metric->finish(); } } } }; if ($@) { warn "Unable to update comment state metric data because $@\n"; eval { $dbh->rollback() }; }}# This function returns as a list the authors emails address that have entered# comments against a topic.sub read_authors { my ($type, $topicid ) = @_; # Obtain a database connection. my $dbh = Codestriker::DB::DBI->get_connection(); # Store the results into an array of objects. my @results; # Retrieve all of the comment information for the specified topicid. my $select_comment = $dbh->prepare_cached('SELECT distinct(commentdata.author) ' . 'FROM commentdata, commentstate ' . 'WHERE commentstate.topicid = ? AND ' .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -