1 29 30 package com.caucho.db.sql; 31 32 import com.caucho.db.Database; 33 import com.caucho.db.store.Transaction; 34 import com.caucho.db.table.Column; 35 import com.caucho.db.table.Table; 36 import com.caucho.db.table.TableIterator; 37 import com.caucho.log.Log; 38 import com.caucho.sql.SQLExceptionWrapper; 39 40 import java.sql.SQLException ; 41 import java.util.ArrayList ; 42 import java.util.logging.Logger ; 43 44 class InsertQuery extends Query { 45 private static final Logger log = Log.open(InsertQuery.class); 46 47 private Table _table; 48 49 private ArrayList <Column> _columns; 50 private ArrayList <Expr> _values; 51 52 InsertQuery(Database db, String sql, Table table, ArrayList <Column> columns) 53 throws SQLException 54 { 55 super(db, sql, null); 56 57 _table = table; 58 59 _columns = columns; 60 } 61 62 public boolean isReadOnly() 63 { 64 return false; 65 } 66 67 public void setValues(ArrayList <Expr> values) 68 { 69 _values = values; 70 } 71 72 void init() 73 throws SQLException 74 { 75 Column []tableColumns = _table.getColumns(); 76 77 for (int i = 0; i < tableColumns.length; i++) { 78 Column column = tableColumns[i]; 79 80 Expr defaultExpr = column.getDefault(); 81 82 if (column.getAutoIncrement() > 0) { 83 defaultExpr = new AutoIncrementExpr(column.getTable()); 84 } 85 86 if (defaultExpr == null) 87 continue; 88 89 int j = 0; 90 for (; j < _columns.size(); j++) { 91 if (_columns.get(j) == column) 92 break; 93 } 94 95 if (j == _columns.size()) { 96 _columns.add(column); 97 _values.add(new NullExpr()); 98 } 99 100 _values.set(j, new DefaultExpr(_values.get(j), defaultExpr)); 101 } 102 } 103 104 107 public void execute(QueryContext queryContext, Transaction xa) 108 throws SQLException 109 { 110 TableIterator []rows = new TableIterator[1]; 111 rows[0] = _table.createTableIterator(); 112 queryContext.init(xa, rows, isReadOnly()); 113 114 try { 115 _table.insert(queryContext, xa, _columns, _values); 116 117 queryContext.setRowUpdateCount(1); 118 } catch (java.io.IOException e) { 119 throw new SQLExceptionWrapper(e); 120 } finally { 121 queryContext.unlock(); 122 } 123 } 124 125 public String toString() 126 { 127 return "InsertQuery[]"; 128 } 129 } 130 | Popular Tags |