Skip to content

Commit

Permalink
[49] Improved SQL statements
Browse files Browse the repository at this point in the history
Better to use REPLACE in case of commits with --amend
Use all column names directly to allow adding custom columns
More performant to use only one statement

Signed-off-by: DoesntMatter <jaed1@gmx.net>
  • Loading branch information
DoesntMatter committed Jan 28, 2012
1 parent 8440dec commit 3c5518a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions libs/SQL.pm
Expand Up @@ -52,6 +52,7 @@ HELP
sub TableStruct { sub TableStruct {
my $file = shift || return undef; my $file = shift || return undef;
my $table = shift || "gitlog"; my $table = shift || "gitlog";
my @columns;


open(FILE, ">$file"); open(FILE, ">$file");
print FILE "-- Create table structure print FILE "-- Create table structure
Expand All @@ -64,21 +65,26 @@ CREATE TABLE IF NOT EXISTS `$table` (
`body` text NOT NULL COMMENT 'Body message of commit', `body` text NOT NULL COMMENT 'Body message of commit',
PRIMARY KEY(`hash`) PRIMARY KEY(`hash`)
);\n );\n
-- Insert rows"; -- Insert rows\n";
close(FILE); close(FILE);


return 1; @columns = qw(`hash` `date` `author` `email` `subject` `body`);
return @columns;
} }


sub CreateSQL { sub CreateSQL {
my $gitlog = shift || return undef; my $gitlog = shift || return undef;
my $file = shift || return undef; my $file = shift || return undef;
my $table = shift || "gitlog"; my $table = shift || "gitlog";
my @items = Parser::SplitCommits($gitlog); my @items = Parser::SplitCommits($gitlog);

my @column = TableStruct($file, $table);
TableStruct($file, $table); my $columns = join(",", @column);
my (@query, $queries);


open(FILE, ">>$file"); open(FILE, ">>$file");
print FILE "REPLACE INTO `$table`
($columns)
VALUES\n";
for my $i ( 0 .. $#items ) { for my $i ( 0 .. $#items ) {
# $items[$i][0] Blank # $items[$i][0] Blank
# $items[$i][1] Commit-Hash # $items[$i][1] Commit-Hash
Expand All @@ -99,10 +105,10 @@ sub CreateSQL {
if ($items[$i][2]) { if ($items[$i][2]) {
$items[$i][2] = Date::Parse::str2time($items[$i][2]); $items[$i][2] = Date::Parse::str2time($items[$i][2]);
} }

push(@query, "('$items[$i][1]', '$items[$i][2]', '$items[$i][3]', '$items[$i][4]', '$items[$i][5]', '$items[$i][6]')");
print FILE "
INSERT IGNORE INTO `$table` VALUES ('$items[$i][1]', '$items[$i][2]', '$items[$i][3]', '$items[$i][4]', '$items[$i][5]', '$items[$i][6]');";
} }
$queries = join(",\n", @query);
print FILE "$queries;";
close(FILE); close(FILE);


return 1; return 1;
Expand Down

0 comments on commit 3c5518a

Please sign in to comment.