OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 61 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 25
Line 452... Line 452...
452
        public final boolean isBackwards() {
452
        public final boolean isBackwards() {
453
            return !this.from.isForeign(this.from.getSingleField());
453
            return !this.from.isForeign(this.from.getSingleField());
454
        }
454
        }
455
 
455
 
456
        @Override
456
        @Override
-
 
457
        public int hashCode() {
-
 
458
            final int prime = 31;
-
 
459
            int result = 1;
-
 
460
            result = prime * result + this.fieldCount;
-
 
461
            result = prime * result + ((this.from == null) ? 0 : this.from.hashCode());
-
 
462
            result = prime * result + this.linkIndex;
-
 
463
            result = prime * result + this.t.hashCode();
-
 
464
            return result;
-
 
465
        }
-
 
466
 
-
 
467
        @Override
-
 
468
        public boolean equals(Object obj) {
-
 
469
            if (this == obj)
-
 
470
                return true;
-
 
471
            if (obj == null)
-
 
472
                return false;
-
 
473
            if (getClass() != obj.getClass())
-
 
474
                return false;
-
 
475
            final GraphNode other = (GraphNode) obj;
-
 
476
            return this.fieldCount == other.fieldCount && this.linkIndex == other.linkIndex && this.t.equals(other.t) && CompareUtils.equals(this.from, other.from);
-
 
477
        }
-
 
478
 
-
 
479
        @Override
457
        public String toString() {
480
        public String toString() {
458
            final String link = this.from == null ? "" : " linked to " + getLinkIndex() + " by " + this.getFromName() + (this.isBackwards() ? " backwards" : " forewards");
481
            final String link = this.from == null ? "" : " linked to " + getLinkIndex() + " by " + this.getFromName() + (this.isBackwards() ? " backwards" : " forewards");
459
            return this.getFieldCount() + " fields of " + this.getTable() + link;
482
            return this.getFieldCount() + " fields of " + this.getTable() + link;
460
        }
483
        }
461
    }
484
    }
462
 
485
 
463
    /**
-
 
464
     * Execute the request transformed by <code>selTransf</code> and return the result as a list of
486
    static private final class RSH implements ResultSetHandler {
465
     * SQLRowValues.
487
        private final List<String> selectFields;
466
     * 
-
 
467
     * @return a list of SQLRowValues, one item per row, each item having the same structure as the
488
        private final List<GraphNode> graphNodes;
-
 
489
 
468
     *         SQLRowValues passed to the constructor.
490
        private RSH(List<String> selectFields, List<GraphNode> l) {
469
     */
-
 
470
    public final List<SQLRowValues> fetch() {
491
            this.selectFields = selectFields;
471
        return this.fetch(true);
492
            this.graphNodes = l;
472
    }
493
        }
473
 
494
 
