Skip to content

Commit b683b7a

Browse files
authored
[APIS-774] column name and table name should also support Encoding with given charset (#9)
* [APIS-774] column name and table name should also support Encoding with given charset * [APIS-774] define constant 254 as the max length of column and table name * [APIS-774] define constant 254 as the max length of column and table name
1 parent 378dcf1 commit b683b7a

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

Code/Src/CUBRIDCciInterface.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ internal struct T_CCI_COL_INFO
156156
public char is_non_null;
157157
public short scale;
158158
public int precision;
159-
public string col_name;
160-
public string real_attr;
161-
public string class_name;
162-
public string default_value;
159+
public IntPtr col_name;
160+
public IntPtr real_attr;
161+
public IntPtr class_name;
162+
public IntPtr default_value;
163163
public char is_auto_increment;
164164
public char is_unique_key;
165165
public char is_primary_key;
@@ -197,6 +197,7 @@ internal struct T_CCI_QUERY_RESULT
197197

198198
internal static class CciInterface
199199
{
200+
const int MAX_TBALE_COLUMN_NAME_LENGTH = 254;
200201
const string dll_name = @"cascci.dll";
201202
[DllImport(dll_name, EntryPoint = "cci_get_db_version", CharSet = CharSet.Ansi)]
202203
public static extern int cci_get_db_version(int con_handle, StringBuilder out_buf, int capacity);
@@ -261,10 +262,12 @@ public static extern int cci_oid_get_class_name(int mapped_conn_id, string oid_s
261262
[DllImport(dll_name, EntryPoint = "cci_get_result_info", CharSet = CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
262263
public static extern IntPtr cci_get_result_info_internal(int req_handle, ref int stmt_type, ref int col_num);
263264

264-
public static ColumnMetaData[] cci_get_result_info(int req_handle)
265+
public static ColumnMetaData[] cci_get_result_info(CUBRIDConnection conn, int req_handle)
265266
{
266267
int stmt_type = 0;
267268
int col_num = 0;
269+
int n;
270+
byte[] name = new byte[MAX_TBALE_COLUMN_NAME_LENGTH];
268271

269272
IntPtr pt = cci_get_result_info_internal(req_handle, ref stmt_type, ref col_num);
270273
ColumnMetaData[] item = new ColumnMetaData[col_num];
@@ -288,11 +291,28 @@ public static ColumnMetaData[] cci_get_result_info(int req_handle)
288291
data.IsShared = int_to_bool(tmp.is_shared - 0);
289292
data.IsUniqueKey = int_to_bool(tmp.is_unique_key - 0);
290293
data.Precision = tmp.precision;
291-
data.RealName = tmp.class_name;
292-
data.Name = tmp.col_name;
293294
data.Scale = tmp.scale;
294-
data.Table = tmp.class_name;
295295
data.Type = (CUBRIDDataType)tmp.ext_type;
296+
297+
Marshal.Copy(tmp.col_name, name, 0, MAX_TBALE_COLUMN_NAME_LENGTH);
298+
for (n = 0; n < MAX_TBALE_COLUMN_NAME_LENGTH; n++) if (name[n] == 0) break;
299+
if (conn.GetEncoding().Equals(Encoding.UTF8))
300+
data.Name = Encoding.UTF8.GetString(name, 0, n);
301+
else
302+
data.Name = Encoding.Unicode.GetString(name, 0, n);
303+
304+
Marshal.Copy(tmp.class_name, name, 0, MAX_TBALE_COLUMN_NAME_LENGTH);
305+
for (n = 0; n < MAX_TBALE_COLUMN_NAME_LENGTH; n++) if (name[n] == 0) break;
306+
if (conn.GetEncoding().Equals(Encoding.UTF8))
307+
{
308+
data.RealName = Encoding.UTF8.GetString(name, 0, n);
309+
data.Table = Encoding.UTF8.GetString(name, 0, n);
310+
}
311+
else
312+
{
313+
data.RealName = Encoding.Unicode.GetString(name, 0, n);
314+
data.Table = Encoding.Unicode.GetString(name, 0, n);
315+
}
296316
}
297317
catch
298318
{

Code/Src/CUBRIDCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,9 @@ internal CUBRIDDataReader ExecuteInternal()
469469
}
470470

471471
//T_CCI_COL_INFO res;
472-
columnInfos = CciInterface.cci_get_result_info (handle);
473-
dataReader = new CUBRIDDataReader (this, handle, ret, columnInfos, ret);
472+
columnInfos = CciInterface.cci_get_result_info (conn, handle);
473+
474+
dataReader = new CUBRIDDataReader (this, handle, ret, columnInfos, ret);
474475

475476
return dataReader;
476477
}
@@ -536,7 +537,7 @@ public override int ExecuteNonQuery()
536537
throw new CUBRIDException (err.err_msg);
537538
}
538539

539-
columnInfos = CciInterface.cci_get_result_info(handle);
540+
columnInfos = CciInterface.cci_get_result_info(conn, handle);
540541

541542
if (this.Parameters.Count > 0)
542543
{

Code/Src/CUBRIDDataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ public override bool NextResult()
804804
resultCount = CciInterface.cci_next_result(handle, ref err);
805805
if (resultCount >= 0)
806806
{
807-
columnMetaData = CciInterface.cci_get_result_info(handle);
807+
columnMetaData = CciInterface.cci_get_result_info(conn, handle);
808808
currentRow = 0;
809809
resultTuple = new ResultTuple(columnMetaData.Length);
810810
commandBehavior = CommandBehavior.Default;

Code/Src/CUBRIDSchemaProvider.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ private void LoadTableColumns(DataTable dt, string tableName, string columnRestr
246246
{
247247
string sql =
248248
String.Format(
249-
"select attr_name, default_value, is_nullable, `data_type`, prec, scale, charset from db_attribute where class_name like '{0}' and attr_name like '{1}' order by def_order asc",
249+
"select * from db_attribute where class_name like '{0}' and attr_name like '{1}' order by def_order asc",
250250
tableName, columnRestriction);
251+
251252
using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
252253
{
253254
int pos = 1;
@@ -264,12 +265,31 @@ private void LoadTableColumns(DataTable dt, string tableName, string columnRestr
264265
row["COLUMN_NAME"] = colName;
265266
row["ORDINAL_POSITION"] = pos++;
266267

267-
row["COLUMN_DEFAULT"] = reader.GetString(1);
268-
row["IS_NULLABLE"] = reader.GetString(2).Equals("YES");
269-
row["DATA_TYPE"] = reader.GetString(3);
270-
row["NUMERIC_PRECISION"] = reader.GetInt(4);
271-
row["NUMERIC_SCALE"] = reader.GetInt(5);
272-
row["CHARACTER_SET"] = reader.GetString(6);
268+
for (int i = 0; i < reader.GetColumnCount(); i++) {
269+
switch (reader.GetColumnName(i)) {
270+
case "defalut_value":
271+
row["COLUMN_DEFAULT"] = reader.GetString(i);
272+
break;
273+
case "is_nullable":
274+
row["IS_NULLABLE"] = reader.GetString(i).Equals("YES");
275+
break;
276+
case "data_type":
277+
row["DATA_TYPE"] = reader.GetString(i);
278+
break;
279+
case "prec":
280+
row["NUMERIC_PRECISION"] = reader.GetInt(i);
281+
break;
282+
case "scale":
283+
row["NUMERIC_SCALE"] = reader.GetInt(i);
284+
break;
285+
case "code_set":
286+
case "charset":
287+
row["CHARACTER_SET"] = reader.GetString(i);
288+
break;
289+
default:
290+
break;
291+
}
292+
}
273293

274294
dt.Rows.Add(row);
275295
}
@@ -455,7 +475,7 @@ public DataTable GetForeignKeys(string[] filters)
455475
throw new CUBRIDException(err.err_msg);
456476
}
457477

458-
ColumnMetaData[] columnInfos = CciInterface.cci_get_result_info(handle);
478+
ColumnMetaData[] columnInfos = CciInterface.cci_get_result_info(conn, handle);
459479
CUBRIDCommand command = new CUBRIDCommand(null,conn);
460480
CUBRIDDataReader reader = new CUBRIDDataReader(command, handle, columnInfos.Length, columnInfos, columnInfos.Length);
461481

0 commit comments

Comments
 (0)