PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dao/account_dao.rb

https://bitbucket.org/jjhop/mycash
Ruby | 70 lines | 45 code | 8 blank | 17 comment | 3 complexity | f9203314afde2190a306e38154f70298 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/account')
  20. module MyCashDAO
  21. class AccountDAO
  22. def initialize(dbh)
  23. @dbh = dbh
  24. end
  25. def find_by_id(id)
  26. begin
  27. r = @dbh.get_first_row("SELECT * FROM accounts WHERE id=:id", 'id' => id)
  28. MyCashModel::Account.create_from_hash(r)
  29. rescue Exception => ex
  30. raise DAOException, ex
  31. end
  32. end
  33. def get_all
  34. result = []
  35. begin
  36. @dbh.execute("SELECT * FROM accounts ORDER by created_at ASC") do |r|
  37. result<< MyCashModel::Account.create_from_hash(r)
  38. end
  39. rescue Exception => ex
  40. raise DAOException, ex
  41. end
  42. result
  43. end
  44. def save_or_update(account)
  45. unless MyCashModel::Account == account.class
  46. raise DAOException,
  47. 'Argumentem metody MyCashDAO::AccountDAO#save_or_update musi' +
  48. ' być prawidłowy obiekt klasy MyCashModel::Account'
  49. end
  50. if account.new?
  51. @dbh.execute(
  52. "INSERT INTO accounts(id, created_at, name) VALUES(?,?,?)",
  53. nil, account.created_at || Time.new, account.name)
  54. else
  55. @dbh.execute( "UPDATE accounts SET name=? WHERE id=?", account.name, account.account_id)
  56. end
  57. end
  58. def delete_by_id(id)
  59. @dbh.execute("DELETE FROM accounts WHERE id=:id", 'id' => id)
  60. end
  61. end
  62. end