1#include "convertMySQLDataType.h"
2
3#include <Core/Field.h>
4#include <Core/Types.h>
5#include <Parsers/ASTFunction.h>
6#include <Parsers/ASTIdentifier.h>
7#include <Parsers/IAST.h>
8#include "DataTypeDate.h"
9#include "DataTypeDateTime.h"
10#include "DataTypeFixedString.h"
11#include "DataTypeNullable.h"
12#include "DataTypeString.h"
13#include "DataTypesNumber.h"
14#include "IDataType.h"
15
16namespace DB
17{
18ASTPtr dataTypeConvertToQuery(const DataTypePtr & data_type)
19{
20 WhichDataType which(data_type);
21
22 if (!which.isNullable())
23 return std::make_shared<ASTIdentifier>(data_type->getName());
24
25 return makeASTFunction("Nullable", dataTypeConvertToQuery(typeid_cast<const DataTypeNullable *>(data_type.get())->getNestedType()));
26}
27
28DataTypePtr convertMySQLDataType(const std::string & mysql_data_type, bool is_nullable, bool is_unsigned, size_t length)
29{
30 DataTypePtr res;
31 if (mysql_data_type == "tinyint")
32 {
33 if (is_unsigned)
34 res = std::make_shared<DataTypeUInt8>();
35 else
36 res = std::make_shared<DataTypeInt8>();
37 }
38 else if (mysql_data_type == "smallint")
39 {
40 if (is_unsigned)
41 res = std::make_shared<DataTypeUInt16>();
42 else
43 res = std::make_shared<DataTypeInt16>();
44 }
45 else if (mysql_data_type == "int" || mysql_data_type == "mediumint")
46 {
47 if (is_unsigned)
48 res = std::make_shared<DataTypeUInt32>();
49 else
50 res = std::make_shared<DataTypeInt32>();
51 }
52 else if (mysql_data_type == "bigint")
53 {
54 if (is_unsigned)
55 res = std::make_shared<DataTypeUInt64>();
56 else
57 res = std::make_shared<DataTypeInt64>();
58 }
59 else if (mysql_data_type == "float")
60 res = std::make_shared<DataTypeFloat32>();
61 else if (mysql_data_type == "double")
62 res = std::make_shared<DataTypeFloat64>();
63 else if (mysql_data_type == "date")
64 res = std::make_shared<DataTypeDate>();
65 else if (mysql_data_type == "datetime" || mysql_data_type == "timestamp")
66 res = std::make_shared<DataTypeDateTime>();
67 else if (mysql_data_type == "binary")
68 res = std::make_shared<DataTypeFixedString>(length);
69 else
70 /// Also String is fallback for all unknown types.
71 res = std::make_shared<DataTypeString>();
72 if (is_nullable)
73 res = std::make_shared<DataTypeNullable>(res);
74 return res;
75}
76
77}
78