示例#1
0
def main():
    table_name = 'import_' + str(
        datetime.datetime.now().strftime("%Y_%m_%d_%H_%M"))

    if len(sys.argv) == 1:
        input_, output_ = fileinput.input(), sys.stdout
    elif len(sys.argv) == 2:
        table_name = sys.argv[1]
        input_, output_ = open(sys.argv[1]), sys.stdout
    elif len(sys.argv) == 3:
        table_name = sys.argv[2]
        input_, output_ = open(sys.argv[1]), open(sys.argv[2], 'w+')
    else:
        print("Usage: ./create_csv_table.py [input] [output]")
        sys.exit(1)

    matrix = read_input(input_).counts
    for row in matrix:
        if not row[1]:
            row[1] = 1

    columns = [Column(n, String(l)) for n, l in matrix]
    table = CreateTable(Table(table_name, MetaData(), *columns))

    output_.write(str(table.compile(dialect=mysql.dialect())))
示例#2
0
文件: carto.py 项目: chrislove/the-el
def create_table(logger, table_name, load_postgis, json_table_schema,
                 if_not_exists, indexes_fields, connection_string):
    if load_postgis:
        load_postgis_support()

    creds = re.match(carto_connection_string_regex, connection_string).groups()
    statement = CreateTable(get_table(table_name, json_table_schema))
    str_statement = statement.compile(dialect=postgresql.dialect())

    if if_not_exists:
        str_statement = str(str_statement).replace(
            'CREATE TABLE', 'CREATE TABLE IF NOT EXISTS')

    carto_sql_call(logger, creds, str_statement)

    check_table_sql = "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '{}');"
    response = carto_sql_call(logger, creds,
                              check_table_sql.format(table_name))
    exists = response['rows'][0]['exists']

    if not exists:
        message = '{} - Could not create table'.format(table_name)
        logger.error(message)
        raise Exception(message)

    if indexes_fields:
        create_indexes(creds, table_name, indexes_fields)
示例#3
0
def create_missing_table(tbl):
    """"""
    stmt = CreateTable(tbl.__table__)
    stmt = stmt.compile(dialect=LiteralDialect())
    stmt_str = stmt.__str__()
    stmt_end = stmt_str.rfind(')')
    stmt_str = '{});\n'.format(stmt_str[:stmt_end])
    sql = stmt_str.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS')
    return sql
示例#4
0
def table_ddl(tables, engine, drop=False):

    output = []

    for table in tables:

        if not drop:
            ddl = CreateTable(table)
        else:
            ddl = DropTable(table)

        output.append(str(ddl.compile(dialect=engine.dialect)).strip())
        output.append(';\n\n')

    return output
示例#5
0
def table_ddl(tables, engine, drop=False):

    output = []

    for table in tables:

        if not drop:
            ddl = CreateTable(table)
        else:
            ddl = DropTable(table)

        output.append(str(ddl.compile(dialect=engine.dialect)).strip())
        output.append(';\n\n')

    return output
示例#6
0
文件: carto.py 项目: matamehta/the-el
def create_table(table_name, load_postgis, json_table_schema, if_not_exists,
                 indexes_fields, connection_string):
    if load_postgis:
        load_postgis_support()

    creds = re.match(carto_connection_string_regex, connection_string).groups()
    statement = CreateTable(get_table(table_name, json_table_schema))
    str_statement = statement.compile(dialect=postgresql.dialect())

    if if_not_exists:
        str_statement = str(str_statement).replace(
            'CREATE TABLE', 'CREATE TABLE IF NOT EXISTS')

    carto_sql_call(creds, str_statement)

    if indexes_fields:
        create_indexes(creds, table_name, indexes_fields)
示例#7
0
    def get_pool():
        pool = yield from create_engine(dsn)

        connection = yield from pool.acquire()
        try:
            result = yield from connection.execute(
                'SELECT tablename FROM pg_tables '
                'WHERE schemaname=%s', ('public', ))
            existing_table_names = {name[0] for name in result}
            print('Existing tables:', existing_table_names)

            for name, table in tables.metadata.tables.items():
                if name not in existing_table_names:
                    create_statement = CreateTable(table)
                    print(create_statement.compile(dialect=dialect))
                    yield from connection.execute(create_statement)
        finally:
            connection.close()

        return pool
示例#8
0
    def get_pool():
        pool = yield from create_engine(dsn)

        connection = yield from pool.acquire()
        try:
            result = yield from connection.execute(
                'SELECT tablename FROM pg_tables '
                'WHERE schemaname=%s', ('public', ))
            existing_table_names = {name[0] for name in result}
            print('Existing tables:', existing_table_names)

            for name, table in tables.metadata.tables.items():
                if name not in existing_table_names:
                    create_statement = CreateTable(table)
                    print(create_statement.compile(dialect=dialect))
                    yield from connection.execute(create_statement)
        finally:
            connection.close()

        return pool
示例#9
0
    def run(self, input_args: Namespace):
        entity_name = input_args.entity_class[
            input_args.entity_class.rfind(".") + 1:]  # noqa: E203
        entity = getattr(importlib.import_module(input_args.entity_class),
                         entity_name)

        statement = CreateTable(getattr(entity, "__table__"))

        db_config = self._connections[input_args.connection_name]
        engine_factory = self.__engine_factory_resolver.resolve(db_config)
        engine = engine_factory.create(db_config)

        query = str(statement.compile(engine, dialect=engine.dialect))

        if input_args.force is True:
            engine.execute(sql_alchemy_text(query))

            self.__logger.warning("Table successfully created")
        else:
            print(query)
            self.__logger.warning(
                "Add --force option to execute the table creation query")
示例#10
0
def main():
    table_name = 'import'

    if len(sys.argv) == 1:
        input_, output_ = fileinput.input(), sys.stdout
    elif len(sys.argv) == 2:
        input_, output_ = open(sys.argv[1]), sys.stdout
    elif len(sys.argv) == 3:
        table_name = sys.argv[2]
        input_, output_ = open(sys.argv[1]), open(sys.argv[2], 'w+')
    else:
        print("Usage: ./create-csv-table.py [input] [output]")
        sys.exit(1)

    reader = csv.reader(input_)
    next(reader)  # discard "Field,Max Length"
    matrix = []
    for row in reader:
        matrix.append([row[0], int(row[1]) + 1])

    columns = [Column(n, String(l)) for n, l in matrix]
    table = CreateTable(Table(table_name, MetaData(), *columns))

    output_.write(str(table.compile(dialect=mysql.dialect())))
示例#11
0
def print_model_sql():
    created_table = CreateTable(TempMessage.__table__)
    tableCompiled = created_table.compile(engine)
    print(tableCompiled)