Browse Source

mysql获取表数据时,表头获取方法修改

liweiquan 2 years ago
parent
commit
25d8058a67
3 changed files with 11 additions and 9 deletions
  1. 6 4
      app/core/datasource/mysql.py
  2. 5 4
      app/routers/job_jdbc_datasource.py
  3. 0 1
      utils/__init__.py

+ 6 - 4
app/core/datasource/mysql.py

@@ -82,13 +82,15 @@ class MysqlDS(DataSourceBase):
 
 
 
 
     def get_preview_data(self, table_name, limit=100):
     def get_preview_data(self, table_name, limit=100):
-        sql1 = f'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA="{self.database_name}"  AND TABLE_NAME="{table_name}"'
+        # sql1 = f'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA="{self.database_name}"  AND TABLE_NAME="{table_name}"'
+        table_schema = self.get_table_schema(table_name)
         sql2 = f"SELECT * FROM {table_name} LIMIT {limit}"
         sql2 = f"SELECT * FROM {table_name} LIMIT {limit}"
-        res = self._execute_sql([sql1, sql2])
+        res = self._execute_sql([sql2])
         logger.info(res)
         logger.info(res)
         return {
         return {
-            'header': flat_map(lambda x: x, res[0]),
-            'content': res[1]
+            # 'header': flat_map(lambda x: x, res[0]),
+            'header': [str(column).split(':')[1] for column in table_schema],
+            'content': res[0]
         }
         }
         # db_connection = create_engine(self.connection_str)
         # db_connection = create_engine(self.connection_str)
         # df = pd.read_sql(sql, con=db_connection)
         # df = pd.read_sql(sql, con=db_connection)

+ 5 - 4
app/routers/job_jdbc_datasource.py

@@ -4,6 +4,7 @@ from fastapi import APIRouter
 from fastapi import Depends
 from fastapi import Depends
 from sqlalchemy.orm import Session
 from sqlalchemy.orm import Session
 from app import schemas
 from app import schemas
+from app.common.decorators import verify_super_admin
 
 
 import app.crud as crud
 import app.crud as crud
 from utils.sx_time import sxtimeit
 from utils.sx_time import sxtimeit
@@ -20,7 +21,7 @@ router = APIRouter(
 )
 )
 
 
 
 
-@router.post("/test")
+@router.post("/test", dependencies=[Depends(verify_super_admin)])
 @web_try()
 @web_try()
 @sxtimeit
 @sxtimeit
 def test_datasource_connection(ds: schemas.JobJdbcDatasourceCreate, db: Session = Depends(get_db)):
 def test_datasource_connection(ds: schemas.JobJdbcDatasourceCreate, db: Session = Depends(get_db)):
@@ -46,7 +47,7 @@ def get_table_names(ds_id: int, db: Session = Depends(get_db)):
 def get_table_schema(ds_id: int, table_name: str, db: Session = Depends(get_db)):
 def get_table_schema(ds_id: int, table_name: str, db: Session = Depends(get_db)):
     return crud.get_table_schema(db, ds_id, table_name)
     return crud.get_table_schema(db, ds_id, table_name)
 
 
-@router.post("/")
+@router.post("/", dependencies=[Depends(verify_super_admin)])
 @web_try()
 @web_try()
 @sxtimeit
 @sxtimeit
 def create_datasource(ds: schemas.JobJdbcDatasourceCreate, db: Session = Depends(get_db)):
 def create_datasource(ds: schemas.JobJdbcDatasourceCreate, db: Session = Depends(get_db)):
@@ -68,13 +69,13 @@ def get_datasources(datasource_type: Optional[str] = None, params: Params=Depend
 def get_datasources_info(ds_id: int, db: Session = Depends(get_db)):
 def get_datasources_info(ds_id: int, db: Session = Depends(get_db)):
     return crud.get_job_jdbc_datasources_info(db, ds_id)
     return crud.get_job_jdbc_datasources_info(db, ds_id)
 
 
-@router.put("/{ds_id}")
+@router.put("/{ds_id}", dependencies=[Depends(verify_super_admin)])
 @web_try()
 @web_try()
 @sxtimeit
 @sxtimeit
 def update_datasource(ds_id: int, ds: schemas.JobJdbcDatasourceUpdate, db: Session = Depends(get_db)):
 def update_datasource(ds_id: int, ds: schemas.JobJdbcDatasourceUpdate, db: Session = Depends(get_db)):
     return crud.update_job_jdbc_datasources(db, ds_id, ds)
     return crud.update_job_jdbc_datasources(db, ds_id, ds)
 
 
-@router.delete("/{ds_id}")
+@router.delete("/{ds_id}", dependencies=[Depends(verify_super_admin)])
 @web_try()
 @web_try()
 @sxtimeit
 @sxtimeit
 def delete_job_jdbc_datasource(ds_id: int, db: Session = Depends(get_db)):
 def delete_job_jdbc_datasource(ds_id: int, db: Session = Depends(get_db)):

+ 0 - 1
utils/__init__.py

@@ -5,6 +5,5 @@ from utils.sx_image import *
 def flat_map(f, xs):
 def flat_map(f, xs):
         ys = []
         ys = []
         for x in xs:
         for x in xs:
-            print(x)
             ys.extend(f(x))
             ys.extend(f(x))
         return ys
         return ys