Put constants into RowEditor class.

This commit is contained in:
Markus Birth 2015-02-13 00:32:24 +01:00
parent ea3179d144
commit d7544a0321
7 changed files with 81 additions and 83 deletions

View File

@ -15,6 +15,20 @@ namespace OpencachingDE\Data;
class RowEditor
{
const TYPE_INT = 1;
const TYPE_STRING = 2;
const TYPE_BOOLEAN = 3;
const TYPE_DATE = 4;
const TYPE_FLOAT = 5;
const TYPE_DOUBLE = 6;
const INSERT_NOTHING = 0; //
const INSERT_OVERWRITE = 1; // ignore given values and use function
const INSERT_IGNORE = 2; // dont use this column on insert
const INSERT_AUTOINCREMENT = 4; // column is an auto increment column
const INSERT_AUTOUUID = 8; // if empty, UUID is generated by before insert trigger (not supported for primary key fields)
const INSERT_NOW = 16; // NOW()
private $sTable;
private $sAutoIncrementField = null;
private $pk; // (idx:name; type, default, nullable, value, insertfunction)
@ -34,7 +48,7 @@ class RowEditor
$this->sTable = $sTable;
}
private function addPK($sField, $type, $default, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
private function addPK($sField, $type, $default, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->pk[$sField] = array(
'type' => $type,
@ -45,45 +59,45 @@ class RowEditor
);
}
public function addPKInt($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addPKInt($sField, $nDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addPK($sField, RE_TYPE_INT, $nDefault, $bNullable, $nInsertFunction);
$this->addPK($sField, static::TYPE_INT, $nDefault, $bNullable, $nInsertFunction);
if (($nInsertFunction & RE_INSERT_AUTOINCREMENT) == RE_INSERT_AUTOINCREMENT) {
if (($nInsertFunction & static::INSERT_AUTOINCREMENT) == static::INSERT_AUTOINCREMENT) {
$this->sAutoIncrementField = $sField;
}
}
public function addPKFloat($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addPKFloat($sField, $nDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addPK($sField, RE_TYPE_FLOAT, $nDefault, $bNullable, $nInsertFunction);
$this->addPK($sField, static::TYPE_FLOAT, $nDefault, $bNullable, $nInsertFunction);
}
public function addPKDouble($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addPKDouble($sField, $nDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addPK($sField, RE_TYPE_DOUBLE, $nDefault, $bNullable, $nInsertFunction);
$this->addPK($sField, static::TYPE_DOUBLE, $nDefault, $bNullable, $nInsertFunction);
}
public function addPKString($sField, $sDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addPKString($sField, $sDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
if (($nInsertFunction & RE_INSERT_AUTOUUID) == RE_INSERT_AUTOUUID) {
die('RowEditor: RE_INSERT_AUTOUUID not supported for primary key fields');
if (($nInsertFunction & static::INSERT_AUTOUUID) == static::INSERT_AUTOUUID) {
die('RowEditor: RowEditor::INSERT_AUTOUUID not supported for primary key fields');
}
$this->addPK($sField, RE_TYPE_STRING, $sDefault, $bNullable, $nInsertFunction);
$this->addPK($sField, static::TYPE_STRING, $sDefault, $bNullable, $nInsertFunction);
}
public function addPKBoolean($sField, $bDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addPKBoolean($sField, $bDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addPK($sField, RE_TYPE_BOOLEAN, $bDefault, $bNullable, $nInsertFunction);
$this->addPK($sField, static::TYPE_BOOLEAN, $bDefault, $bNullable, $nInsertFunction);
}
public function addPKDate($sField, $dDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addPKDate($sField, $dDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addPK($sField, RE_TYPE_DATE, $dDefault, $bNullable, $nInsertFunction);
$this->addPK($sField, static::TYPE_DATE, $dDefault, $bNullable, $nInsertFunction);
}
private function addField($sField, $type, $default, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
private function addField($sField, $type, $default, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->fields[$sField] = array(
'type' => $type,
@ -95,34 +109,34 @@ class RowEditor
);
}
public function addInt($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addInt($sField, $nDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addField($sField, RE_TYPE_INT, $nDefault, $bNullable, $nInsertFunction);
$this->addField($sField, static::TYPE_INT, $nDefault, $bNullable, $nInsertFunction);
}
public function addFloat($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addFloat($sField, $nDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addField($sField, RE_TYPE_FLOAT, $nDefault, $bNullable, $nInsertFunction);
$this->addField($sField, static::TYPE_FLOAT, $nDefault, $bNullable, $nInsertFunction);
}
public function addDouble($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addDouble($sField, $nDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addField($sField, RE_TYPE_DOUBLE, $nDefault, $bNullable, $nInsertFunction);
$this->addField($sField, static::TYPE_DOUBLE, $nDefault, $bNullable, $nInsertFunction);
}
public function addString($sField, $sDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addString($sField, $sDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addField($sField, RE_TYPE_STRING, $sDefault, $bNullable, $nInsertFunction);
$this->addField($sField, static::TYPE_STRING, $sDefault, $bNullable, $nInsertFunction);
}
public function addBoolean($sField, $bDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addBoolean($sField, $bDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addField($sField, RE_TYPE_BOOLEAN, $bDefault, $bNullable, $nInsertFunction);
$this->addField($sField, static::TYPE_BOOLEAN, $bDefault, $bNullable, $nInsertFunction);
}
public function addDate($sField, $dDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING)
public function addDate($sField, $dDefault, $bNullable, $nInsertFunction = RowEditor::INSERT_NOTHING)
{
$this->addField($sField, RE_TYPE_DATE, $dDefault, $bNullable, $nInsertFunction);
$this->addField($sField, static::TYPE_DATE, $dDefault, $bNullable, $nInsertFunction);
}
public function removePK($sField)
@ -225,20 +239,20 @@ class RowEditor
}
switch ($type) {
case RE_TYPE_INT:
case static::TYPE_INT:
$value = (int)$value+0;
break;
case RE_TYPE_FLOAT:
case RE_TYPE_DOUBLE:
case static::TYPE_FLOAT:
case static::TYPE_DOUBLE:
$value = $value+0.0;
break;
case RE_TYPE_BOOLEAN:
case static::TYPE_BOOLEAN:
$value = (($value+0) != 0);
break;
case RE_TYPE_DATE:
case static::TYPE_DATE:
if (!is_numeric($value)) {
$value = strtotime($value);
}
@ -251,20 +265,20 @@ class RowEditor
public function pFormatValueSql($type, $value)
{
switch ($type) {
case RE_TYPE_INT:
case static::TYPE_INT:
$value = (int)$value+0;
break;
case RE_TYPE_FLOAT:
case RE_TYPE_DOUBLE:
case static::TYPE_FLOAT:
case static::TYPE_DOUBLE:
$value = $value+0.0;
break;
case RE_TYPE_BOOLEAN:
case static::TYPE_BOOLEAN:
$value = (($value+0) != 0) ? 1 : 0;
break;
case RE_TYPE_DATE:
case static::TYPE_DATE:
if (!is_numeric($value)) {
$value = strtotime($value);
}
@ -339,7 +353,7 @@ class RowEditor
}
$sFormatedValue = $this->pFormatValue($this->fields[$sField]['type'], $sValue);
if ($this->fields[$sField]['type'] == RE_TYPE_FLOAT) {
if ($this->fields[$sField]['type'] == static::TYPE_FLOAT) {
// Direct float comparison is deprecated and can result in last-digit errors.
// Floats in OC database are only used for reasonably large numbers like coordinates,
// waylengths and time estimates, so using a fixed epsilon threshold is safe:
@ -439,14 +453,14 @@ class RowEditor
continue;
}
if (($field['insertfunction'] & RE_INSERT_IGNORE) == RE_INSERT_IGNORE) {
if (($field['insertfunction'] & static::INSERT_IGNORE) == static::INSERT_IGNORE) {
continue;
}
$sFields[] = '`' . sql_escape($k) . '`';
if ((($field['insertfunction'] & RE_INSERT_OVERWRITE) == RE_INSERT_OVERWRITE) || (($field['changed'] == false) && ($field['insertfunction'] != RE_INSERT_NOTHING))) {
if (($field['insertfunction'] & RE_INSERT_NOW) == RE_INSERT_NOW) {
if ((($field['insertfunction'] & static::INSERT_OVERWRITE) == static::INSERT_OVERWRITE) || (($field['changed'] == false) && ($field['insertfunction'] != static::INSERT_NOTHING))) {
if (($field['insertfunction'] & static::INSERT_NOW) == static::INSERT_NOW) {
$sValues[] = 'NOW()';
} else {
$sValues[] = 'NULL';
@ -461,14 +475,14 @@ class RowEditor
}
foreach ($this->fields as $k => $field) {
if (($field['insertfunction'] & RE_INSERT_IGNORE) == RE_INSERT_IGNORE) {
if (($field['insertfunction'] & static::INSERT_IGNORE) == static::INSERT_IGNORE) {
continue;
}
$sFields[] = '`' . sql_escape($k) . '`';
if ((($field['insertfunction'] & RE_INSERT_OVERWRITE) == RE_INSERT_OVERWRITE) || (($field['changed'] == false) && ($field['insertfunction'] != RE_INSERT_NOTHING))) {
if (($field['insertfunction'] & RE_INSERT_NOW) == RE_INSERT_NOW) {
if ((($field['insertfunction'] & static::INSERT_OVERWRITE) == static::INSERT_OVERWRITE) || (($field['changed'] == false) && ($field['insertfunction'] != static::INSERT_NOTHING))) {
if (($field['insertfunction'] & static::INSERT_NOW) == static::INSERT_NOW) {
$sValues[] = 'NOW()';
} else {
$sValues[] = 'NULL';
@ -549,4 +563,3 @@ class RowEditor
return true;
}
}

View File

@ -82,12 +82,12 @@ class cache
function __construct($nNewCacheId=ID_NEW)
{
$this->reCache = new RowEditor('caches');
$this->reCache->addPKInt('cache_id', null, false, RE_INSERT_AUTOINCREMENT);
$this->reCache->addString('uuid', '', false, RE_INSERT_AUTOUUID);
$this->reCache->addPKInt('cache_id', null, false, RowEditor::INSERT_AUTOINCREMENT);
$this->reCache->addString('uuid', '', false, RowEditor::INSERT_AUTOUUID);
$this->reCache->addInt('node', 0, false);
$this->reCache->addDate('date_created', time(), true, RE_INSERT_IGNORE);
$this->reCache->addDate('last_modified', time(), true, RE_INSERT_IGNORE);
$this->reCache->addDate('listing_last_modified', time(), true, RE_INSERT_IGNORE);
$this->reCache->addDate('date_created', time(), true, RowEditor::INSERT_IGNORE);
$this->reCache->addDate('last_modified', time(), true, RowEditor::INSERT_IGNORE);
$this->reCache->addDate('listing_last_modified', time(), true, RowEditor::INSERT_IGNORE);
$this->reCache->addInt('user_id', 0, false);
$this->reCache->addString('name', '', false);
$this->reCache->addDouble('longitude', 0, false);
@ -105,10 +105,10 @@ class cache
$this->reCache->addString('wp_oc', null, true);
$this->reCache->addString('wp_gc', '', false);
$this->reCache->addString('wp_nc', '', false);
$this->reCache->addString('desc_languages', '', false, RE_INSERT_IGNORE);
$this->reCache->addString('desc_languages', '', false, RowEditor::INSERT_IGNORE);
$this->reCache->addString('default_desclang', '', false);
$this->reCache->addDate('date_activate', null, true);
$this->reCache->addInt('need_npa_recalc', 1, false, RE_INSERT_IGNORE);
$this->reCache->addInt('need_npa_recalc', 1, false, RowEditor::INSERT_IGNORE);
$this->nCacheId = $nNewCacheId+0;

View File

@ -82,11 +82,11 @@ class cachelog
function __construct($nNewLogId=ID_NEW)
{
$this->reCacheLog = new RowEditor('cache_logs');
$this->reCacheLog->addPKInt('id', null, false, RE_INSERT_AUTOINCREMENT);
$this->reCacheLog->addString('uuid', '', false, RE_INSERT_AUTOUUID);
$this->reCacheLog->addPKInt('id', null, false, RowEditor::INSERT_AUTOINCREMENT);
$this->reCacheLog->addString('uuid', '', false, RowEditor::INSERT_AUTOUUID);
$this->reCacheLog->addInt('node', 0, false);
$this->reCacheLog->addDate('date_created', time(), true, RE_INSERT_IGNORE);
$this->reCacheLog->addDate('last_modified', time(), true, RE_INSERT_IGNORE);
$this->reCacheLog->addDate('date_created', time(), true, RowEditor::INSERT_IGNORE);
$this->reCacheLog->addDate('last_modified', time(), true, RowEditor::INSERT_IGNORE);
$this->reCacheLog->addInt('cache_id', 0, false);
$this->reCacheLog->addInt('user_id', 0, false);
$this->reCacheLog->addInt('type', 0, false);

View File

@ -9,20 +9,6 @@
define('ID_NEW', -1);
define('RE_TYPE_INT', 1);
define('RE_TYPE_STRING', 2);
define('RE_TYPE_BOOLEAN', 3);
define('RE_TYPE_DATE', 4);
define('RE_TYPE_FLOAT', 5);
define('RE_TYPE_DOUBLE', 6);
define('RE_INSERT_NOTHING', 0); //
define('RE_INSERT_OVERWRITE', 1); // ignore given values and use function
define('RE_INSERT_IGNORE', 2); // dont use this column on insert
define('RE_INSERT_AUTOINCREMENT', 4); // column is an auto increment column
define('RE_INSERT_AUTOUUID', 8); // if empty, UUID is generated by before insert trigger (not supported for primary key fields)
define('RE_INSERT_NOW', 16); // NOW()
define('REGEX_USERNAME', '^[a-zA-Z0-9\.\-_@äüöÄÜÖ=)(\/\\\&*+~#][a-zA-Z0-9\.\-_ @äüöÄÜÖ=)(\/\\\&*+~#]{1,58}[a-zA-Z0-9\.\-_@äüöÄÜÖ=)(\/\\\&*+~#]$'); // if chars changed, ajust webchat.php // // min. 4 -> 3 chars -- following 2012-8-6
define('REGEX_PASSWORD', '^[a-zA-Z0-9\.\-_ @äüöÄÜÖ=)(\/\\\&*+~#]{3,60}$');
define('REGEX_LAST_NAME', '^[a-zA-Z][a-zA-Z0-9\.\- äüöÄÜÖ]{1,59}$');
@ -87,4 +73,3 @@
define('PICTURE_QUALITY',85);
define('PICTURE_RESOLUTION',72);
define('PICTURE_MAX_LONG_SIDE',1024);
?>

View File

@ -39,11 +39,11 @@ class picture
global $opt;
$this->rePicture = new RowEditor('pictures');
$this->rePicture->addPKInt('id', null, false, RE_INSERT_AUTOINCREMENT);
$this->rePicture->addString('uuid', '', false, RE_INSERT_AUTOUUID);
$this->rePicture->addPKInt('id', null, false, RowEditor::INSERT_AUTOINCREMENT);
$this->rePicture->addString('uuid', '', false, RowEditor::INSERT_AUTOUUID);
$this->rePicture->addInt('node', 0, false);
$this->rePicture->addDate('date_created', time(), true, RE_INSERT_IGNORE);
$this->rePicture->addDate('last_modified', time(), true, RE_INSERT_IGNORE);
$this->rePicture->addDate('date_created', time(), true, RowEditor::INSERT_IGNORE);
$this->rePicture->addDate('last_modified', time(), true, RowEditor::INSERT_IGNORE);
$this->rePicture->addString('url', '', false);
$this->rePicture->addString('title', '', false);
$this->rePicture->addDate('last_url_check', 0, true);

View File

@ -16,7 +16,7 @@ class statpic
function __construct($nNewUserId)
{
$this->reUser = new RowEditor('user');
$this->reUser->addPKInt('user_id', null, false, RE_INSERT_AUTOINCREMENT);
$this->reUser->addPKInt('user_id', null, false, RowEditor::INSERT_AUTOINCREMENT);
$this->reUser->addString('statpic_text', '', false);
$this->reUser->addString('statpic_logo', 0, false);

View File

@ -40,7 +40,7 @@ class user
global $opt;
$this->reUser = new RowEditor('user');
$this->reUser->addPKInt('user_id', null, false, RE_INSERT_AUTOINCREMENT);
$this->reUser->addPKInt('user_id', null, false, RowEditor::INSERT_AUTOINCREMENT);
$this->reUser->addString('username', '', false);
$this->reUser->addString('password', null, true);
$this->reUser->addString('email', null, true);
@ -49,7 +49,7 @@ class user
$this->reUser->addInt('mailing_problems', 0, false);
$this->reUser->addFloat('latitude', 0, false);
$this->reUser->addFloat('longitude', 0, false);
$this->reUser->addDate('last_modified', time(), true, RE_INSERT_IGNORE);
$this->reUser->addDate('last_modified', time(), true, RowEditor::INSERT_IGNORE);
$this->reUser->addBoolean('is_active_flag', false, false);
$this->reUser->addString('last_name', '', false);
$this->reUser->addString('first_name', '', false);
@ -58,11 +58,11 @@ class user
$this->reUser->addBoolean('pmr_flag', false, false);
$this->reUser->addString('new_pw_code', null, true);
$this->reUser->addDate('new_pw_date', null, true);
$this->reUser->addDate('date_created', time(), true, RE_INSERT_IGNORE);
$this->reUser->addDate('date_created', time(), true, RowEditor::INSERT_IGNORE);
$this->reUser->addString('new_email_code', null, true);
$this->reUser->addDate('new_email_date', null, true);
$this->reUser->addString('new_email', null, true);
$this->reUser->addString('uuid', '', false, RE_INSERT_AUTOUUID);
$this->reUser->addString('uuid', '', false, RowEditor::INSERT_AUTOUUID);
$this->reUser->addBoolean('permanent_login_flag', false, false);
$this->reUser->addInt('watchmail_mode', 1, false);
$this->reUser->addInt('watchmail_hour', 0, false);
@ -77,7 +77,7 @@ class user
$this->reUser->addInt('node', 0, false);
$this->reUserStat = new RowEditor('stat_user');
$this->reUserStat->addPKInt('user_id', null, false, RE_INSERT_AUTOINCREMENT);
$this->reUserStat->addPKInt('user_id', null, false, RowEditor::INSERT_AUTOINCREMENT);
$this->reUserStat->addInt('found', 0, false);
$this->reUserStat->addInt('notfound', 0, false);
$this->reUserStat->addInt('note', 0, false);