📄 ndbsqlutil.cpp
字号:
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <NdbSqlUtil.hpp>#include <NdbOut.hpp>#include <my_sys.h>/* * Data types. The entries must be in the numerical order. */const NdbSqlUtil::TypeNdbSqlUtil::m_typeList[] = { { // 0 Type::Undefined, NULL, NULL }, { // 1 Type::Tinyint, cmpTinyint, NULL }, { // 2 Type::Tinyunsigned, cmpTinyunsigned, NULL }, { // 3 Type::Smallint, cmpSmallint, NULL }, { // 4 Type::Smallunsigned, cmpSmallunsigned, NULL }, { // 5 Type::Mediumint, cmpMediumint, NULL }, { // 6 Type::Mediumunsigned, cmpMediumunsigned, NULL }, { // 7 Type::Int, cmpInt, NULL }, { // 8 Type::Unsigned, cmpUnsigned, NULL }, { // 9 Type::Bigint, cmpBigint, NULL }, { // 10 Type::Bigunsigned, cmpBigunsigned, NULL }, { // 11 Type::Float, cmpFloat, NULL }, { // 12 Type::Double, cmpDouble, NULL }, { // 13 Type::Olddecimal, cmpOlddecimal, NULL }, { // 14 Type::Char, cmpChar, likeChar }, { // 15 Type::Varchar, cmpVarchar, likeVarchar }, { // 16 Type::Binary, cmpBinary, likeBinary }, { // 17 Type::Varbinary, cmpVarbinary, likeVarbinary }, { // 18 Type::Datetime, cmpDatetime, NULL }, { // 19 Type::Date, cmpDate, NULL }, { // 20 Type::Blob, NULL, NULL }, { // 21 Type::Text, NULL, NULL }, { // 22 Type::Bit, NULL, NULL }, { // 23 Type::Longvarchar, cmpLongvarchar, likeLongvarchar }, { // 24 Type::Longvarbinary, cmpLongvarbinary, likeLongvarbinary }, { // 25 Type::Time, cmpTime, NULL }, { // 26 Type::Year, cmpYear, NULL }, { // 27 Type::Timestamp, cmpTimestamp, NULL }, { // 28 Type::Olddecimalunsigned, cmpOlddecimalunsigned, NULL }, { // 29 Type::Decimal, cmpDecimal, NULL }, { // 30 Type::Decimalunsigned, cmpDecimalunsigned, NULL }};const NdbSqlUtil::Type&NdbSqlUtil::getType(Uint32 typeId){ if (typeId < sizeof(m_typeList) / sizeof(m_typeList[0]) && m_typeList[typeId].m_typeId != Type::Undefined) { return m_typeList[typeId]; } return m_typeList[Type::Undefined];}const NdbSqlUtil::Type&NdbSqlUtil::getTypeBinary(Uint32 typeId){ switch (typeId) { case Type::Char: case Type::Varchar: case Type::Binary: case Type::Varbinary: case Type::Longvarchar: case Type::Longvarbinary: typeId = Type::Binary; break; case Type::Text: typeId = Type::Blob; break; default: break; } return getType(typeId);}/* * Comparison functions. */intNdbSqlUtil::cmpTinyint(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Int8)) { Int8 v1, v2; memcpy(&v1, p1, sizeof(Int8)); memcpy(&v2, p2, sizeof(Int8)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpTinyunsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Uint8)) { Uint8 v1, v2; memcpy(&v1, p1, sizeof(Uint8)); memcpy(&v2, p2, sizeof(Uint8)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpSmallint(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Int16)) { Int16 v1, v2; memcpy(&v1, p1, sizeof(Int16)); memcpy(&v2, p2, sizeof(Int16)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpSmallunsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Uint16)) { Uint16 v1, v2; memcpy(&v1, p1, sizeof(Uint16)); memcpy(&v2, p2, sizeof(Uint16)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpMediumint(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= 3) { Int32 v1, v2; v1 = sint3korr((const uchar*)p1); v2 = sint3korr((const uchar*)p2); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpMediumunsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= 3) { Uint32 v1, v2; v1 = uint3korr((const uchar*)p1); v2 = uint3korr((const uchar*)p2); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpInt(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Int32)) { Int32 v1, v2; memcpy(&v1, p1, sizeof(Int32)); memcpy(&v2, p2, sizeof(Int32)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpUnsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Uint32)) { Uint32 v1, v2; memcpy(&v1, p1, sizeof(Uint32)); memcpy(&v2, p2, sizeof(Uint32)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpBigint(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Int64)) { Int64 v1, v2; memcpy(&v1, p1, sizeof(Int64)); memcpy(&v2, p2, sizeof(Int64)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpBigunsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(Uint64)) { Uint64 v1, v2; memcpy(&v1, p1, sizeof(Uint64)); memcpy(&v2, p2, sizeof(Uint64)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpFloat(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(float)) { float v1, v2; memcpy(&v1, p1, sizeof(float)); memcpy(&v2, p2, sizeof(float)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmpDouble(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (n2 >= sizeof(double)) { double v1, v2; memcpy(&v1, p1, sizeof(double)); memcpy(&v2, p2, sizeof(double)); if (v1 < v2) return -1; if (v1 > v2) return +1; return 0; } assert(! full); return CmpUnknown;}intNdbSqlUtil::cmp_olddecimal(const uchar* s1, const uchar* s2, unsigned n){ int sgn = +1; unsigned i = 0; while (i < n) { int c1 = s1[i]; int c2 = s2[i]; if (c1 == c2) { if (c1 == '-') sgn = -1; } else if (c1 == '-') { return -1; } else if (c2 == '-') { return +1; } else if (c1 < c2) { return -1 * sgn; } else { return +1 * sgn; } i++; } return 0;}intNdbSqlUtil::cmpOlddecimal(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (full) { assert(n1 == n2); const uchar* v1 = (const uchar*)p1; const uchar* v2 = (const uchar*)p2; return cmp_olddecimal(v1, v2, n1); } return CmpUnknown;}intNdbSqlUtil::cmpOlddecimalunsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ if (full) { assert(n1 == n2); const uchar* v1 = (const uchar*)p1; const uchar* v2 = (const uchar*)p2; return cmp_olddecimal(v1, v2, n1); } return CmpUnknown;}intNdbSqlUtil::cmpDecimal(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ const uchar* v1 = (const uchar*)p1; const uchar* v2 = (const uchar*)p2; // compare as binary strings unsigned n = (n1 <= n2 ? n1 : n2); int k = memcmp(v1, v2, n); if (k == 0) { k = (full ? n1 : n) - n2; } return k < 0 ? -1 : k > 0 ? +1 : full ? 0 : CmpUnknown;}intNdbSqlUtil::cmpDecimalunsigned(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ const uchar* v1 = (const uchar*)p1; const uchar* v2 = (const uchar*)p2; // compare as binary strings unsigned n = (n1 <= n2 ? n1 : n2); int k = memcmp(v1, v2, n); if (k == 0) { k = (full ? n1 : n) - n2; } return k < 0 ? -1 : k > 0 ? +1 : full ? 0 : CmpUnknown;}intNdbSqlUtil::cmpChar(const void* info, const void* p1, unsigned n1, const void* p2, unsigned n2, bool full){ // collation does not work on prefix for some charsets assert(full); const uchar* v1 = (const uchar*)p1; const uchar* v2 = (const uchar*)p2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -