$VERSION
=
'0.5'
;
sub
_die {
require
Carp; Carp::croak(
@_
); }
__PACKAGE__->set_sql(
MakeNewObj
=> <<
''
);
SET NOCOUNT ON
INSERT INTO __TABLE__ (
%s
)
VALUES (
%s
)
SELECT @
@IDENTITY
__PACKAGE__->__grouper( Class::DBI::Sybase::ColumnGrouper->new() );
sub
set_up_table
{
my
(
$class
,
$table
) =
@_
;
my
$dbh
=
$class
->db_Main();
$class
->table(
$table
);
my
$sth
=
$dbh
->prepare(
"sp_columns $table"
);
$sth
->execute();
my
$col
=
$sth
->fetchall_arrayref;
$sth
->finish();
_die(
'The "'
.
$class
->table() .
'" table has no primary key'
)
unless
$col
->[0][3];
$class
->columns(
All
=>
map
{
$_
->[3] }
@$col
);
$class
->columns(
Primary
=>
$col
->[0][3] );
$class
->columns(
TEXT
=>
map
{
$_
->[5] eq
'text'
?
$_
->[3] : () }
@$col
);
$sth
=
$dbh
->prepare(
"sp_help $table"
);
$sth
->execute();
$sth
->fetchall_arrayref()
for
1 .. 2;
$col
=
$sth
->fetchall_arrayref();
my
(
$identity
) =
grep
(
$_
->[9] == 1,
@$col
);
$class
->columns(
IDENTITY
=>
$identity
->[0] )
if
$identity
;
}
sub
sth_to_objects
{
my
(
$class
,
$sth
,
$args
) =
@_
;
$class
->_croak(
"sth_to_objects needs a statement handle"
)
unless
$sth
;
unless
( UNIVERSAL::isa(
$sth
=>
"DBI::st"
) )
{
my
$meth
=
"sql_$sth"
;
$sth
=
$class
->
$meth
();
}
$sth
->finish()
if
$sth
->{Active};
my
(
%data
,
@rows
);
eval
{
$sth
->execute(
@$args
)
unless
$sth
->{Active};
$sth
->bind_columns( \(
@data
{ @{
$sth
->{NAME} } } ) );
push
@rows
, {
%data
}
while
$sth
->fetch;
};
return
$class
->_croak(
"$class can't $sth->{Statement}: $@"
,
err
=> $@ )
if
$@;
return
$class
->_ids_to_objects( \
@rows
);
}
sub
_column_placeholder
{
my
$self
=
shift
;
my
$column
=
shift
;
my
$data
=
shift
;
my
@text_columns
=
$self
->columns(
'TEXT'
);
if
(
$data
&&
grep
{
$_
eq
$column
}
@text_columns
)
{
return
$self
->db_Main->quote(
$data
);
}
else
{
return
$self
->SUPER::_column_placeholder(
$column
);
}
}
sub
_insert_row
{
my
$self
=
shift
;
my
$data
=
shift
;
my
@primary_columns
=
$self
->primary_columns();
my
@identity_columns
=
$self
->columns(
'IDENTITY'
);
my
@text_columns
=
$self
->columns(
'TEXT'
);
eval
{
my
@columns
;
my
@values
;
for
my
$column
(
keys
%$data
)
{
next
if
defined
$identity_columns
[0] &&
$column
eq
$identity_columns
[0];
push
@columns
,
$column
;
push
@values
,
$data
->{
$column
}
unless
grep
{
$_
eq
$column
}
@text_columns
;
}
my
$sth
=
$self
->sql_MakeNewObj(
join
(
', '
,
@columns
),
join
(
', '
,
map
$self
->_column_placeholder(
$_
,
$data
->{
$_
} ),
@columns
)
,
);
$self
->_bind_param(
$sth
, \
@columns
);
$sth
->execute(
@values
);
my
$id
=
$sth
->fetchrow_arrayref()->[0];
$data
->{
$identity_columns
[0] } =
$id
if
@identity_columns
== 1
&& !
defined
$data
->{
$identity_columns
[0] };
$sth
->finish
if
$sth
->{Active};
};
if
($@)
{
my
$class
=
ref
$self
;
return
$self
->_croak(
"Can't insert new $class: $@"
,
err
=> $@,
method
=>
'create'
);
}
return
1;
}
sub
_update_vals
{
my
$self
=
shift
;
my
@text_columns
=
$self
->columns(
'TEXT'
);
my
@identity_columns
=
$self
->columns(
'IDENTITY'
);
my
@changed
=
$self
->is_changed();
my
@columns
;
foreach
my
$changed
(
@changed
)
{
next
if
grep
{
$_
eq
$changed
}
@identity_columns
;
next
if
grep
{
$_
eq
$changed
}
@text_columns
;
push
@columns
,
$changed
;
}
return
$self
->_attrs(
@columns
);
}
sub
_update_line
{
my
$self
=
shift
;
my
@changed
=
$self
->is_changed;
my
@identity_columns
=
$self
->columns(
'IDENTITY'
);
my
@columns
;
foreach
my
$changed
(
@changed
)
{
push
@columns
,
$changed
unless
grep
{
$_
eq
$changed
}
@identity_columns
;
}
return
join
(
', '
,
map
"$_ = "
.
$self
->_column_placeholder(
$_
,
$self
->
$_
() ),
@columns
);
}
sub
_make_method
{
my
(
$class
,
$name
,
$method
) =
@_
;
return
if
defined
&{
"$class\::$name"
};
$class
->_carp(
"Column '$name' in $class clashes with built-in method"
)
if
Class::DBI->can(
$name
)
and not(
$name
eq
"id"
and
join
(
" "
,
$class
->primary_columns ) eq
"id"
);
no
strict
'refs'
;
*{
"$class\::$name"
} =
$method
;
$class
->_make_method(
$name
=>
$method
);
}
sub
_column_class {
'Class::DBI::Sybase::Column'
}
sub
_set_columns
{
my
(
$class
,
$group
,
@columns
) =
@_
;
my
@cols
=
map
ref
$_
?
$_
:
$class
->_column_class->new(
$_
),
@columns
;
$class
->__grouper( Class::DBI::Sybase::ColumnGrouper->clone(
$class
->__grouper )->add_group(
$group
=>
@cols
) );
$class
->_mk_column_accessors(
@cols
);
return
@columns
;
}
1;
sub
name_lc {
shift
->name }
1;
sub
add_column
{
my
(
$self
,
$col
) =
@_
;
croak
"Need a Column, got $col"
unless
$col
->isa(
"Class::DBI::Column"
);
$self
->{_allcol}->{
$col
->name } ||=
$col
;
}
sub
find_column
{
my
(
$self
,
$name
) =
@_
;
return
$name
if
ref
$name
;
return
unless
$self
->{_allcol}->{
$name
};
}