diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..dd84ea7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..8f3a882 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,19 @@ +name: 'Unit Tests' + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: current + - run: npm ci + - run: npm test diff --git a/examples/adventureworks_mysql.sql b/examples/adventureworks_mysql.sql new file mode 100644 index 0000000..ec2340b --- /dev/null +++ b/examples/adventureworks_mysql.sql @@ -0,0 +1,784 @@ +/* +sql generated using: + * Package: little-mermaid-2-the-sql + * Version: 0.0.3 + * databaseInfo: mysql +*/ + +CREATE TABLE `adventureworks_addresstype` ( + `AddressTypeID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`AddressTypeID`) +) + +CREATE TABLE `adventureworks_productsubcategory` ( + `ProductSubcategoryID` INT NOT NULL, + `ProductCategoryID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductSubcategoryID`), + FOREIGN KEY (`ProductCategoryID`) REFERENCES `adventureworks_productcategory`(`ProductCategoryID`) +) + +CREATE TABLE `adventureworks_awbuildversion` ( + `SystemInformationID` INT NOT NULL, + `Database Version` VARCHAR NOT NULL, + `VersionDate` DATETIME NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SystemInformationID`) +) + +CREATE TABLE `adventureworks_contact` ( + `ContactID` INT NOT NULL, + `NameStyle` BIT NOT NULL, + `Title` VARCHAR, + `FirstName` VARCHAR NOT NULL, + `MiddleName` VARCHAR, + `LastName` VARCHAR NOT NULL, + `Suffix` VARCHAR, + `EmailAddress` VARCHAR, + `EmailPromotion` INT NOT NULL, + `Phone` VARCHAR, + `PasswordHash` VARCHAR NOT NULL, + `PasswordSalt` VARCHAR NOT NULL, + `AdditionalContactInfo` MEDIUMTEXT, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ContactID`) +) + +CREATE TABLE `adventureworks_contacttype` ( + `ContactTypeID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ContactTypeID`) +) + +CREATE TABLE `adventureworks_countryregion` ( + `CountryRegionCode` VARCHAR NOT NULL, + `Name` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CountryRegionCode`) +) + +CREATE TABLE `adventureworks_countryregioncurrency` ( + `CountryRegionCode` VARCHAR NOT NULL, + `CurrencyCode` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CountryRegionCode`,`CurrencyCode`) +) + +CREATE TABLE `adventureworks_creditcard` ( + `CreditCardID` INT NOT NULL, + `CardType` VARCHAR NOT NULL, + `CardNumber` VARCHAR NOT NULL, + `ExpMonth` TINYINT NOT NULL, + `ExpYear` SMALLINT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CreditCardID`) +) + +CREATE TABLE `adventureworks_culture` ( + `CultureID` VARCHAR NOT NULL, + `Name` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CultureID`) +) + +CREATE TABLE `adventureworks_currency` ( + `CurrencyCode` VARCHAR NOT NULL, + `Name` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CurrencyCode`) +) + +CREATE TABLE `adventureworks_currencyrate` ( + `CurrencyRateID` INT NOT NULL, + `CurrencyRateDate` DATETIME NOT NULL, + `FromCurrencyCode` VARCHAR NOT NULL, + `ToCurrencyCode` VARCHAR NOT NULL, + `AverageRate` DOUBLE NOT NULL, + `EndOfDayRate` DOUBLE NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CurrencyRateID`) +) + +CREATE TABLE `adventureworks_databaselog` ( + `DatabaseLogID` INT NOT NULL, + `PostTime` TIMESTAMP NOT NULL, + `DatabaseUser` VARCHAR NOT NULL, + `Event` VARCHAR NOT NULL, + `Schema` VARCHAR, + `Object` VARCHAR, + `TSQL` MEDIUMTEXT NOT NULL, + `XmlEvent` MEDIUMTEXT NOT NULL, + PRIMARY KEY(`DatabaseLogID`) +) + +CREATE TABLE `adventureworks_department` ( + `DepartmentID` SMALLINT NOT NULL, + `Name` VARCHAR NOT NULL, + `GroupName` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`DepartmentID`) +) + +CREATE TABLE `adventureworks_document` ( + `DocumentID` INT NOT NULL, + `Title` VARCHAR NOT NULL, + `FileName` MEDIUMTEXT NOT NULL, + `FileExtension` VARCHAR NOT NULL, + `Revision` VARCHAR NOT NULL, + `ChangeNumber` INT NOT NULL, + `Status` TINYINT NOT NULL, + `DocumentSummary` MEDIUMTEXT, + `Document` BLOB, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`DocumentID`) +) + +CREATE TABLE `adventureworks_errorlog` ( + `ErrorLogID` INT NOT NULL, + `ErrorTime` TIMESTAMP NOT NULL, + `UserName` VARCHAR NOT NULL, + `ErrorNumber` INT NOT NULL, + `ErrorSeverity` INT, + `ErrorState` INT, + `ErrorProcedure` VARCHAR, + `ErrorLine` INT, + `ErrorMessage` MEDIUMTEXT NOT NULL, + PRIMARY KEY(`ErrorLogID`) +) + +CREATE TABLE `adventureworks_illustration` ( + `IllustrationID` INT NOT NULL, + `Diagram` TEXT, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`IllustrationID`) +) + +CREATE TABLE `adventureworks_location` ( + `LocationID` SMALLINT NOT NULL, + `Name` VARCHAR NOT NULL, + `CostRate` DOUBLE NOT NULL, + `Availability` DECIMAL NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`LocationID`) +) + +CREATE TABLE `adventureworks_productcategory` ( + `ProductCategoryID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductCategoryID`) +) + +CREATE TABLE `adventureworks_productdescription` ( + `ProductDescriptionID` INT NOT NULL, + `Description` MEDIUMTEXT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductDescriptionID`) +) + +CREATE TABLE `adventureworks_productmodel` ( + `ProductModelID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `CatalogDescription` TEXT, + `Instructions` TEXT, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductModelID`) +) + +CREATE TABLE `adventureworks_productphoto` ( + `ProductPhotoID` INT NOT NULL, + `ThumbNailPhoto` BLOB, + `ThumbnailPhotoFileName` VARCHAR, + `LargePhoto` BLOB, + `LargePhotoFileName` VARCHAR, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductPhotoID`) +) + +CREATE TABLE `adventureworks_salesreason` ( + `SalesReasonID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `ReasonType` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesReasonID`) +) + +CREATE TABLE `adventureworks_salestaxrate` ( + `SalesTaxRateID` INT NOT NULL, + `StateProvinceID` INT NOT NULL, + `TaxType` TINYINT NOT NULL, + `TaxRate` DOUBLE NOT NULL, + `Name` VARCHAR NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesTaxRateID`) +) + +CREATE TABLE `adventureworks_scrapreason` ( + `ScrapReasonID` SMALLINT NOT NULL, + `Name` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ScrapReasonID`) +) + +CREATE TABLE `adventureworks_shift` ( + `ShiftID` TINYINT NOT NULL, + `Name` VARCHAR NOT NULL, + `StartTime` DATETIME NOT NULL, + `EndTime` DATETIME NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ShiftID`) +) + +CREATE TABLE `adventureworks_shipmethod` ( + `ShipMethodID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `ShipBase` DOUBLE NOT NULL, + `ShipRate` DOUBLE NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ShipMethodID`) +) + +CREATE TABLE `adventureworks_specialoffer` ( + `SpecialOfferID` INT NOT NULL, + `Description` VARCHAR NOT NULL, + `DiscountPct` DOUBLE NOT NULL, + `Type` VARCHAR NOT NULL, + `Category` VARCHAR NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME NOT NULL, + `MinQty` INT NOT NULL, + `MaxQty` INT, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SpecialOfferID`) +) + +CREATE TABLE `adventureworks_unitmeasure` ( + `UnitMeasureCode` VARCHAR NOT NULL, + `Name` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`UnitMeasureCode`) +) + +CREATE TABLE `adventureworks_vendor` ( + `VendorID` INT NOT NULL, + `AccountNumber` VARCHAR NOT NULL, + `Name` VARCHAR NOT NULL, + `CreditRating` TINYINT NOT NULL, + `PreferredVendorStatus` BIT NOT NULL, + `ActiveFlag` BIT NOT NULL, + `PurchasingWebServiceURL` MEDIUMTEXT, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`VendorID`) +) + +CREATE TABLE `adventureworks_contactcreditcard` ( + `ContactID` INT NOT NULL, + `CreditCardID` INT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ContactID`,`CreditCardID`), + FOREIGN KEY (`ContactID`) REFERENCES `adventureworks_contact`(`ContactID`), + FOREIGN KEY (`CreditCardID`) REFERENCES `adventureworks_creditcard`(`CreditCardID`) +) + +CREATE TABLE `adventureworks_employee` ( + `EmployeeID` INT NOT NULL, + `NationalIDNumber` VARCHAR NOT NULL, + `ContactID` INT NOT NULL, + `LoginID` VARCHAR NOT NULL, + `ManagerID` INT, + `Title` VARCHAR NOT NULL, + `BirthDate` DATETIME NOT NULL, + `MaritalStatus` VARCHAR NOT NULL, + `Gender` VARCHAR NOT NULL, + `HireDate` DATETIME NOT NULL, + `SalariedFlag` BIT NOT NULL, + `VacationHours` SMALLINT NOT NULL, + `SickLeaveHours` SMALLINT NOT NULL, + `CurrentFlag` BIT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`EmployeeID`), + FOREIGN KEY (`ContactID`) REFERENCES `adventureworks_contact`(`ContactID`) +) + +CREATE TABLE `adventureworks_productmodelillustration` ( + `ProductModelID` INT NOT NULL, + `IllustrationID` INT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductModelID`,`IllustrationID`), + FOREIGN KEY (`IllustrationID`) REFERENCES `adventureworks_illustration`(`IllustrationID`), + FOREIGN KEY (`ProductModelID`) REFERENCES `adventureworks_productmodel`(`ProductModelID`) +) + +CREATE TABLE `adventureworks_productmodelproductdescriptionculture` ( + `ProductModelID` INT NOT NULL, + `ProductDescriptionID` INT NOT NULL, + `CultureID` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductModelID`,`ProductDescriptionID`,`CultureID`), + FOREIGN KEY (`CultureID`) REFERENCES `adventureworks_culture`(`CultureID`), + FOREIGN KEY (`ProductDescriptionID`) REFERENCES `adventureworks_productdescription`(`ProductDescriptionID`), + FOREIGN KEY (`ProductModelID`) REFERENCES `adventureworks_productmodel`(`ProductModelID`) +) + +CREATE TABLE `adventureworks_salesterritory` ( + `TerritoryID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `CountryRegionCode` VARCHAR NOT NULL, + `Group` VARCHAR NOT NULL, + `SalesYTD` DOUBLE NOT NULL, + `SalesLastYear` DOUBLE NOT NULL, + `CostYTD` DOUBLE NOT NULL, + `CostLastYear` DOUBLE NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`TerritoryID`), + FOREIGN KEY (`CountryRegionCode`) REFERENCES `adventureworks_countryregion`(`CountryRegionCode`) +) + +CREATE TABLE `adventureworks_vendorcontact` ( + `VendorID` INT NOT NULL, + `ContactID` INT NOT NULL, + `ContactTypeID` INT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`VendorID`,`ContactID`), + FOREIGN KEY (`ContactID`) REFERENCES `adventureworks_contact`(`ContactID`) +) + +CREATE TABLE `adventureworks_customer` ( + `CustomerID` INT NOT NULL, + `TerritoryID` INT, + `AccountNumber` VARCHAR NOT NULL, + `CustomerType` VARCHAR NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CustomerID`), + FOREIGN KEY (`TerritoryID`) REFERENCES `adventureworks_salesterritory`(`TerritoryID`) +) + +CREATE TABLE `adventureworks_employeedepartmenthistory` ( + `EmployeeID` INT NOT NULL, + `DepartmentID` SMALLINT NOT NULL, + `ShiftID` TINYINT NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`EmployeeID`,`DepartmentID`,`ShiftID`,`StartDate`), + FOREIGN KEY (`DepartmentID`) REFERENCES `adventureworks_department`(`DepartmentID`), + FOREIGN KEY (`EmployeeID`) REFERENCES `adventureworks_employee`(`EmployeeID`), + FOREIGN KEY (`ShiftID`) REFERENCES `adventureworks_shift`(`ShiftID`) +) + +CREATE TABLE `adventureworks_employeepayhistory` ( + `EmployeeID` INT NOT NULL, + `RateChangeDate` DATETIME NOT NULL, + `Rate` DOUBLE NOT NULL, + `PayFrequency` TINYINT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`EmployeeID`,`RateChangeDate`), + FOREIGN KEY (`EmployeeID`) REFERENCES `adventureworks_employee`(`EmployeeID`) +) + +CREATE TABLE `adventureworks_jobcandidate` ( + `JobCandidateID` INT NOT NULL, + `EmployeeID` INT, + `Resume` TEXT, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`JobCandidateID`), + FOREIGN KEY (`EmployeeID`) REFERENCES `adventureworks_employee`(`EmployeeID`) +) + +CREATE TABLE `adventureworks_product` ( + `ProductID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `ProductNumber` VARCHAR NOT NULL, + `MakeFlag` BIT NOT NULL, + `FinishedGoodsFlag` BIT NOT NULL, + `Color` VARCHAR, + `SafetyStockLevel` SMALLINT NOT NULL, + `ReorderPoint` SMALLINT NOT NULL, + `StandardCost` DOUBLE NOT NULL, + `ListPrice` DOUBLE NOT NULL, + `Size` VARCHAR, + `SizeUnitMeasureCode` VARCHAR, + `WeightUnitMeasureCode` VARCHAR, + `Weight` DECIMAL, + `DaysToManufacture` INT NOT NULL, + `ProductLine` VARCHAR, + `Class` VARCHAR, + `Style` VARCHAR, + `ProductSubcategoryID` INT, + `ProductModelID` INT, + `SellStartDate` DATETIME NOT NULL, + `SellEndDate` DATETIME, + `DiscontinuedDate` DATETIME, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`), + FOREIGN KEY (`ProductModelID`) REFERENCES `adventureworks_productmodel`(`ProductModelID`) +) + +CREATE TABLE `adventureworks_purchaseorderheader` ( + `PurchaseOrderID` INT NOT NULL, + `RevisionNumber` TINYINT, + `Status` TINYINT, + `EmployeeID` INT, + `VendorID` INT, + `ShipMethodID` INT, + `OrderDate` DATETIME, + `ShipDate` DATETIME, + `SubTotal` DOUBLE, + `TaxAmt` DOUBLE, + `Freight` DOUBLE, + `TotalDue` DOUBLE, + `ModifiedDate` DATETIME, + PRIMARY KEY(`PurchaseOrderID`) +) + +CREATE TABLE `adventureworks_salesperson` ( + `SalesPersonID` INT NOT NULL, + `TerritoryID` INT, + `SalesQuota` DOUBLE, + `Bonus` DOUBLE NOT NULL, + `CommissionPct` DOUBLE NOT NULL, + `SalesYTD` DOUBLE NOT NULL, + `SalesLastYear` DOUBLE NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesPersonID`) +) + +CREATE TABLE `adventureworks_salesterritoryhistory` ( + `SalesPersonID` INT NOT NULL, + `TerritoryID` INT NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesPersonID`,`TerritoryID`,`StartDate`) +) + +CREATE TABLE `adventureworks_stateprovince` ( + `StateProvinceID` INT NOT NULL, + `StateProvinceCode` VARCHAR NOT NULL, + `CountryRegionCode` VARCHAR NOT NULL, + `IsOnlyStateProvinceFlag` BIT NOT NULL, + `Name` VARCHAR NOT NULL, + `TerritoryID` INT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`StateProvinceID`) +) + +CREATE TABLE `adventureworks_address` ( + `AddressID` INT NOT NULL, + `AddressLine1` VARCHAR NOT NULL, + `AddressLine2` VARCHAR, + `City` VARCHAR NOT NULL, + `StateProvinceID` INT NOT NULL, + `PostalCode` VARCHAR NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`AddressID`) +) + +CREATE TABLE `adventureworks_billofmaterials` ( + `BillOfMaterialsID` INT NOT NULL, + `ProductAssemblyID` INT, + `ComponentID` INT NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME, + `UnitMeasureCode` VARCHAR NOT NULL, + `BOMLevel` SMALLINT NOT NULL, + `PerAssemblyQty` DECIMAL NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`BillOfMaterialsID`) +) + +CREATE TABLE `adventureworks_individual` ( + `CustomerID` INT NOT NULL, + `ContactID` INT NOT NULL, + `Demographics` TEXT, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CustomerID`) +) + +CREATE TABLE `adventureworks_productcosthistory` ( + `ProductID` INT NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME, + `StandardCost` DOUBLE NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`,`StartDate`) +) + +CREATE TABLE `adventureworks_productdocument` ( + `ProductID` INT NOT NULL, + `DocumentID` INT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`,`DocumentID`) +) + +CREATE TABLE `adventureworks_productinventory` ( + `ProductID` INT NOT NULL, + `LocationID` SMALLINT NOT NULL, + `Shelf` VARCHAR NOT NULL, + `Bin` TINYINT NOT NULL, + `Quantity` SMALLINT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`,`LocationID`) +) + +CREATE TABLE `adventureworks_productlistpricehistory` ( + `ProductID` INT NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME, + `ListPrice` DOUBLE NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`,`StartDate`) +) + +CREATE TABLE `adventureworks_productproductphoto` ( + `ProductID` INT NOT NULL, + `ProductPhotoID` INT NOT NULL, + `Primary` BIT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`,`ProductPhotoID`) +) + +CREATE TABLE `adventureworks_productreview` ( + `ProductReviewID` INT NOT NULL, + `ProductID` INT NOT NULL, + `ReviewerName` VARCHAR, + `ReviewDate` TIMESTAMP NOT NULL, + `EmailAddress` VARCHAR, + `Rating` INT NOT NULL, + `Comments` MEDIUMTEXT, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductReviewID`) +) + +CREATE TABLE `adventureworks_productvendor` ( + `ProductID` INT NOT NULL, + `VendorID` INT NOT NULL, + `AverageLeadTime` INT NOT NULL, + `StandardPrice` DOUBLE NOT NULL, + `LastReceiptCost` DOUBLE, + `LastReceiptDate` DATETIME, + `MinOrderQty` INT, + `MaxOrderQty` INT NOT NULL, + `OnOrderQty` INT, + `UnitMeasureCode` VARCHAR NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ProductID`,`VendorID`) +) + +CREATE TABLE `adventureworks_purchaseorderdetail` ( + `PurchaseOrderID` INT NOT NULL, + `PurchaseOrderDetailID` INT NOT NULL, + `DueDate` DATETIME NOT NULL, + `OrderQty` SMALLINT NOT NULL, + `ProductID` INT NOT NULL, + `UnitPrice` DOUBLE NOT NULL, + `LineTotal` DOUBLE NOT NULL, + `ReceivedQty` DECIMAL NOT NULL, + `RejectedQty` DECIMAL NOT NULL, + `StockedQty` DECIMAL NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`PurchaseOrderID`,`PurchaseOrderDetailID`) +) + +CREATE TABLE `adventureworks_salespersonquotahistory` ( + `SalesPersonID` INT NOT NULL, + `QuotaDate` DATETIME NOT NULL, + `SalesQuota` DOUBLE NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesPersonID`,`QuotaDate`) +) + +CREATE TABLE `adventureworks_shoppingcartitem` ( + `ShoppingCartItemID` INT NOT NULL, + `ShoppingCartID` VARCHAR NOT NULL, + `Quantity` INT NOT NULL, + `ProductID` INT NOT NULL, + `DateCreated` TIMESTAMP NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`ShoppingCartItemID`) +) + +CREATE TABLE `adventureworks_specialofferproduct` ( + `SpecialOfferID` INT NOT NULL, + `ProductID` INT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SpecialOfferID`,`ProductID`) +) + +CREATE TABLE `adventureworks_store` ( + `CustomerID` INT NOT NULL, + `Name` VARCHAR NOT NULL, + `SalesPersonID` INT, + `Demographics` TEXT, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CustomerID`) +) + +CREATE TABLE `adventureworks_storecontact` ( + `CustomerID` INT NOT NULL, + `ContactID` INT NOT NULL, + `ContactTypeID` INT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CustomerID`,`ContactID`) +) + +CREATE TABLE `adventureworks_transactionhistory` ( + `TransactionID` INT NOT NULL, + `ProductID` INT NOT NULL, + `ReferenceOrderID` INT NOT NULL, + `ReferenceOrderLineID` INT NOT NULL, + `TransactionDate` TIMESTAMP NOT NULL, + `TransactionType` VARCHAR NOT NULL, + `Quantity` INT NOT NULL, + `ActualCost` DOUBLE NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`TransactionID`) +) + +CREATE TABLE `adventureworks_transactionhistoryarchive` ( + `TransactionID` INT NOT NULL, + `ProductID` INT NOT NULL, + `ReferenceOrderID` INT NOT NULL, + `ReferenceOrderLineID` INT NOT NULL, + `TransactionDate` TIMESTAMP NOT NULL, + `TransactionType` VARCHAR NOT NULL, + `Quantity` INT NOT NULL, + `ActualCost` DOUBLE NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`TransactionID`) +) + +CREATE TABLE `adventureworks_workorder` ( + `WorkOrderID` INT NOT NULL, + `ProductID` INT NOT NULL, + `OrderQty` INT NOT NULL, + `StockedQty` INT NOT NULL, + `ScrappedQty` SMALLINT NOT NULL, + `StartDate` DATETIME NOT NULL, + `EndDate` DATETIME, + `DueDate` DATETIME NOT NULL, + `ScrapReasonID` SMALLINT, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`WorkOrderID`) +) + +CREATE TABLE `adventureworks_customeraddress` ( + `CustomerID` INT NOT NULL, + `AddressID` INT NOT NULL, + `AddressTypeID` INT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`CustomerID`,`AddressID`) +) + +CREATE TABLE `adventureworks_employeeaddress` ( + `EmployeeID` INT NOT NULL, + `AddressID` INT NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`EmployeeID`,`AddressID`) +) + +CREATE TABLE `adventureworks_salesorderheader` ( + `SalesOrderID` INT NOT NULL, + `RevisionNumber` TINYINT NOT NULL, + `OrderDate` TIMESTAMP NOT NULL, + `DueDate` DATETIME NOT NULL, + `ShipDate` DATETIME NOT NULL, + `Status` TINYINT NOT NULL, + `OnlineOrderFlag` BIT NOT NULL, + `SalesOrderNumber` VARCHAR NOT NULL, + `PurchaseOrderNumber` VARCHAR, + `AccountNumber` VARCHAR, + `CustomerID` INT NOT NULL, + `ContactID` INT NOT NULL, + `SalesPersonID` INT, + `TerritoryID` INT, + `BillToAddressID` INT NOT NULL, + `ShipToAddressID` INT NOT NULL, + `ShipMethodID` INT NOT NULL, + `CreditCardID` INT, + `CreditCardApprovalCode` VARCHAR, + `CurrencyRateID` INT, + `SubTotal` DOUBLE NOT NULL, + `TaxAmt` DOUBLE NOT NULL, + `Freight` DOUBLE NOT NULL, + `TotalDue` DOUBLE NOT NULL, + `Comment` VARCHAR, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesOrderID`) +) + +CREATE TABLE `adventureworks_vendoraddress` ( + `VendorID` INT NOT NULL, + `AddressID` INT NOT NULL, + `AddressTypeID` INT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`VendorID`,`AddressID`) +) + +CREATE TABLE `adventureworks_workorderrouting` ( + `WorkOrderID` INT NOT NULL, + `ProductID` INT NOT NULL, + `OperationSequence` SMALLINT NOT NULL, + `LocationID` SMALLINT NOT NULL, + `ScheduledStartDate` DATETIME NOT NULL, + `ScheduledEndDate` DATETIME NOT NULL, + `ActualStartDate` DATETIME, + `ActualEndDate` DATETIME, + `ActualResourceHrs` DECIMAL, + `PlannedCost` DOUBLE NOT NULL, + `ActualCost` DOUBLE, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`WorkOrderID`,`ProductID`,`OperationSequence`) +) + +CREATE TABLE `adventureworks_salesorderdetail` ( + `SalesOrderID` INT NOT NULL, + `SalesOrderDetailID` INT NOT NULL, + `CarrierTrackingNumber` VARCHAR, + `OrderQty` SMALLINT NOT NULL, + `ProductID` INT NOT NULL, + `SpecialOfferID` INT NOT NULL, + `UnitPrice` DOUBLE NOT NULL, + `UnitPriceDiscount` DOUBLE NOT NULL, + `LineTotal` DOUBLE NOT NULL, + `rowguid` VARBINARY NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesOrderID`,`SalesOrderDetailID`) +) + +CREATE TABLE `adventureworks_salesorderheadersalesreason` ( + `SalesOrderID` INT NOT NULL, + `SalesReasonID` INT NOT NULL, + `ModifiedDate` TIMESTAMP NOT NULL, + PRIMARY KEY(`SalesOrderID`,`SalesReasonID`) +) + diff --git a/examples/adventureworks_pg.sql b/examples/adventureworks_pg.sql new file mode 100644 index 0000000..b35849e --- /dev/null +++ b/examples/adventureworks_pg.sql @@ -0,0 +1,1853 @@ +/* +sql generated using: + * Package: little-mermaid-2-the-sql + * Version: 0.0.3 + * databaseInfo: postgres +*/ + +CREATE TABLE "humanresources_department" ( + "departmentid" serial NOT NULL, + "name" Name NOT NULL, + "groupname" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("departmentid") +) + +CREATE TABLE "humanresources_shift" ( + "shiftid" serial NOT NULL, + "name" Name NOT NULL, + "starttime" time NOT NULL, + "endtime" time NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("shiftid") +) + +CREATE TABLE "person_addresstype" ( + "addresstypeid" serial NOT NULL, + "name" Name NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("addresstypeid") +) + +CREATE TABLE "person_businessentity" ( + "businessentityid" serial NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid") +) + +CREATE TABLE "person_contacttype" ( + "contacttypeid" serial NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("contacttypeid") +) + +CREATE TABLE "person_countryregion" ( + "countryregioncode" varchar NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("countryregioncode") +) + +CREATE TABLE "person_phonenumbertype" ( + "phonenumbertypeid" serial NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("phonenumbertypeid") +) + +CREATE TABLE "production_culture" ( + "cultureid" bpchar NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("cultureid") +) + +CREATE TABLE "production_illustration" ( + "illustrationid" serial NOT NULL, + "diagram" xml, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("illustrationid") +) + +CREATE TABLE "production_location" ( + "locationid" serial NOT NULL, + "name" Name NOT NULL, + "costrate" numeric NOT NULL, + "availability" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("locationid") +) + +CREATE TABLE "production_productcategory" ( + "productcategoryid" serial NOT NULL, + "name" Name NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productcategoryid") +) + +CREATE TABLE "production_productdescription" ( + "productdescriptionid" serial NOT NULL, + "description" varchar NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productdescriptionid") +) + +CREATE TABLE "production_productmodel" ( + "productmodelid" serial NOT NULL, + "name" Name NOT NULL, + "catalogdescription" xml, + "instructions" xml, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productmodelid") +) + +CREATE TABLE "production_productphoto" ( + "productphotoid" serial NOT NULL, + "thumbnailphoto" bytea, + "thumbnailphotofilename" varchar, + "largephoto" bytea, + "largephotofilename" varchar, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productphotoid") +) + +CREATE TABLE "production_productreview" ( + "productreviewid" serial NOT NULL, + "productid" int(4) NOT NULL, + "reviewername" Name NOT NULL, + "reviewdate" timestamp NOT NULL, + "emailaddress" varchar NOT NULL, + "rating" int(4) NOT NULL, + "comments" varchar, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productreviewid") +) + +CREATE TABLE "production_scrapreason" ( + "scrapreasonid" serial NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("scrapreasonid") +) + +CREATE TABLE "production_transactionhistoryarchive" ( + "transactionid" int(4) NOT NULL, + "productid" int(4) NOT NULL, + "referenceorderid" int(4) NOT NULL, + "referenceorderlineid" int(4) NOT NULL, + "transactiondate" timestamp NOT NULL, + "transactiontype" bpchar NOT NULL, + "quantity" int(4) NOT NULL, + "actualcost" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("transactionid") +) + +CREATE TABLE "production_unitmeasure" ( + "unitmeasurecode" bpchar NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("unitmeasurecode") +) + +CREATE TABLE "purchasing_shipmethod" ( + "shipmethodid" serial NOT NULL, + "name" Name NOT NULL, + "shipbase" numeric NOT NULL, + "shiprate" numeric NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("shipmethodid") +) + +CREATE TABLE "sales_creditcard" ( + "creditcardid" serial NOT NULL, + "cardtype" varchar NOT NULL, + "cardnumber" varchar NOT NULL, + "expmonth" int(2) NOT NULL, + "expyear" int(2) NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("creditcardid") +) + +CREATE TABLE "sales_currency" ( + "currencycode" bpchar NOT NULL, + "name" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("currencycode") +) + +CREATE TABLE "sales_salesreason" ( + "salesreasonid" serial NOT NULL, + "name" Name NOT NULL, + "reasontype" Name NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("salesreasonid") +) + +CREATE TABLE "sales_specialoffer" ( + "specialofferid" serial NOT NULL, + "description" varchar NOT NULL, + "discountpct" numeric NOT NULL, + "type" varchar NOT NULL, + "category" varchar NOT NULL, + "startdate" timestamp NOT NULL, + "enddate" timestamp NOT NULL, + "minqty" int(4) NOT NULL, + "maxqty" int(4), + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("specialofferid") +) + +CREATE TABLE "person_person" ( + "businessentityid" int(4) NOT NULL, + "persontype" bpchar NOT NULL, + "namestyle" NameStyle NOT NULL, + "title" varchar, + "firstname" Name NOT NULL, + "middlename" Name, + "lastname" Name NOT NULL, + "suffix" varchar, + "emailpromotion" int(4) NOT NULL, + "additionalcontactinfo" xml, + "demographics" xml, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid"), + FOREIGN KEY ("person_person_businessentityid") REFERENCES "person_businessentity"("businessentityid") +) + +CREATE TABLE "production_productmodelillustration" ( + "productmodelid" int(4) NOT NULL, + "illustrationid" int(4) NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productmodelid","illustrationid"), + FOREIGN KEY ("illustrationid") REFERENCES "production_illustration"("illustrationid"), + FOREIGN KEY ("productmodelid") REFERENCES "production_productmodel"("productmodelid") +) + +CREATE TABLE "production_productmodelproductdescriptionculture" ( + "productmodelid" int(4) NOT NULL, + "productdescriptionid" int(4) NOT NULL, + "cultureid" bpchar NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productmodelid","productdescriptionid","cultureid"), + FOREIGN KEY ("cultureid") REFERENCES "production_culture"("cultureid"), + FOREIGN KEY ("productdescriptionid") REFERENCES "production_productdescription"("productdescriptionid"), + FOREIGN KEY ("productmodelid") REFERENCES "production_productmodel"("productmodelid") +) + +CREATE TABLE "production_productsubcategory" ( + "productsubcategoryid" serial NOT NULL, + "productcategoryid" int(4) NOT NULL, + "name" Name NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productsubcategoryid"), + FOREIGN KEY ("productcategoryid") REFERENCES "production_productcategory"("productcategoryid") +) + +CREATE TABLE "purchasing_vendor" ( + "businessentityid" int(4) NOT NULL, + "accountnumber" AccountNumber NOT NULL, + "name" Name NOT NULL, + "creditrating" int(2) NOT NULL, + "preferredvendorstatus" Flag NOT NULL, + "activeflag" Flag NOT NULL, + "purchasingwebserviceurl" varchar, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_businessentity"("businessentityid") +) + +CREATE TABLE "sales_countryregioncurrency" ( + "countryregioncode" varchar NOT NULL, + "currencycode" bpchar NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("countryregioncode","currencycode"), + FOREIGN KEY ("countryregioncode") REFERENCES "person_countryregion"("countryregioncode"), + FOREIGN KEY ("currencycode") REFERENCES "sales_currency"("currencycode") +) + +CREATE TABLE "sales_currencyrate" ( + "currencyrateid" serial NOT NULL, + "currencyratedate" timestamp NOT NULL, + "fromcurrencycode" bpchar NOT NULL, + "tocurrencycode" bpchar NOT NULL, + "averagerate" numeric NOT NULL, + "endofdayrate" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("currencyrateid"), + FOREIGN KEY ("fromcurrencycode") REFERENCES "sales_currency"("currencycode"), + FOREIGN KEY ("tocurrencycode") REFERENCES "sales_currency"("currencycode") +) + +CREATE TABLE "sales_salesterritory" ( + "territoryid" serial NOT NULL, + "name" Name NOT NULL, + "countryregioncode" varchar NOT NULL, + "group" varchar NOT NULL, + "salesytd" numeric NOT NULL, + "saleslastyear" numeric NOT NULL, + "costytd" numeric NOT NULL, + "costlastyear" numeric NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("territoryid"), + FOREIGN KEY ("countryregioncode") REFERENCES "person_countryregion"("countryregioncode") +) + +CREATE TABLE "humanresources_employee" ( + "businessentityid" int(4) NOT NULL, + "nationalidnumber" varchar NOT NULL, + "loginid" varchar NOT NULL, + "jobtitle" varchar NOT NULL, + "birthdate" date NOT NULL, + "maritalstatus" bpchar NOT NULL, + "gender" bpchar NOT NULL, + "hiredate" date NOT NULL, + "salariedflag" Flag NOT NULL, + "vacationhours" int(2) NOT NULL, + "sickleavehours" int(2) NOT NULL, + "currentflag" Flag NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + "organizationnode" varchar, + PRIMARY KEY("businessentityid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_person"("businessentityid") +) + +CREATE TABLE "person_businessentitycontact" ( + "businessentityid" int(4) NOT NULL, + "personid" int(4) NOT NULL, + "contacttypeid" int(4) NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","personid","contacttypeid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_businessentity"("businessentityid"), + FOREIGN KEY ("contacttypeid") REFERENCES "person_contacttype"("contacttypeid"), + FOREIGN KEY ("personid") REFERENCES "person_person"("businessentityid") +) + +CREATE TABLE "person_emailaddress" ( + "businessentityid" int(4) NOT NULL, + "emailaddressid" serial NOT NULL, + "emailaddress" varchar, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","emailaddressid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_person"("businessentityid") +) + +CREATE TABLE "person_password" ( + "businessentityid" int(4) NOT NULL, + "passwordhash" varchar NOT NULL, + "passwordsalt" varchar NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_person"("businessentityid") +) + +CREATE TABLE "person_personphone" ( + "businessentityid" int(4) NOT NULL, + "phonenumber" Phone NOT NULL, + "phonenumbertypeid" int(4) NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","phonenumber","phonenumbertypeid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_person"("businessentityid"), + FOREIGN KEY ("phonenumbertypeid") REFERENCES "person_phonenumbertype"("phonenumbertypeid") +) + +CREATE TABLE "person_stateprovince" ( + "stateprovinceid" serial NOT NULL, + "stateprovincecode" bpchar NOT NULL, + "countryregioncode" varchar NOT NULL, + "isonlystateprovinceflag" Flag NOT NULL, + "name" Name NOT NULL, + "territoryid" int(4) NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("stateprovinceid"), + FOREIGN KEY ("countryregioncode") REFERENCES "person_countryregion"("countryregioncode"), + FOREIGN KEY ("territoryid") REFERENCES "sales_salesterritory"("territoryid") +) + +CREATE TABLE "production_product" ( + "productid" serial NOT NULL, + "name" Name NOT NULL, + "productnumber" varchar NOT NULL, + "makeflag" Flag NOT NULL, + "finishedgoodsflag" Flag NOT NULL, + "color" varchar, + "safetystocklevel" int(2) NOT NULL, + "reorderpoint" int(2) NOT NULL, + "standardcost" numeric NOT NULL, + "listprice" numeric NOT NULL, + "size" varchar, + "sizeunitmeasurecode" bpchar, + "weightunitmeasurecode" bpchar, + "weight" numeric, + "daystomanufacture" int(4) NOT NULL, + "productline" bpchar, + "class" bpchar, + "style" bpchar, + "productsubcategoryid" int(4), + "productmodelid" int(4), + "sellstartdate" timestamp NOT NULL, + "sellenddate" timestamp, + "discontinueddate" timestamp, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productid"), + FOREIGN KEY ("productmodelid") REFERENCES "production_productmodel"("productmodelid"), + FOREIGN KEY ("productsubcategoryid") REFERENCES "production_productsubcategory"("productsubcategoryid"), + FOREIGN KEY ("sizeunitmeasurecode") REFERENCES "production_unitmeasure"("unitmeasurecode"), + FOREIGN KEY ("weightunitmeasurecode") REFERENCES "production_unitmeasure"("unitmeasurecode") +) + +CREATE TABLE "sales_personcreditcard" ( + "businessentityid" int(4) NOT NULL, + "creditcardid" int(4) NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","creditcardid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_person"("businessentityid"), + FOREIGN KEY ("creditcardid") REFERENCES "sales_creditcard"("creditcardid") +) + +CREATE TABLE "humanresources_employeedepartmenthistory" ( + "businessentityid" int(4) NOT NULL, + "departmentid" int(2) NOT NULL, + "shiftid" int(2) NOT NULL, + "startdate" date NOT NULL, + "enddate" date, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","departmentid","shiftid","startdate"), + FOREIGN KEY ("businessentityid") REFERENCES "humanresources_employee"("businessentityid"), + FOREIGN KEY ("departmentid") REFERENCES "humanresources_department"("departmentid"), + FOREIGN KEY ("shiftid") REFERENCES "humanresources_shift"("shiftid") +) + +CREATE TABLE "humanresources_employeepayhistory" ( + "businessentityid" int(4) NOT NULL, + "ratechangedate" timestamp NOT NULL, + "rate" numeric NOT NULL, + "payfrequency" int(2) NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","ratechangedate"), + FOREIGN KEY ("businessentityid") REFERENCES "humanresources_employee"("businessentityid") +) + +CREATE TABLE "humanresources_jobcandidate" ( + "jobcandidateid" serial NOT NULL, + "businessentityid" int(4), + "resume" xml, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("jobcandidateid"), + FOREIGN KEY ("businessentityid") REFERENCES "humanresources_employee"("businessentityid") +) + +CREATE TABLE "person_address" ( + "addressid" serial NOT NULL, + "addressline1" varchar NOT NULL, + "addressline2" varchar, + "city" varchar NOT NULL, + "stateprovinceid" int(4) NOT NULL, + "postalcode" varchar NOT NULL, + "spatiallocation" varchar, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("addressid"), + FOREIGN KEY ("stateprovinceid") REFERENCES "person_stateprovince"("stateprovinceid") +) + +CREATE TABLE "production_billofmaterials" ( + "billofmaterialsid" serial NOT NULL, + "productassemblyid" int(4), + "componentid" int(4) NOT NULL, + "startdate" timestamp NOT NULL, + "enddate" timestamp, + "unitmeasurecode" bpchar NOT NULL, + "bomlevel" int(2) NOT NULL, + "perassemblyqty" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("billofmaterialsid"), + FOREIGN KEY ("componentid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("productassemblyid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("unitmeasurecode") REFERENCES "production_unitmeasure"("unitmeasurecode") +) + +CREATE TABLE "production_document" ( + "title" varchar NOT NULL, + "owner" int(4) NOT NULL, + "folderflag" Flag NOT NULL, + "filename" varchar NOT NULL, + "fileextension" varchar, + "revision" bpchar NOT NULL, + "changenumber" int(4) NOT NULL, + "status" int(2) NOT NULL, + "documentsummary" text, + "document" bytea, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + "documentnode" varchar NOT NULL, + PRIMARY KEY("documentnode"), + FOREIGN KEY ("owner") REFERENCES "humanresources_employee"("businessentityid") +) + +CREATE TABLE "production_productcosthistory" ( + "productid" int(4) NOT NULL, + "startdate" timestamp NOT NULL, + "enddate" timestamp, + "standardcost" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productid","startdate"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid") +) + +CREATE TABLE "production_productinventory" ( + "productid" int(4) NOT NULL, + "locationid" int(2) NOT NULL, + "shelf" varchar NOT NULL, + "bin" int(2) NOT NULL, + "quantity" int(2) NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productid","locationid"), + FOREIGN KEY ("locationid") REFERENCES "production_location"("locationid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid") +) + +CREATE TABLE "production_productlistpricehistory" ( + "productid" int(4) NOT NULL, + "startdate" timestamp NOT NULL, + "enddate" timestamp, + "listprice" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productid","startdate"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid") +) + +CREATE TABLE "production_productproductphoto" ( + "productid" int(4) NOT NULL, + "productphotoid" int(4) NOT NULL, + "primary" Flag NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productid","productphotoid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("productphotoid") REFERENCES "production_productphoto"("productphotoid") +) + +CREATE TABLE "production_transactionhistory" ( + "transactionid" serial NOT NULL, + "productid" int(4) NOT NULL, + "referenceorderid" int(4) NOT NULL, + "referenceorderlineid" int(4) NOT NULL, + "transactiondate" timestamp NOT NULL, + "transactiontype" bpchar NOT NULL, + "quantity" int(4) NOT NULL, + "actualcost" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("transactionid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid") +) + +CREATE TABLE "production_workorder" ( + "workorderid" serial NOT NULL, + "productid" int(4) NOT NULL, + "orderqty" int(4) NOT NULL, + "scrappedqty" int(2) NOT NULL, + "startdate" timestamp NOT NULL, + "enddate" timestamp, + "duedate" timestamp NOT NULL, + "scrapreasonid" int(2), + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("workorderid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("scrapreasonid") REFERENCES "production_scrapreason"("scrapreasonid") +) + +CREATE TABLE "purchasing_productvendor" ( + "productid" int(4) NOT NULL, + "businessentityid" int(4) NOT NULL, + "averageleadtime" int(4) NOT NULL, + "standardprice" numeric NOT NULL, + "lastreceiptcost" numeric, + "lastreceiptdate" timestamp, + "minorderqty" int(4) NOT NULL, + "maxorderqty" int(4) NOT NULL, + "onorderqty" int(4), + "unitmeasurecode" bpchar NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("productid","businessentityid"), + FOREIGN KEY ("businessentityid") REFERENCES "purchasing_vendor"("businessentityid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("unitmeasurecode") REFERENCES "production_unitmeasure"("unitmeasurecode") +) + +CREATE TABLE "purchasing_purchaseorderheader" ( + "purchaseorderid" serial NOT NULL, + "revisionnumber" int(2) NOT NULL, + "status" int(2) NOT NULL, + "employeeid" int(4) NOT NULL, + "vendorid" int(4) NOT NULL, + "shipmethodid" int(4) NOT NULL, + "orderdate" timestamp NOT NULL, + "shipdate" timestamp, + "subtotal" numeric NOT NULL, + "taxamt" numeric NOT NULL, + "freight" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("purchaseorderid"), + FOREIGN KEY ("employeeid") REFERENCES "humanresources_employee"("businessentityid"), + FOREIGN KEY ("shipmethodid") REFERENCES "purchasing_shipmethod"("shipmethodid"), + FOREIGN KEY ("vendorid") REFERENCES "purchasing_vendor"("businessentityid") +) + +CREATE TABLE "sales_salesperson" ( + "businessentityid" int(4) NOT NULL, + "territoryid" int(4), + "salesquota" numeric, + "bonus" numeric NOT NULL, + "commissionpct" numeric NOT NULL, + "salesytd" numeric NOT NULL, + "saleslastyear" numeric NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid"), + FOREIGN KEY ("businessentityid") REFERENCES "humanresources_employee"("businessentityid"), + FOREIGN KEY ("territoryid") REFERENCES "sales_salesterritory"("territoryid") +) + +CREATE TABLE "sales_salestaxrate" ( + "salestaxrateid" serial NOT NULL, + "stateprovinceid" int(4) NOT NULL, + "taxtype" int(2) NOT NULL, + "taxrate" numeric NOT NULL, + "name" Name NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("salestaxrateid"), + FOREIGN KEY ("stateprovinceid") REFERENCES "person_stateprovince"("stateprovinceid") +) + +CREATE TABLE "sales_shoppingcartitem" ( + "shoppingcartitemid" serial NOT NULL, + "shoppingcartid" varchar NOT NULL, + "quantity" int(4) NOT NULL, + "productid" int(4) NOT NULL, + "datecreated" timestamp NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("shoppingcartitemid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid") +) + +CREATE TABLE "sales_specialofferproduct" ( + "specialofferid" int(4) NOT NULL, + "productid" int(4) NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("specialofferid","productid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("specialofferid") REFERENCES "sales_specialoffer"("specialofferid") +) + +CREATE TABLE "person_businessentityaddress" ( + "businessentityid" int(4) NOT NULL, + "addressid" int(4) NOT NULL, + "addresstypeid" int(4) NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","addressid","addresstypeid"), + FOREIGN KEY ("addressid") REFERENCES "person_address"("addressid"), + FOREIGN KEY ("addresstypeid") REFERENCES "person_addresstype"("addresstypeid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_businessentity"("businessentityid") +) + +CREATE TABLE "production_productdocument" ( + "productid" int(4) NOT NULL, + "modifieddate" timestamp NOT NULL, + "documentnode" varchar NOT NULL, + PRIMARY KEY("productid","documentnode"), + FOREIGN KEY ("documentnode") REFERENCES "production_document"("documentnode"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid") +) + +CREATE TABLE "production_workorderrouting" ( + "workorderid" int(4) NOT NULL, + "productid" int(4) NOT NULL, + "operationsequence" int(2) NOT NULL, + "locationid" int(2) NOT NULL, + "scheduledstartdate" timestamp NOT NULL, + "scheduledenddate" timestamp NOT NULL, + "actualstartdate" timestamp, + "actualenddate" timestamp, + "actualresourcehrs" numeric, + "plannedcost" numeric NOT NULL, + "actualcost" numeric, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("workorderid","productid","operationsequence"), + FOREIGN KEY ("locationid") REFERENCES "production_location"("locationid"), + FOREIGN KEY ("workorderid") REFERENCES "production_workorder"("workorderid") +) + +CREATE TABLE "purchasing_purchaseorderdetail" ( + "purchaseorderid" int(4) NOT NULL, + "purchaseorderdetailid" serial NOT NULL, + "duedate" timestamp NOT NULL, + "orderqty" int(2) NOT NULL, + "productid" int(4) NOT NULL, + "unitprice" numeric NOT NULL, + "receivedqty" numeric NOT NULL, + "rejectedqty" numeric NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("purchaseorderid","purchaseorderdetailid"), + FOREIGN KEY ("productid") REFERENCES "production_product"("productid"), + FOREIGN KEY ("purchaseorderid") REFERENCES "purchasing_purchaseorderheader"("purchaseorderid") +) + +CREATE TABLE "sales_salespersonquotahistory" ( + "businessentityid" int(4) NOT NULL, + "quotadate" timestamp NOT NULL, + "salesquota" numeric NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","quotadate"), + FOREIGN KEY ("businessentityid") REFERENCES "sales_salesperson"("businessentityid") +) + +CREATE TABLE "sales_salesterritoryhistory" ( + "businessentityid" int(4) NOT NULL, + "territoryid" int(4) NOT NULL, + "startdate" timestamp NOT NULL, + "enddate" timestamp, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid","territoryid","startdate"), + FOREIGN KEY ("businessentityid") REFERENCES "sales_salesperson"("businessentityid"), + FOREIGN KEY ("territoryid") REFERENCES "sales_salesterritory"("territoryid") +) + +CREATE TABLE "sales_store" ( + "businessentityid" int(4) NOT NULL, + "name" Name NOT NULL, + "salespersonid" int(4), + "demographics" xml, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("businessentityid"), + FOREIGN KEY ("businessentityid") REFERENCES "person_businessentity"("businessentityid"), + FOREIGN KEY ("salespersonid") REFERENCES "sales_salesperson"("businessentityid") +) + +CREATE TABLE "sales_customer" ( + "customerid" serial NOT NULL, + "personid" int(4), + "storeid" int(4), + "territoryid" int(4), + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("customerid"), + FOREIGN KEY ("personid") REFERENCES "person_person"("businessentityid"), + FOREIGN KEY ("storeid") REFERENCES "sales_store"("businessentityid"), + FOREIGN KEY ("territoryid") REFERENCES "sales_salesterritory"("territoryid") +) + +CREATE TABLE "sales_salesorderheader" ( + "salesorderid" serial NOT NULL, + "revisionnumber" int(2) NOT NULL, + "orderdate" timestamp NOT NULL, + "duedate" timestamp NOT NULL, + "shipdate" timestamp, + "status" int(2) NOT NULL, + "onlineorderflag" Flag NOT NULL, + "purchaseordernumber" OrderNumber, + "accountnumber" AccountNumber, + "customerid" int(4) NOT NULL, + "salespersonid" int(4), + "territoryid" int(4), + "billtoaddressid" int(4) NOT NULL, + "shiptoaddressid" int(4) NOT NULL, + "shipmethodid" int(4) NOT NULL, + "creditcardid" int(4), + "creditcardapprovalcode" varchar, + "currencyrateid" int(4), + "subtotal" numeric NOT NULL, + "taxamt" numeric NOT NULL, + "freight" numeric NOT NULL, + "totaldue" numeric, + "comment" varchar, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("salesorderid"), + FOREIGN KEY ("billtoaddressid") REFERENCES "person_address"("addressid"), + FOREIGN KEY ("creditcardid") REFERENCES "sales_creditcard"("creditcardid"), + FOREIGN KEY ("currencyrateid") REFERENCES "sales_currencyrate"("currencyrateid"), + FOREIGN KEY ("customerid") REFERENCES "sales_customer"("customerid"), + FOREIGN KEY ("salespersonid") REFERENCES "sales_salesperson"("businessentityid"), + FOREIGN KEY ("shipmethodid") REFERENCES "purchasing_shipmethod"("shipmethodid"), + FOREIGN KEY ("shiptoaddressid") REFERENCES "person_address"("addressid"), + FOREIGN KEY ("territoryid") REFERENCES "sales_salesterritory"("territoryid") +) + +CREATE TABLE "sales_salesorderdetail" ( + "salesorderid" int(4) NOT NULL, + "salesorderdetailid" serial NOT NULL, + "carriertrackingnumber" varchar, + "orderqty" int(2) NOT NULL, + "productid" int(4) NOT NULL, + "specialofferid" int(4) NOT NULL, + "unitprice" numeric NOT NULL, + "unitpricediscount" numeric NOT NULL, + "rowguid" uuid NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("salesorderid","salesorderdetailid"), + FOREIGN KEY ("salesorderid") REFERENCES "sales_salesorderheader"("salesorderid"), + FOREIGN KEY ("specialofferid, sales_salesorderdetail") REFERENCES "sales_specialofferproduct"("specialofferid, sales_specialofferproduct") +) + +CREATE TABLE "sales_salesorderheadersalesreason" ( + "salesorderid" int(4) NOT NULL, + "salesreasonid" int(4) NOT NULL, + "modifieddate" timestamp NOT NULL, + PRIMARY KEY("salesorderid","salesreasonid"), + FOREIGN KEY ("salesorderid") REFERENCES "sales_salesorderheader"("salesorderid"), + FOREIGN KEY ("salesreasonid") REFERENCES "sales_salesreason"("salesreasonid") +) + +CREATE TABLE "hr_d" ( + "id" int(4), + "departmentid" int(4), + "name" Name, + "groupname" Name, + "modifieddate" timestamp +) + +CREATE TABLE "hr_e" ( + "id" int(4), + "businessentityid" int(4), + "nationalidnumber" varchar, + "loginid" varchar, + "jobtitle" varchar, + "birthdate" date, + "maritalstatus" bpchar, + "gender" bpchar, + "hiredate" date, + "salariedflag" Flag NOT NULL, + "vacationhours" int(2), + "sickleavehours" int(2), + "currentflag" Flag NOT NULL, + "rowguid" uuid, + "modifieddate" timestamp, + "organizationnode" varchar +) + +CREATE TABLE "hr_edh" ( + "id" int(4), + "businessentityid" int(4), + "departmentid" int(2), + "shiftid" int(2), + "startdate" date, + "enddate" date, + "modifieddate" timestamp +) + +CREATE TABLE "hr_eph" ( + "id" int(4), + "businessentityid" int(4), + "ratechangedate" timestamp, + "rate" numeric, + "payfrequency" int(2), + "modifieddate" timestamp +) + +CREATE TABLE "hr_jc" ( + "id" int(4), + "jobcandidateid" int(4), + "businessentityid" int(4), + "resume" xml, + "modifieddate" timestamp +) + +CREATE TABLE "hr_s" ( + "id" int(4), + "shiftid" int(4), + "name" Name, + "starttime" time, + "endtime" time, + "modifieddate" timestamp +) + +CREATE TABLE "humanresources_vemployee" ( + "businessentityid" int(4), + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "jobtitle" varchar, + "phonenumber" Phone, + "phonenumbertype" Name, + "emailaddress" varchar, + "emailpromotion" int(4), + "addressline1" varchar, + "addressline2" varchar, + "city" varchar, + "stateprovincename" Name, + "postalcode" varchar, + "countryregionname" Name, + "additionalcontactinfo" xml +) + +CREATE TABLE "humanresources_vemployeedepartment" ( + "businessentityid" int(4), + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "jobtitle" varchar, + "department" Name, + "groupname" Name, + "startdate" date +) + +CREATE TABLE "humanresources_vemployeedepartmenthistory" ( + "businessentityid" int(4), + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "shift" Name, + "department" Name, + "groupname" Name, + "startdate" date, + "enddate" date +) + +CREATE TABLE "humanresources_vjobcandidate" ( + "jobcandidateid" int(4), + "businessentityid" int(4), + "Name.Prefix" varchar, + "Name.First" varchar, + "Name.Middle" varchar, + "Name.Last" varchar, + "Name.Suffix" varchar, + "Skills" varchar, + "Addr.Type" varchar, + "Addr.Loc.CountryRegion" varchar, + "Addr.Loc.State" varchar, + "Addr.Loc.City" varchar, + "Addr.PostalCode" varchar, + "EMail" varchar, + "WebSite" varchar, + "modifieddate" timestamp +) + +CREATE TABLE "humanresources_vjobcandidateeducation" ( + "jobcandidateid" int(4), + "Edu.Level" varchar, + "Edu.StartDate" date, + "Edu.EndDate" date, + "Edu.Degree" varchar, + "Edu.Major" varchar, + "Edu.Minor" varchar, + "Edu.GPA" varchar, + "Edu.GPAScale" varchar, + "Edu.School" varchar, + "Edu.Loc.CountryRegion" varchar, + "Edu.Loc.State" varchar, + "Edu.Loc.City" varchar +) + +CREATE TABLE "humanresources_vjobcandidateemployment" ( + "jobcandidateid" int(4), + "Emp.StartDate" date, + "Emp.EndDate" date, + "Emp.OrgName" varchar, + "Emp.JobTitle" varchar, + "Emp.Responsibility" varchar, + "Emp.FunctionCategory" varchar, + "Emp.IndustryCategory" varchar, + "Emp.Loc.CountryRegion" varchar, + "Emp.Loc.State" varchar, + "Emp.Loc.City" varchar +) + +CREATE TABLE "pe_a" ( + "id" int(4), + "addressid" int(4), + "addressline1" varchar, + "addressline2" varchar, + "city" varchar, + "stateprovinceid" int(4), + "postalcode" varchar, + "spatiallocation" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_at" ( + "id" int(4), + "addresstypeid" int(4), + "name" Name, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_be" ( + "id" int(4), + "businessentityid" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_bea" ( + "id" int(4), + "businessentityid" int(4), + "addressid" int(4), + "addresstypeid" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_bec" ( + "id" int(4), + "businessentityid" int(4), + "personid" int(4), + "contacttypeid" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_cr" ( + "countryregioncode" varchar, + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "pe_ct" ( + "id" int(4), + "contacttypeid" int(4), + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "pe_e" ( + "id" int(4), + "businessentityid" int(4), + "emailaddressid" int(4), + "emailaddress" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_p" ( + "id" int(4), + "businessentityid" int(4), + "persontype" bpchar, + "namestyle" NameStyle NOT NULL, + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "emailpromotion" int(4), + "additionalcontactinfo" xml, + "demographics" xml, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_pa" ( + "id" int(4), + "businessentityid" int(4), + "passwordhash" varchar, + "passwordsalt" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pe_pnt" ( + "id" int(4), + "phonenumbertypeid" int(4), + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "pe_pp" ( + "id" int(4), + "businessentityid" int(4), + "phonenumber" Phone, + "phonenumbertypeid" int(4), + "modifieddate" timestamp +) + +CREATE TABLE "pe_sp" ( + "id" int(4), + "stateprovinceid" int(4), + "stateprovincecode" bpchar, + "countryregioncode" varchar, + "isonlystateprovinceflag" Flag NOT NULL, + "name" Name, + "territoryid" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "person_vadditionalcontactinfo" ( + "businessentityid" int(4), + "firstname" Name, + "middlename" Name, + "lastname" Name, + "telephonenumber" xml, + "telephonespecialinstructions" text, + "street" xml, + "city" xml, + "stateprovince" xml, + "postalcode" xml, + "countryregion" xml, + "homeaddressspecialinstructions" xml, + "emailaddress" xml, + "emailspecialinstructions" text, + "emailtelephonenumber" xml, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_bom" ( + "id" int(4), + "billofmaterialsid" int(4), + "productassemblyid" int(4), + "componentid" int(4), + "startdate" timestamp, + "enddate" timestamp, + "unitmeasurecode" bpchar, + "bomlevel" int(2), + "perassemblyqty" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pr_c" ( + "id" bpchar, + "cultureid" bpchar, + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "pr_d" ( + "title" varchar, + "owner" int(4), + "folderflag" Flag NOT NULL, + "filename" varchar, + "fileextension" varchar, + "revision" bpchar, + "changenumber" int(4), + "status" int(2), + "documentsummary" text, + "document" bytea, + "rowguid" uuid, + "modifieddate" timestamp, + "documentnode" varchar +) + +CREATE TABLE "pr_i" ( + "id" int(4), + "illustrationid" int(4), + "diagram" xml, + "modifieddate" timestamp +) + +CREATE TABLE "pr_l" ( + "id" int(4), + "locationid" int(4), + "name" Name, + "costrate" numeric, + "availability" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pr_p" ( + "id" int(4), + "productid" int(4), + "name" Name, + "productnumber" varchar, + "makeflag" Flag NOT NULL, + "finishedgoodsflag" Flag NOT NULL, + "color" varchar, + "safetystocklevel" int(2), + "reorderpoint" int(2), + "standardcost" numeric, + "listprice" numeric, + "size" varchar, + "sizeunitmeasurecode" bpchar, + "weightunitmeasurecode" bpchar, + "weight" numeric, + "daystomanufacture" int(4), + "productline" bpchar, + "class" bpchar, + "style" bpchar, + "productsubcategoryid" int(4), + "productmodelid" int(4), + "sellstartdate" timestamp, + "sellenddate" timestamp, + "discontinueddate" timestamp, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pc" ( + "id" int(4), + "productcategoryid" int(4), + "name" Name, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pch" ( + "id" int(4), + "productid" int(4), + "startdate" timestamp, + "enddate" timestamp, + "standardcost" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pd" ( + "id" int(4), + "productdescriptionid" int(4), + "description" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pdoc" ( + "id" int(4), + "productid" int(4), + "modifieddate" timestamp, + "documentnode" varchar +) + +CREATE TABLE "pr_pi" ( + "id" int(4), + "productid" int(4), + "locationid" int(2), + "shelf" varchar, + "bin" int(2), + "quantity" int(2), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_plph" ( + "id" int(4), + "productid" int(4), + "startdate" timestamp, + "enddate" timestamp, + "listprice" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pm" ( + "id" int(4), + "productmodelid" int(4), + "name" Name, + "catalogdescription" xml, + "instructions" xml, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pmi" ( + "productmodelid" int(4), + "illustrationid" int(4), + "modifieddate" timestamp +) + +CREATE TABLE "pr_pmpdc" ( + "productmodelid" int(4), + "productdescriptionid" int(4), + "cultureid" bpchar, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pp" ( + "id" int(4), + "productphotoid" int(4), + "thumbnailphoto" bytea, + "thumbnailphotofilename" varchar, + "largephoto" bytea, + "largephotofilename" varchar, + "modifieddate" timestamp +) + +CREATE TABLE "pr_ppp" ( + "productid" int(4), + "productphotoid" int(4), + "primary" Flag NOT NULL, + "modifieddate" timestamp +) + +CREATE TABLE "pr_pr" ( + "id" int(4), + "productreviewid" int(4), + "productid" int(4), + "reviewername" Name, + "reviewdate" timestamp, + "emailaddress" varchar, + "rating" int(4), + "comments" varchar, + "modifieddate" timestamp +) + +CREATE TABLE "pr_psc" ( + "id" int(4), + "productsubcategoryid" int(4), + "productcategoryid" int(4), + "name" Name, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pr_sr" ( + "id" int(4), + "scrapreasonid" int(4), + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "pr_th" ( + "id" int(4), + "transactionid" int(4), + "productid" int(4), + "referenceorderid" int(4), + "referenceorderlineid" int(4), + "transactiondate" timestamp, + "transactiontype" bpchar, + "quantity" int(4), + "actualcost" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pr_tha" ( + "id" int(4), + "transactionid" int(4), + "productid" int(4), + "referenceorderid" int(4), + "referenceorderlineid" int(4), + "transactiondate" timestamp, + "transactiontype" bpchar, + "quantity" int(4), + "actualcost" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pr_um" ( + "id" bpchar, + "unitmeasurecode" bpchar, + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "pr_w" ( + "id" int(4), + "workorderid" int(4), + "productid" int(4), + "orderqty" int(4), + "scrappedqty" int(2), + "startdate" timestamp, + "enddate" timestamp, + "duedate" timestamp, + "scrapreasonid" int(2), + "modifieddate" timestamp +) + +CREATE TABLE "pr_wr" ( + "id" int(4), + "workorderid" int(4), + "productid" int(4), + "operationsequence" int(2), + "locationid" int(2), + "scheduledstartdate" timestamp, + "scheduledenddate" timestamp, + "actualstartdate" timestamp, + "actualenddate" timestamp, + "actualresourcehrs" numeric, + "plannedcost" numeric, + "actualcost" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "production_vproductmodelcatalogdescription" ( + "productmodelid" int(4), + "name" Name, + "Summary" varchar, + "manufacturer" varchar, + "copyright" varchar, + "producturl" varchar, + "warrantyperiod" varchar, + "warrantydescription" varchar, + "noofyears" varchar, + "maintenancedescription" varchar, + "wheel" varchar, + "saddle" varchar, + "pedal" varchar, + "bikeframe" varchar, + "crankset" varchar, + "pictureangle" varchar, + "picturesize" varchar, + "productphotoid" varchar, + "material" varchar, + "color" varchar, + "productline" varchar, + "style" varchar, + "riderexperience" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "production_vproductmodelinstructions" ( + "productmodelid" int(4), + "name" Name, + "instructions" varchar, + "LocationID" int(4), + "SetupHours" numeric, + "MachineHours" numeric, + "LaborHours" numeric, + "LotSize" int(4), + "Step" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pu_pod" ( + "id" int(4), + "purchaseorderid" int(4), + "purchaseorderdetailid" int(4), + "duedate" timestamp, + "orderqty" int(2), + "productid" int(4), + "unitprice" numeric, + "receivedqty" numeric, + "rejectedqty" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pu_poh" ( + "id" int(4), + "purchaseorderid" int(4), + "revisionnumber" int(2), + "status" int(2), + "employeeid" int(4), + "vendorid" int(4), + "shipmethodid" int(4), + "orderdate" timestamp, + "shipdate" timestamp, + "subtotal" numeric, + "taxamt" numeric, + "freight" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "pu_pv" ( + "id" int(4), + "productid" int(4), + "businessentityid" int(4), + "averageleadtime" int(4), + "standardprice" numeric, + "lastreceiptcost" numeric, + "lastreceiptdate" timestamp, + "minorderqty" int(4), + "maxorderqty" int(4), + "onorderqty" int(4), + "unitmeasurecode" bpchar, + "modifieddate" timestamp +) + +CREATE TABLE "pu_sm" ( + "id" int(4), + "shipmethodid" int(4), + "name" Name, + "shipbase" numeric, + "shiprate" numeric, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "pu_v" ( + "id" int(4), + "businessentityid" int(4), + "accountnumber" AccountNumber, + "name" Name, + "creditrating" int(2), + "preferredvendorstatus" Flag NOT NULL, + "activeflag" Flag NOT NULL, + "purchasingwebserviceurl" varchar, + "modifieddate" timestamp +) + +CREATE TABLE "purchasing_vvendorwithaddresses" ( + "businessentityid" int(4), + "name" Name, + "addresstype" Name, + "addressline1" varchar, + "addressline2" varchar, + "city" varchar, + "stateprovincename" Name, + "postalcode" varchar, + "countryregionname" Name +) + +CREATE TABLE "purchasing_vvendorwithcontacts" ( + "businessentityid" int(4), + "name" Name, + "contacttype" Name, + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "phonenumber" Phone, + "phonenumbertype" Name, + "emailaddress" varchar, + "emailpromotion" int(4) +) + +CREATE TABLE "sa_c" ( + "id" int(4), + "customerid" int(4), + "personid" int(4), + "storeid" int(4), + "territoryid" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_cc" ( + "id" int(4), + "creditcardid" int(4), + "cardtype" varchar, + "cardnumber" varchar, + "expmonth" int(2), + "expyear" int(2), + "modifieddate" timestamp +) + +CREATE TABLE "sa_cr" ( + "currencyrateid" int(4), + "currencyratedate" timestamp, + "fromcurrencycode" bpchar, + "tocurrencycode" bpchar, + "averagerate" numeric, + "endofdayrate" numeric, + "modifieddate" timestamp +) + +CREATE TABLE "sa_crc" ( + "countryregioncode" varchar, + "currencycode" bpchar, + "modifieddate" timestamp +) + +CREATE TABLE "sa_cu" ( + "id" bpchar, + "currencycode" bpchar, + "name" Name, + "modifieddate" timestamp +) + +CREATE TABLE "sa_pcc" ( + "id" int(4), + "businessentityid" int(4), + "creditcardid" int(4), + "modifieddate" timestamp +) + +CREATE TABLE "sa_s" ( + "id" int(4), + "businessentityid" int(4), + "name" Name, + "salespersonid" int(4), + "demographics" xml, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_sci" ( + "id" int(4), + "shoppingcartitemid" int(4), + "shoppingcartid" varchar, + "quantity" int(4), + "productid" int(4), + "datecreated" timestamp, + "modifieddate" timestamp +) + +CREATE TABLE "sa_so" ( + "id" int(4), + "specialofferid" int(4), + "description" varchar, + "discountpct" numeric, + "type" varchar, + "category" varchar, + "startdate" timestamp, + "enddate" timestamp, + "minqty" int(4), + "maxqty" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_sod" ( + "id" int(4), + "salesorderid" int(4), + "salesorderdetailid" int(4), + "carriertrackingnumber" varchar, + "orderqty" int(2), + "productid" int(4), + "specialofferid" int(4), + "unitprice" numeric, + "unitpricediscount" numeric, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_soh" ( + "id" int(4), + "salesorderid" int(4), + "revisionnumber" int(2), + "orderdate" timestamp, + "duedate" timestamp, + "shipdate" timestamp, + "status" int(2), + "onlineorderflag" Flag NOT NULL, + "purchaseordernumber" OrderNumber, + "accountnumber" AccountNumber, + "customerid" int(4), + "salespersonid" int(4), + "territoryid" int(4), + "billtoaddressid" int(4), + "shiptoaddressid" int(4), + "shipmethodid" int(4), + "creditcardid" int(4), + "creditcardapprovalcode" varchar, + "currencyrateid" int(4), + "subtotal" numeric, + "taxamt" numeric, + "freight" numeric, + "totaldue" numeric, + "comment" varchar, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_sohsr" ( + "salesorderid" int(4), + "salesreasonid" int(4), + "modifieddate" timestamp +) + +CREATE TABLE "sa_sop" ( + "id" int(4), + "specialofferid" int(4), + "productid" int(4), + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_sp" ( + "id" int(4), + "businessentityid" int(4), + "territoryid" int(4), + "salesquota" numeric, + "bonus" numeric, + "commissionpct" numeric, + "salesytd" numeric, + "saleslastyear" numeric, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_spqh" ( + "id" int(4), + "businessentityid" int(4), + "quotadate" timestamp, + "salesquota" numeric, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_sr" ( + "id" int(4), + "salesreasonid" int(4), + "name" Name, + "reasontype" Name, + "modifieddate" timestamp +) + +CREATE TABLE "sa_st" ( + "id" int(4), + "territoryid" int(4), + "name" Name, + "countryregioncode" varchar, + "group" varchar, + "salesytd" numeric, + "saleslastyear" numeric, + "costytd" numeric, + "costlastyear" numeric, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_sth" ( + "id" int(4), + "businessentityid" int(4), + "territoryid" int(4), + "startdate" timestamp, + "enddate" timestamp, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sa_tr" ( + "id" int(4), + "salestaxrateid" int(4), + "stateprovinceid" int(4), + "taxtype" int(2), + "taxrate" numeric, + "name" Name, + "rowguid" uuid, + "modifieddate" timestamp +) + +CREATE TABLE "sales_vindividualcustomer" ( + "businessentityid" int(4), + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "phonenumber" Phone, + "phonenumbertype" Name, + "emailaddress" varchar, + "emailpromotion" int(4), + "addresstype" Name, + "addressline1" varchar, + "addressline2" varchar, + "city" varchar, + "stateprovincename" Name, + "postalcode" varchar, + "countryregionname" Name, + "demographics" xml +) + +CREATE TABLE "sales_vpersondemographics" ( + "businessentityid" int(4), + "totalpurchaseytd" money, + "datefirstpurchase" date, + "birthdate" date, + "maritalstatus" varchar, + "yearlyincome" varchar, + "gender" varchar, + "totalchildren" int(4), + "numberchildrenathome" int(4), + "education" varchar, + "occupation" varchar, + "homeownerflag" bool, + "numbercarsowned" int(4) +) + +CREATE TABLE "sales_vsalesperson" ( + "businessentityid" int(4), + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "jobtitle" varchar, + "phonenumber" Phone, + "phonenumbertype" Name, + "emailaddress" varchar, + "emailpromotion" int(4), + "addressline1" varchar, + "addressline2" varchar, + "city" varchar, + "stateprovincename" Name, + "postalcode" varchar, + "countryregionname" Name, + "territoryname" Name, + "territorygroup" varchar, + "salesquota" numeric, + "salesytd" numeric, + "saleslastyear" numeric +) + +CREATE TABLE "sales_vsalespersonsalesbyfiscalyears" ( + "SalesPersonID" int(4), + "FullName" text, + "JobTitle" text, + "SalesTerritory" text, + "2012" numeric, + "2013" numeric, + "2014" numeric +) + +CREATE TABLE "sales_vsalespersonsalesbyfiscalyearsdata" ( + "salespersonid" int(4), + "fullname" text, + "jobtitle" varchar, + "salesterritory" Name, + "salestotal" numeric, + "fiscalyear" numeric +) + +CREATE TABLE "sales_vstorewithaddresses" ( + "businessentityid" int(4), + "name" Name, + "addresstype" Name, + "addressline1" varchar, + "addressline2" varchar, + "city" varchar, + "stateprovincename" Name, + "postalcode" varchar, + "countryregionname" Name +) + +CREATE TABLE "sales_vstorewithcontacts" ( + "businessentityid" int(4), + "name" Name, + "contacttype" Name, + "title" varchar, + "firstname" Name, + "middlename" Name, + "lastname" Name, + "suffix" varchar, + "phonenumber" Phone, + "phonenumbertype" Name, + "emailaddress" varchar, + "emailpromotion" int(4) +) + +CREATE TABLE "sales_vstorewithdemographics" ( + "businessentityid" int(4), + "name" Name, + "AnnualSales" money, + "AnnualRevenue" money, + "BankName" varchar, + "BusinessType" varchar, + "YearOpened" int(4), + "Specialty" varchar, + "SquareFeet" int(4), + "Brands" varchar, + "Internet" varchar, + "NumberEmployees" int(4) +) + diff --git a/src/index.ts b/src/index.ts index fdf8c91..8253a65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,17 @@ -import { DbEntity } from "./types"; +import { + DatabaseModel, + ForeignKeyModel, + PrimaryKeyModel, + PropertyModel, + TableModel, +} from "./types"; /** * Main Parser class */ export class SqlSimpleParser { private dialect: string; - private entities: DbEntity[] = []; + private tableList: TableModel[] = []; /** * Parsed statements. @@ -37,7 +43,16 @@ export class SqlSimpleParser { dialect: "mysql" | "sqlite" | "postgres" | "sqlserver" = "sqlite" ) { this.dialect = dialect; + this.MODE_SQLSERVER = + this.dialect !== undefined && + this.dialect !== null && + this.dialect == this.SQLServer; } + private exportedTables = 0; + private SQLServer = "sqlserver"; + private MODE_SQLSERVER = false; + private foreignKeyList: ForeignKeyModel[] = []; + private primaryKeyList: PrimaryKeyModel[] = []; /** * Feed chunk of string into parser. * @@ -45,42 +60,487 @@ export class SqlSimpleParser { */ feed(chunk: string): SqlSimpleParser { // - let i; - let char; - let parsed = ""; - let lastStatementIndex = 0; + var lines = chunk.split("\n"), + dx = 0, + tableCell = null, + cells = [], + exportedTables = 0, + // tableList = [], + foreignKeyList = [], + rowCell = null; - for (i = 0; i < chunk.length; i += 1) { - char = chunk[i]; - parsed += char; + var currentTableModel = null; + //Parse SQL to objects + for (var i = 0; i < lines.length; i++) { + rowCell = null; - if (char === "\\") { - this.escaped = !this.escaped; - } else { - if (!this.escaped && SqlSimpleParser.isQuoteChar(char)) { - if (this.quoted) { - if (this.quoted === char) { - this.quoted = ""; + var tmp = lines[i].trim(); + + var propertyRow = tmp.substring(0, 12).toLowerCase(); + + //Parse Table + if (propertyRow === "create table") { + //Parse row + var name = tmp.substring(12).trim(); + + //Parse Table Name + name = this.ParseTableName(name); + + if (currentTableModel !== null) { + //Add table to the list + this.tableList.push(currentTableModel); + } + + //Create Table + currentTableModel = this.CreateTable(name); + } + // Parse Properties + else if ( + tmp !== "(" && + currentTableModel != null && + propertyRow !== "alter table " + ) { + //Parse the row + var name = tmp.substring( + 0, + tmp.charAt(tmp.length - 1) === "," ? tmp.length - 1 : tmp.length + ); + + //Attempt to get the Key Type + var propertyType = name.substring(0, 11).toLowerCase(); + + //Add special constraints + if (this.MODE_SQLSERVER) { + if ( + tmp.indexOf("CONSTRAINT") !== -1 && + tmp.indexOf("PRIMARY KEY") !== -1 + ) { + propertyType = "constrain primary key"; + } + + if ( + tmp.indexOf("CONSTRAINT") !== -1 && + tmp.indexOf("FOREIGN KEY") !== -1 + ) { + propertyType = "constrain foreign key"; + } + } + + //Verify if this is a property that doesn't have a relationship (One minute of silence for the property) + var normalProperty = + propertyType !== "primary key" && + propertyType !== "foreign key" && + propertyType !== "constrain primary key" && + propertyType !== "constrain foreign key"; + + //Parse properties that don't have relationships + if (normalProperty) { + if (name === "" || name === "" || name === ");") { + continue; + } + + if (this.MODE_SQLSERVER) { + if ( + name.indexOf(" ASC") !== -1 || + name.indexOf(" DESC") !== -1 || + name.indexOf(" EXEC") !== -1 || + name.indexOf(" WITH") !== -1 || + name.indexOf(" ON") !== -1 || + name.indexOf(" ALTER") !== -1 || + name.indexOf("/*") !== -1 || + name.indexOf(" CONSTRAIN") !== -1 || + name.indexOf(" SET") !== -1 || + name.indexOf(" NONCLUSTERED") !== -1 || + name.indexOf(" GO") !== -1 || + name.indexOf(" REFERENCES") !== -1 + ) { + continue; + } + //Get delimiter of column name + var firstSpaceIndex = name.indexOf(" "); + + //Get full name + name = name.substring(0, firstSpaceIndex); + + name = this.ParseSQLServerName(name, true); + } else { + //Get delimiter of column name + var firstSpaceIndex = name.indexOf(" "); + + //Get full name + name = name.substring(0, firstSpaceIndex); + } + + //Create Property + var propertyModel = this.CreateProperty( + name, + currentTableModel.Name, + null, + false + ); + + //Add Property to table + currentTableModel.Properties.push(propertyModel); + } + + //Parse Primary Key + if ( + propertyType === "primary key" || + propertyType === "constrain primary key" + ) { + if (!this.MODE_SQLSERVER) { + var primaryKey = name.replace("PRIMARY KEY (", "").replace(")", ""); + + //Create Primary Key + var primaryKeyModel = this.CreatePrimaryKey( + primaryKey, + currentTableModel.Name + ); + + //Add Primary Key to List + this.primaryKeyList.push(primaryKeyModel); + } else { + var start = i + 2; + var end = 0; + if ( + name.indexOf("PRIMARY KEY") !== -1 && + name.indexOf("CLUSTERED") === -1 + ) { + var primaryKey = name + .replace("PRIMARY KEY (", "") + .replace(")", ""); + + //Create Primary Key + var primaryKeyModel = this.CreatePrimaryKey( + primaryKey, + currentTableModel.Name + ); + + //Add Primary Key to List + this.primaryKeyList.push(primaryKeyModel); + } else { + while (end === 0) { + var primaryKeyRow = lines[start].trim(); + + if (primaryKeyRow.indexOf(")") !== -1) { + end = 1; + break; + } + + start++; + + primaryKeyRow = primaryKeyRow.replace("ASC", ""); + + //Parse name + primaryKeyRow = this.ParseSQLServerName(primaryKeyRow, true); + + //Create Primary Key + var primaryKeyModel = this.CreatePrimaryKey( + primaryKeyRow, + currentTableModel.Name + ); + + //Add Primary Key to List + this.primaryKeyList.push(primaryKeyModel); + } } + } + } + debugger; + //Parse Foreign Key + if ( + propertyType === "foreign key" || + propertyType === "constrain foreign key" + ) { + if (!this.MODE_SQLSERVER) { + this.ParseMySQLForeignKey(name, currentTableModel); } else { - this.quoted = char; + var completeRow = name; + + if (name.indexOf("REFERENCES") === -1) { + var referencesRow = lines[i + 1].trim(); + completeRow = + "ALTER TABLE [dbo].[" + + currentTableModel.Name + + "] WITH CHECK ADD" + + " " + + name + + " " + + referencesRow; + } + + this.ParseSQLServerForeignKey(completeRow, currentTableModel); } - } else if (char === ";" && !this.quoted) { - const statement = - this.remains + parsed.substr(lastStatementIndex, i + 1); - this.statements.push(statement); - this.remains = ""; - lastStatementIndex = i + 1; } + } else if (propertyRow === "alter table ") { + debugger; + if (this.MODE_SQLSERVER) { + //Parse the row + var alterTableRow = tmp.substring( + 0, + tmp.charAt(tmp.length - 1) === "," ? tmp.length - 1 : tmp.length + ); + var referencesRow = lines[i + 1].trim(); + var completeRow = alterTableRow + " " + referencesRow; - this.escaped = false; + this.ParseSQLServerForeignKey(completeRow, currentTableModel); + } } } - this.remains += parsed.substr(lastStatementIndex); return this; } + private CreatePrimaryKey( + primaryKeyName: string, + primaryKeyTableName: string + ) { + var primaryKey: PrimaryKeyModel = { + PrimaryKeyTableName: primaryKeyTableName, + PrimaryKeyName: primaryKeyName, + }; + + return primaryKey; + } + + private CreateProperty( + name: string, + tableName: string, + foreignKey: ForeignKeyModel[] | null, + isPrimaryKey: boolean + ) { + var isForeignKey = foreignKey !== undefined && foreignKey !== null; + var property: PropertyModel = { + Name: name, + TableName: tableName, + ForeignKey: isForeignKey ? foreignKey : [], + IsForeignKey: isForeignKey, + IsPrimaryKey: isPrimaryKey, + }; + + return property; + } + + private ParseMySQLForeignKey(name: string, currentTableModel: TableModel) { + var referencesIndex = name.toLowerCase().indexOf("references"); + var foreignKeySQL = name.substring(0, referencesIndex); + var referencesSQL = name.substring(referencesIndex, name.length); + + //Remove references syntax + referencesSQL = referencesSQL.replace("REFERENCES ", ""); + + //Get Table and Property Index + var referencedTableIndex = referencesSQL.indexOf("("); + var referencedPropertyIndex = referencesSQL.indexOf(")"); + + //Get Referenced Table + var referencedTableName = referencesSQL.substring(0, referencedTableIndex); + + //Get Referenced Key + var referencedPropertyName = referencesSQL.substring( + referencedTableIndex + 1, + referencedPropertyIndex + ); + + //Get ForeignKey + var foreignKey = foreignKeySQL + .replace("FOREIGN KEY (", "") + .replace(")", "") + .replace(" ", ""); + + //Create ForeignKey + var foreignKeyOriginModel = this.CreateForeignKey( + foreignKey, + currentTableModel.Name, + referencedPropertyName, + referencedTableName, + true + ); + + //Add ForeignKey Origin + this.foreignKeyList.push(foreignKeyOriginModel); + + //Create ForeignKey + var foreignKeyDestinationModel = this.CreateForeignKey( + referencedPropertyName, + referencedTableName, + foreignKey, + currentTableModel.Name, + false + ); + + //Add ForeignKey Destination + this.foreignKeyList.push(foreignKeyDestinationModel); + } + + private ParseSQLServerForeignKey( + name: string, + currentTableModel: TableModel | null + ) { + var referencesIndex = name.toLowerCase().indexOf("references"); + + if (name.toLowerCase().indexOf("foreign key(") !== -1) { + var foreignKeySQL = name + .substring(name.toLowerCase().indexOf("foreign key("), referencesIndex) + .replace("FOREIGN KEY(", "") + .replace(")", ""); + } else { + var foreignKeySQL = name + .substring(name.toLowerCase().indexOf("foreign key ("), referencesIndex) + .replace("FOREIGN KEY (", "") + .replace(")", ""); + } + + var referencesSQL = name.substring(referencesIndex, name.length); + var alterTableName = name + .substring(0, name.indexOf("WITH")) + .replace("ALTER TABLE ", ""); + + if ( + referencesIndex !== -1 && + alterTableName !== "" && + foreignKeySQL !== "" && + referencesSQL !== "" + ) { + //Remove references syntax + referencesSQL = referencesSQL.replace("REFERENCES ", ""); + + //Get Table and Property Index + var referencedTableIndex = referencesSQL.indexOf("("); + var referencedPropertyIndex = referencesSQL.indexOf(")"); + + //Get Referenced Table + var referencedTableName = referencesSQL.substring( + 0, + referencedTableIndex + ); + + //Parse Name + referencedTableName = this.ParseSQLServerName(referencedTableName); + + //Get Referenced Key + var referencedPropertyName = referencesSQL.substring( + referencedTableIndex + 1, + referencedPropertyIndex + ); + + //Parse Name + referencedPropertyName = this.ParseSQLServerName(referencedPropertyName); + + //Get ForeignKey + var foreignKey = foreignKeySQL + .replace("FOREIGN KEY (", "") + .replace(")", ""); + + //Parse Name + foreignKey = this.ParseSQLServerName(foreignKey); + + //Parse Name + alterTableName = this.ParseSQLServerName(alterTableName); + + //Create ForeignKey + var foreignKeyOriginModel = this.CreateForeignKey( + foreignKey, + alterTableName, + referencedPropertyName, + referencedTableName, + true + ); + + //Add ForeignKey Origin + this.foreignKeyList.push(foreignKeyOriginModel); + + //Create ForeignKey + var foreignKeyDestinationModel = this.CreateForeignKey( + referencedPropertyName, + referencedTableName, + foreignKey, + alterTableName, + false + ); + + //Add ForeignKey Destination + this.foreignKeyList.push(foreignKeyDestinationModel); + } + } + + private CreateForeignKey( + primaryKeyName: string, + primaryKeyTableName: string, + referencesPropertyName: string, + referencesTableName: string, + isDestination: boolean + ) { + var foreignKey: ForeignKeyModel = { + PrimaryKeyTableName: primaryKeyTableName, + PrimaryKeyName: primaryKeyName, + ReferencesPropertyName: referencesPropertyName, + ReferencesTableName: referencesTableName, + IsDestination: + isDestination !== undefined && isDestination !== null + ? isDestination + : false, + }; + + return foreignKey; + } + + private ParseTableName(name: string) { + if (name.charAt(name.length - 1) === "(") { + if (!this.MODE_SQLSERVER) { + name = name.substring(0, name.lastIndexOf(" ")); + } else { + name = this.ParseSQLServerName(name); + } + } + + return name; + } + + private ParseSQLServerName(name: string, property?: boolean) { + name = name.replace("[dbo].[", ""); + name = name.replace("](", ""); + name = name.replace("].[", "."); + name = name.replace("[", ""); + + if (property == undefined || property == null) { + name = name.replace(" [", ""); + name = name.replace("] ", ""); + } else { + if (name.indexOf("]") !== -1) { + name = name.substring(0, name.indexOf("]")); + } + } + + if (name.lastIndexOf("]") === name.length - 1) { + name = name.substring(0, name.length - 1); + } + + if (name.lastIndexOf(")") === name.length - 1) { + name = name.substring(0, name.length - 1); + } + + if (name.lastIndexOf("(") === name.length - 1) { + name = name.substring(0, name.length - 1); + } + + name = name.replace(" ", ""); + + return name; + } + + private CreateTable(name: string) { + var table: TableModel = { + Name: name, + Properties: [], + }; + + //Count exported tables + this.exportedTables++; + + return table; + } + /** * Checks whether character is a quotation character. * @@ -90,7 +550,12 @@ export class SqlSimpleParser { return char === '"' || char === "'" || char === "`"; } - ToModel(): any { - return {}; + ToModel(): DatabaseModel { + return { + TableList: this.tableList, + Dialect: this.dialect, + ForeignKeyList: this.foreignKeyList, + PrimaryKeyList: this.primaryKeyList + }; } } diff --git a/src/types/index.ts b/src/types/index.ts index 06f04a1..2ca0889 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,6 +1,33 @@ +export interface DatabaseModel { + TableList: TableModel[]; + Dialect: string; + ForeignKeyList: ForeignKeyModel[]; + PrimaryKeyList: PrimaryKeyModel[]; +} +export interface TableModel { + Name: string; + Properties: PropertyModel[]; +} -export interface DbEntity { - name: string; - -} \ No newline at end of file +export interface PropertyModel { + Name: string; + Value?: string; + TableName: string; + ForeignKey: ForeignKeyModel[] | null; + IsPrimaryKey: boolean; + IsForeignKey: boolean; +} + +export interface ForeignKeyModel { + PrimaryKeyName: string; + ReferencesPropertyName: any; + PrimaryKeyTableName: string; + ReferencesTableName: string; + IsDestination: boolean; +} + +export interface PrimaryKeyModel { + PrimaryKeyName: string; + PrimaryKeyTableName: string; +}