summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
Diffstat (limited to 'classes')
-rw-r--r--classes/Managed_DataObject.php155
-rw-r--r--classes/Schema_version.php22
-rw-r--r--classes/statusnet.ini8
3 files changed, 185 insertions, 0 deletions
diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php
new file mode 100644
index 000000000..35d52c512
--- /dev/null
+++ b/classes/Managed_DataObject.php
@@ -0,0 +1,155 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Wrapper for Memcached_DataObject which knows its own schema definition.
+ * Builds its own damn settings from a schema definition.
+ *
+ * @author Brion Vibber <brion@status.net>
+ */
+abstract class Managed_DataObject extends Memcached_DataObject
+{
+ /**
+ * The One True Thingy that must be defined and declared.
+ */
+ public static abstract function schemaDef();
+
+ /**
+ * get/set an associative array of table columns
+ *
+ * @access public
+ * @return array (associative)
+ */
+ function table()
+ {
+ // Hack for PHP 5.2 not supporting late static binding
+ //$table = static::schemaDef();
+ $table = call_user_func(array(get_class($this), 'schemaDef'));
+ return array_map(array($this, 'columnBitmap'), $table['fields']);
+ }
+
+ /**
+ * get/set an array of table primary keys
+ *
+ * Key info is pulled from the table definition array.
+ *
+ * @access private
+ * @return array
+ */
+ function keys()
+ {
+ return array_keys($this->keyTypes());
+ }
+
+ /**
+ * Get a sequence key
+ *
+ * Returns the first serial column defined in the table, if any.
+ *
+ * @access private
+ * @return array (column,use_native,sequence_name)
+ */
+
+ function sequenceKey()
+ {
+ $table = self::schemaDef();
+ foreach ($table['fields'] as $name => $column) {
+ if ($column['type'] == 'serial') {
+ // We have a serial/autoincrement column.
+ // Declare it to be a native sequence!
+ return array($name, true, false);
+ }
+ }
+
+ // No sequence key on this table.
+ return array(false, false, false);
+ }
+
+ /**
+ * Return key definitions for DB_DataObject and Memcache_DataObject.
+ *
+ * DB_DataObject needs to know about keys that the table has; this function
+ * defines them.
+ *
+ * @return array key definitions
+ */
+
+ function keyTypes()
+ {
+ $keys = array();
+ $table = self::schemaDef();
+
+ if (!empty($table['unique keys'])) {
+ foreach ($table['unique keys'] as $idx => $fields) {
+ foreach ($fields as $name) {
+ $keys[$name] = 'U';
+ }
+ }
+ }
+
+ if (!empty($table['primary key'])) {
+ foreach ($table['primary key'] as $name) {
+ $keys[$name] = 'K';
+ }
+ }
+ return $keys;
+ }
+
+ /**
+ * Build the appropriate DB_DataObject bitfield map for this field.
+ *
+ * @param array $column
+ * @return int
+ */
+ function columnBitmap($column)
+ {
+ $type = $column['type'];
+
+ // For quoting style...
+ $intTypes = array('int',
+ 'integer',
+ 'float',
+ 'serial',
+ 'numeric');
+ if (in_array($type, $intTypes)) {
+ $style = DB_DATAOBJECT_INT;
+ } else {
+ $style = DB_DATAOBJECT_STR;
+ }
+
+ // Data type formatting style...
+ $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
+ 'text' => DB_DATAOBJECT_TXT,
+ 'date' => DB_DATAOBJECT_DATE,
+ 'time' => DB_DATAOBJECT_TIME,
+ 'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
+ 'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
+
+ if (isset($formatStyles[$type])) {
+ $style |= $formatStyles[$type];
+ }
+
+ // Nullable?
+ if (!empty($column['not null'])) {
+ $style |= DB_DATAOBJECT_NOTNULL;
+ }
+
+ return $style;
+ }
+} \ No newline at end of file
diff --git a/classes/Schema_version.php b/classes/Schema_version.php
new file mode 100644
index 000000000..6b464c6d1
--- /dev/null
+++ b/classes/Schema_version.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Table Definition for schema_version
+ */
+
+class Schema_version extends Memcached_DataObject
+{
+ ###START_AUTOCODE
+ /* the code below is auto generated do not remove the above tag */
+
+ public $__table = 'schema_version'; // table name
+ public $table_name; // varchar(64) primary_key not_null
+ public $checksum; // varchar(64) not_null
+ public $modified; // datetime() not_null
+
+ /* Static get */
+ function staticGet($k,$v=null)
+ { return Memcached_DataObject::staticGet('Schema_version',$k,$v); }
+
+ /* the code above is auto generated do not remove the tag below */
+ ###END_AUTOCODE
+}
diff --git a/classes/statusnet.ini b/classes/statusnet.ini
index 2888bab56..d1d2980fd 100644
--- a/classes/statusnet.ini
+++ b/classes/statusnet.ini
@@ -509,6 +509,14 @@ replied_id = 1
notice_id = K
profile_id = K
+[schema_version]
+table_name = 130
+checksum = 130
+modified = 384
+
+[schema_version__keys]
+table_name = K
+
[session]
id = 130
session_data = 34