Add blueprints for manipulating database from sql statements

This commit is contained in:
Tobie Morgan Hitchcock 2016-02-27 01:00:19 +00:00
parent 15e5ab68b4
commit 4544d25762
13 changed files with 306 additions and 4 deletions

23
db/create.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeCreateStatement(stmt sql.Statement) interface{} {
return stmt
}

72
db/db.go Normal file
View file

@ -0,0 +1,72 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"io"
"github.com/abcum/surreal/sql"
)
func Execute(query *sql.Query, err error) (interface{}, error) {
for _, s := range query.Statements {
switch s.(type) {
case *sql.SelectStatement:
return executeSelectStatement(s), err
case *sql.CreateStatement:
return executeCreateStatement(s), err
case *sql.UpdateStatement:
return executeUpdateStatement(s), err
case *sql.ModifyStatement:
return executeModifyStatement(s), err
case *sql.DeleteStatement:
return executeDeleteStatement(s), err
case *sql.RelateStatement:
return executeRelateStatement(s), err
case *sql.RecordStatement:
return executeRecordStatement(s), err
case *sql.DefineViewStatement:
return executeDefineStatement(s), err
case *sql.ResyncViewStatement:
return executeResyncStatement(s), err
case *sql.RemoveViewStatement:
return executeRemoveStatement(s), err
case *sql.DefineIndexStatement:
return executeDefineStatement(s), err
case *sql.ResyncIndexStatement:
return executeResyncStatement(s), err
case *sql.RemoveIndexStatement:
return executeRemoveStatement(s), err
}
}
return query, err
}
func ExecuteString(input string) (interface{}, error) {
return Execute(sql.Parse(input))
}
func ExecuteBuffer(input io.Reader) (interface{}, error) {
return Execute(sql.NewParser(input).Parse())
}

23
db/define.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeDefineStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/delete.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeDeleteStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/modify.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeModifyStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/record.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeRecordStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/relate.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeRelateStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/remove.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeRemoveStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/resync.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeResyncStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/select.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeSelectStatement(stmt sql.Statement) interface{} {
return stmt
}

23
db/update.go Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/abcum/surreal/sql"
)
func executeUpdateStatement(stmt sql.Statement) interface{} {
return stmt
}

View file

@ -15,13 +15,13 @@
package server
import (
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/db"
"github.com/labstack/echo"
)
func crud(c *echo.Context) error {
s, e := sql.NewParser(c.Request().Body).Parse()
s, e := db.ExecuteBuffer(c.Request().Body)
if e == nil {
return c.JSON(200, show(s))

View file

@ -15,7 +15,7 @@
package server
import (
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/db"
"github.com/labstack/echo"
"golang.org/x/net/websocket"
)
@ -32,7 +32,7 @@ func sock(c *echo.Context) error {
break
}
s, e := sql.Parse(msg)
s, e := db.ExecuteString(msg)
if e == nil {
if err := websocket.Message.Send(ws, encode(show(s))); err != nil {