1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import org.compiere.util.*; 20 import java.io.*; 21 22 29 public class MQuery implements Serializable 30 { 31 34 public MQuery () 35 { 36 } 38 42 public MQuery (String TableName) 43 { 44 m_TableName = TableName; 45 } 47 51 public MQuery (int AD_Table_ID) 52 { 53 String sql = "SELECT TableName FROM AD_Table WHERE AD_Table_ID=?"; 54 try 55 { 56 PreparedStatement pstmt = DB.prepareStatement(sql); 57 pstmt.setInt (1, AD_Table_ID); 58 ResultSet rs = pstmt.executeQuery(); 59 if (rs.next()) 60 m_TableName = rs.getString(1); 61 else 62 Log.error("MQuery - Did not find AD_Table_ID=" + AD_Table_ID); 63 rs.close(); 64 pstmt.close(); 65 } 66 catch (SQLException e) 67 { 68 Log.error("MQuery", e); 69 } 70 } 72 73 74 private String m_TableName = ""; 75 76 private ArrayList m_list = new ArrayList(); 77 78 79 80 81 public static final String EQUAL = "="; 82 public static final int EQUAL_INDEX = 0; 83 public static final String NOT_EQUAL = "!="; 84 public static final String LIKE = " LIKE "; 85 public static final String NOT_LIKE = " NOT LIKE "; 86 public static final String GREATER = ">"; 87 public static final String GREATER_EQUAL = ">="; 88 public static final String LESS = "<"; 89 public static final String LESS_EQUAL = "<="; 90 public static final String BETWEEN = " BETWEEN "; 91 public static final int BETWEEN_INDEX = 8; 92 93 public static final ValueNamePair[] OPERATORS = new ValueNamePair[] { 94 new ValueNamePair (EQUAL, " = "), new ValueNamePair (NOT_EQUAL, " != "), 96 new ValueNamePair (LIKE, " ~ "), 97 new ValueNamePair (NOT_LIKE, " !~ "), 98 new ValueNamePair (GREATER, " > "), 99 new ValueNamePair (GREATER_EQUAL, " >= "), new ValueNamePair (LESS, " < "), 101 new ValueNamePair (LESS_EQUAL, " <= "), 102 new ValueNamePair (BETWEEN, " >-< ") }; 104 105 106 107 115 public void addRestriction (String ColumnName, String Operator, 116 Object Code, String InfoName, String InfoDisplay) 117 { 118 Restriction r = new Restriction (ColumnName, Operator, 119 Code, InfoName, InfoDisplay); 120 m_list.add(r); 121 } 123 129 public void addRestriction (String ColumnName, String Operator, 130 Object Code) 131 { 132 Restriction r = new Restriction (ColumnName, Operator, 133 Code, null, null); 134 m_list.add(r); 135 } 137 143 public void addRestriction (String ColumnName, String Operator, 144 int Code) 145 { 146 Restriction r = new Restriction (ColumnName, Operator, 147 new Integer (Code), null, null); 148 m_list.add(r); 149 } 151 160 public void addRangeRestriction (String ColumnName, 161 Object Code, Object Code_to, 162 String InfoName, String InfoDisplay, String InfoDisplay_to) 163 { 164 Restriction r = new Restriction (ColumnName, Code, Code_to, 165 InfoName, InfoDisplay, InfoDisplay_to); 166 m_list.add(r); 167 } 169 175 public void addRangeRestriction (String ColumnName, 176 Object Code, Object Code_to) 177 { 178 Restriction r = new Restriction (ColumnName, Code, Code_to, 179 null, null, null); 180 m_list.add(r); 181 } 183 187 protected void addRestriction (Restriction r) 188 { 189 m_list.add(r); 190 } 192 196 public void addRestriction (String whereClause) 197 { 198 if (whereClause == null || whereClause.trim().length() == 0) 199 return; 200 Restriction r = new Restriction (whereClause); 201 m_list.add(r); 202 } 204 205 206 210 public String getWhereClause () 211 { 212 return getWhereClause(false); 213 } 215 220 public String getWhereClause (boolean fullyQualified) 221 { 222 boolean qualified = fullyQualified; 223 if (qualified && (m_TableName == null || m_TableName.length() == 0)) 224 qualified = false; 225 StringBuffer sb = new StringBuffer (); 227 for (int i = 0; i < m_list.size(); i++) 228 { 229 Restriction r = (Restriction)m_list.get(i); 230 if (i != 0) 231 sb.append(r.andCondition ? " AND " : " OR "); 232 if (qualified) 233 sb.append(r.getSQL(m_TableName)); 234 else 235 sb.append(r.getSQL(null)); 236 } 237 return sb.toString(); 238 } 240 246 public String getWhereClause (int index) 247 { 248 StringBuffer sb = new StringBuffer (); 249 if (index >= 0 && index < m_list.size()) 250 { 251 Restriction r = (Restriction)m_list.get(index); 252 sb.append(r.getSQL(null)); 253 } 254 return sb.toString(); 255 } 257 261 public int getRestrictionCount() 262 { 263 return m_list.size(); 264 } 266 270 public boolean isActive() 271 { 272 return m_list.size() != 0; 273 } 275 279 public String getTableName () 280 { 281 return m_TableName; 282 } 284 288 public void setTableName (String TableName) 289 { 290 m_TableName = TableName; 291 } 293 294 295 300 public String getColumnName (int index) 301 { 302 if (index < 0 || index >= m_list.size()) 303 return null; 304 Restriction r = (Restriction)m_list.get(index); 305 return r.ColumnName; 306 } 308 313 protected void setColumnName (int index, String ColumnName) 314 { 315 if (index < 0 || index >= m_list.size()) 316 return; 317 Restriction r = (Restriction)m_list.get(index); 318 r.ColumnName = ColumnName; 319 } 321 326 public String getOperator (int index) 327 { 328 if (index < 0 || index >= m_list.size()) 329 return null; 330 Restriction r = (Restriction)m_list.get(index); 331 return r.Operator; 332 } 334 339 public String getInfoDisplay (int index) 340 { 341 if (index < 0 || index >= m_list.size()) 342 return null; 343 Restriction r = (Restriction)m_list.get(index); 344 return r.InfoDisplay; 345 } 347 352 public String getInfoDisplay_to (int index) 353 { 354 if (index < 0 || index >= m_list.size()) 355 return null; 356 Restriction r = (Restriction)m_list.get(index); 357 return r.InfoDisplay_to; 358 } 360 365 public String getInfoName(int index) 366 { 367 if (index < 0 || index >= m_list.size()) 368 return null; 369 Restriction r = (Restriction)m_list.get(index); 370 return r.InfoName; 371 } 373 378 public String getInfoOperator(int index) 379 { 380 if (index < 0 || index >= m_list.size()) 381 return null; 382 Restriction r = (Restriction)m_list.get(index); 383 return r.getInfoOperator(); 384 } 386 391 public String getInfoDisplayAll (int index) 392 { 393 if (index < 0 || index >= m_list.size()) 394 return null; 395 Restriction r = (Restriction)m_list.get(index); 396 return r.getInfoDisplayAll(); 397 } 399 403 public String toString() 404 { 405 if (isActive()) 406 return getWhereClause(true); 407 return "MQuery[" + m_TableName + "]"; 408 } 410 414 public MQuery deepCopy() 415 { 416 MQuery newQuery = new MQuery(m_TableName); 417 for (int i = 0; i < m_list.size(); i++) 418 newQuery.addRestriction((Restriction)m_list.get(i)); 419 return newQuery; 420 } 422 423 424 431 public static MQuery getEqualQuery (String columnName, Object value) 432 { 433 MQuery query = new MQuery(); 434 query.addRestriction(columnName, EQUAL, value); 435 return query; 436 } 438 445 public static MQuery getEqualQuery (String columnName, int value) 446 { 447 MQuery query = new MQuery(); 448 query.addRestriction(columnName, EQUAL, new Integer (value)); 449 return query; 450 } 452 } 454 455 456 459 class Restriction implements Serializable 460 { 461 469 Restriction (String ColumnName, String Operator, 470 Object Code, String InfoName, String InfoDisplay) 471 { 472 this.ColumnName = ColumnName.trim(); 473 if (InfoName != null) 474 this.InfoName = InfoName; 475 else 476 this.InfoName = this.ColumnName; 477 this.Operator = Operator; 479 this.Code = Code; 481 if (this.Code instanceof String ) 482 { 483 if (this.Code.toString().startsWith("'")) 484 this.Code = this.Code.toString().substring(1); 485 if (this.Code.toString().endsWith("'")) 486 this.Code = this.Code.toString().substring(0, this.Code.toString().length()-2); 487 } 488 if (InfoDisplay != null) 489 this.InfoDisplay = InfoDisplay.trim(); 490 else if (this.Code != null) 491 this.InfoDisplay = this.Code.toString(); 492 } 494 503 Restriction (String ColumnName, 504 Object Code, Object Code_to, 505 String InfoName, String InfoDisplay, String InfoDisplay_to) 506 { 507 this (ColumnName, MQuery.BETWEEN, Code, InfoName, InfoDisplay); 508 509 this.Code_to = Code_to; 511 if (this.Code_to instanceof String ) 512 { 513 if (this.Code_to.toString().startsWith("'")) 514 this.Code_to = this.Code_to.toString().substring(1); 515 if (this.Code_to.toString().endsWith("'")) 516 this.Code_to = this.Code_to.toString().substring(0, this.Code_to.toString().length()-2); 517 } 518 if (InfoDisplay_to != null) 520 this.InfoDisplay_to = InfoDisplay_to.trim(); 521 else if (this.Code_to != null) 522 this.InfoDisplay_to = this.Code_to.toString(); 523 } 525 529 Restriction (String whereClause) 530 { 531 DircetWhereClause = whereClause; 532 } 534 535 protected String DircetWhereClause = null; 536 537 protected String ColumnName; 538 539 protected String InfoName; 540 541 protected String Operator; 542 543 protected Object Code; 544 545 protected String InfoDisplay; 546 547 protected Object Code_to; 548 549 protected String InfoDisplay_to; 550 551 protected boolean andCondition = true; 552 553 558 public String getSQL (String tableName) 559 { 560 if (DircetWhereClause != null) 561 return DircetWhereClause; 562 StringBuffer sb = new StringBuffer (); 564 if (tableName != null && tableName.length() > 0) 565 { 566 int pos = ColumnName.lastIndexOf('(')+1; int end = ColumnName.indexOf(')'); 569 if (pos != -1 && end != -1) 571 sb.append(ColumnName.substring(0, pos)) 572 .append(tableName).append(".").append(ColumnName.substring(pos, end)) 573 .append(ColumnName.substring(end)); 574 else 575 sb.append(tableName).append(".").append(ColumnName); 576 } 577 else 578 sb.append(ColumnName); 579 sb.append(Operator); 581 if (Code instanceof String ) 582 sb.append(DB.TO_STRING(Code.toString())); 583 else if (Code instanceof Timestamp) 584 sb.append(DB.TO_DATE((Timestamp)Code)); 585 else 586 sb.append(Code); 587 if (MQuery.BETWEEN.equals(Operator)) 590 { 591 sb.append(" AND "); 592 if (Code_to instanceof String ) 593 sb.append(DB.TO_STRING(Code_to.toString())); 594 else if (Code_to instanceof Timestamp) 595 sb.append(DB.TO_DATE((Timestamp)Code_to)); 596 else 597 sb.append(Code_to); 598 } 599 return sb.toString(); 600 } 602 606 public String toString() 607 { 608 return getSQL(null); 609 } 611 615 public String getInfoName() 616 { 617 return InfoName; 618 } 620 624 public String getInfoOperator() 625 { 626 for (int i = 0; i < MQuery.OPERATORS.length; i++) 627 { 628 if (MQuery.OPERATORS[i].getValue().equals(Operator)) 629 return MQuery.OPERATORS[i].getName(); 630 } 631 return Operator; 632 } 634 638 public String getInfoDisplayAll() 639 { 640 if (InfoDisplay_to == null) 641 return InfoDisplay; 642 StringBuffer sb = new StringBuffer (InfoDisplay); 643 sb.append(" - ").append(InfoDisplay_to); 644 return sb.toString(); 645 } 647 } | Popular Tags |