PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dao/suspence_dao.rb

https://bitbucket.org/jjhop/mycash
Ruby | 64 lines | 41 code | 6 blank | 17 comment | 6 complexity | a581f02790593f918dc31b2725e97cd3 MD5 | raw file
  1. # This program is free software: you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation, version 3 of the License.
  4. #
  5. # This program is distributed in the hope that it will be useful,
  6. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. # GNU General Public License for more details.
  9. #
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. #
  13. # Copyright: (C) 2004-2010 Rafal Kotusiewicz aka 'jjhop'. All rights reserved.
  14. #
  15. # http://bitbucket.org/jjhop/mycash/overview/
  16. # http://bitbucket.org/jjhop/mycash/wiki/Home
  17. #
  18. require 'rubygems'
  19. require File.join(File.dirname(__FILE__), '../model/suspence')
  20. module MyCashDAO
  21. class SuspenceDAO
  22. def initialize(dbh)
  23. if dbh.nil?
  24. raise 'Polaczenie z baza musi byc prawidlowe'
  25. end
  26. @dbh = dbh
  27. end
  28. def find_by_id(id)
  29. begin
  30. if id == -1
  31. return
  32. end
  33. r = @dbh.get_first_row("SELECT * FROM suspences WHERE id=:id", 'id' => id)
  34. return MyCashModel::Suspence.create_from_hash(r)
  35. rescue Exception => ex
  36. raise DAOException, ex
  37. end
  38. end
  39. def save_or_update(suspence)
  40. unless MyCashModel::Suspence == suspence.class
  41. raise DAOException,
  42. 'Argumentem metody MyCashDAO::SuspenceDAO#save_or_update musi' +
  43. ' by? prawid?owy obiekt klasy MyCashModel::Suspence'
  44. end
  45. if suspence.new?
  46. @dbh.execute(
  47. "INSERT INTO suspences(id, created_at, amount, what) VALUES(?,?,?,?)",
  48. nil, suspence.created_at || Time.new, suspence.amount, suspence.what)
  49. else
  50. @dbh.execute( "UPDATE suspences SET amount=?, what=? WHERE id=?",
  51. suspence.amount, suspence.what, suspence.suspence_id)
  52. end
  53. end
  54. def delete_by_id(id)
  55. @dbh.execute("DELETE FROM suspences WHERE id=:id", 'id' => id)
  56. end
  57. end
  58. end