474
    private final List<SQLRowValues> fetch(final boolean merge) {
-
 
475
        final SQLSelect req = this.getReq();
-
 
476
        // getName() would take 5% of ResultSetHandler.handle()
-
 
477
        final List<String> selectFields = new ArrayList<String>(req.getSelectFields().size());
-
 
478
        for (final SQLField f : req.getSelectFields())
-
 
479
            selectFields.add(f.getName());
-
 
480
        final SQLTable table = getGraph().getTable();
-
 
481
 
-
 
482
        // create a flat list of the graph nodes, we just need the table, field count and the index
-
 
483
        // in this list of its linked table, eg for CPI -> LOCAL -> BATIMENT -> SITE :
-
 
484
        // <LOCAL,2,0>, <BATIMENT,2,0>, <SITE,5,1>, <CPI,4,0>
-
 
485
        final int graphSize = this.getGraph().getGraph().size();
-
 
486
        final List<GraphNode> l = new ArrayList<GraphNode>(graphSize);
-
 
487
        walk(0, new ITransformer<State<Integer>, Integer>() {
-
 
488
            @Override
-
 
489
            public Integer transformChecked(State<Integer> input) {
-
 
490
                final int index = l.size();
-
 
491
                l.add(new GraphNode(input));
-
 
492
                return index;
-
 
493
            }
-
 
494
        });
-
 
495
        assert l.size() == graphSize : "All nodes weren't explored once : " + l.size() + " != " + graphSize;
-
 
496
 
-
 
497
        @SuppressWarnings("unchecked")
-
 
498
        final List<SQLRowValues> res = (List<SQLRowValues>) table.getBase().getDataSource().execute(req.asString(), new ResultSetHandler() {
-
 
499
            @Override
495
        @Override
500
            public Object handle(final ResultSet rs) throws SQLException {
496
        public Object handle(final ResultSet rs) throws SQLException {
-
 
497
            final List<GraphNode> l = this.graphNodes;
-
 
498
            final int graphSize = l.size();
501
                int nextToLink = 0;
499
            int nextToLink = 0;
502
                final List<Future<?>> futures = new ArrayList<Future<?>>();
500
            final List<Future<?>> futures = new ArrayList<Future<?>>();
503
 
501
 
504
                final List<SQLRowValues> res = new ArrayList<SQLRowValues>(64);
502
            final List<SQLRowValues> res = new ArrayList<SQLRowValues>(64);
505
                final List<List<SQLRowValues>> rows = Collections.synchronizedList(new ArrayList<List<SQLRowValues>>(64));
503
            final List<List<SQLRowValues>> rows = Collections.synchronizedList(new ArrayList<List<SQLRowValues>>(64));
Line 508... Line 506...
508
                while (rs.next()) {
506
            while (rs.next()) {
509
                    int rsIndex = 1;
507
                int rsIndex = 1;
510
 
508
 
511
                    // MAYBE cancel() futures
509
                // MAYBE cancel() futures
512
                    if (Thread.currentThread().isInterrupted())
510
                if (Thread.currentThread().isInterrupted())
513
                        throw new RTInterruptedException("interrupted while fetching " + SQLRowValuesListFetcher.this);
511
                    throw new RTInterruptedException("interrupted while fetching");
514
                    final List<SQLRowValues> row = new ArrayList<SQLRowValues>(graphSize);
512
                final List<SQLRowValues> row = new ArrayList<SQLRowValues>(graphSize);
515
                    for (int i = 0; i < graphSize; i++) {
513
                for (int i = 0; i < graphSize; i++) {
516
                        final GraphNode node = l.get(i);
514
                    final GraphNode node = l.get(i);
517
                        final SQLRowValues creatingVals = new SQLRowValues(node.getTable());
515
                    final SQLRowValues creatingVals = new SQLRowValues(node.getTable());
518
                        if (i == 0)
516
                    if (i == 0)
Line 521... Line 519...
521
                        final int stop = rsIndex + node.getFieldCount();
519
                    final int stop = rsIndex + node.getFieldCount();
522
                        for (; rsIndex < stop; rsIndex++) {
520
                    for (; rsIndex < stop; rsIndex++) {
523
                            try {
521
                        try {
524
                                // -1 since rs starts at 1
522
                            // -1 since rs starts at 1
525
                                // field names checked below
523
                            // field names checked below
526
                                creatingVals.put(selectFields.get(rsIndex - 1), rs.getObject(rsIndex), false);
524
                            creatingVals.put(this.selectFields.get(rsIndex - 1), rs.getObject(rsIndex), false);
527
                            } catch (SQLException e) {
525
                        } catch (SQLException e) {
528
                                throw new IllegalStateException("unable to fill " + creatingVals, e);
526
                            throw new IllegalStateException("unable to fill " + creatingVals, e);
529
                            }
527
                        }
530
                        }
528
                    }
531
                        row.add(creatingVals);
529
                    row.add(creatingVals);
Line 566... Line 564...
566
                    }
564
                }
567
                }
565
            }
568
 
566
 
569
                return res;
567
            return res;
570
            }
568
        }
-
 
569
 
-
 
570
        @Override
-
 
571
        public int hashCode() {
-
 
572
            final int prime = 31;
-
 
573
            int result = 1;
-
 
574
            result = prime * result + this.graphNodes.hashCode();
-
 
575
            result = prime * result + this.selectFields.hashCode();
-
 
576
            return result;
-
 
577
        }
-
 
578
 
-
 
579
        @Override
-
 
580
        public boolean equals(Object obj) {
-
 
581
            if (this == obj)
-
 
582
                return true;
-
 
583
            if (obj == null)
-
 
584
                return false;
-
 
585
            if (getClass() != obj.getClass())
-
 
586
                return false;
-
 
587
            final RSH other = (RSH) obj;
-
 
588
            return this.graphNodes.equals(other.graphNodes) && this.selectFields.equals(other.selectFields);
-
 
589
        }
-
 
590
 
-
 
591
    }
-
 
592
 
-
 
593
    /**
-
 
594
     * Execute the request transformed by <code>selTransf</code> and return the result as a list of
-
 
595
     * SQLRowValues. NOTE: this method doesn't use the cache of SQLDataSource.
-
 
596
     * 
-
 
597
     * @return a list of SQLRowValues, one item per row, each item having the same structure as the
-
 
598
     *         SQLRowValues passed to the constructor.
-
 
599
     */
-
 
600
    public final List<SQLRowValues> fetch() {
-
 
601
        return this.fetch(true);
-
 
602
    }
-
 
603
 
-
 
604
    private final List<SQLRowValues> fetch(final boolean merge) {
-
 
605
        final SQLSelect req = this.getReq();
-
 
606
        // getName() would take 5% of ResultSetHandler.handle()
-
 
607
        final List<String> selectFields = new ArrayList<String>(req.getSelectFields().size());
-
 
608
        for (final SQLField f : req.getSelectFields())
-
 
609
            selectFields.add(f.getName());
-
 
610
        final SQLTable table = getGraph().getTable();
-
 
611
 
-
 
612
        // create a flat list of the graph nodes, we just need the table, field count and the index
-
 
613
        // in this list of its linked table, eg for CPI -> LOCAL -> BATIMENT -> SITE :
-
 
614
        // <LOCAL,2,0>, <BATIMENT,2,0>, <SITE,5,1>, <CPI,4,0>
-
 
615
        final int graphSize = this.getGraph().getGraph().size();
-
 
616
        final List<GraphNode> l = new ArrayList<GraphNode>(graphSize);
-
 
617
        walk(0, new ITransformer<State<Integer>, Integer>() {
-
 
618
            @Override
-
 
619
            public Integer transformChecked(State<Integer> input) {
-
 
620
                final int index = l.size();
-
 
621
                l.add(new GraphNode(input));
-
 
622
                return index;
-
 
623
            }
571
        }, false);
624
        });
-
 
625
        assert l.size() == graphSize : "All nodes weren't explored once : " + l.size() + " != " + graphSize;
-
 
626
 
-
 
627
        // if we wanted to use the cache, we'd need to copy the returned list and its items (i.e.
-
 
628
        // deepCopy()), since we modify them afterwards. Or perhaps include the code after this line
-
 
629
        // into the result set handler.
-
 
630
        final IResultSetHandler rsh = new IResultSetHandler(new RSH(selectFields, l), false);
-
 
631
        @SuppressWarnings("unchecked")
-
 
632
        final List<SQLRowValues> res = (List<SQLRowValues>) table.getBase().getDataSource().execute(req.asString(), rsh, false);
572
        // e.g. list of batiment pointing to site
633
        // e.g. list of batiment pointing to site
573
        final List<SQLRowValues> merged = merge && this.fetchReferents() ? merge(res) : res;
634
        final List<SQLRowValues> merged = merge && this.fetchReferents() ? merge(res) : res;
574
        if (this.grafts.size() > 0) {
635
        if (this.grafts.size() > 0) {
575
            for (final Entry<Path, Map<Path, SQLRowValuesListFetcher>> graftPlaceEntry : this.grafts.entrySet()) {
636
            for (final Entry<Path, Map<Path, SQLRowValuesListFetcher>> graftPlaceEntry : this.grafts.entrySet()) {
576
                // e.g. BATIMENT
637
                // e.g. BATIMENT