Ejemplo n.º 1
0
def build_sql_by_tmpl(source_file,
                      template,
                      single_file=False,
                      model=user_model):
    """
    """
    if single_file == True:
        template = source_file
        ignore_lines = 'IGNORE 1 LINES'
    else:
        ignore_lines = ''

    with open(template) as f:
        sep, columns = preConstruct(f.readline())

    if sep == 'SPACE':
        sep = ' '
    columns = [c.strip() for c in columns]
    if not check_col_name(columns): return False

    col_line = build_col_line(columns)
    table_name = model.__tablename__

    sql_query = """LOAD DATA LOCAL INFILE '{0}'
INTO TABLE {1}
FIELDS TERMINATED BY '{2}'
OPTIONALLY ENCLOSED BY '\\''
LINES TERMINATED BY '\\r\\n'
{3}
({4});
""".format(source_file, table_name, sep, ignore_lines, col_line)
    return sql_query
Ejemplo n.º 2
0
def gen_parse_class(f_name):
    """
    """
    # Read the first line
    with open(f_name) as f:
        sep, columns = preConstruct(f.readline())

    if sep == 'SPACE':
        sep = ' '
    columns = [c.strip() for c in columns]

    # check column name
    if not check_col_name(columns): raise Exception('Invalid column name!')

    # inner define function
    def parse_line_to_dic(self, line):
        """
        The generated class __init__()
        """
        row_elements = line.split(sep)
        row_elements = [r.strip() for r in row_elements]
        kargs = {}
        for ele_i in range(len(columns)):
            try:
                if columns[ele_i] == 'ignore':
                    pass
                else:
                    kargs[columns[ele_i]] = row_elements[ele_i]
            except Exception, e:
                sys.stdout.write(e)
        # do the init
        super(spec_user_model, self).__init__(**kargs)
Ejemplo n.º 3
0
def gen_parse_class(f_name):
    """
    """
    # Read the first line
    with open(f_name) as f:
        sep,columns = preConstruct(f.readline())

    if sep == 'SPACE':
        sep = ' '
    columns = [c.strip() for c in columns]

    # check column name
    if not check_col_name(columns): raise Exception('Invalid column name!')

    # inner define function
    def parse_line_to_dic(self,line):
        """
        The generated class __init__()
        """
        row_elements = line.split(sep)
        row_elements = [r.strip() for r in row_elements]
        kargs = {}
        for ele_i in range(len(columns)):
            try:
                if columns[ele_i] == 'ignore':
                    pass
                else:
                    kargs[columns[ele_i]] = row_elements[ele_i]
            except Exception,e:
                sys.stdout.write(e)
        # do the init
        super(spec_user_model,self).__init__(**kargs)
def build_sql_by_tmpl(source_file,template,single_file=False,model=user_model):
    """
    """
    if single_file == True:
        template = source_file
        ignore_lines = 'IGNORE 1 LINES'
    else:
        ignore_lines = ''

    with open(template) as f:
        sep,columns = preConstruct(f.readline())

    if sep == 'SPACE':
        sep = ' '
    columns = [c.strip() for c in columns]
    if not check_col_name(columns): return False

    col_line = build_col_line(columns)
    table_name = model.__tablename__

    sql_query = """LOAD DATA LOCAL INFILE '{0}'
INTO TABLE {1}
FIELDS TERMINATED BY '{2}'
OPTIONALLY ENCLOSED BY '\\''
LINES TERMINATED BY '\\r\\n'
{3}
({4});
""".format(
        source_file,
        table_name,
        sep,
        ignore_lines,
        col_line
    )
    return sql_query