handler.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. package httpserver
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "sync/atomic"
  9. "time"
  10. "github.com/goccy/go-json"
  11. "github.com/danfragoso/pizzasql-next/pkg/analyzer"
  12. "github.com/danfragoso/pizzasql-next/pkg/capabilities"
  13. "github.com/danfragoso/pizzasql-next/pkg/csvexport"
  14. "github.com/danfragoso/pizzasql-next/pkg/csvimport"
  15. "github.com/danfragoso/pizzasql-next/pkg/executor"
  16. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  17. "github.com/danfragoso/pizzasql-next/pkg/parser"
  18. "github.com/danfragoso/pizzasql-next/pkg/pgerror"
  19. "github.com/danfragoso/pizzasql-next/pkg/sqlexport"
  20. "github.com/danfragoso/pizzasql-next/pkg/sqlimport"
  21. "github.com/danfragoso/pizzasql-next/pkg/sqliteimport"
  22. "github.com/danfragoso/pizzasql-next/pkg/storage"
  23. "github.com/danfragoso/pizzasql-next/pkg/version"
  24. )
  25. // QueryRequest represents a single query request.
  26. type QueryRequest struct {
  27. SQL string `json:"sql"`
  28. Params []interface{} `json:"params"`
  29. TransactionID string `json:"transactionId,omitempty"`
  30. }
  31. // ExecuteRequest represents a batch execution request.
  32. type ExecuteRequest struct {
  33. Statements []QueryRequest `json:"statements"`
  34. Transaction bool `json:"transaction"`
  35. }
  36. // TransactionRequest represents a transaction management request.
  37. type TransactionRequest struct {
  38. TransactionID string `json:"transactionId"`
  39. }
  40. // handleQuery handles POST /query
  41. func (s *Server) handleQuery(w http.ResponseWriter, r *http.Request) {
  42. if r.Method != http.MethodPost {
  43. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only POST method is allowed", nil)
  44. return
  45. }
  46. var req QueryRequest
  47. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  48. writeError(w, http.StatusBadRequest, "INVALID_JSON", "Invalid JSON in request body", nil)
  49. return
  50. }
  51. if req.SQL == "" {
  52. writeError(w, http.StatusBadRequest, "MISSING_SQL", "SQL query is required", nil)
  53. return
  54. }
  55. var tx *transactionExecutor
  56. var exec *executor.Executor
  57. if req.TransactionID != "" {
  58. var ok bool
  59. tx, ok = s.getTransactionExecutor(req.TransactionID, r.Header.Get("X-Database"))
  60. if !ok {
  61. writeError(w, http.StatusNotFound, "TRANSACTION_NOT_FOUND", "unknown transaction ID", nil)
  62. return
  63. }
  64. exec = tx.exec
  65. } else {
  66. // Get database from X-Database header.
  67. dbName := r.Header.Get("X-Database")
  68. var err error
  69. exec, _, err = s.getExecutorForDatabase(dbName)
  70. if err != nil {
  71. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  72. return
  73. }
  74. }
  75. // Check for pretty print
  76. pretty := r.URL.Query().Get("pretty") == "true"
  77. explain := r.URL.Query().Get("explain") == "true"
  78. readonly := r.URL.Query().Get("readonly") == "true"
  79. // Parse timeout
  80. timeout := 5 * time.Minute
  81. if t := r.URL.Query().Get("timeout"); t != "" {
  82. if d, err := time.ParseDuration(t); err == nil {
  83. timeout = d
  84. }
  85. }
  86. // Execute with timeout
  87. resultChan := make(chan *QueryResponse, 1)
  88. errorChan := make(chan error, 1)
  89. go func() {
  90. if tx != nil {
  91. defer tx.mu.Unlock()
  92. }
  93. start := time.Now()
  94. // Check readonly mode
  95. if readonly {
  96. upper := strings.ToUpper(strings.TrimSpace(req.SQL))
  97. if strings.HasPrefix(upper, "INSERT") ||
  98. strings.HasPrefix(upper, "UPDATE") ||
  99. strings.HasPrefix(upper, "DELETE") ||
  100. strings.HasPrefix(upper, "CREATE") ||
  101. strings.HasPrefix(upper, "DROP") ||
  102. strings.HasPrefix(upper, "ALTER") {
  103. errorChan <- &HTTPError{
  104. Code: "READ_ONLY_MODE",
  105. Message: "Write operations not allowed in read-only mode",
  106. Status: http.StatusForbidden,
  107. }
  108. return
  109. }
  110. }
  111. // Substitute parameters
  112. sql := substituteParams(req.SQL, req.Params)
  113. // Parse SQL
  114. l := lexer.New(sql)
  115. p := parser.New(l)
  116. stmt, err := p.Parse()
  117. if err != nil {
  118. errorChan <- &HTTPError{
  119. Code: "SYNTAX_ERROR",
  120. Message: err.Error(),
  121. Status: http.StatusBadRequest,
  122. }
  123. return
  124. }
  125. // Execute using the database-specific executor
  126. result, err := exec.Execute(stmt)
  127. if err != nil {
  128. errorChan <- sqlStateHTTPError(err)
  129. return
  130. }
  131. duration := time.Since(start)
  132. // Build response
  133. resp := &QueryResponse{
  134. Columns: make([]ColumnInfo, len(result.Columns)),
  135. Rows: sanitizeRows(result.Rows),
  136. RowsAffected: result.RowsAffected,
  137. LastInsertID: result.LastInsertID,
  138. LSN: result.LSN,
  139. ExecutionTimeMicro: duration.Microseconds(),
  140. RowsReturned: len(result.Rows),
  141. }
  142. for i, col := range result.Columns {
  143. colType := result.GetColumnType(i)
  144. // If no type info, infer from first row values
  145. if colType == "ANY" && len(result.Rows) > 0 && i < len(result.Rows[0]) {
  146. colType = inferType(result.Rows[0][i])
  147. }
  148. resp.Columns[i] = ColumnInfo{
  149. Name: col,
  150. Type: colType,
  151. }
  152. }
  153. // Calculate bytes read (approximate size of the result set)
  154. // This is the serialized JSON size of the rows data
  155. if jsonBytes, err := json.Marshal(result.Rows); err == nil {
  156. resp.BytesRead = int64(len(jsonBytes))
  157. }
  158. if explain {
  159. resp.QueryPlan = []string{"Full table scan"} // TODO: Real query plan
  160. }
  161. resultChan <- resp
  162. }()
  163. select {
  164. case resp := <-resultChan:
  165. atomic.AddInt64(&s.stats.QueriesExecuted, 1)
  166. atomic.AddInt64(&s.stats.QueriesSuccess, 1)
  167. writeJSON(w, http.StatusOK, resp, pretty)
  168. case err := <-errorChan:
  169. atomic.AddInt64(&s.stats.QueriesExecuted, 1)
  170. atomic.AddInt64(&s.stats.QueriesError, 1)
  171. if httpErr, ok := err.(*HTTPError); ok {
  172. writeError(w, httpErr.Status, httpErr.Code, httpErr.Message, httpErr.Details)
  173. } else {
  174. writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", err.Error(), nil)
  175. }
  176. case <-time.After(timeout):
  177. atomic.AddInt64(&s.stats.QueriesExecuted, 1)
  178. atomic.AddInt64(&s.stats.QueriesError, 1)
  179. writeError(w, http.StatusRequestTimeout, "TIMEOUT", "Query execution timeout", nil)
  180. }
  181. }
  182. // handleExecute handles POST /execute for batch operations
  183. func (s *Server) handleExecute(w http.ResponseWriter, r *http.Request) {
  184. if r.Method != http.MethodPost {
  185. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only POST method is allowed", nil)
  186. return
  187. }
  188. var req ExecuteRequest
  189. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  190. writeError(w, http.StatusBadRequest, "INVALID_JSON", "Invalid JSON in request body", nil)
  191. return
  192. }
  193. if len(req.Statements) == 0 {
  194. writeError(w, http.StatusBadRequest, "MISSING_STATEMENTS", "At least one statement is required", nil)
  195. return
  196. }
  197. // Get database from X-Database header
  198. dbName := r.Header.Get("X-Database")
  199. exec, _, err := s.getExecutorForDatabase(dbName)
  200. if err != nil {
  201. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  202. return
  203. }
  204. pretty := r.URL.Query().Get("pretty") == "true"
  205. start := time.Now()
  206. results := make([]ExecuteResult, 0, len(req.Statements))
  207. // Start transaction if requested
  208. if req.Transaction {
  209. l := lexer.New("BEGIN")
  210. p := parser.New(l)
  211. stmt, _ := p.Parse()
  212. if _, err := exec.Execute(stmt); err != nil {
  213. writeError(w, http.StatusInternalServerError, "TRANSACTION_ERROR", err.Error(), nil)
  214. return
  215. }
  216. }
  217. var executeErr error
  218. for _, stmt := range req.Statements {
  219. // Substitute parameters
  220. sql := substituteParams(stmt.SQL, stmt.Params)
  221. l := lexer.New(sql)
  222. p := parser.New(l)
  223. parsed, err := p.Parse()
  224. if err != nil {
  225. executeErr = err
  226. break
  227. }
  228. result, err := exec.Execute(parsed)
  229. if err != nil {
  230. executeErr = err
  231. break
  232. }
  233. results = append(results, ExecuteResult{
  234. RowsAffected: result.RowsAffected,
  235. LastInsertID: result.LastInsertID,
  236. LSN: result.LSN,
  237. })
  238. }
  239. // Handle transaction
  240. if req.Transaction {
  241. if executeErr != nil {
  242. // Rollback on error
  243. l := lexer.New("ROLLBACK")
  244. p := parser.New(l)
  245. stmt, _ := p.Parse()
  246. if _, rollbackErr := exec.Execute(stmt); rollbackErr != nil {
  247. writeError(w, http.StatusInternalServerError, "ROLLBACK_ERROR", rollbackErr.Error(), nil)
  248. return
  249. }
  250. writeError(w, http.StatusBadRequest, "TRANSACTION_ERROR", executeErr.Error(), nil)
  251. return
  252. } else {
  253. // Commit on success
  254. l := lexer.New("COMMIT")
  255. p := parser.New(l)
  256. stmt, _ := p.Parse()
  257. if _, err := exec.Execute(stmt); err != nil {
  258. if errors.Is(err, storage.ErrSerialization) {
  259. writeError(w, http.StatusConflict, "SERIALIZATION_FAILURE", err.Error(), nil)
  260. } else if constraintErr := constraintViolationHTTPError(err); constraintErr != nil {
  261. writeError(w, constraintErr.Status, constraintErr.Code, constraintErr.Message, constraintErr.Details)
  262. } else {
  263. writeError(w, http.StatusBadRequest, "TRANSACTION_ERROR", err.Error(), nil)
  264. }
  265. return
  266. }
  267. }
  268. } else if executeErr != nil {
  269. httpErr := sqlStateHTTPError(executeErr)
  270. writeError(w, httpErr.Status, httpErr.Code, httpErr.Message, httpErr.Details)
  271. return
  272. }
  273. resp := &ExecuteResponse{
  274. Results: results,
  275. ExecutionTime: time.Since(start).String(),
  276. }
  277. writeJSON(w, http.StatusOK, resp, pretty)
  278. }
  279. // handleSchemaTables handles GET /schema/tables
  280. func (s *Server) handleSchemaTables(w http.ResponseWriter, r *http.Request) {
  281. if r.Method != http.MethodGet {
  282. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only GET method is allowed", nil)
  283. return
  284. }
  285. // Get database from X-Database header (trim whitespace)
  286. dbName := strings.TrimSpace(r.Header.Get("X-Database"))
  287. _, schema, err := s.getExecutorForDatabase(dbName)
  288. if err != nil {
  289. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  290. return
  291. }
  292. actualDB := schema.GetDatabaseName()
  293. tables, err := schema.ListTables()
  294. if err != nil {
  295. writeError(w, http.StatusInternalServerError, "SCHEMA_ERROR", err.Error(), nil)
  296. return
  297. }
  298. // Include the actual database name and requested name in the response for verification
  299. resp := map[string]interface{}{
  300. "database": actualDB,
  301. "requested_database": dbName,
  302. "tables": tables,
  303. }
  304. pretty := r.URL.Query().Get("pretty") == "true"
  305. writeJSON(w, http.StatusOK, resp, pretty)
  306. }
  307. // handleSchemaTable handles GET /schema/tables/{table}
  308. func (s *Server) handleSchemaTable(w http.ResponseWriter, r *http.Request) {
  309. if r.Method != http.MethodGet {
  310. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only GET method is allowed", nil)
  311. return
  312. }
  313. // Get database from X-Database header
  314. dbName := r.Header.Get("X-Database")
  315. _, schemaManager, err := s.getExecutorForDatabase(dbName)
  316. if err != nil {
  317. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  318. return
  319. }
  320. // Extract table name from path
  321. path := strings.TrimPrefix(r.URL.Path, "/schema/tables/")
  322. tableName := strings.TrimSpace(path)
  323. if tableName == "" {
  324. writeError(w, http.StatusBadRequest, "MISSING_TABLE_NAME", "Table name is required", nil)
  325. return
  326. }
  327. schema, err := schemaManager.GetSchema(tableName)
  328. if err != nil {
  329. writeError(w, http.StatusNotFound, "TABLE_NOT_FOUND", fmt.Sprintf("Table '%s' not found", tableName), nil)
  330. return
  331. }
  332. columns := make([]map[string]interface{}, len(schema.Columns))
  333. for i, col := range schema.Columns {
  334. columns[i] = map[string]interface{}{
  335. "name": col.Name,
  336. "type": col.Type,
  337. "nullable": col.Nullable,
  338. "primaryKey": col.PrimaryKey,
  339. "default": col.DefaultLiteral(),
  340. }
  341. }
  342. resp := map[string]interface{}{
  343. "name": schema.Name,
  344. "columns": columns,
  345. }
  346. pretty := r.URL.Query().Get("pretty") == "true"
  347. writeJSON(w, http.StatusOK, resp, pretty)
  348. }
  349. // handleCapabilities serves the machine-readable capability document so clients
  350. // can discover supported SQL surface for this engine build.
  351. func (s *Server) handleCapabilities(w http.ResponseWriter, r *http.Request) {
  352. pretty := r.URL.Query().Get("pretty") == "true"
  353. writeJSON(w, http.StatusOK, capabilities.Current(), pretty)
  354. }
  355. // handleVersion reports the build identity and the protocol compatibility
  356. // floor separately, so clients can pin behavior to a specific engine build.
  357. func (s *Server) handleVersion(w http.ResponseWriter, r *http.Request) {
  358. resp := map[string]interface{}{
  359. "name": "pizzasql",
  360. "version": version.String(),
  361. "commit": version.Commit,
  362. "date": version.Date,
  363. "dirty": version.Dirty != "",
  364. "postgres_compat": version.PostgreSQLCompatVersion,
  365. "sqlite_compat": executor.SQLiteCompatVersion,
  366. }
  367. pretty := r.URL.Query().Get("pretty") == "true"
  368. writeJSON(w, http.StatusOK, resp, pretty)
  369. }
  370. // constraintViolationHTTPError maps a row-constraint violation onto the same
  371. // SQLSTATE response the PostgreSQL endpoint reports, and returns nil for any
  372. // other error. Transaction endpoints use it for a deferred unique, not-null, or
  373. // check failure at COMMIT while keeping their generic TRANSACTION_ERROR
  374. // fallback for errors that are not constraint violations.
  375. func constraintViolationHTTPError(err error) *HTTPError {
  376. var checkErr *storage.CheckViolationError
  377. var notNullErr *storage.NotNullViolationError
  378. var uniqueErr *storage.UniqueViolationError
  379. var primaryKeyErr *storage.PrimaryKeyViolationError
  380. var foreignKeyErr *storage.ForeignKeyViolationError
  381. switch {
  382. case errors.As(err, &foreignKeyErr):
  383. return &HTTPError{
  384. Status: http.StatusConflict,
  385. Code: "23503",
  386. Message: err.Error(),
  387. Details: map[string]interface{}{"constraint": foreignKeyErr.Constraint},
  388. }
  389. case errors.As(err, &checkErr):
  390. return &HTTPError{
  391. Status: http.StatusBadRequest,
  392. Code: "23514",
  393. Message: err.Error(),
  394. Details: map[string]interface{}{"constraint": checkErr.Constraint},
  395. }
  396. case errors.As(err, &notNullErr):
  397. return &HTTPError{
  398. Status: http.StatusBadRequest,
  399. Code: "23502",
  400. Message: err.Error(),
  401. }
  402. case errors.As(err, &uniqueErr):
  403. return &HTTPError{
  404. Status: http.StatusConflict,
  405. Code: "23505",
  406. Message: err.Error(),
  407. Details: map[string]interface{}{"constraint": uniqueErr.Constraint},
  408. }
  409. case errors.As(err, &primaryKeyErr):
  410. return &HTTPError{
  411. Status: http.StatusConflict,
  412. Code: "23505",
  413. Message: err.Error(),
  414. }
  415. default:
  416. return nil
  417. }
  418. }
  419. // sqlStateHTTPError maps engine errors onto the same SQLSTATE codes the
  420. // PostgreSQL endpoint reports. Typed constraint violations map first; the
  421. // executor also returns plain errors on released builds, so the
  422. // unique-violation, not-null, and unsupported-feature markers are matched on
  423. // their stable message prefixes as a fallback.
  424. func sqlStateHTTPError(err error) *HTTPError {
  425. if err == nil {
  426. return nil
  427. }
  428. if typed := constraintViolationHTTPError(err); typed != nil {
  429. return typed
  430. }
  431. message := err.Error()
  432. if strings.HasPrefix(message, "UNIQUE constraint failed: ") {
  433. constraint := strings.TrimPrefix(message, "UNIQUE constraint failed: ")
  434. return &HTTPError{
  435. Status: http.StatusConflict,
  436. Code: "23505",
  437. Message: message,
  438. Details: map[string]interface{}{"constraint": constraint},
  439. }
  440. }
  441. if strings.HasPrefix(message, "NOT NULL constraint failed: ") {
  442. return &HTTPError{
  443. Status: http.StatusBadRequest,
  444. Code: "23502",
  445. Message: message,
  446. }
  447. }
  448. if strings.HasPrefix(message, "FOREIGN KEY constraint failed: ") {
  449. constraint := strings.TrimPrefix(message, "FOREIGN KEY constraint failed: ")
  450. return &HTTPError{
  451. Status: http.StatusConflict,
  452. Code: "23503",
  453. Message: message,
  454. Details: map[string]interface{}{"constraint": constraint},
  455. }
  456. }
  457. if strings.HasPrefix(message, "CHECK constraint failed: ") {
  458. constraint := strings.TrimPrefix(message, "CHECK constraint failed: ")
  459. return &HTTPError{
  460. Status: http.StatusBadRequest,
  461. Code: "23514",
  462. Message: message,
  463. Details: map[string]interface{}{"constraint": constraint},
  464. }
  465. }
  466. if errors.Is(err, storage.ErrSerialization) {
  467. return &HTTPError{
  468. Status: http.StatusConflict,
  469. Code: "40001",
  470. Message: message,
  471. }
  472. }
  473. if strings.Contains(message, "feature not supported:") {
  474. return &HTTPError{
  475. Status: http.StatusBadRequest,
  476. Code: "0A000",
  477. Message: message,
  478. }
  479. }
  480. if errors.Is(err, pgerror.ErrInvalidText) {
  481. return &HTTPError{
  482. Status: http.StatusBadRequest,
  483. Code: pgerror.CodeInvalidText,
  484. Message: message,
  485. }
  486. }
  487. var analysisErr *analyzer.AnalysisError
  488. if errors.Is(err, pgerror.ErrDatatypeMismatch) || (errors.As(err, &analysisErr) && analysisErr.Type == analyzer.ErrTypeMismatch) {
  489. return &HTTPError{
  490. Status: http.StatusBadRequest,
  491. Code: pgerror.CodeDatatypeMismatch,
  492. Message: message,
  493. }
  494. }
  495. return &HTTPError{
  496. Status: http.StatusInternalServerError,
  497. Code: "EXECUTION_ERROR",
  498. Message: message,
  499. }
  500. }
  501. // handleHealth handles GET /health
  502. func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
  503. resp := map[string]interface{}{
  504. "status": "ok",
  505. "version": version.String(),
  506. "uptime": time.Since(s.stats.StartTime).String(),
  507. }
  508. pretty := r.URL.Query().Get("pretty") == "true"
  509. writeJSON(w, http.StatusOK, resp, pretty)
  510. }
  511. // handleStats handles GET /stats
  512. func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
  513. // Get database from X-Database header
  514. dbName := r.Header.Get("X-Database")
  515. _, schema, _ := s.getExecutorForDatabase(dbName)
  516. var tables []string
  517. if schema != nil {
  518. tables, _ = schema.ListTables()
  519. }
  520. var avgQueryTime string
  521. if s.stats.QueriesExecuted > 0 {
  522. avgQueryTime = "N/A" // TODO: Track actual query times
  523. } else {
  524. avgQueryTime = "0ms"
  525. }
  526. resp := map[string]interface{}{
  527. "queriesExecuted": atomic.LoadInt64(&s.stats.QueriesExecuted),
  528. "queriesSuccess": atomic.LoadInt64(&s.stats.QueriesSuccess),
  529. "queriesError": atomic.LoadInt64(&s.stats.QueriesError),
  530. "tablesCount": len(tables),
  531. "avgQueryTime": avgQueryTime,
  532. "uptime": time.Since(s.stats.StartTime).String(),
  533. }
  534. pretty := r.URL.Query().Get("pretty") == "true"
  535. writeJSON(w, http.StatusOK, resp, pretty)
  536. }
  537. // handleTransactionBegin handles POST /transaction/begin
  538. func (s *Server) handleTransactionBegin(w http.ResponseWriter, r *http.Request) {
  539. if r.Method != http.MethodPost {
  540. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only POST method is allowed", nil)
  541. return
  542. }
  543. // Get database from X-Database header
  544. dbName := r.Header.Get("X-Database")
  545. txID, _, err := s.beginTransaction(dbName)
  546. if err != nil {
  547. writeError(w, http.StatusInternalServerError, "TRANSACTION_ERROR", err.Error(), nil)
  548. return
  549. }
  550. resp := map[string]interface{}{
  551. "transactionId": txID,
  552. "status": "started",
  553. }
  554. pretty := r.URL.Query().Get("pretty") == "true"
  555. writeJSON(w, http.StatusOK, resp, pretty)
  556. }
  557. // handleTransactionCommit handles POST /transaction/commit
  558. func (s *Server) handleTransactionCommit(w http.ResponseWriter, r *http.Request) {
  559. if r.Method != http.MethodPost {
  560. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only POST method is allowed", nil)
  561. return
  562. }
  563. var req TransactionRequest
  564. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  565. writeError(w, http.StatusBadRequest, "INVALID_JSON", "Invalid JSON in request body", nil)
  566. return
  567. }
  568. tx, ok := s.takeTransactionExecutor(req.TransactionID, r.Header.Get("X-Database"))
  569. if !ok {
  570. writeError(w, http.StatusNotFound, "TRANSACTION_NOT_FOUND", "unknown transaction ID", nil)
  571. return
  572. }
  573. defer tx.mu.Unlock()
  574. l := lexer.New("COMMIT")
  575. p := parser.New(l)
  576. stmt, _ := p.Parse()
  577. if _, err := tx.exec.Execute(stmt); err != nil {
  578. if errors.Is(err, storage.ErrSerialization) {
  579. writeError(w, http.StatusConflict, "SERIALIZATION_FAILURE", err.Error(), nil)
  580. } else if constraintErr := constraintViolationHTTPError(err); constraintErr != nil {
  581. writeError(w, constraintErr.Status, constraintErr.Code, constraintErr.Message, constraintErr.Details)
  582. } else {
  583. writeError(w, http.StatusBadRequest, "TRANSACTION_ERROR", err.Error(), nil)
  584. }
  585. return
  586. }
  587. resp := map[string]interface{}{
  588. "status": "committed",
  589. }
  590. pretty := r.URL.Query().Get("pretty") == "true"
  591. writeJSON(w, http.StatusOK, resp, pretty)
  592. }
  593. // substituteParams replaces ? placeholders with actual parameter values.
  594. // This is a simple implementation that handles basic SQL escaping.
  595. func substituteParams(sql string, params []interface{}) string {
  596. if len(params) == 0 {
  597. return sql
  598. }
  599. result := sql
  600. for _, param := range params {
  601. idx := strings.Index(result, "?")
  602. if idx == -1 {
  603. break
  604. }
  605. var replacement string
  606. switch v := param.(type) {
  607. case nil:
  608. replacement = "NULL"
  609. case string:
  610. // Escape single quotes in strings
  611. escaped := strings.ReplaceAll(v, "'", "''")
  612. replacement = "'" + escaped + "'"
  613. case int, int64, int32, int16, int8:
  614. replacement = fmt.Sprintf("%d", v)
  615. case float64, float32:
  616. replacement = fmt.Sprintf("%g", v)
  617. case bool:
  618. if v {
  619. replacement = "1"
  620. } else {
  621. replacement = "0"
  622. }
  623. default:
  624. // For other types, convert to string
  625. escaped := strings.ReplaceAll(fmt.Sprintf("%v", v), "'", "''")
  626. replacement = "'" + escaped + "'"
  627. }
  628. result = result[:idx] + replacement + result[idx+1:]
  629. }
  630. return result
  631. }
  632. // inferType infers SQL type from a Go value.
  633. func inferType(v interface{}) string {
  634. switch v.(type) {
  635. case nil:
  636. return "NULL"
  637. case int, int64, int32, int16, int8:
  638. return "INTEGER"
  639. case float64, float32:
  640. return "REAL"
  641. case string:
  642. return "TEXT"
  643. case []byte:
  644. return "BLOB"
  645. case bool:
  646. return "INTEGER"
  647. default:
  648. return "ANY"
  649. }
  650. }
  651. // handleTransactionRollback handles POST /transaction/rollback
  652. func (s *Server) handleTransactionRollback(w http.ResponseWriter, r *http.Request) {
  653. if r.Method != http.MethodPost {
  654. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only POST method is allowed", nil)
  655. return
  656. }
  657. var req TransactionRequest
  658. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  659. writeError(w, http.StatusBadRequest, "INVALID_JSON", "Invalid JSON in request body", nil)
  660. return
  661. }
  662. tx, ok := s.takeTransactionExecutor(req.TransactionID, r.Header.Get("X-Database"))
  663. if !ok {
  664. writeError(w, http.StatusNotFound, "TRANSACTION_NOT_FOUND", "unknown transaction ID", nil)
  665. return
  666. }
  667. defer tx.mu.Unlock()
  668. l := lexer.New("ROLLBACK")
  669. p := parser.New(l)
  670. stmt, _ := p.Parse()
  671. if _, err := tx.exec.Execute(stmt); err != nil {
  672. writeError(w, http.StatusBadRequest, "TRANSACTION_ERROR", err.Error(), nil)
  673. return
  674. }
  675. resp := map[string]interface{}{
  676. "status": "rolled back",
  677. }
  678. pretty := r.URL.Query().Get("pretty") == "true"
  679. writeJSON(w, http.StatusOK, resp, pretty)
  680. }
  681. // handleMetrics handles GET /metrics in Prometheus format
  682. func (s *Server) handleMetrics(w http.ResponseWriter, r *http.Request) {
  683. if r.Method != http.MethodGet {
  684. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only GET method is allowed", nil)
  685. return
  686. }
  687. // Get database from X-Database header
  688. dbName := r.Header.Get("X-Database")
  689. _, schema, _ := s.getExecutorForDatabase(dbName)
  690. var tables []string
  691. if schema != nil {
  692. tables, _ = schema.ListTables()
  693. }
  694. uptime := time.Since(s.stats.StartTime).Seconds()
  695. queriesTotal := atomic.LoadInt64(&s.stats.QueriesExecuted)
  696. queriesSuccess := atomic.LoadInt64(&s.stats.QueriesSuccess)
  697. queriesError := atomic.LoadInt64(&s.stats.QueriesError)
  698. w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
  699. // Write Prometheus format metrics
  700. fmt.Fprintf(w, "# HELP pizzasql_queries_total Total number of queries executed\n")
  701. fmt.Fprintf(w, "# TYPE pizzasql_queries_total counter\n")
  702. fmt.Fprintf(w, "pizzasql_queries_total{status=\"success\"} %d\n", queriesSuccess)
  703. fmt.Fprintf(w, "pizzasql_queries_total{status=\"error\"} %d\n", queriesError)
  704. fmt.Fprintf(w, "\n")
  705. fmt.Fprintf(w, "# HELP pizzasql_queries_executed_total Total queries executed (all statuses)\n")
  706. fmt.Fprintf(w, "# TYPE pizzasql_queries_executed_total counter\n")
  707. fmt.Fprintf(w, "pizzasql_queries_executed_total %d\n", queriesTotal)
  708. fmt.Fprintf(w, "\n")
  709. fmt.Fprintf(w, "# HELP pizzasql_tables_count Number of tables in the database\n")
  710. fmt.Fprintf(w, "# TYPE pizzasql_tables_count gauge\n")
  711. fmt.Fprintf(w, "pizzasql_tables_count %d\n", len(tables))
  712. fmt.Fprintf(w, "\n")
  713. fmt.Fprintf(w, "# HELP pizzasql_uptime_seconds Server uptime in seconds\n")
  714. fmt.Fprintf(w, "# TYPE pizzasql_uptime_seconds gauge\n")
  715. fmt.Fprintf(w, "pizzasql_uptime_seconds %.2f\n", uptime)
  716. fmt.Fprintf(w, "\n")
  717. fmt.Fprintf(w, "# HELP pizzasql_info PizzaSQL server information\n")
  718. fmt.Fprintf(w, "# TYPE pizzasql_info gauge\n")
  719. fmt.Fprintf(w, "pizzasql_info{version=\"%s\"} 1\n", version.PrometheusLabel())
  720. }
  721. // handleExport handles GET /export
  722. func (s *Server) handleExport(w http.ResponseWriter, r *http.Request) {
  723. if r.Method != http.MethodGet {
  724. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only GET method is allowed", nil)
  725. return
  726. }
  727. // Get database from X-Database header
  728. dbName := strings.TrimSpace(r.Header.Get("X-Database"))
  729. _, schema, err := s.getExecutorForDatabase(dbName)
  730. if err != nil {
  731. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  732. return
  733. }
  734. // Get the table manager from the database instance
  735. dbInstance, err := s.dbManager.GetDatabase(dbName)
  736. if err != nil {
  737. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  738. return
  739. }
  740. // Get format parameter (default: sql)
  741. format := strings.ToLower(r.URL.Query().Get("format"))
  742. if format == "" {
  743. format = "sql"
  744. }
  745. tableName := r.URL.Query().Get("table")
  746. switch format {
  747. case "csv":
  748. // CSV export requires a single table
  749. if tableName == "" {
  750. writeError(w, http.StatusBadRequest, "TABLE_REQUIRED", "CSV export requires 'table' parameter", nil)
  751. return
  752. }
  753. csvOpts := csvexport.DefaultExportOptions()
  754. csvOpts.Table = tableName
  755. data, err := csvexport.ExportTableToBytes(schema, dbInstance.Table, csvOpts)
  756. if err != nil {
  757. writeError(w, http.StatusInternalServerError, "EXPORT_ERROR", err.Error(), nil)
  758. return
  759. }
  760. w.Header().Set("Content-Type", "text/csv")
  761. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.csv\"", tableName))
  762. w.WriteHeader(http.StatusOK)
  763. w.Write(data)
  764. case "sql", "sqlite":
  765. // SQL export (also used for SQLite-compatible export)
  766. opts := sqlexport.DefaultExportOptions()
  767. // Specific table(s) to export
  768. if tableName != "" {
  769. opts.Tables = strings.Split(tableName, ",")
  770. }
  771. // Include data (default: true)
  772. if r.URL.Query().Get("schema_only") == "true" {
  773. opts.IncludeData = false
  774. }
  775. // Include DROP TABLE statements
  776. if r.URL.Query().Get("drop") == "true" {
  777. opts.DropTables = true
  778. }
  779. // Generate SQL export
  780. sql, err := sqlexport.ExportDatabase(schema, dbInstance.Table, opts)
  781. if err != nil {
  782. writeError(w, http.StatusInternalServerError, "EXPORT_ERROR", err.Error(), nil)
  783. return
  784. }
  785. // Determine filename and content type
  786. ext := "sql"
  787. contentType := "application/sql"
  788. if format == "sqlite" {
  789. ext = "sql" // Still SQL text, but sqlite-compatible
  790. }
  791. filename := schema.GetDatabaseName() + "_export." + ext
  792. if len(opts.Tables) == 1 {
  793. filename = opts.Tables[0] + "_export." + ext
  794. }
  795. w.Header().Set("Content-Type", contentType)
  796. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
  797. w.WriteHeader(http.StatusOK)
  798. w.Write([]byte(sql))
  799. default:
  800. writeError(w, http.StatusBadRequest, "INVALID_FORMAT",
  801. fmt.Sprintf("Invalid format '%s'. Supported formats: sql, csv", format), nil)
  802. }
  803. }
  804. // handleImport handles POST /import
  805. func (s *Server) handleImport(w http.ResponseWriter, r *http.Request) {
  806. if r.Method != http.MethodPost {
  807. writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Only POST method is allowed", nil)
  808. return
  809. }
  810. // Get database from X-Database header
  811. dbName := strings.TrimSpace(r.Header.Get("X-Database"))
  812. exec, schema, err := s.getExecutorForDatabase(dbName)
  813. if err != nil {
  814. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  815. return
  816. }
  817. // Get the table manager from the database instance
  818. dbInstance, err := s.dbManager.GetDatabase(dbName)
  819. if err != nil {
  820. writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
  821. return
  822. }
  823. // Get format parameter (default: sql, can be auto-detected)
  824. format := strings.ToLower(r.URL.Query().Get("format"))
  825. tableName := r.URL.Query().Get("table")
  826. ignoreErrors := r.URL.Query().Get("ignore_errors") == "true"
  827. createTable := r.URL.Query().Get("create_table") == "true"
  828. pretty := r.URL.Query().Get("pretty") == "true"
  829. // Check content type
  830. contentType := r.Header.Get("Content-Type")
  831. var fileContent []byte
  832. var filename string
  833. if strings.HasPrefix(contentType, "multipart/form-data") {
  834. // Handle file upload
  835. if err := r.ParseMultipartForm(32 << 20); err != nil { // 32MB max
  836. writeError(w, http.StatusBadRequest, "INVALID_FORM", "Failed to parse multipart form: "+err.Error(), nil)
  837. return
  838. }
  839. file, header, err := r.FormFile("file")
  840. if err != nil {
  841. writeError(w, http.StatusBadRequest, "MISSING_FILE", "No file uploaded. Use 'file' field name.", nil)
  842. return
  843. }
  844. defer file.Close()
  845. filename = header.Filename
  846. // Read file content
  847. fileContent, err = io.ReadAll(file)
  848. if err != nil {
  849. writeError(w, http.StatusBadRequest, "READ_ERROR", "Failed to read uploaded file: "+err.Error(), nil)
  850. return
  851. }
  852. } else if strings.HasPrefix(contentType, "application/json") {
  853. // Handle JSON body with SQL content
  854. var req struct {
  855. SQL string `json:"sql"`
  856. }
  857. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  858. writeError(w, http.StatusBadRequest, "INVALID_JSON", "Invalid JSON in request body", nil)
  859. return
  860. }
  861. fileContent = []byte(req.SQL)
  862. } else if strings.HasPrefix(contentType, "text/plain") || strings.HasPrefix(contentType, "application/sql") || strings.HasPrefix(contentType, "text/csv") {
  863. // Handle raw content in body
  864. var err error
  865. fileContent, err = io.ReadAll(r.Body)
  866. if err != nil {
  867. writeError(w, http.StatusBadRequest, "READ_ERROR", "Failed to read request body: "+err.Error(), nil)
  868. return
  869. }
  870. // Auto-detect CSV from content type
  871. if strings.HasPrefix(contentType, "text/csv") && format == "" {
  872. format = "csv"
  873. }
  874. } else {
  875. writeError(w, http.StatusBadRequest, "INVALID_CONTENT_TYPE",
  876. "Content-Type must be multipart/form-data, application/json, text/plain, text/csv, or application/sql", nil)
  877. return
  878. }
  879. if len(fileContent) == 0 {
  880. writeError(w, http.StatusBadRequest, "EMPTY_CONTENT", "No content provided", nil)
  881. return
  882. }
  883. // Auto-detect format from filename extension if not specified
  884. if format == "" && filename != "" {
  885. lower := strings.ToLower(filename)
  886. if strings.HasSuffix(lower, ".csv") {
  887. format = "csv"
  888. } else if strings.HasSuffix(lower, ".db") || strings.HasSuffix(lower, ".sqlite") || strings.HasSuffix(lower, ".sqlite3") {
  889. format = "sqlite"
  890. }
  891. }
  892. // Detect binary SQLite from content-type
  893. if format == "" {
  894. ct := strings.ToLower(contentType)
  895. if strings.Contains(ct, "application/x-sqlite3") || strings.Contains(ct, "application/octet-stream") {
  896. format = "sqlite"
  897. }
  898. }
  899. // Check magic bytes: SQLite files start with "SQLite format 3\000"
  900. if format == "" && len(fileContent) >= 16 && string(fileContent[:15]) == "SQLite format 3" {
  901. format = "sqlite"
  902. }
  903. if format == "" {
  904. format = "sql"
  905. }
  906. switch format {
  907. case "csv":
  908. // CSV import requires table name
  909. if tableName == "" {
  910. writeError(w, http.StatusBadRequest, "TABLE_REQUIRED", "CSV import requires 'table' parameter", nil)
  911. return
  912. }
  913. csvOpts := csvimport.DefaultImportOptions()
  914. csvOpts.TableName = tableName
  915. csvOpts.IgnoreErrors = ignoreErrors
  916. csvOpts.CreateTable = createTable
  917. result, err := csvimport.ImportCSV(strings.NewReader(string(fileContent)), schema, dbInstance.Table, csvOpts)
  918. if err != nil && !ignoreErrors {
  919. writeError(w, http.StatusBadRequest, "IMPORT_ERROR", err.Error(), map[string]interface{}{
  920. "rowsImported": result.RowsImported,
  921. "rowsSkipped": result.RowsSkipped,
  922. "tableCreated": result.TableCreated,
  923. "errors": result.Errors,
  924. })
  925. return
  926. }
  927. // Sync catalog after import
  928. exec.SyncCatalog()
  929. writeJSON(w, http.StatusOK, result, pretty)
  930. case "sqlite":
  931. // Binary SQLite .db import
  932. opts := sqliteimport.DefaultImportOptions()
  933. opts.CreateTables = r.URL.Query().Get("create_tables") != "false"
  934. opts.IgnoreErrors = ignoreErrors
  935. result, err := sqliteimport.ImportSQLiteBytes(fileContent, exec, opts)
  936. if err != nil && !ignoreErrors {
  937. writeError(w, http.StatusBadRequest, "IMPORT_ERROR", err.Error(), map[string]interface{}{
  938. "tablesCreated": result.TablesCreated,
  939. "rowsInserted": result.RowsInserted,
  940. "errors": result.Errors,
  941. })
  942. return
  943. }
  944. exec.SyncCatalog()
  945. writeJSON(w, http.StatusOK, result, pretty)
  946. case "sql":
  947. // SQL text import
  948. opts := sqlimport.DefaultImportOptions()
  949. opts.IgnoreErrors = ignoreErrors
  950. result, err := sqlimport.ImportSQL(exec, string(fileContent), opts)
  951. if err != nil && !ignoreErrors {
  952. writeError(w, http.StatusBadRequest, "IMPORT_ERROR", err.Error(), map[string]interface{}{
  953. "statementsExecuted": result.StatementsExecuted,
  954. "tablesCreated": result.TablesCreated,
  955. "rowsInserted": result.RowsInserted,
  956. "errors": result.Errors,
  957. })
  958. return
  959. }
  960. // Sync catalog after import
  961. exec.SyncCatalog()
  962. writeJSON(w, http.StatusOK, result, pretty)
  963. default:
  964. writeError(w, http.StatusBadRequest, "INVALID_FORMAT",
  965. fmt.Sprintf("Invalid format '%s'. Supported formats: sql, csv", format), nil)
  966. }
  967. }