Difference between revisions of "PostgreSQL Adapter-nexj/schema-test"
(→2- Modifying Syntax) |
|||
(72 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Admon/obsolete}} | ||
+ | |||
<big><big> PostgreSQL Adapter for NexJ - Working Inside Schema 'test' </big></big> | <big><big> PostgreSQL Adapter for NexJ - Working Inside Schema 'test' </big></big> | ||
− | |||
− | + | ==1- Initializing the SQL environment== | |
* ''PostgreSQLAdapter.java'' | * ''PostgreSQLAdapter.java'' | ||
: When a connection is first established, this initial SQL statement should execute in MySQL, which is not the case for PostgreSQL, so the implementation was removed from PostgreSQLAdapter.java: | : When a connection is first established, this initial SQL statement should execute in MySQL, which is not the case for PostgreSQL, so the implementation was removed from PostgreSQLAdapter.java: | ||
Line 21: | Line 22: | ||
</source> | </source> | ||
* ''postgresql_create.sql'' | * ''postgresql_create.sql'' | ||
− | : | + | : This file was just created as a copy of mysql_create.sql, which was not the case for PostgreSQL again. So the line in the file is removed for now. The file itself may be deleted later, if there was no need for it. Name of the script file is being returned in '''''getCreateEtcScriptName()'''''' in '''PostgreSQLSchemaManager.java''' |
:: | :: | ||
<source lang=java> | <source lang=java> | ||
Line 29: | Line 30: | ||
: After activating the connection, the database is locked. | : After activating the connection, the database is locked. | ||
− | + | == 2- DATA DEFINITION LANGUAGE _ DDL == | |
− | * | + | === CREATING SCHEMA === |
− | : <code> CREATE TABLE </code> | + | * List of Files needed to be modified * |
− | + | :src- [[ PostgreSQL_Adapter-nexj/SchemaManger | PostgreSQLSchemaManager.java ]] --- ([[SchemaBackup]]) // Extends SQLSchemaManager.java - class for reading, creating and upgrading the database schema | |
− | + | : test- [[ PostgreSQL_Adapter-nexj/SchemaMangerTest | PostgreSQLSchemaManagerTest.java]] // Extends SQLSchemaManagerTest.java | |
− | + | : Script - scripts are being processed through SQLDataTest.java | |
− | + | ||
− | + | * <u><code> CREATE TABLE </code></u> | |
+ | : 1- DATA TYPE: data types are changed in '''''<code> appendColumnType() </code>''''' based on each jdbc type's equivalent in PostgreSQL. | ||
+ | ::*[http://db.apache.org/ddlutils/databases/postgresql.html mapping JDBC data types to PostgreSQL data types] | ||
+ | ::*[http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL#Data_Types comparison between Postgre and MySQL data types] | ||
+ | : 2- STORAGE ENGINE: In MySQL, the storage engine is set to a transactional safe engine such as [http://www.wikivs.com/wiki/MySQL_vs_PostgreSQL#MySQL:InnoDB InnoDB], whereas PostgreSQL has a single built in engine. So, implementation of '''''<code> appendTableSuffix() </code>''''' in PostgreSQLManager.java was removed | ||
+ | : 3- CHARACTER SET: In MySQL, the character set should be defined when creating a table. Whereas in PostgreSQL when a database is created, the character set is set to 'UTF8' by default. | ||
+ | : 4- AUTO INCREMENT: PostgreSQL doesn't support 'auto_increment' as some other databases for a unique identifier column. There are two ways around this, 1- To create a 'sequence', 2- Use SERIAL, which is a macro around 'sequence'. SERIAL is an 'integer' and a 'sequence, with the column default to the sequences next value. As of postgreSQL 7.3, to make a serial column unique, it should be specified as a unique constraint or a primary key. More info @ [[PostgreSQL_Adapter-nexj/Resources#Mapping_Data_Types.2C_Metadata | PostgreSQL Resources]] | ||
+ | : | ||
<source lang=java> | <source lang=java> | ||
/* MySQL version of creating table script */ | /* MySQL version of creating table script */ | ||
+ | |||
create table test.RangeTest( | create table test.RangeTest( | ||
id int auto_increment not null, s varchar(1) character set utf8 null, bin varbinary(1) null, | id int auto_increment not null, s varchar(1) character set utf8 null, bin varbinary(1) null, | ||
Line 46: | Line 55: | ||
)engine=InnoDB character set = utf8 | )engine=InnoDB character set = utf8 | ||
</source> | </source> | ||
− | + | : | |
<source lang=java> | <source lang=java> | ||
/* PostgreSQL version of creating table script */ | /* PostgreSQL version of creating table script */ | ||
+ | |||
CREATE TABLE test.RangeTest( | CREATE TABLE test.RangeTest( | ||
− | id | + | id serial not null, s text null, bin bytea null, |
n integer null, n1 smallint null, l bigint null, "DEC" decimal(10,5) null, | n integer null, n1 smallint null, l bigint null, "DEC" decimal(10,5) null, | ||
f double precision null, d double precision null, tm timestamp null, b boolean null, | f double precision null, d double precision null, tm timestamp null, b boolean null, | ||
constraint RangeTest_PK primary key(id) | constraint RangeTest_PK primary key(id) | ||
); | ); | ||
− | CREATE SEQUENCE test.RangeTest_id_seq; | + | // Using SERIAL is another way to work around 'sequence' |
− | ALTER TABLE test.RangeTest ALTER COLUMN id SET DEFAULT NEXTVAL('test.RangeTest_id_seq'); | + | //CREATE SEQUENCE test.RangeTest_id_seq; |
+ | //ALTER TABLE test.RangeTest ALTER COLUMN id SET DEFAULT NEXTVAL('test.RangeTest_id_seq'); | ||
</source> | </source> | ||
− | + | *<u><code> CREATE TEXT TABLE </code></u> | |
− | + | :The implementation in '''''<code> crateTextTable() </code>''''' is removed for now (base class's version is being called). | |
− | : <code>CREATE INDEX </code> | + | :TODO To be checked if it's the case for PostgreSQL as well. |
− | : <code> CREATE Trigger // if | + | |
+ | * <u><code>CREATE INDEX </code></u> | ||
+ | :The implementation in '''''<code> createIndex() </code>''''' is removed for now (base class's version is being called). | ||
+ | :TODO To be checked if it's the case for PostgreSQL as well. | ||
+ | :Note- If there was a complain for duplicate index name, a fully qualified name can be used. Just note that PostgreSQL doesn't like fully qualified name as <code>table.indexname</code> for indexes or triggers, instead a '_' can be used: <code>table_indexname</code> | ||
+ | |||
+ | * <code> <u> CREATE Trigger </u></code> | ||
+ | :TODO To be checked if triggers are needed for PostgreSQL as well. It's not an abstract method in '''''SQLSchemaManager.java'''''. | ||
+ | : <code>'''''createTrigger()'''''</code> is added to '''''MySQLSchemaManager.java''''' | ||
+ | |||
+ | ===UPDGRADING SCHEMA=== | ||
+ | * List of Files needed to be modified | ||
+ | :src - [[ PostgreSQL_Adapter-nexj/SchemaManger | PostgreSQLSchemaManager.java ]] --- ([[SchemaBackup]]) // Extends SQLSchemaManager.java - class for reading, creating and upgrading the database schema | ||
+ | : [[ PostgreSQL_Adapter-nexj/Upgrade | main.upgrade ]] | ||
+ | : test- [[ PostgreSQL_Adapter-nexj/SchemaMangerTest | PostgreSQLSchemaManagerTest.java]] // Extends SQLSchemaManagerTest.java | ||
+ | : Script - postgresql_drop.sql | ||
+ | |||
+ | * <u><code> DROP TABLE </code></u> | ||
+ | |||
+ | * <u><code> INSERT INTO TABLE </code></u> | ||
+ | |||
+ | * <u><code> ALTER TABLE </code></u> | ||
− | + | ==3- DATA MANIPULATION LANGUAGE _ DML == | |
− | + | * List of Files needed to be modified[[sampleProj]] | |
− | + | :src- [[ PostgreSQL_Adapter-nexj/Adapter | PostgreSQLAdapter.java ]] // Extends SQLAdapter.java - SQL Persistence adapter, responsible for regular data queries (insert, select, delete) | |
− | + | : test- [[ PostgreSQL_Adapter-nexj/AdapterTest | PostgreSQLAdapterTest.java ]]] // Extends SQLAdapterTest.java | |
− | :src | ||
− | |||
− | : | ||
− | |||
− | |||
− | |||
− | |||
: Script - scripts are being processed through SQLDataTest.java | : Script - scripts are being processed through SQLDataTest.java | ||
− | :: | + | :: postgresql_insert.sql |
− | |||
− |
Latest revision as of 19:49, 26 January 2014
PostgreSQL Adapter for NexJ - Working Inside Schema 'test'
Contents
1- Initializing the SQL environment
- PostgreSQLAdapter.java
- When a connection is first established, this initial SQL statement should execute in MySQL, which is not the case for PostgreSQL, so the implementation was removed from PostgreSQLAdapter.java:
/* MySQLAdapter.java */
public String getInitialSQL()
{
StringBuffer buf = new StringBuffer();
buf.append("set sql_mode = concat(@@sql_mode, ',ANSI_QUOTES')"); // allow using doublequote when quoting column names in "CREATE TABLE" statements
buf.append(";set optimizer_search_depth = 0"); // let DB automatically decide on how long it takes to examine plans, improves long planning sessions
buf.append(";set max_sort_length = ").append
(Math.max(MAX_VARCHAR_PRECISION, MAX_VARBINARY_PRECISION)); // set TEXT/BLOB minimum sorting length to be same as cutoff between varchar/text
return buf.toString();
}
- postgresql_create.sql
- This file was just created as a copy of mysql_create.sql, which was not the case for PostgreSQL again. So the line in the file is removed for now. The file itself may be deleted later, if there was no need for it. Name of the script file is being returned in getCreateEtcScriptName()' in PostgreSQLSchemaManager.java
/* nexj/core/persistence/sql/etc/postgresql_create.sql */
set sql_mode = concat(@@sql_mode, ',ANSI_QUOTES');
- After activating the connection, the database is locked.
2- DATA DEFINITION LANGUAGE _ DDL
CREATING SCHEMA
- List of Files needed to be modified *
- src- PostgreSQLSchemaManager.java --- (SchemaBackup) // Extends SQLSchemaManager.java - class for reading, creating and upgrading the database schema
- test- PostgreSQLSchemaManagerTest.java // Extends SQLSchemaManagerTest.java
- Script - scripts are being processed through SQLDataTest.java
-
CREATE TABLE
- 1- DATA TYPE: data types are changed in
appendColumnType()
based on each jdbc type's equivalent in PostgreSQL. - 2- STORAGE ENGINE: In MySQL, the storage engine is set to a transactional safe engine such as InnoDB, whereas PostgreSQL has a single built in engine. So, implementation of
appendTableSuffix()
in PostgreSQLManager.java was removed - 3- CHARACTER SET: In MySQL, the character set should be defined when creating a table. Whereas in PostgreSQL when a database is created, the character set is set to 'UTF8' by default.
- 4- AUTO INCREMENT: PostgreSQL doesn't support 'auto_increment' as some other databases for a unique identifier column. There are two ways around this, 1- To create a 'sequence', 2- Use SERIAL, which is a macro around 'sequence'. SERIAL is an 'integer' and a 'sequence, with the column default to the sequences next value. As of postgreSQL 7.3, to make a serial column unique, it should be specified as a unique constraint or a primary key. More info @ PostgreSQL Resources
/* MySQL version of creating table script */
create table test.RangeTest(
id int auto_increment not null, s varchar(1) character set utf8 null, bin varbinary(1) null,
n int null, n1 tinyint unsigned null, l bigint null, "DEC" decimal(10,5) null,
f float null, d double null, tm datetime null, b boolean null,
constraint RangeTest_PK primary key(id)
)engine=InnoDB character set = utf8
/* PostgreSQL version of creating table script */
CREATE TABLE test.RangeTest(
id serial not null, s text null, bin bytea null,
n integer null, n1 smallint null, l bigint null, "DEC" decimal(10,5) null,
f double precision null, d double precision null, tm timestamp null, b boolean null,
constraint RangeTest_PK primary key(id)
);
// Using SERIAL is another way to work around 'sequence'
//CREATE SEQUENCE test.RangeTest_id_seq;
//ALTER TABLE test.RangeTest ALTER COLUMN id SET DEFAULT NEXTVAL('test.RangeTest_id_seq');
CREATE TEXT TABLE
- The implementation in
crateTextTable()
is removed for now (base class's version is being called). - TODO To be checked if it's the case for PostgreSQL as well.
-
CREATE INDEX
- The implementation in
createIndex()
is removed for now (base class's version is being called). - TODO To be checked if it's the case for PostgreSQL as well.
- Note- If there was a complain for duplicate index name, a fully qualified name can be used. Just note that PostgreSQL doesn't like fully qualified name as
table.indexname
for indexes or triggers, instead a '_' can be used:table_indexname
-
CREATE Trigger
- TODO To be checked if triggers are needed for PostgreSQL as well. It's not an abstract method in SQLSchemaManager.java.
-
createTrigger()
is added to MySQLSchemaManager.java
UPDGRADING SCHEMA
- List of Files needed to be modified
- src - PostgreSQLSchemaManager.java --- (SchemaBackup) // Extends SQLSchemaManager.java - class for reading, creating and upgrading the database schema
- main.upgrade
- test- PostgreSQLSchemaManagerTest.java // Extends SQLSchemaManagerTest.java
- Script - postgresql_drop.sql
-
DROP TABLE
-
INSERT INTO TABLE
-
ALTER TABLE
3- DATA MANIPULATION LANGUAGE _ DML
- List of Files needed to be modifiedsampleProj
- src- PostgreSQLAdapter.java // Extends SQLAdapter.java - SQL Persistence adapter, responsible for regular data queries (insert, select, delete)
- test- PostgreSQLAdapterTest.java ] // Extends SQLAdapterTest.java
- Script - scripts are being processed through SQLDataTest.java
- postgresql_insert.sql