annotate base/database/datasets-functions.lisp @ 221:317fe3126c6c

add database-admin function to create composition identifiers from file Ignore-this: ff758dc4be1061baebae471feee53f00 darcs-hash:20090820181949-16a00-389500f68b818adca481631fc7ae26be59a8ad4a.gz committer: Jamie Forth <j.forth@gold.ac.uk>
author j.forth <j.forth@gold.ac.uk>
date Thu, 24 Feb 2011 11:23:18 +0000
parents 385935631532
children
rev   line source
j@216 1 (cl:in-package #:amuse-database-admin)
j@216 2
j@216 3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
j@216 4 ;;; Constructors
j@216 5
j@216 6 (defun make-amuse-dataset-identifier (dataset-id)
j@216 7 (make-instance 'amuse-dataset-identifier
j@216 8 :dataset-id dataset-id))
j@216 9
j@216 10 (defun %make-amuse-dataset (dataset-identifier description
j@216 11 composition-identifiers)
j@216 12 (make-instance 'amuse-dataset
j@216 13 :%data composition-identifiers
j@216 14 :identifier dataset-identifier
j@216 15 :description description))
j@216 16
j@216 17
j@216 18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
j@216 19 ;;; Database functions
j@216 20
j@216 21 (defun make-new-dataset (description &optional (database
j@216 22 *amuse-database*))
j@216 23 "A dataset is a set of pieces used for a particualar analytical
j@216 24 task. A dataset is not necessarily the same thing as a corpus or
j@216 25 collection (are these things different?). Corpus indicates that a set
j@216 26 of pieces have been curated in some way and in that sense `belong
j@216 27 together'. A dataset is just simply a set of pieces gathered together
j@216 28 to analyse, and the pieces can be from any corpus or backend."
j@216 29 (let (dataset-id)
j@216 30 #.(clsql:locally-enable-sql-reader-syntax)
j@216 31 (clsql:insert-records :into "amuse_datasets"
j@216 32 :attributes '([description])
j@216 33 :values (list description)
j@216 34 :database database)
j@216 35 #.(clsql:locally-disable-sql-reader-syntax)
j@216 36 (setf dataset-id (clsql-mysql::mysql-insert-id
j@216 37 (clsql-mysql::database-mysql-ptr
j@216 38 database)))
j@216 39 (make-amuse-dataset-identifier dataset-id)))
j@216 40
j@216 41 (defun assign-composition-to-dataset (composition-identifier
j@216 42 dataset-identifier
j@216 43 &optional (database
j@216 44 *amuse-database*))
j@216 45 (clsql:execute-command (format nil "
j@216 46 INSERT INTO amuse_datasets_join
j@216 47 SET dataset_id := ~S,
j@216 48 implementation_id := (SELECT get_impl_id('~A')),
j@216 49 composition_id := ~S;"
j@216 50 (dataset-id dataset-identifier)
j@216 51 (implementation-namestring
j@216 52 composition-identifier)
j@216 53 (composition-id
j@216 54 composition-identifier)))
j@216 55 :database database)
j@216 56
j@218 57 (defun assign-compositions-to-dataset (composition-identifiers
j@218 58 dataset-identifier
j@218 59 &optional (database
j@218 60 *amuse-database*))
j@218 61 "This should be done more sensibly."
j@218 62 (loop for identifier in composition-identifiers
j@218 63 do (assign-composition-to-dataset identifier
j@218 64 dataset-identifier
j@218 65 database)))
j@218 66
j@221 67 (defun make-composition-identifiers-from-file (package pathname)
j@221 68 "This reads a file that contains one id per line, and returns a list
j@221 69 of composition-identifiers (specialised on package). It is useful for
j@221 70 reading files that have been exported from the database, for use with
j@221 71 the above functions."
j@221 72 (with-open-file (stream pathname :direction :input)
j@221 73 (loop for id = (read stream nil)
j@221 74 while id
j@221 75 collect (make-composition-identifier package id))))
j@221 76
j@216 77 (defun get-dataset (dataset-identifier &optional (database
j@216 78 *amuse-database*))
j@216 79 (let ((dataset-header (clsql:query (format nil "
j@216 80 SELECT description
j@216 81 FROM amuse_datasets
j@216 82 WHERE dataset_id = ~S" (dataset-id dataset-identifier))
j@216 83 :database database
j@216 84 :flatp t
j@216 85 :field-names nil))
j@216 86 (dataset-rows (clsql:query (format nil "
j@216 87 SELECT implementation_name, composition_id
j@216 88 FROM amuse_datasets_join
j@216 89 LEFT JOIN amuse_implementations
j@216 90 USING (implementation_id)
j@216 91 WHERE dataset_id = ~S"
j@216 92 (dataset-id
j@216 93 dataset-identifier))
j@216 94 :flatp t
j@216 95 :field-names nil
j@216 96 :database database)))
j@216 97 (%make-amuse-dataset dataset-identifier (car dataset-header)
j@216 98 (%init-dataset-rows dataset-rows))))
j@216 99
j@216 100 (defun %init-dataset-rows (dataset-rows)
j@216 101 (loop for row in dataset-rows
j@216 102 collect (make-composition-identifier (find-package
j@216 103 (first row)) (second row))
j@216 104 into composition-identifiers
j@216 105 finally (return composition-identifiers)))