<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jreynoso666</title>
    <description>The latest articles on DEV Community by jreynoso666 (@jreynoso666).</description>
    <link>https://dev.to/jreynoso666</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F28557%2Fb3e92312-f59e-439e-96a8-972d751c424b.png</url>
      <title>DEV Community: jreynoso666</title>
      <link>https://dev.to/jreynoso666</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jreynoso666"/>
    <language>en</language>
    <item>
      <title>Postgresql and Lisp (DAO)</title>
      <dc:creator>jreynoso666</dc:creator>
      <pubDate>Wed, 02 Aug 2017 21:32:03 +0000</pubDate>
      <link>https://dev.to/jreynoso666/postgresql-and-lisp-dao</link>
      <guid>https://dev.to/jreynoso666/postgresql-and-lisp-dao</guid>
      <description>&lt;p&gt;&lt;a href="http://www.huuii.com/people/juan/3868"&gt;http://www.huuii.com/people/juan/3868&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Postgresql and Lisp&lt;/p&gt;

&lt;p&gt;There is a project called postmodern.&lt;/p&gt;

&lt;p&gt;Postmodern is a Common Lisp library for interacting with PostgreSQL databases.&lt;/p&gt;

&lt;p&gt;see for more information: &lt;a href="http://marijnhaverbeke.nl/postmodern/#quickstart"&gt;http://marijnhaverbeke.nl/postmodern/#quickstart&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First I am going to create the user and  database into postgresql:&lt;/p&gt;

&lt;p&gt;create user foo with password 'foobar';&lt;/p&gt;

&lt;p&gt;create database bar owner foo ;&lt;/p&gt;

&lt;p&gt;Now we need to load the library by quicklisp&lt;/p&gt;

&lt;p&gt;(ql:quickload "postmodern")&lt;/p&gt;

&lt;p&gt;To load "postmodern":&lt;/p&gt;

&lt;p&gt;Load 1 ASDF system:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postmodern
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;; Loading "postmodern"&lt;/p&gt;

&lt;p&gt;("postmodern")&lt;/p&gt;

&lt;p&gt;After that, we are going to create a file with:&lt;/p&gt;

&lt;p&gt;(in-package :postmodern)&lt;/p&gt;

&lt;p&gt;;;; define the var with the values of the database&lt;br&gt;
(defvar &lt;em&gt;db-parameters&lt;/em&gt; '("bar" "foo" "foobar" "localhost" :POOLED-P T))&lt;/p&gt;

&lt;p&gt;;; define the macro to connect to the postgresql server&lt;br&gt;
(defmacro with-database (&amp;amp;body query)&lt;br&gt;
  "This macro creates the connection and disconnection with specified database in &lt;em&gt;db-parameter&lt;/em&gt; and execute the query."&lt;br&gt;
  `(postmodern:with-connection &lt;em&gt;db-parameters&lt;/em&gt; ,@query))&lt;/p&gt;

&lt;p&gt;Create the table&lt;/p&gt;

&lt;p&gt;;;; define a simple table&lt;/p&gt;

&lt;p&gt;(defclass fruits ()&lt;/p&gt;

&lt;p&gt;((id-fruit :accessor id-fruit :col-type serial :initarg :id-fruit)&lt;/p&gt;

&lt;p&gt;(name :accessor name :col-type string :initarg :name :initform ""))&lt;/p&gt;

&lt;p&gt;(:documentation "Dao class for a fruit record.")&lt;/p&gt;

&lt;p&gt;(:metaclass postmodern:dao-class)&lt;/p&gt;

&lt;p&gt;(:table-name fruits)(:keys id-fruit))&lt;/p&gt;

&lt;p&gt;;;create the table in postgresql server&lt;/p&gt;

&lt;p&gt;(with-database (create-table 'fruits))&lt;/p&gt;

&lt;p&gt;Database access objects (CRUD)&lt;/p&gt;

&lt;p&gt;;; insert&lt;br&gt;
(with-database&lt;br&gt;
  (insert-dao (make-instance 'fruits :name "apple"))&lt;br&gt;
  (insert-dao (make-instance 'fruits :name "orange")))&lt;/p&gt;

&lt;p&gt;;; select&lt;br&gt;
(with-database&lt;br&gt;
  (get-dao 'fruits 1))&lt;/p&gt;

&lt;p&gt;(with-database&lt;br&gt;
  (select-dao 'fruits (:= 'id-fruit 2)))&lt;/p&gt;

&lt;p&gt;;; define a method to display information&lt;br&gt;
(defmethod read-information ((obj fruits))&lt;br&gt;
  (format t "id= ~a~%name= ~a~%" (id-fruit obj) (name obj)))&lt;/p&gt;

&lt;p&gt;;;delete&lt;br&gt;
(with-database&lt;br&gt;
  (delete-dao (make-instance 'fruits :id-fruit 1)))&lt;/p&gt;

&lt;p&gt;;;update&lt;br&gt;
(defun the-update (id new-name)&lt;br&gt;
  (let ((record nil))&lt;br&gt;
    ;; first we neet the record&lt;br&gt;
    (setf record (with-database&lt;br&gt;
           (get-dao 'fruits id)))&lt;br&gt;
    ;; the the new value&lt;br&gt;
    (setf (name record) new-name)&lt;br&gt;
    ;; finally update the record&lt;br&gt;
    (with-database&lt;br&gt;
      (update-dao record))))&lt;/p&gt;

</description>
      <category>lisp</category>
      <category>postgres</category>
      <category>example</category>
    </item>
  </channel>
</rss>
