Skip to content

Commit

Permalink
select dml fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vahagz committed Jan 25, 2024
1 parent 386e57c commit d0bf49f
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 101 deletions.
202 changes: 113 additions & 89 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,107 +47,131 @@ func main() {
}
}()

// q, err := ps.ParseQuery([]byte(`{
// "type": "CREATE",
// "target": "TABLE",
// "name": "testtable",
// "columns": [
// {
// "name": "id",
// "type": 0,
// "meta": {
// "signed": false,
// "bit_size": 4,
// "auto_increment": {
// "enabled": true
// }
// }
// },
// {
// "name": "firstname",
// "type": 2,
// "meta": {
// "cap": 32
// }
// },
// {
// "name": "lastname",
// "type": 2,
// "meta": {
// "cap": 32
// }
// }
// ],
// "indexes": [
// {
// "name": "id_1",
// "columns": [ "id" ],
// "primary": true,
// "auto_increment": true
// },
// {
// "name": "firstname_lastname_1",
// "columns": [ "firstname", "lastname" ]
// }
// ]
// }`))
// if err != nil {
// logrus.Fatal(err)
// }
// res, err := es.Exec(q)
// if err != nil {
// logrus.Fatal(err)
// }
// printResponse(res)

// q, err = ps.ParseQuery([]byte(`{
// "type": "INSERT",
// "table": "testtable",
// "columns": [ "firstname", "lastname" ],
// "values": [
// [ "Vahag", "Zargaryan" ],
// [ "Ruben", "Manandyan" ],
// [ "Sergey", "Zargaryan" ],
// [ "Arman", "Sargsyan" ],
// [ "Mery", "Voskanyan" ],
// [ "David", "Harutyunyan" ],
// [ "Alexader", "Bakunc" ]
// ]
// }`))
// if err != nil {
// logrus.Fatal(err)
// }

// res, err = es.Exec(q)
// if err != nil {
// logrus.Fatal(err)
// }
// printResponse(res)


q, err := ps.ParseQuery([]byte(`{
"type": "CREATE",
"target": "TABLE",
"name": "testtable",
"columns": [
{
"name": "id",
"type": 0,
"meta": {
"signed": false,
"bit_size": 4,
"auto_increment": {
"enabled": true
}
}
},
{
"name": "firstname",
"type": 2,
"meta": {
"cap": 32
"type": "SELECT",
"table": "testtable",
"columns": [ "id", "firstname", "lastname" ],
"where_index": {
"name": "id_1",
"filter_start": {
"operator": ">=",
"value": {
"id": 4
}
},
{
"name": "lastname",
"type": 2,
"meta": {
"cap": 32
"filter_end": {
"operator": "<=",
"value": {
"id": 6
}
}
],
"indexes": [
{
"name": "id_1",
"columns": [ "id" ],
"primary": true,
"auto_increment": true
},
{
"name": "firstname_lastname_1",
"columns": [ "firstname", "lastname" ]
}
]
},
"where": {
"or": [
{
"statement": {
"column": "firstname",
"operator": "=",
"value": "Arman"
}
},
{
"statement": {
"column": "lastname",
"operator": "=",
"value": "Harutyunyan"
}
}
]
}
}`))
if err != nil {
logrus.Fatal(err)
}

res, err := es.Exec(q)
if err != nil {
logrus.Fatal(err)
}
printResponse(res)

q, err = ps.ParseQuery([]byte(`{
"type": "INSERT",
"table": "testtable",
"columns": [ "firstname", "lastname" ],
"values": [
[ "Vahag", "Zargaryan" ],
[ "Ruben", "Manandyan" ],
[ "Sergey", "Zargaryan" ],
[ "Arman", "Sargsyan" ],
[ "Mery", "Voskanyan" ],
[ "David", "Harutyunyan" ],
[ "Alexader", "Bakunc" ]
]
}`))
if err != nil {
logrus.Fatal(err)
}

res, err = es.Exec(q)
if err != nil {
logrus.Fatal(err)
}
printResponse(res)


