{-- TerraHS - Interface between TerraLib and Haskell

    (c) Sergio Costa (INPE) - Setembro, 2005

	This program is free software; you can redistribute it 
    and/or modify it under the terms of the GNU General 
    Public License 2.1 as published by the Free Software Foundation
    (http://www.opensource.org/licenses/gpl-license.php)
--}

{-- |
	A module for supporting a TeDatabase TerraLib class

	Instances of this classes represent connections
	to a database server. It includes the host name, 
	user name and password parameters of the connection.
	It should be implemented by the drivers to specific
	database servers as: MySQL, Ado, PostgreSQL or any
	other server to be used in TerraLib applications.

	 More information - <http://www.terralib.org>
--}

module TerraHS.TerraLib.TeDatabase
	(
	
	-- * The @TeDatabase@ type
	TeDatabase(..),

	-- * The @TeDatabasePtr@ type
	TeDatabasePtr,
	
	-- * The @Layers@ class	
	--TeDatabases (..) ,
	
	--teimportraster,
	) 
	where


import Foreign.C.String
import qualified Foreign.Ptr (Ptr)

import TerraHS.Algebras.Base.Object
import TerraHS.Misc.GenericFunctions

--import TerraHS.TerraLib.TeGeometry
--import TerraHS.TerraLib.TeGeometryAlgorithms
--import TerraHS.TerraLib.TeSTInstance
--import TerraHS.TerraLib.TeLayer
--import TerraHS.TerraLib.TeRaster
--import TerraHS.TerraLib.TeTable
--import TerraHS.TerraLib.TeQuerier

import TerraHS.Algebras.DB.Databases

data TeDatabase = TeMySQL String String String String | TePostgreSQL

-- | The type @TeDatabasePtr@ is a pointer to @TeDatabase@
type TeDatabasePtr = Foreign.Ptr.Ptr TeDatabase

instance Connection TeDatabase where

	open (TeMySQL host user pass dbname) = do
		h <- newCString host 
		u <- newCString user 
		p <- newCString pass 
		n <- newCString dbname 
		ptr <- new (TeMySQL host user pass dbname)
		st <- h_tedatabase_connect ptr h u p n
		if st == False then (errorMessage ptr) >>= error else do return ptr

		
	close db = (tedatabase_close db)



instance Pointer TeDatabase where
	new (TeMySQL _ _ _ _)  = (temysql_new)
	new TePostgreSQL = error ("nao implementado")
		
	delete db = (tedatabase_destroy db)			

--- 
errorMessage :: TeDatabasePtr -> Prelude.IO String	
errorMessage db = h_tedatabase_errorMessage db >>= peekCString >>= return


-----------------------------------------------------------------------------------------
-- terralib functions
-----------------------------------------------------------------------------------------

-- instanciação (MySQL)	 
foreign import stdcall unsafe "c_temysql_new"  temysql_new :: Prelude.IO TeDatabasePtr
-- destruição ponteiro
foreign import stdcall unsafe "c_tedatabase_destroy" tedatabase_destroy :: TeDatabasePtr -> Prelude.IO ()
foreign import stdcall unsafe "c_tedatabase_connect" h_tedatabase_connect :: TeDatabasePtr -> CString -> CString -> CString -> CString -> Prelude.IO Bool
-- fechando um banco de dados
foreign import stdcall unsafe "c_tedatabase_close" tedatabase_close :: TeDatabasePtr -> Prelude.IO ()
foreign import stdcall unsafe "c_tedatabase_errorMessage"  h_tedatabase_errorMessage :: TeDatabasePtr -> Prelude.IO CString


--