calculate length of integer columns as bytes #733

Merged
merged 1 commit into from Nov 7, 2011

Conversation

Projects
None yet
3 participants
Contributor

jnimety commented Nov 2, 2011

see discussion in issue #728

@jnimety jnimety calculate length of integer columns as bytes
Signed-off-by: Joel Nimety <jnimety@continuity.net>
9ead0e9
Owner

justinfrench commented Nov 2, 2011

Thanks! This is out of my depth a bit, can we get a +1 from someone?

Collaborator

sobrinho commented Nov 3, 2011

Note we have an "issue" with signed columns.

If the column is unsigned with 8 bytes, the formula matches:

2 ** ( 8 * 8 )
#=> 18446744073709551616

But if the column is signed, this will be wrong because the value minimum value will be -9223372036854775808 and maximum value will be 9223372036854775808.

The only issue in this case is that, the size attribute will be 21 instead of 20 but I guess it's not a real problem.

Another question is this + 1.

The size attribute should not be the real size of string?

( 2 ** ( 2 * 8 ) )
#=> 65536
( 2 ** ( 2 * 8 ) ).to_s.length
#=> 5
( 2 ** ( 2 * 8 ) ).to_s.length + 1
#=> 6
Collaborator

sobrinho commented Nov 3, 2011

ok, the unsigned will not be a problem because user may want to type -300 on field.

the only question is the + 1 now :)

Owner

justinfrench commented Nov 6, 2011

@sobrinho, I think it needs to be +1, because it should be the maximum length of a valid string that can be entered.

Collaborator

sobrinho commented Nov 6, 2011

@justinfrench, note that:

2 ** ( 2 * 8 )
#=> 65536

A column using limit 2, the max value will be 65536.

The string limit will be 5 but this formula returns 6:

( 2 ** ( 2 * 8 ) ).to_s.length + 1
#=> 6

That allows user to type 965536 for example :)

Contributor

jnimety commented Nov 6, 2011

We need to keep in mind that imposing a character limit is imperfect anyway. The max value for a signed integer column is 2147483647 (10 characters) and the min value is -2147483648 (11 characters), see http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html. The max and min values are not 9999999999 and -9999999999, and both of these would pass the maxlength character limit restriction.

Perhaps setting max/min instead of maxlength is more appropriate for numeric inputs, but I don't know if it has sufficient browser or standards support. http://www.w3schools.com/html5/att_input_max.asp

Owner

justinfrench commented Nov 6, 2011

@sobrino happy for you to merge this in when you're happy

Collaborator

sobrinho commented Nov 7, 2011

@jnimety you're right.

@justinfrench it's ok for me!

@justinfrench justinfrench added a commit that referenced this pull request Nov 7, 2011

@justinfrench justinfrench Merge pull request #733 from ContinuityControl/bugfix-integer-maxleng…
…th-as-bytes

calculate length of integer columns as bytes
1f3746d

@justinfrench justinfrench merged commit 1f3746d into justinfrench:master Nov 7, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment