This topic was marked solved by Nao, on August 28th, 2013, 04:22 PM

live627

  • Should five per cent appear too small / Be thankful I don't take it all / 'Cause I'm the taxman, yeah I'm the taxman
  • Posts: 1,670
DB column size cannnot be comma seperated
« on August 23rd, 2013, 04:13 PM »
I need to specify my own size fur floats (18,15) and cannot rely on the defaults (10,2).

Code: [Select]
/Sources/Class-DBPackages.php (working copy)
@@ -88,8 +88,16 @@
  $default = '';
 
  // Sort out the size... and stuff...
- $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
+ if (isset($column['size']))
+ {
+ $s = array_filter(explode(',', $column['size']), 'is_numeric');
 
+ if (!empty($s))
+ $column['size'] = implode(',', $s);
+ else
+ $column['size'] = null;
+ }
+
  // Allow unsigned integers
  $unsigned = in_array($column['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';
 
@@ -141,8 +149,16 @@
  if (!isset($current_columns[$column['name']]))
  {
  // The column is new, add it to the list of columns to be added
- $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
+ if (isset($column['size']))
+ {
+ $s = array_filter(explode(',', $column['size']), 'is_numeric');
 
+ if (!empty($s))
+ $column['size'] = implode(',', $s);
+ else
+ $column['size'] = null;
+ }
+
  // Allow unsigned integers
  $unsigned = in_array($column['type'], $numeric_types) && !empty($column['unsigned']) ? 'unsigned ' : '';
 
@@ -160,8 +176,16 @@
  else
  {
  // The column already exists, does it need changing?
- $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
+ if (isset($column['size']))
+ {
+ $s = array_filter(explode(',', $column['size']), 'is_numeric');
 
+ if (!empty($s))
+ $column['size'] = implode(',', $s);
+ else
+ $column['size'] = null;
+ }
+
  // Allow unsigned integers
  $unsigned = in_array($column['type'], $numeric_types) && !empty($column['unsigned']) ? 'unsigned ' : '';

The above code does not break anything as per my tests.

Before, only numeric type sizes were allowed. Now, the same is allowed within any number of commas.
A confident man keeps quiet.whereas a frightened man keeps talking, hiding his fear.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: DB column size cannnot be comma sepeerated
« Reply #1, on August 23rd, 2013, 04:16 PM »
There's a reason that such things are excluded.

What are you doing with floats of that precision? Are you doing maths on them? Do you worry about loss of precision?
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

live627

  • Should five per cent appear too small / Be thankful I don't take it all / 'Cause I'm the taxman, yeah I'm the taxman
  • Posts: 1,670
Re: DB column size cannnot be comma sepeerated
« Reply #2, on August 23rd, 2013, 04:23 PM »
I'm storing latitude and longitude of pins on a Google map. Needs precision, as you may imagine. Also, not doing any math, other than looking for exact matches.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: DB column size cannnot be comma seperated
« Reply #3, on August 23rd, 2013, 04:26 PM »
Then use a string column.

ETA: As a general rule, only store numbers when you need them to be numbers because you're doing work on them in some fashion. For example counts of things, or numeric ids (because they are still a mathematical concept at that point)

When I built it for Crossing Overland, that's exactly what I did, stored them as varchars. There's no risk of loss of accuracy.