1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.command.dml.Query; 10 import org.h2.engine.Database; 11 import org.h2.engine.Session; 12 import org.h2.message.Message; 13 import org.h2.schema.Schema; 14 import org.h2.table.TableView; 15 16 public class CreateView extends SchemaCommand { 17 18 private Query select; 19 private String viewName; 20 private boolean ifNotExists; 21 private String selectSQL; 22 private String [] columnNames; 23 private String comment; 24 25 public CreateView(Session session, Schema schema) { 26 super(session, schema); 27 } 28 29 public void setViewName(String name) { 30 viewName = name; 31 } 32 33 public void setSelect(Query select) { 34 this.select = select; 35 } 36 37 public int update() throws SQLException { 38 session.commit(); 40 Database db = session.getDatabase(); 41 if(getSchema().findTableOrView(session, viewName)!=null) { 42 if (ifNotExists) { 43 return 0; 44 } 45 throw Message.getSQLException(Message.VIEW_ALREADY_EXISTS_1, 46 viewName); 47 } 48 int id = getObjectId(true, true); 49 String querySQL; 50 if(select == null) { 51 querySQL = selectSQL; 52 } else { 53 querySQL = select.getSQL(); 54 } 55 TableView view = new TableView(getSchema(), id, viewName, querySQL, null, columnNames, session); 56 view.setComment(comment); 57 db.addSchemaObject(session, view); 58 return 0; 59 } 60 61 public void setIfNotExists(boolean ifNotExists) { 62 this.ifNotExists = ifNotExists; 63 } 64 65 public void setSelectSQL(String selectSQL) { 66 this.selectSQL = selectSQL; 67 } 68 69 public void setColumnNames(String [] cols) { 70 this.columnNames = cols; 71 } 72 73 public void setComment(String comment) { 74 this.comment = comment; 75 } 76 77 } 78 | Popular Tags |