eb080f3 12 years ago
1 contributor
340 lines | 7.822kb
  1. package DBIx::Custom::Model;
  2. use Object::Simple -base;
  3.  
  4. use Carp 'croak';
  5. use DBIx::Custom::Util '_subname';
  6.  
  7. # Carp trust relationship
  8. push @DBIx::Custom::CARP_NOT, __PACKAGE__;
  9.  
  10. has [qw/dbi table ctime mtime bind_type join primary_key/],
  11. columns => sub { [] };
  12.  
  13. our $AUTOLOAD;
  14.  
  15. sub AUTOLOAD {
  16. my $self = shift;
  17.  
  18. # Method name
  19. my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
  20.  
  21. # Method
  22. $self->{_methods} ||= {};
  23. if (my $method = $self->{_methods}->{$mname}) {
  24. return $self->$method(@_)
  25. }
  26. elsif (my $dbi_method = $self->dbi->can($mname)) {
  27. $self->dbi->$dbi_method(@_);
  28. }
  29. elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
  30. $self->dbi->dbh->$dbh_method(@_);
  31. }
  32. else {
  33. croak qq{Can't locate object method "$mname" via "$package" }
  34. . _subname;
  35. }
  36. }
  37.  
  38. my @methods = qw/insert insert_at update update_at update_all
  39. delete delete_at delete_all select select_at count/;
  40. for my $method (@methods) {
  41. my $code =
  42. qq/sub {/ .
  43. qq/my \$self = shift;/ .
  44. qq/\$self->dbi->$method(/ .
  45. qq/\@_ % 2 ? shift : (),/;
  46.  
  47. my @attrs = qw/table type primary_key bind_type/;
  48. my @insert_attrs = qw/created_at updated_at ctime mtime/;
  49. my @update_attrs = qw/updated_at mtime/;
  50. my @select_attrs = qw/join/;
  51. if ($method eq 'insert') { push @attrs, @insert_attrs }
  52. elsif ($method eq 'update') { push @attrs, @update_attrs }
  53. elsif (index($method, 'select') != -1 || $method eq 'count') {
  54. push @attrs, @select_attrs
  55. }
  56. for my $attr (@attrs) {
  57. $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
  58. }
  59. $code .= qq/\@_);/ .
  60. qq/}/;
  61. no strict 'refs';
  62. *{__PACKAGE__ . "::$method"} = eval $code;
  63. croak $code if $@;
  64. }
  65.  
  66. sub update_or_insert {
  67. my ($self, $param, %opt) = @_;
  68. croak "update_or_insert method need primary_key and id option "
  69. unless (defined $opt{id} || defined $self->{id})
  70. && (defined $opt{primary_key} || defined $self->{primary_key});
  71. my $statement_opt = $opt{option} || {};
  72. my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
  73. if (@$rows == 0) {
  74. return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
  75. }
  76. elsif (@$rows == 1) {
  77. return $self->update($param, %opt, %{$statement_opt->{update} || {}});
  78. }
  79. else { croak "selected row must be one " . _subname }
  80. }
  81.  
  82. sub execute {
  83. my $self = shift;
  84. if ($ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE}) {
  85. $self->dbi->execute(@_);
  86. }
  87. else {
  88. warn "DBIx::Custom::Model execute method is DEPRECATED! " .
  89. "use DBIx::Custom execute method. " .
  90. "If you want to call DBIx::Custom execute method directory from model, " .
  91. "set \$ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE} to 1 " .
  92. "until DBIx::Custom::Model execute method is removed in the future." ;
  93. return $self->dbi->execute(
  94. shift,
  95. shift,
  96. table => $self->table,
  97. bind_type => $self->bind_type,
  98. primary_key => $self->primary_key,
  99. type => $self->type,
  100. @_
  101. );
  102. }
  103. }
  104.  
  105. sub DESTROY { }
  106.  
  107. sub helper {
  108. my $self = shift;
  109. # Merge
  110. my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
  111. $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
  112. return $self;
  113. }
  114.  
  115. sub mycolumn {
  116. my $self = shift;
  117. my $table = shift unless ref $_[0];
  118. my $columns = shift;
  119. $table ||= $self->table || '';
  120. $columns ||= $self->columns;
  121. return $self->dbi->mycolumn($table, $columns);
  122. }
  123.  
  124. sub new {
  125. my $self = shift->SUPER::new(@_);
  126. # Check attribute names
  127. my @attrs = keys %$self;
  128. for my $attr (@attrs) {
  129. croak qq{"$attr" is invalid attribute name } . _subname
  130. unless $self->can($attr);
  131. }
  132. # Cache
  133. for my $attr (qw/dbi table created_at updated_at ctime mtime bind_type join primary_key/) {
  134. $self->$attr;
  135. $self->{$attr} = undef unless exists $self->{$attr};
  136. }
  137. $self->columns;
  138. return $self;
  139. }
  140.  
  141. # DEPRECATED!
  142. has 'filter';
  143. has 'name';
  144. has 'type';
  145. has 'created_at';
  146. has 'updated_at';
  147.  
  148. # DEPRECATED!
  149. sub method {
  150. warn "method method is DEPRECATED! use helper instead";
  151. return shift->helper(@_);
  152. }
  153.  
  154. 1;
  155.  
  156. =head1 NAME
  157.  
  158. DBIx::Custom::Model - Model
  159.  
  160. =head1 SYNOPSIS
  161.  
  162. use DBIx::Custom::Model;
  163.  
  164. my $model = DBIx::Custom::Model->new(table => 'books');
  165.  
  166. =head1 ATTRIBUTES
  167.  
  168. =head2 C<dbi>
  169.  
  170. my $dbi = $model->dbi;
  171. $model = $model->dbi($dbi);
  172.  
  173. L<DBIx::Custom> object.
  174.  
  175. =head2 C<ctime>
  176.  
  177. my $ctime = $model->ctime;
  178. $model = $model->ctime('created_time');
  179.  
  180. Create timestamp column, this is passed to C<insert> or C<update> method.
  181.  
  182. =head2 C<join>
  183.  
  184. my $join = $model->join;
  185. $model = $model->join(
  186. ['left outer join company on book.company_id = company.id']
  187. );
  188. Join clause, this value is passed to C<select> method.
  189.  
  190. =head2 C<primary_key>
  191.  
  192. my $primary_key = $model->primary_key;
  193. $model = $model->primary_key(['id', 'number']);
  194.  
  195. Primary key,this is passed to C<insert>, C<update>,
  196. C<delete>, and C<select> method.
  197.  
  198. =head2 C<table>
  199.  
  200. my $model = $model->table;
  201. $model = $model->table('book');
  202.  
  203. Table name, this is passed to C<select> method.
  204.  
  205. =head2 C<bind_type>
  206.  
  207. my $type = $model->bind_type;
  208. $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
  209. Database data type, this is used as type optioon of C<insert>,
  210. C<update>, C<update_all>, C<delete>, C<delete_all>,
  211. and C<select> method
  212.  
  213. =head2 C<mtime>
  214.  
  215. my $mtime = $model->mtime;
  216. $model = $model->mtime('modified_time');
  217.  
  218. Updated timestamp column, this is passed to C<update> method.
  219.  
  220. =head1 METHODS
  221.  
  222. L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>,
  223. and you can use all methods of L<DBIx::Custom> and L<DBI>
  224. and implements the following new ones.
  225.  
  226. =head2 C<count>
  227.  
  228. my $count = $model->count;
  229.  
  230. Get rows count.
  231.  
  232. Options is same as C<select> method's ones.
  233.  
  234. =head2 C<delete>
  235.  
  236. $model->delete(...);
  237. Same as C<delete> of L<DBIx::Custom> except that
  238. you don't have to specify options if you set attribute in model.
  239.  
  240. =head2 C<delete_all>
  241.  
  242. $model->delete_all(...);
  243. Same as C<delete_all> of L<DBIx::Custom> except that
  244. you don't have to specify options if you set attribute in model.
  245.  
  246. =head2 C<insert>
  247.  
  248. $model->insert(...);
  249. Same as C<insert> of L<DBIx::Custom> except that
  250. you don't have to specify options if you set attribute in model.
  251.  
  252. =head2 C<helper>
  253.  
  254. $model->helper(
  255. update_or_insert => sub {
  256. my $self = shift;
  257. # ...
  258. },
  259. find_or_create => sub {
  260. my $self = shift;
  261. # ...
  262. }
  263. );
  264.  
  265. Register helper. These helper is called directly from L<DBIx::Custom::Model> object.
  266.  
  267. $model->update_or_insert;
  268. $model->find_or_create;
  269.  
  270. =head2 C<mycolumn>
  271.  
  272. my $column = $self->mycolumn;
  273. my $column = $self->mycolumn(book => ['author', 'title']);
  274. my $column = $self->mycolumn(['author', 'title']);
  275.  
  276. Create column clause for myself. The follwoing column clause is created.
  277.  
  278. book.author as author,
  279. book.title as title
  280.  
  281. If table name is ommited, C<table> attribute of the model is used.
  282. If column names is omitted, C<columns> attribute of the model is used.
  283.  
  284. =head2 C<new>
  285.  
  286. my $model = DBIx::Custom::Model->new;
  287.  
  288. Create a L<DBIx::Custom::Model> object.
  289.  
  290. =head2 C<select>
  291.  
  292. $model->select(...);
  293. Same as C<select> of L<DBIx::Custom> except that
  294. you don't have to specify options if you set attribute in model.
  295.  
  296. =head2 C<update>
  297.  
  298. $model->update(...);
  299. Same as C<update> of L<DBIx::Custom> except that
  300. you don't have to specify options if you set attribute in model.
  301.  
  302. =head2 C<update_all>
  303.  
  304. $model->update_all(param => \%param);
  305. Same as C<update_all> of L<DBIx::Custom> except that
  306. you don't have to specify options if you set attribute in model.
  307.  
  308. =head2 C<update_or_insert>
  309.  
  310. $model->update_or_insert(...);
  311. Same as C<update> of L<DBIx::Custom> except that
  312. you don't have to specify options if you set attribute in model.
  313.  
  314. =cut