File size: 865 Bytes
570f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import time
from nl2sql.types import StageResult, StageTrace
from adapters.db.base import DBAdapter

class Executor:
    name = "executor"

    def __init__(self, db: DBAdapter):
        self.db = db

    def run(self, sql: str) -> StageResult:
        t0 = time.perf_counter()
        try:
            rows, cols = self.db.execute(sql)
            trace = StageTrace(stage=self.name, duration_ms=(time.perf_counter()-t0)*1000,
                               notes={"row_count": len(rows), "col_count": len(cols)})
            return StageResult(ok=True, data={"rows": rows, "columns": cols}, trace=trace)
        except Exception as e:
            trace = StageTrace(stage=self.name, duration_ms=(time.perf_counter()-t0)*1000,
                               notes={"error": str(e)})
            return StageResult(ok=False, data=None, trace=trace, error=[str(e)])