Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cta
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dCache
cta
Commits
21fd1064
Commit
21fd1064
authored
5 years ago
by
Victor Kotlyar
Committed by
Michael Davis
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add table names DB verification against schema for sqlite.
parent
ed63a0b4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
catalogue/SqliteCatalogueSchema.hpp
+58
-0
58 additions, 0 deletions
catalogue/SqliteCatalogueSchema.hpp
catalogue/VerifySchemaCmd.cpp
+38
-1
38 additions, 1 deletion
catalogue/VerifySchemaCmd.cpp
catalogue/VerifySchemaCmd.hpp
+11
-0
11 additions, 0 deletions
catalogue/VerifySchemaCmd.hpp
with
107 additions
and
1 deletion
catalogue/SqliteCatalogueSchema.hpp
+
58
−
0
View file @
21fd1064
...
...
@@ -18,7 +18,15 @@
#pragma once
#include
"common/utils/Regex.hpp"
#include
"common/utils/utils.hpp"
#include
"common/exception/Exception.hpp"
#include
<string>
#include
<list>
// TODO
#include
<iostream>
namespace
cta
{
namespace
catalogue
{
...
...
@@ -48,6 +56,56 @@ struct SqliteCatalogueSchema {
* The schema.
*/
const
std
::
string
sql
;
// TODO
std
::
list
<
std
::
string
>
getSchemaTableNames
()
const
{
std
::
list
<
std
::
string
>
schemaTables
;
std
::
string
::
size_type
searchPos
=
0
;
std
::
string
::
size_type
findResult
=
std
::
string
::
npos
;
try
{
while
(
std
::
string
::
npos
!=
(
findResult
=
sql
.
find
(
';'
,
searchPos
)))
{
// Calculate the length of the current statement without the trailing ';'
const
std
::
string
::
size_type
stmtLen
=
findResult
-
searchPos
;
const
std
::
string
sqlStmt
=
utils
::
trimString
(
sql
.
substr
(
searchPos
,
stmtLen
));
searchPos
=
findResult
+
1
;
if
(
0
<
sqlStmt
.
size
())
{
// Ignore empty statements
cta
::
utils
::
Regex
tableNamesRegex
(
"CREATE TABLE ([a-zA-Z_]+)"
);
auto
tableName
=
tableNamesRegex
.
exec
(
sqlStmt
);
if
(
2
==
tableName
.
size
())
{
schemaTables
.
push_back
(
tableName
[
1
].
c_str
());
}
}
}
}
catch
(
exception
::
Exception
&
ex
)
{
throw
exception
::
Exception
(
std
::
string
(
__FUNCTION__
)
+
" failed: "
+
ex
.
getMessage
().
str
());
}
return
schemaTables
;
}
// TODO
std
::
list
<
std
::
string
>
getSchemaIndexNames
()
{
std
::
list
<
std
::
string
>
schemaIndices
;
std
::
string
::
size_type
searchPos
=
0
;
std
::
string
::
size_type
findResult
=
std
::
string
::
npos
;
try
{
while
(
std
::
string
::
npos
!=
(
findResult
=
sql
.
find
(
';'
,
searchPos
)))
{
// Calculate the length of the current statement without the trailing ';'
const
std
::
string
::
size_type
stmtLen
=
findResult
-
searchPos
;
const
std
::
string
sqlStmt
=
utils
::
trimString
(
sql
.
substr
(
searchPos
,
stmtLen
));
searchPos
=
findResult
+
1
;
if
(
0
<
sqlStmt
.
size
())
{
// Ignore empty statements
cta
::
utils
::
Regex
tableIndicesRegex
(
"CREATE INDEX ([a-zA-Z_]+)"
);
auto
indexName
=
tableIndicesRegex
.
exec
(
sqlStmt
);
if
(
2
==
indexName
.
size
())
{
schemaIndices
.
push_back
(
indexName
[
1
].
c_str
());
}
}
}
}
catch
(
exception
::
Exception
&
ex
)
{
throw
exception
::
Exception
(
std
::
string
(
__FUNCTION__
)
+
" failed: "
+
ex
.
getMessage
().
str
());
}
return
schemaIndices
;
}
};
}
// namespace catalogue
...
...
This diff is collapsed.
Click to expand it.
catalogue/VerifySchemaCmd.cpp
+
38
−
1
View file @
21fd1064
...
...
@@ -27,6 +27,8 @@
#include
"rdbms/Login.hpp"
#include
"rdbms/AutocommitMode.hpp"
#include
<algorithm>
namespace
cta
{
namespace
catalogue
{
...
...
@@ -71,7 +73,15 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
case
rdbms
::
Login
::
DBTYPE_SQLITE
:
{
// TODO
//SqliteCatalogueSchema schema;
SqliteCatalogueSchema
schema
;
const
auto
schemaTableNames
=
schema
.
getSchemaTableNames
();
const
auto
dbTableNames
=
conn
.
getTableNames
();
std
::
cerr
<<
"error code: "
<<
static_cast
<
int
>
(
verifyTableNames
(
schemaTableNames
,
dbTableNames
))
<<
std
::
endl
;
const
auto
schemaIndexNames
=
schema
.
getSchemaIndexNames
();
for
(
auto
index
:
schemaIndexNames
)
{
std
::
cout
<<
index
<<
std
::
endl
;
}
//conn.executeNonQueries(schema.sql);
}
break
;
...
...
@@ -135,5 +145,32 @@ void VerifySchemaCmd::printUsage(std::ostream &os) {
VerifySchemaCmdLineArgs
::
printUsage
(
os
);
}
//------------------------------------------------------------------------------
// verifyTableNames
//------------------------------------------------------------------------------
VerifySchemaCmd
::
VerifyStatus
VerifySchemaCmd
::
verifyTableNames
(
const
std
::
list
<
std
::
string
>
&
schemaTableNames
,
const
std
::
list
<
std
::
string
>
&
dbTableNames
)
const
{
VerifyStatus
status
=
VerifyStatus
::
UNKNOWN
;
// check for schema tables
for
(
auto
&
tableName
:
schemaTableNames
)
{
const
bool
schemaTableIsNotInDb
=
dbTableNames
.
end
()
==
std
::
find
(
dbTableNames
.
begin
(),
dbTableNames
.
end
(),
tableName
);
if
(
schemaTableIsNotInDb
)
{
std
::
cerr
<<
"ERROR: the schema table "
<<
tableName
<<
" not found in the DB"
<<
std
::
endl
;
status
=
VerifyStatus
::
ERROR
;
}
}
// check for extra tables in DB
for
(
auto
&
tableName
:
dbTableNames
)
{
const
bool
dbTableIsNotInSchema
=
schemaTableNames
.
end
()
==
std
::
find
(
schemaTableNames
.
begin
(),
schemaTableNames
.
end
(),
tableName
);
if
(
dbTableIsNotInSchema
)
{
std
::
cerr
<<
"WARNING: the database table "
<<
tableName
<<
" not found in the schema"
<<
std
::
endl
;
if
(
VerifyStatus
::
ERROR
!=
status
)
{
status
=
VerifyStatus
::
WARNING
;
}
}
}
return
status
;
}
}
// namespace catalogue
}
// namespace cta
This diff is collapsed.
Click to expand it.
catalogue/VerifySchemaCmd.hpp
+
11
−
0
View file @
21fd1064
...
...
@@ -43,6 +43,8 @@ public:
* Destructor.
*/
~
VerifySchemaCmd
()
noexcept
;
enum
class
VerifyStatus
{
OK
,
WARNING
,
ERROR
,
UNKNOWN
};
private
:
...
...
@@ -71,6 +73,15 @@ private:
* @return True if the table exists.
*/
bool
tableExists
(
const
std
::
string
tableName
,
rdbms
::
Conn
&
conn
)
const
;
/**
* TODO
* @param
* @param
* @return
*/
VerifyStatus
verifyTableNames
(
const
std
::
list
<
std
::
string
>
&
schemaTableNames
,
const
std
::
list
<
std
::
string
>
&
dbTableNames
)
const
;
};
// class VerifySchemaCmd
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment