/
db.go
836 lines (710 loc) · 20 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
package registry
import (
"bytes"
"context"
"database/sql"
_ "embed"
"fmt"
"os"
"reflect"
"regexp"
"runtime/pprof"
"strings"
"time"
log "github.com/duglin/dlog"
_ "github.com/go-sql-driver/mysql"
)
var DB *sql.DB
var DBUSER = "root"
var DBHOST = "localhost"
var DBPORT = "3306"
var DBPASSWORD = "password"
// TODO load these from a config file
func init() {
if tmp := os.Getenv("DBUSER"); tmp != "" {
DBUSER = tmp
}
if tmp := os.Getenv("DBPASSWORD"); tmp != "" {
DBPASSWORD = tmp
}
if tmp := os.Getenv("DBHOST"); tmp != "" {
DBHOST = tmp
}
if tmp := os.Getenv("DBPORT"); tmp != "" {
DBPORT = tmp
}
}
// Active transaction - mainly for debugging and testing
var TXs = map[string]*Tx{}
func DumpTXs() {
// Only show info if there are active Txs
if len(TXs) == 0 {
return
}
count := 1
for _, t := range TXs {
log.Printf("NewTx Stack %d:", count)
for _, s := range t.stack {
log.Printf(" %s", s)
}
count++
}
// Show threads/processes
pprof.Lookup("goroutine").WriteTo(PProfFilter, 1)
log.Printf("==========================")
log.Printf("")
PProfFilter.count = 0
PProfFilter.inSection = false
PProfFilter.buffer.Reset()
}
var PProfFilter = &FilterPProf{}
type FilterPProf struct {
buffer bytes.Buffer
count int
inSection bool
}
// Extract func name and files info
var fpRE = regexp.MustCompile(`^#\s+[^\s]+\s+[^.]*.[^.]*\.([^+]*)\+[^/]*.*/(.*)$`)
// When dumping all processes, filter out the ones that aren't running our
// code and only show lines of interest to keep it small
func (fp *FilterPProf) Write(p []byte) (n int, err error) {
for _, b := range p {
if b == '\n' {
line := fp.buffer.String()
fp.buffer.Reset()
if strings.Contains(line, "xreg-github") &&
!strings.Contains(line, "(*Server).Serve+") &&
!strings.Contains(line, "(*Server).Serve.") &&
!strings.Contains(line, "TestMain") {
if !fp.inSection {
fp.inSection = true
fp.count++
log.Printf("Thread %d:", fp.count)
}
line = fpRE.ReplaceAllString(line, ` $1 $2`)
log.Printf(line)
} else {
fp.inSection = false
}
} else {
fp.buffer.WriteByte(b)
}
}
return len(p), nil
}
// Holds info about the current transaction. In a lot of ways this is similar
// to golang's Context in that it holds other info related to the current
// changes that are going on. Maybe one day convert this to a Context where
// Tx is just as apsect of it.
type Tx struct {
tx *sql.Tx
Registry *Registry
CreateTime string // use for entity timestamps too
User string
// Cache of entities this Tx is dealing with. Things can get funky if
// we have more than one instance of the same entity in memory.
// TODO DUG expand this to save all types, not just Versions.
// Also, consider having Commit() just automatically call ValidateAndSave
// for all entities in the Tx - then people don't need to call save
// explicitly
// Registries map[string]*Registry // reg.UID
// Groups map[string]*Group // reg.DbSID+g.UID
// Resources map[string]*Resource // reg.DbSID+g.DbSID+r.UID
Versions map[string]*Version // reg.DbSID+g.DbSID+r.DbSID+v.UID
// For debugging
uuid string // just a unique ID for the TXs map key
stack []string // Stack at time NewTX
}
func (tx *Tx) String() string {
regStr := "<none>"
if tx.Registry != nil {
regStr = tx.Registry.DbSID
}
txStr := "<none>"
if tx.tx != nil {
txStr = "<set>"
}
return fmt.Sprintf("Tx: sql.tx: %s, Registry: %s", txStr, regStr)
}
func NewTx() *Tx {
log.VPrintf(4, ">Enter: NewTx")
defer log.VPrintf(4, "<exit: NewTx")
tx := &Tx{}
tx.NewTx()
return tx
}
// It's ok for this to be called multiple times for the same Tx just to
// make sure we have an active transaction - it's a no-op at that point
func (tx *Tx) NewTx() {
log.VPrintf(4, ">Enter: tx.NewTx")
defer log.VPrintf(4, "<Exit: tx.NewTx")
if tx.tx != nil {
return
}
t, err := DB.BeginTx(context.Background(),
&sql.TxOptions{sql.LevelReadCommitted, false})
Must(err)
tx.tx = t
// if TESTING {
tx.CreateTime = time.Now().Format(time.RFC3339Nano)
// } else {
// tx.CreateTime = time.Now().Format(time.RFC3339)
// }
tx.Versions = map[string]*Version{}
tx.uuid = NewUUID()
tx.stack = GetStack()
TXs[tx.uuid] = tx
}
func (tx *Tx) Commit() error {
if tx.tx == nil {
return nil
}
err := tx.tx.Commit()
Must(err)
if err != nil {
return err
}
delete(TXs, tx.uuid)
tx.tx = nil
tx.CreateTime = ""
tx.Versions = nil // force a NPE if someone tries to use it outside of a tx
tx.uuid = ""
return nil
}
func (tx *Tx) Rollback() error {
if tx.tx == nil {
return nil
}
err := tx.tx.Rollback()
Must(err)
if err != nil {
return err
}
delete(TXs, tx.uuid)
tx.tx = nil
tx.CreateTime = ""
tx.Versions = nil // force a NPE if someone tries to use it outside of a tx
tx.uuid = ""
return nil
}
func (tx *Tx) Conditional(err error) error {
if err == nil {
return tx.Commit()
}
return tx.Rollback()
}
func (tx *Tx) Prepare(query string) (*sql.Stmt, error) {
// If the current Tx is closed, create a new one
if tx.tx == nil {
tx.NewTx()
}
ps, err := tx.tx.Prepare(query)
return ps, err
}
func (tx *Tx) AddVersion(v *Version) {
if tx.Versions == nil {
tx.Versions = map[string]*Version{}
}
tx.Versions[v.Resource.Group.Registry.DbSID+
v.Resource.Group.DbSID+
v.Resource.DbSID+
v.UID] = v
}
func (tx *Tx) GetVersion(r *Resource, vID string) *Version {
key := r.Group.Registry.DbSID + r.Group.DbSID + r.DbSID + vID
return tx.Versions[key]
}
type Result struct {
tx *Tx
sqlRows *sql.Rows
colTypes []reflect.Type
Data []*any // One row
TempData []any
Reuse bool
AllRows [][]*any
}
func (r *Result) Close() {
if r == nil {
return
}
if r.Data == nil {
// Already done
return
}
if r.tx != nil {
r.tx = nil
}
if r.sqlRows != nil {
r.sqlRows.Close()
r.sqlRows = nil
}
r.Data = nil
r.TempData = nil
r.AllRows = nil
}
func (r *Result) Push() {
if r.Reuse {
panic("Already pushed")
}
r.Reuse = true
}
func (r *Result) NextRow() []*any {
if r.Data == nil {
return nil
}
if r.Reuse {
r.Reuse = false
} else {
// check for error from PullNextRow
r.PullNextRow()
}
return r.Data
}
func (r *Result) PullNextRow() {
if r.AllRows == nil || len(r.AllRows) == 0 {
r.Close()
return
}
r.Data = r.AllRows[0]
r.AllRows = r.AllRows[1:]
if log.GetVerbose() > 3 {
dd := []string{}
for _, d := range r.Data {
dVal := reflect.ValueOf(*d)
if !IsNil(*d) && dVal.Type().String() == "[]uint8" {
// if reflect.ValueOf(*d).Type().String() == "[]uint8"
dd = append(dd, string((*d).([]byte)))
} else {
dd = append(dd, fmt.Sprintf("%v", *d))
}
}
log.VPrintf(4, "row: %v", dd)
}
}
func (r *Result) RetrieveAllRowsFromDB() {
for {
if r.RetrieveNextRowFromDB() == false {
break
}
r.AllRows = append(r.AllRows, r.Data)
}
// When done, technically r.Data contains the last item from the query
// but it'll be overwritten on the first call to PullNextRow
// Close the MYSQL query and prepare stmt
if r.sqlRows != nil {
r.sqlRows.Close()
r.sqlRows = nil
}
}
func (r *Result) RetrieveNextRowFromDB() bool {
if r.sqlRows == nil {
panic("sqlRows is nil")
}
if r.sqlRows.Next() == false {
// r.Close()
return false
}
r.TempData = make([]any, len(r.TempData))
r.Data = make([]*any, len(r.Data))
for i, _ := range r.TempData {
r.TempData[i] = new(any)
r.Data[i] = r.TempData[i].(*any)
}
err := r.sqlRows.Scan(r.TempData...) // Can't pass r.Data directly
if err != nil {
panic(fmt.Sprintf("Error scanning DB row: %s", err))
// should return err. r.Data = nil ; return err..
}
// Move data from TempData to Data
if log.GetVerbose() >= 4 {
dd := []string{}
for _, d := range r.Data {
dVal := reflect.ValueOf(*d)
if !IsNil(*d) && dVal.Type().String() == "[]uint8" {
// if reflect.ValueOf(*d).Type().String() == "[]uint8"
dd = append(dd, string((*d).([]byte)))
} else {
dd = append(dd, fmt.Sprintf("%v", *d))
}
}
log.Printf("row: %v", dd)
}
return true
}
func Query(tx *Tx, cmd string, args ...interface{}) (*Result, error) {
if log.GetVerbose() >= 4 {
log.Printf("Query: %s", SubQuery(cmd, args))
}
ps, err := tx.Prepare(cmd)
if err != nil {
log.Printf("Error Prepping query (%s)->%s\n", cmd, err)
return nil, fmt.Errorf("Error Prepping query (%s)->%s\n", cmd, err)
}
defer ps.Close()
rows, err := ps.Query(args...)
if err != nil {
log.Printf("Error querying DB(%s)(%v)->%s\n", cmd, args, err)
return nil, fmt.Errorf("Error querying DB(%s)->%s\n", cmd, err)
}
colTypes, err := rows.ColumnTypes()
if err != nil {
log.Printf("Error querying DB(%s)(%v)->%s\n", cmd, args, err)
return nil, fmt.Errorf("Error querying DB(%s)->%s\n", cmd, err)
}
result := &Result{
tx: tx,
sqlRows: rows,
colTypes: []reflect.Type{},
}
for _, col := range colTypes {
result.colTypes = append(result.colTypes, col.ScanType())
result.Data = append(result.Data, new(any))
result.TempData = append(result.TempData, new(any))
}
// Download all data. We used to pull from DB on each PullNextRow
// but mysql doesn't support multiple queries being active in the same Tx
result.RetrieveAllRowsFromDB()
return result, nil
}
func doCount(tx *Tx, cmd string, args ...interface{}) (int, error) {
log.VPrintf(4, "doCount: %q args: %v", cmd, args)
ps, err := tx.Prepare(cmd)
if err != nil {
ShowStack()
log.VPrintf(0, "CMD: %q args: %v", cmd, args)
return 0, err
}
defer ps.Close()
result, err := ps.Exec(args...)
if err != nil {
query := SubQuery(cmd, args)
log.Printf("doCount:Error DB(%s)->%s\n", query, err)
ShowStack()
log.VPrintf(0, "CMD: %q args: %v", cmd, args)
return 0, err
}
count, _ := result.RowsAffected()
return int(count), err
}
func Do(tx *Tx, cmd string, args ...interface{}) error {
_, err := doCount(tx, cmd, args...)
return err
}
func DoOne(tx *Tx, cmd string, args ...interface{}) error {
count, err := doCount(tx, cmd, args...)
if err != nil {
return err
}
if count != 1 {
query := SubQuery(cmd, args)
ShowStack()
log.Printf("DoOne:Error DB(%s) didn't change exactly 1 row(%d)",
query, count)
return fmt.Errorf("DoOne:Error DB(%s) didn't change exactly 1 row(%d)",
query, count)
}
return nil
}
func DoZeroOne(tx *Tx, cmd string, args ...interface{}) error {
count, err := doCount(tx, cmd, args...)
if err != nil {
return err
}
if count != 0 && count != 1 {
query := SubQuery(cmd, args)
ShowStack()
log.Printf("DoZeroOne:Error DB(%s) didn't change exactly 0/1 rows(%d)",
query, count)
return fmt.Errorf("DoZeroOne:Error DB(%s) didn't change exactly 0/1 rows(%d)",
query, count)
}
return nil
}
func DoOneTwo(tx *Tx, cmd string, args ...interface{}) error {
count, err := doCount(tx, cmd, args...)
if err != nil {
return err
}
if count != 1 && count != 2 {
query := SubQuery(cmd, args)
ShowStack()
log.Printf("DoOneTwo:Error DB(%s) didn't change exactly 1/2 rows(%d)",
query, count)
return fmt.Errorf("DoOneTwo:Error DB(%s) didn't change exactly 1/2 rows(%d)",
query, count)
}
return nil
}
func DoZeroTwo(tx *Tx, cmd string, args ...interface{}) error {
count, err := doCount(tx, cmd, args...)
if err != nil {
return err
}
if count != 0 && count != 2 {
query := SubQuery(cmd, args)
ShowStack()
log.Printf("DoZeroTwo:Error DB(%s) didn't change exactly 0/2 rows(%d)",
query, count)
return fmt.Errorf("DoZeroTwo:Error DB(%s) didn't change exactly 0/2 rows(%d)",
query, count)
}
return nil
}
func DoCount(tx *Tx, num int, cmd string, args ...interface{}) error {
log.VPrintf(4, "DoCount: %s", cmd)
count, err := doCount(tx, cmd, args...)
if err != nil {
return err
}
if count != num {
query := SubQuery(cmd, args)
ShowStack()
log.Printf("DoCount:Error DB(%s) didn't change exactly %d rows(%d)",
query, num, count)
return fmt.Errorf("DoCount:Error DB(%s) didn't change exactly %d rows(%d)",
query, num, count)
}
return nil
}
func DBExists(name string) bool {
log.VPrintf(3, ">Enter: DBExists %q", name)
defer log.VPrintf(3, "<Exit: DBExists")
db, err := sql.Open("mysql",
DBUSER+":"+DBPASSWORD+"@tcp("+DBHOST+":"+DBPORT+")/")
if err != nil {
panic(err)
}
defer db.Close()
rows, err := db.Query(`
SELECT SCHEMA_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME=?`, name)
if err != nil {
panic(err)
}
found := rows.Next()
log.VPrintf(3, "<Exit: found: %v", found)
return found
}
//go:embed init.sql
var initDB string
var firstTime = true
func OpenDB(name string) {
if firstTime {
log.VPrintf(1, "DB: %s:%s", DBHOST, DBPORT)
firstTime = false
}
log.VPrintf(3, ">Enter: OpenDB %q", name)
defer log.VPrintf(3, "<Exit: OpenDB")
// DB, err := sql.Open("mysql",
// DBUSER + ":"+DBPASSWORD+"@tcp(localhost:3306)/")
var err error
DB, err = sql.Open("mysql",
DBUSER+":"+DBPASSWORD+"@tcp("+DBHOST+":"+DBPORT+")/"+name)
if err != nil {
err = fmt.Errorf("Error talking to SQL: %s\n", err)
log.Print(err)
panic(err)
// return err
}
DB.SetMaxOpenConns(5)
DB.SetMaxIdleConns(5)
}
func CreateDB(name string) error {
log.VPrintf(3, ">Enter: CreateDB %q", name)
defer log.VPrintf(3, "<Exit: CreateDB")
db, err := sql.Open("mysql",
DBUSER+":"+DBPASSWORD+"@tcp("+DBHOST+":"+DBPORT+")/")
if err != nil {
panic(err)
}
defer db.Close()
if _, err = db.Exec("CREATE DATABASE " + name); err != nil {
panic(err)
}
if _, err = db.Exec("USE " + name); err != nil {
panic(err)
}
log.VPrintf(3, "Creating DB")
for _, cmd := range strings.Split(initDB, ";") {
cmd = strings.TrimSpace(cmd)
cmd = strings.Replace(cmd, "@", ";", -1) // Can't use ; in file
if cmd == "" {
continue
}
log.VPrintf(4, "CMD: %s", cmd)
if _, err := db.Exec(cmd); err != nil {
panic(fmt.Sprintf("Error on: %s\n%s", cmd, err))
}
}
return nil
}
func DeleteDB(name string) error {
log.VPrintf(3, "Deleting DB %q", name)
db, err := sql.Open("mysql",
DBUSER+":"+DBPASSWORD+"@tcp("+DBHOST+":"+DBPORT+")/")
if err != nil {
panic(err)
}
defer db.Close()
_, err = db.Exec("DROP DATABASE IF EXISTS " + name)
if err != nil {
panic(err)
}
return nil
}
func SubQuery(query string, args []interface{}) string {
argNum := 0
for pos := 0; pos < len(query); pos++ {
if ch := query[pos]; ch != '?' {
continue
}
if argNum >= len(args) {
panic(fmt.Sprintf("Extra ? in query at %q", query[pos:]))
}
val := fmt.Sprintf("%v", args[argNum])
query = fmt.Sprintf("%s'%s'%s", query[:pos], val, query[pos+1:])
pos += len(val) + 1 // one more will be added due to pos++
argNum++
}
if argNum != len(args) {
panic(fmt.Sprintf("Too many args passed into %q", query))
}
return query
}
/*
select * from FullTree
where
eID in (
select gID from FullTree
where PropName='Name' and PropValue='docker.com' and Path='apiProviders/7fbc05b2'
union select rID from FullTree
where PropName='Name' and PropValue='docker.com' and Path='apiProviders/7fbc05b2'
union select vID from FullTree
where PropName='Name' and PropValue='docker.com' and Path='apiProviders/7fbc05b2'
)
order by Path ;
Children:
select ft.* from FullTree as ft where ft.Path like concat((select Path from FullTree where PropValue=4 and LevelNum=2),'/%') order by ft.Path ;
Node+Children:
select ft.* from FullTree as ft where ft.Path like concat((select Path from FullTree where PropValue=4 and LevelNum=2),'%') order by ft.Path ;
Parents:
select ft.* from FullTree as ft where (select Path from FullTree where PropValue=4 and LevelNum=2) like concat(ft.Path, '/%') order by ft.Path;
Node+Parents:
select ft.* from FullTree as ft where (select Path from FullTree where PropValue=4 and LevelNum=2) like concat(ft.Path, '%') order by ft.Path;
NODES + Children:
select ft2.* from FullTree as ft right JOIN FullTree as ft2 on(ft2.Path like concat(ft.Path, '%')) where (ft.PropValue=3 and ft.LevelNum=2) or (ft.PropValue=4 and ft.LevelNum=3) group by ft2.eID,ft2.PropName Order by ft2.Path;
PARENTS (not NODES):
select ft2.* from FullTree as ft right JOIN FullTree as ft2 on(ft.Path like concat(ft2.Path,'/%')) where (ft.PropValue=3 and ft.LevelNum=2) or (ft.PropValue=4 and ft.LevelNum=3) group by ft2.eID,ft2.PropName Order by ft2.Path;
( ( exp1 AND expr2 ...) or ( expr3 AND expr4 ) )
Find IDs that match expr1 OR expr2
SELECT eID FROM FullTree WHERE ( (expr1) OR (expr2) );
SELECT eID FROM FullTree WHERE (Level=2 AND PropName='epoch' && PropValue='4');
Given an ID find all Parents (include original ID)
WITH RECURSIVE cte(eID,ParentID,Path) AS (
SELECT eID,ParentID,Path FROM Entities
WHERE eID in (
-- below find IDs of interes
SELECT eID FROM FullTree
WHERE (PropName='labels.int' AND PropValue=3 AND Level=2)
-- end of ID selection
)
UNION ALL SELECT e.eID,e.ParentID,e.Path FROM Entities AS e
INNER JOIN cte ON e.eID=cte.ParentID)
SELECT * FROM cte ;
Given an ID find all Leaves (with recursion)
WITH RECURSIVE cte(eID,ParentID,Path) AS (
SELECT eID,ParentID,Path FROM Entities
WHERE eID='f91a4ec9'
UNION ALL SELECT e.eID,e.ParentID,e.Path FROM Entities AS e
INNER JOIN cte ON e.ParentID=cte.eID)
SELECT eID,ParentID,Path FROM cte
WHERE eID IN (SELECT * FROM Leaves);
Given an ID find all Leaves (w/o recursion)
Should use IDs instead of Path to pick-up the Registry itself
SELECT e2.eID,e2.ParentID,e2.Path FROM Entities AS e1
RIGHT JOIN Entities AS e2 ON (e2.Path=e1.Path OR e2.Path LIKE
CONCAT(e1.Path,'%')) WHERE e1.eID in (
-- below finds IDs of interest
SELECT eID FROM FullTree
WHERE (PropName='labels.int' AND PropValue=3 AND Level=2)
-- end of ID selection
)
AND e2.eID IN (SELECT * from Leaves);
Given an ID, find all leaves, and then find all Parents
-- Finding all parents
WITH RECURSIVE cte(eID,ParentID,Path) AS (
SELECT eID,ParentID,Path FROM Entities
WHERE eID in (
-- below find IDs of interest (finding all leaves)
SELECT e2.eID FROM Entities AS e1
RIGHT JOIN Entities AS e2 ON (
e2.RegID=e1.RegID AND
(e2.Path=e1.Path OR e2.Path LIKE CONCAT(e1.Path,'%'))
)
WHERE e1.eID in (
-- below finds SeachNodes/IDs of interest
-- Add regID into the search
SELECT eID FROM FullTree
WHERE (PropName='labels.int' AND PropValue=3 AND Level=2)
-- end of ID selection
)
AND e2.eID IN (SELECT * from Leaves)
-- end of Leaves/ID selection
)
UNION ALL SELECT e.eID,e.ParentID,e.Path FROM Entities AS e
INNER JOIN cte ON e.eID=cte.ParentID)
SELECT * FROM cte ;
(expr1 AND expr2)
WITH RECURSIVE cte(eID,ParentID,Path) AS (
SELECT eID,ParentID,Path FROM Entities
WHERE eID in (
-- below find IDs of interest (finding all leaves)
-- start of (expr1 and expr2 and expr3)
SELECT list.eID FROM (
SELECT count(*) as cnt,e2.eID,e2.Path FROM Entities AS e1
RIGHT JOIN (
-- below finds SeachNodes/IDs of interest
-- Add regID into the search
SELECT eID,Path FROM FullTree
WHERE (CONCAT(Abstract,'.',PropName)='myGroups/ress.labels.int')
UNION ALL
SELECT eID,Path FROM FullTree
WHERE (PropName='labels.int' AND PropValue=3 AND Level=2)
UNION ALL
SELECT eID,Path from FullTree
WHERE (PropName='id' AND PropValue='g1' AND Level=1)
-- end of ID selection
) as res ON ( res.eID=e1.eID )
JOIN Entities AS e2 ON (
(e2.Path=res.Path OR e2.Path LIKE CONCAT(res.Path,'%'))
AND e2.eID IN (SELECT * from Leaves)
) GROUP BY e2.eID
-- end of Leaves/ID selection
) as list
WHERE list.cnt=3
-- end of (expr1 and expr2 and expr3)
-- ADD the next OR expr here
UNION
-- start of expr4
SELECT list.eID FROM (
SELECT count(*) as cnt,e2.eID,e2.Path FROM Entities AS e1
RIGHT JOIN (
-- below finds SeachNodes/IDs of interest
-- Add regID into the search
SELECT eID,Path FROM FullTree
WHERE (PropName='defaultVersionId' AND PropValue='v1.0' AND Level=2)
-- end of ID selection
) as res ON ( res.eID=e1.eID )
JOIN Entities AS e2 ON (
(e2.Path=res.Path OR e2.Path LIKE CONCAT(res.Path,'%'))
AND e2.eID IN (SELECT * from Leaves)
) GROUP BY e2.eID
-- end of Leaves/ID selection
) as list
WHERE list.cnt=1
)
UNION ALL SELECT e.eID,e.ParentID,e.Path FROM Entities AS e
INNER JOIN cte ON e.eID=cte.ParentID)
SELECT * FROM cte ;
*/