=head1 NAME

LabKey::Query

=head1 SYNOPSIS

	use LabKey::Query;
	my $results = LabKey::Query::selectRows(
		-baseUrl => 'http://labkey.com:8080/labkey/',
		-containerPath => 'myFolder/',
		-schemaName => 'lists',
		-queryName => 'mid_tags',
	);
		
=head1 ABSTRACT

For interacting with data in LabKey Server

=head1 DESCRIPTION

This module is designed to simplify querying and manipulating data in LabKey Server.  It should more or less replicate the javascript APIs of the same names. 

After the module is installed, if you need to login with a specific user you 
will need to create a L<.netrc|https://www.labkey.org/Documentation/wiki-page.view?name=netrc>
file in the home directory of the user running the perl script.

In API versions 0.08 and later, you can specify the param '-loginAsGuest'
which will query the server without any credentials.  The server must permit 
guest to that folder for this to work though.

=head1 FUNCTIONS

=head2 selectRows()

selectRows() can be used to query data from LabKey server

The following are the minimum required params:
		
	my $results = LabKey::Query::selectRows(
		-baseUrl => 'http://labkey.com:8080/labkey/',
		-containerPath => 'myFolder/',
		-schemaName => 'lists',
		-queryName => 'mid_tags',
	);

The following are optional:

	-viewName => 'view1',
	-filterArray => [
		['file_active', 'eq', 1], 
		['species', 'neq', 'zebra']
	], #allows filters to be applied to the query similar to the labkey Javascript API.
	-parameters => [
		['enddate', '2011/01/01'], 
		['totalDays', 15]
	], #allows parameters to be applied to the query similar to the labkey Javascript API.	
	-maxRows => 10	#the max number of rows returned
	-sort => 'ColumnA,ColumnB'	#sort order used for this query
	-offset => 100	#the offset used when running the query
	-columns => 'ColumnA,ColumnB'  #A comma-delimited list of column names to include in the results.
	-containerFilterName => 'currentAndSubfolders'
	-debug => 1,	#will result in a more verbose output
	-loginAsGuest => #will not attempt to lookup credentials in netrc
	-netrcFile => optional. the location of a file to use in place of a .netrc file.  see also the environment variable LABKEY_NETRC.
	-requiredVersion => 9.1 #if 8.3 is selected, it will use LabKey's pre-9.1 format for returning the data.  9.1 is the default.  See documentation of LABKEY.Query.ExtendedSelectRowsResults for more detail here:
		https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.html
	-useragent => an instance of LWP::UserAgent (if not provided, a new instance will be created)
	-timeout => timeout in seconds (used when creating a new LWP::UserAgent)
	
=head3 NOTE

In version 1.0 and later of the perl API, the default result format is 9.1.  This is different from the LabKey JS, which defaults to the earlier format for legacy purposes.

=head2 insertRows()

insertRows() can be used to insert records into a LabKey table

The following are the minimum required params:

	my $insert = LabKey::Query::insertRows(
		-baseUrl => 'http://labkey.com:8080/labkey/',
		-containerPath => 'myFolder/',
		-schemaName => 'lists',
		-queryName => 'backup',
		-rows => [{
			"JobName" => 'jobName', 
			"Status" => $status, 
			"Log" => $log, 
			"Date" => $date
		}],
	);
 
The following are optional:

	-debug => 1,  #will result in a more verbose output 
	-loginAsGuest => #will not attempt to lookup credentials in netrc
	-netrcFile => optional. the location of a file to use in place of a .netrc file.  see also the environment variable LABKEY_NETRC.
	-useragent => an instance of LWP::UserAgent (if not provided, a new instance will be created)
	-timeout => timeout in seconds (used when creating a new LWP::UserAgent)

=head2 updateRows()

updateRows() can be used to update records in a LabKey table

The following are the minimum required params:

	my $update = LabKey::Query::updateRows(
		-baseUrl => 'http://labkey.com:8080/labkey/',
		-containerPath => 'myFolder/',
		-schemaName => 'lists',
		-queryName => 'backup',
		-rows => [{
			"JobName" => 'jobName', 
			"Status" => $status, 
			"Log" => $log, 
			"Date" => $date
		}],
	);
		
The following are optional:

	-debug => 1,  #will result in a more verbose output
	-loginAsGuest => #will not attempt to lookup credentials in netrc
	-netrcFile => optional. the location of a file to use in place of a .netrc file.  see also the environment variable LABKEY_NETRC.
	-useragent => an instance of LWP::UserAgent (if not provided, a new instance will be created)
	-timeout => timeout in seconds (used when creating a new LWP::UserAgent)

=head2 deleteRows()

deleteRows() can be used to delete records in a LabKey table

The following are the minimum required params:

	my $update = LabKey::Query::deleteRows(
		-baseUrl => 'http://labkey.com:8080/labkey/',
		-containerPath => 'myFolder/',
		-schemaName => 'lists',
		-queryName => 'backup',
		-rows => [{
			"Key" => '12', 
		}],
	);
		
The following are optional:

	-debug => 1,  #will result in a more verbose output
	-loginAsGuest => #will not attempt to lookup credentials in netrc
	-netrcFile => optional. the location of a file to use in place of a .netrc file.  see also the environment variable LABKEY_NETRC.
	-useragent => an instance of LWP::UserAgent (if not provided, a new instance will be created)
	-timeout => timeout in seconds (used when creating a new LWP::UserAgent)

=head2 executeSql()

executeSql() can be used to execute arbitrary SQL

The following are the minimum required params:

	my $result = LabKey::Query::executeSql(
		-baseUrl => 'http://labkey.com:8080/labkey/',
		-containerPath => 'myFolder/',
		-schemaName => 'study',
		-sql => 'select MyDataset.foo, MyDataset.bar from MyDataset',
	);
		
The following are optional:

	-maxRows => 10	#the max number of rows returned
	-sort => 'ColumnA,ColumnB'	#sort order used for this query
	-offset => 100	#the offset used when running the query
	-containerFilterName => 'currentAndSubfolders'
	-debug => 1,  #will result in a more verbose output
	-loginAsGuest => #will not attempt to lookup credentials in netrc
	-netrcFile => optional. the location of a file to use in place of a .netrc file.  see also the environment variable LABKEY_NETRC.
	-useragent => an instance of LWP::UserAgent (if not provided, a new instance will be created)
	-timeout => timeout in seconds (used when creating a new LWP::UserAgent)

=pod

=head1 ENVIORNMENT VARIABLES

=over 4

=item *
The 'LABKEY_URL' environment variable can be used instead of supplying a '-baseUrl' param.

=item *
The 'LABKEY_NETRC' environment variable can be used to specify an alternate location of a netrc file, if not in the user's home directory.

=back

=head1 AUTHOR

LabKey C<info@labkey.com>

=head1 CONTRIBUTING

Send comments, suggestions and bug reports to:

L<https://www.labkey.org/home/developer/forum/project-start.view>


=head1 COPYRIGHT
 
Copyright (c) 2010 Ben Bimber
Copyright (c) 2011-2018 LabKey Corporation

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

=head1 SEE ALSO

The LabKey client APIs are described in greater detail here:
https://www.labkey.org/Documentation/wiki-page.view?name=viewAPIs

Support questions should be directed to the LabKey forum:
https://www.labkey.org/home/Server/Forum/announcements-list.view

