Re: Create New Data Type
Posted by: larry williams
Date: May 14, 2012 12:53PM

I was able to create a simple new data type, MYTYPE as a similar data type to the CHAR type. The first thing to understand is the use of the sql_yacc.yy and the lex.h files. If you don't understand what these files are and how to generate their products, the sql_yacc.cc, sql_yacc.h and lex_hash.h files, read up on that first. That is step 1. I'm starting at step 2.

In the sql_yacc.yy file
1) Declare your token:
%token MYTYPE
Notice that the tokens are in alphabetic order. Keep yours this way.

2) Declare your type in the %type <NONE> area. You'll notice a few other 'non-primitive' field types such as varchar in there. Include MYTYPE in alphabetic order within this area near varchar. (NOTE: I'm not sure what this section's purpose is, but I needed to include my type for it to work.)

3) I based my type off of the char data type. As a result, I want to immulate the char data type in my data type. I found:

| char field_length opt_binary
{
$$=MYSQL_TYPE_STRING;
}

and beneath it included the definition for my data type

| mytype field_length opt_binary
{
$$=MYSQL_TYPE_STRING;
}

What this does is assigns the base string type defined in MySQL to my data type. I'm essentially inheriting.

4) Continued my search for the char data type and found:

char:
CHAR_SYM {}
;
so I included my data type specification right below it.

mytype:
MYTYPE {}

********* That's it for the sql_yacc.yy file *********

In the lex.h file

5) Within the "static SYMBOL symbols[]' array, include your data type. Notice that this is also in alphabetic order. Keep it this way.

{ "MYTYPE", SYM(MYTYPE)},

********* That's it for the lex.h file *********

6) Generate the sql_yacc.cc, sql_yacc.h and lex_hash.h files.

7) Because I am basically creating an 'alias' for the char type, all of the functionality I need comes from the MYSQL_TYPE_STRING defined inside of MySQL.

NOW:
I can start MySQL
I can 'alter table test add column myval mytype(124)' and the server accepts my data type. You'll notice that when you 'describe' the table, your new data type shows up as 'char'. That's because MySQL converts some fields 'under the covers'. Here's a reference:

http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

Hope this helps.

Options: ReplyQuote


Subject
Views
Written By
Posted
2699
March 24, 2012 09:31AM
1051
March 26, 2012 10:38AM
1053
March 26, 2012 04:50PM
842
March 26, 2012 12:26PM
1021
March 26, 2012 04:51PM
1028
March 26, 2012 06:55PM
947
March 31, 2012 05:04AM
1105
April 01, 2012 09:15AM
1015
April 01, 2012 12:11PM
1029
April 03, 2012 09:45AM
1068
April 04, 2012 11:05AM
1174
May 14, 2012 12:21PM
Re: Create New Data Type
1804
May 14, 2012 12:53PM
1097
March 26, 2012 01:32AM
1121
June 23, 2012 08:18AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.