Ejemplo n.º 1
0
def write_to_ts(args, database):
    """
    Writes the database directly to ThoughtSpot.
    :param args:  The command line arguments.
    :param database: The database to write.
    :type database: Database
    :return: None
    """
    rtql = RemoteTQL(hostname=args.to_ts,
                     username=args.username,
                     password=args.password)

    tempfile = f"{database.database_name}.tmp"

    writer = TQLWriter(args.uppercase, args.lowercase, args.camelcase,
                       args.create_db)
    writer.write_tql(database, tempfile)

    # The parser expects a file, so create a temp file, parse, then delete.
    data = ""
    with open(tempfile, "r") as infile:
        for line in infile:
            data += line
    response = rtql.run_tql_command(data)
    os.remove(tempfile)
    print(response)
Ejemplo n.º 2
0
def read_from_ts(args):
    """
    Reads the database (from args) from TQL remotely.
    :param args: The argument list.  Must have the host, database and possibly user/password.
    :return: A database that was read.
    :rtype: Database
    """
    rtql = RemoteTQL(hostname=args.ts_ip, username=args.username, password=args.password)
    out = rtql.run_tql_command(f"script database {args.database};")

    # The parser expects a file, so create a temp file, parse, then delete.
    filename = f"{args.database}.tmp"
    with open(filename, "w") as outfile:
        for line in out:
            outfile.write(line + "\n")

    parser = DDLParser(database_name=args.database)
    database = parser.parse_ddl(filename=filename)
    os.remove(filename)

    return database
Ejemplo n.º 3
0
class TestRemoteTQL(unittest.TestCase):
    """Test case for testing remote TQL."""
    def setUp(self) -> None:
        """Returns a new TQL shell."""
        self.rtql = RemoteTQL(hostname=TEST_HOSTNAME,
                              username=TEST_USERNAME,
                              password=TEST_PASSWORD)

    def tearDown(self) -> None:
        """Returns a new TQL shell."""
        del self.rtql

    def test_get_databases(self):
        databases = self.rtql.get_databases()

        # check for a couple of known DBs.  Not exhaustive.
        self.assertIn("thoughtspot_internal_stats", databases)
        self.assertIn("thoughtspot_internal", databases)

    def test_get_data(self):
        """Tests making a data query."""
        self.rtql.run_tql_command("CREATE DATABASE foo;")
        self.rtql.run_tql_command("USE foo;")
        self.rtql.run_tql_command(
            "CREATE TABLE foo (col1 int, col2 varchar(0));")
        self.rtql.run_tql_command("DELETE FROM foo;")  # make sure empty.
        for row in range(1, 4):  # creates three rows 1-3
            self.rtql.run_tql_command(
                f"INSERT INTO foo VALUES ({row}, 'value_{row}');")

        table = self.rtql.execute_tql_query("SELECT * FROM foo;")

        self.assertEqual(2, table.nbr_columns())
        self.assertEqual(3, table.nbr_rows())

        self.rtql.run_tql_command("DROP DATABASE foo;")