lxd/api_internal.go
func internalSQLPost(d *Daemon, r *http.Request) Response {
	req := &internalSQLQuery{}
	// Parse the request.
	err := json.NewDecoder(r.Body).Decode(&req)
	if err != nil {
		return BadRequest(err)
	}

	if !shared.StringInSlice(req.Database, []string{"local", "global"}) {
		return BadRequest(fmt.Errorf("Invalid database"))
	}

	if req.Query == "" {
		return BadRequest(fmt.Errorf("No query provided"))
	}

	var db *sql.DB
	if req.Database == "global" {
		db = d.cluster.DB()
	} else {
		db = d.db.DB()
	}

	batch := internalSQLBatch{}

	if req.Query == ".sync" {
		d.gateway.Sync()
		return SyncResponse(true, batch)
	}

	for _, query := range strings.Split(req.Query, ";") {
		query = strings.TrimLeft(query, " ")

		if query == "" {
			continue
		}

		result := internalSQLResult{}

		tx, err := db.Begin()
		if err != nil {
			return SmartError(err)
		}

		if strings.HasPrefix(strings.ToUpper(query), "SELECT") {
			err = internalSQLSelect(tx, query, &result)
			tx.Rollback()
		} else {
			err = internalSQLExec(tx, query, &result)
			if err != nil {
				tx.Rollback()
			} else {
				err = tx.Commit()
			}
		}
		if err != nil {
			return SmartError(err)
		}

		batch.Results = append(batch.Results, result)
	}

	return SyncResponse(true, batch)
}
func internalSQLPost(d *Daemon, r *http.Request) Response {
	req := &internalSQLQuery{}
	// Parse the request.
	err := json.NewDecoder(r.Body).Decode(&req)
	if err != nil {
		return BadRequest(err)
	}

	if !shared.StringInSlice(req.Database, []string{"local", "global"}) {
		return BadRequest(fmt.Errorf("Invalid database"))
	}

	if req.Query == "" {
		return BadRequest(fmt.Errorf("No query provided"))
	}

	var db *sql.DB
	if req.Database == "global" {
		db = d.cluster.DB()
	} else {
		db = d.db.DB()
	}

	batch := internalSQLBatch{}

	if req.Query == ".sync" {
		d.gateway.Sync()
		return SyncResponse(true, batch)
	}

	for _, query := range strings.Split(req.Query, ";") {
		query = strings.TrimLeft(query, " ")

		if query == "" {
			continue
		}

		result := internalSQLResult{}

		tx, err := db.Begin()
		if err != nil {
			return SmartError(err)
		}

		if strings.HasPrefix(strings.ToUpper(query), "SELECT") {
			err = internalSQLSelect(tx, query, &result)
			tx.Rollback()
		} else {
			err = internalSQLExec(tx, query, &result)
			if err != nil {
				tx.Rollback()
			} else {
				err = tx.Commit()
			}
		}
		if err != nil {
			return SmartError(err)
		}

		batch.Results = append(batch.Results, result)
	}

	return SyncResponse(true, batch)
}
func internalSQLPost ( d * Daemon , r * http . Request ) Response { req := & internalSQLQuery { } // Parse the request. err := json . NewDecoder ( r . Body ) . Decode ( & req ) if err != nil { return BadRequest ( err ) } if ! shared . StringInSlice ( req . Database , [ ] string { " " , " " } ) { return BadRequest ( fmt . Errorf ( " " ) ) } if req . Query == " " { return BadRequest ( fmt . Errorf ( " " ) ) } var db * sql . DB if req . Database == " " { db = d . cluster . DB ( ) } else { db = d . db . DB ( ) } batch := internalSQLBatch { } if req . Query == " " { d . gateway . Sync ( ) return SyncResponse ( true , batch ) } for _ , query := range strings . Split ( req . Query , " " ) { query = strings . TrimLeft ( query , " " ) if query == " " { continue } result := internalSQLResult { } tx , err := db . Begin ( ) if err != nil { return SmartError ( err ) } if strings . HasPrefix ( strings . ToUpper ( query ) , " " ) { err = internalSQLSelect ( tx , query , & result ) tx . Rollback ( ) } else { err = internalSQLExec ( tx , query , & result ) if err != nil { tx . Rollback ( ) } else { err = tx . Commit ( ) } } if err != nil { return SmartError ( err ) } batch . Results = append ( batch . Results , result ) } return SyncResponse ( true , batch ) }
// Execute queries.
Execute queries .