Skip to content

Commit

Permalink
SQL output: add foreign key relating metadata to t, #761
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Aug 31, 2023
1 parent 2c0543f commit 3e27d4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
25 changes: 18 additions & 7 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -3937,6 +3937,7 @@ sub generate_sql { # {{{1
$schema = "
CREATE TABLE metadata
(
id INTEGER PRIMARY KEY,
timestamp TIMESTAMP,
project VARCHAR2(500 CHAR),
elapsed_s NUMBER(10, 6)
Expand All @@ -3945,6 +3946,7 @@ CREATE TABLE metadata

CREATE TABLE t
(
id INTEGER ,
project VARCHAR2(500 CHAR),
language VARCHAR2(500 CHAR),
file_fullname VARCHAR2(500 CHAR),
Expand All @@ -3953,18 +3955,22 @@ CREATE TABLE t
nblank INTEGER,
ncomment INTEGER,
ncode INTEGER,
nscaled NUMBER(10, 6)
nscaled NUMBER(10, 6),
FOREIGN KEY (id)
REFERENCES metadata (id)
)
/

";
} else {
$schema = "
create table metadata ( -- $URL v $VERSION
id integer primary key,
timestamp varchar(500),
Project varchar(500),
elapsed_s real);
create table t (
id integer ,
Project varchar(500) ,
Language varchar(500) ,
File varchar(500) ,
Expand All @@ -3973,7 +3979,9 @@ create table t (
nBlank integer ,
nComment integer ,
nCode integer ,
nScaled real );
nScaled real ,
foreign key (id)
references metadata (id));
";
}
$opt_sql = "-" if $opt_sql eq "1";
Expand All @@ -3995,18 +4003,20 @@ create table t (
}
print $fh $schema unless defined $opt_sql_append;

my $id = int(time());
my $insert_into_t = "insert into t ";
if ($opt_sql_style eq "oracle") {
printf $fh "insert into metadata values(TO_TIMESTAMP('%s','yyyy-mm-dd hh24:mi:ss'), '%s', %f);\n",
printf $fh "insert into metadata values(%d, TO_TIMESTAMP('%s','yyyy-mm-dd hh24:mi:ss'), '%s', %f);\n",
$id,
strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
$opt_sql_project, $elapsed_sec;
} elsif ($opt_sql_style eq "named_columns") {
print $fh "begin transaction;\n";
$insert_into_t .= "( Project, Language, File, File_dirname, File_basename, nBlank, nComment, nCode, nScaled )";
$insert_into_t .= "( Project, Language, File, File_dirname, File_basename, nBlank, nComment, nCode, nScaled, id )";
} else {
print $fh "begin transaction;\n";
printf $fh "insert into metadata values('%s', '%s', %f);\n",
strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
printf $fh "insert into metadata values(%d, '%s', '%s', %f);\n",
$id, strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
$opt_sql_project, $elapsed_sec;
}

Expand All @@ -4023,8 +4033,9 @@ create table t (
$clean_filename =~ s/\'/''/g; # double embedded single quotes
# to escape them

printf $fh "$insert_into_t values('%s', '%s', '%s', '%s', '%s', " .
printf $fh "$insert_into_t values(%d, '%s', '%s', '%s', '%s', '%s', " .
"%d, %d, %d, %f);\n",
$id ,
$opt_sql_project ,
$language ,
$clean_filename ,
Expand Down
25 changes: 18 additions & 7 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,7 @@ sub generate_sql { # {{{1
$schema = "
CREATE TABLE metadata
(
id INTEGER PRIMARY KEY,
timestamp TIMESTAMP,
project VARCHAR2(500 CHAR),
elapsed_s NUMBER(10, 6)
Expand All @@ -3960,6 +3961,7 @@ CREATE TABLE metadata

CREATE TABLE t
(
id INTEGER ,
project VARCHAR2(500 CHAR),
language VARCHAR2(500 CHAR),
file_fullname VARCHAR2(500 CHAR),
Expand All @@ -3968,18 +3970,22 @@ CREATE TABLE t
nblank INTEGER,
ncomment INTEGER,
ncode INTEGER,
nscaled NUMBER(10, 6)
nscaled NUMBER(10, 6),
FOREIGN KEY (id)
REFERENCES metadata (id)
)
/

";
} else {
$schema = "
create table metadata ( -- $URL v $VERSION
id integer primary key,
timestamp varchar(500),
Project varchar(500),
elapsed_s real);
create table t (
id integer ,
Project varchar(500) ,
Language varchar(500) ,
File varchar(500) ,
Expand All @@ -3988,7 +3994,9 @@ create table t (
nBlank integer ,
nComment integer ,
nCode integer ,
nScaled real );
nScaled real ,
foreign key (id)
references metadata (id));
";
}
$opt_sql = "-" if $opt_sql eq "1";
Expand All @@ -4010,18 +4018,20 @@ create table t (
}
print $fh $schema unless defined $opt_sql_append;

my $id = int(time());
my $insert_into_t = "insert into t ";
if ($opt_sql_style eq "oracle") {
printf $fh "insert into metadata values(TO_TIMESTAMP('%s','yyyy-mm-dd hh24:mi:ss'), '%s', %f);\n",
printf $fh "insert into metadata values(%d, TO_TIMESTAMP('%s','yyyy-mm-dd hh24:mi:ss'), '%s', %f);\n",
$id,
strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
$opt_sql_project, $elapsed_sec;
} elsif ($opt_sql_style eq "named_columns") {
print $fh "begin transaction;\n";
$insert_into_t .= "( Project, Language, File, File_dirname, File_basename, nBlank, nComment, nCode, nScaled )";
$insert_into_t .= "( Project, Language, File, File_dirname, File_basename, nBlank, nComment, nCode, nScaled, id )";
} else {
print $fh "begin transaction;\n";
printf $fh "insert into metadata values('%s', '%s', %f);\n",
strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
printf $fh "insert into metadata values(%d, '%s', '%s', %f);\n",
$id, strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
$opt_sql_project, $elapsed_sec;
}

Expand All @@ -4038,8 +4048,9 @@ create table t (
$clean_filename =~ s/\'/''/g; # double embedded single quotes
# to escape them

printf $fh "$insert_into_t values('%s', '%s', '%s', '%s', '%s', " .
printf $fh "$insert_into_t values(%d, '%s', '%s', '%s', '%s', '%s', " .
"%d, %d, %d, %f);\n",
$id ,
$opt_sql_project ,
$language ,
$clean_filename ,
Expand Down

0 comments on commit 3e27d4f

Please sign in to comment.