DBIx-Struct

 view release on metacpan or  search on metacpan

lib/DBIx/Struct.pm  view on Meta::CPAN

							\$self->\$f(\$_[\$i + 1]);
						} else {
							CORE::push \@unknown_columns, \$_[\$i];
						}
					}
				}
			}
			DBIx::Struct::error_message {
					result  => 'SQLERR',
					message => 'unknown columns '.CORE::join(", ", \@unknown_columns).' for $table->data'
			} if \@unknown_columns;
			\$self;
		}
SET
    $set;
}

sub make_object_data {
    my $table = $_[0];
    my $data  = <<DATA;
		sub data {
			my \$self = \$_[0];
			my \@ret_keys;
			my \$ret;
			if(CORE::defined(\$_[1])) {
				if(CORE::ref(\$_[1]) eq 'ARRAY') {
					if(!\@{\$_[1]}) {
						\$ret = \$self->[@{[_row_data]}];
					} else {
						\$ret = [CORE::map {\$self->[@{[_row_data]}]->[\$fields{\$_}] } \@{\$_[1]}];
					}
				} else {
					for my \$k (\@_[1..\$#_]) {
						CORE::push \@ret_keys, \$k if CORE::exists \$fields{\$k};
					}
				}
			} else {
				\@ret_keys = keys \%fields;
			}
			my \@unknown_columns = CORE::grep {not CORE::exists \$fields{\$_}} \@ret_keys;
			DBIx::Struct::error_message {
					result  => 'SQLERR',
					message => 'unknown columns '.CORE::join(", ", \@unknown_columns).' for $table->data'
			} if \@unknown_columns;
			\$ret = { 
				CORE::map {\$_ => \$self->\$_} \@ret_keys
			} if not CORE::defined \$ret;
			\$ret;
		}
DATA
    $data;
}

sub make_object_update {
    my ($table, $pk_where, $pk_row_data) = @_;
    my $update;
    if (not ref $table) {

        # means this is just one simple table
        $update = <<UPD;
		sub update {
			my \$self = \$_[0];
			if(\@_ > 1 && CORE::ref(\$_[1]) eq 'HASH') {
				my (\$set, \$where, \@bind, \@bind_where);
				{
					no strict 'vars';
					local *set_hash = \$_[1];
					my \@unknown_columns = CORE::grep {not CORE::exists \$fields{\$_}} CORE::keys %set_hash;
					DBIx::Struct::error_message {
							result  => 'SQLERR',
							message => 'unknown columns '.CORE::join(", ", \@unknown_columns).' updating table $table'
					} if \@unknown_columns;
					\$set = 
						CORE::join ", ", 
						CORE::map { 
							if(CORE::ref(\$set_hash{\$_}) eq 'ARRAY' and CORE::ref(\$set_hash{\$_}[0]) eq 'SCALAR') {
								CORE::push \@bind, \@{\$set_hash{\$_}}[1..\$#{\$set_hash{\$_}}];
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$set_hash{\$_}[0]};
							} elsif(CORE::ref(\$set_hash{\$_}) eq 'REF' and CORE::ref(\${\$set_hash{\$_}}) eq 'ARRAY') {
								if(CORE::defined \${\$set_hash{\$_}}->[0]) {
									CORE::push \@bind, \@{\${\$set_hash{\$_}}}[1..\$#{\${\$set_hash{\$_}}}];
									DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$set_hash{\$_}}->[0];
								} else {
									DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = null"
								}
							} elsif(CORE::ref(\$set_hash{\$_}) eq 'SCALAR') {
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$set_hash{\$_}};
							} elsif(CORE::exists(\$json_fields{\$_})
								&& (CORE::ref(\$set_hash{\$_}) eq 'ARRAY' || CORE::ref(\$set_hash{\$_}) eq 'HASH')) {
								CORE::push \@bind, JSON::to_json(\$set_hash{\$_});
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?" 
							} else {
								CORE::push \@bind, \$set_hash{\$_};
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?" 
							}						
						} CORE::keys \%set_hash;
				}
				if(\@_ > 2) {
					my \$cond = \$_[2];
					if(not CORE::ref(\$cond)) {
						\$cond = {(selectKeys)[0] => \$_[2]};
					}
					(\$where, \@bind_where) = SQL::Abstract->new->where(\$cond);
				}
				return DBIx::Struct::connect->run(sub {
					\$_->do(qq{update $table set \$set \$where}, undef, \@bind, \@bind_where)
					or DBIx::Struct::error_message {
						result  => 'SQLERR',
						message => 'error '.\$_->errstr.' updating table $table'
					}
				});
			} elsif (CORE::ref(\$self) && \@\$self > 1 && \%{\$self->[@{[_row_updates]}]}) {
				my (\$set, \@bind);
				{
					no strict 'vars';
					\$set = 
						CORE::join ", ", 
						CORE::map { 
							local *column_value = \\\$self->[@{[_row_data]}][\$fields{\$_}];
							if(CORE::ref(\$column_value) eq 'ARRAY' and CORE::ref(\$column_value->[0]) eq 'SCALAR') {
								CORE::push \@bind, \@{\$column_value}[1..\$#\$column_value];
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$column_value->[0]};
							} elsif(CORE::ref(\$column_value) eq 'REF' and CORE::ref(\${\$column_value}) eq 'ARRAY') {
								if(CORE::defined \${\$column_value}->[0]) {
									CORE::push \@bind, \@{\${\$column_value}}[1..\$#{\${\$column_value}}];
									DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$column_value}->[0];
								} else {
									DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = null"
								}
							} elsif(CORE::ref(\$column_value) && CORE::ref(\$column_value) =~ /^DBIx::Struct::JSON/) {
								\$column_value->revert;
								CORE::push \@bind, \$column_value;
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?" 
							} elsif(CORE::ref(\$column_value) eq 'SCALAR') {
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \$\$column_value;
							} else {
								CORE::push \@bind, \$column_value;
								DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?" 
							}						
						} CORE::keys \%{\$self->[@{[_row_updates]}]};
				}
				my \$update_query = qq{update $table set \$set where $pk_where};
				DBIx::Struct::connect->run(
					sub {
						\$_->do(\$update_query, undef, \@bind, $pk_row_data)
						or DBIx::Struct::error_message {
							result  => 'SQLERR',
							message => 'error '.\$_->errstr.' updating table $table',
							query   => \$update_query,
							bind    => \\\@bind,
						}
					}
				);
				\$#\$self = @{[_row_data]};
			}
			\$self;
		}
UPD
    } else {
        $update = <<UPD;
		sub update {}
UPD
    }
    $update;
}

