function out = get_type_and_contraints(column,value)

% get_type_and_constraints - get type and constraint information
% --------------------------------------------------------------
%
% out = get_type_and_constraints(column,value)
%
% Input:
% ------
%  column - column name
%  value - column instance values
%
% Output:
% -------
%  out - struct output

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1600 $
% $Date: 2005-08-18 17:41:06 -0400 (Thu, 18 Aug 2005) $
%--------------------------------

% TODO: use a sequence of values to infer types

out.constraints = get_constraints(column);

out.type = get_type(value);


%-----------------------------------------------------
% GET_CONSTRAINTS
%-----------------------------------------------------

function str = get_constraints(column)

% get_constraints - get constraints based on naming conventions
% -------------------------------------------------------------
%
% str = get_constraints(column)
%
% Input:
% ------
%  column - field name
%
% Output:
% -------
%  str - constraint description

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1600 $
% $Date: 2005-08-18 17:41:06 -0400 (Thu, 18 Aug 2005) $
%--------------------------------

%--
% initialize constraints and normalize column name
%--

str = cell(0); column = lower(column);

%--
% consider not null conventions
%--

not_null_words = {'created','modified','author'};

for k = 1:length(not_null_words)
	
	if (~isempty(findstr(column,not_null_words{k})))
		str{end + 1} = 'NOT NULL'; break;
	end
	
end

%--
% consider key conventions
%--

if (strcmp(column,'id'))
	
	str{end + 1} = 'NOT NULL PRIMARY KEY'; 
	
else
			
	key_words = {'_id'};

	for k = 1:length(key_words)
		
		if (~isempty(findstr(column,key_words{k})))
			str{end + 1} = 'NOT NULL FOREIGN KEY'; break;
		end
		
	end
	
end

%--
% concatenate constraints
%--

temp = '';

for k = 1:length(str)
	temp = [temp, ' ', str{k}];
end

str = temp;

disp(str);


%-----------------------------------------------------
% GET_TYPE
%-----------------------------------------------------

function type = get_type(value)

% get_type - get type description for value
% -----------------------------------------
%
% type = get_type(value)
%
% Input:
% ------
%  value - content of struct field
%
% Output:
% -------
%  type - fragment describing value type

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1600 $
% $Date: 2005-08-18 17:41:06 -0400 (Thu, 18 Aug 2005) $
%--------------------------------

%--
% DEFAULT
%--

% NOTE: return empty for container and object types

type = '';

%--
% NUMERIC
%--

if (isnumeric(value))
	
	% NOTE: array contents are stored as blobs
	
	if (numel(value) > 1)
		type = 'BLOB'; return;
	end
	
	if (isfloat(value))
		type = 'REAL'; return;
	end

	if (isinteger(value))
		type = 'INTEGER'; return;
	end
	
	type = 'BLOB';
	
end

%--
% LOGICAL
%--

if (islogical(value))
	
	if (numel(value) > 1)
		type = 'BLOB'; return;
	end
	
	type = 'BOOLEAN'; return;
	
end

%--
% STRINGS
%--

if (ischar(value))
	type = 'VARCHAR'; return;
end

if (iscellstr(value))
	type = 'TEXT'; return;
end