null+****@clear*****
null+****@clear*****
2012年 3月 19日 (月) 18:20:25 JST
Kouhei Sutou 2012-03-19 18:20:25 +0900 (Mon, 19 Mar 2012) New Revision: f1f9f9ee1a8cdd3b144f222f531eaca4e2e55b26 Log: Add PathMapper.table_name() Modified files: lib/mrn_path_mapper.cpp lib/mrn_path_mapper.hpp test/unit/test_mrn_path_mapper.cpp Modified: lib/mrn_path_mapper.cpp (+27 -0) =================================================================== --- lib/mrn_path_mapper.cpp 2012-03-19 18:19:51 +0900 (8590bec) +++ lib/mrn_path_mapper.cpp 2012-03-19 18:20:25 +0900 (8cc047c) @@ -32,6 +32,7 @@ namespace mrn { : mysql_path_(mysql_path) { db_path_[0] = '\0'; db_name_[0] = '\0'; + table_name_[0] = '\0'; } /** @@ -82,4 +83,30 @@ namespace mrn { } return db_name_; } + + /** + * "./${db}/${table}" ==> "${table}" (with encoding first '_') + */ + const char* PathMapper::table_name() { + if (table_name_[0] != '\0') { + return table_name_; + } + + int len = strlen(mysql_path_); + int i = len, j = 0; + for (; mysql_path_[--i] != '/' ;) {} + if (mysql_path_[i + 1] == '_') { + table_name_[j++] = '@'; + table_name_[j++] = '0'; + table_name_[j++] = '0'; + table_name_[j++] = '5'; + table_name_[j++] = 'f'; + i++; + } + for (; i < len ;) { + table_name_[j++] = mysql_path_[++i]; + } + table_name_[j] = '\0'; + return table_name_; + } } Modified: lib/mrn_path_mapper.hpp (+2 -0) =================================================================== --- lib/mrn_path_mapper.hpp 2012-03-19 18:19:51 +0900 (d24a3af) +++ lib/mrn_path_mapper.hpp 2012-03-19 18:20:25 +0900 (2a492de) @@ -30,10 +30,12 @@ namespace mrn { PathMapper(const char* mysql_path); const char* db_path(); const char* db_name(); + const char* table_name(); private: const char* mysql_path_; char db_path_[MRN_MAX_PATH_SIZE]; char db_name_[MRN_MAX_PATH_SIZE]; + char table_name_[MRN_MAX_PATH_SIZE]; }; } Modified: test/unit/test_mrn_path_mapper.cpp (+17 -0) =================================================================== --- test/unit/test_mrn_path_mapper.cpp 2012-03-19 18:19:51 +0900 (91ed87a) +++ test/unit/test_mrn_path_mapper.cpp 2012-03-19 18:20:25 +0900 (007f988) @@ -58,5 +58,22 @@ namespace test_mrn_path_mapper { mapper.db_name()); } } + + namespace table_name { + void test_normal_table() { + mrn::PathMapper mapper("./db/table"); + cppcut_assert_equal("table", mapper.table_name()); + } + + void test_temporary_table() { + mrn::PathMapper mapper("/tmp/mysqld.1/#sql27c5_1_0"); + cppcut_assert_equal("#sql27c5_1_0", mapper.table_name()); + } + + void test_underscore_start_table() { + mrn::PathMapper mapper("./db/_table"); + cppcut_assert_equal("@005ftable", mapper.table_name()); + } + } }