annotate base/database/datasets-functions.lisp @ 330:2fbff655ba47 tip

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