// q, err = ps.ParseQuery([]byte(`{
// "type": "SELECT",
// "table": "testtable",
// "columns": [ "id", "firstname", "lastname" ],
// "where_index": {
// "name": "id_1",
// "filter_start": {
// "operator": ">=",
// "value": {
// "id": 3
// }
// }
// }
// }`))
// if err != nil {
// logrus.Fatal(err)
// }

// res, err = es.Exec(q)
// if err != nil {
// logrus.Fatal(err)
// }
// printResponse(res)


// logrus.SetLevel(logrus.DebugLevel)
Expand Down Expand Up @@ -815,12 +839,12 @@ func randomString(length int) string {
}

func printResponse(res io.Reader) {
rr := response.NewReader(res, 4, executor.Bin)
rr := response.NewReader(res)
for {
msg, err := rr.ReadLine()
fmt.Printf("%v '%s'\n", len(msg), string(msg))
if err != nil {
fmt.Println(err)
fmt.Println("error =>", err)
break
}
}
Expand Down
73 changes: 61 additions & 12 deletions services/executor/dml_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,52 @@ func (es *ExecutorServiceT) dmlSelectValidate(q *dml.QuerySelect) error {
if !table.HasIndex(q.WhereIndex.Name) {
return fmt.Errorf("index not found: '%s'", q.WhereIndex.Name)
}

fs := q.WhereIndex.FilterStart
if fs != nil {
for k, v := range fs.Value {
casted, err := v.Cast(table.Column(k).Typ, table.Column(k).Meta)
if err != nil {
return errors.Wrapf(err, "failed to cast %v to %v", v.GetCode(), table.Column(k).Typ)
}

fs.Value[k] = casted
}
}

fe := q.WhereIndex.FilterEnd
if fe != nil {
for k, v := range fe.Value {
casted, err := v.Cast(table.Column(k).Typ, table.Column(k).Meta)
if err != nil {
return errors.Wrapf(err, "failed to cast %v to %v", v.GetCode(), table.Column(k).Typ)
}

fe.Value[k] = casted
}
}

var validateWhere func(w *statement.WhereStatement)
validateWhere = func (w *statement.WhereStatement) {
if w == nil {
return
}

if w.And != nil {
for _, ws := range w.And {
validateWhere(ws)
}
}
if w.Or != nil {
for _, ws := range w.Or {
validateWhere(ws)
}
}
if w.Statement != nil {
w.Statement.Val = types.Type(table.Column(w.Statement.Col).Meta).Set(w.Statement.Val.Value())
}
}
validateWhere((*statement.WhereStatement)(q.Where))
}

return nil
Expand Down Expand Up @@ -57,11 +103,12 @@ func (es *ExecutorServiceT) dmlSelect(q *dml.QuerySelect) (io.Reader, error) {
Operator: q.WhereIndex.FilterStart.Operator,
Value: q.WhereIndex.FilterStart.Value,
}
}
if q.WhereIndex.FilterEnd != nil {
indexFilterEnd = &index.Filter{
Operator: q.WhereIndex.FilterEnd.Operator,
Value: q.WhereIndex.FilterEnd.Value,

if q.WhereIndex.FilterEnd != nil {
indexFilterEnd = &index.Filter{
Operator: q.WhereIndex.FilterEnd.Operator,
Value: q.WhereIndex.FilterEnd.Value,
}
}
}
}
Expand All @@ -70,16 +117,18 @@ func (es *ExecutorServiceT) dmlSelect(q *dml.QuerySelect) (io.Reader, error) {
}

process := func(row map[string]types.DataType) (bool, error) {
m := make(map[string]interface{}, len(row))
for k, dt := range row {
m[k] = dt.Value()
record := make([]interface{}, 0, len(q.Columns))
for _, col := range q.Columns {
record = append(record, row[col].Value())
}
blob, _ := json.Marshal(m)
_, err := p.Write(blob)

blob, err := json.Marshal(record)
if err != nil {
return true, errors.Wrap(err, "failed to push marshaled record")
return true, errors.Wrap(err, "failed to marshal record")
}
return false, nil

_, err = p.Write(blob)
return false, errors.Wrap(err, "failed to push marshaled record")
}

var err error
Expand Down

0 comments on commit d0bf49f

Please sign in to comment.