[Loro-extends] LoroI SQL VEE 0.1
Status: Beta
Brought to you by:
carueda
From: Marlon J. M. <ma...@ex...> - 2001-07-31 05:17:30
|
LoroI =96 SQL Acceso a Bases de Datos Relacionales http://loro.sourceforge.net Marlon J. Manrique ma...@ex... Versi=F3n 0.1 Julio 30 de 2001 Introducci=F3n Los motores de bases de datos relaci=F3nales, son el paradigma m=E1s utilizado en la industria para permitir la persistencia de datos. La especificaci=F3n pretende proveer una soluci=F3n a los requerimientos m=EDnimos para el acceso a datos almacenados en bases de datos relaci=F3nales como PostgreSQL, MySQL, MS Access, Oracle, DB2 entre otros. Objetivos Los objetivos perseguidos con este especificaci=F3n son : Permitir el intercambio de datos entre aplicaciones escritas en el lenguaje Loro y motores de bases de datos relaci=F3nales. Utilizar el SQL como lenguaje para realizar el intercambio de datos, as=ED como la definici=F3n de la base de datos. Desarrollo de aplicaciones con Acceso a Bases de Datos Las aplicaciones que acceden a bases de datos son aplicaciones que para su funcionamiento usan las bases de datos como mecanismo para dar persistencia a datos que existen mas halla de una ejecuci=F3n. Las aplicaciones que utilizan bases de datos necesitan de una aplicaci=F3= n encargada de la persistencia de datos con la cual se puedan comunicar, esta aplicaci=F3n t=EDpicamente reside en un equipo dentro de la red conocido como el servidor de datos, la aplicaci=F3n se comunica a trav=E9= s de protocolos de red con dicha aplicaci=F3n comport=E1ndose como un clien= te de la base de datos. As=ED la conexi=F3n con la base de datos necesita conocer en que equipo y puerto se encuentra la aplicaci=F3n encargada de escuchar las peticiones a la base datos, as=ED como tambi=E9n un identificador de usuario y una palabra clave que permita a la base de datos verificar la autenticidad de la identidad del cliente, como tambi=E9n los privilegios que el usuario posee dentro de la base de datos. Uno de los standards m=E1s usados para la comunicaci=F3n entre las aplicaciones cliente y el motor de base de datos es el lenguaje estructurado de consulta que permite a trav=E9s de sentencias crear, actualizar y eliminar tanto datos como la definici=F3n propia de las estructuras que componen la base de datos. Componentes Los componentes de una aplicaci=F3n que acceda bases de datos son las siguientes : Un motor de base de datos encargado de dar persistencia a los datos suministrados por la aplicaci=F3n. Manija de conexi=F3n e intercambio de datos con la base de datos a trav=E9= s de SQL. Aplicaci=F3n que acceda a la base de datos para crear, actualizar o eliminar estructuras dentro de la base de datos y datos. Responsabilidades El sistema debe hacerse responsable de : Cargar el manejador de acceso a la base de datos especifico para la base de datos a utilizar. Crear un o mas conexiones a la base de datos. Permitir crear, actualizar y eliminar estruturas de datos y datos a trav=E9s de SQL. Permitir liberar los recursos utilizados para realizar la conexi=F3n y el intercambio de datos. Especificaciones Se propone la siguiente especificaci=F3n en Loro nivel I para el acceso a bases de datos relaci=F3nales. Paquete loroI::sql Clases Conexion Resultado Sentencia Especificaciones cargarControlador crearConexion Conexion_cerrar Conexion_crearSentencia Resultado_cerrar Resultado_obtBooleano Resultado_obtCadena Resultado_obtEntero Resultado_obtReal Resultado_siguiente Sentencia_ejecutarActualizacion Sentencia_ejecutarConsulta cargarControlador especificacion cargarControlador(controlador : cadena) -> cargado : booleano descripcion "Cagar el controlador de acceso a la base de datos, este controlador esta representado por un identificador." entrada controlador : "Identificador del controlador." salida cargado : "cierto si el controlador pudo ser cargado, falso de lo contrario." pre { controlador !=3D nulo } pos { cierto } // pendiente fin especificacion crearConexion especificacion crearConexion(url : cadena, usuario : cadena, contrase=F1a : cadena) -> conexion : Conexion descripcion "Trata de crear una conexion con la base de datos representada por el URL suministrado, identificandose como un usuario de la base de datos con la contrase=F1a suministrada." entrada url : "URL de acceso a la base de datos." usuario : "nombre de usuario en la base de datos." contrase=F1a : "contrase=F1a asociada al usuario en la base de datos." salida conexion : "Objeto que representa la conexion con la base de datos." pre { url !=3D nulo && usuario !=3D nulo && contrase=F1a !=3D nulo } pos { cierto } // pendiente fin especificacion Conexion_cerrar especificacion Conexion_cerrar(conexion : Conexion) descripcion "Cierra la conexion con la base de datos, liberando los recursos utilizados para la Conexion." entrada conexion : "Conexion con la base de datos." pre { conexion !=3D nulo } fin especificacion Conexion_crearSentencia especificacion Conexion_crearSentencia(conexion : Conexion) -> sentencia : Sentencia descripcion "Crea una objeto Sentencia que permite enviar sentencias SQL a la base de datos, asociada a la conexion." entrada conexion : "Conexion a la base de datos." salida sentencia : "Objeto que permite enviar sentencias a la base de datos." pre { conexion !=3D nulo } pos { cierto } // pendiente fin especificacion Resultado_cerrar especificacion Resultado_cerrar(resultado : Resultado) descripcion "Cierra el conjunto de resultados, liberando los recursos usados por el Resultado. Nota : Un resultado es cerrado automaticamente cuando la Sentencia que los genero es cerrada o se vuelve a ejecutar." entrada resultado : "conjunto de resultados de una consulta." pre { resultado !=3D nulo } fin especificacion Resultado_obtBooleano especificacion Resultado_obtBooleano(resultado : resultado, indiceColumna : entero) -> valor : booleano descripcion "Retorna el valor de la columna en el registro actual del Resultado como un booleano." entrada resultado : "Resultado de la ejecucion de la consulta." indiceColumna : "Indice de la columna." salida valor : "valor booleano de la columna." pre { resultado !=3D nulo && indiceColumna > 0 } pos { cierto } // pendiente fin especificacion Resultado_obtCadena especificacion Resultado_obtCadena(resultado : resultado, indiceColumna : entero) -> valor : cadena descripcion "Retorna el valor de la columna en el registro actual del Resultado como una cadena." entrada resultado : "Resultado de la ejecucion de la consulta." indiceColumna : "Indice de la columna." salida valor : "valor cadena de la columna." pre { resultado !=3D nulo && indiceColumna > 0 } pos { cierto } // pendiente fin especificacion Resultado_obtEntero especificacion Resultado_obtEntero(resultado : resultado, indiceColumna : entero) -> valor : entero descripcion "Retorna el valor de la columna en el registro actual del Resultado como un entero." entrada resultado : "Resultado de la ejecucion de la consulta." indiceColumna : "Indice de la columna." salida valor : "valor entero de la columna." pre { resultado !=3D nulo && indiceColumna > 0 } pos { cierto } // pendiente fin especificacion Resultado_obtReal especificacion Resultado_obtReal(resultado : resultado, indiceColumna : entero) -> valor : real descripcion "Retorna el valor de la columna en el registro actual del Resultado como un real." entrada resultado : "Resultado de la ejecucion de la consulta." indiceColumna : "Indice de la columna." salida valor : "valor real de la columna." pre { resultado !=3D nulo && indiceColumna > 0 } pos { cierto } // pendiente fin especificacion Resultado_siguiente especificacion Resultado_siguiente(resultado : resultado) -> siguiente : booleano descripcion "Mueve el cursor asociado al resultado una fila abajo. El cursor esta inicialmente posicionado antes de la primera fila, el primer llamado a siguiente hace de que la fila uno sea la actual." entrada resultado : "Resultado de la ejecucion de la consulta." salida siguiente : "falso si no existen mas columna, cierto de lo contrario." pre { resultado !=3D nulo } pos { cierto } // pendiente fin especificacion Sentencia_ejecutarActualizacion especificacion Sentencia_ejecutarActualizacion(sentencia : Sentencia, sql : cadena) -> registros : entero descripcion "Ejecuta una sentencia de actualizacion en la base de datos (Insertar, Actualiza o Borrar). Adicionalmente se pueden ejecutar sentencias que no retornen nada, como sentencias para la definicion de la base de datos." entrada sentencia : "Objeto que permite ejecutar sentencias SQL en la base de datos asociada con la conexion." sql : "Sentencia SQL para Insertar, Actualizar o Eliminar datos o sentencias para la definicion de la base de datos." salida registros : "Conteo de registros modificados por la sentencia o 0 si la sentencia no modificaba la base de datos." pre { sentencia !=3D nulo && sql !=3D nulo} pos { cierto } // pendiente fin especificacion Sentencia_ejecutarConsulta especificacion Sentencia_ejecutarConsulta(sentencia : Sentencia, sql : cadena) -> resultado : Resultado descripcion "Ejecuta una sentencia que retorna un resultado en la base de datos." entrada sentencia : "Objeto que permite ejecutar sentencias SQL en la base de datos." sql : "Sentencia SQL que retorna un resultado." salida resultado : "Resultado de la ejecucion de la consulta." pre { sentencia !=3D nulo && sql !=3D nulo} pos { cierto } // pendiente fin especificacion Un Ejemplo de Uso Se mostrar el uso de la anterior especificaci=F3n a trav=E9s de un ejempl= o de carga de un controlador de acceso a la base de datos, la creaci=F3n de una tabla, la inserci=F3n de datos a dicha tabla y la consulta de datos ingresados en dicha tabla. utiliza clase loroI::sql::Conexion; utiliza clase loroI::sql::Resultado; utiliza clase loroI::sql::Sentencia; utiliza algoritmo loroI::sql::cargarControlador; utiliza algoritmo loroI::sql::crearConexion; utiliza algoritmo loroI::sql::Conexion_cerrar; utiliza algoritmo loroI::sql::Conexion_crearSentencia; utiliza algoritmo loroI::sql::Sentencia_cerrar; utiliza algoritmo loroI::sql::Sentencia_ejecutarActualizacion; utiliza algoritmo loroI::sql::Sentencia_ejecutarConsulta; utiliza algoritmo loroI::sql::Resultado_cerrar; utiliza algoritmo loroI::sql::Resultado_obtCadena; utiliza algoritmo loroI::sql::Resultado_siguiente; especificacion demoSQL() descripcion "Demostrar el uso de la especificacion SQL, Cargar el controlador de acceso a la base de datos, crear una conexion con una base de datos, crear una sentencia para enviar comandos SQL a la base de datos, actualizando y consultado la base de datos y liberar los recursos utilizados." fin especificacion algoritmo para demoSQL() estrategia "" inicio conexion : Conexion; sentencia : Sentencia; resultado : Resultado; // Cargar el controlador especifico para la conexion con // la base de datos. si cargarControlador("sun.jdbc.odbc.JdbcOdbcDriver") !=3D cierto entonces escribirln("No es posible cargar el Controlador"); fin si; // Crear la conexion con la base de datos conexion :=3D crearConexion("jdbc:odbc:prueba","",""); si conexion =3D nulo entonces escribirln("No es posible abrir la conexion con la base de datos."); retorne; fin si; // Crear la sentencia. sentencia :=3D Conexion_crearSentencia(conexion); // Actualizar la base de datos. Sentencia_ejecutarActualizacion(sentencia,"drop table usuario"); Sentencia_ejecutarActualizacion(sentencia,"create table usuario (nombre char(8), password char(8))"); Sentencia_ejecutarActualizacion(sentencia,"insert into usuario values('pepito','malcriado')"); Sentencia_ejecutarActualizacion(sentencia,"insert into usuario values('juanito','pera')"); Sentencia_ejecutarActualizacion(sentencia,"insert into usuario values('mika','f1')"); Sentencia_ejecutarActualizacion(sentencia,"insert into usuario values('marlonj','deamon')"); // Consultar la base de datos. resultado :=3D Sentencia_ejecutarConsulta(sentencia,"select * from usuario"); // Mientras haya filas en el resultado imprmir el contenido de la // primera columna. mientras Resultado_siguiente(resultado) =3D cierto haga nombre : cadena; nombre :=3D Resultado_obtCadena(resultado,1); escribirln(nombre); fin mientras; // Cierra el resultado, la sentencia y la conexion a la base de // datos. Resultado_cerrar(resultado); Sentencia_cerrar(sentencia); Conexion_cerrar(conexion); fin algoritmo |