Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openstructure
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor 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
schwede
openstructure
Commits
a3f3f3ab
Commit
a3f3f3ab
authored
5 years ago
by
Gerardo Tauriello
Browse files
Options
Downloads
Patches
Plain Diff
SCHWED-4246: Add mol.QueryQuoteName function.
parent
a58471ff
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
modules/mol/base/pymod/export_query.cc
+1
-1
1 addition, 1 deletion
modules/mol/base/pymod/export_query.cc
modules/mol/base/src/query.hh
+19
-0
19 additions, 0 deletions
modules/mol/base/src/query.hh
modules/mol/base/tests/test_query.cc
+45
-1
45 additions, 1 deletion
modules/mol/base/tests/test_query.cc
with
65 additions
and
2 deletions
modules/mol/base/pymod/export_query.cc
+
1
−
1
View file @
a3f3f3ab
...
...
@@ -68,5 +68,5 @@ void export_Query()
return_value_policy
<
copy_const_reference
>
())
;
def
(
"QueryQuoteName"
,
&
QueryQuoteName
);
}
This diff is collapsed.
Click to expand it.
modules/mol/base/src/query.hh
+
19
−
0
View file @
a3f3f3ab
...
...
@@ -117,6 +117,25 @@ private:
impl
::
QueryImplP
impl_
;
};
// inlined helper function to quote strings for use in queries (e.g. cname=..).
// throws Error if string cannot be quoted
inline
String
DLLEXPORT_OST_MOL
QueryQuoteName
(
const
String
&
name
)
{
// check what quotation marks to use
char
quote
=
'\''
;
if
(
name
.
find
(
'\''
)
!=
String
::
npos
)
{
if
(
name
.
find
(
'"'
)
!=
String
::
npos
)
{
throw
Error
(
"Cannot quote chain name "
+
name
+
" because it contains '"
" and
\"
in its name."
);
}
quote
=
'"'
;
}
// check problematic \ at end (escapes quotation mark and breaks logic)
if
(
name
[
name
.
length
()
-
1
]
==
'\\'
)
{
throw
Error
(
"Cannot quote chain name "
+
name
+
"because it ends in
\\
."
);
}
return
quote
+
name
+
quote
;
}
}}
// ns
#endif
...
...
This diff is collapsed.
Click to expand it.
modules/mol/base/tests/test_query.cc
+
45
−
1
View file @
a3f3f3ab
...
...
@@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(test_query_throw)
BOOST_CHECK_NO_THROW
(
e
.
Select
(
"gcnotsetprop:0=1"
));
}
BOOST_AUTO_TEST_CASE
(
test_glob
)
BOOST_AUTO_TEST_CASE
(
test_glob
)
{
EntityHandle
e
=
make_query_test_entity
();
ensure_counts
(
e
,
"rname=MET and aname=C*"
,
1
,
1
,
5
);
...
...
@@ -299,4 +299,48 @@ BOOST_AUTO_TEST_CASE(test_glob)
ensure_counts
(
e
,
"rname=LEU and aname=?D?"
,
1
,
1
,
2
);
}
BOOST_AUTO_TEST_CASE
(
test_quoting
)
{
// possible letters taken from mmCIF dictionary
// -> http://mmcif.wwpdb.org/dictionaries/mmcif_pdbx_v50.dic/Items/_atom_site.label_asym_id.html
// not ok for us: '\' alone
String
single_letters
=
" ][_,.;:
\"
&<>()/{}'`~!@#$\%|+-"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefgh"
"ijklmnopqrstuvwxyz"
;
// put into vector
std
::
vector
<
String
>
chain_names
;
for
(
size_t
i
=
0
;
i
<
single_letters
.
length
();
++
i
)
{
chain_names
.
push_back
(
String
(
1
,
single_letters
[
i
]));
}
// add some multi letter names and empty chain name too
// not ok: mixing ' and ", '\' at end
chain_names
.
push_back
(
"
\\
][_,.;:&<>()/{}'`~!@#$\%|+-"
);
chain_names
.
push_back
(
"ABCDEFGHIJKLMNOPQRSTU'VWXYZ0123456789abcdefgh"
);
chain_names
.
push_back
(
"ijklmno
\"
pqrstuvwxyz"
);
chain_names
.
push_back
(
""
);
// setup entity
EntityHandle
ent
=
CreateEntity
();
XCSEditor
edi
=
ent
.
EditXCS
();
for
(
size_t
i
=
0
;
i
<
chain_names
.
size
();
++
i
)
{
edi
.
InsertChain
(
chain_names
[
i
]);
}
// test quoting
String
query_all
=
"cname="
;
for
(
size_t
i
=
0
;
i
<
chain_names
.
size
();
++
i
)
{
const
String
quoted_name
=
QueryQuoteName
(
chain_names
[
i
]);
BOOST_CHECK_EQUAL
(
ent
.
Select
(
"cname="
+
quoted_name
).
GetChainCount
(),
1
);
query_all
+=
quoted_name
;
if
(
i
!=
chain_names
.
size
()
-
1
)
query_all
+=
","
;
}
BOOST_CHECK_EQUAL
(
ent
.
Select
(
query_all
).
GetChainCount
(),
int
(
chain_names
.
size
()));
// note: quoting * keeps it as wild card!
BOOST_CHECK_EQUAL
(
ent
.
Select
(
"cname="
+
QueryQuoteName
(
"*"
)).
GetChainCount
(),
int
(
chain_names
.
size
()));
// expected failures (mixing ' and ", '\' at end)
BOOST_CHECK_THROW
(
QueryQuoteName
(
"'
\"
"
),
Error
);
BOOST_CHECK_THROW
(
QueryQuoteName
(
"
\\
"
),
Error
);
BOOST_CHECK_THROW
(
QueryQuoteName
(
"a
\\
"
),
Error
);
}
BOOST_AUTO_TEST_SUITE_END
();
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