More integration of the database into the objects. So far we have AuctionEntry, AuctionInfo, and Category as DB-backed classes, and Seller as a partially DB-backed object. (It needs to be refactored to be fully HashBacked.)
There are evidently still problems with updates nuking info. :(
- A jbidwatcher/trunk/src/com/jbidwatcher/search/Category.java
- M jbidwatcher/trunk/src/com/jbidwatcher/auction/AuctionEntry.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/auction/AuctionInfo.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/auction/Seller.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/auction/server/AuctionServerManager.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/util/HashBacked.java view
- M jbidwatcher/trunk/src/com/jbidwatcher/util/db/AuctionDB.java view
| 205 | 206 | |
|---|---|---|
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import com.jbidwatcher.Constants; |
| 9 | import com.jbidwatcher.search.Category; | |
| 9 | 10 | import com.jbidwatcher.auction.server.AuctionServer; |
| 10 | 11 | import com.jbidwatcher.auction.server.AuctionServerManager; |
| 11 | 12 | import com.jbidwatcher.config.JConfig; |
| 12 | 13 | import com.jbidwatcher.queue.AuctionQObject; |
| 13 | 14 | import com.jbidwatcher.queue.MQFactory; |
| 14 | 15 | import com.jbidwatcher.util.*; |
| 16 | import com.jbidwatcher.util.db.DBRecord; | |
| 17 | import com.jbidwatcher.util.db.AuctionDB; | |
| 15 | 18 | import com.jbidwatcher.xml.XMLElement; |
| 16 | 19 | |
| 17 | 20 | import java.io.FileNotFoundException; |
| --- | --- | |
| 44 | 47 | public static final int SUCC_HIGHBID=0, SUCC_OUTBID=1, FAIL_ENDED=2; |
| 45 | 48 | public static final int FAIL_CONNECT=3, FAIL_PARSE=4, FAIL_BADMULTI=5; |
| 46 | 49 | |
| 50 | private static AuctionDB sDB; | |
| 51 | ||
| 47 | 52 | /** |
| 48 | 53 | * @brief Set a status message, and mark that the connection is currently invalid. |
| 49 | 54 | */ |
| --- | --- | |
| 528 | 533 | * not set, return a standard 30 seconds. |
| 529 | 534 | */ |
| 530 | 535 | private static long getGlobalSnipeTime() { |
| 531 | String strConfigSnipeAt; | |
| 532 | 536 | long snipeTime; |
| 533 | 537 | |
| 534 | strConfigSnipeAt = JConfig.queryConfiguration("snipemilliseconds"); | |
| 538 | String strConfigSnipeAt = JConfig.queryConfiguration("snipemilliseconds"); | |
| 535 | 539 | if(strConfigSnipeAt != null) { |
| 536 | 540 | snipeTime = Long.parseLong(strConfigSnipeAt); |
| 537 | 541 | } else { |
| --- | --- | |
| 554 | 558 | */ |
| 555 | 559 | private void checkConfigurationSnipeTime() { |
| 556 | 560 | mDefaultSnipeAt = getGlobalSnipeTime(); |
| 561 | if(sDB == null) sDB = setTable("entries"); | |
| 562 | setDB(sDB); | |
| 557 | 563 | } |
| 558 | 564 | |
| 559 | 565 | /** |
| --- | --- | |
| 668 | 674 | } |
| 669 | 675 | } else { |
| 670 | 676 | if(!isDutch()) { |
| 671 | String localUserId; | |
| 672 | ||
| 673 | localUserId = mServer.getUserId().trim(); | |
| 677 | String localUserId = mServer.getUserId().trim(); | |
| 674 | 678 | mHighBidder = localUserId.equalsIgnoreCase(getHighBidder()); |
| 675 | 679 | } |
| 676 | 680 | } |
| --- | --- | |
| 781 | 785 | * we effectively delete the comment. |
| 782 | 786 | */ |
| 783 | 787 | public void setComment(String newComment) { |
| 784 | if(newComment.trim().equals("")) | |
| 788 | if(newComment.trim().length() == 0) | |
| 785 | 789 | mComment = null; |
| 786 | 790 | else |
| 787 | 791 | mComment = newComment; |
| --- | --- | |
| 921 | 925 | */ |
| 922 | 926 | public XMLElement toXML() { |
| 923 | 927 | XMLElement xmlResult = new XMLElement("auction"); |
| 924 | XMLElement xbid, xsnipe, xcomplete, xinvalid, xcomment, xlog, xmulti, xshipping, xcategory; | |
| 925 | 928 | |
| 926 | 929 | xmlResult.setProperty("id", getIdentifier()); |
| 927 | 930 | xmlResult.addChild(mAuction.toXML()); |
| 928 | 931 | |
| 929 | 932 | if(isBidOn()) { |
| 930 | xbid = new XMLElement("bid"); | |
| 933 | XMLElement xbid = new XMLElement("bid"); | |
| 931 | 934 | xbid.setEmpty(); |
| 932 | 935 | xbid.setProperty("quantity", Integer.toString(mBidQuantity)); |
| 933 | 936 | xbid.setProperty("currency", mBid.fullCurrencyName()); |
| --- | --- | |
| 939 | 942 | } |
| 940 | 943 | |
| 941 | 944 | if(isSniped()) { |
| 942 | xsnipe = new XMLElement("snipe"); | |
| 945 | XMLElement xsnipe = new XMLElement("snipe"); | |
| 943 | 946 | xsnipe.setEmpty(); |
| 944 | 947 | xsnipe.setProperty("quantity", Integer.toString(mSnipeQuantity)); |
| 945 | 948 | xsnipe.setProperty("currency", getSnipe().fullCurrencyName()); |
| --- | --- | |
| 951 | 954 | if(isMultiSniped()) { |
| 952 | 955 | MultiSnipe outMS = getMultiSnipe(); |
| 953 | 956 | |
| 954 | xmulti = new XMLElement("multisnipe"); | |
| 957 | XMLElement xmulti = new XMLElement("multisnipe"); | |
| 955 | 958 | xmulti.setEmpty(); |
| 956 | 959 | xmulti.setProperty("subtractshipping", Boolean.toString(outMS.subtractShipping())); |
| 957 | 960 | xmulti.setProperty("color", outMS.getColorString()); |
| --- | --- | |
| 961 | 964 | } |
| 962 | 965 | |
| 963 | 966 | if(isComplete()) { |
| 964 | xcomplete = new XMLElement("complete"); | |
| 967 | XMLElement xcomplete = new XMLElement("complete"); | |
| 965 | 968 | xcomplete.setEmpty(); |
| 966 | 969 | xmlResult.addChild(xcomplete); |
| 967 | 970 | } |
| 968 | 971 | |
| 969 | 972 | if(isInvalid()) { |
| 970 | xinvalid = new XMLElement("invalid"); | |
| 973 | XMLElement xinvalid = new XMLElement("invalid"); | |
| 971 | 974 | xinvalid.setEmpty(); |
| 972 | 975 | xmlResult.addChild(xinvalid); |
| 973 | 976 | } |
| 974 | 977 | |
| 975 | 978 | if(getComment() != null) { |
| 976 | xcomment = new XMLElement("comment"); | |
| 979 | XMLElement xcomment = new XMLElement("comment"); | |
| 977 | 980 | xcomment.setContents(getComment()); |
| 978 | 981 | xmlResult.addChild(xcomment); |
| 979 | 982 | } |
| 980 | 983 | |
| 981 | 984 | if(getCategory() != null) { |
| 982 | xcategory = new XMLElement("category"); | |
| 985 | XMLElement xcategory = new XMLElement("category"); | |
| 983 | 986 | xcategory.setContents(getCategory()); |
| 984 | 987 | xcategory.setProperty("sticky", isSticky() ?"true":"false"); |
| 985 | 988 | xmlResult.addChild(xcategory); |
| 986 | 989 | } |
| 987 | 990 | |
| 988 | 991 | if(getShipping() != null) { |
| 989 | xshipping = new XMLElement("shipping"); | |
| 992 | XMLElement xshipping = new XMLElement("shipping"); | |
| 990 | 993 | xshipping.setEmpty(); |
| 991 | 994 | xshipping.setProperty("currency", getShipping().fullCurrencyName()); |
| 992 | 995 | xshipping.setProperty("price", Double.toString(getShipping().getValue())); |
| 993 | 996 | xmlResult.addChild(xshipping); |
| 994 | 997 | } |
| 995 | 998 | |
| 996 | xlog = mEntryEvents.toXML(); | |
| 999 | XMLElement xlog = mEntryEvents.toXML(); | |
| 997 | 1000 | if(xlog != null) { |
| 998 | 1001 | xmlResult.addChild(xlog); |
| 999 | 1002 | } |
| --- | --- | |
| 1074 | 1077 | boolean shouldSnipe = false; |
| 1075 | 1078 | |
| 1076 | 1079 | if(isSniped()) { |
| 1077 | long endDate, curDate, adjustedDate; | |
| 1080 | long endDate = mAuction.getEndDate().getTime(); | |
| 1081 | long curDate = mServer.getAdjustedTime(); | |
| 1078 | 1082 | |
| 1079 | endDate = mAuction.getEndDate().getTime(); | |
| 1080 | curDate = mServer.getAdjustedTime(); | |
| 1081 | ||
| 1082 | 1083 | // If the auction hasn't ended already... |
| 1083 | 1084 | if(endDate > curDate) { |
| 1084 | 1085 | // mSnipeAt / 1000 seconds before the end of the auction. |
| 1086 | long adjustedDate; | |
| 1085 | 1087 | if(hasDefaultSnipeTime()) { |
| 1086 | 1088 | adjustedDate = curDate + mDefaultSnipeAt; |
| 1087 | 1089 | } else { |
| --- | --- | |
| 1288 | 1290 | */ |
| 1289 | 1291 | public void forceUpdate() { mForceUpdate = true; mDontUpdate = 0; mNeedsUpdate = true; } |
| 1290 | 1292 | |
| 1293 | Category mCategory; | |
| 1294 | ||
| 1291 | 1295 | /** |
| 1292 | 1296 | * @brief Get the category this belongs in, usually used for tab names, and fitting in search results. |
| 1293 | 1297 | * |
| 1294 | 1298 | * @return - A category, or null if none has been assigned. |
| 1295 | 1299 | */ |
| 1296 | public String getCategory() { return getString("category"); } | |
| 1300 | public String getCategory() { return mCategory.getName(); } | |
| 1297 | 1301 | |
| 1298 | 1302 | /** |
| 1299 | 1303 | * @brief Set the category associated with the auction entry. If the |
| --- | --- | |
| 1302 | 1306 | * @param newCategory - The new category to associate this item with. |
| 1303 | 1307 | */ |
| 1304 | 1308 | public void setCategory(String newCategory) { |
| 1305 | setString("category", newCategory); | |
| 1309 | mCategory = Category.findCategory(newCategory); | |
| 1310 | setInteger("category_id", mCategory.getId()); | |
| 1306 | 1311 | if(isComplete()) setSticky(true); |
| 1307 | 1312 | } |
| 1308 | 1313 | |
| --- | --- | |
| 1639 | 1644 | protected void setSnipe(Currency snipe) { |
| 1640 | 1645 | mSnipe = snipe; |
| 1641 | 1646 | } |
| 1647 | ||
| 1648 | public DBRecord getMap() { | |
| 1649 | setInteger("auction_id", mAuction.getId()); | |
| 1650 | return getBacking(); | |
| 1651 | } | |
| 1642 | 1652 | } |
| 203 | 206 | |
|---|---|---|
| 16 | 16 | import com.jbidwatcher.config.JConfig; |
| 17 | 17 | import com.jbidwatcher.util.*; |
| 18 | 18 | import com.jbidwatcher.util.db.DBRecord; |
| 19 | import com.jbidwatcher.util.db.AuctionDB; | |
| 19 | 20 | import com.jbidwatcher.xml.XMLElement; |
| 20 | 21 | |
| 21 | 22 | import java.io.File; |
| --- | --- | |
| 70 | 71 | protected static final sun.misc.BASE64Encoder b64enc = new sun.misc.BASE64Encoder(); |
| 71 | 72 | protected GZip _loadedPage = null; |
| 72 | 73 | |
| 74 | private static AuctionDB sDB = null; | |
| 73 | 75 | /** |
| 74 | 76 | * @brief Empty constructor, for XML parsing. |
| 75 | 77 | * |
| 76 | 78 | */ |
| 77 | AuctionInfo() { | |
| 79 | protected AuctionInfo() { | |
| 78 | 80 | setTranslationTable(mKeys); |
| 81 | if(sDB == null) sDB = setTable("auctions"); | |
| 82 | setDB(sDB); | |
| 79 | 83 | } |
| 80 | 84 | |
| 81 | 85 | /** |
| --- | --- | |
| 90 | 94 | * @param auctionEnd - The end time for the auction. |
| 91 | 95 | * @param auctionBidCount - The number of bids that have been placed so far. |
| 92 | 96 | */ |
| 93 | AuctionInfo(String auctionTitle, String auctionSeller, String auctionHighBidder, | |
| 97 | protected AuctionInfo(String auctionTitle, String auctionSeller, String auctionHighBidder, | |
| 94 | 98 | Currency auctionCurBid, Date auctionStart, Date auctionEnd, int auctionBidCount) { |
| 95 | 99 | setTranslationTable(mKeys); |
| 100 | setTable("auctions"); | |
| 96 | 101 | setTitle(auctionTitle.trim()); |
| 97 | 102 | setHighBidder(auctionHighBidder.trim()); |
| 98 | 103 | _seller = Seller.makeSeller(auctionSeller.trim()); |
| --- | --- | |
| 191 | 196 | |
| 192 | 197 | public XMLElement toXML() { |
| 193 | 198 | XMLElement xmlResult = new XMLElement("info"); |
| 194 | XMLElement xseller, xbidcount, xstart, xend, xdutch, xinsurance; | |
| 195 | 199 | |
| 196 | 200 | addStringChild(xmlResult, "title"); |
| 197 | 201 | |
| 198 | xseller = _seller.toXML(); | |
| 202 | XMLElement xseller = _seller.toXML(); | |
| 199 | 203 | xmlResult.addChild(xseller); |
| 200 | 204 | |
| 201 | xstart = new XMLElement("start"); | |
| 205 | XMLElement xstart = new XMLElement("start"); | |
| 202 | 206 | xstart.setContents(Long.toString(getStart().getTime())); |
| 203 | 207 | xmlResult.addChild(xstart); |
| 204 | 208 | |
| 205 | xend = new XMLElement("end"); | |
| 209 | XMLElement xend = new XMLElement("end"); | |
| 206 | 210 | xend.setContents(Long.toString(getEnd().getTime())); |
| 207 | 211 | xmlResult.addChild(xend); |
| 208 | 212 | |
| 209 | xbidcount = new XMLElement("bidcount"); | |
| 213 | XMLElement xbidcount = new XMLElement("bidcount"); | |
| 210 | 214 | xbidcount.setContents(Integer.toString(getNumBids())); |
| 211 | 215 | xmlResult.addChild(xbidcount); |
| 212 | 216 | |
| 213 | xinsurance = addCurrencyChild(xmlResult, "insurance"); | |
| 217 | XMLElement xinsurance = addCurrencyChild(xmlResult, "insurance"); | |
| 214 | 218 | if(xinsurance != null) xinsurance.setProperty("optional", isInsuranceOptional() ?"true":"false"); |
| 215 | 219 | |
| 216 | 220 | if(getCurBid().getCurrencyType() != Currency.US_DOLLAR) { |
| --- | --- | |
| 222 | 226 | addCurrencyChild(xmlResult, "buynow"); |
| 223 | 227 | addCurrencyChild(xmlResult, "minimum"); |
| 224 | 228 | |
| 225 | xdutch = addBooleanChild(xmlResult, "dutch"); | |
| 229 | XMLElement xdutch = addBooleanChild(xmlResult, "dutch"); | |
| 226 | 230 | if(xdutch != null) xdutch.setProperty("quantity", Integer.toString(getQuantity())); |
| 227 | 231 | |
| 228 | 232 | XMLElement xreserve = addBooleanChild(xmlResult, "reserve"); |
| 205 | 206 | |
|---|---|---|
| 1 | 1 | package com.jbidwatcher.auction; |
| 2 | 2 | |
| 3 | import com.jbidwatcher.util.ErrorManagement; | |
| 4 | 3 | import com.jbidwatcher.util.db.DBRecord; |
| 5 | 4 | import com.jbidwatcher.util.db.AuctionDB; |
| 6 | 5 | import com.jbidwatcher.xml.XMLElement; |
| --- | --- | |
| 9 | 8 | import java.util.Map; |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | * Created by IntelliJ IDEA. | |
| 13 | * User: Morgan | |
| 14 | * Date: Sep 29, 2007 | |
| 15 | * Time: 7:27:30 PM | |
| 16 | * To change this template use File | Settings | File Templates. | |
| 17 | */ | |
| 11 | * User: Morgan | |
| 12 | * Date: Sep 29, 2007 | |
| 13 | * Time: 7:27:30 PM | |
| 14 | * To change this template use File | Settings | File Templates. | |
| 15 | */ | |
| 18 | 16 | public class Seller { |
| 19 | static Map<String,Seller> mSellerMap = new HashMap<String,Seller>(); | |
| 17 | static Map<String, Seller> mSellerMap = new HashMap<String,Seller>(); | |
| 20 | 18 | String mSeller; |
| 21 | 19 | String mPositivePercentage; |
| 22 | 20 | int mFeedback; |
| --- | --- | |
| 27 | 25 | establishSellerDatabase(); |
| 28 | 26 | } |
| 29 | 27 | |
| 28 | private Seller(DBRecord record) { | |
| 29 | mSeller = record.get("seller"); | |
| 30 | try { mFeedback = Integer.parseInt(record.get("feedback")); } catch(Exception e) { mFeedback = 0; } | |
| 31 | mPositivePercentage = record.get("feedback_percentage"); | |
| 32 | } | |
| 33 | ||
| 30 | 34 | private static void establishSellerDatabase() { |
| 31 | 35 | if (sDB == null) { |
| 32 | 36 | try { |
| --- | --- | |
| 41 | 45 | establishSellerDatabase(); |
| 42 | 46 | Seller existing_seller = mSellerMap.get(sellerName); |
| 43 | 47 | if(existing_seller == null) { |
| 44 | // This should look up the seller in the database, so there's only one instance of any given seller. | |
| 45 | Seller rval = new Seller(); | |
| 46 | rval.setSeller(sellerName); | |
| 48 | DBRecord existing = sDB.findByColumn("seller", sellerName); | |
| 49 | Seller rval; | |
| 47 | 50 | |
| 51 | if(existing != null) { | |
| 52 | rval = new Seller(existing); | |
| 53 | } else { | |
| 54 | // This should look up the seller in the database, so there's only one instance of any given seller. | |
| 55 | rval = new Seller(); | |
| 56 | rval.setSeller(sellerName); | |
| 57 | } | |
| 58 | ||
| 48 | 59 | mSellerMap.put(sellerName, rval); |
| 49 | 60 | return rval; |
| 50 | 61 | } |
| --- | --- | |
| 54 | 65 | |
| 55 | 66 | public Integer getId() { |
| 56 | 67 | if(mId == null) { |
| 57 | String id = sDB.storeMap(getMap()); | |
| 58 | if(id != null && id.length() != 0) { | |
| 59 | mId = Integer.parseInt(id); | |
| 60 | } else { | |
| 61 | mId = null; | |
| 62 | } | |
| 68 | create(); | |
| 63 | 69 | } |
| 64 | 70 | return mId; |
| 65 | 71 | } |
| 66 | 72 | |
| 73 | private void create() { | |
| 74 | String id = sDB.storeMap(getMap()); | |
| 75 | if(id != null && id.length() != 0) { | |
| 76 | mId = Integer.parseInt(id); | |
| 77 | } else { | |
| 78 | mId = null; | |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 67 | 82 | public DBRecord getMap() { |
| 68 | 83 | DBRecord values = new DBRecord(); |
| 69 | 84 | |
| 205 | 206 | |
|---|---|---|
| 124 | 124 | |
| 125 | 125 | ae.setServer(newServer); |
| 126 | 126 | ae.fromXML(perEntry); |
| 127 | mDB.storeMap(ae.getMap()); | |
| 127 | ae.create(); | |
| 128 | 128 | if (sEntryManager != null) { |
| 129 | 129 | sEntryManager.addEntry(ae); |
| 130 | 130 | } |
| 205 | 206 | |
|---|---|---|
| 3 | 3 | import com.jbidwatcher.xml.XMLElement; |
| 4 | 4 | import com.jbidwatcher.xml.XMLSerializeSimple; |
| 5 | 5 | import com.jbidwatcher.util.db.DBRecord; |
| 6 | import com.jbidwatcher.util.db.AuctionDB; | |
| 6 | 7 | |
| 7 | 8 | import java.util.Date; |
| 8 | 9 | import java.util.Map; |
| 9 | 10 | import java.util.TimeZone; |
| 10 | 11 | import java.text.SimpleDateFormat; |
| 12 | import java.sql.SQLException; | |
| 11 | 13 | |
| 12 | 14 | /** |
| 13 | * Created by IntelliJ IDEA. | |
| 14 | 15 | * User: Morgan |
| 15 | 16 | * Date: Sep 30, 2007 |
| 16 | 17 | * Time: 1:54:43 PM |
| --- | --- | |
| 22 | 23 | private SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 23 | 24 | private Map<String, String> mTranslationTable; |
| 24 | 25 | private String mDefaultCurrency; |
| 26 | protected Integer mId; | |
| 27 | protected AuctionDB mDB = null; | |
| 25 | 28 | |
| 26 | public HashBacked() { | |
| 29 | protected void setDB(AuctionDB db) { mDB = db; } | |
| 30 | protected AuctionDB setTable(String tableName) { | |
| 31 | if(mDB == null) try { | |
| 32 | mDB = new AuctionDB(tableName); | |
| 33 | } catch (Exception e) { | |
| 34 | e.printStackTrace(); | |
| 35 | } | |
| 36 | ||
| 37 | return mDB; | |
| 38 | } | |
| 39 | ||
| 40 | protected HashBacked() { | |
| 27 | 41 | mDateFormat.setTimeZone(TimeZone.getDefault()); |
| 28 | 42 | mBacking = new DBRecord(); |
| 29 | 43 | mDefaultCurrency = Currency.getCurrency("$1.00").fullCurrencyName(); |
| --- | --- | |
| 198 | 212 | return xadd; |
| 199 | 213 | } |
| 200 | 214 | |
| 215 | public Integer getId() { | |
| 216 | if(mId == null) { | |
| 217 | create(); | |
| 218 | } | |
| 219 | return mId; | |
| 220 | } | |
| 221 | ||
| 222 | public void create() { | |
| 223 | String id = mDB.storeMap(getBacking()); | |
| 224 | if(id != null && id.length() != 0) { | |
| 225 | mId = Integer.parseInt(id); | |
| 226 | } else { | |
| 227 | mId = null; | |
| 228 | } | |
| 229 | } | |
| 230 | ||
| 201 | 231 | protected DBRecord getBacking() { return mBacking; } |
| 232 | protected void setBacking(DBRecord newRecord) { mBacking = newRecord; } | |
| 202 | 233 | } |
| 197 | 206 | |
|---|---|---|
| 14 | 14 | * Wrap the auction information up in a database. |
| 15 | 15 | */ |
| 16 | 16 | public class AuctionDB { |
| 17 | private class TypeColumn { | |
| 17 | private static class TypeColumn { | |
| 18 | 18 | private String mType; |
| 19 | 19 | private Integer mIndex; |
| 20 | 20 | |
| --- | --- | |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | public String updateMap(String tableName, String columnKey, String value, DBRecord newRow) { |
| 126 | DBRecord oldRow = getOldRow(tableName, columnKey, value); | |
| 126 | DBRecord oldRow = getOldRow(tableName, columnKey, value, true); | |
| 127 | 127 | if(oldRow == null) return storeMap(newRow); |
| 128 | 128 | |
| 129 | 129 | String sql = createPreparedUpdate(tableName, oldRow, newRow); |
| --- | --- | |
| 136 | 136 | for(String key: newRow.keySet()) { |
| 137 | 137 | if (newRow.get(key) != null) { |
| 138 | 138 | if(!setColumn(ps, column++, key, newRow.get(key))) { |
| 139 | System.err.println("Error from columns: (" + column + "," + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")"); | |
| 139 | ErrorManagement.logMessage("Error from columns: (" + column + "," + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")"); | |
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | 142 | } |
| --- | --- | |
| 149 | 149 | return null; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | private DBRecord getOldRow(String tableName, String columnKey, String value) { | |
| 152 | public DBRecord findByColumn(String columnKey, String value) { | |
| 153 | return findByColumn(columnKey, value, false); | |
| 154 | } | |
| 155 | ||
| 156 | public DBRecord findByColumn(String columnKey, String value, boolean forUpdate) { | |
| 157 | return getOldRow(mTableName, columnKey, value, forUpdate); | |
| 158 | } | |
| 159 | ||
| 160 | private DBRecord getOldRow(String tableName, String columnKey, String value, boolean forUpdate) { | |
| 153 | 161 | DBRecord oldRow = null; |
| 154 | 162 | try { |
| 155 | ResultSet rs = mS.executeQuery("SELECT * FROM " + tableName + " FOR UPDATE WHERE " + columnKey + "=" + value); | |
| 163 | String statement = "SELECT * FROM " + tableName; | |
| 164 | if(forUpdate) statement += " FOR UPDATE"; | |
| 165 | statement += " WHERE " + columnKey + " = ?"; | |
| 166 | PreparedStatement ps = mDB.prepare(statement); | |
| 167 | setColumn(ps, 1, columnKey, value); | |
| 168 | ResultSet rs = ps.executeQuery(); | |
| 156 | 169 | oldRow = getFirstResult(rs); |
| 157 | 170 | } catch (SQLException e) { |
| 158 | ErrorManagement.handleException("Can't get row for update.", e); | |
| 171 | ErrorManagement.handleException("Can't get row" + (forUpdate? " for update":"") + " (columnKey = '" + value +"'.", e); | |
| 159 | 172 | } |
| 160 | 173 | return oldRow; |
| 161 | 174 | } |
| --- | --- | |
| 171 | 184 | for(String key: newRow.keySet()) { |
| 172 | 185 | if (newRow.get(key) != null) { |
| 173 | 186 | if(!setColumn(ps, column++, key, newRow.get(key))) { |
| 174 | System.err.println("Error from columns: (" + column + ", " + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")"); | |
| 187 | ErrorManagement.logDebug("Error from columns: (" + column + ", " + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")"); | |
| 175 | 188 | } |
| 176 | 189 | } |
| 177 | 190 | } |
| --- | --- | |
| 274 | 287 | } |
| 275 | 288 | } |
| 276 | 289 | } else { |
| 277 | System.err.println("WTF?!?!"); | |
| 290 | ErrorManagement.logDebug("WTF?!?!"); | |
| 278 | 291 | } |
| 279 | 292 | } catch (SQLException e) { |
| 280 | 293 | e.printStackTrace(); |
