j@287: (cl:in-package #:amuse-database-admin) j@287: j@287: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; j@287: ;;; Constructors j@287: j@287: (defun make-amuse-dataset-identifier (dataset-id) j@287: (make-instance 'amuse-dataset-identifier j@287: :dataset-id dataset-id)) j@287: j@287: (defun %make-amuse-dataset (dataset-identifier description j@287: composition-identifiers) j@287: (make-instance 'amuse-dataset j@287: :%data composition-identifiers j@287: :identifier dataset-identifier j@287: :description description)) j@287: j@287: j@287: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; j@287: ;;; Database functions j@287: j@287: (defun make-new-dataset (description &optional (database j@287: *amuse-database*)) j@287: "A dataset is a set of pieces used for a particualar analytical j@287: task. A dataset is not necessarily the same thing as a corpus or j@287: collection (are these things different?). Corpus indicates that a set j@287: of pieces have been curated in some way and in that sense `belong j@287: together'. A dataset is just simply a set of pieces gathered together j@287: to analyse, and the pieces can be from any corpus or backend." j@287: (let (dataset-id) j@287: #.(clsql:locally-enable-sql-reader-syntax) j@287: (clsql:insert-records :into "amuse_datasets" j@287: :attributes '([description]) j@287: :values (list description) j@287: :database database) j@287: #.(clsql:locally-disable-sql-reader-syntax) j@287: (setf dataset-id (clsql-mysql::mysql-insert-id j@287: (clsql-mysql::database-mysql-ptr j@287: database))) j@287: (make-amuse-dataset-identifier dataset-id))) j@287: j@287: (defun assign-composition-to-dataset (composition-identifier j@287: dataset-identifier j@287: &optional (database j@287: *amuse-database*)) j@287: (clsql:execute-command (format nil " j@287: INSERT INTO amuse_datasets_join j@287: SET dataset_id := ~S, j@287: implementation_id := (SELECT get_impl_id('~A')), j@287: composition_id := ~S;" j@287: (dataset-id dataset-identifier) j@287: (implementation-namestring j@287: composition-identifier) j@287: (composition-id j@287: composition-identifier))) j@287: :database database) j@287: j@289: (defun assign-compositions-to-dataset (composition-identifiers j@289: dataset-identifier j@289: &optional (database j@289: *amuse-database*)) j@289: "This should be done more sensibly." j@289: (loop for identifier in composition-identifiers j@289: do (assign-composition-to-dataset identifier j@289: dataset-identifier j@289: database))) j@289: j@295: (defun make-composition-identifiers-from-file (package pathname) j@295: "This reads a file that contains one id per line, and returns a list j@295: of composition-identifiers (specialised on package). It is useful for j@295: reading files that have been exported from the database, for use with j@295: the above functions." j@295: (with-open-file (stream pathname :direction :input) j@295: (loop for id = (read stream nil) j@295: while id j@295: collect (make-composition-identifier package id)))) j@295: j@287: (defun get-dataset (dataset-identifier &optional (database j@287: *amuse-database*)) j@287: (let ((dataset-header (clsql:query (format nil " j@287: SELECT description j@287: FROM amuse_datasets j@287: WHERE dataset_id = ~S" (dataset-id dataset-identifier)) j@287: :database database j@287: :flatp t j@287: :field-names nil)) j@287: (dataset-rows (clsql:query (format nil " j@287: SELECT implementation_name, composition_id j@287: FROM amuse_datasets_join j@287: LEFT JOIN amuse_implementations j@287: USING (implementation_id) j@287: WHERE dataset_id = ~S" j@287: (dataset-id j@287: dataset-identifier)) j@287: :flatp t j@287: :field-names nil j@287: :database database))) j@287: (%make-amuse-dataset dataset-identifier (car dataset-header) j@287: (%init-dataset-rows dataset-rows)))) j@287: j@287: (defun %init-dataset-rows (dataset-rows) j@287: (loop for row in dataset-rows j@287: collect (make-composition-identifier (find-package j@287: (first row)) (second row)) j@287: into composition-identifiers j@287: finally (return composition-identifiers)))