123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- class ChadoRecord {
-
- protected $table_name = '';
-
- protected $schema = [];
-
- protected $values = [];
-
-
- protected $required_cols = [];
-
-
- protected $record_id = NULL;
-
-
-
- protected $pkey = '';
-
-
-
- protected $column_names = [];
-
-
-
- public function __construct($table_name, $record_id = NULL) {
-
- if (!$table_name) {
- $message = t('ChadoRecord::_construct(). The $table_name argument is required for a ChadoRecord instance.');
- throw new Exception($message);
- }
-
-
- $this->table_name = $table_name;
- $this->schema = chado_get_schema($this->table_name);
- if (!$this->schema) {
- $message = t('ChadoRecord::_construct(). Could not find a matching table schema in Chado for the table: !table.',
- ['!table' => $this->table_name]);
- throw new Exception($message);
- }
-
-
-
- $this->pkey = $this->schema['primary key'][0];
-
-
- foreach ($this->schema['fields'] as $column_name => $col_details) {
- $this->column_names[] = $column_name;
- }
-
-
- foreach ($this->schema['fields'] as $column => $col_schema) {
- foreach ($col_schema as $param => $val) {
- if (preg_match('/not null/i', $param) and $col_schema[$param]) {
- $this->required_cols[] = $column;
- }
- }
- }
-
-
- if ($record_id) {
- try {
- $sql = 'SELECT * FROM {' . $this->table_name . '} WHERE ' . $this->pkey . ' = :record_id';
- $result = chado_query($sql, [':record_id' => $record_id]);
- $values = $result->fetchAssoc();
- if (empty($values)) {
- $message = t('ChadoRecord::_construct(). Could not find a record in table, !table, with the given !pkey: !record_id.',
- ['!pkey' => $this->pkey, '!record_id' => $record_id, '!table' => $this->table_name]);
- throw new Exception($message);
- }
- $this->record_id = $record_id;
- $this->values = $values;
- }
- catch (Exception $e) {
- $message = t('ChadoRecord::_construct(). Could not find a record in table, !table, with the given !pkey: !record_id. ERROR: !error',
- ['!pkey' => $this->pkey, '!record_id' => $record_id, '!table' => $this->table_name, '!error' => $e->getMessage()]);
- throw new Exception($message);
- }
- }
- }
-
-
- public function getID() {
- return $this->record_id;
- }
-
-
- public function getTable() {
- return $this->table_name;
- }
-
-
- public function getSchema() {
- return $this->schema;
- }
-
- public function save() {
-
-
- $num_matches = $this->find();
- if ($num_matches == 1) {
- $this->update();
- }
- if ($num_matches == 0) {
- $this->insert();
- }
- if ($num_matches > 1) {
- $message = t('ChadoRecord::save(). Could not save the record into the table, !table. '.
- 'Multiple records already exist that match the values: !values. '.
- 'Please provide a set of values that can uniquely identify a record.',
- ['!table' => $this->table_name, '!values' => print_r($this->values, TRUE), '!error' => $e->getMessage()]);
- throw new Exception($message);
- }
- }
-
- public function insert() {
-
-
- if (empty($this->values)) {
- $message = t('ChadoRecord::insert(). Could not insert a record into the table, !table, without any values.',
- ['!table' => $this->table_name]);
- throw new Exception($message);
- }
-
-
- $insert_cols = [];
- $insert_vals = [];
- $insert_args = [];
- foreach ($this->values as $column => $value) {
- $insert_cols[] = $column;
- $insert_vals[] = ':' . $column;
- $insert_args[':' . $column] = $value;
- }
- $sql = 'INSERT INTO {' . $this->table_name . '} (' .
- implode(", ", $insert_cols) . ') VALUES (' .
- implode(", ", $insert_vals) . ')';
- try {
- chado_query($sql, $insert_args);
-
-
-
- $this->find();
- }
- catch (Exception $e) {
- $message = t('ChadoRecord::insert(). Could not insert a record into the table, !table, with the following values: !values. ERROR: !error',
- ['!table' => $this->table_name, '!values' => print_r($this->values, TRUE), '!error' => $e->getMessage()]);
- throw new Exception($message);
- }
- }
-
-
- public function update() {
-
-
- if (empty($this->values)) {
- $message = t('ChadoRecord::update(). Could not update a record into the table, !table, without any values.',
- ['!table' => $this->table_name]);
- throw new Exception($message);
- }
-
-
- if (!$this->record_id) {
- $message = t('ChadoRecord::update(). Could not update a record in the table, !table, without a record ID.',
- ['!table' => $this->table_name]);
- throw new Exception($message);
- }
-
-
- $update_args = [];
- $sql = 'UPDATE {' . $this->table_name . '} SET ';
- foreach ($this->values as $column => $value) {
-
- if ($column == $this->pkey) {
- continue;
- }
- $sql .= $column . ' = :' . $column . ', ';
- $update_args[':' . $column] = $value;
- }
-
- $sql = substr($sql, 0, -2);
- $sql .= ' WHERE ' . $this->pkey . ' = :record_id';
- $update_args[':record_id'] = $this->record_id;
-
-
- try {
- chado_query($sql, $update_args);
- }
- catch (Exception $e) {
- $message = t('ChadoRecord::update(). Could not update a record in the table, !table, with !record_id as the record ID and the following values: !values. ERROR: !error',
- ['!table' => $this->table_name,
- '!record_id' => $this->record_id,
- '!values' => print_r($this->values, TRUE),
- '!error' => $e->getMessage()]);
- throw new Exception($message);
- }
- }
-
-
- public function delete() {
-
-
- if (!$this->record_id) {
- $message = t('ChadoRecord::delete(). Could not delete a record in the table, !table, without a record ID.',
- ['!table' => $this->table_name]);
- throw new Exception($message);
- }
-
- try {
- $sql = 'DELETE FROM {' . $this->table_name . '} WHERE ' . $this->pkey . ' = :record_id';
- chado_query($sql, [':record_id' => $this->record_id]);
- }
- catch (Exception $e) {
- $message = t('ChadoRecord::delete(). Could not delete a record in the table, !table, with !record_id as the record ID. ERROR: !error',
- ['!table' => $this->table_name,
- '!record_id' => $this->record_id,
- '!error' => $e->getMessage()]);
- throw new Exception($message);
- }
- }
-
- public function setValues($values) {
-
-
- $this->values = [];
-
-
- foreach ($values as $column => $value) {
- if (in_array($column, $this->column_names)) {
- $this->values[$column] = $value;
- }
- else {
- $message = t('ChadoRecord::setValues(). The column named, "!column", does not exist in table: "!table". Values: !values".',
- ['!column' => $column, '!table' => $this->table_name, '!values' => print_r($values, TRUE)]);
- throw new Exception($message);
- }
- }
-
-
-
- foreach ($this->required_cols as $rcol) {
-
-
- if ($rcol == $this->pkey) {
- continue;
- }
-
- if (in_array($rcol, array_keys($this->values)) and $this->values[$rcol] === '__NULL__') {
- $message = t('ChadoRecord::setValues(). The column named, "!column", requires a value for the table: "!table".',
- ['!column' => $rcol, '!table' => $this->table_name]);
- throw new Exception($message);
- }
- }
-
-
- if (in_array($this->pkey, array_keys($values))) {
- $this->record_id = $values[$this->pkey];
- }
- }
-
-
- public function getValues() {
- return $this->values;
- }
-
-
- public function setValue($column_name, $value) {
-
-
- if (!in_array($column_name, $this->column_names)) {
- $message = t('ChadoRecord::setValue(). The column named, "!column", does not exist in table: "!table".',
- ['!column' => $column_name, '!table' => $this->table_name]);
- throw new Exception($message);
- }
-
-
- if (!in_array($column_name, $this->required_cols) and $value == '__NULL__') {
- $message = t('ChadoRecord::setValue(). The column named, "!column", requires a value for the table: "!table".',
- ['!column' => $column_name, '!table' => $this->table_name]);
- throw new Exception($message);
- }
-
- $this->values[$column_name] = $value;
- }
-
- public function getValue($column_name) {
-
-
- if (!in_array($column_name, $this->column_names)) {
- $message = t('ChadoRecord::getValue(). The column named, "!column", does not exist in table: "!table".',
- ['!column' => $column_name, '!table' => $this->table_name]);
- throw new Exception($message);
- }
-
- return $this->values[$column_name];
- }
-
- public function find() {
-
-
- if (empty($this->values)) {
- $message = t('ChadoRecord::find(). Could not find a record from the table, !table, without any values.',
- ['!table' => $this->table_name]);
- throw new Exception($message);
- }
-
-
- $select_args = [];
- $sql = 'SELECT * FROM {' . $this->table_name . '} WHERE 1=1 ';
- foreach ($this->values as $column => $value) {
- $sql .= ' AND ' . $column . ' = :' . $column;
- $select_args[':' . $column] = $value;
- }
- try {
- $results = chado_query($sql, $select_args);
- }
- catch (Exception $e) {
- $message = t('ChadoRecord::find(). Could not find a record in the table, !table, with the following values: !values. ERROR: !error',
- ['!table' => $this->table_name, '!values' => print_r($this->values, TRUE), '!error' => $e->getMessage()]);
- throw new Exception($message);
- }
-
-
-
- $num_matches = $results->rowCount();
- if ($num_matches == 1) {
- $record = $results->fetchAssoc();
- $this->values = [];
- foreach ($record as $column => $value) {
- $this->values[$column] = $value;
- }
- $this->record_id = $record[$this->pkey];
- }
-
-
- return $num_matches;
- }
- }
|