[7691] Added MySQL to PostgreSQL converter.
[getmangos.git] / contrib / mysql_to_pgsql / src / defines.h
blob662a7aab2fddf6d25ebc88f1bb6978ac582e3f00
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _DEFINES_
20 #define _DEFINES_
22 #ifdef WIN32
23 #include <winsock2.h>
24 #pragma warning(disable:4996)
25 #endif
27 #include <libpq-fe.h>
28 #include <mysql.h>
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <iostream>
33 #include <sstream>
34 #include <cstdlib>
35 #include <string.h>
36 using namespace std;
39 #ifdef WIN32
40 typedef unsigned __int64 uint64;
41 typedef unsigned int uint32;
42 #else
43 #include <stdint.h>
44 #ifndef uint64_t
45 #include <linux/types.h>
46 #endif
47 typedef uint64_t uint64;
48 typedef unsigned int uint32;
49 #endif
51 struct sField
53 string name; // field name
54 string def; // field default data
55 string type; // field type
56 uint32 flags; // filed flags, see field flags;
58 typedef vector<sField> T_Table;
59 typedef vector<string> T_TableList;
60 typedef map< string, T_Table > TDataBase;
62 static
63 void pg_notice(void *arg, const char *message)
65 /// Do nothing
66 //printf("%s\n", message);
69 inline
70 string ConvertNativeType(enum_field_types mysqlType, uint32 length)
73 switch (mysqlType)
75 case FIELD_TYPE_TIMESTAMP:
76 return "timestamp";
77 case FIELD_TYPE_BIT:
78 return "bit(1)";
79 case FIELD_TYPE_DATETIME:
80 return "date";
81 case FIELD_TYPE_YEAR:
82 case FIELD_TYPE_BLOB:
83 case FIELD_TYPE_SET:
84 case FIELD_TYPE_NULL:
85 case FIELD_TYPE_ENUM:
86 return "text";
87 case FIELD_TYPE_TINY:
88 case FIELD_TYPE_SHORT:
89 case FIELD_TYPE_INT24:
90 return "integer";
91 case FIELD_TYPE_LONGLONG:
92 return "int8";
93 case FIELD_TYPE_LONG:
94 return "bigint";
95 case FIELD_TYPE_DECIMAL:
96 case FIELD_TYPE_FLOAT:
97 case FIELD_TYPE_DOUBLE:
98 return "float";
99 case FIELD_TYPE_STRING:
101 string temp;
102 char str[10];
103 temp = "char";
104 if (length)
106 temp.append("(");
107 sprintf(str,"%d",length);
108 temp.append(str);
109 temp.append(")");
111 return temp;
113 case FIELD_TYPE_VAR_STRING:
115 string temp;
116 char str[10];
117 temp = "varchar";
118 if (length)
120 temp.append("(");
121 sprintf(str,"%d",length);
122 temp.append(str);
123 temp.append(")");
125 return temp;
127 default:
128 return "text";
130 return "text";
133 inline
134 bool IsNeeedEscapeString(enum_field_types mysqlType)
136 switch(mysqlType)
138 case FIELD_TYPE_VAR_STRING:
139 case FIELD_TYPE_STRING:
140 case FIELD_TYPE_TINY_BLOB:
141 case FIELD_TYPE_MEDIUM_BLOB:
142 case FIELD_TYPE_LONG_BLOB:
143 case FIELD_TYPE_BLOB:
144 return true;
145 default:
146 return false;
148 return false;
151 inline
152 void PG_Exec_str(string sql, PGconn *mPGconn)
154 PGresult *res = PQexec (mPGconn, sql.c_str());
155 if (PQresultStatus(res) != PGRES_COMMAND_OK)
157 printf( "SQL: %s", sql.c_str() );
158 printf( "SQL %s", PQerrorMessage(mPGconn) );
162 void PG_Escape_Str(string& str)
164 if(str.empty())
165 return;
166 char* buf = new char[str.size()*2+1];
167 PQescapeString(buf,str.c_str(),str.size());
168 str = buf;
169 delete[] buf;
172 #endif