예제 #1
0
 def test_save_changes_to_csv(self):
     test_tile = self.csv_file + '_test.csv'
     copyfile(self.csv_file, test_tile)
     db = CSVDB(test_tile)
     db.print_sql("update personnes_csv_test set id = 0")
     db.print_sql("commit")
     time.sleep(0.5)
     db = CSVDB(test_tile)
     result = db.execute_sql("select distinct id from personnes_csv_test")
     self.assertEqual(result, [['id'], (0, )])
예제 #2
0
def main(root_dir=None):
    db = CSVDB(root_dir)
    populate_table_list(db)
    session = PromptSession(lexer=PygmentsLexer(SqlLexer),
                            completer=WordCompleter(sql_csv_keywords,
                                                    ignore_case=True),
                            style=style,
                            history=FileHistory('history.txt'),
                            auto_suggest=AutoSuggestFromHistory())

    while True:
        messages = []
        try:
            cmd = session.prompt("SQLCSV> ")
        except KeyboardInterrupt:
            continue  # Control-C pressed. Try again.
        except EOFError:
            break  # Control-D pressed.
        try:
            messages = execute_command(cmd, db)
            # add potential new table to the auto complete
            for table in db.table_list:
                if table.name not in sql_csv_keywords:
                    sql_csv_keywords.append(table.name)
        except Exception as e:
            print(repr(e))
        finally:
            for message in messages:
                print(message)
예제 #3
0
def execute_script(root_dir, sql_script):
    db = CSVDB(root_dir)
    populate_table_list(db)
    script_file = open(sql_script, 'r')
    command = ''
    for line in script_file.readlines():
        command += line
        if line.strip().endswith(';'):
            print("Executing: " + command)
            execute_command(command, db)
            command = ''
    script_file.close()
    execute_command('exit', db)
예제 #4
0
 def setUp(self):
     self.csv_file = 'data/rh/personnes.csv'
     self.root_dir = 'data'
     self.db = CSVDB(self.root_dir)
예제 #5
0
 def test_execute_sql(self):
     db = CSVDB(self.root_dir)
     result = db.execute_sql("select 1")
     self.assertEqual(result, [['1'], (1, )])
예제 #6
0
 def test_get_table_name_list(self):
     db = CSVDB(self.root_dir)
     table_list = db.get_table_name_list()
     self.assertTrue('rh_personnes' in table_list)
예제 #7
0
 def test_desc(self):
     db = CSVDB(self.root_dir)
     result = db.desc('rh_personnes')
     self.assertEqual(result, '')
예제 #8
0
 def test_print_sql(self):
     db = CSVDB(self.root_dir)
     result = db.print_sql("select * from rh_personnes")
     self.assertEqual(result, '')
예제 #9
0
 def test_load(self):
     db = CSVDB(self.root_dir)
예제 #10
0
 def test_load_file(self):
     db = CSVDB(self.csv_file)
     type = get_file_type(self.csv_file)
     db.load_file(type, self.csv_file, self.root_dir)