Beispiel #1
0
 def __appname__(self):
     """
         读取项目名称
     :return: 读取项目名称
     """
     conf = read_conf(self.app_dir)
     return conf['app']
Beispiel #2
0
    def syncdb(self):
        """
            sync database with model. only mysql.

        """
        conf = read_conf(self.app_dir)
        if 'db.url' not in conf:
            print 'This Application is not enable database or not mysql database.'
            sys.exit()
        db_url = conf['db.url']
        host_index = db_url.find(':', 12)
        host = db_url[13:host_index]
        port_idx = db_url.find('/', host_index)
        port = db_url[host_index + 1: port_idx]
        db = db_url[port_idx + 1: db_url.find('?', port_idx)]

        db_user = conf['db.username']
        db_pwd = conf['db.password']
        import MySQLdb

        db = MySQLdb.connect(host, db_user, db_pwd, db)
        cursor = db.cursor()
        sql = 'show tables'

        models = []

        try:
            cursor.execute(sql)
            results = cursor.fetchall()
            for row in results:
                # table name like ol_member_type
                real_table_name = row[0]
                row__find = real_table_name.find('_')
                table_name = 'tmp_' + real_table_name[row__find + 1: len(real_table_name)]
                # find table column name
                cursor.execute(
                    "select column_name from information_schema.columns where table_name='%s'" % real_table_name)
                columns = cursor.fetchall()
                column_list = []
                for column in columns:
                    column_list.append(column[0])

                models.append({'model': underline_to_camel(table_name).replace('tmp', ''), 'table': real_table_name,
                               'columns': ','.join(column_list)})

            cursor.close()
        except:
            print "error unkown tables;"
        finally:
            db.close()

        model_dir = os.path.join(self.app_dir, 'src', 'main', 'java', 'app', 'models')
        sql_conf_dir = os.path.join(self.app_dir, 'src', 'main', 'resources', 'sqlcnf')
        for _m in models:
            model_ = _m['model']
            model__lower = model_.lower()
            params = {'tableName': _m['table'], 'model': model_, 'lower_model': model__lower}
            file_content = self.__code_tpl__('code/model.java', params)
            if not os.path.exists(os.path.join(model_dir, model_ + '.java')):
                storage_file(model_dir, file_content, model_ + ".java")

            sql_params = {'model': model__lower, 'columns': _m['columns'], 'tableName': _m['table']}
            sql_conf_content = self.__code_tpl__('code/sql.xml', sql_params)
            if not os.path.exists(os.path.join(sql_conf_dir, model__lower + '-sql.xml')):
                storage_file(sql_conf_dir, sql_conf_content, model__lower + '-sql.xml')
        print 'sync db to model Success!'