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.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

and beneath it included the definition for my data type

| mytype field_length opt_binary

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:

so I included my data type specification right below it.


********* 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.


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

6) Generate the, 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.

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:

Hope this helps.

Options: ReplyQuote

Written By
March 24, 2012 09:31AM
March 26, 2012 10:38AM
March 26, 2012 04:50PM
March 26, 2012 12:26PM
March 26, 2012 04:51PM
March 26, 2012 06:55PM
March 31, 2012 05:04AM
April 01, 2012 09:15AM
April 01, 2012 12:11PM
April 03, 2012 09:45AM
April 04, 2012 11:05AM
May 14, 2012 12:21PM
Re: Create New Data Type
May 14, 2012 12:53PM
March 26, 2012 01:32AM
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.