Skip to content

Commit f26a7dd

Browse files
authored
Merge pull request #30 from thombashi/develop
Develop
2 parents 10774c3 + f1488e4 commit f26a7dd

File tree

5 files changed

+80
-39
lines changed

5 files changed

+80
-39
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
setuptools.setup(
2626
name="SimpleSQLite",
27-
version="0.3.4",
27+
version="0.3.5",
2828
url="https://github.com/thombashi/SimpleSQLite",
2929
bugtrack_url="https://github.com/thombashi/SimpleSQLite/issues",
3030

simplesqlite/core.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -893,15 +893,23 @@ def create_index(self, table_name, attribute_name):
893893

894894
index_name = "%s_%s_index" % (
895895
SqlQuery.sanitize(table_name), SqlQuery.sanitize(attribute_name))
896-
query = "CREATE INDEX IF NOT EXISTS %s ON %s('%s')" % (
897-
index_name, SqlQuery.to_table_str(table_name), attribute_name)
896+
if attribute_name.find("'") != -1:
897+
query_format = 'CREATE INDEX IF NOT EXISTS %s ON %s("%s")'
898+
else:
899+
query_format = "CREATE INDEX IF NOT EXISTS %s ON %s('%s')"
900+
901+
query = query_format % (
902+
index_name,
903+
SqlQuery.to_table_str(table_name),
904+
attribute_name)
898905
self.execute_query(query, logging.getLogger().findCaller())
899906

900907
def create_index_list(self, table_name, attribute_name_list):
901908
"""
902909
:param str table_name: Table name that exists attribute.
903910
:param list attribute_name_list:
904911
List of attribute names to create indices.
912+
Ignore attributes that not existing in the table.
905913
906914
.. seealso:: :py:meth:`.create_index`
907915
"""
@@ -911,7 +919,10 @@ def create_index_list(self, table_name, attribute_name_list):
911919
if dataproperty.is_empty_list_or_tuple(attribute_name_list):
912920
return
913921

914-
for attribute in attribute_name_list:
922+
table_attr_set = set(self.get_attribute_name_list(table_name))
923+
index_attr_set = set(attribute_name_list)
924+
925+
for attribute in list(table_attr_set.intersection(index_attr_set)):
915926
self.create_index(table_name, attribute)
916927

917928
def create_table_with_data(
@@ -947,19 +958,11 @@ def create_table_with_data(
947958
attribute_name_list, data_matrix)
948959
self.__verify_value_matrix(attribute_name_list, data_matrix)
949960

950-
strip_index_attribute_list = list(
951-
set(attribute_name_list).intersection(set(index_attribute_list)))
952-
attr_description_list = []
953-
954-
for col, value_type in sorted(
955-
six.iteritems(self.__get_column_valuetype(data_matrix))):
956-
attr_name = attribute_name_list[col]
957-
attr_description_list.append(
958-
"'%s' %s" % (attr_name, value_type))
959-
960-
self.create_table(table_name, attr_description_list)
961+
self.create_table(
962+
table_name,
963+
self.__get_attr_desc_list(attribute_name_list, data_matrix))
961964
self.insert_many(table_name, data_matrix)
962-
self.create_index_list(table_name, strip_index_attribute_list)
965+
self.create_index_list(table_name, index_attribute_list)
963966
self.commit()
964967

965968
def create_table_from_tabledata(self, tabledata):
@@ -1180,6 +1183,20 @@ def __initialize_connection(self):
11801183
self.__dict_query_count = {}
11811184
self.__dict_query_totalexectime = {}
11821185

1186+
def __get_attr_desc_list(self, attr_name_list, data_matrix):
1187+
attr_description_list = []
1188+
for col, value_type in sorted(
1189+
six.iteritems(self.__get_column_valuetype(data_matrix))):
1190+
attr_name = attr_name_list[col]
1191+
if attr_name.find("'") != -1:
1192+
desc_format = '"%s" %s'
1193+
else:
1194+
desc_format = "'%s' %s"
1195+
attr_description_list.append(
1196+
desc_format % (attr_name, value_type))
1197+
1198+
return attr_description_list
1199+
11831200
def validate_access_permission(self, valid_permission_list):
11841201
"""
11851202
:param valid_permission_list:

simplesqlite/sqlquery.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ class SqlQuery:
2121
Support class for making SQLite query.
2222
"""
2323

24-
__RE_SANITIZE = re.compile("[%s]" % (re.escape("%/()[]<>.:;'!\# -+=\n\r")))
24+
__RE_SANITIZE = re.compile(
25+
"[%s]" % (re.escape("%/()[]<>.:;'\"!\# -+=\n\r")))
2526
__RE_TABLE_STR = re.compile("[%s]" % (re.escape("%()-+/.")))
26-
__RE_TO_ATTR_STR = re.compile("[%s0-9\s#]" % (re.escape("[%()-+/.]")))
27+
__RE_BRACKET = re.compile("[%s]" % (re.escape("[]")))
28+
__RE_TO_ATTR_STR = re.compile("[%s0-9\s#]" % (re.escape("%()-+/.'\"")))
2729
__RE_SPACE = re.compile("[\s]+")
2830

2931
__VALID_WHERE_OPERATION_LIST = [
@@ -100,7 +102,9 @@ def to_attr_str(cls, name, operation_query=""):
100102
'SUM(key)'
101103
"""
102104

103-
if cls.__RE_TO_ATTR_STR.search(name):
105+
if cls.__RE_BRACKET.search(name):
106+
sql_name = '"%s"' % (name)
107+
elif cls.__RE_TO_ATTR_STR.search(name):
104108
sql_name = "[%s]" % (name)
105109
elif name == "join":
106110
sql_name = "[%s]" % (name)

test/test_simplesqlite.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -668,35 +668,56 @@ def test_null(self, con_null):
668668

669669
class Test_SimpleSQLite_create_table_with_data:
670670

671-
@pytest.mark.parametrize(["data_matrix"], [
671+
@pytest.mark.parametrize(
672+
["attr_name_list", "data_matrix", "index_attr_list"],
672673
[
673674
[
674-
[1, 4, "a"],
675-
[2, 2.1, "bb"],
676-
[3, 120.9, "ccc"],
675+
["attr_a", "attr_b", "attr_c"],
676+
[
677+
[1, 4, "a"],
678+
[2, 2.1, "bb"],
679+
[3, 120.9, "ccc"],
680+
],
681+
["attr_a"],
682+
],
683+
[
684+
["attr_a", "attr_b", "attr_c"],
685+
[
686+
{"attr_a": 1, "attr_b": 4, "attr_c": "a"},
687+
{"attr_a": 2, "attr_b": 2.1, "attr_c": "bb"},
688+
{"attr_a": 3, "attr_b": 120.9, "attr_c": "ccc"},
689+
],
690+
[
691+
"not_exist_attr_0",
692+
"attr_a", "attr_b", "attr_c",
693+
"not_exist_attr_1",
694+
],
677695
],
678-
],
679-
[
680696
[
681-
{"attr_a": 1, "attr_b": 4, "attr_c": "a"},
682-
{"attr_a": 2, "attr_b": 2.1, "attr_c": "bb"},
683-
{"attr_a": 3, "attr_b": 120.9, "attr_c": "ccc"},
697+
["attr'a", 'attr"b', "attr'c[%]", "attr($)"],
698+
[
699+
[1, 4, "a", None],
700+
[2, 2.1, "bb", None],
701+
[2, 2.1, "bb", None],
702+
],
703+
["attr'a", 'attr"b', "attr[%]"],
684704
],
685-
],
686-
])
687-
def test_normal(self, tmpdir, data_matrix):
705+
]
706+
)
707+
def test_normal(
708+
self, tmpdir, attr_name_list, data_matrix, index_attr_list):
688709
p = tmpdir.join("tmp.db")
689710
con = SimpleSQLite(str(p), "w")
690711
table_name = TEST_TABLE_NAME
691-
attribute_name_list = ["attr_a", "attr_b", "attr_c"]
692-
index_attribute_list = ["attr_a"]
693712

694713
con.create_table_with_data(
695-
table_name, attribute_name_list, data_matrix, index_attribute_list)
714+
table_name, attr_name_list, data_matrix, index_attr_list)
696715
con.commit()
697716

698717
# check data ---
699-
result = con.select(select="*", table_name=table_name)
718+
result = con.select(
719+
select=",".join(SqlQuery.to_attr_str_list(attr_name_list)),
720+
table_name=table_name)
700721
result_matrix = result.fetchall()
701722
assert len(result_matrix) == 3
702723

test/test_sqlquery.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import os
99
import re
10+
import string
1011

1112
import pytest
1213

@@ -20,7 +21,7 @@
2021
class Test_SqlQuery_sanitize:
2122
SANITIZE_CHAR_LIST = [
2223
"%", "/", "(", ")", "[", "]", "<", ">", ".", ";",
23-
"'", "!", "\\", "#", " ", "-", "+", "=", "\n"
24+
"'", '"', "!", "\\", "#", " ", "-", "+", "=", "\n"
2425
]
2526

2627
@pytest.mark.parametrize(
@@ -85,9 +86,7 @@ class Test_SqlQuery_to_attr_str:
8586
["attr_a", True, "attr_a"],
8687
] + [
8788
["te%sst" % (re.escape(c)), None, "[te%sst]" % (re.escape(c))]
88-
for c in [
89-
"%", "(", ")", ".", " ", "-", "+", "#"
90-
] + [str(n) for n in range(10)]
89+
for c in string.digits + "%(). -+#'\""
9190
]
9291
)
9392
def test_normal(self, value, operation, expected):

0 commit comments

Comments
 (0)