File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\Analyze.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.command.Prepared;
11 import org.h2.engine.Database;
12 import org.h2.engine.Right;
13 import org.h2.engine.Session;
14 import org.h2.result.ResultInterface;
15 import org.h2.table.Column;
16 import org.h2.table.RegularTable;
17 import org.h2.table.Table;
18 import org.h2.util.StatementBuilder;
19 import org.h2.value.Value;
20
21 /**
22 * This class represents the statement
23 * ANALYZE
24 */
25 public class Analyze extends DefineCommand {
26
27 /**
28 * The sample size.
29 */
30 private int sampleRows;
31
32 public Analyze(Session session) {
33 super(session);
34 sampleRows = session.getDatabase().getSettings().analyzeSample;
35 }
36
37 public int update() {
38 session.commit(true);
39 session.getUser().checkAdmin();
40 Database db = session.getDatabase();
41 for (Table table : db.getAllTablesAndViews(false)) {
42 analyzeTable(session, table, sampleRows, true);
43 }
44 return 0;
45 }
46
47 /**
48 * Analyze this table.
49 *
50 * @param session the session
51 * @param table the table
52 * @param sample the number of sample rows
53 * @param manual whether the command was called by the user
54 */
55 public static void analyzeTable(Session session, Table table, int sample, boolean manual) {
56 if (!(table instanceof RegularTable) || table.isHidden() || session == null) {
57 return;
58 }
59 if (!manual) {
60 if (session.getDatabase().isSysTableLocked()) {
61 return;
62 }
63 if (table.hasSelectTrigger()) {
64 return;
65 }
66 }
67 if (table.isTemporary() && !table.isGlobalTemporary()
68 && session.findLocalTempTable(table.getName()) == null) {
69 return;
70 }
71 if (table.isLockedExclusively() && !table.isLockedExclusivelyBy(session)) {
72 return;
73 }
74 if (!session.getUser().hasRight(table, Right.SELECT)) {
75 return;
76 }
77 if (session.getCancel() != 0) {
78 // if the connection is closed and there is something to undo
79 return;
80 }
81 Database db = session.getDatabase();
82 StatementBuilder buff = new StatementBuilder("SELECT ");
83 Column[] columns = table.getColumns();
84 for (Column col : columns) {
85 buff.appendExceptFirst(", ");
86 int type = col.getType();
87 if (type == Value.BLOB || type == Value.CLOB) {
88 // can not index LOB columns, so calculating
89 // the selectivity is not required
90 buff.append("100");
91 } else {
92 buff.append("SELECTIVITY(").append(col.getSQL()).append(')');
93 }
94 }
95 buff.append(" FROM ").append(table.getSQL());
96 if (sample > 0) {
97 buff.append(" LIMIT 1 SAMPLE_SIZE ").append(sample);
98 }
99 String sql = buff.toString();
100 Prepared command = session.prepare(sql);
101 ResultInterface result = command.query(0);
102 result.next();
103 for (int j = 0; j < columns.length; j++) {
104 int selectivity = result.currentRow()[j].getInt();
105 columns[j].setSelectivity(selectivity);
106 }
107 if (manual) {
108 db.update(session, table);
109 } else {
110 Session s = db.getSystemSession();
111 if (s != session) {
112 // if the current session is the system session
113 // (which is the case if we are within a trigger)
114 // then we can't update the statistics because
115 // that would unlock all locked objects
116 db.update(s, table);
117 s.commit(true);
118 }
119 }
120 }
121
122 public void setTop(int top) {
123 this.sampleRows = top;
124 }
125
126 public int getType() {
127 return CommandInterface.ANALYZE;
128 }
129
130 }
131