sub make_object_delete {
    my ($table, $pk_where, $pk_row_data) = @_;
    my $delete;
    if (not ref $table) {
        $delete = <<DEL;
		sub delete {
			my \$self = \$_[0];
			if(Scalar::Util::blessed \$self) {
				DBIx::Struct::connect->run(
					sub {
						\$_->do(qq{delete from $table where $pk_where}, undef, $pk_row_data)
						or DBIx::Struct::error_message {
							result  => 'SQLERR',
							message => 'error '.\$_->errstr.' updating table $table'
						}
					});
				return \$self;
			}
			my \$where = '';
			my \@bind;
			my \$cond = \$_[1] if \@_ > 1;
			if(not CORE::ref(\$cond)) {
				\$cond = {};
				my \@keys = selectKeys();
				for(my \$i = 1; \$i < \@_; ++\$i) {
					DBIx::Struct::error_message {
						result  => 'SQLERR',
						message => "Too many keys to delete for $table"
					} if not CORE::defined \$keys[\$i-1];
					\$cond->{\$keys[\$i-1]} = \$_[\$i];
				}
			}
			my \@rpar = ();
			if(\$cond) {
				(\$where, \@bind) = SQL::Abstract->new->where(\$cond);
				\@rpar = (undef, \@bind);
			}
			return DBIx::Struct::connect->run(sub {
				\$_->do(qq{delete from $table \$where}, \@rpar)
				or DBIx::Struct::error_message {
					result  => 'SQLERR',
					message => 'error '.\$_->errstr.' updating table $table'
				}
			});
		}
DEL
    } else {
        $delete = <<DEL
		sub delete {}
DEL
    }
    $delete;
}

sub make_object_autoload_find {



( run in 0.891 second using v1.01-cache-2.11-cpan-39bf76dae61 )