--[[----------------------------------------------------------------------------

WFSQLiteUtils.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2011 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- WF.core.table
local WFTableArchiving = require 'WFTableArchiving'

	-- WF.core.utils
local KSFunctionUtils = require 'KSFunctionUtils'
local WFArguments = require 'WFArguments'


--------------------------------------------------------------------------------

local check = assert( WFArguments.check )

--------------------------------------------------------------------------------

local createVariableArgList = KSFunctionUtils.memoizeFn( function( numberOfArgs )

	if numberOfArgs > 0 then
	
		return string.format( "?%s", string.rep( ", ?", numberOfArgs - 1 ) )

	else
	
		return ""
	
	end

end )

--============================================================================--
--[[!
	\brief This namespace assists in encoding Lua data types
		that are not native to SQLite.
--]]

-- @mark WFSQLiteUtils namespace

local WFSQLiteUtils = {}

--------------------------------------------------------------------------------
--[[!
	\brief Converts a Lua value to a value and type that can be represented
		in SQLite.

	\param value (any scalar data type) The value to be converted.

	\return The value encoded for SQLite
	\return (string or nil) The data type if translation occurred, \c nil if not.
--]]

function WFSQLiteUtils.luaValueToSqliteValueAndType( --[[optional scalar]] value )

	local vType = type( value )

	if vType == 'boolean' then

		return tostring( value ), vType

	elseif vType == 'table' then

		return WFTableArchiving.tableToString( value ), 'table'

	else

		return value, nil
			-- Nil for data type: Don't waste space encoding data types that
			-- we can bridge directly to SQLite as is.

	end

end

--------------------------------------------------------------------------------
--[[!
	\brief Converts a SQLite value and type pair (a returned from a previous call to
		\c luaValueToSqliteValueAndType) back to the original Lua value type.

	\param value (any scalar data type) The value to be converted.
	\param dataType (string or nil) The data type if translation occurred, or nil to pass through

	\return The decoded value.
--]]

function WFSQLiteUtils.sqliteValueAndTypeToLuaValue( --[[optional scalar]] value,
													   --[[optional string]] dataType )

	check( '_', '?string' )

	if dataType == 'boolean' then

		return value ~= 'false'

	elseif dataType == 'table' then

		local loadfunc = loadstring( 'return ' .. value )
		return loadfunc()

	else

		return value

	end

end

--------------------------------------------------------------------------------
--[[!
	\brief Retrieves the table name from an SQL <code>CREATE TABLE</code> statement.

	\param sql (string) An SQL <code>CREATE TABLE</code> statement to parse,

	\return (string) The table name.
--]]

function WFSQLiteUtils.tableNameFromCreateTableStatement( sql )

	check( 'string' )

	local _, _, tableName = sql:find( "([%a_][%w_]*)%s+[Uu][Ss][Ii][Nn][Gg]%s+[Ff][Tt][Ss][34]%s*%(" )
	if not tableName then
		_, _, tableName = sql:find( "([%a_][%w_]*)%s*%(" )
	end

	return tableName

end

--------------------------------------------------------------------------------
--[[!
	\brief Retrieves a formatted list of variable SQL arguments that can be plugged
		into the IN( .. ) part of a SQL statement.

	\param numberOfArgs (number) The number of variable args.

	\return (string) A formatted list of variable SQL arguments.
--]]

function WFSQLiteUtils.createVariableArgList( numberOfArgs )

	check( 'number' )

	return createVariableArgList( numberOfArgs )

end

--------------------------------------------------------------------------------

return WFSQLiteUtils
